MDL-37734 use prefetching workaround for MARS transaction problems
[moodle.git] / mod / workshop / renderer.php
blobeeee5ad59a9985f0162eff2443b01b51fa304e24
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Workshop module renderering methods are defined here
21 * @package mod
22 * @subpackage workshop
23 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Workshop module renderer class
32 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class mod_workshop_renderer extends plugin_renderer_base {
37 ////////////////////////////////////////////////////////////////////////////
38 // External API - methods to render workshop renderable components
39 ////////////////////////////////////////////////////////////////////////////
41 /**
42 * Renders workshop message
44 * @param workshop_message $message to display
45 * @return string html code
47 protected function render_workshop_message(workshop_message $message) {
49 $text = $message->get_message();
50 $url = $message->get_action_url();
51 $label = $message->get_action_label();
53 if (empty($text) and empty($label)) {
54 return '';
57 switch ($message->get_type()) {
58 case workshop_message::TYPE_OK:
59 $sty = 'ok';
60 break;
61 case workshop_message::TYPE_ERROR:
62 $sty = 'error';
63 break;
64 default:
65 $sty = 'info';
68 $o = html_writer::tag('span', $message->get_message());
70 if (!is_null($url) and !is_null($label)) {
71 $o .= $this->output->single_button($url, $label, 'get');
74 return $this->output->container($o, array('message', $sty));
78 /**
79 * Renders full workshop submission
81 * @param workshop_submission $submission
82 * @return string HTML
84 protected function render_workshop_submission(workshop_submission $submission) {
86 $o = ''; // output HTML code
87 $anonymous = $submission->is_anonymous();
88 $classes = 'submission-full';
89 if ($anonymous) {
90 $classes .= ' anonymous';
92 $o .= $this->output->container_start($classes);
93 $o .= $this->output->container_start('header');
95 $title = format_string($submission->title);
97 if ($this->page->url != $submission->url) {
98 $title = html_writer::link($submission->url, $title);
101 $o .= $this->output->heading($title, 3, 'title');
103 if (!$anonymous) {
104 $author = new stdclass();
105 $author->id = $submission->authorid;
106 $author->firstname = $submission->authorfirstname;
107 $author->lastname = $submission->authorlastname;
108 $author->picture = $submission->authorpicture;
109 $author->imagealt = $submission->authorimagealt;
110 $author->email = $submission->authoremail;
111 $userpic = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 64));
112 $userurl = new moodle_url('/user/view.php',
113 array('id' => $author->id, 'course' => $this->page->course->id));
114 $a = new stdclass();
115 $a->name = fullname($author);
116 $a->url = $userurl->out();
117 $byfullname = get_string('byfullname', 'workshop', $a);
118 $oo = $this->output->container($userpic, 'picture');
119 $oo .= $this->output->container($byfullname, 'fullname');
121 $o .= $this->output->container($oo, 'author');
124 $created = get_string('userdatecreated', 'workshop', userdate($submission->timecreated));
125 $o .= $this->output->container($created, 'userdate created');
127 if ($submission->timemodified > $submission->timecreated) {
128 $modified = get_string('userdatemodified', 'workshop', userdate($submission->timemodified));
129 $o .= $this->output->container($modified, 'userdate modified');
132 $o .= $this->output->container_end(); // end of header
134 $content = format_text($submission->content, $submission->contentformat, array('overflowdiv'=>true));
135 $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $this->page->context->id,
136 'mod_workshop', 'submission_content', $submission->id);
137 $o .= $this->output->container($content, 'content');
139 $o .= $this->helper_submission_attachments($submission->id, 'html');
141 $o .= $this->output->container_end(); // end of submission-full
143 return $o;
147 * Renders short summary of the submission
149 * @param workshop_submission_summary $summary
150 * @return string text to be echo'ed
152 protected function render_workshop_submission_summary(workshop_submission_summary $summary) {
154 $o = ''; // output HTML code
155 $anonymous = $summary->is_anonymous();
156 $classes = 'submission-summary';
158 if ($anonymous) {
159 $classes .= ' anonymous';
162 $gradestatus = '';
164 if ($summary->status == 'notgraded') {
165 $classes .= ' notgraded';
166 $gradestatus = $this->output->container(get_string('nogradeyet', 'workshop'), 'grade-status');
168 } else if ($summary->status == 'graded') {
169 $classes .= ' graded';
170 $gradestatus = $this->output->container(get_string('alreadygraded', 'workshop'), 'grade-status');
173 $o .= $this->output->container_start($classes); // main wrapper
174 $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
176 if (!$anonymous) {
177 $author = new stdClass();
178 $author->id = $summary->authorid;
179 $author->firstname = $summary->authorfirstname;
180 $author->lastname = $summary->authorlastname;
181 $author->picture = $summary->authorpicture;
182 $author->imagealt = $summary->authorimagealt;
183 $author->email = $summary->authoremail;
184 $userpic = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 35));
185 $userurl = new moodle_url('/user/view.php',
186 array('id' => $author->id, 'course' => $this->page->course->id));
187 $a = new stdClass();
188 $a->name = fullname($author);
189 $a->url = $userurl->out();
190 $byfullname = get_string('byfullname', 'workshop', $a);
192 $oo = $this->output->container($userpic, 'picture');
193 $oo .= $this->output->container($byfullname, 'fullname');
194 $o .= $this->output->container($oo, 'author');
197 $created = get_string('userdatecreated', 'workshop', userdate($summary->timecreated));
198 $o .= $this->output->container($created, 'userdate created');
200 if ($summary->timemodified > $summary->timecreated) {
201 $modified = get_string('userdatemodified', 'workshop', userdate($summary->timemodified));
202 $o .= $this->output->container($modified, 'userdate modified');
205 $o .= $gradestatus;
206 $o .= $this->output->container_end(); // end of the main wrapper
207 return $o;
211 * Renders full workshop example submission
213 * @param workshop_example_submission $example
214 * @return string HTML
216 protected function render_workshop_example_submission(workshop_example_submission $example) {
218 $o = ''; // output HTML code
219 $classes = 'submission-full example';
220 $o .= $this->output->container_start($classes);
221 $o .= $this->output->container_start('header');
222 $o .= $this->output->heading(format_string($example->title), 3, 'title');
223 $o .= $this->output->container_end(); // end of header
225 $content = format_text($example->content, $example->contentformat, array('overflowdiv'=>true));
226 $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $this->page->context->id,
227 'mod_workshop', 'submission_content', $example->id);
228 $o .= $this->output->container($content, 'content');
230 $o .= $this->helper_submission_attachments($example->id, 'html');
232 $o .= $this->output->container_end(); // end of submission-full
234 return $o;
238 * Renders short summary of the example submission
240 * @param workshop_example_submission_summary $summary
241 * @return string text to be echo'ed
243 protected function render_workshop_example_submission_summary(workshop_example_submission_summary $summary) {
245 $o = ''; // output HTML code
247 // wrapping box
248 $o .= $this->output->box_start('generalbox example-summary ' . $summary->status);
250 // title
251 $o .= $this->output->container_start('example-title');
252 $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
254 if ($summary->editable) {
255 $o .= $this->output->action_icon($summary->editurl, new pix_icon('i/edit', get_string('edit')));
257 $o .= $this->output->container_end();
259 // additional info
260 if ($summary->status == 'notgraded') {
261 $o .= $this->output->container(get_string('nogradeyet', 'workshop'), 'example-info nograde');
262 } else {
263 $o .= $this->output->container(get_string('gradeinfo', 'workshop' , $summary->gradeinfo), 'example-info grade');
266 // button to assess
267 $button = new single_button($summary->assessurl, $summary->assesslabel, 'get');
268 $o .= $this->output->container($this->output->render($button), 'example-actions');
270 // end of wrapping box
271 $o .= $this->output->box_end();
273 return $o;
277 * Renders the user plannner tool
279 * @param workshop_user_plan $plan prepared for the user
280 * @return string html code to be displayed
282 protected function render_workshop_user_plan(workshop_user_plan $plan) {
283 $table = new html_table();
284 $table->attributes['class'] = 'userplan';
285 $table->head = array();
286 $table->colclasses = array();
287 $row = new html_table_row();
288 $row->attributes['class'] = 'phasetasks';
289 foreach ($plan->phases as $phasecode => $phase) {
290 $title = html_writer::tag('span', $phase->title);
291 $actions = '';
292 foreach ($phase->actions as $action) {
293 switch ($action->type) {
294 case 'switchphase':
295 $icon = 'i/marker';
296 if ($phasecode == workshop::PHASE_ASSESSMENT
297 and $plan->workshop->phase == workshop::PHASE_SUBMISSION
298 and $plan->workshop->phaseswitchassessment) {
299 $icon = 'i/scheduled';
301 $actions .= $this->output->action_icon($action->url, new pix_icon($icon, get_string('switchphase', 'workshop')));
302 break;
305 if (!empty($actions)) {
306 $actions = $this->output->container($actions, 'actions');
308 $table->head[] = $this->output->container($title . $actions);
309 $classes = 'phase' . $phasecode;
310 if ($phase->active) {
311 $classes .= ' active';
312 } else {
313 $classes .= ' nonactive';
315 $table->colclasses[] = $classes;
316 $cell = new html_table_cell();
317 $cell->text = $this->helper_user_plan_tasks($phase->tasks);
318 $row->cells[] = $cell;
320 $table->data = array($row);
322 return html_writer::table($table);
326 * Renders the result of the submissions allocation process
328 * @param workshop_allocation_result $result as returned by the allocator's init() method
329 * @return string HTML to be echoed
331 protected function render_workshop_allocation_result(workshop_allocation_result $result) {
333 $status = $result->get_status();
335 if (is_null($status) or $status == workshop_allocation_result::STATUS_VOID) {
336 debugging('Attempt to render workshop_allocation_result with empty status', DEBUG_DEVELOPER);
337 return '';
340 switch ($status) {
341 case workshop_allocation_result::STATUS_FAILED:
342 if ($message = $result->get_message()) {
343 $message = new workshop_message($message, workshop_message::TYPE_ERROR);
344 } else {
345 $message = new workshop_message(get_string('allocationerror', 'workshop'), workshop_message::TYPE_ERROR);
347 break;
349 case workshop_allocation_result::STATUS_CONFIGURED:
350 if ($message = $result->get_message()) {
351 $message = new workshop_message($message, workshop_message::TYPE_INFO);
352 } else {
353 $message = new workshop_message(get_string('allocationconfigured', 'workshop'), workshop_message::TYPE_INFO);
355 break;
357 case workshop_allocation_result::STATUS_EXECUTED:
358 if ($message = $result->get_message()) {
359 $message = new workshop_message($message, workshop_message::TYPE_OK);
360 } else {
361 $message = new workshop_message(get_string('allocationdone', 'workshop'), workshop_message::TYPE_OK);
363 break;
365 default:
366 throw new coding_exception('Unknown allocation result status', $status);
369 // start with the message
370 $o = $this->render($message);
372 // display the details about the process if available
373 $logs = $result->get_logs();
374 if (is_array($logs) and !empty($logs)) {
375 $o .= html_writer::start_tag('ul', array('class' => 'allocation-init-results'));
376 foreach ($logs as $log) {
377 if ($log->type == 'debug' and !debugging('', DEBUG_DEVELOPER)) {
378 // display allocation debugging messages for developers only
379 continue;
381 $class = $log->type;
382 if ($log->indent) {
383 $class .= ' indent';
385 $o .= html_writer::tag('li', $log->message, array('class' => $class)).PHP_EOL;
387 $o .= html_writer::end_tag('ul');
390 return $o;
394 * Renders the workshop grading report
396 * @param workshop_grading_report $gradingreport
397 * @return string html code
399 protected function render_workshop_grading_report(workshop_grading_report $gradingreport) {
401 $data = $gradingreport->get_data();
402 $options = $gradingreport->get_options();
403 $grades = $data->grades;
404 $userinfo = $data->userinfo;
406 if (empty($grades)) {
407 return '';
410 $table = new html_table();
411 $table->attributes['class'] = 'grading-report';
413 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'), 'firstname', $options->sortby, $options->sorthow);
414 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'), 'lastname', $options->sortby, $options->sorthow);
415 if (self::fullname_format() == 'lf') {
416 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
417 } else {
418 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
421 $table->head = array();
422 $table->head[] = $sortbyname;
423 $table->head[] = $this->helper_sortable_heading(get_string('submission', 'workshop'), 'submissiontitle',
424 $options->sortby, $options->sorthow);
425 $table->head[] = $this->helper_sortable_heading(get_string('receivedgrades', 'workshop'));
426 if ($options->showsubmissiongrade) {
427 $table->head[] = $this->helper_sortable_heading(get_string('submissiongradeof', 'workshop', $data->maxgrade),
428 'submissiongrade', $options->sortby, $options->sorthow);
430 $table->head[] = $this->helper_sortable_heading(get_string('givengrades', 'workshop'));
431 if ($options->showgradinggrade) {
432 $table->head[] = $this->helper_sortable_heading(get_string('gradinggradeof', 'workshop', $data->maxgradinggrade),
433 'gradinggrade', $options->sortby, $options->sorthow);
436 $table->rowclasses = array();
437 $table->colclasses = array();
438 $table->data = array();
440 foreach ($grades as $participant) {
441 $numofreceived = count($participant->reviewedby);
442 $numofgiven = count($participant->reviewerof);
443 $published = $participant->submissionpublished;
445 // compute the number of <tr> table rows needed to display this participant
446 if ($numofreceived > 0 and $numofgiven > 0) {
447 $numoftrs = workshop::lcm($numofreceived, $numofgiven);
448 $spanreceived = $numoftrs / $numofreceived;
449 $spangiven = $numoftrs / $numofgiven;
450 } elseif ($numofreceived == 0 and $numofgiven > 0) {
451 $numoftrs = $numofgiven;
452 $spanreceived = $numoftrs;
453 $spangiven = $numoftrs / $numofgiven;
454 } elseif ($numofreceived > 0 and $numofgiven == 0) {
455 $numoftrs = $numofreceived;
456 $spanreceived = $numoftrs / $numofreceived;
457 $spangiven = $numoftrs;
458 } else {
459 $numoftrs = 1;
460 $spanreceived = 1;
461 $spangiven = 1;
464 for ($tr = 0; $tr < $numoftrs; $tr++) {
465 $row = new html_table_row();
466 if ($published) {
467 $row->attributes['class'] = 'published';
469 // column #1 - participant - spans over all rows
470 if ($tr == 0) {
471 $cell = new html_table_cell();
472 $cell->text = $this->helper_grading_report_participant($participant, $userinfo);
473 $cell->rowspan = $numoftrs;
474 $cell->attributes['class'] = 'participant';
475 $row->cells[] = $cell;
477 // column #2 - submission - spans over all rows
478 if ($tr == 0) {
479 $cell = new html_table_cell();
480 $cell->text = $this->helper_grading_report_submission($participant);
481 $cell->rowspan = $numoftrs;
482 $cell->attributes['class'] = 'submission';
483 $row->cells[] = $cell;
485 // column #3 - received grades
486 if ($tr % $spanreceived == 0) {
487 $idx = intval($tr / $spanreceived);
488 $assessment = self::array_nth($participant->reviewedby, $idx);
489 $cell = new html_table_cell();
490 $cell->text = $this->helper_grading_report_assessment($assessment, $options->showreviewernames, $userinfo,
491 get_string('gradereceivedfrom', 'workshop'));
492 $cell->rowspan = $spanreceived;
493 $cell->attributes['class'] = 'receivedgrade';
494 if (is_null($assessment) or is_null($assessment->grade)) {
495 $cell->attributes['class'] .= ' null';
496 } else {
497 $cell->attributes['class'] .= ' notnull';
499 $row->cells[] = $cell;
501 // column #4 - total grade for submission
502 if ($options->showsubmissiongrade and $tr == 0) {
503 $cell = new html_table_cell();
504 $cell->text = $this->helper_grading_report_grade($participant->submissiongrade, $participant->submissiongradeover);
505 $cell->rowspan = $numoftrs;
506 $cell->attributes['class'] = 'submissiongrade';
507 $row->cells[] = $cell;
509 // column #5 - given grades
510 if ($tr % $spangiven == 0) {
511 $idx = intval($tr / $spangiven);
512 $assessment = self::array_nth($participant->reviewerof, $idx);
513 $cell = new html_table_cell();
514 $cell->text = $this->helper_grading_report_assessment($assessment, $options->showauthornames, $userinfo,
515 get_string('gradegivento', 'workshop'));
516 $cell->rowspan = $spangiven;
517 $cell->attributes['class'] = 'givengrade';
518 if (is_null($assessment) or is_null($assessment->grade)) {
519 $cell->attributes['class'] .= ' null';
520 } else {
521 $cell->attributes['class'] .= ' notnull';
523 $row->cells[] = $cell;
525 // column #6 - total grade for assessment
526 if ($options->showgradinggrade and $tr == 0) {
527 $cell = new html_table_cell();
528 $cell->text = $this->helper_grading_report_grade($participant->gradinggrade);
529 $cell->rowspan = $numoftrs;
530 $cell->attributes['class'] = 'gradinggrade';
531 $row->cells[] = $cell;
534 $table->data[] = $row;
538 return html_writer::table($table);
542 * Renders the feedback for the author of the submission
544 * @param workshop_feedback_author $feedback
545 * @return string HTML
547 protected function render_workshop_feedback_author(workshop_feedback_author $feedback) {
548 return $this->helper_render_feedback($feedback);
552 * Renders the feedback for the reviewer of the submission
554 * @param workshop_feedback_reviewer $feedback
555 * @return string HTML
557 protected function render_workshop_feedback_reviewer(workshop_feedback_reviewer $feedback) {
558 return $this->helper_render_feedback($feedback);
562 * Helper method to rendering feedback
564 * @param workshop_feedback_author|workshop_feedback_reviewer $feedback
565 * @return string HTML
567 private function helper_render_feedback($feedback) {
569 $o = ''; // output HTML code
570 $o .= $this->output->container_start('feedback feedbackforauthor');
571 $o .= $this->output->container_start('header');
572 $o .= $this->output->heading(get_string('feedbackby', 'workshop', s(fullname($feedback->get_provider()))), 3, 'title');
574 $userpic = $this->output->user_picture($feedback->get_provider(), array('courseid' => $this->page->course->id, 'size' => 32));
575 $o .= $this->output->container($userpic, 'picture');
576 $o .= $this->output->container_end(); // end of header
578 $content = format_text($feedback->get_content(), $feedback->get_format(), array('overflowdiv' => true));
579 $o .= $this->output->container($content, 'content');
581 $o .= $this->output->container_end();
583 return $o;
587 * Renders the full assessment
589 * @param workshop_assessment $assessment
590 * @return string HTML
592 protected function render_workshop_assessment(workshop_assessment $assessment) {
594 $o = ''; // output HTML code
595 $anonymous = is_null($assessment->reviewer);
596 $classes = 'assessment-full';
597 if ($anonymous) {
598 $classes .= ' anonymous';
601 $o .= $this->output->container_start($classes);
602 $o .= $this->output->container_start('header');
604 if (!empty($assessment->title)) {
605 $title = s($assessment->title);
606 } else {
607 $title = get_string('assessment', 'workshop');
609 if (($assessment->url instanceof moodle_url) and ($this->page->url != $assessment->url)) {
610 $o .= $this->output->container(html_writer::link($assessment->url, $title), 'title');
611 } else {
612 $o .= $this->output->container($title, 'title');
615 if (!$anonymous) {
616 $reviewer = $assessment->reviewer;
617 $userpic = $this->output->user_picture($reviewer, array('courseid' => $this->page->course->id, 'size' => 32));
619 $userurl = new moodle_url('/user/view.php',
620 array('id' => $reviewer->id, 'course' => $this->page->course->id));
621 $a = new stdClass();
622 $a->name = fullname($reviewer);
623 $a->url = $userurl->out();
624 $byfullname = get_string('assessmentby', 'workshop', $a);
625 $oo = $this->output->container($userpic, 'picture');
626 $oo .= $this->output->container($byfullname, 'fullname');
628 $o .= $this->output->container($oo, 'reviewer');
631 if (is_null($assessment->realgrade)) {
632 $o .= $this->output->container(
633 get_string('notassessed', 'workshop'),
634 'grade nograde'
636 } else {
637 $a = new stdClass();
638 $a->max = $assessment->maxgrade;
639 $a->received = $assessment->realgrade;
640 $o .= $this->output->container(
641 get_string('gradeinfo', 'workshop', $a),
642 'grade'
645 if (!is_null($assessment->weight) and $assessment->weight != 1) {
646 $o .= $this->output->container(
647 get_string('weightinfo', 'workshop', $assessment->weight),
648 'weight'
653 $o .= $this->output->container_start('actions');
654 foreach ($assessment->actions as $action) {
655 $o .= $this->output->single_button($action->url, $action->label, $action->method);
657 $o .= $this->output->container_end(); // actions
659 $o .= $this->output->container_end(); // header
661 if (!is_null($assessment->form)) {
662 $o .= print_collapsible_region_start('assessment-form-wrapper', uniqid('workshop-assessment'),
663 get_string('assessmentform', 'workshop'), '', false, true);
664 $o .= $this->output->container(self::moodleform($assessment->form), 'assessment-form');
665 $o .= print_collapsible_region_end(true);
668 $o .= $this->output->container_end(); // main wrapper
670 return $o;
674 * Renders the assessment of an example submission
676 * @param workshop_example_assessment $assessment
677 * @return string HTML
679 protected function render_workshop_example_assessment(workshop_example_assessment $assessment) {
680 return $this->render_workshop_assessment($assessment);
684 * Renders the reference assessment of an example submission
686 * @param workshop_example_reference_assessment $assessment
687 * @return string HTML
689 protected function render_workshop_example_reference_assessment(workshop_example_reference_assessment $assessment) {
690 return $this->render_workshop_assessment($assessment);
694 * Renders a perpage selector for workshop listings
696 * The scripts using this have to define the $PAGE->url prior to calling this
697 * and deal with eventually submitted value themselves.
699 * @param int $current current value of the perpage parameter
700 * @return string HTML
702 public function perpage_selector($current=10) {
704 $options = array();
705 foreach (array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 1000) as $option) {
706 if ($option != $current) {
707 $options[$option] = $option;
710 $select = new single_select($this->page->url, 'perpage', $options, '', array('' => get_string('showingperpagechange', 'mod_workshop')));
711 $select->label = get_string('showingperpage', 'mod_workshop', $current);
712 $select->method = 'post';
714 return $this->output->container($this->output->render($select), 'perpagewidget');
717 ////////////////////////////////////////////////////////////////////////////
718 // Internal rendering helper methods
719 ////////////////////////////////////////////////////////////////////////////
722 * Renders a list of files attached to the submission
724 * If format==html, then format a html string. If format==text, then format a text-only string.
725 * Otherwise, returns html for non-images and html to display the image inline.
727 * @param int $submissionid submission identifier
728 * @param string format the format of the returned string - html|text
729 * @return string formatted text to be echoed
731 protected function helper_submission_attachments($submissionid, $format = 'html') {
732 global $CFG;
733 require_once($CFG->libdir.'/filelib.php');
735 $fs = get_file_storage();
736 $ctx = $this->page->context;
737 $files = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid);
739 $outputimgs = ''; // images to be displayed inline
740 $outputfiles = ''; // list of attachment files
742 foreach ($files as $file) {
743 if ($file->is_directory()) {
744 continue;
747 $filepath = $file->get_filepath();
748 $filename = $file->get_filename();
749 $fileurl = file_encode_url($CFG->wwwroot . '/pluginfile.php',
750 '/' . $ctx->id . '/mod_workshop/submission_attachment/' . $submissionid . $filepath . $filename, true);
751 $type = $file->get_mimetype();
752 $image = $this->output->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon'));
754 $linkhtml = html_writer::link($fileurl, $image) . substr($filepath, 1) . html_writer::link($fileurl, $filename);
755 $linktxt = "$filename [$fileurl]";
757 if ($format == 'html') {
758 if (file_mimetype_in_typegroup($type, 'web_image')) {
759 $preview = html_writer::empty_tag('img', array('src' => $fileurl, 'alt' => '', 'class' => 'preview'));
760 $preview = html_writer::tag('a', $preview, array('href' => $fileurl));
761 $outputimgs .= $this->output->container($preview);
763 } else {
764 $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type));
767 } else if ($format == 'text') {
768 $outputfiles .= $linktxt . PHP_EOL;
772 if ($format == 'html') {
773 if ($outputimgs) {
774 $outputimgs = $this->output->container($outputimgs, 'images');
777 if ($outputfiles) {
778 $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files'));
781 return $this->output->container($outputimgs . $outputfiles, 'attachments');
783 } else {
784 return $outputfiles;
789 * Renders the tasks for the single phase in the user plan
791 * @param stdClass $tasks
792 * @return string html code
794 protected function helper_user_plan_tasks(array $tasks) {
795 $out = '';
796 foreach ($tasks as $taskcode => $task) {
797 $classes = '';
798 $icon = null;
799 if ($task->completed === true) {
800 $classes .= ' completed';
801 } elseif ($task->completed === false) {
802 $classes .= ' fail';
803 } elseif ($task->completed === 'info') {
804 $classes .= ' info';
806 if (is_null($task->link)) {
807 $title = $task->title;
808 } else {
809 $title = html_writer::link($task->link, $task->title);
811 $title = $this->output->container($title, 'title');
812 $details = $this->output->container($task->details, 'details');
813 $out .= html_writer::tag('li', $title . $details, array('class' => $classes));
815 if ($out) {
816 $out = html_writer::tag('ul', $out, array('class' => 'tasks'));
818 return $out;
822 * Renders a text with icons to sort by the given column
824 * This is intended for table headings.
826 * @param string $text The heading text
827 * @param string $sortid The column id used for sorting
828 * @param string $sortby Currently sorted by (column id)
829 * @param string $sorthow Currently sorted how (ASC|DESC)
831 * @return string
833 protected function helper_sortable_heading($text, $sortid=null, $sortby=null, $sorthow=null) {
834 global $PAGE;
836 $out = html_writer::tag('span', $text, array('class'=>'text'));
838 if (!is_null($sortid)) {
839 if ($sortby !== $sortid or $sorthow !== 'ASC') {
840 $url = new moodle_url($PAGE->url);
841 $url->params(array('sortby' => $sortid, 'sorthow' => 'ASC'));
842 $out .= $this->output->action_icon($url, new pix_icon('t/up', get_string('sortasc', 'workshop')), null, array('class' => 'sort asc'));
844 if ($sortby !== $sortid or $sorthow !== 'DESC') {
845 $url = new moodle_url($PAGE->url);
846 $url->params(array('sortby' => $sortid, 'sorthow' => 'DESC'));
847 $out .= $this->output->action_icon($url, new pix_icon('t/down', get_string('sortdesc', 'workshop')), null, array('class' => 'sort desc'));
850 return $out;
854 * @param stdClass $participant
855 * @param array $userinfo
856 * @return string
858 protected function helper_grading_report_participant(stdclass $participant, array $userinfo) {
859 $userid = $participant->userid;
860 $out = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 35));
861 $out .= html_writer::tag('span', fullname($userinfo[$userid]));
863 return $out;
867 * @param stdClass $participant
868 * @return string
870 protected function helper_grading_report_submission(stdclass $participant) {
871 global $CFG;
873 if (is_null($participant->submissionid)) {
874 $out = $this->output->container(get_string('nosubmissionfound', 'workshop'), 'info');
875 } else {
876 $url = new moodle_url('/mod/workshop/submission.php',
877 array('cmid' => $this->page->context->instanceid, 'id' => $participant->submissionid));
878 $out = html_writer::link($url, format_string($participant->submissiontitle), array('class'=>'title'));
881 return $out;
885 * @todo Highlight the nulls
886 * @param stdClass|null $assessment
887 * @param bool $shownames
888 * @param string $separator between the grade and the reviewer/author
889 * @return string
891 protected function helper_grading_report_assessment($assessment, $shownames, array $userinfo, $separator) {
892 global $CFG;
894 if (is_null($assessment)) {
895 return get_string('nullgrade', 'workshop');
897 $a = new stdclass();
898 $a->grade = is_null($assessment->grade) ? get_string('nullgrade', 'workshop') : $assessment->grade;
899 $a->gradinggrade = is_null($assessment->gradinggrade) ? get_string('nullgrade', 'workshop') : $assessment->gradinggrade;
900 $a->weight = $assessment->weight;
901 // grrr the following logic should really be handled by a future language pack feature
902 if (is_null($assessment->gradinggradeover)) {
903 if ($a->weight == 1) {
904 $grade = get_string('formatpeergrade', 'workshop', $a);
905 } else {
906 $grade = get_string('formatpeergradeweighted', 'workshop', $a);
908 } else {
909 $a->gradinggradeover = $assessment->gradinggradeover;
910 if ($a->weight == 1) {
911 $grade = get_string('formatpeergradeover', 'workshop', $a);
912 } else {
913 $grade = get_string('formatpeergradeoverweighted', 'workshop', $a);
916 $url = new moodle_url('/mod/workshop/assessment.php',
917 array('asid' => $assessment->assessmentid));
918 $grade = html_writer::link($url, $grade, array('class'=>'grade'));
920 if ($shownames) {
921 $userid = $assessment->userid;
922 $name = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 16));
923 $name .= html_writer::tag('span', fullname($userinfo[$userid]), array('class' => 'fullname'));
924 $name = $separator . html_writer::tag('span', $name, array('class' => 'user'));
925 } else {
926 $name = '';
929 return $this->output->container($grade . $name, 'assessmentdetails');
933 * Formats the aggreagated grades
935 protected function helper_grading_report_grade($grade, $over=null) {
936 $a = new stdclass();
937 $a->grade = is_null($grade) ? get_string('nullgrade', 'workshop') : $grade;
938 if (is_null($over)) {
939 $text = get_string('formataggregatedgrade', 'workshop', $a);
940 } else {
941 $a->over = is_null($over) ? get_string('nullgrade', 'workshop') : $over;
942 $text = get_string('formataggregatedgradeover', 'workshop', $a);
944 return $text;
947 ////////////////////////////////////////////////////////////////////////////
948 // Static helpers
949 ////////////////////////////////////////////////////////////////////////////
952 * Helper method dealing with the fact we can not just fetch the output of moodleforms
954 * @param moodleform $mform
955 * @return string HTML
957 protected static function moodleform(moodleform $mform) {
959 ob_start();
960 $mform->display();
961 $o = ob_get_contents();
962 ob_end_clean();
964 return $o;
968 * Helper function returning the n-th item of the array
970 * @param array $a
971 * @param int $n from 0 to m, where m is th number of items in the array
972 * @return mixed the $n-th element of $a
974 protected static function array_nth(array $a, $n) {
975 $keys = array_keys($a);
976 if ($n < 0 or $n > count($keys) - 1) {
977 return null;
979 $key = $keys[$n];
980 return $a[$key];
984 * Tries to guess the fullname format set at the site
986 * @return string fl|lf
988 protected static function fullname_format() {
989 $fake = new stdclass(); // fake user
990 $fake->lastname = 'LLLL';
991 $fake->firstname = 'FFFF';
992 $fullname = get_string('fullnamedisplay', '', $fake);
993 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
994 return 'lf';
995 } else {
996 return 'fl';