PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/workshop/renderer.php

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