PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/renderer.php

https://bitbucket.org/mbbid5r/moodle_workshop_phaseless
PHP | 1011 lines | 649 code | 139 blank | 223 comment | 121 complexity | 63e47329ea2449ecba1f636f95b83dc6 MD5 | raw file
  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. // //grading button
  364. // if ($options->showgradinggrade) {
  365. // $table->head[] = '';
  366. // }
  367. $table->rowclasses = array();
  368. $table->colclasses = array();
  369. $table->data = array();
  370. foreach ($grades as $participant) {
  371. $numofreceived = count($participant->reviewedby);
  372. $numofgiven = count($participant->reviewerof);
  373. $published = $participant->submissionpublished;
  374. // compute the number of <tr> table rows needed to display this participant
  375. if ($numofreceived > 0 and $numofgiven > 0) {
  376. $numoftrs = workshop::lcm($numofreceived, $numofgiven);
  377. $spanreceived = $numoftrs / $numofreceived;
  378. $spangiven = $numoftrs / $numofgiven;
  379. } elseif ($numofreceived == 0 and $numofgiven > 0) {
  380. $numoftrs = $numofgiven;
  381. $spanreceived = $numoftrs;
  382. $spangiven = $numoftrs / $numofgiven;
  383. } elseif ($numofreceived > 0 and $numofgiven == 0) {
  384. $numoftrs = $numofreceived;
  385. $spanreceived = $numoftrs / $numofreceived;
  386. $spangiven = $numoftrs;
  387. } else {
  388. $numoftrs = 1;
  389. $spanreceived = 1;
  390. $spangiven = 1;
  391. }
  392. for ($tr = 0; $tr < $numoftrs; $tr++) {
  393. $row = new html_table_row();
  394. if ($published) {
  395. $row->attributes['class'] = 'published';
  396. }
  397. // column #1 - participant - spans over all rows
  398. if ($tr == 0) {
  399. $cell = new html_table_cell();
  400. $cell->text = $this->helper_grading_report_participant($participant, $userinfo);
  401. $cell->rowspan = $numoftrs;
  402. $cell->attributes['class'] = 'participant';
  403. $row->cells[] = $cell;
  404. }
  405. // column #2 - submission - spans over all rows
  406. if ($tr == 0) {
  407. $cell = new html_table_cell();
  408. $cell->text = $this->helper_grading_report_submission($participant);
  409. $cell->rowspan = $numoftrs;
  410. $cell->attributes['class'] = 'submission';
  411. $row->cells[] = $cell;
  412. }
  413. // column #3 - received grades
  414. if ($tr % $spanreceived == 0) {
  415. $idx = intval($tr / $spanreceived);
  416. $assessment = self::array_nth($participant->reviewedby, $idx);
  417. $cell = new html_table_cell();
  418. $cell->text = $this->helper_grading_report_assessment($assessment, $options->showreviewernames, $userinfo,
  419. get_string('gradereceivedfrom', 'workshop'));
  420. $cell->rowspan = $spanreceived;
  421. $cell->attributes['class'] = 'receivedgrade';
  422. if (is_null($assessment) or is_null($assessment->grade)) {
  423. $cell->attributes['class'] .= ' null';
  424. } else {
  425. $cell->attributes['class'] .= ' notnull';
  426. }
  427. $row->cells[] = $cell;
  428. }
  429. // column #4 - total grade for submission
  430. if ($options->showsubmissiongrade and $tr == 0) {
  431. $cell = new html_table_cell();
  432. $cell->text = $this->helper_grading_report_grade($participant->submissiongrade, $participant->submissiongradeover);
  433. $cell->rowspan = $numoftrs;
  434. $cell->attributes['class'] = 'submissiongrade';
  435. $row->cells[] = $cell;
  436. }
  437. // column #5 - given grades
  438. if ($tr % $spangiven == 0) {
  439. $idx = intval($tr / $spangiven);
  440. $assessment = self::array_nth($participant->reviewerof, $idx);
  441. $cell = new html_table_cell();
  442. $cell->text = $this->helper_grading_report_assessment($assessment, $options->showauthornames, $userinfo,
  443. get_string('gradegivento', 'workshop'));
  444. $cell->rowspan = $spangiven;
  445. $cell->attributes['class'] = 'givengrade';
  446. if (is_null($assessment) or is_null($assessment->grade)) {
  447. $cell->attributes['class'] .= ' null';
  448. } else {
  449. $cell->attributes['class'] .= ' notnull';
  450. }
  451. $row->cells[] = $cell;
  452. }
  453. // column #6 - total grade for assessment
  454. if ($options->showgradinggrade and $tr == 0) {
  455. $cell = new html_table_cell();
  456. $cell->text = $this->helper_grading_report_grade($participant->gradinggrade);
  457. $cell->rowspan = $numoftrs;
  458. $cell->attributes['class'] = 'gradinggrade';
  459. $row->cells[] = $cell;
  460. }
  461. // //column #7 - individual grading
  462. // if ($options->showgradinggrade and $tr == 0) {
  463. // $cell = new html_table_cell();
  464. // $cell->text = html_writer::tag('button', 'update grades');
  465. // $cell->rowspan = $numoftrs;
  466. // $cell->attributes['class'] = 'updategrades';
  467. // $row->cells[] = $cell;
  468. // }
  469. $table->data[] = $row;
  470. }
  471. }
  472. return html_writer::table($table);
  473. }
  474. /**
  475. * Renders the feedback for the author of the submission
  476. *
  477. * @param workshop_feedback_author $feedback
  478. * @return string HTML
  479. */
  480. protected function render_workshop_feedback_author(workshop_feedback_author $feedback) {
  481. return $this->helper_render_feedback($feedback);
  482. }
  483. /**
  484. * Renders the feedback for the reviewer of the submission
  485. *
  486. * @param workshop_feedback_reviewer $feedback
  487. * @return string HTML
  488. */
  489. protected function render_workshop_feedback_reviewer(workshop_feedback_reviewer $feedback) {
  490. return $this->helper_render_feedback($feedback);
  491. }
  492. /**
  493. * Helper method to rendering feedback
  494. *
  495. * @param workshop_feedback_author|workshop_feedback_reviewer $feedback
  496. * @return string HTML
  497. */
  498. private function helper_render_feedback($feedback) {
  499. $o = ''; // output HTML code
  500. $o .= $this->output->container_start('feedback feedbackforauthor');
  501. $o .= $this->output->container_start('header');
  502. $o .= $this->output->heading(get_string('feedbackby', 'workshop', s(fullname($feedback->get_provider()))), 3, 'title');
  503. $userpic = $this->output->user_picture($feedback->get_provider(), array('courseid' => $this->page->course->id, 'size' => 32));
  504. $o .= $this->output->container($userpic, 'picture');
  505. $o .= $this->output->container_end(); // end of header
  506. $content = format_text($feedback->get_content(), $feedback->get_format(), array('overflowdiv' => true));
  507. $o .= $this->output->container($content, 'content');
  508. $o .= $this->output->container_end();
  509. return $o;
  510. }
  511. /**
  512. * Renders the full assessment
  513. *
  514. * @param workshop_assessment $assessment
  515. * @return string HTML
  516. */
  517. protected function render_workshop_assessment(workshop_assessment $assessment) {
  518. $o = ''; // output HTML code
  519. $anonymous = is_null($assessment->reviewer);
  520. $classes = 'assessment-full';
  521. if ($anonymous) {
  522. $classes .= ' anonymous';
  523. }
  524. $o .= $this->output->container_start($classes);
  525. $o .= $this->output->container_start('header');
  526. if (!empty($assessment->title)) {
  527. $title = s($assessment->title);
  528. } else {
  529. $title = get_string('assessment', 'workshop');
  530. }
  531. if (($assessment->url instanceof moodle_url) and ($this->page->url != $assessment->url)) {
  532. $o .= $this->output->container(html_writer::link($assessment->url, $title), 'title');
  533. } else {
  534. $o .= $this->output->container($title, 'title');
  535. }
  536. if (!$anonymous) {
  537. $reviewer = $assessment->reviewer;
  538. $userpic = $this->output->user_picture($reviewer, array('courseid' => $this->page->course->id, 'size' => 32));
  539. $userurl = new moodle_url('/user/view.php',
  540. array('id' => $reviewer->id, 'course' => $this->page->course->id));
  541. $a = new stdClass();
  542. $a->name = fullname($reviewer);
  543. $a->url = $userurl->out();
  544. $byfullname = get_string('assessmentby', 'workshop', $a);
  545. $oo = $this->output->container($userpic, 'picture');
  546. $oo .= $this->output->container($byfullname, 'fullname');
  547. $o .= $this->output->container($oo, 'reviewer');
  548. }
  549. if (is_null($assessment->realgrade)) {
  550. $o .= $this->output->container(
  551. get_string('notassessed', 'workshop'),
  552. 'grade nograde'
  553. );
  554. } else {
  555. $a = new stdClass();
  556. $a->max = $assessment->maxgrade;
  557. $a->received = $assessment->realgrade;
  558. $o .= $this->output->container(
  559. get_string('gradeinfo', 'workshop', $a),
  560. 'grade'
  561. );
  562. if (!is_null($assessment->weight) and $assessment->weight != 1) {
  563. $o .= $this->output->container(
  564. get_string('weightinfo', 'workshop', $assessment->weight),
  565. 'weight'
  566. );
  567. }
  568. }
  569. $o .= $this->output->container_start('actions');
  570. foreach ($assessment->actions as $action) {
  571. $o .= $this->output->single_button($action->url, $action->label, $action->method);
  572. }
  573. $o .= $this->output->container_end(); // actions
  574. $o .= $this->output->container_end(); // header
  575. if (!is_null($assessment->form)) {
  576. $o .= print_collapsible_region_start('assessment-form-wrapper', uniqid('workshop-assessment'),
  577. get_string('assessmentform', 'workshop'), '', false, true);
  578. $o .= $this->output->container(self::moodleform($assessment->form), 'assessment-form');
  579. $o .= print_collapsible_region_end(true);
  580. }
  581. $o .= $this->output->container_end(); // main wrapper
  582. return $o;
  583. }
  584. /**
  585. * Renders the assessment of an example submission
  586. *
  587. * @param workshop_example_assessment $assessment
  588. * @return string HTML
  589. */
  590. protected function render_workshop_example_assessment(workshop_example_assessment $assessment) {
  591. return $this->render_workshop_assessment($assessment);
  592. }
  593. /**
  594. * Renders the reference assessment of an example submission
  595. *
  596. * @param workshop_example_reference_assessment $assessment
  597. * @return string HTML
  598. */
  599. protected function render_workshop_example_reference_assessment(workshop_example_reference_assessment $assessment) {
  600. return $this->render_workshop_assessment($assessment);
  601. }
  602. /**
  603. * Renders a perpage selector for workshop listings
  604. *
  605. * The scripts using this have to define the $PAGE->url prior to calling this
  606. * and deal with eventually submitted value themselves.
  607. *
  608. * @param int $current current value of the perpage parameter
  609. * @return string HTML
  610. */
  611. public function perpage_selector($current=10) {
  612. $options = array();
  613. foreach (array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 1000) as $option) {
  614. if ($option != $current) {
  615. $options[$option] = $option;
  616. }
  617. }
  618. $select = new single_select($this->page->url, 'perpage', $options, '', array('' => get_string('showingperpagechange', 'mod_workshop')));
  619. $select->label = get_string('showingperpage', 'mod_workshop', $current);
  620. $select->method = 'post';
  621. return $this->output->container($this->output->render($select), 'perpagewidget');
  622. }
  623. ////////////////////////////////////////////////////////////////////////////
  624. // Internal rendering helper methods
  625. ////////////////////////////////////////////////////////////////////////////
  626. /**
  627. * Renders a list of files attached to the submission
  628. *
  629. * If format==html, then format a html string. If format==text, then format a text-only string.
  630. * Otherwise, returns html for non-images and html to display the image inline.
  631. *
  632. * @param int $submissionid submission identifier
  633. * @param string format the format of the returned string - html|text
  634. * @return string formatted text to be echoed
  635. */
  636. protected function helper_submission_attachments($submissionid, $format = 'html') {
  637. global $CFG;
  638. require_once($CFG->libdir.'/filelib.php');
  639. $fs = get_file_storage();
  640. $ctx = $this->page->context;
  641. $files = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid);
  642. $outputimgs = ''; // images to be displayed inline
  643. $outputfiles = ''; // list of attachment files
  644. foreach ($files as $file) {
  645. if ($file->is_directory()) {
  646. continue;
  647. }
  648. $filepath = $file->get_filepath();
  649. $filename = $file->get_filename();
  650. $fileurl = file_encode_url($CFG->wwwroot . '/pluginfile.php',
  651. '/' . $ctx->id . '/mod_workshop/submission_attachment/' . $submissionid . $filepath . $filename, true);
  652. $type = $file->get_mimetype();
  653. $image = $this->output->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon'));
  654. $linkhtml = html_writer::link($fileurl, $image) . substr($filepath, 1) . html_writer::link($fileurl, $filename);
  655. $linktxt = "$filename [$fileurl]";
  656. if ($format == 'html') {
  657. if (file_mimetype_in_typegroup($type, 'web_image')) {
  658. $preview = html_writer::empty_tag('img', array('src' => $fileurl, 'alt' => '', 'class' => 'preview'));
  659. $preview = html_writer::tag('a', $preview, array('href' => $fileurl));
  660. $outputimgs .= $this->output->container($preview);
  661. } else {
  662. $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type));
  663. }
  664. } else if ($format == 'text') {
  665. $outputfiles .= $linktxt . PHP_EOL;
  666. }
  667. }
  668. if ($format == 'html') {
  669. if ($outputimgs) {
  670. $outputimgs = $this->output->container($outputimgs, 'images');
  671. }
  672. if ($outputfiles) {
  673. $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files'));
  674. }
  675. return $this->output->container($outputimgs . $outputfiles, 'attachments');
  676. } else {
  677. return $outputfiles;
  678. }
  679. }
  680. /**
  681. * Renders the tasks for the single phase in the user plan
  682. *
  683. * @param stdClass $tasks
  684. * @return string html code
  685. */
  686. protected function helper_user_plan_tasks(array $tasks) {
  687. $out = '';
  688. foreach ($tasks as $taskcode => $task) {
  689. $classes = '';
  690. $icon = null;
  691. if ($task->completed === true) {
  692. $classes .= ' completed';
  693. } elseif ($task->completed === false) {
  694. $classes .= ' fail';
  695. } elseif ($task->completed === 'info') {
  696. $classes .= ' info';
  697. }
  698. if (is_null($task->link)) {
  699. $title = $task->title;
  700. } else {
  701. $title = html_writer::link($task->link, $task->title);
  702. }
  703. $title = $this->output->container($title, 'title');
  704. $details = $this->output->container($task->details, 'details');
  705. $out .= html_writer::tag('li', $title . $details, array('class' => $classes));
  706. }
  707. if ($out) {
  708. $out = html_writer::tag('ul', $out, array('class' => 'tasks'));
  709. }
  710. return $out;
  711. }
  712. /**
  713. * Renders a text with icons to sort by the given column
  714. *
  715. * This is intended for table headings.
  716. *
  717. * @param string $text The heading text
  718. * @param string $sortid The column id used for sorting
  719. * @param string $sortby Currently sorted by (column id)
  720. * @param string $sorthow Currently sorted how (ASC|DESC)
  721. *
  722. * @return string
  723. */
  724. protected function helper_sortable_heading($text, $sortid=null, $sortby=null, $sorthow=null) {
  725. global $PAGE;
  726. $out = html_writer::tag('span', $text, array('class'=>'text'));
  727. if (!is_null($sortid)) {
  728. if ($sortby !== $sortid or $sorthow !== 'ASC') {
  729. $url = new moodle_url($PAGE->url);
  730. $url->params(array('sortby' => $sortid, 'sorthow' => 'ASC'));
  731. $out .= $this->output->action_icon($url, new pix_icon('t/up', get_string('sortasc', 'workshop')), null, array('class' => 'sort asc'));
  732. }
  733. if ($sortby !== $sortid or $sorthow !== 'DESC') {
  734. $url = new moodle_url($PAGE->url);
  735. $url->params(array('sortby' => $sortid, 'sorthow' => 'DESC'));
  736. $out .= $this->output->action_icon($url, new pix_icon('t/down', get_string('sortdesc', 'workshop')), null, array('class' => 'sort desc'));
  737. }
  738. }
  739. return $out;
  740. }
  741. /**
  742. * @param stdClass $participant
  743. * @param array $userinfo
  744. * @return string
  745. */
  746. protected function helper_grading_report_participant(stdclass $participant, array $userinfo) {
  747. $userid = $participant->userid;
  748. $out = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 35));
  749. $out .= html_writer::tag('span', fullname($userinfo[$userid]));
  750. return $out;
  751. }
  752. /**
  753. * @param stdClass $participant
  754. * @return string
  755. */
  756. protected function helper_grading_report_submission(stdclass $participant) {
  757. global $CFG;
  758. if (is_null($participant->submissionid)) {
  759. $out = $this->output->container(get_string('nosubmissionfound', 'workshop'), 'info');
  760. } else {
  761. $url = new moodle_url('/mod/workshop/submission.php',
  762. array('cmid' => $this->page->context->instanceid, 'id' => $participant->submissionid));
  763. $out = html_writer::link($url, format_string($participant->submissiontitle), array('class'=>'title'));
  764. }
  765. return $out;
  766. }
  767. /**
  768. * @todo Highlight the nulls
  769. * @param stdClass|null $assessment
  770. * @param bool $shownames
  771. * @param string $separator between the grade and the reviewer/author
  772. * @return string
  773. */
  774. protected function helper_grading_report_assessment($assessment, $shownames, array $userinfo, $separator) {
  775. global $CFG;
  776. if (is_null($assessment)) {
  777. return get_string('nullgrade', 'workshop');
  778. }
  779. $a = new stdclass();
  780. $a->grade = is_null($assessment->grade) ? get_string('nullgrade', 'workshop') : $assessment->grade;
  781. $a->gradinggrade = is_null($assessment->gradinggrade) ? get_string('nullgrade', 'workshop') : $assessment->gradinggrade;
  782. $a->weight = $assessment->weight;
  783. // grrr the following logic should really be handled by a future language pack feature
  784. if (is_null($assessment->gradinggradeover)) {
  785. if ($a->weight == 1) {
  786. $grade = get_string('formatpeergrade', 'workshop', $a);
  787. } else {
  788. $grade = get_string('formatpeergradeweighted', 'workshop', $a);
  789. }
  790. } else {
  791. $a->gradinggradeover = $assessment->gradinggradeover;
  792. if ($a->weight == 1) {
  793. $grade = get_string('formatpeergradeover', 'workshop', $a);
  794. } else {
  795. $grade = get_string('formatpeergradeoverweighted', 'workshop', $a);
  796. }
  797. }
  798. $url = new moodle_url('/mod/workshop/assessment.php',
  799. array('asid' => $assessment->assessmentid));
  800. $grade = html_writer::link($url, $grade, array('class'=>'grade'));
  801. if ($shownames) {
  802. $userid = $assessment->userid;
  803. $name = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 16));
  804. $name .= html_writer::tag('span', fullname($userinfo[$userid]), array('class' => 'fullname'));
  805. $name = $separator . html_writer::tag('span', $name, array('class' => 'user'));
  806. } else {
  807. $name = '';
  808. }
  809. return $this->output->container($grade . $name, 'assessmentdetails');
  810. }
  811. /**
  812. * Formats the aggreagated grades
  813. */
  814. protected function helper_grading_report_grade($grade, $over=null) {
  815. $a = new stdclass();
  816. $a->grade = is_null($grade) ? get_string('nullgrade', 'workshop') : $grade;
  817. if (is_null($over)) {
  818. $text = get_string('formataggregatedgrade', 'workshop', $a);
  819. } else {
  820. $a->over = is_null($over) ? get_string('nullgrade', 'workshop') : $over;
  821. $text = get_string('formataggregatedgradeover', 'workshop', $a);
  822. }
  823. return $text;
  824. }
  825. ////////////////////////////////////////////////////////////////////////////
  826. // Static helpers
  827. ////////////////////////////////////////////////////////////////////////////
  828. /**
  829. * Helper method dealing with the fact we can not just fetch the output of moodleforms
  830. *
  831. * @param moodleform $mform
  832. * @return string HTML
  833. */
  834. protected static function moodleform(moodleform $mform) {
  835. ob_start();
  836. $mform->display();
  837. $o = ob_get_contents();
  838. ob_end_clean();
  839. return $o;
  840. }
  841. /**
  842. * Helper function returning the n-th item of the array
  843. *
  844. * @param array $a
  845. * @param int $n from 0 to m, where m is th number of items in the array
  846. * @return mixed the $n-th element of $a
  847. */
  848. protected static function array_nth(array $a, $n) {
  849. $keys = array_keys($a);
  850. if ($n < 0 or $n > count($keys) - 1) {
  851. return null;
  852. }
  853. $key = $keys[$n];
  854. return $a[$key];
  855. }
  856. /**
  857. * Tries to guess the fullname format set at the site
  858. *
  859. * @return string fl|lf
  860. */
  861. protected static function fullname_format() {
  862. $fake = new stdclass(); // fake user
  863. $fake->lastname = 'LLLL';
  864. $fake->firstname = 'FFFF';
  865. $fullname = get_string('fullnamedisplay', '', $fake);
  866. if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
  867. return 'lf';
  868. } else {
  869. return 'fl';
  870. }
  871. }
  872. }