2dd91bc955a0ed4e6d72e522639b9acc1f3aa59f
[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');
94         $o .= $this->output->heading(format_string($submission->title), 3, 'title');
96         if (!$anonymous) {
97             $author             = new stdclass();
98             $author->id         = $submission->authorid;
99             $author->firstname  = $submission->authorfirstname;
100             $author->lastname   = $submission->authorlastname;
101             $author->picture    = $submission->authorpicture;
102             $author->imagealt   = $submission->authorimagealt;
103             $author->email      = $submission->authoremail;
104             $userpic            = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 64));
105             $userurl            = new moodle_url('/user/view.php',
106                                             array('id' => $author->id, 'course' => $this->page->course->id));
107             $a                  = new stdclass();
108             $a->name            = fullname($author);
109             $a->url             = $userurl->out();
110             $byfullname         = get_string('byfullname', 'workshop', $a);
111             $oo  = $this->output->container($userpic, 'picture');
112             $oo .= $this->output->container($byfullname, 'fullname');
114             $o .= $this->output->container($oo, 'author');
115         }
117         $created = get_string('userdatecreated', 'workshop', userdate($submission->timecreated));
118         $o .= $this->output->container($created, 'userdate created');
120         if ($submission->timemodified > $submission->timecreated) {
121             $modified = get_string('userdatemodified', 'workshop', userdate($submission->timemodified));
122             $o .= $this->output->container($modified, 'userdate modified');
123         }
125         $o .= $this->output->container_end(); // end of header
127         $content = format_text($submission->content, $submission->contentformat, array('overflowdiv'=>true));
128         $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $this->page->context->id,
129                                                         'mod_workshop', 'submission_content', $submission->id);
130         $o .= $this->output->container($content, 'content');
132         $o .= $this->helper_submission_attachments($submission->id, 'html');
134         $o .= $this->output->container_end(); // end of submission-full
136         return $o;
137     }
139     /**
140      * Renders short summary of the submission
141      *
142      * @param workshop_submission_summary $summary
143      * @return string text to be echo'ed
144      */
145     protected function render_workshop_submission_summary(workshop_submission_summary $summary) {
147         $o  = '';    // output HTML code
148         $anonymous = $summary->is_anonymous();
149         $classes = 'submission-summary';
151         if ($anonymous) {
152             $classes .= ' anonymous';
153         }
155         $gradestatus = '';
157         if ($summary->status == 'notgraded') {
158             $classes    .= ' notgraded';
159             $gradestatus = $this->output->container(get_string('nogradeyet', 'workshop'), 'grade-status');
161         } else if ($summary->status == 'graded') {
162             $classes    .= ' graded';
163             $gradestatus = $this->output->container(get_string('alreadygraded', 'workshop'), 'grade-status');
164         }
166         $o .= $this->output->container_start($classes);  // main wrapper
167         $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
169         if (!$anonymous) {
170             $author             = new stdClass();
171             $author->id         = $summary->authorid;
172             $author->firstname  = $summary->authorfirstname;
173             $author->lastname   = $summary->authorlastname;
174             $author->picture    = $summary->authorpicture;
175             $author->imagealt   = $summary->authorimagealt;
176             $author->email      = $summary->authoremail;
177             $userpic            = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 35));
178             $userurl            = new moodle_url('/user/view.php',
179                                             array('id' => $author->id, 'course' => $this->page->course->id));
180             $a                  = new stdClass();
181             $a->name            = fullname($author);
182             $a->url             = $userurl->out();
183             $byfullname         = get_string('byfullname', 'workshop', $a);
185             $oo  = $this->output->container($userpic, 'picture');
186             $oo .= $this->output->container($byfullname, 'fullname');
187             $o  .= $this->output->container($oo, 'author');
188         }
190         $created = get_string('userdatecreated', 'workshop', userdate($summary->timecreated));
191         $o .= $this->output->container($created, 'userdate created');
193         if ($summary->timemodified > $summary->timecreated) {
194             $modified = get_string('userdatemodified', 'workshop', userdate($summary->timemodified));
195             $o .= $this->output->container($modified, 'userdate modified');
196         }
198         $o .= $gradestatus;
199         $o .= $this->output->container_end(); // end of the main wrapper
200         return $o;
201     }
203     /**
204      * Renders full workshop example submission
205      *
206      * @param workshop_example_submission $example
207      * @return string HTML
208      */
209     protected function render_workshop_example_submission(workshop_example_submission $example) {
211         $o  = '';    // output HTML code
212         $classes = 'submission-full example';
213         $o .= $this->output->container_start($classes);
214         $o .= $this->output->container_start('header');
215         $o .= $this->output->heading(format_string($example->title), 3, 'title');
216         $o .= $this->output->container_end(); // end of header
218         $content = format_text($example->content, $example->contentformat, array('overflowdiv'=>true));
219         $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $this->page->context->id,
220                                                         'mod_workshop', 'submission_content', $example->id);
221         $o .= $this->output->container($content, 'content');
223         $o .= $this->helper_submission_attachments($example->id, 'html');
225         $o .= $this->output->container_end(); // end of submission-full
227         return $o;
228     }
230     /**
231      * Renders short summary of the example submission
232      *
233      * @param workshop_example_submission_summary $summary
234      * @return string text to be echo'ed
235      */
236     protected function render_workshop_example_submission_summary(workshop_example_submission_summary $summary) {
238         $o  = '';    // output HTML code
240         // wrapping box
241         $o .= $this->output->box_start('generalbox example-summary ' . $summary->status);
243         // title
244         $o .= $this->output->container_start('example-title');
245         $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
247         if ($summary->editable) {
248             $o .= $this->output->action_icon($summary->editurl, new pix_icon('i/edit', get_string('edit')));
249         }
250         $o .= $this->output->container_end();
252         // additional info
253         if ($summary->status == 'notgraded') {
254             $o .= $this->output->container(get_string('nogradeyet', 'workshop'), 'example-info nograde');
255         } else {
256             $o .= $this->output->container(get_string('gradeinfo', 'workshop' , $summary->gradeinfo), 'example-info grade');
257         }
259         // button to assess
260         $button = new single_button($summary->assessurl, $summary->assesslabel, 'get');
261         $o .= $this->output->container($this->output->render($button), 'example-actions');
263         // end of wrapping box
264         $o .= $this->output->box_end();
266         return $o;
267     }
269     /**
270      * Renders the user plannner tool
271      *
272      * @param workshop_user_plan $plan prepared for the user
273      * @return string html code to be displayed
274      */
275     protected function render_workshop_user_plan(workshop_user_plan $plan) {
276         $table = new html_table();
277         $table->attributes['class'] = 'userplan';
278         $table->head = array();
279         $table->colclasses = array();
280         $row = new html_table_row();
281         $row->attributes['class'] = 'phasetasks';
282         foreach ($plan->phases as $phasecode => $phase) {
283             $title = html_writer::tag('span', $phase->title);
284             $actions = '';
285             foreach ($phase->actions as $action) {
286                 switch ($action->type) {
287                 case 'switchphase':
288                     $actions .= $this->output->action_icon($action->url, new pix_icon('i/marker', get_string('switchphase', 'workshop')));
289                     break;
290                 }
291             }
292             if (!empty($actions)) {
293                 $actions = $this->output->container($actions, 'actions');
294             }
295             $table->head[] = $this->output->container($title . $actions);
296             $classes = 'phase' . $phasecode;
297             if ($phase->active) {
298                 $classes .= ' active';
299             } else {
300                 $classes .= ' nonactive';
301             }
302             $table->colclasses[] = $classes;
303             $cell = new html_table_cell();
304             $cell->text = $this->helper_user_plan_tasks($phase->tasks);
305             $row->cells[] = $cell;
306         }
307         $table->data = array($row);
309         return html_writer::table($table);
310     }
312     /**
313      * Renders the result of the submissions allocation process
314      *
315      * @param workshop_allocation_init_result
316      * @return string html to be echoed
317      */
318     protected function render_workshop_allocation_init_result(workshop_allocation_init_result $result) {
320         // start with the message
321         $o = $this->render($result->get_message());
323         // display the details about the process if available
324         $info = $result->get_info();
325         if (is_array($info) and !empty($info)) {
326             $o .= html_writer::start_tag('ul', array('class' => 'allocation-init-results'));
327             foreach ($info as $message) {
328                 $parts  = explode('::', $message);
329                 $text   = array_pop($parts);
330                 $class  = implode(' ', $parts);
331                 if (in_array('debug', $parts) && !debugging('', DEBUG_DEVELOPER)) {
332                     // do not display allocation debugging messages
333                     continue;
334                 }
335                 $o .= html_writer::tag('li', $text, array('class' => $class)) . "\n";
336             }
337             $o .= html_writer::end_tag('ul');
338         }
340         $o .= $this->output->continue_button($result->get_continue_url());
342         return $o;
343     }
345     /**
346      * Renders the workshop grading report
347      *
348      * @param workshop_grading_report $gradingreport
349      * @return string html code
350      */
351     protected function render_workshop_grading_report(workshop_grading_report $gradingreport) {
353         $data       = $gradingreport->get_data();
354         $options    = $gradingreport->get_options();
355         $grades     = $data->grades;
356         $userinfo   = $data->userinfo;
358         if (empty($grades)) {
359             return '';
360         }
362         $table = new html_table();
363         $table->attributes['class'] = 'grading-report';
365         $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'), 'firstname', $options->sortby, $options->sorthow);
366         $sortbylastname = $this->helper_sortable_heading(get_string('lastname'), 'lastname', $options->sortby, $options->sorthow);
367         if (self::fullname_format() == 'lf') {
368             $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
369         } else {
370             $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
371         }
373         $table->head = array();
374         $table->head[] = $sortbyname;
375         $table->head[] = $this->helper_sortable_heading(get_string('submission', 'workshop'), 'submissiontitle',
376                 $options->sortby, $options->sorthow);
377         $table->head[] = $this->helper_sortable_heading(get_string('receivedgrades', 'workshop'));
378         if ($options->showsubmissiongrade) {
379             $table->head[] = $this->helper_sortable_heading(get_string('submissiongradeof', 'workshop', $data->maxgrade),
380                     'submissiongrade', $options->sortby, $options->sorthow);
381         }
382         $table->head[] = $this->helper_sortable_heading(get_string('givengrades', 'workshop'));
383         if ($options->showgradinggrade) {
384             $table->head[] = $this->helper_sortable_heading(get_string('gradinggradeof', 'workshop', $data->maxgradinggrade),
385                     'gradinggrade', $options->sortby, $options->sorthow);
386         }
388         $table->rowclasses  = array();
389         $table->colclasses  = array();
390         $table->data        = array();
392         foreach ($grades as $participant) {
393             $numofreceived  = count($participant->reviewedby);
394             $numofgiven     = count($participant->reviewerof);
395             $published      = $participant->submissionpublished;
397             // compute the number of <tr> table rows needed to display this participant
398             if ($numofreceived > 0 and $numofgiven > 0) {
399                 $numoftrs       = workshop::lcm($numofreceived, $numofgiven);
400                 $spanreceived   = $numoftrs / $numofreceived;
401                 $spangiven      = $numoftrs / $numofgiven;
402             } elseif ($numofreceived == 0 and $numofgiven > 0) {
403                 $numoftrs       = $numofgiven;
404                 $spanreceived   = $numoftrs;
405                 $spangiven      = $numoftrs / $numofgiven;
406             } elseif ($numofreceived > 0 and $numofgiven == 0) {
407                 $numoftrs       = $numofreceived;
408                 $spanreceived   = $numoftrs / $numofreceived;
409                 $spangiven      = $numoftrs;
410             } else {
411                 $numoftrs       = 1;
412                 $spanreceived   = 1;
413                 $spangiven      = 1;
414             }
416             for ($tr = 0; $tr < $numoftrs; $tr++) {
417                 $row = new html_table_row();
418                 if ($published) {
419                     $row->attributes['class'] = 'published';
420                 }
421                 // column #1 - participant - spans over all rows
422                 if ($tr == 0) {
423                     $cell = new html_table_cell();
424                     $cell->text = $this->helper_grading_report_participant($participant, $userinfo);
425                     $cell->rowspan = $numoftrs;
426                     $cell->attributes['class'] = 'participant';
427                     $row->cells[] = $cell;
428                 }
429                 // column #2 - submission - spans over all rows
430                 if ($tr == 0) {
431                     $cell = new html_table_cell();
432                     $cell->text = $this->helper_grading_report_submission($participant);
433                     $cell->rowspan = $numoftrs;
434                     $cell->attributes['class'] = 'submission';
435                     $row->cells[] = $cell;
436                 }
437                 // column #3 - received grades
438                 if ($tr % $spanreceived == 0) {
439                     $idx = intval($tr / $spanreceived);
440                     $assessment = self::array_nth($participant->reviewedby, $idx);
441                     $cell = new html_table_cell();
442                     $cell->text = $this->helper_grading_report_assessment($assessment, $options->showreviewernames, $userinfo,
443                             get_string('gradereceivedfrom', 'workshop'));
444                     $cell->rowspan = $spanreceived;
445                     $cell->attributes['class'] = 'receivedgrade';
446                     if (is_null($assessment) or is_null($assessment->grade)) {
447                         $cell->attributes['class'] .= ' null';
448                     } else {
449                         $cell->attributes['class'] .= ' notnull';
450                     }
451                     $row->cells[] = $cell;
452                 }
453                 // column #4 - total grade for submission
454                 if ($options->showsubmissiongrade and $tr == 0) {
455                     $cell = new html_table_cell();
456                     $cell->text = $this->helper_grading_report_grade($participant->submissiongrade, $participant->submissiongradeover);
457                     $cell->rowspan = $numoftrs;
458                     $cell->attributes['class'] = 'submissiongrade';
459                     $row->cells[] = $cell;
460                 }
461                 // column #5 - given grades
462                 if ($tr % $spangiven == 0) {
463                     $idx = intval($tr / $spangiven);
464                     $assessment = self::array_nth($participant->reviewerof, $idx);
465                     $cell = new html_table_cell();
466                     $cell->text = $this->helper_grading_report_assessment($assessment, $options->showauthornames, $userinfo,
467                             get_string('gradegivento', 'workshop'));
468                     $cell->rowspan = $spangiven;
469                     $cell->attributes['class'] = 'givengrade';
470                     if (is_null($assessment) or is_null($assessment->grade)) {
471                         $cell->attributes['class'] .= ' null';
472                     } else {
473                         $cell->attributes['class'] .= ' notnull';
474                     }
475                     $row->cells[] = $cell;
476                 }
477                 // column #6 - total grade for assessment
478                 if ($options->showgradinggrade and $tr == 0) {
479                     $cell = new html_table_cell();
480                     $cell->text = $this->helper_grading_report_grade($participant->gradinggrade);
481                     $cell->rowspan = $numoftrs;
482                     $cell->attributes['class'] = 'gradinggrade';
483                     $row->cells[] = $cell;
484                 }
486                 $table->data[] = $row;
487             }
488         }
490         return html_writer::table($table);
491     }
493     /**
494      * Renders the feedback for the author of the submission
495      *
496      * @param workshop_feedback_author $feedback
497      * @return string HTML
498      */
499     protected function render_workshop_feedback_author(workshop_feedback_author $feedback) {
500         return $this->helper_render_feedback($feedback);
501     }
503     /**
504      * Renders the feedback for the reviewer of the submission
505      *
506      * @param workshop_feedback_reviewer $feedback
507      * @return string HTML
508      */
509     protected function render_workshop_feedback_reviewer(workshop_feedback_reviewer $feedback) {
510         return $this->helper_render_feedback($feedback);
511     }
513     /**
514      * Helper method to rendering feedback
515      *
516      * @param workshop_feedback_author|workshop_feedback_reviewer $feedback
517      * @return string HTML
518      */
519     private function helper_render_feedback($feedback) {
521         $o  = '';    // output HTML code
522         $o .= $this->output->container_start('feedback feedbackforauthor');
523         $o .= $this->output->container_start('header');
524         $o .= $this->output->heading(get_string('feedbackby', 'workshop', s(fullname($feedback->get_provider()))), 3, 'title');
526         $userpic = $this->output->user_picture($feedback->get_provider(), array('courseid' => $this->page->course->id, 'size' => 32));
527         $o .= $this->output->container($userpic, 'picture');
528         $o .= $this->output->container_end(); // end of header
530         $content = format_text($feedback->get_content(), $feedback->get_format(), array('overflowdiv' => true));
531         $o .= $this->output->container($content, 'content');
533         $o .= $this->output->container_end();
535         return $o;
536     }
538     /**
539      * Renders the full assessment
540      *
541      * @param workshop_assessment $assessment
542      * @return string HTML
543      */
544     protected function render_workshop_assessment(workshop_assessment $assessment) {
546         $o = ''; // output HTML code
547         $anonymous = is_null($assessment->reviewer);
548         $classes = 'assessment-full';
549         if ($anonymous) {
550             $classes .= ' anonymous';
551         }
553         $o .= $this->output->container_start($classes);
554         $o .= $this->output->container_start('header');
556         if (!empty($assessment->title)) {
557             $o .= $this->output->container(s($assessment->title), 'title');
558         } else {
559             $o .= $this->output->container(get_string('assessment', 'workshop'), 'title');
560         }
562         if (!$anonymous) {
563             $reviewer   = $assessment->reviewer;
564             $userpic    = $this->output->user_picture($reviewer, array('courseid' => $this->page->course->id, 'size' => 32));
566             $userurl    = new moodle_url('/user/view.php',
567                                        array('id' => $reviewer->id, 'course' => $this->page->course->id));
568             $a          = new stdClass();
569             $a->name    = fullname($reviewer);
570             $a->url     = $userurl->out();
571             $byfullname = get_string('assessmentby', 'workshop', $a);
572             $oo         = $this->output->container($userpic, 'picture');
573             $oo        .= $this->output->container($byfullname, 'fullname');
575             $o .= $this->output->container($oo, 'reviewer');
576         }
578         if (is_null($assessment->realgrade)) {
579             $o .= $this->output->container(
580                 get_string('notassessed', 'workshop'),
581                 'grade nograde'
582             );
583         } else {
584             $a              = new stdClass();
585             $a->max         = $assessment->maxgrade;
586             $a->received    = $assessment->realgrade;
587             $o .= $this->output->container(
588                 get_string('gradeinfo', 'workshop', $a),
589                 'grade'
590             );
592             if (!is_null($assessment->weight) and $assessment->weight != 1) {
593                 $o .= $this->output->container(
594                     get_string('weightinfo', 'workshop', $assessment->weight),
595                     'weight'
596                 );
597             }
598         }
600         $o .= $this->output->container_start('actions');
601         foreach ($assessment->actions as $action) {
602             $o .= $this->output->single_button($action->url, $action->label, $action->method);
603         }
604         $o .= $this->output->container_end(); // actions
606         $o .= $this->output->container_end(); // header
608         if (!is_null($assessment->form)) {
609             $o .= print_collapsible_region_start('assessment-form-wrapper', uniqid('workshop-assessment'),
610                     get_string('assessmentform', 'workshop'), '', false, true);
611             $o .= $this->output->container(self::moodleform($assessment->form), 'assessment-form');
612             $o .= print_collapsible_region_end(true);
613         }
615         $o .= $this->output->container_end(); // main wrapper
617         return $o;
618     }
620     ////////////////////////////////////////////////////////////////////////////
621     // Internal rendering helper methods
622     ////////////////////////////////////////////////////////////////////////////
624     /**
625      * Renders a list of files attached to the submission
626      *
627      * If format==html, then format a html string. If format==text, then format a text-only string.
628      * Otherwise, returns html for non-images and html to display the image inline.
629      *
630      * @param int $submissionid submission identifier
631      * @param string format the format of the returned string - html|text
632      * @return string formatted text to be echoed
633      */
634     protected function helper_submission_attachments($submissionid, $format = 'html') {
635         global $CFG;
636         require_once($CFG->libdir.'/filelib.php');
638         $fs     = get_file_storage();
639         $ctx    = $this->page->context;
640         $files  = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid);
642         $outputimgs     = '';   // images to be displayed inline
643         $outputfiles    = '';   // list of attachment files
645         foreach ($files as $file) {
646             if ($file->is_directory()) {
647                 continue;
648             }
650             $filepath   = $file->get_filepath();
651             $filename   = $file->get_filename();
652             $fileurl    = file_encode_url($CFG->wwwroot . '/pluginfile.php',
653                                 '/' . $ctx->id . '/mod_workshop/submission_attachment/' . $submissionid . $filepath . $filename, true);
654             $type       = $file->get_mimetype();
655             $type       = mimeinfo_from_type('type', $type);
656             $image      = html_writer::empty_tag('img', array('src'=>$this->output->pix_url(file_mimetype_icon($type)), 'alt'=>$type, 'class'=>'icon'));
658             $linkhtml   = html_writer::link($fileurl, $image) . substr($filepath, 1) . html_writer::link($fileurl, $filename);
659             $linktxt    = "$filename [$fileurl]";
661             if ($format == 'html') {
662                 if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) {
663                     $preview     = html_writer::empty_tag('img', array('src' => $fileurl, 'alt' => '', 'class' => 'preview'));
664                     $preview     = html_writer::tag('a', $preview, array('href' => $fileurl));
665                     $outputimgs .= $this->output->container($preview);
667                 } else {
668                     $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type));
669                 }
671             } else if ($format == 'text') {
672                 $outputfiles .= $linktxt . PHP_EOL;
673             }
674         }
676         if ($format == 'html') {
677             if ($outputimgs) {
678                 $outputimgs = $this->output->container($outputimgs, 'images');
679             }
681             if ($outputfiles) {
682                 $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files'));
683             }
685             return $this->output->container($outputimgs . $outputfiles, 'attachments');
687         } else {
688             return $outputfiles;
689         }
690     }
692     /**
693      * Renders the tasks for the single phase in the user plan
694      *
695      * @param stdClass $tasks
696      * @return string html code
697      */
698     protected function helper_user_plan_tasks(array $tasks) {
699         $out = '';
700         foreach ($tasks as $taskcode => $task) {
701             $classes = '';
702             $icon = null;
703             if ($task->completed === true) {
704                 $classes .= ' completed';
705             } elseif ($task->completed === false) {
706                 $classes .= ' fail';
707             } elseif ($task->completed === 'info') {
708                 $classes .= ' info';
709             }
710             if (is_null($task->link)) {
711                 $title = $task->title;
712             } else {
713                 $title = html_writer::link($task->link, $task->title);
714             }
715             $title = $this->output->container($title, 'title');
716             $details = $this->output->container($task->details, 'details');
717             $out .= html_writer::tag('li', $title . $details, array('class' => $classes));
718         }
719         if ($out) {
720             $out = html_writer::tag('ul', $out, array('class' => 'tasks'));
721         }
722         return $out;
723     }
725     /**
726      * Renders a text with icons to sort by the given column
727      *
728      * This is intended for table headings.
729      *
730      * @param string $text    The heading text
731      * @param string $sortid  The column id used for sorting
732      * @param string $sortby  Currently sorted by (column id)
733      * @param string $sorthow Currently sorted how (ASC|DESC)
734      *
735      * @return string
736      */
737     protected function helper_sortable_heading($text, $sortid=null, $sortby=null, $sorthow=null) {
738         global $PAGE;
740         $out = html_writer::tag('span', $text, array('class'=>'text'));
742         if (!is_null($sortid)) {
743             if ($sortby !== $sortid or $sorthow !== 'ASC') {
744                 $url = new moodle_url($PAGE->url);
745                 $url->params(array('sortby' => $sortid, 'sorthow' => 'ASC'));
746                 $out .= $this->output->action_icon($url, new pix_icon('t/up', get_string('sortasc', 'workshop')), null, array('class' => 'sort asc'));
747             }
748             if ($sortby !== $sortid or $sorthow !== 'DESC') {
749                 $url = new moodle_url($PAGE->url);
750                 $url->params(array('sortby' => $sortid, 'sorthow' => 'DESC'));
751                 $out .= $this->output->action_icon($url, new pix_icon('t/down', get_string('sortdesc', 'workshop')), null, array('class' => 'sort desc'));
752             }
753         }
754         return $out;
757     /**
758      * @param stdClass $participant
759      * @param array $userinfo
760      * @return string
761      */
762     protected function helper_grading_report_participant(stdclass $participant, array $userinfo) {
763         $userid = $participant->userid;
764         $out  = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 35));
765         $out .= html_writer::tag('span', fullname($userinfo[$userid]));
767         return $out;
768     }
770     /**
771      * @param stdClass $participant
772      * @return string
773      */
774     protected function helper_grading_report_submission(stdclass $participant) {
775         global $CFG;
777         if (is_null($participant->submissionid)) {
778             $out = $this->output->container(get_string('nosubmissionfound', 'workshop'), 'info');
779         } else {
780             $url = new moodle_url('/mod/workshop/submission.php',
781                                   array('cmid' => $this->page->context->instanceid, 'id' => $participant->submissionid));
782             $out = html_writer::link($url, format_string($participant->submissiontitle), array('class'=>'title'));
783         }
785         return $out;
786     }
788     /**
789      * @todo Highlight the nulls
790      * @param stdClass|null $assessment
791      * @param bool $shownames
792      * @param string $separator between the grade and the reviewer/author
793      * @return string
794      */
795     protected function helper_grading_report_assessment($assessment, $shownames, array $userinfo, $separator) {
796         global $CFG;
798         if (is_null($assessment)) {
799             return get_string('nullgrade', 'workshop');
800         }
801         $a = new stdclass();
802         $a->grade = is_null($assessment->grade) ? get_string('nullgrade', 'workshop') : $assessment->grade;
803         $a->gradinggrade = is_null($assessment->gradinggrade) ? get_string('nullgrade', 'workshop') : $assessment->gradinggrade;
804         $a->weight = $assessment->weight;
805         // grrr the following logic should really be handled by a future language pack feature
806         if (is_null($assessment->gradinggradeover)) {
807             if ($a->weight == 1) {
808                 $grade = get_string('formatpeergrade', 'workshop', $a);
809             } else {
810                 $grade = get_string('formatpeergradeweighted', 'workshop', $a);
811             }
812         } else {
813             $a->gradinggradeover = $assessment->gradinggradeover;
814             if ($a->weight == 1) {
815                 $grade = get_string('formatpeergradeover', 'workshop', $a);
816             } else {
817                 $grade = get_string('formatpeergradeoverweighted', 'workshop', $a);
818             }
819         }
820         $url = new moodle_url('/mod/workshop/assessment.php',
821                               array('asid' => $assessment->assessmentid));
822         $grade = html_writer::link($url, $grade, array('class'=>'grade'));
824         if ($shownames) {
825             $userid = $assessment->userid;
826             $name   = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 16));
827             $name  .= html_writer::tag('span', fullname($userinfo[$userid]), array('class' => 'fullname'));
828             $name   = $separator . html_writer::tag('span', $name, array('class' => 'user'));
829         } else {
830             $name   = '';
831         }
833         return $this->output->container($grade . $name, 'assessmentdetails');
834     }
836     /**
837      * Formats the aggreagated grades
838      */
839     protected function helper_grading_report_grade($grade, $over=null) {
840         $a = new stdclass();
841         $a->grade = is_null($grade) ? get_string('nullgrade', 'workshop') : $grade;
842         if (is_null($over)) {
843             $text = get_string('formataggregatedgrade', 'workshop', $a);
844         } else {
845             $a->over = is_null($over) ? get_string('nullgrade', 'workshop') : $over;
846             $text = get_string('formataggregatedgradeover', 'workshop', $a);
847         }
848         return $text;
849     }
851     ////////////////////////////////////////////////////////////////////////////
852     // Static helpers
853     ////////////////////////////////////////////////////////////////////////////
855     /**
856      * Helper method dealing with the fact we can not just fetch the output of moodleforms
857      *
858      * @param moodleform $mform
859      * @return string HTML
860      */
861     protected static function moodleform(moodleform $mform) {
863         ob_start();
864         $mform->display();
865         $o = ob_get_contents();
866         ob_end_clean();
868         return $o;
869     }
871     /**
872      * Helper function returning the n-th item of the array
873      *
874      * @param array $a
875      * @param int   $n from 0 to m, where m is th number of items in the array
876      * @return mixed the $n-th element of $a
877      */
878     protected static function array_nth(array $a, $n) {
879         $keys = array_keys($a);
880         if ($n < 0 or $n > count($keys) - 1) {
881             return null;
882         }
883         $key = $keys[$n];
884         return $a[$key];
885     }
887     /**
888      * Tries to guess the fullname format set at the site
889      *
890      * @return string fl|lf
891      */
892     protected static function fullname_format() {
893         $fake = new stdclass(); // fake user
894         $fake->lastname = 'LLLL';
895         $fake->firstname = 'FFFF';
896         $fullname = get_string('fullnamedisplay', '', $fake);
897         if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
898             return 'lf';
899         } else {
900             return 'fl';
901         }
902     }