Moodle release 2.7.8
[moodle.git] / mod / workshop / renderer.php
blobe5cc7aca674b2578289da385a853f49ebd587043
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 $actions = '';
293 foreach ($phase->actions as $action) {
294 switch ($action->type) {
295 case 'switchphase':
296 $icon = 'i/marker';
297 if ($phasecode == workshop::PHASE_ASSESSMENT
298 and $plan->workshop->phase == workshop::PHASE_SUBMISSION
299 and $plan->workshop->phaseswitchassessment) {
300 $icon = 'i/scheduled';
302 $actions .= $this->output->action_icon($action->url, new pix_icon($icon, get_string('switchphase', 'workshop')));
303 break;
306 if (!empty($actions)) {
307 $actions = $this->output->container($actions, 'actions');
309 $table->head[] = $this->output->container($title . $actions);
310 $classes = 'phase' . $phasecode;
311 if ($phase->active) {
312 $classes .= ' active';
313 } else {
314 $classes .= ' nonactive';
316 $table->colclasses[] = $classes;
317 $cell = new html_table_cell();
318 $cell->text = $this->helper_user_plan_tasks($phase->tasks);
319 $row->cells[] = $cell;
321 $table->data = array($row);
323 return html_writer::table($table);
327 * Renders the result of the submissions allocation process
329 * @param workshop_allocation_result $result as returned by the allocator's init() method
330 * @return string HTML to be echoed
332 protected function render_workshop_allocation_result(workshop_allocation_result $result) {
333 global $CFG;
335 $status = $result->get_status();
337 if (is_null($status) or $status == workshop_allocation_result::STATUS_VOID) {
338 debugging('Attempt to render workshop_allocation_result with empty status', DEBUG_DEVELOPER);
339 return '';
342 switch ($status) {
343 case workshop_allocation_result::STATUS_FAILED:
344 if ($message = $result->get_message()) {
345 $message = new workshop_message($message, workshop_message::TYPE_ERROR);
346 } else {
347 $message = new workshop_message(get_string('allocationerror', 'workshop'), workshop_message::TYPE_ERROR);
349 break;
351 case workshop_allocation_result::STATUS_CONFIGURED:
352 if ($message = $result->get_message()) {
353 $message = new workshop_message($message, workshop_message::TYPE_INFO);
354 } else {
355 $message = new workshop_message(get_string('allocationconfigured', 'workshop'), workshop_message::TYPE_INFO);
357 break;
359 case workshop_allocation_result::STATUS_EXECUTED:
360 if ($message = $result->get_message()) {
361 $message = new workshop_message($message, workshop_message::TYPE_OK);
362 } else {
363 $message = new workshop_message(get_string('allocationdone', 'workshop'), workshop_message::TYPE_OK);
365 break;
367 default:
368 throw new coding_exception('Unknown allocation result status', $status);
371 // start with the message
372 $o = $this->render($message);
374 // display the details about the process if available
375 $logs = $result->get_logs();
376 if (is_array($logs) and !empty($logs)) {
377 $o .= html_writer::start_tag('ul', array('class' => 'allocation-init-results'));
378 foreach ($logs as $log) {
379 if ($log->type == 'debug' and !$CFG->debugdeveloper) {
380 // display allocation debugging messages for developers only
381 continue;
383 $class = $log->type;
384 if ($log->indent) {
385 $class .= ' indent';
387 $o .= html_writer::tag('li', $log->message, array('class' => $class)).PHP_EOL;
389 $o .= html_writer::end_tag('ul');
392 return $o;
396 * Renders the workshop grading report
398 * @param workshop_grading_report $gradingreport
399 * @return string html code
401 protected function render_workshop_grading_report(workshop_grading_report $gradingreport) {
403 $data = $gradingreport->get_data();
404 $options = $gradingreport->get_options();
405 $grades = $data->grades;
406 $userinfo = $data->userinfo;
408 if (empty($grades)) {
409 return '';
412 $table = new html_table();
413 $table->attributes['class'] = 'grading-report';
415 $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'), 'firstname', $options->sortby, $options->sorthow);
416 $sortbylastname = $this->helper_sortable_heading(get_string('lastname'), 'lastname', $options->sortby, $options->sorthow);
417 if (self::fullname_format() == 'lf') {
418 $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
419 } else {
420 $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
423 $table->head = array();
424 $table->head[] = $sortbyname;
425 $table->head[] = $this->helper_sortable_heading(get_string('submission', 'workshop'), 'submissiontitle',
426 $options->sortby, $options->sorthow);
427 $table->head[] = $this->helper_sortable_heading(get_string('receivedgrades', 'workshop'));
428 if ($options->showsubmissiongrade) {
429 $table->head[] = $this->helper_sortable_heading(get_string('submissiongradeof', 'workshop', $data->maxgrade),
430 'submissiongrade', $options->sortby, $options->sorthow);
432 $table->head[] = $this->helper_sortable_heading(get_string('givengrades', 'workshop'));
433 if ($options->showgradinggrade) {
434 $table->head[] = $this->helper_sortable_heading(get_string('gradinggradeof', 'workshop', $data->maxgradinggrade),
435 'gradinggrade', $options->sortby, $options->sorthow);
438 $table->rowclasses = array();
439 $table->colclasses = array();
440 $table->data = array();
442 foreach ($grades as $participant) {
443 $numofreceived = count($participant->reviewedby);
444 $numofgiven = count($participant->reviewerof);
445 $published = $participant->submissionpublished;
447 // compute the number of <tr> table rows needed to display this participant
448 if ($numofreceived > 0 and $numofgiven > 0) {
449 $numoftrs = workshop::lcm($numofreceived, $numofgiven);
450 $spanreceived = $numoftrs / $numofreceived;
451 $spangiven = $numoftrs / $numofgiven;
452 } elseif ($numofreceived == 0 and $numofgiven > 0) {
453 $numoftrs = $numofgiven;
454 $spanreceived = $numoftrs;
455 $spangiven = $numoftrs / $numofgiven;
456 } elseif ($numofreceived > 0 and $numofgiven == 0) {
457 $numoftrs = $numofreceived;
458 $spanreceived = $numoftrs / $numofreceived;
459 $spangiven = $numoftrs;
460 } else {
461 $numoftrs = 1;
462 $spanreceived = 1;
463 $spangiven = 1;
466 for ($tr = 0; $tr < $numoftrs; $tr++) {
467 $row = new html_table_row();
468 if ($published) {
469 $row->attributes['class'] = 'published';
471 // column #1 - participant - spans over all rows
472 if ($tr == 0) {
473 $cell = new html_table_cell();
474 $cell->text = $this->helper_grading_report_participant($participant, $userinfo);
475 $cell->rowspan = $numoftrs;
476 $cell->attributes['class'] = 'participant';
477 $row->cells[] = $cell;
479 // column #2 - submission - spans over all rows
480 if ($tr == 0) {
481 $cell = new html_table_cell();
482 $cell->text = $this->helper_grading_report_submission($participant);
483 $cell->rowspan = $numoftrs;
484 $cell->attributes['class'] = 'submission';
485 $row->cells[] = $cell;
487 // column #3 - received grades
488 if ($tr % $spanreceived == 0) {
489 $idx = intval($tr / $spanreceived);
490 $assessment = self::array_nth($participant->reviewedby, $idx);
491 $cell = new html_table_cell();
492 $cell->text = $this->helper_grading_report_assessment($assessment, $options->showreviewernames, $userinfo,
493 get_string('gradereceivedfrom', 'workshop'));
494 $cell->rowspan = $spanreceived;
495 $cell->attributes['class'] = 'receivedgrade';
496 if (is_null($assessment) or is_null($assessment->grade)) {
497 $cell->attributes['class'] .= ' null';
498 } else {
499 $cell->attributes['class'] .= ' notnull';
501 $row->cells[] = $cell;
503 // column #4 - total grade for submission
504 if ($options->showsubmissiongrade and $tr == 0) {
505 $cell = new html_table_cell();
506 $cell->text = $this->helper_grading_report_grade($participant->submissiongrade, $participant->submissiongradeover);
507 $cell->rowspan = $numoftrs;
508 $cell->attributes['class'] = 'submissiongrade';
509 $row->cells[] = $cell;
511 // column #5 - given grades
512 if ($tr % $spangiven == 0) {
513 $idx = intval($tr / $spangiven);
514 $assessment = self::array_nth($participant->reviewerof, $idx);
515 $cell = new html_table_cell();
516 $cell->text = $this->helper_grading_report_assessment($assessment, $options->showauthornames, $userinfo,
517 get_string('gradegivento', 'workshop'));
518 $cell->rowspan = $spangiven;
519 $cell->attributes['class'] = 'givengrade';
520 if (is_null($assessment) or is_null($assessment->grade)) {
521 $cell->attributes['class'] .= ' null';
522 } else {
523 $cell->attributes['class'] .= ' notnull';
525 $row->cells[] = $cell;
527 // column #6 - total grade for assessment
528 if ($options->showgradinggrade and $tr == 0) {
529 $cell = new html_table_cell();
530 $cell->text = $this->helper_grading_report_grade($participant->gradinggrade);
531 $cell->rowspan = $numoftrs;
532 $cell->attributes['class'] = 'gradinggrade';
533 $row->cells[] = $cell;
536 $table->data[] = $row;
540 return html_writer::table($table);
544 * Renders the feedback for the author of the submission
546 * @param workshop_feedback_author $feedback
547 * @return string HTML
549 protected function render_workshop_feedback_author(workshop_feedback_author $feedback) {
550 return $this->helper_render_feedback($feedback);
554 * Renders the feedback for the reviewer of the submission
556 * @param workshop_feedback_reviewer $feedback
557 * @return string HTML
559 protected function render_workshop_feedback_reviewer(workshop_feedback_reviewer $feedback) {
560 return $this->helper_render_feedback($feedback);
564 * Helper method to rendering feedback
566 * @param workshop_feedback_author|workshop_feedback_reviewer $feedback
567 * @return string HTML
569 private function helper_render_feedback($feedback) {
571 $o = ''; // output HTML code
572 $o .= $this->output->container_start('feedback feedbackforauthor');
573 $o .= $this->output->container_start('header');
574 $o .= $this->output->heading(get_string('feedbackby', 'workshop', s(fullname($feedback->get_provider()))), 3, 'title');
576 $userpic = $this->output->user_picture($feedback->get_provider(), array('courseid' => $this->page->course->id, 'size' => 32));
577 $o .= $this->output->container($userpic, 'picture');
578 $o .= $this->output->container_end(); // end of header
580 $content = format_text($feedback->get_content(), $feedback->get_format(), array('overflowdiv' => true));
581 $o .= $this->output->container($content, 'content');
583 $o .= $this->output->container_end();
585 return $o;
589 * Renders the full assessment
591 * @param workshop_assessment $assessment
592 * @return string HTML
594 protected function render_workshop_assessment(workshop_assessment $assessment) {
596 $o = ''; // output HTML code
597 $anonymous = is_null($assessment->reviewer);
598 $classes = 'assessment-full';
599 if ($anonymous) {
600 $classes .= ' anonymous';
603 $o .= $this->output->container_start($classes);
604 $o .= $this->output->container_start('header');
606 if (!empty($assessment->title)) {
607 $title = s($assessment->title);
608 } else {
609 $title = get_string('assessment', 'workshop');
611 if (($assessment->url instanceof moodle_url) and ($this->page->url != $assessment->url)) {
612 $o .= $this->output->container(html_writer::link($assessment->url, $title), 'title');
613 } else {
614 $o .= $this->output->container($title, 'title');
617 if (!$anonymous) {
618 $reviewer = $assessment->reviewer;
619 $userpic = $this->output->user_picture($reviewer, array('courseid' => $this->page->course->id, 'size' => 32));
621 $userurl = new moodle_url('/user/view.php',
622 array('id' => $reviewer->id, 'course' => $this->page->course->id));
623 $a = new stdClass();
624 $a->name = fullname($reviewer);
625 $a->url = $userurl->out();
626 $byfullname = get_string('assessmentby', 'workshop', $a);
627 $oo = $this->output->container($userpic, 'picture');
628 $oo .= $this->output->container($byfullname, 'fullname');
630 $o .= $this->output->container($oo, 'reviewer');
633 if (is_null($assessment->realgrade)) {
634 $o .= $this->output->container(
635 get_string('notassessed', 'workshop'),
636 'grade nograde'
638 } else {
639 $a = new stdClass();
640 $a->max = $assessment->maxgrade;
641 $a->received = $assessment->realgrade;
642 $o .= $this->output->container(
643 get_string('gradeinfo', 'workshop', $a),
644 'grade'
647 if (!is_null($assessment->weight) and $assessment->weight != 1) {
648 $o .= $this->output->container(
649 get_string('weightinfo', 'workshop', $assessment->weight),
650 'weight'
655 $o .= $this->output->container_start('actions');
656 foreach ($assessment->actions as $action) {
657 $o .= $this->output->single_button($action->url, $action->label, $action->method);
659 $o .= $this->output->container_end(); // actions
661 $o .= $this->output->container_end(); // header
663 if (!is_null($assessment->form)) {
664 $o .= print_collapsible_region_start('assessment-form-wrapper', uniqid('workshop-assessment'),
665 get_string('assessmentform', 'workshop'), '', false, true);
666 $o .= $this->output->container(self::moodleform($assessment->form), 'assessment-form');
667 $o .= print_collapsible_region_end(true);
669 if (!$assessment->form->is_editable()) {
670 $o .= $this->overall_feedback($assessment);
674 $o .= $this->output->container_end(); // main wrapper
676 return $o;
680 * Renders the assessment of an example submission
682 * @param workshop_example_assessment $assessment
683 * @return string HTML
685 protected function render_workshop_example_assessment(workshop_example_assessment $assessment) {
686 return $this->render_workshop_assessment($assessment);
690 * Renders the reference assessment of an example submission
692 * @param workshop_example_reference_assessment $assessment
693 * @return string HTML
695 protected function render_workshop_example_reference_assessment(workshop_example_reference_assessment $assessment) {
696 return $this->render_workshop_assessment($assessment);
700 * Renders the overall feedback for the author of the submission
702 * @param workshop_assessment $assessment
703 * @return string HTML
705 protected function overall_feedback(workshop_assessment $assessment) {
707 $content = $assessment->get_overall_feedback_content();
709 if ($content === false) {
710 return '';
713 $o = '';
715 if (!is_null($content)) {
716 $o .= $this->output->container($content, 'content');
719 $attachments = $assessment->get_overall_feedback_attachments();
721 if (!empty($attachments)) {
722 $o .= $this->output->container_start('attachments');
723 $images = '';
724 $files = '';
725 foreach ($attachments as $attachment) {
726 $icon = $this->output->pix_icon(file_file_icon($attachment), get_mimetype_description($attachment),
727 'moodle', array('class' => 'icon'));
728 $link = html_writer::link($attachment->fileurl, $icon.' '.substr($attachment->filepath.$attachment->filename, 1));
729 if (file_mimetype_in_typegroup($attachment->mimetype, 'web_image')) {
730 $preview = html_writer::empty_tag('img', array('src' => $attachment->previewurl, 'alt' => '', 'class' => 'preview'));
731 $preview = html_writer::tag('a', $preview, array('href' => $attachment->fileurl));
732 $images .= $this->output->container($preview);
733 } else {
734 $files .= html_writer::tag('li', $link, array('class' => $attachment->mimetype));
737 if ($images) {
738 $images = $this->output->container($images, 'images');
741 if ($files) {
742 $files = html_writer::tag('ul', $files, array('class' => 'files'));
745 $o .= $images.$files;
746 $o .= $this->output->container_end();
749 if ($o === '') {
750 return '';
753 $o = $this->output->box($o, 'overallfeedback');
754 $o = print_collapsible_region($o, 'overall-feedback-wrapper', uniqid('workshop-overall-feedback'),
755 get_string('overallfeedback', 'workshop'), '', false, true);
757 return $o;
761 * Renders a perpage selector for workshop listings
763 * The scripts using this have to define the $PAGE->url prior to calling this
764 * and deal with eventually submitted value themselves.
766 * @param int $current current value of the perpage parameter
767 * @return string HTML
769 public function perpage_selector($current=10) {
771 $options = array();
772 foreach (array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 1000) as $option) {
773 if ($option != $current) {
774 $options[$option] = $option;
777 $select = new single_select($this->page->url, 'perpage', $options, '', array('' => get_string('showingperpagechange', 'mod_workshop')));
778 $select->label = get_string('showingperpage', 'mod_workshop', $current);
779 $select->method = 'post';
781 return $this->output->container($this->output->render($select), 'perpagewidget');
785 * Renders the user's final grades
787 * @param workshop_final_grades $grades with the info about grades in the gradebook
788 * @return string HTML
790 protected function render_workshop_final_grades(workshop_final_grades $grades) {
792 $out = html_writer::start_tag('div', array('class' => 'finalgrades'));
794 if (!empty($grades->submissiongrade)) {
795 $cssclass = 'grade submissiongrade';
796 if ($grades->submissiongrade->hidden) {
797 $cssclass .= ' hiddengrade';
799 $out .= html_writer::tag(
800 'div',
801 html_writer::tag('div', get_string('submissiongrade', 'mod_workshop'), array('class' => 'gradetype')) .
802 html_writer::tag('div', $grades->submissiongrade->str_long_grade, array('class' => 'gradevalue')),
803 array('class' => $cssclass)
807 if (!empty($grades->assessmentgrade)) {
808 $cssclass = 'grade assessmentgrade';
809 if ($grades->assessmentgrade->hidden) {
810 $cssclass .= ' hiddengrade';
812 $out .= html_writer::tag(
813 'div',
814 html_writer::tag('div', get_string('gradinggrade', 'mod_workshop'), array('class' => 'gradetype')) .
815 html_writer::tag('div', $grades->assessmentgrade->str_long_grade, array('class' => 'gradevalue')),
816 array('class' => $cssclass)
820 $out .= html_writer::end_tag('div');
822 return $out;
825 ////////////////////////////////////////////////////////////////////////////
826 // Internal rendering helper methods
827 ////////////////////////////////////////////////////////////////////////////
830 * Renders a list of files attached to the submission
832 * If format==html, then format a html string. If format==text, then format a text-only string.
833 * Otherwise, returns html for non-images and html to display the image inline.
835 * @param int $submissionid submission identifier
836 * @param string format the format of the returned string - html|text
837 * @return string formatted text to be echoed
839 protected function helper_submission_attachments($submissionid, $format = 'html') {
840 global $CFG;
841 require_once($CFG->libdir.'/filelib.php');
843 $fs = get_file_storage();
844 $ctx = $this->page->context;
845 $files = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid);
847 $outputimgs = ''; // images to be displayed inline
848 $outputfiles = ''; // list of attachment files
850 foreach ($files as $file) {
851 if ($file->is_directory()) {
852 continue;
855 $filepath = $file->get_filepath();
856 $filename = $file->get_filename();
857 $fileurl = moodle_url::make_pluginfile_url($ctx->id, 'mod_workshop', 'submission_attachment',
858 $submissionid, $filepath, $filename, true);
859 $embedurl = moodle_url::make_pluginfile_url($ctx->id, 'mod_workshop', 'submission_attachment',
860 $submissionid, $filepath, $filename, false);
861 $embedurl = new moodle_url($embedurl, array('preview' => 'bigthumb'));
862 $type = $file->get_mimetype();
863 $image = $this->output->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon'));
865 $linkhtml = html_writer::link($fileurl, $image) . substr($filepath, 1) . html_writer::link($fileurl, $filename);
866 $linktxt = "$filename [$fileurl]";
868 if ($format == 'html') {
869 if (file_mimetype_in_typegroup($type, 'web_image')) {
870 $preview = html_writer::empty_tag('img', array('src' => $embedurl, 'alt' => '', 'class' => 'preview'));
871 $preview = html_writer::tag('a', $preview, array('href' => $fileurl));
872 $outputimgs .= $this->output->container($preview);
874 } else {
875 $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type));
878 } else if ($format == 'text') {
879 $outputfiles .= $linktxt . PHP_EOL;
882 if (!empty($CFG->enableplagiarism)) {
883 require_once($CFG->libdir.'/plagiarismlib.php');
884 $outputfiles .= plagiarism_get_links(array('userid' => $file->get_userid(),
885 'file' => $file,
886 'cmid' => $this->page->cm->id,
887 'course' => $this->page->course->id));
891 if ($format == 'html') {
892 if ($outputimgs) {
893 $outputimgs = $this->output->container($outputimgs, 'images');
896 if ($outputfiles) {
897 $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files'));
900 return $this->output->container($outputimgs . $outputfiles, 'attachments');
902 } else {
903 return $outputfiles;
908 * Renders the tasks for the single phase in the user plan
910 * @param stdClass $tasks
911 * @return string html code
913 protected function helper_user_plan_tasks(array $tasks) {
914 $out = '';
915 foreach ($tasks as $taskcode => $task) {
916 $classes = '';
917 $icon = null;
918 if ($task->completed === true) {
919 $classes .= ' completed';
920 } elseif ($task->completed === false) {
921 $classes .= ' fail';
922 } elseif ($task->completed === 'info') {
923 $classes .= ' info';
925 if (is_null($task->link)) {
926 $title = $task->title;
927 } else {
928 $title = html_writer::link($task->link, $task->title);
930 $title = $this->output->container($title, 'title');
931 $details = $this->output->container($task->details, 'details');
932 $out .= html_writer::tag('li', $title . $details, array('class' => $classes));
934 if ($out) {
935 $out = html_writer::tag('ul', $out, array('class' => 'tasks'));
937 return $out;
941 * Renders a text with icons to sort by the given column
943 * This is intended for table headings.
945 * @param string $text The heading text
946 * @param string $sortid The column id used for sorting
947 * @param string $sortby Currently sorted by (column id)
948 * @param string $sorthow Currently sorted how (ASC|DESC)
950 * @return string
952 protected function helper_sortable_heading($text, $sortid=null, $sortby=null, $sorthow=null) {
953 global $PAGE;
955 $out = html_writer::tag('span', $text, array('class'=>'text'));
957 if (!is_null($sortid)) {
958 if ($sortby !== $sortid or $sorthow !== 'ASC') {
959 $url = new moodle_url($PAGE->url);
960 $url->params(array('sortby' => $sortid, 'sorthow' => 'ASC'));
961 $out .= $this->output->action_icon($url, new pix_icon('t/sort_asc', get_string('sortasc', 'workshop')),
962 null, array('class' => 'iconsort sort asc'));
964 if ($sortby !== $sortid or $sorthow !== 'DESC') {
965 $url = new moodle_url($PAGE->url);
966 $url->params(array('sortby' => $sortid, 'sorthow' => 'DESC'));
967 $out .= $this->output->action_icon($url, new pix_icon('t/sort_desc', get_string('sortdesc', 'workshop')),
968 null, array('class' => 'iconsort sort desc'));
971 return $out;
975 * @param stdClass $participant
976 * @param array $userinfo
977 * @return string
979 protected function helper_grading_report_participant(stdclass $participant, array $userinfo) {
980 $userid = $participant->userid;
981 $out = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 35));
982 $out .= html_writer::tag('span', fullname($userinfo[$userid]));
984 return $out;
988 * @param stdClass $participant
989 * @return string
991 protected function helper_grading_report_submission(stdclass $participant) {
992 global $CFG;
994 if (is_null($participant->submissionid)) {
995 $out = $this->output->container(get_string('nosubmissionfound', 'workshop'), 'info');
996 } else {
997 $url = new moodle_url('/mod/workshop/submission.php',
998 array('cmid' => $this->page->context->instanceid, 'id' => $participant->submissionid));
999 $out = html_writer::link($url, format_string($participant->submissiontitle), array('class'=>'title'));
1002 return $out;
1006 * @todo Highlight the nulls
1007 * @param stdClass|null $assessment
1008 * @param bool $shownames
1009 * @param string $separator between the grade and the reviewer/author
1010 * @return string
1012 protected function helper_grading_report_assessment($assessment, $shownames, array $userinfo, $separator) {
1013 global $CFG;
1015 if (is_null($assessment)) {
1016 return get_string('nullgrade', 'workshop');
1018 $a = new stdclass();
1019 $a->grade = is_null($assessment->grade) ? get_string('nullgrade', 'workshop') : $assessment->grade;
1020 $a->gradinggrade = is_null($assessment->gradinggrade) ? get_string('nullgrade', 'workshop') : $assessment->gradinggrade;
1021 $a->weight = $assessment->weight;
1022 // grrr the following logic should really be handled by a future language pack feature
1023 if (is_null($assessment->gradinggradeover)) {
1024 if ($a->weight == 1) {
1025 $grade = get_string('formatpeergrade', 'workshop', $a);
1026 } else {
1027 $grade = get_string('formatpeergradeweighted', 'workshop', $a);
1029 } else {
1030 $a->gradinggradeover = $assessment->gradinggradeover;
1031 if ($a->weight == 1) {
1032 $grade = get_string('formatpeergradeover', 'workshop', $a);
1033 } else {
1034 $grade = get_string('formatpeergradeoverweighted', 'workshop', $a);
1037 $url = new moodle_url('/mod/workshop/assessment.php',
1038 array('asid' => $assessment->assessmentid));
1039 $grade = html_writer::link($url, $grade, array('class'=>'grade'));
1041 if ($shownames) {
1042 $userid = $assessment->userid;
1043 $name = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 16));
1044 $name .= html_writer::tag('span', fullname($userinfo[$userid]), array('class' => 'fullname'));
1045 $name = $separator . html_writer::tag('span', $name, array('class' => 'user'));
1046 } else {
1047 $name = '';
1050 return $this->output->container($grade . $name, 'assessmentdetails');
1054 * Formats the aggreagated grades
1056 protected function helper_grading_report_grade($grade, $over=null) {
1057 $a = new stdclass();
1058 $a->grade = is_null($grade) ? get_string('nullgrade', 'workshop') : $grade;
1059 if (is_null($over)) {
1060 $text = get_string('formataggregatedgrade', 'workshop', $a);
1061 } else {
1062 $a->over = is_null($over) ? get_string('nullgrade', 'workshop') : $over;
1063 $text = get_string('formataggregatedgradeover', 'workshop', $a);
1065 return $text;
1068 ////////////////////////////////////////////////////////////////////////////
1069 // Static helpers
1070 ////////////////////////////////////////////////////////////////////////////
1073 * Helper method dealing with the fact we can not just fetch the output of moodleforms
1075 * @param moodleform $mform
1076 * @return string HTML
1078 protected static function moodleform(moodleform $mform) {
1080 ob_start();
1081 $mform->display();
1082 $o = ob_get_contents();
1083 ob_end_clean();
1085 return $o;
1089 * Helper function returning the n-th item of the array
1091 * @param array $a
1092 * @param int $n from 0 to m, where m is th number of items in the array
1093 * @return mixed the $n-th element of $a
1095 protected static function array_nth(array $a, $n) {
1096 $keys = array_keys($a);
1097 if ($n < 0 or $n > count($keys) - 1) {
1098 return null;
1100 $key = $keys[$n];
1101 return $a[$key];
1105 * Tries to guess the fullname format set at the site
1107 * @return string fl|lf
1109 protected static function fullname_format() {
1110 $fake = new stdclass(); // fake user
1111 $fake->lastname = 'LLLL';
1112 $fake->firstname = 'FFFF';
1113 $fullname = get_string('fullnamedisplay', '', $fake);
1114 if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
1115 return 'lf';
1116 } else {
1117 return 'fl';