MDL-27550 workshop: fixed XHTML strictness of H3 and A combo
[moodle.git] / mod / workshop / renderer.php
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.
14 //
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
20  *
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
25  */
27 defined('MOODLE_INTERNAL') || die();
29 /**
30  * Workshop module renderer class
31  *
32  * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
33  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34  */
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
43      *
44      * @param workshop_message $message to display
45      * @return string html code
46      */
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 '';
55         }
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';
66         }
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');
72         }
74         return $this->output->container($o, array('message', $sty));
75     }
78     /**
79      * Renders full workshop submission
80      *
81      * @param workshop_submission $submission
82      * @return string HTML
83      */
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';
91         }
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);
99         }
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');
122         }
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');
130         }
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;
144     }
146     /**
147      * Renders short summary of the submission
148      *
149      * @param workshop_submission_summary $summary
150      * @return string text to be echo'ed
151      */
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';
160         }
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');
171         }
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');
195         }
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');
203         }
205         $o .= $gradestatus;
206         $o .= $this->output->container_end(); // end of the main wrapper
207         return $o;
208     }
210     /**
211      * Renders full workshop example submission
212      *
213      * @param workshop_example_submission $example
214      * @return string HTML
215      */
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;
235     }
237     /**
238      * Renders short summary of the example submission
239      *
240      * @param workshop_example_submission_summary $summary
241      * @return string text to be echo'ed
242      */
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')));
256         }
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');
264         }
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;
274     }
276     /**
277      * Renders the user plannner tool
278      *
279      * @param workshop_user_plan $plan prepared for the user
280      * @return string html code to be displayed
281      */
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                     $actions .= $this->output->action_icon($action->url, new pix_icon('i/marker', get_string('switchphase', 'workshop')));
296                     break;
297                 }
298             }
299             if (!empty($actions)) {
300                 $actions = $this->output->container($actions, 'actions');
301             }
302             $table->head[] = $this->output->container($title . $actions);
303             $classes = 'phase' . $phasecode;
304             if ($phase->active) {
305                 $classes .= ' active';
306             } else {
307                 $classes .= ' nonactive';
308             }
309             $table->colclasses[] = $classes;
310             $cell = new html_table_cell();
311             $cell->text = $this->helper_user_plan_tasks($phase->tasks);
312             $row->cells[] = $cell;
313         }
314         $table->data = array($row);
316         return html_writer::table($table);
317     }
319     /**
320      * Renders the result of the submissions allocation process
321      *
322      * @param workshop_allocation_init_result
323      * @return string html to be echoed
324      */
325     protected function render_workshop_allocation_init_result(workshop_allocation_init_result $result) {
327         // start with the message
328         $o = $this->render($result->get_message());
330         // display the details about the process if available
331         $info = $result->get_info();
332         if (is_array($info) and !empty($info)) {
333             $o .= html_writer::start_tag('ul', array('class' => 'allocation-init-results'));
334             foreach ($info as $message) {
335                 $parts  = explode('::', $message);
336                 $text   = array_pop($parts);
337                 $class  = implode(' ', $parts);
338                 if (in_array('debug', $parts) && !debugging('', DEBUG_DEVELOPER)) {
339                     // do not display allocation debugging messages
340                     continue;
341                 }
342                 $o .= html_writer::tag('li', $text, array('class' => $class)) . "\n";
343             }
344             $o .= html_writer::end_tag('ul');
345         }
347         $o .= $this->output->continue_button($result->get_continue_url());
349         return $o;
350     }
352     /**
353      * Renders the workshop grading report
354      *
355      * @param workshop_grading_report $gradingreport
356      * @return string html code
357      */
358     protected function render_workshop_grading_report(workshop_grading_report $gradingreport) {
360         $data       = $gradingreport->get_data();
361         $options    = $gradingreport->get_options();
362         $grades     = $data->grades;
363         $userinfo   = $data->userinfo;
365         if (empty($grades)) {
366             return '';
367         }
369         $table = new html_table();
370         $table->attributes['class'] = 'grading-report';
372         $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'), 'firstname', $options->sortby, $options->sorthow);
373         $sortbylastname = $this->helper_sortable_heading(get_string('lastname'), 'lastname', $options->sortby, $options->sorthow);
374         if (self::fullname_format() == 'lf') {
375             $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
376         } else {
377             $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
378         }
380         $table->head = array();
381         $table->head[] = $sortbyname;
382         $table->head[] = $this->helper_sortable_heading(get_string('submission', 'workshop'), 'submissiontitle',
383                 $options->sortby, $options->sorthow);
384         $table->head[] = $this->helper_sortable_heading(get_string('receivedgrades', 'workshop'));
385         if ($options->showsubmissiongrade) {
386             $table->head[] = $this->helper_sortable_heading(get_string('submissiongradeof', 'workshop', $data->maxgrade),
387                     'submissiongrade', $options->sortby, $options->sorthow);
388         }
389         $table->head[] = $this->helper_sortable_heading(get_string('givengrades', 'workshop'));
390         if ($options->showgradinggrade) {
391             $table->head[] = $this->helper_sortable_heading(get_string('gradinggradeof', 'workshop', $data->maxgradinggrade),
392                     'gradinggrade', $options->sortby, $options->sorthow);
393         }
395         $table->rowclasses  = array();
396         $table->colclasses  = array();
397         $table->data        = array();
399         foreach ($grades as $participant) {
400             $numofreceived  = count($participant->reviewedby);
401             $numofgiven     = count($participant->reviewerof);
402             $published      = $participant->submissionpublished;
404             // compute the number of <tr> table rows needed to display this participant
405             if ($numofreceived > 0 and $numofgiven > 0) {
406                 $numoftrs       = workshop::lcm($numofreceived, $numofgiven);
407                 $spanreceived   = $numoftrs / $numofreceived;
408                 $spangiven      = $numoftrs / $numofgiven;
409             } elseif ($numofreceived == 0 and $numofgiven > 0) {
410                 $numoftrs       = $numofgiven;
411                 $spanreceived   = $numoftrs;
412                 $spangiven      = $numoftrs / $numofgiven;
413             } elseif ($numofreceived > 0 and $numofgiven == 0) {
414                 $numoftrs       = $numofreceived;
415                 $spanreceived   = $numoftrs / $numofreceived;
416                 $spangiven      = $numoftrs;
417             } else {
418                 $numoftrs       = 1;
419                 $spanreceived   = 1;
420                 $spangiven      = 1;
421             }
423             for ($tr = 0; $tr < $numoftrs; $tr++) {
424                 $row = new html_table_row();
425                 if ($published) {
426                     $row->attributes['class'] = 'published';
427                 }
428                 // column #1 - participant - spans over all rows
429                 if ($tr == 0) {
430                     $cell = new html_table_cell();
431                     $cell->text = $this->helper_grading_report_participant($participant, $userinfo);
432                     $cell->rowspan = $numoftrs;
433                     $cell->attributes['class'] = 'participant';
434                     $row->cells[] = $cell;
435                 }
436                 // column #2 - submission - spans over all rows
437                 if ($tr == 0) {
438                     $cell = new html_table_cell();
439                     $cell->text = $this->helper_grading_report_submission($participant);
440                     $cell->rowspan = $numoftrs;
441                     $cell->attributes['class'] = 'submission';
442                     $row->cells[] = $cell;
443                 }
444                 // column #3 - received grades
445                 if ($tr % $spanreceived == 0) {
446                     $idx = intval($tr / $spanreceived);
447                     $assessment = self::array_nth($participant->reviewedby, $idx);
448                     $cell = new html_table_cell();
449                     $cell->text = $this->helper_grading_report_assessment($assessment, $options->showreviewernames, $userinfo,
450                             get_string('gradereceivedfrom', 'workshop'));
451                     $cell->rowspan = $spanreceived;
452                     $cell->attributes['class'] = 'receivedgrade';
453                     if (is_null($assessment) or is_null($assessment->grade)) {
454                         $cell->attributes['class'] .= ' null';
455                     } else {
456                         $cell->attributes['class'] .= ' notnull';
457                     }
458                     $row->cells[] = $cell;
459                 }
460                 // column #4 - total grade for submission
461                 if ($options->showsubmissiongrade and $tr == 0) {
462                     $cell = new html_table_cell();
463                     $cell->text = $this->helper_grading_report_grade($participant->submissiongrade, $participant->submissiongradeover);
464                     $cell->rowspan = $numoftrs;
465                     $cell->attributes['class'] = 'submissiongrade';
466                     $row->cells[] = $cell;
467                 }
468                 // column #5 - given grades
469                 if ($tr % $spangiven == 0) {
470                     $idx = intval($tr / $spangiven);
471                     $assessment = self::array_nth($participant->reviewerof, $idx);
472                     $cell = new html_table_cell();
473                     $cell->text = $this->helper_grading_report_assessment($assessment, $options->showauthornames, $userinfo,
474                             get_string('gradegivento', 'workshop'));
475                     $cell->rowspan = $spangiven;
476                     $cell->attributes['class'] = 'givengrade';
477                     if (is_null($assessment) or is_null($assessment->grade)) {
478                         $cell->attributes['class'] .= ' null';
479                     } else {
480                         $cell->attributes['class'] .= ' notnull';
481                     }
482                     $row->cells[] = $cell;
483                 }
484                 // column #6 - total grade for assessment
485                 if ($options->showgradinggrade and $tr == 0) {
486                     $cell = new html_table_cell();
487                     $cell->text = $this->helper_grading_report_grade($participant->gradinggrade);
488                     $cell->rowspan = $numoftrs;
489                     $cell->attributes['class'] = 'gradinggrade';
490                     $row->cells[] = $cell;
491                 }
493                 $table->data[] = $row;
494             }
495         }
497         return html_writer::table($table);
498     }
500     /**
501      * Renders the feedback for the author of the submission
502      *
503      * @param workshop_feedback_author $feedback
504      * @return string HTML
505      */
506     protected function render_workshop_feedback_author(workshop_feedback_author $feedback) {
507         return $this->helper_render_feedback($feedback);
508     }
510     /**
511      * Renders the feedback for the reviewer of the submission
512      *
513      * @param workshop_feedback_reviewer $feedback
514      * @return string HTML
515      */
516     protected function render_workshop_feedback_reviewer(workshop_feedback_reviewer $feedback) {
517         return $this->helper_render_feedback($feedback);
518     }
520     /**
521      * Helper method to rendering feedback
522      *
523      * @param workshop_feedback_author|workshop_feedback_reviewer $feedback
524      * @return string HTML
525      */
526     private function helper_render_feedback($feedback) {
528         $o  = '';    // output HTML code
529         $o .= $this->output->container_start('feedback feedbackforauthor');
530         $o .= $this->output->container_start('header');
531         $o .= $this->output->heading(get_string('feedbackby', 'workshop', s(fullname($feedback->get_provider()))), 3, 'title');
533         $userpic = $this->output->user_picture($feedback->get_provider(), array('courseid' => $this->page->course->id, 'size' => 32));
534         $o .= $this->output->container($userpic, 'picture');
535         $o .= $this->output->container_end(); // end of header
537         $content = format_text($feedback->get_content(), $feedback->get_format(), array('overflowdiv' => true));
538         $o .= $this->output->container($content, 'content');
540         $o .= $this->output->container_end();
542         return $o;
543     }
545     /**
546      * Renders the full assessment
547      *
548      * @param workshop_assessment $assessment
549      * @return string HTML
550      */
551     protected function render_workshop_assessment(workshop_assessment $assessment) {
553         $o = ''; // output HTML code
554         $anonymous = is_null($assessment->reviewer);
555         $classes = 'assessment-full';
556         if ($anonymous) {
557             $classes .= ' anonymous';
558         }
560         $o .= $this->output->container_start($classes);
561         $o .= $this->output->container_start('header');
563         if (!empty($assessment->title)) {
564             $title = s($assessment->title);
565         } else {
566             $title = get_string('assessment', 'workshop');
567         }
568         if ($this->page->url != $assessment->url) {
569             $o .= $this->output->container(html_writer::link($assessment->url, $title), 'title');
570         } else {
571             $o .= $this->output->container($title, 'title');
572         }
574         if (!$anonymous) {
575             $reviewer   = $assessment->reviewer;
576             $userpic    = $this->output->user_picture($reviewer, array('courseid' => $this->page->course->id, 'size' => 32));
578             $userurl    = new moodle_url('/user/view.php',
579                                        array('id' => $reviewer->id, 'course' => $this->page->course->id));
580             $a          = new stdClass();
581             $a->name    = fullname($reviewer);
582             $a->url     = $userurl->out();
583             $byfullname = get_string('assessmentby', 'workshop', $a);
584             $oo         = $this->output->container($userpic, 'picture');
585             $oo        .= $this->output->container($byfullname, 'fullname');
587             $o .= $this->output->container($oo, 'reviewer');
588         }
590         if (is_null($assessment->realgrade)) {
591             $o .= $this->output->container(
592                 get_string('notassessed', 'workshop'),
593                 'grade nograde'
594             );
595         } else {
596             $a              = new stdClass();
597             $a->max         = $assessment->maxgrade;
598             $a->received    = $assessment->realgrade;
599             $o .= $this->output->container(
600                 get_string('gradeinfo', 'workshop', $a),
601                 'grade'
602             );
604             if (!is_null($assessment->weight) and $assessment->weight != 1) {
605                 $o .= $this->output->container(
606                     get_string('weightinfo', 'workshop', $assessment->weight),
607                     'weight'
608                 );
609             }
610         }
612         $o .= $this->output->container_start('actions');
613         foreach ($assessment->actions as $action) {
614             $o .= $this->output->single_button($action->url, $action->label, $action->method);
615         }
616         $o .= $this->output->container_end(); // actions
618         $o .= $this->output->container_end(); // header
620         if (!is_null($assessment->form)) {
621             $o .= print_collapsible_region_start('assessment-form-wrapper', uniqid('workshop-assessment'),
622                     get_string('assessmentform', 'workshop'), '', false, true);
623             $o .= $this->output->container(self::moodleform($assessment->form), 'assessment-form');
624             $o .= print_collapsible_region_end(true);
625         }
627         $o .= $this->output->container_end(); // main wrapper
629         return $o;
630     }
632     ////////////////////////////////////////////////////////////////////////////
633     // Internal rendering helper methods
634     ////////////////////////////////////////////////////////////////////////////
636     /**
637      * Renders a list of files attached to the submission
638      *
639      * If format==html, then format a html string. If format==text, then format a text-only string.
640      * Otherwise, returns html for non-images and html to display the image inline.
641      *
642      * @param int $submissionid submission identifier
643      * @param string format the format of the returned string - html|text
644      * @return string formatted text to be echoed
645      */
646     protected function helper_submission_attachments($submissionid, $format = 'html') {
647         global $CFG;
648         require_once($CFG->libdir.'/filelib.php');
650         $fs     = get_file_storage();
651         $ctx    = $this->page->context;
652         $files  = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid);
654         $outputimgs     = '';   // images to be displayed inline
655         $outputfiles    = '';   // list of attachment files
657         foreach ($files as $file) {
658             if ($file->is_directory()) {
659                 continue;
660             }
662             $filepath   = $file->get_filepath();
663             $filename   = $file->get_filename();
664             $fileurl    = file_encode_url($CFG->wwwroot . '/pluginfile.php',
665                                 '/' . $ctx->id . '/mod_workshop/submission_attachment/' . $submissionid . $filepath . $filename, true);
666             $type       = $file->get_mimetype();
667             $type       = mimeinfo_from_type('type', $type);
668             $image      = html_writer::empty_tag('img', array('src'=>$this->output->pix_url(file_mimetype_icon($type)), 'alt'=>$type, 'class'=>'icon'));
670             $linkhtml   = html_writer::link($fileurl, $image) . substr($filepath, 1) . html_writer::link($fileurl, $filename);
671             $linktxt    = "$filename [$fileurl]";
673             if ($format == 'html') {
674                 if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) {
675                     $preview     = html_writer::empty_tag('img', array('src' => $fileurl, 'alt' => '', 'class' => 'preview'));
676                     $preview     = html_writer::tag('a', $preview, array('href' => $fileurl));
677                     $outputimgs .= $this->output->container($preview);
679                 } else {
680                     $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type));
681                 }
683             } else if ($format == 'text') {
684                 $outputfiles .= $linktxt . PHP_EOL;
685             }
686         }
688         if ($format == 'html') {
689             if ($outputimgs) {
690                 $outputimgs = $this->output->container($outputimgs, 'images');
691             }
693             if ($outputfiles) {
694                 $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files'));
695             }
697             return $this->output->container($outputimgs . $outputfiles, 'attachments');
699         } else {
700             return $outputfiles;
701         }
702     }
704     /**
705      * Renders the tasks for the single phase in the user plan
706      *
707      * @param stdClass $tasks
708      * @return string html code
709      */
710     protected function helper_user_plan_tasks(array $tasks) {
711         $out = '';
712         foreach ($tasks as $taskcode => $task) {
713             $classes = '';
714             $icon = null;
715             if ($task->completed === true) {
716                 $classes .= ' completed';
717             } elseif ($task->completed === false) {
718                 $classes .= ' fail';
719             } elseif ($task->completed === 'info') {
720                 $classes .= ' info';
721             }
722             if (is_null($task->link)) {
723                 $title = $task->title;
724             } else {
725                 $title = html_writer::link($task->link, $task->title);
726             }
727             $title = $this->output->container($title, 'title');
728             $details = $this->output->container($task->details, 'details');
729             $out .= html_writer::tag('li', $title . $details, array('class' => $classes));
730         }
731         if ($out) {
732             $out = html_writer::tag('ul', $out, array('class' => 'tasks'));
733         }
734         return $out;
735     }
737     /**
738      * Renders a text with icons to sort by the given column
739      *
740      * This is intended for table headings.
741      *
742      * @param string $text    The heading text
743      * @param string $sortid  The column id used for sorting
744      * @param string $sortby  Currently sorted by (column id)
745      * @param string $sorthow Currently sorted how (ASC|DESC)
746      *
747      * @return string
748      */
749     protected function helper_sortable_heading($text, $sortid=null, $sortby=null, $sorthow=null) {
750         global $PAGE;
752         $out = html_writer::tag('span', $text, array('class'=>'text'));
754         if (!is_null($sortid)) {
755             if ($sortby !== $sortid or $sorthow !== 'ASC') {
756                 $url = new moodle_url($PAGE->url);
757                 $url->params(array('sortby' => $sortid, 'sorthow' => 'ASC'));
758                 $out .= $this->output->action_icon($url, new pix_icon('t/up', get_string('sortasc', 'workshop')), null, array('class' => 'sort asc'));
759             }
760             if ($sortby !== $sortid or $sorthow !== 'DESC') {
761                 $url = new moodle_url($PAGE->url);
762                 $url->params(array('sortby' => $sortid, 'sorthow' => 'DESC'));
763                 $out .= $this->output->action_icon($url, new pix_icon('t/down', get_string('sortdesc', 'workshop')), null, array('class' => 'sort desc'));
764             }
765         }
766         return $out;
769     /**
770      * @param stdClass $participant
771      * @param array $userinfo
772      * @return string
773      */
774     protected function helper_grading_report_participant(stdclass $participant, array $userinfo) {
775         $userid = $participant->userid;
776         $out  = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 35));
777         $out .= html_writer::tag('span', fullname($userinfo[$userid]));
779         return $out;
780     }
782     /**
783      * @param stdClass $participant
784      * @return string
785      */
786     protected function helper_grading_report_submission(stdclass $participant) {
787         global $CFG;
789         if (is_null($participant->submissionid)) {
790             $out = $this->output->container(get_string('nosubmissionfound', 'workshop'), 'info');
791         } else {
792             $url = new moodle_url('/mod/workshop/submission.php',
793                                   array('cmid' => $this->page->context->instanceid, 'id' => $participant->submissionid));
794             $out = html_writer::link($url, format_string($participant->submissiontitle), array('class'=>'title'));
795         }
797         return $out;
798     }
800     /**
801      * @todo Highlight the nulls
802      * @param stdClass|null $assessment
803      * @param bool $shownames
804      * @param string $separator between the grade and the reviewer/author
805      * @return string
806      */
807     protected function helper_grading_report_assessment($assessment, $shownames, array $userinfo, $separator) {
808         global $CFG;
810         if (is_null($assessment)) {
811             return get_string('nullgrade', 'workshop');
812         }
813         $a = new stdclass();
814         $a->grade = is_null($assessment->grade) ? get_string('nullgrade', 'workshop') : $assessment->grade;
815         $a->gradinggrade = is_null($assessment->gradinggrade) ? get_string('nullgrade', 'workshop') : $assessment->gradinggrade;
816         $a->weight = $assessment->weight;
817         // grrr the following logic should really be handled by a future language pack feature
818         if (is_null($assessment->gradinggradeover)) {
819             if ($a->weight == 1) {
820                 $grade = get_string('formatpeergrade', 'workshop', $a);
821             } else {
822                 $grade = get_string('formatpeergradeweighted', 'workshop', $a);
823             }
824         } else {
825             $a->gradinggradeover = $assessment->gradinggradeover;
826             if ($a->weight == 1) {
827                 $grade = get_string('formatpeergradeover', 'workshop', $a);
828             } else {
829                 $grade = get_string('formatpeergradeoverweighted', 'workshop', $a);
830             }
831         }
832         $url = new moodle_url('/mod/workshop/assessment.php',
833                               array('asid' => $assessment->assessmentid));
834         $grade = html_writer::link($url, $grade, array('class'=>'grade'));
836         if ($shownames) {
837             $userid = $assessment->userid;
838             $name   = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 16));
839             $name  .= html_writer::tag('span', fullname($userinfo[$userid]), array('class' => 'fullname'));
840             $name   = $separator . html_writer::tag('span', $name, array('class' => 'user'));
841         } else {
842             $name   = '';
843         }
845         return $this->output->container($grade . $name, 'assessmentdetails');
846     }
848     /**
849      * Formats the aggreagated grades
850      */
851     protected function helper_grading_report_grade($grade, $over=null) {
852         $a = new stdclass();
853         $a->grade = is_null($grade) ? get_string('nullgrade', 'workshop') : $grade;
854         if (is_null($over)) {
855             $text = get_string('formataggregatedgrade', 'workshop', $a);
856         } else {
857             $a->over = is_null($over) ? get_string('nullgrade', 'workshop') : $over;
858             $text = get_string('formataggregatedgradeover', 'workshop', $a);
859         }
860         return $text;
861     }
863     ////////////////////////////////////////////////////////////////////////////
864     // Static helpers
865     ////////////////////////////////////////////////////////////////////////////
867     /**
868      * Helper method dealing with the fact we can not just fetch the output of moodleforms
869      *
870      * @param moodleform $mform
871      * @return string HTML
872      */
873     protected static function moodleform(moodleform $mform) {
875         ob_start();
876         $mform->display();
877         $o = ob_get_contents();
878         ob_end_clean();
880         return $o;
881     }
883     /**
884      * Helper function returning the n-th item of the array
885      *
886      * @param array $a
887      * @param int   $n from 0 to m, where m is th number of items in the array
888      * @return mixed the $n-th element of $a
889      */
890     protected static function array_nth(array $a, $n) {
891         $keys = array_keys($a);
892         if ($n < 0 or $n > count($keys) - 1) {
893             return null;
894         }
895         $key = $keys[$n];
896         return $a[$key];
897     }
899     /**
900      * Tries to guess the fullname format set at the site
901      *
902      * @return string fl|lf
903      */
904     protected static function fullname_format() {
905         $fake = new stdclass(); // fake user
906         $fake->lastname = 'LLLL';
907         $fake->firstname = 'FFFF';
908         $fullname = get_string('fullnamedisplay', '', $fake);
909         if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
910             return 'lf';
911         } else {
912             return 'fl';
913         }
914     }