MDL-54934 workshop: ensure "Current phase" is always separated
[moodle.git] / mod / workshop / renderer.php
blob58cfa6a040c6e8aae6934aebf694cc6474a47937
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_workshop
22 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Workshop module renderer class
31 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class mod_workshop_renderer extends plugin_renderer_base {
36 ////////////////////////////////////////////////////////////////////////////
37 // External API - methods to render workshop renderable components
38 ////////////////////////////////////////////////////////////////////////////
40 /**
41 * Renders workshop message
43 * @param workshop_message $message to display
44 * @return string html code
46 protected function render_workshop_message(workshop_message $message) {
48 $text = $message->get_message();
49 $url = $message->get_action_url();
50 $label = $message->get_action_label();
52 if (empty($text) and empty($label)) {
53 return '';
56 switch ($message->get_type()) {
57 case workshop_message::TYPE_OK:
58 $sty = 'ok';
59 break;
60 case workshop_message::TYPE_ERROR:
61 $sty = 'error';
62 break;
63 default:
64 $sty = 'info';
67 $o = html_writer::tag('span', $message->get_message());
69 if (!is_null($url) and !is_null($label)) {
70 $o .= $this->output->single_button($url, $label, 'get');
73 return $this->output->container($o, array('message', $sty));
77 /**
78 * Renders full workshop submission
80 * @param workshop_submission $submission
81 * @return string HTML
83 protected function render_workshop_submission(workshop_submission $submission) {
84 global $CFG;
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 $additionalfields = explode(',', user_picture::fields());
106 $author = username_load_fields_from_object($author, $submission, 'author', $additionalfields);
107 $userpic = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 64));
108 $userurl = new moodle_url('/user/view.php',
109 array('id' => $author->id, 'course' => $this->page->course->id));
110 $a = new stdclass();
111 $a->name = fullname($author);
112 $a->url = $userurl->out();
113 $byfullname = get_string('byfullname', 'workshop', $a);
114 $oo = $this->output->container($userpic, 'picture');
115 $oo .= $this->output->container($byfullname, 'fullname');
117 $o .= $this->output->container($oo, 'author');
120 $created = get_string('userdatecreated', 'workshop', userdate($submission->timecreated));
121 $o .= $this->output->container($created, 'userdate created');
123 if ($submission->timemodified > $submission->timecreated) {
124 $modified = get_string('userdatemodified', 'workshop', userdate($submission->timemodified));
125 $o .= $this->output->container($modified, 'userdate modified');
128 $o .= $this->output->container_end(); // end of header
130 $content = file_rewrite_pluginfile_urls($submission->content, 'pluginfile.php', $this->page->context->id,
131 'mod_workshop', 'submission_content', $submission->id);
132 $content = format_text($content, $submission->contentformat, array('overflowdiv'=>true));
133 if (!empty($content)) {
134 if (!empty($CFG->enableplagiarism)) {
135 require_once($CFG->libdir.'/plagiarismlib.php');
136 $content .= plagiarism_get_links(array('userid' => $submission->authorid,
137 'content' => $submission->content,
138 'cmid' => $this->page->cm->id,
139 'course' => $this->page->course));
142 $o .= $this->output->container($content, 'content');
144 $o .= $this->helper_submission_attachments($submission->id, 'html');
146 $o .= $this->output->container_end(); // end of submission-full
148 return $o;
152 * Renders short summary of the submission
154 * @param workshop_submission_summary $summary
155 * @return string text to be echo'ed
157 protected function render_workshop_submission_summary(workshop_submission_summary $summary) {
159 $o = ''; // output HTML code
160 $anonymous = $summary->is_anonymous();
161 $classes = 'submission-summary';
163 if ($anonymous) {
164 $classes .= ' anonymous';
167 $gradestatus = '';
169 if ($summary->status == 'notgraded') {
170 $classes .= ' notgraded';
171 $gradestatus = $this->output->container(get_string('nogradeyet', 'workshop'), 'grade-status');
173 } else if ($summary->status == 'graded') {
174 $classes .= ' graded';
175 $gradestatus = $this->output->container(get_string('alreadygraded', 'workshop'), 'grade-status');
178 $o .= $this->output->container_start($classes); // main wrapper
179 $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
181 if (!$anonymous) {
182 $author = new stdClass();
183 $additionalfields = explode(',', user_picture::fields());
184 $author = username_load_fields_from_object($author, $summary, 'author', $additionalfields);
185 $userpic = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 35));
186 $userurl = new moodle_url('/user/view.php',
187 array('id' => $author->id, 'course' => $this->page->course->id));
188 $a = new stdClass();
189 $a->name = fullname($author);
190 $a->url = $userurl->out();
191 $byfullname = get_string('byfullname', 'workshop', $a);
193 $oo = $this->output->container($userpic, 'picture');
194 $oo .= $this->output->container($byfullname, 'fullname');
195 $o .= $this->output->container($oo, 'author');
198 $created = get_string('userdatecreated', 'workshop', userdate($summary->timecreated));
199 $o .= $this->output->container($created, 'userdate created');
201 if ($summary->timemodified > $summary->timecreated) {
202 $modified = get_string('userdatemodified', 'workshop', userdate($summary->timemodified));
203 $o .= $this->output->container($modified, 'userdate modified');
206 $o .= $gradestatus;
207 $o .= $this->output->container_end(); // end of the main wrapper
208 return $o;
212 * Renders full workshop example submission
214 * @param workshop_example_submission $example
215 * @return string HTML
217 protected function render_workshop_example_submission(workshop_example_submission $example) {
219 $o = ''; // output HTML code
220 $classes = 'submission-full example';
221 $o .= $this->output->container_start($classes);
222 $o .= $this->output->container_start('header');
223 $o .= $this->output->container(format_string($example->title), array('class' => 'title'));
224 $o .= $this->output->container_end(); // end of header
226 $content = file_rewrite_pluginfile_urls($example->content, 'pluginfile.php', $this->page->context->id,
227 'mod_workshop', 'submission_content', $example->id);
228 $content = format_text($content, $example->contentformat, array('overflowdiv'=>true));
229 $o .= $this->output->container($content, 'content');
231 $o .= $this->helper_submission_attachments($example->id, 'html');
233 $o .= $this->output->container_end(); // end of submission-full
235 return $o;
239 * Renders short summary of the example submission
241 * @param workshop_example_submission_summary $summary
242 * @return string text to be echo'ed
244 protected function render_workshop_example_submission_summary(workshop_example_submission_summary $summary) {
246 $o = ''; // output HTML code
248 // wrapping box
249 $o .= $this->output->box_start('generalbox example-summary ' . $summary->status);
251 // title
252 $o .= $this->output->container_start('example-title');
253 $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
255 if ($summary->editable) {
256 $o .= $this->output->action_icon($summary->editurl, new pix_icon('i/edit', get_string('edit')));
258 $o .= $this->output->container_end();
260 // additional info
261 if ($summary->status == 'notgraded') {
262 $o .= $this->output->container(get_string('nogradeyet', 'workshop'), 'example-info nograde');
263 } else {
264 $o .= $this->output->container(get_string('gradeinfo', 'workshop' , $summary->gradeinfo), 'example-info grade');
267 // button to assess
268 $button = new single_button($summary->assessurl, $summary->assesslabel, 'get');
269 $o .= $this->output->container($this->output->render($button), 'example-actions');
271 // end of wrapping box
272 $o .= $this->output->box_end();
274 return $o;
278 * Renders the user plannner tool
280 * @param workshop_user_plan $plan prepared for the user
281 * @return string html code to be displayed
283 protected function render_workshop_user_plan(workshop_user_plan $plan) {
284 $table = new html_table();
285 $table->attributes['class'] = 'userplan';
286 $table->head = array();
287 $table->colclasses = array();
288 $row = new html_table_row();
289 $row->attributes['class'] = 'phasetasks';
290 foreach ($plan->phases as $phasecode => $phase) {
291 $title = html_writer::tag('span', $phase->title);
292 if ($phase->active) {
293 $title .= ' ' . html_writer::tag('span', get_string('userplancurrentphase', 'workshop'),
294 array('class' => 'accesshide'));
296 $actions = '';
297 foreach ($phase->actions as $action) {
298 switch ($action->type) {
299 case 'switchphase':
300 $icon = 'i/marker';
301 if ($phasecode == workshop::PHASE_ASSESSMENT
302 and $plan->workshop->phase == workshop::PHASE_SUBMISSION
303 and $plan->workshop->phaseswitchassessment) {
304 $icon = 'i/scheduled';
306 $actions .= $this->output->action_icon($action->url, new pix_icon($icon, get_string('switchphase', 'workshop')));
307 break;
310 if (!empty($actions)) {
311 $actions = $this->output->container($actions, 'actions');
313 $table->head[] = $this->output->container($title . $actions);
314 $classes = 'phase' . $phasecode;
315 if ($phase->active) {
316 $classes .= ' active';
317 } else {
318 $classes .= ' nonactive';
320 $table->colclasses[] = $classes;
321 $cell = new html_table_cell();
322 $cell->text = $this->helper_user_plan_tasks($phase->tasks);
323 $row->cells[] = $cell;
325 $table->data = array($row);
327 return html_writer::table($table);
331 * Renders the result of the submissions allocation process
333 * @param workshop_allocation_result $result as returned by the allocator's init() method
334 * @return string HTML to be echoed
336 protected function render_workshop_allocation_result(workshop_allocation_result $result) {
337 global $CFG;
339 $status = $result->get_status();
341 if (is_null($status) or $status == workshop_allocation_result::STATUS_VOID) {
342 debugging('Attempt to render workshop_allocation_result with empty status', DEBUG_DEVELOPER);
343 return '';
346 switch ($status) {
347 case workshop_allocation_result::STATUS_FAILED:
348 if ($message = $result->get_message()) {
349 $message = new workshop_message($message, workshop_message::TYPE_ERROR);
350 } else {
351 $message = new workshop_message(get_string('allocationerror', 'workshop'), workshop_message::TYPE_ERROR);
353 break;
355 case workshop_allocation_result::STATUS_CONFIGURED:
356 if ($message = $result->get_message()) {
357 $message = new workshop_message($message, workshop_message::TYPE_INFO);
358 } else {
359 $message = new workshop_message(get_string('allocationconfigured', 'workshop'), workshop_message::TYPE_INFO);
361 break;
363 case workshop_allocation_result::STATUS_EXECUTED:
364 if ($message = $result->get_message()) {
365 $message = new workshop_message($message, workshop_message::TYPE_OK);
366 } else {
367 $message = new workshop_message(get_string('allocationdone', 'workshop'), workshop_message::TYPE_OK);
369 break;
371 default:
372 throw new coding_exception('Unknown allocation result status', $status);
375 // start with the message
376 $o = $this->render($message);
378 // display the details about the process if available
379 $logs = $result->get_logs();
380 if (is_array($logs) and !empty($logs)) {
381 $o .= html_writer::start_tag('ul', array('class' => 'allocation-init-results'));
382 foreach ($logs as $log) {
383 if ($log->type == 'debug' and !$CFG->debugdeveloper) {
384 // display allocation debugging messages for developers only
385 continue;
387 $class = $log->type;
388 if ($log->indent) {
389 $class .= ' indent';
391 $o .= html_writer::tag('li', $log->message, array('class' => $class)).PHP_EOL;
393 $o .= html_writer::end_tag('ul');
396 return $o;
400 * Renders the workshop grading report
402 * @param workshop_grading_report $gradingreport
403 * @return string html code
405 protected function render_workshop_grading_report(workshop_grading_report $gradingreport) {
407 $data = $gradingreport->get_data();
408 $options = $gradingreport->get_options();
409 $grades = $data->grades;
410 $userinfo = $data->userinfo;
412 if (empty($grades)) {
413 return '';
416 $table = new html_table();
417 $table->attributes['class'] = 'grading-report';
419 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'), 'firstname', $options->sortby, $options->sorthow);
420 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'), 'lastname', $options->sortby, $options->sorthow);
421 if (self::fullname_format() == 'lf') {
422 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
423 } else {
424 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
427 $sortbysubmisstiontitle = $this->helper_sortable_heading(get_string('submission', 'workshop'), 'submissiontitle',
428 $options->sortby, $options->sorthow);
429 $sortbysubmisstionlastmodified = $this->helper_sortable_heading(get_string('submissionlastmodified', 'workshop'),
430 'submissionmodified', $options->sortby, $options->sorthow);
431 $sortbysubmisstion = $sortbysubmisstiontitle . ' / ' . $sortbysubmisstionlastmodified;
433 $table->head = array();
434 $table->head[] = $sortbyname;
435 $table->head[] = $sortbysubmisstion;
437 // If we are in submission phase ignore the following headers (columns).
438 if ($options->workshopphase != workshop::PHASE_SUBMISSION) {
439 $table->head[] = $this->helper_sortable_heading(get_string('receivedgrades', 'workshop'));
440 if ($options->showsubmissiongrade) {
441 $table->head[] = $this->helper_sortable_heading(get_string('submissiongradeof', 'workshop', $data->maxgrade),
442 'submissiongrade', $options->sortby, $options->sorthow);
444 $table->head[] = $this->helper_sortable_heading(get_string('givengrades', 'workshop'));
445 if ($options->showgradinggrade) {
446 $table->head[] = $this->helper_sortable_heading(get_string('gradinggradeof', 'workshop', $data->maxgradinggrade),
447 'gradinggrade', $options->sortby, $options->sorthow);
450 $table->rowclasses = array();
451 $table->colclasses = array();
452 $table->data = array();
454 foreach ($grades as $participant) {
455 $numofreceived = count($participant->reviewedby);
456 $numofgiven = count($participant->reviewerof);
457 $published = $participant->submissionpublished;
459 // compute the number of <tr> table rows needed to display this participant
460 if ($numofreceived > 0 and $numofgiven > 0) {
461 $numoftrs = workshop::lcm($numofreceived, $numofgiven);
462 $spanreceived = $numoftrs / $numofreceived;
463 $spangiven = $numoftrs / $numofgiven;
464 } elseif ($numofreceived == 0 and $numofgiven > 0) {
465 $numoftrs = $numofgiven;
466 $spanreceived = $numoftrs;
467 $spangiven = $numoftrs / $numofgiven;
468 } elseif ($numofreceived > 0 and $numofgiven == 0) {
469 $numoftrs = $numofreceived;
470 $spanreceived = $numoftrs / $numofreceived;
471 $spangiven = $numoftrs;
472 } else {
473 $numoftrs = 1;
474 $spanreceived = 1;
475 $spangiven = 1;
478 for ($tr = 0; $tr < $numoftrs; $tr++) {
479 $row = new html_table_row();
480 if ($published) {
481 $row->attributes['class'] = 'published';
483 // column #1 - participant - spans over all rows
484 if ($tr == 0) {
485 $cell = new html_table_cell();
486 $cell->text = $this->helper_grading_report_participant($participant, $userinfo);
487 $cell->rowspan = $numoftrs;
488 $cell->attributes['class'] = 'participant';
489 $row->cells[] = $cell;
491 // column #2 - submission - spans over all rows
492 if ($tr == 0) {
493 $cell = new html_table_cell();
494 $cell->text = $this->helper_grading_report_submission($participant);
495 $cell->rowspan = $numoftrs;
496 $cell->attributes['class'] = 'submission';
497 $row->cells[] = $cell;
500 // If we are in submission phase ignore the following columns.
501 if ($options->workshopphase == workshop::PHASE_SUBMISSION) {
502 $table->data[] = $row;
503 continue;
506 // column #3 - received grades
507 if ($tr % $spanreceived == 0) {
508 $idx = intval($tr / $spanreceived);
509 $assessment = self::array_nth($participant->reviewedby, $idx);
510 $cell = new html_table_cell();
511 $cell->text = $this->helper_grading_report_assessment($assessment, $options->showreviewernames, $userinfo,
512 get_string('gradereceivedfrom', 'workshop'));
513 $cell->rowspan = $spanreceived;
514 $cell->attributes['class'] = 'receivedgrade';
515 if (is_null($assessment) or is_null($assessment->grade)) {
516 $cell->attributes['class'] .= ' null';
517 } else {
518 $cell->attributes['class'] .= ' notnull';
520 $row->cells[] = $cell;
522 // column #4 - total grade for submission
523 if ($options->showsubmissiongrade and $tr == 0) {
524 $cell = new html_table_cell();
525 $cell->text = $this->helper_grading_report_grade($participant->submissiongrade, $participant->submissiongradeover);
526 $cell->rowspan = $numoftrs;
527 $cell->attributes['class'] = 'submissiongrade';
528 $row->cells[] = $cell;
530 // column #5 - given grades
531 if ($tr % $spangiven == 0) {
532 $idx = intval($tr / $spangiven);
533 $assessment = self::array_nth($participant->reviewerof, $idx);
534 $cell = new html_table_cell();
535 $cell->text = $this->helper_grading_report_assessment($assessment, $options->showauthornames, $userinfo,
536 get_string('gradegivento', 'workshop'));
537 $cell->rowspan = $spangiven;
538 $cell->attributes['class'] = 'givengrade';
539 if (is_null($assessment) or is_null($assessment->grade)) {
540 $cell->attributes['class'] .= ' null';
541 } else {
542 $cell->attributes['class'] .= ' notnull';
544 $row->cells[] = $cell;
546 // column #6 - total grade for assessment
547 if ($options->showgradinggrade and $tr == 0) {
548 $cell = new html_table_cell();
549 $cell->text = $this->helper_grading_report_grade($participant->gradinggrade);
550 $cell->rowspan = $numoftrs;
551 $cell->attributes['class'] = 'gradinggrade';
552 $row->cells[] = $cell;
555 $table->data[] = $row;
559 return html_writer::table($table);
563 * Renders the feedback for the author of the submission
565 * @param workshop_feedback_author $feedback
566 * @return string HTML
568 protected function render_workshop_feedback_author(workshop_feedback_author $feedback) {
569 return $this->helper_render_feedback($feedback);
573 * Renders the feedback for the reviewer of the submission
575 * @param workshop_feedback_reviewer $feedback
576 * @return string HTML
578 protected function render_workshop_feedback_reviewer(workshop_feedback_reviewer $feedback) {
579 return $this->helper_render_feedback($feedback);
583 * Helper method to rendering feedback
585 * @param workshop_feedback_author|workshop_feedback_reviewer $feedback
586 * @return string HTML
588 private function helper_render_feedback($feedback) {
590 $o = ''; // output HTML code
591 $o .= $this->output->container_start('feedback feedbackforauthor');
592 $o .= $this->output->container_start('header');
593 $o .= $this->output->heading(get_string('feedbackby', 'workshop', s(fullname($feedback->get_provider()))), 3, 'title');
595 $userpic = $this->output->user_picture($feedback->get_provider(), array('courseid' => $this->page->course->id, 'size' => 32));
596 $o .= $this->output->container($userpic, 'picture');
597 $o .= $this->output->container_end(); // end of header
599 $content = format_text($feedback->get_content(), $feedback->get_format(), array('overflowdiv' => true));
600 $o .= $this->output->container($content, 'content');
602 $o .= $this->output->container_end();
604 return $o;
608 * Renders the full assessment
610 * @param workshop_assessment $assessment
611 * @return string HTML
613 protected function render_workshop_assessment(workshop_assessment $assessment) {
615 $o = ''; // output HTML code
616 $anonymous = is_null($assessment->reviewer);
617 $classes = 'assessment-full';
618 if ($anonymous) {
619 $classes .= ' anonymous';
622 $o .= $this->output->container_start($classes);
623 $o .= $this->output->container_start('header');
625 if (!empty($assessment->title)) {
626 $title = s($assessment->title);
627 } else {
628 $title = get_string('assessment', 'workshop');
630 if (($assessment->url instanceof moodle_url) and ($this->page->url != $assessment->url)) {
631 $o .= $this->output->container(html_writer::link($assessment->url, $title), 'title');
632 } else {
633 $o .= $this->output->container($title, 'title');
636 if (!$anonymous) {
637 $reviewer = $assessment->reviewer;
638 $userpic = $this->output->user_picture($reviewer, array('courseid' => $this->page->course->id, 'size' => 32));
640 $userurl = new moodle_url('/user/view.php',
641 array('id' => $reviewer->id, 'course' => $this->page->course->id));
642 $a = new stdClass();
643 $a->name = fullname($reviewer);
644 $a->url = $userurl->out();
645 $byfullname = get_string('assessmentby', 'workshop', $a);
646 $oo = $this->output->container($userpic, 'picture');
647 $oo .= $this->output->container($byfullname, 'fullname');
649 $o .= $this->output->container($oo, 'reviewer');
652 if (is_null($assessment->realgrade)) {
653 $o .= $this->output->container(
654 get_string('notassessed', 'workshop'),
655 'grade nograde'
657 } else {
658 $a = new stdClass();
659 $a->max = $assessment->maxgrade;
660 $a->received = $assessment->realgrade;
661 $o .= $this->output->container(
662 get_string('gradeinfo', 'workshop', $a),
663 'grade'
666 if (!is_null($assessment->weight) and $assessment->weight != 1) {
667 $o .= $this->output->container(
668 get_string('weightinfo', 'workshop', $assessment->weight),
669 'weight'
674 $o .= $this->output->container_start('actions');
675 foreach ($assessment->actions as $action) {
676 $o .= $this->output->single_button($action->url, $action->label, $action->method);
678 $o .= $this->output->container_end(); // actions
680 $o .= $this->output->container_end(); // header
682 if (!is_null($assessment->form)) {
683 $o .= print_collapsible_region_start('assessment-form-wrapper', uniqid('workshop-assessment'),
684 get_string('assessmentform', 'workshop'), '', false, true);
685 $o .= $this->output->container(self::moodleform($assessment->form), 'assessment-form');
686 $o .= print_collapsible_region_end(true);
688 if (!$assessment->form->is_editable()) {
689 $o .= $this->overall_feedback($assessment);
693 $o .= $this->output->container_end(); // main wrapper
695 return $o;
699 * Renders the assessment of an example submission
701 * @param workshop_example_assessment $assessment
702 * @return string HTML
704 protected function render_workshop_example_assessment(workshop_example_assessment $assessment) {
705 return $this->render_workshop_assessment($assessment);
709 * Renders the reference assessment of an example submission
711 * @param workshop_example_reference_assessment $assessment
712 * @return string HTML
714 protected function render_workshop_example_reference_assessment(workshop_example_reference_assessment $assessment) {
715 return $this->render_workshop_assessment($assessment);
719 * Renders the overall feedback for the author of the submission
721 * @param workshop_assessment $assessment
722 * @return string HTML
724 protected function overall_feedback(workshop_assessment $assessment) {
726 $content = $assessment->get_overall_feedback_content();
728 if ($content === false) {
729 return '';
732 $o = '';
734 if (!is_null($content)) {
735 $o .= $this->output->container($content, 'content');
738 $attachments = $assessment->get_overall_feedback_attachments();
740 if (!empty($attachments)) {
741 $o .= $this->output->container_start('attachments');
742 $images = '';
743 $files = '';
744 foreach ($attachments as $attachment) {
745 $icon = $this->output->pix_icon(file_file_icon($attachment), get_mimetype_description($attachment),
746 'moodle', array('class' => 'icon'));
747 $link = html_writer::link($attachment->fileurl, $icon.' '.substr($attachment->filepath.$attachment->filename, 1));
748 if (file_mimetype_in_typegroup($attachment->mimetype, 'web_image')) {
749 $preview = html_writer::empty_tag('img', array('src' => $attachment->previewurl, 'alt' => '', 'class' => 'preview'));
750 $preview = html_writer::tag('a', $preview, array('href' => $attachment->fileurl));
751 $images .= $this->output->container($preview);
752 } else {
753 $files .= html_writer::tag('li', $link, array('class' => $attachment->mimetype));
756 if ($images) {
757 $images = $this->output->container($images, 'images');
760 if ($files) {
761 $files = html_writer::tag('ul', $files, array('class' => 'files'));
764 $o .= $images.$files;
765 $o .= $this->output->container_end();
768 if ($o === '') {
769 return '';
772 $o = $this->output->box($o, 'overallfeedback');
773 $o = print_collapsible_region($o, 'overall-feedback-wrapper', uniqid('workshop-overall-feedback'),
774 get_string('overallfeedback', 'workshop'), '', false, true);
776 return $o;
780 * Renders a perpage selector for workshop listings
782 * The scripts using this have to define the $PAGE->url prior to calling this
783 * and deal with eventually submitted value themselves.
785 * @param int $current current value of the perpage parameter
786 * @return string HTML
788 public function perpage_selector($current=10) {
790 $options = array();
791 foreach (array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 1000) as $option) {
792 if ($option != $current) {
793 $options[$option] = $option;
796 $select = new single_select($this->page->url, 'perpage', $options, '', array('' => get_string('showingperpagechange', 'mod_workshop')));
797 $select->label = get_string('showingperpage', 'mod_workshop', $current);
798 $select->method = 'post';
800 return $this->output->container($this->output->render($select), 'perpagewidget');
804 * Renders the user's final grades
806 * @param workshop_final_grades $grades with the info about grades in the gradebook
807 * @return string HTML
809 protected function render_workshop_final_grades(workshop_final_grades $grades) {
811 $out = html_writer::start_tag('div', array('class' => 'finalgrades'));
813 if (!empty($grades->submissiongrade)) {
814 $cssclass = 'grade submissiongrade';
815 if ($grades->submissiongrade->hidden) {
816 $cssclass .= ' hiddengrade';
818 $out .= html_writer::tag(
819 'div',
820 html_writer::tag('div', get_string('submissiongrade', 'mod_workshop'), array('class' => 'gradetype')) .
821 html_writer::tag('div', $grades->submissiongrade->str_long_grade, array('class' => 'gradevalue')),
822 array('class' => $cssclass)
826 if (!empty($grades->assessmentgrade)) {
827 $cssclass = 'grade assessmentgrade';
828 if ($grades->assessmentgrade->hidden) {
829 $cssclass .= ' hiddengrade';
831 $out .= html_writer::tag(
832 'div',
833 html_writer::tag('div', get_string('gradinggrade', 'mod_workshop'), array('class' => 'gradetype')) .
834 html_writer::tag('div', $grades->assessmentgrade->str_long_grade, array('class' => 'gradevalue')),
835 array('class' => $cssclass)
839 $out .= html_writer::end_tag('div');
841 return $out;
844 ////////////////////////////////////////////////////////////////////////////
845 // Internal rendering helper methods
846 ////////////////////////////////////////////////////////////////////////////
849 * Renders a list of files attached to the submission
851 * If format==html, then format a html string. If format==text, then format a text-only string.
852 * Otherwise, returns html for non-images and html to display the image inline.
854 * @param int $submissionid submission identifier
855 * @param string format the format of the returned string - html|text
856 * @return string formatted text to be echoed
858 protected function helper_submission_attachments($submissionid, $format = 'html') {
859 global $CFG;
860 require_once($CFG->libdir.'/filelib.php');
862 $fs = get_file_storage();
863 $ctx = $this->page->context;
864 $files = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid);
866 $outputimgs = ''; // images to be displayed inline
867 $outputfiles = ''; // list of attachment files
869 foreach ($files as $file) {
870 if ($file->is_directory()) {
871 continue;
874 $filepath = $file->get_filepath();
875 $filename = $file->get_filename();
876 $fileurl = moodle_url::make_pluginfile_url($ctx->id, 'mod_workshop', 'submission_attachment',
877 $submissionid, $filepath, $filename, true);
878 $embedurl = moodle_url::make_pluginfile_url($ctx->id, 'mod_workshop', 'submission_attachment',
879 $submissionid, $filepath, $filename, false);
880 $embedurl = new moodle_url($embedurl, array('preview' => 'bigthumb'));
881 $type = $file->get_mimetype();
882 $image = $this->output->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon'));
884 $linkhtml = html_writer::link($fileurl, $image . substr($filepath, 1) . $filename);
885 $linktxt = "$filename [$fileurl]";
887 if ($format == 'html') {
888 if (file_mimetype_in_typegroup($type, 'web_image')) {
889 $preview = html_writer::empty_tag('img', array('src' => $embedurl, 'alt' => '', 'class' => 'preview'));
890 $preview = html_writer::tag('a', $preview, array('href' => $fileurl));
891 $outputimgs .= $this->output->container($preview);
893 } else {
894 $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type));
897 } else if ($format == 'text') {
898 $outputfiles .= $linktxt . PHP_EOL;
901 if (!empty($CFG->enableplagiarism)) {
902 require_once($CFG->libdir.'/plagiarismlib.php');
903 $outputfiles .= plagiarism_get_links(array('userid' => $file->get_userid(),
904 'file' => $file,
905 'cmid' => $this->page->cm->id,
906 'course' => $this->page->course->id));
910 if ($format == 'html') {
911 if ($outputimgs) {
912 $outputimgs = $this->output->container($outputimgs, 'images');
915 if ($outputfiles) {
916 $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files'));
919 return $this->output->container($outputimgs . $outputfiles, 'attachments');
921 } else {
922 return $outputfiles;
927 * Renders the tasks for the single phase in the user plan
929 * @param stdClass $tasks
930 * @return string html code
932 protected function helper_user_plan_tasks(array $tasks) {
933 $out = '';
934 foreach ($tasks as $taskcode => $task) {
935 $classes = '';
936 $icon = null;
937 if ($task->completed === true) {
938 $classes .= ' completed';
939 } elseif ($task->completed === false) {
940 $classes .= ' fail';
941 } elseif ($task->completed === 'info') {
942 $classes .= ' info';
944 if (is_null($task->link)) {
945 $title = $task->title;
946 } else {
947 $title = html_writer::link($task->link, $task->title);
949 $title = $this->output->container($title, 'title');
950 $details = $this->output->container($task->details, 'details');
951 $out .= html_writer::tag('li', $title . $details, array('class' => $classes));
953 if ($out) {
954 $out = html_writer::tag('ul', $out, array('class' => 'tasks'));
956 return $out;
960 * Renders a text with icons to sort by the given column
962 * This is intended for table headings.
964 * @param string $text The heading text
965 * @param string $sortid The column id used for sorting
966 * @param string $sortby Currently sorted by (column id)
967 * @param string $sorthow Currently sorted how (ASC|DESC)
969 * @return string
971 protected function helper_sortable_heading($text, $sortid=null, $sortby=null, $sorthow=null) {
972 global $PAGE;
974 $out = html_writer::tag('span', $text, array('class'=>'text'));
976 if (!is_null($sortid)) {
977 if ($sortby !== $sortid or $sorthow !== 'ASC') {
978 $url = new moodle_url($PAGE->url);
979 $url->params(array('sortby' => $sortid, 'sorthow' => 'ASC'));
980 $out .= $this->output->action_icon($url, new pix_icon('t/sort_asc', get_string('sortasc', 'workshop')),
981 null, array('class' => 'iconsort sort asc'));
983 if ($sortby !== $sortid or $sorthow !== 'DESC') {
984 $url = new moodle_url($PAGE->url);
985 $url->params(array('sortby' => $sortid, 'sorthow' => 'DESC'));
986 $out .= $this->output->action_icon($url, new pix_icon('t/sort_desc', get_string('sortdesc', 'workshop')),
987 null, array('class' => 'iconsort sort desc'));
990 return $out;
994 * @param stdClass $participant
995 * @param array $userinfo
996 * @return string
998 protected function helper_grading_report_participant(stdclass $participant, array $userinfo) {
999 $userid = $participant->userid;
1000 $out = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 35));
1001 $out .= html_writer::tag('span', fullname($userinfo[$userid]));
1003 return $out;
1007 * @param stdClass $participant
1008 * @return string
1010 protected function helper_grading_report_submission(stdclass $participant) {
1011 global $CFG;
1013 if (is_null($participant->submissionid)) {
1014 $out = $this->output->container(get_string('nosubmissionfound', 'workshop'), 'info');
1015 } else {
1016 $url = new moodle_url('/mod/workshop/submission.php',
1017 array('cmid' => $this->page->context->instanceid, 'id' => $participant->submissionid));
1018 $out = html_writer::link($url, format_string($participant->submissiontitle), array('class'=>'title'));
1020 $lastmodified = get_string('userdatemodified', 'workshop', userdate($participant->submissionmodified));
1021 $out .= html_writer::tag('div', $lastmodified, array('class' => 'lastmodified'));
1024 return $out;
1028 * @todo Highlight the nulls
1029 * @param stdClass|null $assessment
1030 * @param bool $shownames
1031 * @param string $separator between the grade and the reviewer/author
1032 * @return string
1034 protected function helper_grading_report_assessment($assessment, $shownames, array $userinfo, $separator) {
1035 global $CFG;
1037 if (is_null($assessment)) {
1038 return get_string('nullgrade', 'workshop');
1040 $a = new stdclass();
1041 $a->grade = is_null($assessment->grade) ? get_string('nullgrade', 'workshop') : $assessment->grade;
1042 $a->gradinggrade = is_null($assessment->gradinggrade) ? get_string('nullgrade', 'workshop') : $assessment->gradinggrade;
1043 $a->weight = $assessment->weight;
1044 // grrr the following logic should really be handled by a future language pack feature
1045 if (is_null($assessment->gradinggradeover)) {
1046 if ($a->weight == 1) {
1047 $grade = get_string('formatpeergrade', 'workshop', $a);
1048 } else {
1049 $grade = get_string('formatpeergradeweighted', 'workshop', $a);
1051 } else {
1052 $a->gradinggradeover = $assessment->gradinggradeover;
1053 if ($a->weight == 1) {
1054 $grade = get_string('formatpeergradeover', 'workshop', $a);
1055 } else {
1056 $grade = get_string('formatpeergradeoverweighted', 'workshop', $a);
1059 $url = new moodle_url('/mod/workshop/assessment.php',
1060 array('asid' => $assessment->assessmentid));
1061 $grade = html_writer::link($url, $grade, array('class'=>'grade'));
1063 if ($shownames) {
1064 $userid = $assessment->userid;
1065 $name = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 16));
1066 $name .= html_writer::tag('span', fullname($userinfo[$userid]), array('class' => 'fullname'));
1067 $name = $separator . html_writer::tag('span', $name, array('class' => 'user'));
1068 } else {
1069 $name = '';
1072 return $this->output->container($grade . $name, 'assessmentdetails');
1076 * Formats the aggreagated grades
1078 protected function helper_grading_report_grade($grade, $over=null) {
1079 $a = new stdclass();
1080 $a->grade = is_null($grade) ? get_string('nullgrade', 'workshop') : $grade;
1081 if (is_null($over)) {
1082 $text = get_string('formataggregatedgrade', 'workshop', $a);
1083 } else {
1084 $a->over = is_null($over) ? get_string('nullgrade', 'workshop') : $over;
1085 $text = get_string('formataggregatedgradeover', 'workshop', $a);
1087 return $text;
1090 ////////////////////////////////////////////////////////////////////////////
1091 // Static helpers
1092 ////////////////////////////////////////////////////////////////////////////
1095 * Helper method dealing with the fact we can not just fetch the output of moodleforms
1097 * @param moodleform $mform
1098 * @return string HTML
1100 protected static function moodleform(moodleform $mform) {
1102 ob_start();
1103 $mform->display();
1104 $o = ob_get_contents();
1105 ob_end_clean();
1107 return $o;
1111 * Helper function returning the n-th item of the array
1113 * @param array $a
1114 * @param int $n from 0 to m, where m is th number of items in the array
1115 * @return mixed the $n-th element of $a
1117 protected static function array_nth(array $a, $n) {
1118 $keys = array_keys($a);
1119 if ($n < 0 or $n > count($keys) - 1) {
1120 return null;
1122 $key = $keys[$n];
1123 return $a[$key];
1127 * Tries to guess the fullname format set at the site
1129 * @return string fl|lf
1131 protected static function fullname_format() {
1132 $fake = new stdclass(); // fake user
1133 $fake->lastname = 'LLLL';
1134 $fake->firstname = 'FFFF';
1135 $fullname = get_string('fullnamedisplay', '', $fake);
1136 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
1137 return 'lf';
1138 } else {
1139 return 'fl';