PageRenderTime 33ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/workshop/renderer.php

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