MDL-37635 Assign: Prevent errors when viewing a feedback for an assignment with no...
[moodle.git] / mod / assign / renderer.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * This file contains a renderer for the assignment class
19  *
20  * @package   mod_assign
21  * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->dirroot . '/mod/assign/locallib.php');
29 /**
30  * A custom renderer class that extends the plugin_renderer_base and is used by the assign module.
31  *
32  * @package mod_assign
33  * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
34  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class mod_assign_renderer extends plugin_renderer_base {
38     /**
39      * Rendering assignment files
40      *
41      * @param context $context
42      * @param int $userid
43      * @param string $filearea
44      * @param string $component
45      * @return string
46      */
47     public function assign_files(context $context, $userid, $filearea, $component) {
48         return $this->render(new assign_files($context, $userid, $filearea, $component));
49     }
51     /**
52      * Rendering assignment files
53      *
54      * @param assign_files $tree
55      * @return string
56      */
57     public function render_assign_files(assign_files $tree) {
58         $this->htmlid = 'assign_files_tree_'.uniqid();
59         $this->page->requires->js_init_call('M.mod_assign.init_tree', array(true, $this->htmlid));
60         $html = '<div id="'.$this->htmlid.'">';
61         $html .= $this->htmllize_tree($tree, $tree->dir);
62         $html .= '</div>';
64         if ($tree->portfolioform) {
65             $html .= $tree->portfolioform;
66         }
67         return $html;
68     }
70     /**
71      * Utility function to add a row of data to a table with 2 columns. Modified
72      * the table param and does not return a value
73      *
74      * @param html_table $table The table to append the row of data to
75      * @param string $first The first column text
76      * @param string $second The second column text
77      * @return void
78      */
79     private function add_table_row_tuple(html_table $table, $first, $second) {
80         $row = new html_table_row();
81         $cell1 = new html_table_cell($first);
82         $cell2 = new html_table_cell($second);
83         $row->cells = array($cell1, $cell2);
84         $table->data[] = $row;
85     }
87     /**
88      * Render a grading error notification
89      * @param assign_quickgrading_result $result The result to render
90      * @return string
91      */
92     public function render_assign_quickgrading_result(assign_quickgrading_result $result) {
93         $urlparams = array('id' => $result->coursemoduleid, 'action'=>'grading');
94         $url = new moodle_url('/mod/assign/view.php', $urlparams);
96         $o = '';
97         $o .= $this->output->heading(get_string('quickgradingresult', 'assign'), 4);
98         $o .= $this->output->notification($result->message);
99         $o .= $this->output->continue_button($url);
100         return $o;
101     }
103     /**
104      * Render the generic form
105      * @param assign_form $form The form to render
106      * @return string
107      */
108     public function render_assign_form(assign_form $form) {
109         $o = '';
110         if ($form->jsinitfunction) {
111             $this->page->requires->js_init_call($form->jsinitfunction, array());
112         }
113         $o .= $this->output->box_start('boxaligncenter ' . $form->classname);
114         $o .= $this->moodleform($form->form);
115         $o .= $this->output->box_end();
116         return $o;
117     }
119     /**
120      * Render the user summary
121      *
122      * @param assign_user_summary $summary The user summary to render
123      * @return string
124      */
125     public function render_assign_user_summary(assign_user_summary $summary) {
126         $o = '';
128         if (!$summary->user) {
129             return;
130         }
131         $o .= $this->output->container_start('usersummary');
132         $o .= $this->output->box_start('boxaligncenter usersummarysection');
133         if ($summary->blindmarking) {
134             $o .= get_string('hiddenuser', 'assign') . $summary->uniqueidforuser;
135         } else {
136             $o .= $this->output->user_picture($summary->user);
137             $o .= $this->output->spacer(array('width'=>30));
138             $urlparams = array('id' => $summary->user->id, 'course'=>$summary->courseid);
139             $url = new moodle_url('/user/view.php', $urlparams);
140             $o .= $this->output->action_link($url, fullname($summary->user, $summary->viewfullnames));
141         }
142         $o .= $this->output->box_end();
143         $o .= $this->output->container_end();
145         return $o;
146     }
148     /**
149      * Render the submit for grading page
150      *
151      * @param assign_submit_for_grading_page $page
152      * @return string
153      */
154     public function render_assign_submit_for_grading_page($page) {
155         $o = '';
157         $o .= $this->output->container_start('submitforgrading');
158         $o .= $this->output->heading(get_string('submitassignment', 'assign'), 3);
159         $o .= $this->output->spacer(array('height'=>30));
161         $cancelurl = new moodle_url('/mod/assign/view.php', array('id' => $page->coursemoduleid));
162         if (count($page->notifications)) {
163             // At least one of the submission plugins is not ready for submission.
165             $o .= $this->output->heading(get_string('submissionnotready', 'assign'), 4);
167             foreach ($page->notifications as $notification) {
168                 $o .= $this->output->notification($notification);
169             }
171             $o .= $this->output->continue_button($cancelurl);
172         } else {
173             // All submission plugins ready - show the confirmation form.
174             $o .= $this->moodleform($page->confirmform);
175         }
176         $o .= $this->output->container_end();
178         return $o;
179     }
181     /**
182      * Page is done - render the footer.
183      *
184      * @return void
185      */
186     public function render_footer() {
187         return $this->output->footer();
188     }
190     /**
191      * Render the header.
192      *
193      * @param assign_header $header
194      * @return string
195      */
196     public function render_assign_header(assign_header $header) {
197         $o = '';
199         if ($header->subpage) {
200             $this->page->navbar->add($header->subpage);
201         }
203         $this->page->set_title(get_string('pluginname', 'assign'));
204         $this->page->set_heading($header->assign->name);
206         $o .= $this->output->header();
207         if ($header->preface) {
208             $o .= $header->preface;
209         }
210         $heading = format_string($header->assign->name, false, array('context' => $header->context));
211         $o .= $this->output->heading($heading);
213         if ($header->showintro) {
214             $o .= $this->output->box_start('generalbox boxaligncenter', 'intro');
215             $o .= format_module_intro('assign', $header->assign, $header->coursemoduleid);
216             $o .= $this->output->box_end();
217         }
219         return $o;
220     }
222     /**
223      * Render a table containing the current status of the grading process.
224      *
225      * @param assign_grading_summary $summary
226      * @return string
227      */
228     public function render_assign_grading_summary(assign_grading_summary $summary) {
229         // Create a table for the data.
230         $o = '';
231         $o .= $this->output->container_start('gradingsummary');
232         $o .= $this->output->heading(get_string('gradingsummary', 'assign'), 3);
233         $o .= $this->output->box_start('boxaligncenter gradingsummarytable');
234         $t = new html_table();
236         // Status.
237         if ($summary->teamsubmission) {
238             $this->add_table_row_tuple($t, get_string('numberofteams', 'assign'),
239                                        $summary->participantcount);
240         } else {
241             $this->add_table_row_tuple($t, get_string('numberofparticipants', 'assign'),
242                                        $summary->participantcount);
243         }
245         // Drafts.
246         if ($summary->submissiondraftsenabled) {
247             $this->add_table_row_tuple($t, get_string('numberofdraftsubmissions', 'assign'),
248                                        $summary->submissiondraftscount);
249         }
251         // Submitted for grading.
252         if ($summary->submissionsenabled) {
253             $this->add_table_row_tuple($t, get_string('numberofsubmittedassignments', 'assign'),
254                                        $summary->submissionssubmittedcount);
255             if (!$summary->teamsubmission) {
256                 $this->add_table_row_tuple($t, get_string('numberofsubmissionsneedgrading', 'assign'),
257                                            $summary->submissionsneedgradingcount);
258             }
259         }
261         $time = time();
262         if ($summary->duedate) {
263             // Due date.
264             $duedate = $summary->duedate;
265             $this->add_table_row_tuple($t, get_string('duedate', 'assign'),
266                                        userdate($duedate));
268             // Time remaining.
269             $due = '';
270             if ($duedate - $time <= 0) {
271                 $due = get_string('assignmentisdue', 'assign');
272             } else {
273                 $due = format_time($duedate - $time);
274             }
275             $this->add_table_row_tuple($t, get_string('timeremaining', 'assign'), $due);
277             if ($duedate < $time) {
278                 $cutoffdate = $summary->cutoffdate;
279                 if ($cutoffdate) {
280                     if ($cutoffdate > $time) {
281                         $late = get_string('latesubmissionsaccepted', 'assign');
282                     } else {
283                         $late = get_string('nomoresubmissionsaccepted', 'assign');
284                     }
285                     $this->add_table_row_tuple($t, get_string('latesubmissions', 'assign'), $late);
286                 }
287             }
289         }
291         // All done - write the table.
292         $o .= html_writer::table($t);
293         $o .= $this->output->box_end();
295         // Link to the grading page.
296         $o .= $this->output->container_start('submissionlinks');
297         $urlparams = array('id' => $summary->coursemoduleid, 'action'=>'grading');
298         $url = new moodle_url('/mod/assign/view.php', $urlparams);
299         $o .= $this->output->action_link($url, get_string('viewgrading', 'assign'));
300         $o .= $this->output->container_end();
302         // Close the container and insert a spacer.
303         $o .= $this->output->container_end();
305         return $o;
306     }
308     /**
309      * Render a table containing all the current grades and feedback.
310      *
311      * @param assign_feedback_status $status
312      * @return string
313      */
314     public function render_assign_feedback_status(assign_feedback_status $status) {
315         global $DB, $CFG;
316         $o = '';
318         $o .= $this->output->container_start('feedback');
319         $o .= $this->output->heading(get_string('feedback', 'assign'), 3);
320         $o .= $this->output->box_start('boxaligncenter feedbacktable');
321         $t = new html_table();
323         // Grade.
324         if (isset($status->gradefordisplay)) {
325             $row = new html_table_row();
326             $cell1 = new html_table_cell(get_string('grade'));
327             $cell2 = new html_table_cell($status->gradefordisplay);
328             $row->cells = array($cell1, $cell2);
329             $t->data[] = $row;
331             // Grade date.
332             $row = new html_table_row();
333             $cell1 = new html_table_cell(get_string('gradedon', 'assign'));
334             $cell2 = new html_table_cell(userdate($status->gradeddate));
335             $row->cells = array($cell1, $cell2);
336             $t->data[] = $row;
337         }
339         if ($status->grader) {
340             // Grader.
341             $row = new html_table_row();
342             $cell1 = new html_table_cell(get_string('gradedby', 'assign'));
343             $userdescription = $this->output->user_picture($status->grader) .
344                                $this->output->spacer(array('width'=>30)) .
345                                fullname($status->grader);
346             $cell2 = new html_table_cell($userdescription);
347             $row->cells = array($cell1, $cell2);
348             $t->data[] = $row;
349         }
351         foreach ($status->feedbackplugins as $plugin) {
352             if ($plugin->is_enabled() &&
353                     $plugin->is_visible() &&
354                     $plugin->has_user_summary() &&
355                     !empty($status->grade) &&
356                     !$plugin->is_empty($status->grade)) {
358                 $row = new html_table_row();
359                 $cell1 = new html_table_cell($plugin->get_name());
360                 $displaymode = assign_feedback_plugin_feedback::SUMMARY;
361                 $pluginfeedback = new assign_feedback_plugin_feedback($plugin,
362                                                                       $status->grade,
363                                                                       $displaymode,
364                                                                       $status->coursemoduleid,
365                                                                       $status->returnaction,
366                                                                       $status->returnparams);
367                 $cell2 = new html_table_cell($this->render($pluginfeedback));
368                 $row->cells = array($cell1, $cell2);
369                 $t->data[] = $row;
370             }
371         }
373         $o .= html_writer::table($t);
374         $o .= $this->output->box_end();
376         $o .= $this->output->container_end();
377         return $o;
378     }
380     /**
381      * Render a table containing the current status of the submission.
382      *
383      * @param assign_submission_status $status
384      * @return string
385      */
386     public function render_assign_submission_status(assign_submission_status $status) {
387         $o = '';
388         $o .= $this->output->container_start('submissionstatustable');
389         $o .= $this->output->heading(get_string('submissionstatusheading', 'assign'), 3);
390         $time = time();
392         if ($status->allowsubmissionsfromdate &&
393                 $time <= $status->allowsubmissionsfromdate) {
394             $o .= $this->output->box_start('generalbox boxaligncenter submissionsalloweddates');
395             if ($status->alwaysshowdescription) {
396                 $date = userdate($status->allowsubmissionsfromdate);
397                 $o .= get_string('allowsubmissionsfromdatesummary', 'assign', $date);
398             } else {
399                 $date = userdate($status->allowsubmissionsfromdate);
400                 $o .= get_string('allowsubmissionsanddescriptionfromdatesummary', 'assign', $date);
401             }
402             $o .= $this->output->box_end();
403         }
404         $o .= $this->output->box_start('boxaligncenter submissionsummarytable');
406         $t = new html_table();
408         if ($status->teamsubmissionenabled) {
409             $row = new html_table_row();
410             $cell1 = new html_table_cell(get_string('submissionteam', 'assign'));
411             $group = $status->submissiongroup;
412             if ($group) {
413                 $cell2 = new html_table_cell(format_string($group->name, false, $status->context));
414             } else {
415                 $cell2 = new html_table_cell(get_string('defaultteam', 'assign'));
416             }
417             $row->cells = array($cell1, $cell2);
418             $t->data[] = $row;
419         }
421         $row = new html_table_row();
422         $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
423         if (!$status->teamsubmissionenabled) {
424             if ($status->submission) {
425                 $statusstr = get_string('submissionstatus_' . $status->submission->status, 'assign');
426                 $cell2 = new html_table_cell($statusstr);
427                 $cell2->attributes = array('class'=>'submissionstatus' . $status->submission->status);
428             } else {
429                 if (!$status->submissionsenabled) {
430                     $cell2 = new html_table_cell(get_string('noonlinesubmissions', 'assign'));
431                 } else {
432                     $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
433                 }
434             }
435             $row->cells = array($cell1, $cell2);
436             $t->data[] = $row;
437         } else {
438             $row = new html_table_row();
439             $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
440             if ($status->teamsubmission) {
441                 $teamstatus = $status->teamsubmission->status;
442                 $submissionsummary = get_string('submissionstatus_' . $teamstatus, 'assign');
443                 $groupid = 0;
444                 if ($status->submissiongroup) {
445                     $groupid = $status->submissiongroup->id;
446                 }
448                 $members = $status->submissiongroupmemberswhoneedtosubmit;
449                 $userslist = array();
450                 foreach ($members as $member) {
451                     $urlparams = array('id' => $member->id, 'course'=>$status->courseid);
452                     $url = new moodle_url('/user/view.php', $urlparams);
453                     if ($status->view == assign_submission_status::GRADER_VIEW && $status->blindmarking) {
454                         $userslist[] = $member->alias;
455                     } else {
456                         $fullname = fullname($member, $status->canviewfullnames);
457                         $userslist[] = $this->output->action_link($url, $fullname);
458                     }
459                 }
460                 if (count($userslist) > 0) {
461                     $userstr = join(', ', $userslist);
462                     $formatteduserstr = get_string('userswhoneedtosubmit', 'assign', $userstr);
463                     $submissionsummary .= $this->output->container($formatteduserstr);
464                 }
466                 $cell2 = new html_table_cell($submissionsummary);
467                 $cell2->attributes = array('class'=>'submissionstatus' . $status->teamsubmission->status);
468             } else {
469                 $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
470                 if (!$status->submissionsenabled) {
471                     $cell2 = new html_table_cell(get_string('noonlinesubmissions', 'assign'));
472                 } else {
473                     $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
474                 }
475             }
476             $row->cells = array($cell1, $cell2);
477             $t->data[] = $row;
478         }
480         // Is locked?
481         if ($status->locked) {
482             $row = new html_table_row();
483             $cell1 = new html_table_cell();
484             $cell2 = new html_table_cell(get_string('submissionslocked', 'assign'));
485             $cell2->attributes = array('class'=>'submissionlocked');
486             $row->cells = array($cell1, $cell2);
487             $t->data[] = $row;
488         }
490         // Grading status.
491         $row = new html_table_row();
492         $cell1 = new html_table_cell(get_string('gradingstatus', 'assign'));
494         if ($status->graded) {
495             $cell2 = new html_table_cell(get_string('graded', 'assign'));
496             $cell2->attributes = array('class'=>'submissiongraded');
497         } else {
498             $cell2 = new html_table_cell(get_string('notgraded', 'assign'));
499             $cell2->attributes = array('class'=>'submissionnotgraded');
500         }
501         $row->cells = array($cell1, $cell2);
502         $t->data[] = $row;
504         $duedate = $status->duedate;
505         if ($duedate > 0) {
506             // Due date.
507             $row = new html_table_row();
508             $cell1 = new html_table_cell(get_string('duedate', 'assign'));
509             $cell2 = new html_table_cell(userdate($duedate));
510             $row->cells = array($cell1, $cell2);
511             $t->data[] = $row;
513             if ($status->view == assign_submission_status::GRADER_VIEW) {
514                 if ($status->cutoffdate) {
515                     // Cut off date.
516                     $row = new html_table_row();
517                     $cell1 = new html_table_cell(get_string('cutoffdate', 'assign'));
518                     $cell2 = new html_table_cell(userdate($status->cutoffdate));
519                     $row->cells = array($cell1, $cell2);
520                     $t->data[] = $row;
521                 }
522             }
524             if ($status->extensionduedate) {
525                 // Extension date.
526                 $row = new html_table_row();
527                 $cell1 = new html_table_cell(get_string('extensionduedate', 'assign'));
528                 $cell2 = new html_table_cell(userdate($status->extensionduedate));
529                 $row->cells = array($cell1, $cell2);
530                 $t->data[] = $row;
531                 $duedate = $status->extensionduedate;
532             }
534             // Time remaining.
535             $row = new html_table_row();
536             $cell1 = new html_table_cell(get_string('timeremaining', 'assign'));
537             if ($duedate - $time <= 0) {
538                 if (!$status->submission ||
539                         $status->submission->status != ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
540                     if ($status->submissionsenabled) {
541                         $overduestr = get_string('overdue', 'assign', format_time($time - $duedate));
542                         $cell2 = new html_table_cell($overduestr);
543                         $cell2->attributes = array('class'=>'overdue');
544                     } else {
545                         $cell2 = new html_table_cell(get_string('duedatereached', 'assign'));
546                     }
547                 } else {
548                     if ($status->submission->timemodified > $duedate) {
549                         $latestr = get_string('submittedlate',
550                                               'assign',
551                                               format_time($status->submission->timemodified - $duedate));
552                         $cell2 = new html_table_cell($latestr);
553                         $cell2->attributes = array('class'=>'latesubmission');
554                     } else {
555                         $earlystr = get_string('submittedearly',
556                                                'assign',
557                                                format_time($status->submission->timemodified - $duedate));
558                         $cell2 = new html_table_cell($earlystr);
559                         $cell2->attributes = array('class'=>'earlysubmission');
560                     }
561                 }
562             } else {
563                 $cell2 = new html_table_cell(format_time($duedate - $time));
564             }
565             $row->cells = array($cell1, $cell2);
566             $t->data[] = $row;
567         }
569         // Show graders whether this submission is editable by students.
570         if ($status->view == assign_submission_status::GRADER_VIEW) {
571             $row = new html_table_row();
572             $cell1 = new html_table_cell(get_string('editingstatus', 'assign'));
573             if ($status->canedit) {
574                 $cell2 = new html_table_cell(get_string('submissioneditable', 'assign'));
575                 $cell2->attributes = array('class'=>'submissioneditable');
576             } else {
577                 $cell2 = new html_table_cell(get_string('submissionnoteditable', 'assign'));
578                 $cell2->attributes = array('class'=>'submissionnoteditable');
579             }
580             $row->cells = array($cell1, $cell2);
581             $t->data[] = $row;
582         }
584         // Grading criteria preview.
585         if (!empty($status->gradingcontrollerpreview)) {
586             $row = new html_table_row();
587             $cell1 = new html_table_cell(get_string('gradingmethodpreview', 'assign'));
588             $cell2 = new html_table_cell($status->gradingcontrollerpreview);
589             $row->cells = array($cell1, $cell2);
590             $t->data[] = $row;
591         }
593         // Last modified.
594         $submission = $status->teamsubmission ? $status->teamsubmission : $status->submission;
595         if ($submission) {
596             $row = new html_table_row();
597             $cell1 = new html_table_cell(get_string('timemodified', 'assign'));
598             $cell2 = new html_table_cell(userdate($submission->timemodified));
599             $row->cells = array($cell1, $cell2);
600             $t->data[] = $row;
602             foreach ($status->submissionplugins as $plugin) {
603                 $pluginshowsummary = !$plugin->is_empty($submission) || !$plugin->allow_submissions();
604                 if ($plugin->is_enabled() &&
605                     $plugin->is_visible() &&
606                     $plugin->has_user_summary() &&
607                     $pluginshowsummary) {
609                     $row = new html_table_row();
610                     $cell1 = new html_table_cell($plugin->get_name());
611                     $displaymode = assign_submission_plugin_submission::SUMMARY;
612                     $pluginsubmission = new assign_submission_plugin_submission($plugin,
613                                                                                 $submission,
614                                                                                 $displaymode,
615                                                                                 $status->coursemoduleid,
616                                                                                 $status->returnaction,
617                                                                                 $status->returnparams);
618                     $cell2 = new html_table_cell($this->render($pluginsubmission));
619                     $row->cells = array($cell1, $cell2);
620                     $t->data[] = $row;
621                 }
622             }
623         }
625         $o .= html_writer::table($t);
626         $o .= $this->output->box_end();
628         // Links.
629         if ($status->view == assign_submission_status::STUDENT_VIEW) {
630             if ($status->canedit) {
631                 if (!$submission) {
632                     $urlparams = array('id' => $status->coursemoduleid, 'action' => 'editsubmission');
633                     $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
634                                                        get_string('addsubmission', 'assign'), 'get');
635                 } else {
636                     $urlparams = array('id' => $status->coursemoduleid, 'action' => 'editsubmission');
637                     $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
638                                                        get_string('editsubmission', 'assign'), 'get');
639                 }
640             }
642             if ($status->cansubmit) {
643                 $urlparams = array('id' => $status->coursemoduleid, 'action'=>'submit');
644                 $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
645                                                    get_string('submitassignment', 'assign'), 'get');
646                 $o .= $this->output->box_start('boxaligncenter submithelp');
647                 $o .= get_string('submitassignment_help', 'assign');
648                 $o .= $this->output->box_end();
649             }
650         }
652         $o .= $this->output->container_end();
653         return $o;
654     }
656     /**
657      * Render a submission plugin submission
658      *
659      * @param assign_submission_plugin_submission $submissionplugin
660      * @return string
661      */
662     public function render_assign_submission_plugin_submission(assign_submission_plugin_submission $submissionplugin) {
663         $o = '';
665         if ($submissionplugin->view == assign_submission_plugin_submission::SUMMARY) {
666             $showviewlink = false;
667             $summary = $submissionplugin->plugin->view_summary($submissionplugin->submission,
668                                                                $showviewlink);
670             $classsuffix = $submissionplugin->plugin->get_subtype() .
671                            '_' .
672                            $submissionplugin->plugin->get_type() .
673                            '_' .
674                            $submissionplugin->submission->id;
676             $o .= $this->output->box_start('boxaligncenter plugincontentsummary summary_' . $classsuffix);
678             $link = '';
679             if ($showviewlink) {
680                 $previewstr = get_string('viewsubmission', 'assign');
681                 $icon = $this->output->pix_icon('t/preview', $previewstr);
683                 $expandstr = get_string('viewfull', 'assign');
684                 $options = array('class'=>'expandsummaryicon expand_' . $classsuffix);
685                 $o .= $this->output->pix_icon('t/switch_plus', $expandstr, null, $options);
687                 $jsparams = array($submissionplugin->plugin->get_subtype(),
688                                   $submissionplugin->plugin->get_type(),
689                                   $submissionplugin->submission->id);
691                 $this->page->requires->js_init_call('M.mod_assign.init_plugin_summary', $jsparams);
693                 $action = 'viewplugin' . $submissionplugin->plugin->get_subtype();
694                 $returnparams = http_build_query($submissionplugin->returnparams);
695                 $link .= '<noscript>';
696                 $urlparams = array('id' => $submissionplugin->coursemoduleid,
697                                    'sid'=>$submissionplugin->submission->id,
698                                    'plugin'=>$submissionplugin->plugin->get_type(),
699                                    'action'=>$action,
700                                    'returnaction'=>$submissionplugin->returnaction,
701                                    'returnparams'=>$returnparams);
702                 $url = new moodle_url('/mod/assign/view.php', $urlparams);
703                 $link .= $this->output->action_link($url, $icon);
704                 $link .= '</noscript>';
706                 $link .= $this->output->spacer(array('width'=>15));
707             }
709             $o .= $link . $summary;
710             $o .= $this->output->box_end();
711             if ($showviewlink) {
712                 $o .= $this->output->box_start('boxaligncenter hidefull full_' . $classsuffix);
713                 $classes = 'expandsummaryicon contract_' . $classsuffix;
714                 $o .= $this->output->pix_icon('t/switch_minus',
715                                               get_string('viewsummary', 'assign'),
716                                               null,
717                                               array('class'=>$classes));
718                 $o .= $submissionplugin->plugin->view($submissionplugin->submission);
719                 $o .= $this->output->box_end();
720             }
721         } else if ($submissionplugin->view == assign_submission_plugin_submission::FULL) {
722             $o .= $this->output->box_start('boxaligncenter submissionfull');
723             $o .= $submissionplugin->plugin->view($submissionplugin->submission);
724             $o .= $this->output->box_end();
725         }
727         return $o;
728     }
730     /**
731      * Render the grading table.
732      *
733      * @param assign_grading_table $table
734      * @return string
735      */
736     public function render_assign_grading_table(assign_grading_table $table) {
737         $o = '';
738         $o .= $this->output->box_start('boxaligncenter gradingtable');
740         $this->page->requires->js_init_call('M.mod_assign.init_grading_table', array());
741         $this->page->requires->string_for_js('nousersselected', 'assign');
742         $this->page->requires->string_for_js('batchoperationconfirmgrantextension', 'assign');
743         $this->page->requires->string_for_js('batchoperationconfirmlock', 'assign');
744         $this->page->requires->string_for_js('batchoperationconfirmreverttodraft', 'assign');
745         $this->page->requires->string_for_js('batchoperationconfirmunlock', 'assign');
746         $this->page->requires->string_for_js('editaction', 'assign');
747         foreach ($table->plugingradingbatchoperations as $plugin => $operations) {
748             foreach ($operations as $operation => $description) {
749                 $this->page->requires->string_for_js('batchoperationconfirm' . $operation,
750                                                      'assignfeedback_' . $plugin);
751             }
752         }
753         $o .= $this->flexible_table($table, $table->get_rows_per_page(), true);
754         $o .= $this->output->box_end();
756         return $o;
757     }
759     /**
760      * Render a feedback plugin feedback
761      *
762      * @param assign_feedback_plugin_feedback $feedbackplugin
763      * @return string
764      */
765     public function render_assign_feedback_plugin_feedback(assign_feedback_plugin_feedback $feedbackplugin) {
766         $o = '';
768         if ($feedbackplugin->view == assign_feedback_plugin_feedback::SUMMARY) {
769             $showviewlink = false;
770             $summary = $feedbackplugin->plugin->view_summary($feedbackplugin->grade, $showviewlink);
772             $classsuffix = $feedbackplugin->plugin->get_subtype() .
773                            '_' .
774                            $feedbackplugin->plugin->get_type() .
775                            '_' .
776                            $feedbackplugin->grade->id;
777             $o .= $this->output->box_start('boxaligncenter plugincontentsummary summary_' . $classsuffix);
779             $link = '';
780             if ($showviewlink) {
781                 $previewstr = get_string('viewfeedback', 'assign');
782                 $icon = $this->output->pix_icon('t/preview', $previewstr);
784                 $expandstr = get_string('viewfull', 'assign');
785                 $options = array('class'=>'expandsummaryicon expand_' . $classsuffix);
786                 $o .= $this->output->pix_icon('t/switch_plus', $expandstr, null, $options);
788                 $jsparams = array($feedbackplugin->plugin->get_subtype(),
789                                   $feedbackplugin->plugin->get_type(),
790                                   $feedbackplugin->grade->id);
791                 $this->page->requires->js_init_call('M.mod_assign.init_plugin_summary', $jsparams);
793                 $urlparams = array('id' => $feedbackplugin->coursemoduleid,
794                                    'gid'=>$feedbackplugin->grade->id,
795                                    'plugin'=>$feedbackplugin->plugin->get_type(),
796                                    'action'=>'viewplugin' . $feedbackplugin->plugin->get_subtype(),
797                                    'returnaction'=>$feedbackplugin->returnaction,
798                                    'returnparams'=>http_build_query($feedbackplugin->returnparams));
799                 $url = new moodle_url('/mod/assign/view.php', $urlparams);
800                 $link .= '<noscript>';
801                 $link .= $this->output->action_link($url, $icon);
802                 $link .= '</noscript>';
804                 $link .= $this->output->spacer(array('width'=>15));
805             }
807             $o .= $link . $summary;
808             $o .= $this->output->box_end();
809             if ($showviewlink) {
810                 $o .= $this->output->box_start('boxaligncenter hidefull full_' . $classsuffix);
811                 $classes = 'expandsummaryicon contract_' . $classsuffix;
812                 $o .= $this->output->pix_icon('t/switch_minus',
813                                               get_string('viewsummary', 'assign'),
814                                               null,
815                                               array('class'=>$classes));
816                 $o .= $feedbackplugin->plugin->view($feedbackplugin->grade);
817                 $o .= $this->output->box_end();
818             }
819         } else if ($feedbackplugin->view == assign_feedback_plugin_feedback::FULL) {
820             $o .= $this->output->box_start('boxaligncenter feedbackfull');
821             $o .= $feedbackplugin->plugin->view($feedbackplugin->grade);
822             $o .= $this->output->box_end();
823         }
825         return $o;
826     }
828     /**
829      * Render a course index summary
830      *
831      * @param assign_course_index_summary $indexsummary
832      * @return string
833      */
834     public function render_assign_course_index_summary(assign_course_index_summary $indexsummary) {
835         $o = '';
837         $strplural = get_string('modulenameplural', 'assign');
838         $strsectionname  = $indexsummary->courseformatname;
839         $strduedate = get_string('duedate', 'assign');
840         $strsubmission = get_string('submission', 'assign');
841         $strgrade = get_string('grade');
843         $table = new html_table();
844         if ($indexsummary->usesections) {
845             $table->head  = array ($strsectionname, $strplural, $strduedate, $strsubmission, $strgrade);
846             $table->align = array ('left', 'left', 'center', 'right', 'right');
847         } else {
848             $table->head  = array ($strplural, $strduedate, $strsubmission, $strgrade);
849             $table->align = array ('left', 'left', 'center', 'right');
850         }
851         $table->data = array();
853         $currentsection = '';
854         foreach ($indexsummary->assignments as $info) {
855             $params = array('id' => $info['cmid']);
856             $link = html_writer::link(new moodle_url('/mod/assign/view.php', $params),
857                                       $info['cmname']);
858             $due = $info['timedue'] ? userdate($info['timedue']) : '-';
860             $printsection = '';
861             if ($indexsummary->usesections) {
862                 if ($info['sectionname'] !== $currentsection) {
863                     if ($info['sectionname']) {
864                         $printsection = $info['sectionname'];
865                     }
866                     if ($currentsection !== '') {
867                         $table->data[] = 'hr';
868                     }
869                     $currentsection = $info['sectionname'];
870                 }
871             }
873             if ($indexsummary->usesections) {
874                 $row = array($printsection, $link, $due, $info['submissioninfo'], $info['gradeinfo']);
875             } else {
876                 $row = array($link, $due, $info['submissioninfo'], $info['gradeinfo']);
877             }
878             $table->data[] = $row;
879         }
881         $o .= html_writer::table($table);
883         return $o;
884     }
888     /**
889      * Internal function - creates htmls structure suitable for YUI tree.
890      *
891      * @param assign_files $tree
892      * @param array $dir
893      * @return string
894      */
895     protected function htmllize_tree(assign_files $tree, $dir) {
896         global $CFG;
897         $yuiconfig = array();
898         $yuiconfig['type'] = 'html';
900         if (empty($dir['subdirs']) and empty($dir['files'])) {
901             return '';
902         }
904         $result = '<ul>';
905         foreach ($dir['subdirs'] as $subdir) {
906             $image = $this->output->pix_icon(file_folder_icon(),
907                                              $subdir['dirname'],
908                                              'moodle',
909                                              array('class'=>'icon'));
910             $result .= '<li yuiConfig=\'' . json_encode($yuiconfig) . '\'>' .
911                        '<div>' . $image . ' ' . s($subdir['dirname']) . '</div> ' .
912                        $this->htmllize_tree($tree, $subdir) .
913                        '</li>';
914         }
916         foreach ($dir['files'] as $file) {
917             $filename = $file->get_filename();
918             if ($CFG->enableplagiarism) {
919                 require_once($CFG->libdir.'/plagiarismlib.php');
920                 $plagiarismlinks = plagiarism_get_links(array('userid'=>$file->get_userid(),
921                                                              'file'=>$file,
922                                                              'cmid'=>$tree->cm->id,
923                                                              'course'=>$tree->course));
924             } else {
925                 $plagiarismlinks = '';
926             }
927             $image = $this->output->pix_icon(file_file_icon($file),
928                                              $filename,
929                                              'moodle',
930                                              array('class'=>'icon'));
931             $result .= '<li yuiConfig=\'' . json_encode($yuiconfig) . '\'>' .
932                        '<div>' . $image . ' ' .
933                                  $file->fileurl . ' ' .
934                                  $plagiarismlinks .
935                                  $file->portfoliobutton . '</div>' .
936                        '</li>';
937         }
939         $result .= '</ul>';
941         return $result;
942     }
944     /**
945      * Helper method dealing with the fact we can not just fetch the output of flexible_table
946      *
947      * @param flexible_table $table The table to render
948      * @param int $rowsperpage How many assignments to render in a page
949      * @param bool $displaylinks - Whether to render links in the table
950      *                             (e.g. downloads would not enable this)
951      * @return string HTML
952      */
953     protected function flexible_table(flexible_table $table, $rowsperpage, $displaylinks) {
955         $o = '';
956         ob_start();
957         $table->out($rowsperpage, $displaylinks);
958         $o = ob_get_contents();
959         ob_end_clean();
961         return $o;
962     }
964     /**
965      * Helper method dealing with the fact we can not just fetch the output of moodleforms
966      *
967      * @param moodleform $mform
968      * @return string HTML
969      */
970     protected function moodleform(moodleform $mform) {
972         $o = '';
973         ob_start();
974         $mform->display();
975         $o = ob_get_contents();
976         ob_end_clean();
978         return $o;
979     }