PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/workshop/renderer.php

https://bitbucket.org/bmbrands/swets
PHP | 915 lines | 596 code | 129 blank | 190 comment | 106 complexity | 775243fff7a101c48bda45f1d9b7dbc3 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, GPL-2.0, LGPL-2.1, BSD-3-Clause, AGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Workshop module renderering methods are defined here
  18. *
  19. * @package mod
  20. * @subpackage workshop
  21. * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * Workshop module renderer class
  27. *
  28. * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. class mod_workshop_renderer extends plugin_renderer_base {
  32. ////////////////////////////////////////////////////////////////////////////
  33. // External API - methods to render workshop renderable components
  34. ////////////////////////////////////////////////////////////////////////////
  35. /**
  36. * Renders workshop message
  37. *
  38. * @param workshop_message $message to display
  39. * @return string html code
  40. */
  41. protected function render_workshop_message(workshop_message $message) {
  42. $text = $message->get_message();
  43. $url = $message->get_action_url();
  44. $label = $message->get_action_label();
  45. if (empty($text) and empty($label)) {
  46. return '';
  47. }
  48. switch ($message->get_type()) {
  49. case workshop_message::TYPE_OK:
  50. $sty = 'ok';
  51. break;
  52. case workshop_message::TYPE_ERROR:
  53. $sty = 'error';
  54. break;
  55. default:
  56. $sty = 'info';
  57. }
  58. $o = html_writer::tag('span', $message->get_message());
  59. if (!is_null($url) and !is_null($label)) {
  60. $o .= $this->output->single_button($url, $label, 'get');
  61. }
  62. return $this->output->container($o, array('message', $sty));
  63. }
  64. /**
  65. * Renders full workshop submission
  66. *
  67. * @param workshop_submission $submission
  68. * @return string HTML
  69. */
  70. protected function render_workshop_submission(workshop_submission $submission) {
  71. $o = ''; // output HTML code
  72. $anonymous = $submission->is_anonymous();
  73. $classes = 'submission-full';
  74. if ($anonymous) {
  75. $classes .= ' anonymous';
  76. }
  77. $o .= $this->output->container_start($classes);
  78. $o .= $this->output->container_start('header');
  79. $title = format_string($submission->title);
  80. if ($this->page->url != $submission->url) {
  81. $title = html_writer::link($submission->url, $title);
  82. }
  83. $o .= $this->output->heading($title, 3, 'title');
  84. if (!$anonymous) {
  85. $author = new stdclass();
  86. $author->id = $submission->authorid;
  87. $author->firstname = $submission->authorfirstname;
  88. $author->lastname = $submission->authorlastname;
  89. $author->picture = $submission->authorpicture;
  90. $author->imagealt = $submission->authorimagealt;
  91. $author->email = $submission->authoremail;
  92. $userpic = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 64));
  93. $userurl = new moodle_url('/user/view.php',
  94. array('id' => $author->id, 'course' => $this->page->course->id));
  95. $a = new stdclass();
  96. $a->name = fullname($author);
  97. $a->url = $userurl->out();
  98. $byfullname = get_string('byfullname', 'workshop', $a);
  99. $oo = $this->output->container($userpic, 'picture');
  100. $oo .= $this->output->container($byfullname, 'fullname');
  101. $o .= $this->output->container($oo, 'author');
  102. }
  103. $created = get_string('userdatecreated', 'workshop', userdate($submission->timecreated));
  104. $o .= $this->output->container($created, 'userdate created');
  105. if ($submission->timemodified > $submission->timecreated) {
  106. $modified = get_string('userdatemodified', 'workshop', userdate($submission->timemodified));
  107. $o .= $this->output->container($modified, 'userdate modified');
  108. }
  109. $o .= $this->output->container_end(); // end of header
  110. $content = format_text($submission->content, $submission->contentformat, array('overflowdiv'=>true));
  111. $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $this->page->context->id,
  112. 'mod_workshop', 'submission_content', $submission->id);
  113. $o .= $this->output->container($content, 'content');
  114. $o .= $this->helper_submission_attachments($submission->id, 'html');
  115. $o .= $this->output->container_end(); // end of submission-full
  116. return $o;
  117. }
  118. /**
  119. * Renders short summary of the submission
  120. *
  121. * @param workshop_submission_summary $summary
  122. * @return string text to be echo'ed
  123. */
  124. protected function render_workshop_submission_summary(workshop_submission_summary $summary) {
  125. $o = ''; // output HTML code
  126. $anonymous = $summary->is_anonymous();
  127. $classes = 'submission-summary';
  128. if ($anonymous) {
  129. $classes .= ' anonymous';
  130. }
  131. $gradestatus = '';
  132. if ($summary->status == 'notgraded') {
  133. $classes .= ' notgraded';
  134. $gradestatus = $this->output->container(get_string('nogradeyet', 'workshop'), 'grade-status');
  135. } else if ($summary->status == 'graded') {
  136. $classes .= ' graded';
  137. $gradestatus = $this->output->container(get_string('alreadygraded', 'workshop'), 'grade-status');
  138. }
  139. $o .= $this->output->container_start($classes); // main wrapper
  140. $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
  141. if (!$anonymous) {
  142. $author = new stdClass();
  143. $author->id = $summary->authorid;
  144. $author->firstname = $summary->authorfirstname;
  145. $author->lastname = $summary->authorlastname;
  146. $author->picture = $summary->authorpicture;
  147. $author->imagealt = $summary->authorimagealt;
  148. $author->email = $summary->authoremail;
  149. $userpic = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 35));
  150. $userurl = new moodle_url('/user/view.php',
  151. array('id' => $author->id, 'course' => $this->page->course->id));
  152. $a = new stdClass();
  153. $a->name = fullname($author);
  154. $a->url = $userurl->out();
  155. $byfullname = get_string('byfullname', 'workshop', $a);
  156. $oo = $this->output->container($userpic, 'picture');
  157. $oo .= $this->output->container($byfullname, 'fullname');
  158. $o .= $this->output->container($oo, 'author');
  159. }
  160. $created = get_string('userdatecreated', 'workshop', userdate($summary->timecreated));
  161. $o .= $this->output->container($created, 'userdate created');
  162. if ($summary->timemodified > $summary->timecreated) {
  163. $modified = get_string('userdatemodified', 'workshop', userdate($summary->timemodified));
  164. $o .= $this->output->container($modified, 'userdate modified');
  165. }
  166. $o .= $gradestatus;
  167. $o .= $this->output->container_end(); // end of the main wrapper
  168. return $o;
  169. }
  170. /**
  171. * Renders full workshop example submission
  172. *
  173. * @param workshop_example_submission $example
  174. * @return string HTML
  175. */
  176. protected function render_workshop_example_submission(workshop_example_submission $example) {
  177. $o = ''; // output HTML code
  178. $classes = 'submission-full example';
  179. $o .= $this->output->container_start($classes);
  180. $o .= $this->output->container_start('header');
  181. $o .= $this->output->heading(format_string($example->title), 3, 'title');
  182. $o .= $this->output->container_end(); // end of header
  183. $content = format_text($example->content, $example->contentformat, array('overflowdiv'=>true));
  184. $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $this->page->context->id,
  185. 'mod_workshop', 'submission_content', $example->id);
  186. $o .= $this->output->container($content, 'content');
  187. $o .= $this->helper_submission_attachments($example->id, 'html');
  188. $o .= $this->output->container_end(); // end of submission-full
  189. return $o;
  190. }
  191. /**
  192. * Renders short summary of the example submission
  193. *
  194. * @param workshop_example_submission_summary $summary
  195. * @return string text to be echo'ed
  196. */
  197. protected function render_workshop_example_submission_summary(workshop_example_submission_summary $summary) {
  198. $o = ''; // output HTML code
  199. // wrapping box
  200. $o .= $this->output->box_start('generalbox example-summary ' . $summary->status);
  201. // title
  202. $o .= $this->output->container_start('example-title');
  203. $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
  204. if ($summary->editable) {
  205. $o .= $this->output->action_icon($summary->editurl, new pix_icon('i/edit', get_string('edit')));
  206. }
  207. $o .= $this->output->container_end();
  208. // additional info
  209. if ($summary->status == 'notgraded') {
  210. $o .= $this->output->container(get_string('nogradeyet', 'workshop'), 'example-info nograde');
  211. } else {
  212. $o .= $this->output->container(get_string('gradeinfo', 'workshop' , $summary->gradeinfo), 'example-info grade');
  213. }
  214. // button to assess
  215. $button = new single_button($summary->assessurl, $summary->assesslabel, 'get');
  216. $o .= $this->output->container($this->output->render($button), 'example-actions');
  217. // end of wrapping box
  218. $o .= $this->output->box_end();
  219. return $o;
  220. }
  221. /**
  222. * Renders the user plannner tool
  223. *
  224. * @param workshop_user_plan $plan prepared for the user
  225. * @return string html code to be displayed
  226. */
  227. protected function render_workshop_user_plan(workshop_user_plan $plan) {
  228. $table = new html_table();
  229. $table->attributes['class'] = 'userplan';
  230. $table->head = array();
  231. $table->colclasses = array();
  232. $row = new html_table_row();
  233. $row->attributes['class'] = 'phasetasks';
  234. foreach ($plan->phases as $phasecode => $phase) {
  235. $title = html_writer::tag('span', $phase->title);
  236. $actions = '';
  237. foreach ($phase->actions as $action) {
  238. switch ($action->type) {
  239. case 'switchphase':
  240. $actions .= $this->output->action_icon($action->url, new pix_icon('i/marker', get_string('switchphase', 'workshop')));
  241. break;
  242. }
  243. }
  244. if (!empty($actions)) {
  245. $actions = $this->output->container($actions, 'actions');
  246. }
  247. $table->head[] = $this->output->container($title . $actions);
  248. $classes = 'phase' . $phasecode;
  249. if ($phase->active) {
  250. $classes .= ' active';
  251. } else {
  252. $classes .= ' nonactive';
  253. }
  254. $table->colclasses[] = $classes;
  255. $cell = new html_table_cell();
  256. $cell->text = $this->helper_user_plan_tasks($phase->tasks);
  257. $row->cells[] = $cell;
  258. }
  259. $table->data = array($row);
  260. return html_writer::table($table);
  261. }
  262. /**
  263. * Renders the result of the submissions allocation process
  264. *
  265. * @param workshop_allocation_init_result
  266. * @return string html to be echoed
  267. */
  268. protected function render_workshop_allocation_init_result(workshop_allocation_init_result $result) {
  269. // start with the message
  270. $o = $this->render($result->get_message());
  271. // display the details about the process if available
  272. $info = $result->get_info();
  273. if (is_array($info) and !empty($info)) {
  274. $o .= html_writer::start_tag('ul', array('class' => 'allocation-init-results'));
  275. foreach ($info as $message) {
  276. $parts = explode('::', $message);
  277. $text = array_pop($parts);
  278. $class = implode(' ', $parts);
  279. if (in_array('debug', $parts) && !debugging('', DEBUG_DEVELOPER)) {
  280. // do not display allocation debugging messages
  281. continue;
  282. }
  283. $o .= html_writer::tag('li', $text, array('class' => $class)) . "\n";
  284. }
  285. $o .= html_writer::end_tag('ul');
  286. }
  287. $o .= $this->output->continue_button($result->get_continue_url());
  288. return $o;
  289. }
  290. /**
  291. * Renders the workshop grading report
  292. *
  293. * @param workshop_grading_report $gradingreport
  294. * @return string html code
  295. */
  296. protected function render_workshop_grading_report(workshop_grading_report $gradingreport) {
  297. $data = $gradingreport->get_data();
  298. $options = $gradingreport->get_options();
  299. $grades = $data->grades;
  300. $userinfo = $data->userinfo;
  301. if (empty($grades)) {
  302. return '';
  303. }
  304. $table = new html_table();
  305. $table->attributes['class'] = 'grading-report';
  306. $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'), 'firstname', $options->sortby, $options->sorthow);
  307. $sortbylastname = $this->helper_sortable_heading(get_string('lastname'), 'lastname', $options->sortby, $options->sorthow);
  308. if (self::fullname_format() == 'lf') {
  309. $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
  310. } else {
  311. $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
  312. }
  313. $table->head = array();
  314. $table->head[] = $sortbyname;
  315. $table->head[] = $this->helper_sortable_heading(get_string('submission', 'workshop'), 'submissiontitle',
  316. $options->sortby, $options->sorthow);
  317. $table->head[] = $this->helper_sortable_heading(get_string('receivedgrades', 'workshop'));
  318. if ($options->showsubmissiongrade) {
  319. $table->head[] = $this->helper_sortable_heading(get_string('submissiongradeof', 'workshop', $data->maxgrade),
  320. 'submissiongrade', $options->sortby, $options->sorthow);
  321. }
  322. $table->head[] = $this->helper_sortable_heading(get_string('givengrades', 'workshop'));
  323. if ($options->showgradinggrade) {
  324. $table->head[] = $this->helper_sortable_heading(get_string('gradinggradeof', 'workshop', $data->maxgradinggrade),
  325. 'gradinggrade', $options->sortby, $options->sorthow);
  326. }
  327. $table->rowclasses = array();
  328. $table->colclasses = array();
  329. $table->data = array();
  330. foreach ($grades as $participant) {
  331. $numofreceived = count($participant->reviewedby);
  332. $numofgiven = count($participant->reviewerof);
  333. $published = $participant->submissionpublished;
  334. // compute the number of <tr> table rows needed to display this participant
  335. if ($numofreceived > 0 and $numofgiven > 0) {
  336. $numoftrs = workshop::lcm($numofreceived, $numofgiven);
  337. $spanreceived = $numoftrs / $numofreceived;
  338. $spangiven = $numoftrs / $numofgiven;
  339. } elseif ($numofreceived == 0 and $numofgiven > 0) {
  340. $numoftrs = $numofgiven;
  341. $spanreceived = $numoftrs;
  342. $spangiven = $numoftrs / $numofgiven;
  343. } elseif ($numofreceived > 0 and $numofgiven == 0) {
  344. $numoftrs = $numofreceived;
  345. $spanreceived = $numoftrs / $numofreceived;
  346. $spangiven = $numoftrs;
  347. } else {
  348. $numoftrs = 1;
  349. $spanreceived = 1;
  350. $spangiven = 1;
  351. }
  352. for ($tr = 0; $tr < $numoftrs; $tr++) {
  353. $row = new html_table_row();
  354. if ($published) {
  355. $row->attributes['class'] = 'published';
  356. }
  357. // column #1 - participant - spans over all rows
  358. if ($tr == 0) {
  359. $cell = new html_table_cell();
  360. $cell->text = $this->helper_grading_report_participant($participant, $userinfo);
  361. $cell->rowspan = $numoftrs;
  362. $cell->attributes['class'] = 'participant';
  363. $row->cells[] = $cell;
  364. }
  365. // column #2 - submission - spans over all rows
  366. if ($tr == 0) {
  367. $cell = new html_table_cell();
  368. $cell->text = $this->helper_grading_report_submission($participant);
  369. $cell->rowspan = $numoftrs;
  370. $cell->attributes['class'] = 'submission';
  371. $row->cells[] = $cell;
  372. }
  373. // column #3 - received grades
  374. if ($tr % $spanreceived == 0) {
  375. $idx = intval($tr / $spanreceived);
  376. $assessment = self::array_nth($participant->reviewedby, $idx);
  377. $cell = new html_table_cell();
  378. $cell->text = $this->helper_grading_report_assessment($assessment, $options->showreviewernames, $userinfo,
  379. get_string('gradereceivedfrom', 'workshop'));
  380. $cell->rowspan = $spanreceived;
  381. $cell->attributes['class'] = 'receivedgrade';
  382. if (is_null($assessment) or is_null($assessment->grade)) {
  383. $cell->attributes['class'] .= ' null';
  384. } else {
  385. $cell->attributes['class'] .= ' notnull';
  386. }
  387. $row->cells[] = $cell;
  388. }
  389. // column #4 - total grade for submission
  390. if ($options->showsubmissiongrade and $tr == 0) {
  391. $cell = new html_table_cell();
  392. $cell->text = $this->helper_grading_report_grade($participant->submissiongrade, $participant->submissiongradeover);
  393. $cell->rowspan = $numoftrs;
  394. $cell->attributes['class'] = 'submissiongrade';
  395. $row->cells[] = $cell;
  396. }
  397. // column #5 - given grades
  398. if ($tr % $spangiven == 0) {
  399. $idx = intval($tr / $spangiven);
  400. $assessment = self::array_nth($participant->reviewerof, $idx);
  401. $cell = new html_table_cell();
  402. $cell->text = $this->helper_grading_report_assessment($assessment, $options->showauthornames, $userinfo,
  403. get_string('gradegivento', 'workshop'));
  404. $cell->rowspan = $spangiven;
  405. $cell->attributes['class'] = 'givengrade';
  406. if (is_null($assessment) or is_null($assessment->grade)) {
  407. $cell->attributes['class'] .= ' null';
  408. } else {
  409. $cell->attributes['class'] .= ' notnull';
  410. }
  411. $row->cells[] = $cell;
  412. }
  413. // column #6 - total grade for assessment
  414. if ($options->showgradinggrade and $tr == 0) {
  415. $cell = new html_table_cell();
  416. $cell->text = $this->helper_grading_report_grade($participant->gradinggrade);
  417. $cell->rowspan = $numoftrs;
  418. $cell->attributes['class'] = 'gradinggrade';
  419. $row->cells[] = $cell;
  420. }
  421. $table->data[] = $row;
  422. }
  423. }
  424. return html_writer::table($table);
  425. }
  426. /**
  427. * Renders the feedback for the author of the submission
  428. *
  429. * @param workshop_feedback_author $feedback
  430. * @return string HTML
  431. */
  432. protected function render_workshop_feedback_author(workshop_feedback_author $feedback) {
  433. return $this->helper_render_feedback($feedback);
  434. }
  435. /**
  436. * Renders the feedback for the reviewer of the submission
  437. *
  438. * @param workshop_feedback_reviewer $feedback
  439. * @return string HTML
  440. */
  441. protected function render_workshop_feedback_reviewer(workshop_feedback_reviewer $feedback) {
  442. return $this->helper_render_feedback($feedback);
  443. }
  444. /**
  445. * Helper method to rendering feedback
  446. *
  447. * @param workshop_feedback_author|workshop_feedback_reviewer $feedback
  448. * @return string HTML
  449. */
  450. private function helper_render_feedback($feedback) {
  451. $o = ''; // output HTML code
  452. $o .= $this->output->container_start('feedback feedbackforauthor');
  453. $o .= $this->output->container_start('header');
  454. $o .= $this->output->heading(get_string('feedbackby', 'workshop', s(fullname($feedback->get_provider()))), 3, 'title');
  455. $userpic = $this->output->user_picture($feedback->get_provider(), array('courseid' => $this->page->course->id, 'size' => 32));
  456. $o .= $this->output->container($userpic, 'picture');
  457. $o .= $this->output->container_end(); // end of header
  458. $content = format_text($feedback->get_content(), $feedback->get_format(), array('overflowdiv' => true));
  459. $o .= $this->output->container($content, 'content');
  460. $o .= $this->output->container_end();
  461. return $o;
  462. }
  463. /**
  464. * Renders the full assessment
  465. *
  466. * @param workshop_assessment $assessment
  467. * @return string HTML
  468. */
  469. protected function render_workshop_assessment(workshop_assessment $assessment) {
  470. $o = ''; // output HTML code
  471. $anonymous = is_null($assessment->reviewer);
  472. $classes = 'assessment-full';
  473. if ($anonymous) {
  474. $classes .= ' anonymous';
  475. }
  476. $o .= $this->output->container_start($classes);
  477. $o .= $this->output->container_start('header');
  478. if (!empty($assessment->title)) {
  479. $title = s($assessment->title);
  480. } else {
  481. $title = get_string('assessment', 'workshop');
  482. }
  483. if ($this->page->url != $assessment->url) {
  484. $o .= $this->output->container(html_writer::link($assessment->url, $title), 'title');
  485. } else {
  486. $o .= $this->output->container($title, 'title');
  487. }
  488. if (!$anonymous) {
  489. $reviewer = $assessment->reviewer;
  490. $userpic = $this->output->user_picture($reviewer, array('courseid' => $this->page->course->id, 'size' => 32));
  491. $userurl = new moodle_url('/user/view.php',
  492. array('id' => $reviewer->id, 'course' => $this->page->course->id));
  493. $a = new stdClass();
  494. $a->name = fullname($reviewer);
  495. $a->url = $userurl->out();
  496. $byfullname = get_string('assessmentby', 'workshop', $a);
  497. $oo = $this->output->container($userpic, 'picture');
  498. $oo .= $this->output->container($byfullname, 'fullname');
  499. $o .= $this->output->container($oo, 'reviewer');
  500. }
  501. if (is_null($assessment->realgrade)) {
  502. $o .= $this->output->container(
  503. get_string('notassessed', 'workshop'),
  504. 'grade nograde'
  505. );
  506. } else {
  507. $a = new stdClass();
  508. $a->max = $assessment->maxgrade;
  509. $a->received = $assessment->realgrade;
  510. $o .= $this->output->container(
  511. get_string('gradeinfo', 'workshop', $a),
  512. 'grade'
  513. );
  514. if (!is_null($assessment->weight) and $assessment->weight != 1) {
  515. $o .= $this->output->container(
  516. get_string('weightinfo', 'workshop', $assessment->weight),
  517. 'weight'
  518. );
  519. }
  520. }
  521. $o .= $this->output->container_start('actions');
  522. foreach ($assessment->actions as $action) {
  523. $o .= $this->output->single_button($action->url, $action->label, $action->method);
  524. }
  525. $o .= $this->output->container_end(); // actions
  526. $o .= $this->output->container_end(); // header
  527. if (!is_null($assessment->form)) {
  528. $o .= print_collapsible_region_start('assessment-form-wrapper', uniqid('workshop-assessment'),
  529. get_string('assessmentform', 'workshop'), '', false, true);
  530. $o .= $this->output->container(self::moodleform($assessment->form), 'assessment-form');
  531. $o .= print_collapsible_region_end(true);
  532. }
  533. $o .= $this->output->container_end(); // main wrapper
  534. return $o;
  535. }
  536. ////////////////////////////////////////////////////////////////////////////
  537. // Internal rendering helper methods
  538. ////////////////////////////////////////////////////////////////////////////
  539. /**
  540. * Renders a list of files attached to the submission
  541. *
  542. * If format==html, then format a html string. If format==text, then format a text-only string.
  543. * Otherwise, returns html for non-images and html to display the image inline.
  544. *
  545. * @param int $submissionid submission identifier
  546. * @param string format the format of the returned string - html|text
  547. * @return string formatted text to be echoed
  548. */
  549. protected function helper_submission_attachments($submissionid, $format = 'html') {
  550. global $CFG;
  551. require_once($CFG->libdir.'/filelib.php');
  552. $fs = get_file_storage();
  553. $ctx = $this->page->context;
  554. $files = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid);
  555. $outputimgs = ''; // images to be displayed inline
  556. $outputfiles = ''; // list of attachment files
  557. foreach ($files as $file) {
  558. if ($file->is_directory()) {
  559. continue;
  560. }
  561. $filepath = $file->get_filepath();
  562. $filename = $file->get_filename();
  563. $fileurl = file_encode_url($CFG->wwwroot . '/pluginfile.php',
  564. '/' . $ctx->id . '/mod_workshop/submission_attachment/' . $submissionid . $filepath . $filename, true);
  565. $type = $file->get_mimetype();
  566. $type = mimeinfo_from_type('type', $type);
  567. $image = html_writer::empty_tag('img', array('src'=>$this->output->pix_url(file_mimetype_icon($type)), 'alt'=>$type, 'class'=>'icon'));
  568. $linkhtml = html_writer::link($fileurl, $image) . substr($filepath, 1) . html_writer::link($fileurl, $filename);
  569. $linktxt = "$filename [$fileurl]";
  570. if ($format == 'html') {
  571. if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) {
  572. $preview = html_writer::empty_tag('img', array('src' => $fileurl, 'alt' => '', 'class' => 'preview'));
  573. $preview = html_writer::tag('a', $preview, array('href' => $fileurl));
  574. $outputimgs .= $this->output->container($preview);
  575. } else {
  576. $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type));
  577. }
  578. } else if ($format == 'text') {
  579. $outputfiles .= $linktxt . PHP_EOL;
  580. }
  581. }
  582. if ($format == 'html') {
  583. if ($outputimgs) {
  584. $outputimgs = $this->output->container($outputimgs, 'images');
  585. }
  586. if ($outputfiles) {
  587. $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files'));
  588. }
  589. return $this->output->container($outputimgs . $outputfiles, 'attachments');
  590. } else {
  591. return $outputfiles;
  592. }
  593. }
  594. /**
  595. * Renders the tasks for the single phase in the user plan
  596. *
  597. * @param stdClass $tasks
  598. * @return string html code
  599. */
  600. protected function helper_user_plan_tasks(array $tasks) {
  601. $out = '';
  602. foreach ($tasks as $taskcode => $task) {
  603. $classes = '';
  604. $icon = null;
  605. if ($task->completed === true) {
  606. $classes .= ' completed';
  607. } elseif ($task->completed === false) {
  608. $classes .= ' fail';
  609. } elseif ($task->completed === 'info') {
  610. $classes .= ' info';
  611. }
  612. if (is_null($task->link)) {
  613. $title = $task->title;
  614. } else {
  615. $title = html_writer::link($task->link, $task->title);
  616. }
  617. $title = $this->output->container($title, 'title');
  618. $details = $this->output->container($task->details, 'details');
  619. $out .= html_writer::tag('li', $title . $details, array('class' => $classes));
  620. }
  621. if ($out) {
  622. $out = html_writer::tag('ul', $out, array('class' => 'tasks'));
  623. }
  624. return $out;
  625. }
  626. /**
  627. * Renders a text with icons to sort by the given column
  628. *
  629. * This is intended for table headings.
  630. *
  631. * @param string $text The heading text
  632. * @param string $sortid The column id used for sorting
  633. * @param string $sortby Currently sorted by (column id)
  634. * @param string $sorthow Currently sorted how (ASC|DESC)
  635. *
  636. * @return string
  637. */
  638. protected function helper_sortable_heading($text, $sortid=null, $sortby=null, $sorthow=null) {
  639. global $PAGE;
  640. $out = html_writer::tag('span', $text, array('class'=>'text'));
  641. if (!is_null($sortid)) {
  642. if ($sortby !== $sortid or $sorthow !== 'ASC') {
  643. $url = new moodle_url($PAGE->url);
  644. $url->params(array('sortby' => $sortid, 'sorthow' => 'ASC'));
  645. $out .= $this->output->action_icon($url, new pix_icon('t/up', get_string('sortasc', 'workshop')), null, array('class' => 'sort asc'));
  646. }
  647. if ($sortby !== $sortid or $sorthow !== 'DESC') {
  648. $url = new moodle_url($PAGE->url);
  649. $url->params(array('sortby' => $sortid, 'sorthow' => 'DESC'));
  650. $out .= $this->output->action_icon($url, new pix_icon('t/down', get_string('sortdesc', 'workshop')), null, array('class' => 'sort desc'));
  651. }
  652. }
  653. return $out;
  654. }
  655. /**
  656. * @param stdClass $participant
  657. * @param array $userinfo
  658. * @return string
  659. */
  660. protected function helper_grading_report_participant(stdclass $participant, array $userinfo) {
  661. $userid = $participant->userid;
  662. $out = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 35));
  663. $out .= html_writer::tag('span', fullname($userinfo[$userid]));
  664. return $out;
  665. }
  666. /**
  667. * @param stdClass $participant
  668. * @return string
  669. */
  670. protected function helper_grading_report_submission(stdclass $participant) {
  671. global $CFG;
  672. if (is_null($participant->submissionid)) {
  673. $out = $this->output->container(get_string('nosubmissionfound', 'workshop'), 'info');
  674. } else {
  675. $url = new moodle_url('/mod/workshop/submission.php',
  676. array('cmid' => $this->page->context->instanceid, 'id' => $participant->submissionid));
  677. $out = html_writer::link($url, format_string($participant->submissiontitle), array('class'=>'title'));
  678. }
  679. return $out;
  680. }
  681. /**
  682. * @todo Highlight the nulls
  683. * @param stdClass|null $assessment
  684. * @param bool $shownames
  685. * @param string $separator between the grade and the reviewer/author
  686. * @return string
  687. */
  688. protected function helper_grading_report_assessment($assessment, $shownames, array $userinfo, $separator) {
  689. global $CFG;
  690. if (is_null($assessment)) {
  691. return get_string('nullgrade', 'workshop');
  692. }
  693. $a = new stdclass();
  694. $a->grade = is_null($assessment->grade) ? get_string('nullgrade', 'workshop') : $assessment->grade;
  695. $a->gradinggrade = is_null($assessment->gradinggrade) ? get_string('nullgrade', 'workshop') : $assessment->gradinggrade;
  696. $a->weight = $assessment->weight;
  697. // grrr the following logic should really be handled by a future language pack feature
  698. if (is_null($assessment->gradinggradeover)) {
  699. if ($a->weight == 1) {
  700. $grade = get_string('formatpeergrade', 'workshop', $a);
  701. } else {
  702. $grade = get_string('formatpeergradeweighted', 'workshop', $a);
  703. }
  704. } else {
  705. $a->gradinggradeover = $assessment->gradinggradeover;
  706. if ($a->weight == 1) {
  707. $grade = get_string('formatpeergradeover', 'workshop', $a);
  708. } else {
  709. $grade = get_string('formatpeergradeoverweighted', 'workshop', $a);
  710. }
  711. }
  712. $url = new moodle_url('/mod/workshop/assessment.php',
  713. array('asid' => $assessment->assessmentid));
  714. $grade = html_writer::link($url, $grade, array('class'=>'grade'));
  715. if ($shownames) {
  716. $userid = $assessment->userid;
  717. $name = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 16));
  718. $name .= html_writer::tag('span', fullname($userinfo[$userid]), array('class' => 'fullname'));
  719. $name = $separator . html_writer::tag('span', $name, array('class' => 'user'));
  720. } else {
  721. $name = '';
  722. }
  723. return $this->output->container($grade . $name, 'assessmentdetails');
  724. }
  725. /**
  726. * Formats the aggreagated grades
  727. */
  728. protected function helper_grading_report_grade($grade, $over=null) {
  729. $a = new stdclass();
  730. $a->grade = is_null($grade) ? get_string('nullgrade', 'workshop') : $grade;
  731. if (is_null($over)) {
  732. $text = get_string('formataggregatedgrade', 'workshop', $a);
  733. } else {
  734. $a->over = is_null($over) ? get_string('nullgrade', 'workshop') : $over;
  735. $text = get_string('formataggregatedgradeover', 'workshop', $a);
  736. }
  737. return $text;
  738. }
  739. ////////////////////////////////////////////////////////////////////////////
  740. // Static helpers
  741. ////////////////////////////////////////////////////////////////////////////
  742. /**
  743. * Helper method dealing with the fact we can not just fetch the output of moodleforms
  744. *
  745. * @param moodleform $mform
  746. * @return string HTML
  747. */
  748. protected static function moodleform(moodleform $mform) {
  749. ob_start();
  750. $mform->display();
  751. $o = ob_get_contents();
  752. ob_end_clean();
  753. return $o;
  754. }
  755. /**
  756. * Helper function returning the n-th item of the array
  757. *
  758. * @param array $a
  759. * @param int $n from 0 to m, where m is th number of items in the array
  760. * @return mixed the $n-th element of $a
  761. */
  762. protected static function array_nth(array $a, $n) {
  763. $keys = array_keys($a);
  764. if ($n < 0 or $n > count($keys) - 1) {
  765. return null;
  766. }
  767. $key = $keys[$n];
  768. return $a[$key];
  769. }
  770. /**
  771. * Tries to guess the fullname format set at the site
  772. *
  773. * @return string fl|lf
  774. */
  775. protected static function fullname_format() {
  776. $fake = new stdclass(); // fake user
  777. $fake->lastname = 'LLLL';
  778. $fake->firstname = 'FFFF';
  779. $fullname = get_string('fullnamedisplay', '', $fake);
  780. if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
  781. return 'lf';
  782. } else {
  783. return 'fl';
  784. }
  785. }
  786. }