PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/workshop/renderer.php

http://github.com/moodle/moodle
PHP | 1145 lines | 758 code | 163 blank | 224 comment | 142 complexity | f9845b601be629d803de001f0113d790 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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_workshop
  20. * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. /**
  25. * Workshop module renderer class
  26. *
  27. * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29. */
  30. class mod_workshop_renderer extends plugin_renderer_base {
  31. ////////////////////////////////////////////////////////////////////////////
  32. // External API - methods to render workshop renderable components
  33. ////////////////////////////////////////////////////////////////////////////
  34. /**
  35. * Renders workshop message
  36. *
  37. * @param workshop_message $message to display
  38. * @return string html code
  39. */
  40. protected function render_workshop_message(workshop_message $message) {
  41. $text = $message->get_message();
  42. $url = $message->get_action_url();
  43. $label = $message->get_action_label();
  44. if (empty($text) and empty($label)) {
  45. return '';
  46. }
  47. switch ($message->get_type()) {
  48. case workshop_message::TYPE_OK:
  49. $sty = 'ok';
  50. break;
  51. case workshop_message::TYPE_ERROR:
  52. $sty = 'error';
  53. break;
  54. default:
  55. $sty = 'info';
  56. }
  57. $o = html_writer::tag('span', $message->get_message());
  58. if (!is_null($url) and !is_null($label)) {
  59. $o .= $this->output->single_button($url, $label, 'get');
  60. }
  61. return $this->output->container($o, array('message', $sty));
  62. }
  63. /**
  64. * Renders full workshop submission
  65. *
  66. * @param workshop_submission $submission
  67. * @return string HTML
  68. */
  69. protected function render_workshop_submission(workshop_submission $submission) {
  70. global $CFG;
  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. $additionalfields = explode(',', user_picture::fields());
  87. $author = username_load_fields_from_object($author, $submission, 'author', $additionalfields);
  88. $userpic = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 64));
  89. $userurl = new moodle_url('/user/view.php',
  90. array('id' => $author->id, 'course' => $this->page->course->id));
  91. $a = new stdclass();
  92. $a->name = fullname($author);
  93. $a->url = $userurl->out();
  94. $byfullname = get_string('byfullname', 'workshop', $a);
  95. $oo = $this->output->container($userpic, 'picture');
  96. $oo .= $this->output->container($byfullname, 'fullname');
  97. $o .= $this->output->container($oo, 'author');
  98. }
  99. $created = get_string('userdatecreated', 'workshop', userdate($submission->timecreated));
  100. $o .= $this->output->container($created, 'userdate created');
  101. if ($submission->timemodified > $submission->timecreated) {
  102. $modified = get_string('userdatemodified', 'workshop', userdate($submission->timemodified));
  103. $o .= $this->output->container($modified, 'userdate modified');
  104. }
  105. $o .= $this->output->container_end(); // end of header
  106. $content = file_rewrite_pluginfile_urls($submission->content, 'pluginfile.php', $this->page->context->id,
  107. 'mod_workshop', 'submission_content', $submission->id);
  108. $content = format_text($content, $submission->contentformat, array('overflowdiv'=>true));
  109. if (!empty($content)) {
  110. if (!empty($CFG->enableplagiarism)) {
  111. require_once($CFG->libdir.'/plagiarismlib.php');
  112. $content .= plagiarism_get_links(array('userid' => $submission->authorid,
  113. 'content' => $submission->content,
  114. 'cmid' => $this->page->cm->id,
  115. 'course' => $this->page->course));
  116. }
  117. }
  118. $o .= $this->output->container($content, 'content');
  119. $o .= $this->helper_submission_attachments($submission->id, 'html');
  120. $o .= $this->output->container_end(); // end of submission-full
  121. return $o;
  122. }
  123. /**
  124. * Renders short summary of the submission
  125. *
  126. * @param workshop_submission_summary $summary
  127. * @return string text to be echo'ed
  128. */
  129. protected function render_workshop_submission_summary(workshop_submission_summary $summary) {
  130. $o = ''; // output HTML code
  131. $anonymous = $summary->is_anonymous();
  132. $classes = 'submission-summary';
  133. if ($anonymous) {
  134. $classes .= ' anonymous';
  135. }
  136. $gradestatus = '';
  137. if ($summary->status == 'notgraded') {
  138. $classes .= ' notgraded';
  139. $gradestatus = $this->output->container(get_string('nogradeyet', 'workshop'), 'grade-status');
  140. } else if ($summary->status == 'graded') {
  141. $classes .= ' graded';
  142. $gradestatus = $this->output->container(get_string('alreadygraded', 'workshop'), 'grade-status');
  143. }
  144. $o .= $this->output->container_start($classes); // main wrapper
  145. $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
  146. if (!$anonymous) {
  147. $author = new stdClass();
  148. $additionalfields = explode(',', user_picture::fields());
  149. $author = username_load_fields_from_object($author, $summary, 'author', $additionalfields);
  150. $userpic = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 35));
  151. $userurl = new moodle_url('/user/view.php',
  152. array('id' => $author->id, 'course' => $this->page->course->id));
  153. $a = new stdClass();
  154. $a->name = fullname($author);
  155. $a->url = $userurl->out();
  156. $byfullname = get_string('byfullname', 'workshop', $a);
  157. $oo = $this->output->container($userpic, 'picture');
  158. $oo .= $this->output->container($byfullname, 'fullname');
  159. $o .= $this->output->container($oo, 'author');
  160. }
  161. $created = get_string('userdatecreated', 'workshop', userdate($summary->timecreated));
  162. $o .= $this->output->container($created, 'userdate created');
  163. if ($summary->timemodified > $summary->timecreated) {
  164. $modified = get_string('userdatemodified', 'workshop', userdate($summary->timemodified));
  165. $o .= $this->output->container($modified, 'userdate modified');
  166. }
  167. $o .= $gradestatus;
  168. $o .= $this->output->container_end(); // end of the main wrapper
  169. return $o;
  170. }
  171. /**
  172. * Renders full workshop example submission
  173. *
  174. * @param workshop_example_submission $example
  175. * @return string HTML
  176. */
  177. protected function render_workshop_example_submission(workshop_example_submission $example) {
  178. $o = ''; // output HTML code
  179. $classes = 'submission-full example';
  180. $o .= $this->output->container_start($classes);
  181. $o .= $this->output->container_start('header');
  182. $o .= $this->output->container(format_string($example->title), array('class' => 'title'));
  183. $o .= $this->output->container_end(); // end of header
  184. $content = file_rewrite_pluginfile_urls($example->content, 'pluginfile.php', $this->page->context->id,
  185. 'mod_workshop', 'submission_content', $example->id);
  186. $content = format_text($content, $example->contentformat, array('overflowdiv'=>true));
  187. $o .= $this->output->container($content, 'content');
  188. $o .= $this->helper_submission_attachments($example->id, 'html');
  189. $o .= $this->output->container_end(); // end of submission-full
  190. return $o;
  191. }
  192. /**
  193. * Renders short summary of the example submission
  194. *
  195. * @param workshop_example_submission_summary $summary
  196. * @return string text to be echo'ed
  197. */
  198. protected function render_workshop_example_submission_summary(workshop_example_submission_summary $summary) {
  199. $o = ''; // output HTML code
  200. // wrapping box
  201. $o .= $this->output->box_start('generalbox example-summary ' . $summary->status);
  202. // title
  203. $o .= $this->output->container_start('example-title');
  204. $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
  205. if ($summary->editable) {
  206. $o .= $this->output->action_icon($summary->editurl, new pix_icon('i/edit', get_string('edit')));
  207. }
  208. $o .= $this->output->container_end();
  209. // additional info
  210. if ($summary->status == 'notgraded') {
  211. $o .= $this->output->container(get_string('nogradeyet', 'workshop'), 'example-info nograde');
  212. } else {
  213. $o .= $this->output->container(get_string('gradeinfo', 'workshop' , $summary->gradeinfo), 'example-info grade');
  214. }
  215. // button to assess
  216. $button = new single_button($summary->assessurl, $summary->assesslabel, 'get');
  217. $o .= $this->output->container($this->output->render($button), 'example-actions');
  218. // end of wrapping box
  219. $o .= $this->output->box_end();
  220. return $o;
  221. }
  222. /**
  223. * Renders the user plannner tool
  224. *
  225. * @param workshop_user_plan $plan prepared for the user
  226. * @return string html code to be displayed
  227. */
  228. protected function render_workshop_user_plan(workshop_user_plan $plan) {
  229. $table = new html_table();
  230. $table->attributes['class'] = 'userplan';
  231. $table->attributes['role'] = 'section';
  232. $numberofphases = count($plan->phases);
  233. $table->attributes['aria-label'] = get_string('userplanaccessibilitytitle', 'workshop', $numberofphases);
  234. $table->head = array();
  235. $table->colclasses = array();
  236. $row = new html_table_row();
  237. $row->attributes['class'] = 'phasetasks';
  238. foreach ($plan->phases as $phasecode => $phase) {
  239. $title = html_writer::tag('span', $phase->title);
  240. if ($phase->active) {
  241. $title .= ' ' . html_writer::tag('span', get_string('userplancurrentphase', 'workshop'),
  242. array('class' => 'accesshide'));
  243. }
  244. $actions = '';
  245. foreach ($phase->actions as $action) {
  246. switch ($action->type) {
  247. case 'switchphase':
  248. $icon = 'i/marker';
  249. if ($phasecode == workshop::PHASE_ASSESSMENT
  250. and $plan->workshop->phase == workshop::PHASE_SUBMISSION
  251. and $plan->workshop->phaseswitchassessment) {
  252. $icon = 'i/scheduled';
  253. }
  254. $actions .= $this->output->action_icon($action->url, new pix_icon($icon, get_string('switchphase', 'workshop')));
  255. break;
  256. }
  257. }
  258. if (!empty($actions)) {
  259. $actions = $this->output->container($actions, 'actions');
  260. }
  261. $table->head[] = $this->output->container($title . $actions);
  262. $classes = 'phase' . $phasecode;
  263. if ($phase->active) {
  264. $classes .= ' active';
  265. } else {
  266. $classes .= ' nonactive';
  267. }
  268. $table->colclasses[] = $classes;
  269. $cell = new html_table_cell();
  270. $cell->text = $this->helper_user_plan_tasks($phase->tasks);
  271. $row->cells[] = $cell;
  272. }
  273. $table->data = array($row);
  274. return html_writer::table($table);
  275. }
  276. /**
  277. * Renders the result of the submissions allocation process
  278. *
  279. * @param workshop_allocation_result $result as returned by the allocator's init() method
  280. * @return string HTML to be echoed
  281. */
  282. protected function render_workshop_allocation_result(workshop_allocation_result $result) {
  283. global $CFG;
  284. $status = $result->get_status();
  285. if (is_null($status) or $status == workshop_allocation_result::STATUS_VOID) {
  286. debugging('Attempt to render workshop_allocation_result with empty status', DEBUG_DEVELOPER);
  287. return '';
  288. }
  289. switch ($status) {
  290. case workshop_allocation_result::STATUS_FAILED:
  291. if ($message = $result->get_message()) {
  292. $message = new workshop_message($message, workshop_message::TYPE_ERROR);
  293. } else {
  294. $message = new workshop_message(get_string('allocationerror', 'workshop'), workshop_message::TYPE_ERROR);
  295. }
  296. break;
  297. case workshop_allocation_result::STATUS_CONFIGURED:
  298. if ($message = $result->get_message()) {
  299. $message = new workshop_message($message, workshop_message::TYPE_INFO);
  300. } else {
  301. $message = new workshop_message(get_string('allocationconfigured', 'workshop'), workshop_message::TYPE_INFO);
  302. }
  303. break;
  304. case workshop_allocation_result::STATUS_EXECUTED:
  305. if ($message = $result->get_message()) {
  306. $message = new workshop_message($message, workshop_message::TYPE_OK);
  307. } else {
  308. $message = new workshop_message(get_string('allocationdone', 'workshop'), workshop_message::TYPE_OK);
  309. }
  310. break;
  311. default:
  312. throw new coding_exception('Unknown allocation result status', $status);
  313. }
  314. // start with the message
  315. $o = $this->render($message);
  316. // display the details about the process if available
  317. $logs = $result->get_logs();
  318. if (is_array($logs) and !empty($logs)) {
  319. $o .= html_writer::start_tag('ul', array('class' => 'allocation-init-results'));
  320. foreach ($logs as $log) {
  321. if ($log->type == 'debug' and !$CFG->debugdeveloper) {
  322. // display allocation debugging messages for developers only
  323. continue;
  324. }
  325. $class = $log->type;
  326. if ($log->indent) {
  327. $class .= ' indent';
  328. }
  329. $o .= html_writer::tag('li', $log->message, array('class' => $class)).PHP_EOL;
  330. }
  331. $o .= html_writer::end_tag('ul');
  332. }
  333. return $o;
  334. }
  335. /**
  336. * Renders the workshop grading report
  337. *
  338. * @param workshop_grading_report $gradingreport
  339. * @return string html code
  340. */
  341. protected function render_workshop_grading_report(workshop_grading_report $gradingreport) {
  342. $data = $gradingreport->get_data();
  343. $options = $gradingreport->get_options();
  344. $grades = $data->grades;
  345. $userinfo = $data->userinfo;
  346. if (empty($grades)) {
  347. return '';
  348. }
  349. $table = new html_table();
  350. $table->attributes['class'] = 'grading-report';
  351. $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'), 'firstname', $options->sortby, $options->sorthow);
  352. $sortbylastname = $this->helper_sortable_heading(get_string('lastname'), 'lastname', $options->sortby, $options->sorthow);
  353. if (self::fullname_format() == 'lf') {
  354. $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
  355. } else {
  356. $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
  357. }
  358. $sortbysubmisstiontitle = $this->helper_sortable_heading(get_string('submission', 'workshop'), 'submissiontitle',
  359. $options->sortby, $options->sorthow);
  360. $sortbysubmisstionlastmodified = $this->helper_sortable_heading(get_string('submissionlastmodified', 'workshop'),
  361. 'submissionmodified', $options->sortby, $options->sorthow);
  362. $sortbysubmisstion = $sortbysubmisstiontitle . ' / ' . $sortbysubmisstionlastmodified;
  363. $table->head = array();
  364. $table->head[] = $sortbyname;
  365. $table->head[] = $sortbysubmisstion;
  366. // If we are in submission phase ignore the following headers (columns).
  367. if ($options->workshopphase != workshop::PHASE_SUBMISSION) {
  368. $table->head[] = $this->helper_sortable_heading(get_string('receivedgrades', 'workshop'));
  369. if ($options->showsubmissiongrade) {
  370. $table->head[] = $this->helper_sortable_heading(get_string('submissiongradeof', 'workshop', $data->maxgrade),
  371. 'submissiongrade', $options->sortby, $options->sorthow);
  372. }
  373. $table->head[] = $this->helper_sortable_heading(get_string('givengrades', 'workshop'));
  374. if ($options->showgradinggrade) {
  375. $table->head[] = $this->helper_sortable_heading(get_string('gradinggradeof', 'workshop', $data->maxgradinggrade),
  376. 'gradinggrade', $options->sortby, $options->sorthow);
  377. }
  378. }
  379. $table->rowclasses = array();
  380. $table->colclasses = array();
  381. $table->data = array();
  382. foreach ($grades as $participant) {
  383. $numofreceived = count($participant->reviewedby);
  384. $numofgiven = count($participant->reviewerof);
  385. $published = $participant->submissionpublished;
  386. // compute the number of <tr> table rows needed to display this participant
  387. if ($numofreceived > 0 and $numofgiven > 0) {
  388. $numoftrs = workshop::lcm($numofreceived, $numofgiven);
  389. $spanreceived = $numoftrs / $numofreceived;
  390. $spangiven = $numoftrs / $numofgiven;
  391. } elseif ($numofreceived == 0 and $numofgiven > 0) {
  392. $numoftrs = $numofgiven;
  393. $spanreceived = $numoftrs;
  394. $spangiven = $numoftrs / $numofgiven;
  395. } elseif ($numofreceived > 0 and $numofgiven == 0) {
  396. $numoftrs = $numofreceived;
  397. $spanreceived = $numoftrs / $numofreceived;
  398. $spangiven = $numoftrs;
  399. } else {
  400. $numoftrs = 1;
  401. $spanreceived = 1;
  402. $spangiven = 1;
  403. }
  404. for ($tr = 0; $tr < $numoftrs; $tr++) {
  405. $row = new html_table_row();
  406. if ($published) {
  407. $row->attributes['class'] = 'published';
  408. }
  409. // column #1 - participant - spans over all rows
  410. if ($tr == 0) {
  411. $cell = new html_table_cell();
  412. $cell->text = $this->helper_grading_report_participant($participant, $userinfo);
  413. $cell->rowspan = $numoftrs;
  414. $cell->attributes['class'] = 'participant';
  415. $row->cells[] = $cell;
  416. }
  417. // column #2 - submission - spans over all rows
  418. if ($tr == 0) {
  419. $cell = new html_table_cell();
  420. $cell->text = $this->helper_grading_report_submission($participant);
  421. $cell->rowspan = $numoftrs;
  422. $cell->attributes['class'] = 'submission';
  423. $row->cells[] = $cell;
  424. }
  425. // If we are in submission phase ignore the following columns.
  426. if ($options->workshopphase == workshop::PHASE_SUBMISSION) {
  427. $table->data[] = $row;
  428. continue;
  429. }
  430. // column #3 - received grades
  431. if ($tr % $spanreceived == 0) {
  432. $idx = intval($tr / $spanreceived);
  433. $assessment = self::array_nth($participant->reviewedby, $idx);
  434. $cell = new html_table_cell();
  435. $cell->text = $this->helper_grading_report_assessment($assessment, $options->showreviewernames, $userinfo,
  436. get_string('gradereceivedfrom', 'workshop'));
  437. $cell->rowspan = $spanreceived;
  438. $cell->attributes['class'] = 'receivedgrade';
  439. if (is_null($assessment) or is_null($assessment->grade)) {
  440. $cell->attributes['class'] .= ' null';
  441. } else {
  442. $cell->attributes['class'] .= ' notnull';
  443. }
  444. $row->cells[] = $cell;
  445. }
  446. // column #4 - total grade for submission
  447. if ($options->showsubmissiongrade and $tr == 0) {
  448. $cell = new html_table_cell();
  449. $cell->text = $this->helper_grading_report_grade($participant->submissiongrade, $participant->submissiongradeover);
  450. $cell->rowspan = $numoftrs;
  451. $cell->attributes['class'] = 'submissiongrade';
  452. $row->cells[] = $cell;
  453. }
  454. // column #5 - given grades
  455. if ($tr % $spangiven == 0) {
  456. $idx = intval($tr / $spangiven);
  457. $assessment = self::array_nth($participant->reviewerof, $idx);
  458. $cell = new html_table_cell();
  459. $cell->text = $this->helper_grading_report_assessment($assessment, $options->showauthornames, $userinfo,
  460. get_string('gradegivento', 'workshop'));
  461. $cell->rowspan = $spangiven;
  462. $cell->attributes['class'] = 'givengrade';
  463. if (is_null($assessment) or is_null($assessment->grade)) {
  464. $cell->attributes['class'] .= ' null';
  465. } else {
  466. $cell->attributes['class'] .= ' notnull';
  467. }
  468. $row->cells[] = $cell;
  469. }
  470. // column #6 - total grade for assessment
  471. if ($options->showgradinggrade and $tr == 0) {
  472. $cell = new html_table_cell();
  473. $cell->text = $this->helper_grading_report_grade($participant->gradinggrade);
  474. $cell->rowspan = $numoftrs;
  475. $cell->attributes['class'] = 'gradinggrade';
  476. $row->cells[] = $cell;
  477. }
  478. $table->data[] = $row;
  479. }
  480. }
  481. return html_writer::table($table);
  482. }
  483. /**
  484. * Renders the feedback for the author of the submission
  485. *
  486. * @param workshop_feedback_author $feedback
  487. * @return string HTML
  488. */
  489. protected function render_workshop_feedback_author(workshop_feedback_author $feedback) {
  490. return $this->helper_render_feedback($feedback);
  491. }
  492. /**
  493. * Renders the feedback for the reviewer of the submission
  494. *
  495. * @param workshop_feedback_reviewer $feedback
  496. * @return string HTML
  497. */
  498. protected function render_workshop_feedback_reviewer(workshop_feedback_reviewer $feedback) {
  499. return $this->helper_render_feedback($feedback);
  500. }
  501. /**
  502. * Helper method to rendering feedback
  503. *
  504. * @param workshop_feedback_author|workshop_feedback_reviewer $feedback
  505. * @return string HTML
  506. */
  507. private function helper_render_feedback($feedback) {
  508. $o = ''; // output HTML code
  509. $o .= $this->output->container_start('feedback feedbackforauthor');
  510. $o .= $this->output->container_start('header');
  511. $o .= $this->output->heading(get_string('feedbackby', 'workshop', s(fullname($feedback->get_provider()))), 3, 'title');
  512. $userpic = $this->output->user_picture($feedback->get_provider(), array('courseid' => $this->page->course->id, 'size' => 32));
  513. $o .= $this->output->container($userpic, 'picture');
  514. $o .= $this->output->container_end(); // end of header
  515. $content = format_text($feedback->get_content(), $feedback->get_format(), array('overflowdiv' => true));
  516. $o .= $this->output->container($content, 'content');
  517. $o .= $this->output->container_end();
  518. return $o;
  519. }
  520. /**
  521. * Renders the full assessment
  522. *
  523. * @param workshop_assessment $assessment
  524. * @return string HTML
  525. */
  526. protected function render_workshop_assessment(workshop_assessment $assessment) {
  527. $o = ''; // output HTML code
  528. $anonymous = is_null($assessment->reviewer);
  529. $classes = 'assessment-full';
  530. if ($anonymous) {
  531. $classes .= ' anonymous';
  532. }
  533. $o .= $this->output->container_start($classes);
  534. $o .= $this->output->container_start('header');
  535. if (!empty($assessment->title)) {
  536. $title = s($assessment->title);
  537. } else {
  538. $title = get_string('assessment', 'workshop');
  539. }
  540. if (($assessment->url instanceof moodle_url) and ($this->page->url != $assessment->url)) {
  541. $o .= $this->output->container(html_writer::link($assessment->url, $title), 'title');
  542. } else {
  543. $o .= $this->output->container($title, 'title');
  544. }
  545. if (!$anonymous) {
  546. $reviewer = $assessment->reviewer;
  547. $userpic = $this->output->user_picture($reviewer, array('courseid' => $this->page->course->id, 'size' => 32));
  548. $userurl = new moodle_url('/user/view.php',
  549. array('id' => $reviewer->id, 'course' => $this->page->course->id));
  550. $a = new stdClass();
  551. $a->name = fullname($reviewer);
  552. $a->url = $userurl->out();
  553. $byfullname = get_string('assessmentby', 'workshop', $a);
  554. $oo = $this->output->container($userpic, 'picture');
  555. $oo .= $this->output->container($byfullname, 'fullname');
  556. $o .= $this->output->container($oo, 'reviewer');
  557. }
  558. if (is_null($assessment->realgrade)) {
  559. $o .= $this->output->container(
  560. get_string('notassessed', 'workshop'),
  561. 'grade nograde'
  562. );
  563. } else {
  564. $a = new stdClass();
  565. $a->max = $assessment->maxgrade;
  566. $a->received = $assessment->realgrade;
  567. $o .= $this->output->container(
  568. get_string('gradeinfo', 'workshop', $a),
  569. 'grade'
  570. );
  571. if (!is_null($assessment->weight) and $assessment->weight != 1) {
  572. $o .= $this->output->container(
  573. get_string('weightinfo', 'workshop', $assessment->weight),
  574. 'weight'
  575. );
  576. }
  577. }
  578. $o .= $this->output->container_start('actions');
  579. foreach ($assessment->actions as $action) {
  580. $o .= $this->output->single_button($action->url, $action->label, $action->method);
  581. }
  582. $o .= $this->output->container_end(); // actions
  583. $o .= $this->output->container_end(); // header
  584. if (!is_null($assessment->form)) {
  585. $o .= print_collapsible_region_start('assessment-form-wrapper', uniqid('workshop-assessment'),
  586. get_string('assessmentform', 'workshop'), '', false, true);
  587. $o .= $this->output->container(self::moodleform($assessment->form), 'assessment-form');
  588. $o .= print_collapsible_region_end(true);
  589. if (!$assessment->form->is_editable()) {
  590. $o .= $this->overall_feedback($assessment);
  591. }
  592. }
  593. $o .= $this->output->container_end(); // main wrapper
  594. return $o;
  595. }
  596. /**
  597. * Renders the assessment of an example submission
  598. *
  599. * @param workshop_example_assessment $assessment
  600. * @return string HTML
  601. */
  602. protected function render_workshop_example_assessment(workshop_example_assessment $assessment) {
  603. return $this->render_workshop_assessment($assessment);
  604. }
  605. /**
  606. * Renders the reference assessment of an example submission
  607. *
  608. * @param workshop_example_reference_assessment $assessment
  609. * @return string HTML
  610. */
  611. protected function render_workshop_example_reference_assessment(workshop_example_reference_assessment $assessment) {
  612. return $this->render_workshop_assessment($assessment);
  613. }
  614. /**
  615. * Renders the overall feedback for the author of the submission
  616. *
  617. * @param workshop_assessment $assessment
  618. * @return string HTML
  619. */
  620. protected function overall_feedback(workshop_assessment $assessment) {
  621. $content = $assessment->get_overall_feedback_content();
  622. if ($content === false) {
  623. return '';
  624. }
  625. $o = '';
  626. if (!is_null($content)) {
  627. $o .= $this->output->container($content, 'content');
  628. }
  629. $attachments = $assessment->get_overall_feedback_attachments();
  630. if (!empty($attachments)) {
  631. $o .= $this->output->container_start('attachments');
  632. $images = '';
  633. $files = '';
  634. foreach ($attachments as $attachment) {
  635. $icon = $this->output->pix_icon(file_file_icon($attachment), get_mimetype_description($attachment),
  636. 'moodle', array('class' => 'icon'));
  637. $link = html_writer::link($attachment->fileurl, $icon.' '.substr($attachment->filepath.$attachment->filename, 1));
  638. if (file_mimetype_in_typegroup($attachment->mimetype, 'web_image')) {
  639. $preview = html_writer::empty_tag('img', array('src' => $attachment->previewurl, 'alt' => '', 'class' => 'preview'));
  640. $preview = html_writer::tag('a', $preview, array('href' => $attachment->fileurl));
  641. $images .= $this->output->container($preview);
  642. } else {
  643. $files .= html_writer::tag('li', $link, array('class' => $attachment->mimetype));
  644. }
  645. }
  646. if ($images) {
  647. $images = $this->output->container($images, 'images');
  648. }
  649. if ($files) {
  650. $files = html_writer::tag('ul', $files, array('class' => 'files'));
  651. }
  652. $o .= $images.$files;
  653. $o .= $this->output->container_end();
  654. }
  655. if ($o === '') {
  656. return '';
  657. }
  658. $o = $this->output->box($o, 'overallfeedback');
  659. $o = print_collapsible_region($o, 'overall-feedback-wrapper', uniqid('workshop-overall-feedback'),
  660. get_string('overallfeedback', 'workshop'), '', false, true);
  661. return $o;
  662. }
  663. /**
  664. * Renders a perpage selector for workshop listings
  665. *
  666. * The scripts using this have to define the $PAGE->url prior to calling this
  667. * and deal with eventually submitted value themselves.
  668. *
  669. * @param int $current current value of the perpage parameter
  670. * @return string HTML
  671. */
  672. public function perpage_selector($current=10) {
  673. $options = array();
  674. foreach (array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 1000) as $option) {
  675. if ($option != $current) {
  676. $options[$option] = $option;
  677. }
  678. }
  679. $select = new single_select($this->page->url, 'perpage', $options, '', array('' => get_string('showingperpagechange', 'mod_workshop')));
  680. $select->label = get_string('showingperpage', 'mod_workshop', $current);
  681. $select->method = 'post';
  682. return $this->output->container($this->output->render($select), 'perpagewidget');
  683. }
  684. /**
  685. * Renders the user's final grades
  686. *
  687. * @param workshop_final_grades $grades with the info about grades in the gradebook
  688. * @return string HTML
  689. */
  690. protected function render_workshop_final_grades(workshop_final_grades $grades) {
  691. $out = html_writer::start_tag('div', array('class' => 'finalgrades'));
  692. if (!empty($grades->submissiongrade)) {
  693. $cssclass = 'grade submissiongrade';
  694. if ($grades->submissiongrade->hidden) {
  695. $cssclass .= ' hiddengrade';
  696. }
  697. $out .= html_writer::tag(
  698. 'div',
  699. html_writer::tag('div', get_string('submissiongrade', 'mod_workshop'), array('class' => 'gradetype')) .
  700. html_writer::tag('div', $grades->submissiongrade->str_long_grade, array('class' => 'gradevalue')),
  701. array('class' => $cssclass)
  702. );
  703. }
  704. if (!empty($grades->assessmentgrade)) {
  705. $cssclass = 'grade assessmentgrade';
  706. if ($grades->assessmentgrade->hidden) {
  707. $cssclass .= ' hiddengrade';
  708. }
  709. $out .= html_writer::tag(
  710. 'div',
  711. html_writer::tag('div', get_string('gradinggrade', 'mod_workshop'), array('class' => 'gradetype')) .
  712. html_writer::tag('div', $grades->assessmentgrade->str_long_grade, array('class' => 'gradevalue')),
  713. array('class' => $cssclass)
  714. );
  715. }
  716. $out .= html_writer::end_tag('div');
  717. return $out;
  718. }
  719. ////////////////////////////////////////////////////////////////////////////
  720. // Internal rendering helper methods
  721. ////////////////////////////////////////////////////////////////////////////
  722. /**
  723. * Renders a list of files attached to the submission
  724. *
  725. * If format==html, then format a html string. If format==text, then format a text-only string.
  726. * Otherwise, returns html for non-images and html to display the image inline.
  727. *
  728. * @param int $submissionid submission identifier
  729. * @param string format the format of the returned string - html|text
  730. * @return string formatted text to be echoed
  731. */
  732. protected function helper_submission_attachments($submissionid, $format = 'html') {
  733. global $CFG;
  734. require_once($CFG->libdir.'/filelib.php');
  735. $fs = get_file_storage();
  736. $ctx = $this->page->context;
  737. $files = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid);
  738. $outputimgs = ''; // images to be displayed inline
  739. $outputfiles = ''; // list of attachment files
  740. foreach ($files as $file) {
  741. if ($file->is_directory()) {
  742. continue;
  743. }
  744. $filepath = $file->get_filepath();
  745. $filename = $file->get_filename();
  746. $fileurl = moodle_url::make_pluginfile_url($ctx->id, 'mod_workshop', 'submission_attachment',
  747. $submissionid, $filepath, $filename, true);
  748. $embedurl = moodle_url::make_pluginfile_url($ctx->id, 'mod_workshop', 'submission_attachment',
  749. $submissionid, $filepath, $filename, false);
  750. $embedurl = new moodle_url($embedurl, array('preview' => 'bigthumb'));
  751. $type = $file->get_mimetype();
  752. $image = $this->output->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon'));
  753. $linkhtml = html_writer::link($fileurl, $image . substr($filepath, 1) . $filename);
  754. $linktxt = "$filename [$fileurl]";
  755. if ($format == 'html') {
  756. if (file_mimetype_in_typegroup($type, 'web_image')) {
  757. $preview = html_writer::empty_tag('img', array('src' => $embedurl, 'alt' => '', 'class' => 'preview'));
  758. $preview = html_writer::tag('a', $preview, array('href' => $fileurl));
  759. $outputimgs .= $this->output->container($preview);
  760. } else {
  761. $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type));
  762. }
  763. } else if ($format == 'text') {
  764. $outputfiles .= $linktxt . PHP_EOL;
  765. }
  766. if (!empty($CFG->enableplagiarism)) {
  767. require_once($CFG->libdir.'/plagiarismlib.php');
  768. $outputfiles .= plagiarism_get_links(array('userid' => $file->get_userid(),
  769. 'file' => $file,
  770. 'cmid' => $this->page->cm->id,
  771. 'course' => $this->page->course->id));
  772. }
  773. }
  774. if ($format == 'html') {
  775. if ($outputimgs) {
  776. $outputimgs = $this->output->container($outputimgs, 'images');
  777. }
  778. if ($outputfiles) {
  779. $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files'));
  780. }
  781. return $this->output->container($outputimgs . $outputfiles, 'attachments');
  782. } else {
  783. return $outputfiles;
  784. }
  785. }
  786. /**
  787. * Renders the tasks for the single phase in the user plan
  788. *
  789. * @param stdClass $tasks
  790. * @return string html code
  791. */
  792. protected function helper_user_plan_tasks(array $tasks) {
  793. $out = '';
  794. foreach ($tasks as $taskcode => $task) {
  795. $classes = '';
  796. $icon = null;
  797. if ($task->completed === true) {
  798. $classes .= ' completed';
  799. } elseif ($task->completed === false) {
  800. $classes .= ' fail';
  801. } elseif ($task->completed === 'info') {
  802. $classes .= ' info';
  803. }
  804. if (is_null($task->link)) {
  805. $title = $task->title;
  806. } else {
  807. $title = html_writer::link($task->link, $task->title);
  808. }
  809. $title = $this->output->container($title, 'title');
  810. $details = $this->output->container($task->details, 'details');
  811. $out .= html_writer::tag('li', $title . $details, array('class' => $classes));
  812. }
  813. if ($out) {
  814. $out = html_writer::tag('ul', $out, array('class' => 'tasks'));
  815. }
  816. return $out;
  817. }
  818. /**
  819. * Renders a text with icons to sort by the given column
  820. *
  821. * This is intended for table headings.
  822. *
  823. * @param string $text The heading text
  824. * @param string $sortid The column id used for sorting
  825. * @param string $sortby Currently sorted by (column id)
  826. * @param string $sorthow Currently sorted how (ASC|DESC)
  827. *
  828. * @return string
  829. */
  830. protected function helper_sortable_heading($text, $sortid=null, $sortby=null, $sorthow=null) {
  831. global $PAGE;
  832. $out = html_writer::tag('span', $text, array('class'=>'text'));
  833. if (!is_null($sortid)) {
  834. if ($sortby !== $sortid or $sorthow !== 'ASC') {
  835. $url = new moodle_url($PAGE->url);
  836. $url->params(array('sortby' => $sortid, 'sorthow' => 'ASC'));
  837. $out .= $this->output->action_icon($url, new pix_icon('t/sort_asc', get_string('sortasc', 'workshop')),
  838. null, array('class' => 'iconsort sort asc'));
  839. }
  840. if ($sortby !== $sortid or $sorthow !== 'DESC') {
  841. $url = new moodle_url($PAGE->url);
  842. $url->params(array('sortby' => $sortid, 'sorthow' => 'DESC'));
  843. $out .= $this->output->action_icon($url, new pix_icon('t/sort_desc', get_string('sortdesc', 'workshop')),
  844. null, array('class' => 'iconsort sort desc'));
  845. }
  846. }
  847. return $out;
  848. }
  849. /**
  850. * @param stdClass $participant
  851. * @param array $userinfo
  852. * @return string
  853. */
  854. protected function helper_grading_report_participant(stdclass $participant, array $userinfo) {
  855. $userid = $participant->userid;
  856. $out = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 35));
  857. $out .= html_writer::tag('span', fullname($userinfo[$userid]));
  858. return $out;
  859. }
  860. /**
  861. * @param stdClass $participant
  862. * @return string
  863. */
  864. protected function helper_grading_report_submission(stdclass $participant) {
  865. global $CFG;
  866. if (is_null($participant->submissionid)) {
  867. $out = $this->output->container(get_string('nosubmissionfound', 'workshop'), 'info');
  868. } else {
  869. $url = new moodle_url('/mod/workshop/submission.php',
  870. array('cmid' => $this->page->context->instanceid, 'id' => $participant->submissionid));
  871. $out = html_writer::link($url, format_string($participant->submissiontitle), array('class'=>'title'));
  872. $lastmodified = get_string('userdatemodified', 'workshop', userdate($participant->submissionmodified));
  873. $out .= html_writer::tag('div', $lastmodified, array('class' => 'lastmodified'));
  874. }
  875. return $out;
  876. }
  877. /**
  878. * @todo Highlight the nulls
  879. * @param stdClass|null $assessment
  880. * @param bool $shownames
  881. * @param string $separator between the grade and the reviewer/author
  882. * @return string
  883. */
  884. protected function helper_grading_report_assessment($assessment, $shownames, array $userinfo, $separator) {
  885. global $CFG;
  886. if (is_null($assessment)) {
  887. return get_string('nullgrade', 'workshop');
  888. }
  889. $a = new stdclass();
  890. $a->grade = is_null($assessment->grade) ? get_string('nullgrade', 'workshop') : $assessment->grade;
  891. $a->gradinggrade = is_null($assessment->gradinggrade) ? get_string('nullgrade', 'workshop') : $assessment->gradinggrade;
  892. $a->weight = $assessment->weight;
  893. // grrr the following logic should really be handled by a future language pack feature
  894. if (is_null($assessment->gradinggradeover)) {
  895. if ($a->weight == 1) {
  896. $grade = get_string('formatpeergrade', 'workshop', $a);
  897. } else {
  898. $grade = get_string('formatpeergradeweighted', 'workshop', $a);
  899. }
  900. } else {
  901. $a->gradinggradeover = $assessment->gradinggradeover;
  902. if ($a->weight == 1) {
  903. $grade = get_string('formatpeergradeover', 'workshop', $a);
  904. } else {
  905. $grade = get_string('formatpeergradeoverweighted', 'workshop', $a);
  906. }
  907. }
  908. $url = new moodle_url('/mod/workshop/assessment.php',
  909. array('asid' => $assessment->assessmentid));
  910. $grade = html_writer::link($url, $grade, array('class'=>'grade'));
  911. if ($shownames) {
  912. $userid = $assessment->userid;
  913. $name = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 16));
  914. $name .= html_writer::tag('span', fullname($userinfo[$userid]), array('class' => 'fullname'));
  915. $name = $separator . html_writer::tag('span', $name, array('class' => 'user'));
  916. } else {
  917. $name = '';
  918. }
  919. return $this->output->container($grade . $name, 'assessmentdetails');
  920. }
  921. /**
  922. * Formats the aggreagated grades
  923. */
  924. protected function helper_grading_report_grade($grade, $over=null) {
  925. $a = new stdclass();
  926. $a->grade = is_null($grade) ? get_string('nullgrade', 'workshop') : $grade;
  927. if (is_null($over)) {
  928. $text = get_string('formataggregatedgrade', 'workshop', $a);
  929. } else {
  930. $a->over = is_null($over) ? get_string('nullgrade', 'workshop') : $over;
  931. $text = get_string('formataggregatedgradeover', 'workshop', $a);
  932. }
  933. return $text;
  934. }
  935. ////////////////////////////////////////////////////////////////////////////
  936. // Static helpers
  937. ////////////////////////////////////////////////////////////////////////////
  938. /**
  939. * Helper method dealing with the fact we can not just fetch the output of moodleforms
  940. *
  941. * @param moodleform $mform
  942. * @return string HTML
  943. */
  944. protected static function moodleform(moodleform $mform) {
  945. ob_start();
  946. $mform->display();
  947. $o = ob_get_contents();
  948. ob_end_clean();
  949. return $o;
  950. }
  951. /**
  952. * Helper function returning the n-th item of the array
  953. *
  954. * @param array $a
  955. * @param int $n from 0 to m, where m is th number of items in the array
  956. * @return mixed the $n-th element of $a
  957. */
  958. protected static function array_nth(array $a, $n) {
  959. $keys = array_keys($a);
  960. if ($n < 0 or $n > count($keys) - 1) {
  961. return null;
  962. }
  963. $key = $keys[$n];
  964. return $a[$key];
  965. }
  966. /**
  967. * Tries to guess the fullname format set at the site
  968. *
  969. * @return string fl|lf
  970. */
  971. protected static function fullname_format() {
  972. $fake = new stdclass(); // fake user
  973. $fake->lastname = 'LLLL';
  974. $fake->firstname = 'FFFF';
  975. $fullname = get_string('fullnamedisplay', '', $fake);
  976. if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
  977. return 'lf';
  978. } else {
  979. return 'fl';
  980. }
  981. }
  982. }