22b24191bb7345d07395f03a2309cbb87693c451
[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) {
501         $o  = '';    // output HTML code
502         $o .= $this->output->container_start('feedback feedbackforauthor');
503         $o .= $this->output->container_start('header');
504         $o .= $this->output->heading(get_string('feedbackby', 'workshop', s(fullname($feedback->get_provider()))), 3, 'title');
506         $userpic = $this->output->user_picture($feedback->get_provider(), array('courseid' => $this->page->course->id, 'size' => 32));
507         $o .= $this->output->container($userpic, 'picture');
508         $o .= $this->output->container_end(); // end of header
510         $content = format_text($feedback->get_content(), $feedback->get_format(), array('overflowdiv' => true));
511         $o .= $this->output->container($content, 'content');
513         $o .= $this->output->container_end(); // end of submission-full
515         return $o;
516     }
518     ////////////////////////////////////////////////////////////////////////////
519     // Internal rendering helper methods
520     ////////////////////////////////////////////////////////////////////////////
522     /**
523      * Renders a list of files attached to the submission
524      *
525      * If format==html, then format a html string. If format==text, then format a text-only string.
526      * Otherwise, returns html for non-images and html to display the image inline.
527      *
528      * @param int $submissionid submission identifier
529      * @param string format the format of the returned string - html|text
530      * @return string formatted text to be echoed
531      */
532     protected function helper_submission_attachments($submissionid, $format = 'html') {
533         global $CFG;
534         require_once($CFG->libdir.'/filelib.php');
536         $fs     = get_file_storage();
537         $ctx    = $this->page->context;
538         $files  = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid);
540         $outputimgs     = '';   // images to be displayed inline
541         $outputfiles    = '';   // list of attachment files
543         foreach ($files as $file) {
544             if ($file->is_directory()) {
545                 continue;
546             }
548             $filepath   = $file->get_filepath();
549             $filename   = $file->get_filename();
550             $fileurl    = file_encode_url($CFG->wwwroot . '/pluginfile.php',
551                                 '/' . $ctx->id . '/mod_workshop/submission_attachment/' . $submissionid . $filepath . $filename, true);
552             $type       = $file->get_mimetype();
553             $type       = mimeinfo_from_type('type', $type);
554             $image      = html_writer::empty_tag('img', array('src'=>$this->output->pix_url(file_mimetype_icon($type)), 'alt'=>$type, 'class'=>'icon'));
556             $linkhtml   = html_writer::link($fileurl, $image) . substr($filepath, 1) . html_writer::link($fileurl, $filename);
557             $linktxt    = "$filename [$fileurl]";
559             if ($format == 'html') {
560                 if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) {
561                     $preview     = html_writer::empty_tag('img', array('src' => $fileurl, 'alt' => '', 'class' => 'preview'));
562                     $preview     = html_writer::tag('a', $preview, array('href' => $fileurl));
563                     $outputimgs .= $this->output->container($preview);
565                 } else {
566                     $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type));
567                 }
569             } else if ($format == 'text') {
570                 $outputfiles .= $linktxt . PHP_EOL;
571             }
572         }
574         if ($format == 'html') {
575             if ($outputimgs) {
576                 $outputimgs = $this->output->container($outputimgs, 'images');
577             }
579             if ($outputfiles) {
580                 $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files'));
581             }
583             return $this->output->container($outputimgs . $outputfiles, 'attachments');
585         } else {
586             return $outputfiles;
587         }
588     }
590     /**
591      * Renders the tasks for the single phase in the user plan
592      *
593      * @param stdClass $tasks
594      * @return string html code
595      */
596     protected function helper_user_plan_tasks(array $tasks) {
597         $out = '';
598         foreach ($tasks as $taskcode => $task) {
599             $classes = '';
600             $icon = null;
601             if ($task->completed === true) {
602                 $classes .= ' completed';
603             } elseif ($task->completed === false) {
604                 $classes .= ' fail';
605             } elseif ($task->completed === 'info') {
606                 $classes .= ' info';
607             }
608             if (is_null($task->link)) {
609                 $title = $task->title;
610             } else {
611                 $title = html_writer::link($task->link, $task->title);
612             }
613             $title = $this->output->container($title, 'title');
614             $details = $this->output->container($task->details, 'details');
615             $out .= html_writer::tag('li', $title . $details, array('class' => $classes));
616         }
617         if ($out) {
618             $out = html_writer::tag('ul', $out, array('class' => 'tasks'));
619         }
620         return $out;
621     }
623     /**
624      * Renders a text with icons to sort by the given column
625      *
626      * This is intended for table headings.
627      *
628      * @param string $text    The heading text
629      * @param string $sortid  The column id used for sorting
630      * @param string $sortby  Currently sorted by (column id)
631      * @param string $sorthow Currently sorted how (ASC|DESC)
632      *
633      * @return string
634      */
635     protected function helper_sortable_heading($text, $sortid=null, $sortby=null, $sorthow=null) {
636         global $PAGE;
638         $out = html_writer::tag('span', $text, array('class'=>'text'));
640         if (!is_null($sortid)) {
641             if ($sortby !== $sortid or $sorthow !== 'ASC') {
642                 $url = new moodle_url($PAGE->url);
643                 $url->params(array('sortby' => $sortid, 'sorthow' => 'ASC'));
644                 $out .= $this->output->action_icon($url, new pix_icon('t/up', get_string('sortasc', 'workshop')), null, array('class' => 'sort asc'));
645             }
646             if ($sortby !== $sortid or $sorthow !== 'DESC') {
647                 $url = new moodle_url($PAGE->url);
648                 $url->params(array('sortby' => $sortid, 'sorthow' => 'DESC'));
649                 $out .= $this->output->action_icon($url, new pix_icon('t/down', get_string('sortdesc', 'workshop')), null, array('class' => 'sort desc'));
650             }
651         }
652         return $out;
655     /**
656      * @param stdClass $participant
657      * @param array $userinfo
658      * @return string
659      */
660     protected function helper_grading_report_participant(stdclass $participant, array $userinfo) {
661         $userid = $participant->userid;
662         $out  = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 35));
663         $out .= html_writer::tag('span', fullname($userinfo[$userid]));
665         return $out;
666     }
668     /**
669      * @param stdClass $participant
670      * @return string
671      */
672     protected function helper_grading_report_submission(stdclass $participant) {
673         global $CFG;
675         if (is_null($participant->submissionid)) {
676             $out = $this->output->container(get_string('nosubmissionfound', 'workshop'), 'info');
677         } else {
678             $url = new moodle_url('/mod/workshop/submission.php',
679                                   array('cmid' => $this->page->context->instanceid, 'id' => $participant->submissionid));
680             $out = html_writer::link($url, format_string($participant->submissiontitle), array('class'=>'title'));
681         }
683         return $out;
684     }
686     /**
687      * @todo Highlight the nulls
688      * @param stdClass|null $assessment
689      * @param bool $shownames
690      * @param string $separator between the grade and the reviewer/author
691      * @return string
692      */
693     protected function helper_grading_report_assessment($assessment, $shownames, array $userinfo, $separator) {
694         global $CFG;
696         if (is_null($assessment)) {
697             return get_string('nullgrade', 'workshop');
698         }
699         $a = new stdclass();
700         $a->grade = is_null($assessment->grade) ? get_string('nullgrade', 'workshop') : $assessment->grade;
701         $a->gradinggrade = is_null($assessment->gradinggrade) ? get_string('nullgrade', 'workshop') : $assessment->gradinggrade;
702         $a->weight = $assessment->weight;
703         // grrr the following logic should really be handled by a future language pack feature
704         if (is_null($assessment->gradinggradeover)) {
705             if ($a->weight == 1) {
706                 $grade = get_string('formatpeergrade', 'workshop', $a);
707             } else {
708                 $grade = get_string('formatpeergradeweighted', 'workshop', $a);
709             }
710         } else {
711             $a->gradinggradeover = $assessment->gradinggradeover;
712             if ($a->weight == 1) {
713                 $grade = get_string('formatpeergradeover', 'workshop', $a);
714             } else {
715                 $grade = get_string('formatpeergradeoverweighted', 'workshop', $a);
716             }
717         }
718         $url = new moodle_url('/mod/workshop/assessment.php',
719                               array('asid' => $assessment->assessmentid));
720         $grade = html_writer::link($url, $grade, array('class'=>'grade'));
722         if ($shownames) {
723             $userid = $assessment->userid;
724             $name   = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 16));
725             $name  .= html_writer::tag('span', fullname($userinfo[$userid]), array('class' => 'fullname'));
726             $name   = $separator . html_writer::tag('span', $name, array('class' => 'user'));
727         } else {
728             $name   = '';
729         }
731         return $this->output->container($grade . $name, 'assessmentdetails');
732     }
734     /**
735      * Formats the aggreagated grades
736      */
737     protected function helper_grading_report_grade($grade, $over=null) {
738         $a = new stdclass();
739         $a->grade = is_null($grade) ? get_string('nullgrade', 'workshop') : $grade;
740         if (is_null($over)) {
741             $text = get_string('formataggregatedgrade', 'workshop', $a);
742         } else {
743             $a->over = is_null($over) ? get_string('nullgrade', 'workshop') : $over;
744             $text = get_string('formataggregatedgradeover', 'workshop', $a);
745         }
746         return $text;
747     }
749     ////////////////////////////////////////////////////////////////////////////
750     // Static helpers
751     ////////////////////////////////////////////////////////////////////////////
753     /**
754      * Helper function returning the n-th item of the array
755      *
756      * @param array $a
757      * @param int   $n from 0 to m, where m is th number of items in the array
758      * @return mixed the $n-th element of $a
759      */
760     protected static function array_nth(array $a, $n) {
761         $keys = array_keys($a);
762         if ($n < 0 or $n > count($keys) - 1) {
763             return null;
764         }
765         $key = $keys[$n];
766         return $a[$key];
767     }
769     /**
770      * Tries to guess the fullname format set at the site
771      *
772      * @return string fl|lf
773      */
774     protected static function fullname_format() {
775         $fake = new stdclass(); // fake user
776         $fake->lastname = 'LLLL';
777         $fake->firstname = 'FFFF';
778         $fullname = get_string('fullnamedisplay', '', $fake);
779         if (strpos($fullname, 'LLLL') < strpos($fullname, 'FFFF')) {
780             return 'lf';
781         } else {
782             return 'fl';
783         }
784     }