MDL-36415 usability: Use new up/down and ordering icons
[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) {
85         global $CFG;
87         $o  = '';    // output HTML code
88         $anonymous = $submission->is_anonymous();
89         $classes = 'submission-full';
90         if ($anonymous) {
91             $classes .= ' anonymous';
92         }
93         $o .= $this->output->container_start($classes);
94         $o .= $this->output->container_start('header');
96         $title = format_string($submission->title);
98         if ($this->page->url != $submission->url) {
99             $title = html_writer::link($submission->url, $title);
100         }
102         $o .= $this->output->heading($title, 3, 'title');
104         if (!$anonymous) {
105             $author             = new stdclass();
106             $author->id         = $submission->authorid;
107             $author->firstname  = $submission->authorfirstname;
108             $author->lastname   = $submission->authorlastname;
109             $author->picture    = $submission->authorpicture;
110             $author->imagealt   = $submission->authorimagealt;
111             $author->email      = $submission->authoremail;
112             $userpic            = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 64));
113             $userurl            = new moodle_url('/user/view.php',
114                                             array('id' => $author->id, 'course' => $this->page->course->id));
115             $a                  = new stdclass();
116             $a->name            = fullname($author);
117             $a->url             = $userurl->out();
118             $byfullname         = get_string('byfullname', 'workshop', $a);
119             $oo  = $this->output->container($userpic, 'picture');
120             $oo .= $this->output->container($byfullname, 'fullname');
122             $o .= $this->output->container($oo, 'author');
123         }
125         $created = get_string('userdatecreated', 'workshop', userdate($submission->timecreated));
126         $o .= $this->output->container($created, 'userdate created');
128         if ($submission->timemodified > $submission->timecreated) {
129             $modified = get_string('userdatemodified', 'workshop', userdate($submission->timemodified));
130             $o .= $this->output->container($modified, 'userdate modified');
131         }
133         $o .= $this->output->container_end(); // end of header
135         $content = format_text($submission->content, $submission->contentformat, array('overflowdiv'=>true));
136         $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $this->page->context->id,
137                                                         'mod_workshop', 'submission_content', $submission->id);
138         if (!empty($content)) {
139             if (!empty($CFG->enableplagiarism)) {
140                 require_once($CFG->libdir.'/plagiarismlib.php');
141                 $content .= plagiarism_get_links(array('userid' => $submission->authorid,
142                     'content' => $submission->content,
143                     'cmid' => $this->page->cm->id,
144                     'course' => $this->page->course));
145             }
146         }
147         $o .= $this->output->container($content, 'content');
149         $o .= $this->helper_submission_attachments($submission->id, 'html');
151         $o .= $this->output->container_end(); // end of submission-full
153         return $o;
154     }
156     /**
157      * Renders short summary of the submission
158      *
159      * @param workshop_submission_summary $summary
160      * @return string text to be echo'ed
161      */
162     protected function render_workshop_submission_summary(workshop_submission_summary $summary) {
164         $o  = '';    // output HTML code
165         $anonymous = $summary->is_anonymous();
166         $classes = 'submission-summary';
168         if ($anonymous) {
169             $classes .= ' anonymous';
170         }
172         $gradestatus = '';
174         if ($summary->status == 'notgraded') {
175             $classes    .= ' notgraded';
176             $gradestatus = $this->output->container(get_string('nogradeyet', 'workshop'), 'grade-status');
178         } else if ($summary->status == 'graded') {
179             $classes    .= ' graded';
180             $gradestatus = $this->output->container(get_string('alreadygraded', 'workshop'), 'grade-status');
181         }
183         $o .= $this->output->container_start($classes);  // main wrapper
184         $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
186         if (!$anonymous) {
187             $author             = new stdClass();
188             $author->id         = $summary->authorid;
189             $author->firstname  = $summary->authorfirstname;
190             $author->lastname   = $summary->authorlastname;
191             $author->picture    = $summary->authorpicture;
192             $author->imagealt   = $summary->authorimagealt;
193             $author->email      = $summary->authoremail;
194             $userpic            = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 35));
195             $userurl            = new moodle_url('/user/view.php',
196                                             array('id' => $author->id, 'course' => $this->page->course->id));
197             $a                  = new stdClass();
198             $a->name            = fullname($author);
199             $a->url             = $userurl->out();
200             $byfullname         = get_string('byfullname', 'workshop', $a);
202             $oo  = $this->output->container($userpic, 'picture');
203             $oo .= $this->output->container($byfullname, 'fullname');
204             $o  .= $this->output->container($oo, 'author');
205         }
207         $created = get_string('userdatecreated', 'workshop', userdate($summary->timecreated));
208         $o .= $this->output->container($created, 'userdate created');
210         if ($summary->timemodified > $summary->timecreated) {
211             $modified = get_string('userdatemodified', 'workshop', userdate($summary->timemodified));
212             $o .= $this->output->container($modified, 'userdate modified');
213         }
215         $o .= $gradestatus;
216         $o .= $this->output->container_end(); // end of the main wrapper
217         return $o;
218     }
220     /**
221      * Renders full workshop example submission
222      *
223      * @param workshop_example_submission $example
224      * @return string HTML
225      */
226     protected function render_workshop_example_submission(workshop_example_submission $example) {
228         $o  = '';    // output HTML code
229         $classes = 'submission-full example';
230         $o .= $this->output->container_start($classes);
231         $o .= $this->output->container_start('header');
232         $o .= $this->output->heading(format_string($example->title), 3, 'title');
233         $o .= $this->output->container_end(); // end of header
235         $content = format_text($example->content, $example->contentformat, array('overflowdiv'=>true));
236         $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $this->page->context->id,
237                                                         'mod_workshop', 'submission_content', $example->id);
238         $o .= $this->output->container($content, 'content');
240         $o .= $this->helper_submission_attachments($example->id, 'html');
242         $o .= $this->output->container_end(); // end of submission-full
244         return $o;
245     }
247     /**
248      * Renders short summary of the example submission
249      *
250      * @param workshop_example_submission_summary $summary
251      * @return string text to be echo'ed
252      */
253     protected function render_workshop_example_submission_summary(workshop_example_submission_summary $summary) {
255         $o  = '';    // output HTML code
257         // wrapping box
258         $o .= $this->output->box_start('generalbox example-summary ' . $summary->status);
260         // title
261         $o .= $this->output->container_start('example-title');
262         $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
264         if ($summary->editable) {
265             $o .= $this->output->action_icon($summary->editurl, new pix_icon('i/edit', get_string('edit')));
266         }
267         $o .= $this->output->container_end();
269         // additional info
270         if ($summary->status == 'notgraded') {
271             $o .= $this->output->container(get_string('nogradeyet', 'workshop'), 'example-info nograde');
272         } else {
273             $o .= $this->output->container(get_string('gradeinfo', 'workshop' , $summary->gradeinfo), 'example-info grade');
274         }
276         // button to assess
277         $button = new single_button($summary->assessurl, $summary->assesslabel, 'get');
278         $o .= $this->output->container($this->output->render($button), 'example-actions');
280         // end of wrapping box
281         $o .= $this->output->box_end();
283         return $o;
284     }
286     /**
287      * Renders the user plannner tool
288      *
289      * @param workshop_user_plan $plan prepared for the user
290      * @return string html code to be displayed
291      */
292     protected function render_workshop_user_plan(workshop_user_plan $plan) {
293         $table = new html_table();
294         $table->attributes['class'] = 'userplan';
295         $table->head = array();
296         $table->colclasses = array();
297         $row = new html_table_row();
298         $row->attributes['class'] = 'phasetasks';
299         foreach ($plan->phases as $phasecode => $phase) {
300             $title = html_writer::tag('span', $phase->title);
301             $actions = '';
302             foreach ($phase->actions as $action) {
303                 switch ($action->type) {
304                 case 'switchphase':
305                     $icon = 'i/marker';
306                     if ($phasecode == workshop::PHASE_ASSESSMENT
307                             and $plan->workshop->phase == workshop::PHASE_SUBMISSION
308                             and $plan->workshop->phaseswitchassessment) {
309                         $icon = 'i/scheduled';
310                     }
311                     $actions .= $this->output->action_icon($action->url, new pix_icon($icon, get_string('switchphase', 'workshop')));
312                     break;
313                 }
314             }
315             if (!empty($actions)) {
316                 $actions = $this->output->container($actions, 'actions');
317             }
318             $table->head[] = $this->output->container($title . $actions);
319             $classes = 'phase' . $phasecode;
320             if ($phase->active) {
321                 $classes .= ' active';
322             } else {
323                 $classes .= ' nonactive';
324             }
325             $table->colclasses[] = $classes;
326             $cell = new html_table_cell();
327             $cell->text = $this->helper_user_plan_tasks($phase->tasks);
328             $row->cells[] = $cell;
329         }
330         $table->data = array($row);
332         return html_writer::table($table);
333     }
335     /**
336      * Renders the result of the submissions allocation process
337      *
338      * @param workshop_allocation_result $result as returned by the allocator's init() method
339      * @return string HTML to be echoed
340      */
341     protected function render_workshop_allocation_result(workshop_allocation_result $result) {
343         $status = $result->get_status();
345         if (is_null($status) or $status == workshop_allocation_result::STATUS_VOID) {
346             debugging('Attempt to render workshop_allocation_result with empty status', DEBUG_DEVELOPER);
347             return '';
348         }
350         switch ($status) {
351         case workshop_allocation_result::STATUS_FAILED:
352             if ($message = $result->get_message()) {
353                 $message = new workshop_message($message, workshop_message::TYPE_ERROR);
354             } else {
355                 $message = new workshop_message(get_string('allocationerror', 'workshop'), workshop_message::TYPE_ERROR);
356             }
357             break;
359         case workshop_allocation_result::STATUS_CONFIGURED:
360             if ($message = $result->get_message()) {
361                 $message = new workshop_message($message, workshop_message::TYPE_INFO);
362             } else {
363                 $message = new workshop_message(get_string('allocationconfigured', 'workshop'), workshop_message::TYPE_INFO);
364             }
365             break;
367         case workshop_allocation_result::STATUS_EXECUTED:
368             if ($message = $result->get_message()) {
369                 $message = new workshop_message($message, workshop_message::TYPE_OK);
370             } else {
371                 $message = new workshop_message(get_string('allocationdone', 'workshop'), workshop_message::TYPE_OK);
372             }
373             break;
375         default:
376             throw new coding_exception('Unknown allocation result status', $status);
377         }
379         // start with the message
380         $o = $this->render($message);
382         // display the details about the process if available
383         $logs = $result->get_logs();
384         if (is_array($logs) and !empty($logs)) {
385             $o .= html_writer::start_tag('ul', array('class' => 'allocation-init-results'));
386             foreach ($logs as $log) {
387                 if ($log->type == 'debug' and !debugging('', DEBUG_DEVELOPER)) {
388                     // display allocation debugging messages for developers only
389                     continue;
390                 }
391                 $class = $log->type;
392                 if ($log->indent) {
393                     $class .= ' indent';
394                 }
395                 $o .= html_writer::tag('li', $log->message, array('class' => $class)).PHP_EOL;
396             }
397             $o .= html_writer::end_tag('ul');
398         }
400         return $o;
401     }
403     /**
404      * Renders the workshop grading report
405      *
406      * @param workshop_grading_report $gradingreport
407      * @return string html code
408      */
409     protected function render_workshop_grading_report(workshop_grading_report $gradingreport) {
411         $data       = $gradingreport->get_data();
412         $options    = $gradingreport->get_options();
413         $grades     = $data->grades;
414         $userinfo   = $data->userinfo;
416         if (empty($grades)) {
417             return '';
418         }
420         $table = new html_table();
421         $table->attributes['class'] = 'grading-report';
423         $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'), 'firstname', $options->sortby, $options->sorthow);
424         $sortbylastname = $this->helper_sortable_heading(get_string('lastname'), 'lastname', $options->sortby, $options->sorthow);
425         if (self::fullname_format() == 'lf') {
426             $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
427         } else {
428             $sortbyname = $sortbyfirstname . ' / ' . $sortbylastname;
429         }
431         $table->head = array();
432         $table->head[] = $sortbyname;
433         $table->head[] = $this->helper_sortable_heading(get_string('submission', 'workshop'), 'submissiontitle',
434                 $options->sortby, $options->sorthow);
435         $table->head[] = $this->helper_sortable_heading(get_string('receivedgrades', 'workshop'));
436         if ($options->showsubmissiongrade) {
437             $table->head[] = $this->helper_sortable_heading(get_string('submissiongradeof', 'workshop', $data->maxgrade),
438                     'submissiongrade', $options->sortby, $options->sorthow);
439         }
440         $table->head[] = $this->helper_sortable_heading(get_string('givengrades', 'workshop'));
441         if ($options->showgradinggrade) {
442             $table->head[] = $this->helper_sortable_heading(get_string('gradinggradeof', 'workshop', $data->maxgradinggrade),
443                     'gradinggrade', $options->sortby, $options->sorthow);
444         }
446         $table->rowclasses  = array();
447         $table->colclasses  = array();
448         $table->data        = array();
450         foreach ($grades as $participant) {
451             $numofreceived  = count($participant->reviewedby);
452             $numofgiven     = count($participant->reviewerof);
453             $published      = $participant->submissionpublished;
455             // compute the number of <tr> table rows needed to display this participant
456             if ($numofreceived > 0 and $numofgiven > 0) {
457                 $numoftrs       = workshop::lcm($numofreceived, $numofgiven);
458                 $spanreceived   = $numoftrs / $numofreceived;
459                 $spangiven      = $numoftrs / $numofgiven;
460             } elseif ($numofreceived == 0 and $numofgiven > 0) {
461                 $numoftrs       = $numofgiven;
462                 $spanreceived   = $numoftrs;
463                 $spangiven      = $numoftrs / $numofgiven;
464             } elseif ($numofreceived > 0 and $numofgiven == 0) {
465                 $numoftrs       = $numofreceived;
466                 $spanreceived   = $numoftrs / $numofreceived;
467                 $spangiven      = $numoftrs;
468             } else {
469                 $numoftrs       = 1;
470                 $spanreceived   = 1;
471                 $spangiven      = 1;
472             }
474             for ($tr = 0; $tr < $numoftrs; $tr++) {
475                 $row = new html_table_row();
476                 if ($published) {
477                     $row->attributes['class'] = 'published';
478                 }
479                 // column #1 - participant - spans over all rows
480                 if ($tr == 0) {
481                     $cell = new html_table_cell();
482                     $cell->text = $this->helper_grading_report_participant($participant, $userinfo);
483                     $cell->rowspan = $numoftrs;
484                     $cell->attributes['class'] = 'participant';
485                     $row->cells[] = $cell;
486                 }
487                 // column #2 - submission - spans over all rows
488                 if ($tr == 0) {
489                     $cell = new html_table_cell();
490                     $cell->text = $this->helper_grading_report_submission($participant);
491                     $cell->rowspan = $numoftrs;
492                     $cell->attributes['class'] = 'submission';
493                     $row->cells[] = $cell;
494                 }
495                 // column #3 - received grades
496                 if ($tr % $spanreceived == 0) {
497                     $idx = intval($tr / $spanreceived);
498                     $assessment = self::array_nth($participant->reviewedby, $idx);
499                     $cell = new html_table_cell();
500                     $cell->text = $this->helper_grading_report_assessment($assessment, $options->showreviewernames, $userinfo,
501                             get_string('gradereceivedfrom', 'workshop'));
502                     $cell->rowspan = $spanreceived;
503                     $cell->attributes['class'] = 'receivedgrade';
504                     if (is_null($assessment) or is_null($assessment->grade)) {
505                         $cell->attributes['class'] .= ' null';
506                     } else {
507                         $cell->attributes['class'] .= ' notnull';
508                     }
509                     $row->cells[] = $cell;
510                 }
511                 // column #4 - total grade for submission
512                 if ($options->showsubmissiongrade and $tr == 0) {
513                     $cell = new html_table_cell();
514                     $cell->text = $this->helper_grading_report_grade($participant->submissiongrade, $participant->submissiongradeover);
515                     $cell->rowspan = $numoftrs;
516                     $cell->attributes['class'] = 'submissiongrade';
517                     $row->cells[] = $cell;
518                 }
519                 // column #5 - given grades
520                 if ($tr % $spangiven == 0) {
521                     $idx = intval($tr / $spangiven);
522                     $assessment = self::array_nth($participant->reviewerof, $idx);
523                     $cell = new html_table_cell();
524                     $cell->text = $this->helper_grading_report_assessment($assessment, $options->showauthornames, $userinfo,
525                             get_string('gradegivento', 'workshop'));
526                     $cell->rowspan = $spangiven;
527                     $cell->attributes['class'] = 'givengrade';
528                     if (is_null($assessment) or is_null($assessment->grade)) {
529                         $cell->attributes['class'] .= ' null';
530                     } else {
531                         $cell->attributes['class'] .= ' notnull';
532                     }
533                     $row->cells[] = $cell;
534                 }
535                 // column #6 - total grade for assessment
536                 if ($options->showgradinggrade and $tr == 0) {
537                     $cell = new html_table_cell();
538                     $cell->text = $this->helper_grading_report_grade($participant->gradinggrade);
539                     $cell->rowspan = $numoftrs;
540                     $cell->attributes['class'] = 'gradinggrade';
541                     $row->cells[] = $cell;
542                 }
544                 $table->data[] = $row;
545             }
546         }
548         return html_writer::table($table);
549     }
551     /**
552      * Renders the feedback for the author of the submission
553      *
554      * @param workshop_feedback_author $feedback
555      * @return string HTML
556      */
557     protected function render_workshop_feedback_author(workshop_feedback_author $feedback) {
558         return $this->helper_render_feedback($feedback);
559     }
561     /**
562      * Renders the feedback for the reviewer of the submission
563      *
564      * @param workshop_feedback_reviewer $feedback
565      * @return string HTML
566      */
567     protected function render_workshop_feedback_reviewer(workshop_feedback_reviewer $feedback) {
568         return $this->helper_render_feedback($feedback);
569     }
571     /**
572      * Helper method to rendering feedback
573      *
574      * @param workshop_feedback_author|workshop_feedback_reviewer $feedback
575      * @return string HTML
576      */
577     private function helper_render_feedback($feedback) {
579         $o  = '';    // output HTML code
580         $o .= $this->output->container_start('feedback feedbackforauthor');
581         $o .= $this->output->container_start('header');
582         $o .= $this->output->heading(get_string('feedbackby', 'workshop', s(fullname($feedback->get_provider()))), 3, 'title');
584         $userpic = $this->output->user_picture($feedback->get_provider(), array('courseid' => $this->page->course->id, 'size' => 32));
585         $o .= $this->output->container($userpic, 'picture');
586         $o .= $this->output->container_end(); // end of header
588         $content = format_text($feedback->get_content(), $feedback->get_format(), array('overflowdiv' => true));
589         $o .= $this->output->container($content, 'content');
591         $o .= $this->output->container_end();
593         return $o;
594     }
596     /**
597      * Renders the full assessment
598      *
599      * @param workshop_assessment $assessment
600      * @return string HTML
601      */
602     protected function render_workshop_assessment(workshop_assessment $assessment) {
604         $o = ''; // output HTML code
605         $anonymous = is_null($assessment->reviewer);
606         $classes = 'assessment-full';
607         if ($anonymous) {
608             $classes .= ' anonymous';
609         }
611         $o .= $this->output->container_start($classes);
612         $o .= $this->output->container_start('header');
614         if (!empty($assessment->title)) {
615             $title = s($assessment->title);
616         } else {
617             $title = get_string('assessment', 'workshop');
618         }
619         if (($assessment->url instanceof moodle_url) and ($this->page->url != $assessment->url)) {
620             $o .= $this->output->container(html_writer::link($assessment->url, $title), 'title');
621         } else {
622             $o .= $this->output->container($title, 'title');
623         }
625         if (!$anonymous) {
626             $reviewer   = $assessment->reviewer;
627             $userpic    = $this->output->user_picture($reviewer, array('courseid' => $this->page->course->id, 'size' => 32));
629             $userurl    = new moodle_url('/user/view.php',
630                                        array('id' => $reviewer->id, 'course' => $this->page->course->id));
631             $a          = new stdClass();
632             $a->name    = fullname($reviewer);
633             $a->url     = $userurl->out();
634             $byfullname = get_string('assessmentby', 'workshop', $a);
635             $oo         = $this->output->container($userpic, 'picture');
636             $oo        .= $this->output->container($byfullname, 'fullname');
638             $o .= $this->output->container($oo, 'reviewer');
639         }
641         if (is_null($assessment->realgrade)) {
642             $o .= $this->output->container(
643                 get_string('notassessed', 'workshop'),
644                 'grade nograde'
645             );
646         } else {
647             $a              = new stdClass();
648             $a->max         = $assessment->maxgrade;
649             $a->received    = $assessment->realgrade;
650             $o .= $this->output->container(
651                 get_string('gradeinfo', 'workshop', $a),
652                 'grade'
653             );
655             if (!is_null($assessment->weight) and $assessment->weight != 1) {
656                 $o .= $this->output->container(
657                     get_string('weightinfo', 'workshop', $assessment->weight),
658                     'weight'
659                 );
660             }
661         }
663         $o .= $this->output->container_start('actions');
664         foreach ($assessment->actions as $action) {
665             $o .= $this->output->single_button($action->url, $action->label, $action->method);
666         }
667         $o .= $this->output->container_end(); // actions
669         $o .= $this->output->container_end(); // header
671         if (!is_null($assessment->form)) {
672             $o .= print_collapsible_region_start('assessment-form-wrapper', uniqid('workshop-assessment'),
673                     get_string('assessmentform', 'workshop'), '', false, true);
674             $o .= $this->output->container(self::moodleform($assessment->form), 'assessment-form');
675             $o .= print_collapsible_region_end(true);
676         }
678         $o .= $this->output->container_end(); // main wrapper
680         return $o;
681     }
683     /**
684      * Renders the assessment of an example submission
685      *
686      * @param workshop_example_assessment $assessment
687      * @return string HTML
688      */
689     protected function render_workshop_example_assessment(workshop_example_assessment $assessment) {
690         return $this->render_workshop_assessment($assessment);
691     }
693     /**
694      * Renders the reference assessment of an example submission
695      *
696      * @param workshop_example_reference_assessment $assessment
697      * @return string HTML
698      */
699     protected function render_workshop_example_reference_assessment(workshop_example_reference_assessment $assessment) {
700         return $this->render_workshop_assessment($assessment);
701     }
703     /**
704      * Renders a perpage selector for workshop listings
705      *
706      * The scripts using this have to define the $PAGE->url prior to calling this
707      * and deal with eventually submitted value themselves.
708      *
709      * @param int $current current value of the perpage parameter
710      * @return string HTML
711      */
712     public function perpage_selector($current=10) {
714         $options = array();
715         foreach (array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 1000) as $option) {
716             if ($option != $current) {
717                 $options[$option] = $option;
718             }
719         }
720         $select = new single_select($this->page->url, 'perpage', $options, '', array('' => get_string('showingperpagechange', 'mod_workshop')));
721         $select->label = get_string('showingperpage', 'mod_workshop', $current);
722         $select->method = 'post';
724         return $this->output->container($this->output->render($select), 'perpagewidget');
725     }
727     /**
728      * Renders the user's final grades
729      *
730      * @param workshop_final_grades $grades with the info about grades in the gradebook
731      * @return string HTML
732      */
733     protected function render_workshop_final_grades(workshop_final_grades $grades) {
735         $out = html_writer::start_tag('div', array('class' => 'finalgrades'));
737         if (!empty($grades->submissiongrade)) {
738             $cssclass = 'grade submissiongrade';
739             if ($grades->submissiongrade->hidden) {
740                 $cssclass .= ' hiddengrade';
741             }
742             $out .= html_writer::tag(
743                 'div',
744                 html_writer::tag('div', get_string('submissiongrade', 'mod_workshop'), array('class' => 'gradetype')) .
745                 html_writer::tag('div', $grades->submissiongrade->str_long_grade, array('class' => 'gradevalue')),
746                 array('class' => $cssclass)
747             );
748         }
750         if (!empty($grades->assessmentgrade)) {
751             $cssclass = 'grade assessmentgrade';
752             if ($grades->assessmentgrade->hidden) {
753                 $cssclass .= ' hiddengrade';
754             }
755             $out .= html_writer::tag(
756                 'div',
757                 html_writer::tag('div', get_string('gradinggrade', 'mod_workshop'), array('class' => 'gradetype')) .
758                 html_writer::tag('div', $grades->assessmentgrade->str_long_grade, array('class' => 'gradevalue')),
759                 array('class' => $cssclass)
760             );
761         }
763         $out .= html_writer::end_tag('div');
765         return $out;
766     }
768     ////////////////////////////////////////////////////////////////////////////
769     // Internal rendering helper methods
770     ////////////////////////////////////////////////////////////////////////////
772     /**
773      * Renders a list of files attached to the submission
774      *
775      * If format==html, then format a html string. If format==text, then format a text-only string.
776      * Otherwise, returns html for non-images and html to display the image inline.
777      *
778      * @param int $submissionid submission identifier
779      * @param string format the format of the returned string - html|text
780      * @return string formatted text to be echoed
781      */
782     protected function helper_submission_attachments($submissionid, $format = 'html') {
783         global $CFG;
784         require_once($CFG->libdir.'/filelib.php');
786         $fs     = get_file_storage();
787         $ctx    = $this->page->context;
788         $files  = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid);
790         $outputimgs     = '';   // images to be displayed inline
791         $outputfiles    = '';   // list of attachment files
793         foreach ($files as $file) {
794             if ($file->is_directory()) {
795                 continue;
796             }
798             $filepath   = $file->get_filepath();
799             $filename   = $file->get_filename();
800             $fileurl    = file_encode_url($CFG->wwwroot . '/pluginfile.php',
801                                 '/' . $ctx->id . '/mod_workshop/submission_attachment/' . $submissionid . $filepath . $filename, true);
802             $type       = $file->get_mimetype();
803             $image      = $this->output->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon'));
805             $linkhtml   = html_writer::link($fileurl, $image) . substr($filepath, 1) . html_writer::link($fileurl, $filename);
806             $linktxt    = "$filename [$fileurl]";
808             if ($format == 'html') {
809                 if (file_mimetype_in_typegroup($type, 'web_image')) {
810                     $preview     = html_writer::empty_tag('img', array('src' => $fileurl, 'alt' => '', 'class' => 'preview'));
811                     $preview     = html_writer::tag('a', $preview, array('href' => $fileurl));
812                     $outputimgs .= $this->output->container($preview);
814                 } else {
815                     $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type));
816                 }
818             } else if ($format == 'text') {
819                 $outputfiles .= $linktxt . PHP_EOL;
820             }
822             if (!empty($CFG->enableplagiarism)) {
823                 require_once($CFG->libdir.'/plagiarismlib.php');
824                 $outputfiles .= plagiarism_get_links(array('userid' => $file->get_userid(),
825                     'file' => $file,
826                     'cmid' => $this->page->cm->id,
827                     'course' => $this->page->course->id));
828             }
829         }
831         if ($format == 'html') {
832             if ($outputimgs) {
833                 $outputimgs = $this->output->container($outputimgs, 'images');
834             }
836             if ($outputfiles) {
837                 $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files'));
838             }
840             return $this->output->container($outputimgs . $outputfiles, 'attachments');
842         } else {
843             return $outputfiles;
844         }
845     }
847     /**
848      * Renders the tasks for the single phase in the user plan
849      *
850      * @param stdClass $tasks
851      * @return string html code
852      */
853     protected function helper_user_plan_tasks(array $tasks) {
854         $out = '';
855         foreach ($tasks as $taskcode => $task) {
856             $classes = '';
857             $icon = null;
858             if ($task->completed === true) {
859                 $classes .= ' completed';
860             } elseif ($task->completed === false) {
861                 $classes .= ' fail';
862             } elseif ($task->completed === 'info') {
863                 $classes .= ' info';
864             }
865             if (is_null($task->link)) {
866                 $title = $task->title;
867             } else {
868                 $title = html_writer::link($task->link, $task->title);
869             }
870             $title = $this->output->container($title, 'title');
871             $details = $this->output->container($task->details, 'details');
872             $out .= html_writer::tag('li', $title . $details, array('class' => $classes));
873         }
874         if ($out) {
875             $out = html_writer::tag('ul', $out, array('class' => 'tasks'));
876         }
877         return $out;
878     }
880     /**
881      * Renders a text with icons to sort by the given column
882      *
883      * This is intended for table headings.
884      *
885      * @param string $text    The heading text
886      * @param string $sortid  The column id used for sorting
887      * @param string $sortby  Currently sorted by (column id)
888      * @param string $sorthow Currently sorted how (ASC|DESC)
889      *
890      * @return string
891      */
892     protected function helper_sortable_heading($text, $sortid=null, $sortby=null, $sorthow=null) {
893         global $PAGE;
895         $out = html_writer::tag('span', $text, array('class'=>'text'));
897         if (!is_null($sortid)) {
898             if ($sortby !== $sortid or $sorthow !== 'ASC') {
899                 $url = new moodle_url($PAGE->url);
900                 $url->params(array('sortby' => $sortid, 'sorthow' => 'ASC'));
901                 $out .= $this->output->action_icon($url, new pix_icon('t/sort_asc', get_string('sortasc', 'workshop')),
902                     null, array('class' => 'iconsort sort asc'));
903             }
904             if ($sortby !== $sortid or $sorthow !== 'DESC') {
905                 $url = new moodle_url($PAGE->url);
906                 $url->params(array('sortby' => $sortid, 'sorthow' => 'DESC'));
907                 $out .= $this->output->action_icon($url, new pix_icon('t/sort_desc', get_string('sortdesc', 'workshop')),
908                     null, array('class' => 'iconsort sort desc'));
909             }
910         }
911         return $out;
914     /**
915      * @param stdClass $participant
916      * @param array $userinfo
917      * @return string
918      */
919     protected function helper_grading_report_participant(stdclass $participant, array $userinfo) {
920         $userid = $participant->userid;
921         $out  = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 35));
922         $out .= html_writer::tag('span', fullname($userinfo[$userid]));
924         return $out;
925     }
927     /**
928      * @param stdClass $participant
929      * @return string
930      */
931     protected function helper_grading_report_submission(stdclass $participant) {
932         global $CFG;
934         if (is_null($participant->submissionid)) {
935             $out = $this->output->container(get_string('nosubmissionfound', 'workshop'), 'info');
936         } else {
937             $url = new moodle_url('/mod/workshop/submission.php',
938                                   array('cmid' => $this->page->context->instanceid, 'id' => $participant->submissionid));
939             $out = html_writer::link($url, format_string($participant->submissiontitle), array('class'=>'title'));
940         }
942         return $out;
943     }
945     /**
946      * @todo Highlight the nulls
947      * @param stdClass|null $assessment
948      * @param bool $shownames
949      * @param string $separator between the grade and the reviewer/author
950      * @return string
951      */
952     protected function helper_grading_report_assessment($assessment, $shownames, array $userinfo, $separator) {
953         global $CFG;
955         if (is_null($assessment)) {
956             return get_string('nullgrade', 'workshop');
957         }
958         $a = new stdclass();
959         $a->grade = is_null($assessment->grade) ? get_string('nullgrade', 'workshop') : $assessment->grade;
960         $a->gradinggrade = is_null($assessment->gradinggrade) ? get_string('nullgrade', 'workshop') : $assessment->gradinggrade;
961         $a->weight = $assessment->weight;
962         // grrr the following logic should really be handled by a future language pack feature
963         if (is_null($assessment->gradinggradeover)) {
964             if ($a->weight == 1) {
965                 $grade = get_string('formatpeergrade', 'workshop', $a);
966             } else {
967                 $grade = get_string('formatpeergradeweighted', 'workshop', $a);
968             }
969         } else {
970             $a->gradinggradeover = $assessment->gradinggradeover;
971             if ($a->weight == 1) {
972                 $grade = get_string('formatpeergradeover', 'workshop', $a);
973             } else {
974                 $grade = get_string('formatpeergradeoverweighted', 'workshop', $a);
975             }
976         }
977         $url = new moodle_url('/mod/workshop/assessment.php',
978                               array('asid' => $assessment->assessmentid));
979         $grade = html_writer::link($url, $grade, array('class'=>'grade'));
981         if ($shownames) {
982             $userid = $assessment->userid;
983             $name   = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 16));
984             $name  .= html_writer::tag('span', fullname($userinfo[$userid]), array('class' => 'fullname'));
985             $name   = $separator . html_writer::tag('span', $name, array('class' => 'user'));
986         } else {
987             $name   = '';
988         }
990         return $this->output->container($grade . $name, 'assessmentdetails');
991     }
993     /**
994      * Formats the aggreagated grades
995      */
996     protected function helper_grading_report_grade($grade, $over=null) {
997         $a = new stdclass();
998         $a->grade = is_null($grade) ? get_string('nullgrade', 'workshop') : $grade;
999         if (is_null($over)) {
1000             $text = get_string('formataggregatedgrade', 'workshop', $a);
1001         } else {
1002             $a->over = is_null($over) ? get_string('nullgrade', 'workshop') : $over;
1003             $text = get_string('formataggregatedgradeover', 'workshop', $a);
1004         }
1005         return $text;
1006     }
1008     ////////////////////////////////////////////////////////////////////////////
1009     // Static helpers
1010     ////////////////////////////////////////////////////////////////////////////
1012     /**
1013      * Helper method dealing with the fact we can not just fetch the output of moodleforms
1014      *
1015      * @param moodleform $mform
1016      * @return string HTML
1017      */
1018     protected static function moodleform(moodleform $mform) {
1020         ob_start();
1021         $mform->display();
1022         $o = ob_get_contents();
1023         ob_end_clean();
1025         return $o;
1026     }
1028     /**
1029      * Helper function returning the n-th item of the array
1030      *
1031      * @param array $a
1032      * @param int   $n from 0 to m, where m is th number of items in the array
1033      * @return mixed the $n-th element of $a
1034      */
1035     protected static function array_nth(array $a, $n) {
1036         $keys = array_keys($a);
1037         if ($n < 0 or $n > count($keys) - 1) {
1038             return null;
1039         }
1040         $key = $keys[$n];
1041         return $a[$key];
1042     }
1044     /**
1045      * Tries to guess the fullname format set at the site
1046      *
1047      * @return string fl|lf
1048      */
1049     protected static function fullname_format() {
1050         $fake = new stdclass(); // fake user
1051         $fake->lastname = 'LLLL';
1052         $fake->firstname = 'FFFF';
1053         $fullname = get_string('fullnamedisplay', '', $fake);
1054         if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
1055             return 'lf';
1056         } else {
1057             return 'fl';
1058         }
1059     }