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

/mod/workshop/renderer.php

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