MDL-34793 mod_assign Fix rendering error that causes student view to break in some...
[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 /** Include locallib.php */
28 require_once($CFG->dirroot . '/mod/assign/locallib.php');
31 /**
32  * A custom renderer class that extends the plugin_renderer_base and is used by the assign module.
33  *
34  * @package mod_assign
35  * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
36  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  */
38 class mod_assign_renderer extends plugin_renderer_base {
40     /**
41      * rendering assignment files
42      *
43      * @param context $context
44      * @param int $userid
45      * @param string $filearea
46      * @param string $component
47      * @return string
48      */
49     public function assign_files(context $context, $userid, $filearea, $component) {
50         return $this->render(new assign_files($context, $userid, $filearea, $component));
51     }
53     /**
54      * rendering assignment files
55      *
56      * @param assign_files $tree
57      * @return string
58      */
59     public function render_assign_files(assign_files $tree) {
60         $this->htmlid = 'assign_files_tree_'.uniqid();
61         $this->page->requires->js_init_call('M.mod_assign.init_tree', array(true, $this->htmlid));
62         $html = '<div id="'.$this->htmlid.'">';
63         $html .= $this->htmllize_tree($tree, $tree->dir);
64         $html .= '</div>';
66         if ($tree->portfolioform) {
67             $html .= $tree->portfolioform;
68         }
69         return $html;
70     }
72     /**
73      * Utility function to add a row of data to a table with 2 columns. Modified
74      * the table param and does not return a value
75      *
76      * @param html_table $table The table to append the row of data to
77      * @param string $first The first column text
78      * @param string $second The second column text
79      * @return void
80      */
81     private function add_table_row_tuple(html_table $table, $first, $second) {
82         $row = new html_table_row();
83         $cell1 = new html_table_cell($first);
84         $cell2 = new html_table_cell($second);
85         $row->cells = array($cell1, $cell2);
86         $table->data[] = $row;
87     }
89     /**
90      * Render a grading error notification
91      * @param assign_quickgrading_result $result The result to render
92      * @return string
93      */
94     public function render_assign_quickgrading_result(assign_quickgrading_result $result) {
95         $url = new moodle_url('/mod/assign/view.php', array('id' => $result->coursemoduleid, 'action'=>'grading'));
97         $o = '';
98         $o .= $this->output->heading(get_string('quickgradingresult', 'assign'), 4);
99         $o .= $this->output->notification($result->message);
100         $o .= $this->output->continue_button($url);
101         return $o;
102     }
104     /**
105      * Render the generic form
106      * @param assign_form $form The form to render
107      * @return string
108      */
109     public function render_assign_form(assign_form $form) {
110         $o = '';
111         if ($form->jsinitfunction) {
112             $this->page->requires->js_init_call($form->jsinitfunction, array());
113         }
114         $o .= $this->output->box_start('boxaligncenter ' . $form->classname);
115         $o .= $this->moodleform($form->form);
116         $o .= $this->output->box_end();
117         return $o;
118     }
120     /**
121      * Render the user summary
122      *
123      * @param assign_user_summary $summary The user summary to render
124      * @return string
125      */
126     public function render_assign_user_summary(assign_user_summary $summary) {
127         $o = '';
129         if (!$summary->user) {
130             return;
131         }
132         $o .= $this->output->container_start('usersummary');
133         $o .= $this->output->box_start('boxaligncenter usersummarysection');
134         $o .= $this->output->user_picture($summary->user);
135         $o .= $this->output->spacer(array('width'=>30));
136         $o .= $this->output->action_link(new moodle_url('/user/view.php',
137                                                         array('id' => $summary->user->id,
138                                                               'course'=>$summary->courseid)),
139                                                               fullname($summary->user, $summary->viewfullnames));
140         $o .= $this->output->box_end();
141         $o .= $this->output->container_end();
143         return $o;
144     }
146     /**
147      * Render the submit for grading page
148      *
149      * @param assign_submit_for_grading_page $page
150      * @return string
151      */
152     public function render_assign_submit_for_grading_page($page) {
153         $o = '';
155         $o .= $this->output->container_start('submitforgrading');
156         $o .= $this->output->heading(get_string('submitassignment', 'assign'), 3);
157         $o .= $this->output->spacer(array('height'=>30));
159         $cancelurl = new moodle_url('/mod/assign/view.php', array('id' => $page->coursemoduleid));
160         if (count($page->notifications)) {
161             // At least one of the submission plugins is not ready for submission
163             $o .= $this->output->heading(get_string('submissionnotready', 'assign'), 4);
165             foreach ($page->notifications as $notification) {
166                 $o .= $this->output->notification($notification);
167             }
169             $o .= $this->output->continue_button($cancelurl);
170         } else {
171             // All submission plugins ready - show the confirmation form (may contain submission statement)
172             $o .= $this->moodleform($page->confirmform);
173         }
174         $o .= $this->output->container_end();
177         return $o;
178     }
180     /**
181      * Page is done - render the footer
182      *
183      * @return void
184      */
185     public function render_footer() {
186         return $this->output->footer();
187     }
189     /**
190      * render the header
191      *
192      * @param assign_header $header
193      * @return string
194      */
195     public function render_assign_header(assign_header $header) {
196         $o = '';
198         if ($header->subpage) {
199             $this->page->navbar->add($header->subpage);
200         }
202         $this->page->set_title(get_string('pluginname', 'assign'));
203         $this->page->set_heading($header->assign->name);
205         $o .= $this->output->header();
206         if ($header->preface) {
207             $o .= $header->preface;
208         }
209         $o .= $this->output->heading(format_string($header->assign->name,false, array('context' => $header->context)));
211         if ($header->showintro) {
212             $o .= $this->output->box_start('generalbox boxaligncenter', 'intro');
213             $o .= format_module_intro('assign', $header->assign, $header->coursemoduleid);
214             $o .= $this->output->box_end();
215         }
217         return $o;
218     }
220     /**
221      * render a table containing the current status of the grading process
222      *
223      * @param assign_grading_summary $summary
224      * @return string
225      */
226     public function render_assign_grading_summary(assign_grading_summary $summary) {
227         // create a table for the data
228         $o = '';
229         $o .= $this->output->container_start('gradingsummary');
230         $o .= $this->output->heading(get_string('gradingsummary', 'assign'), 3);
231         $o .= $this->output->box_start('boxaligncenter gradingsummarytable');
232         $t = new html_table();
234         // status
235         $this->add_table_row_tuple($t, get_string('numberofparticipants', 'assign'),
236                                    $summary->participantcount);
238         // drafts
239         if ($summary->submissiondraftsenabled) {
240             $this->add_table_row_tuple($t, get_string('numberofdraftsubmissions', 'assign'),
241                                        $summary->submissiondraftscount);
242        }
244         // submitted for grading
245         if ($summary->submissionsenabled) {
246             $this->add_table_row_tuple($t, get_string('numberofsubmittedassignments', 'assign'),
247                                        $summary->submissionssubmittedcount);
248             $this->add_table_row_tuple($t, get_string('numberofsubmissionsneedgrading', 'assign'),
249                                        $summary->submissionsneedgradingcount);
250         }
252         $time = time();
253         if ($summary->duedate) {
254             // due date
255             // submitted for grading
256             $duedate = $summary->duedate;
257             $this->add_table_row_tuple($t, get_string('duedate', 'assign'),
258                                        userdate($duedate));
260             // time remaining
261             $due = '';
262             if ($duedate - $time <= 0) {
263                 $due = get_string('assignmentisdue', 'assign');
264             } else {
265                 $due = format_time($duedate - $time);
266             }
267             $this->add_table_row_tuple($t, get_string('timeremaining', 'assign'), $due);
268         }
270         // all done - write the table
271         $o .= html_writer::table($t);
272         $o .= $this->output->box_end();
274         // link to the grading page
275         $o .= $this->output->container_start('submissionlinks');
276         $o .= $this->output->action_link(new moodle_url('/mod/assign/view.php',
277                                                           array('id' => $summary->coursemoduleid,
278                                                                 'action'=>'grading')),
279                                                           get_string('viewgrading', 'assign'));
280         $o .= $this->output->container_end();
282         // close the container and insert a spacer
283         $o .= $this->output->container_end();
285         return $o;
286     }
288     /**
289      * render a table containing all the current grades and feedback
290      *
291      * @param assign_feedback_status $status
292      * @return string
293      */
294     public function render_assign_feedback_status(assign_feedback_status $status) {
295         global $DB, $CFG;
296         $o = '';
298         $o .= $this->output->container_start('feedback');
299         $o .= $this->output->heading(get_string('feedback', 'assign'), 3);
300         $o .= $this->output->box_start('boxaligncenter feedbacktable');
301         $t = new html_table();
303         $row = new html_table_row();
304         $cell1 = new html_table_cell(get_string('grade'));
305         $cell2 = new html_table_cell($status->gradefordisplay);
306         $row->cells = array($cell1, $cell2);
307         $t->data[] = $row;
309         $row = new html_table_row();
310         $cell1 = new html_table_cell(get_string('gradedon', 'assign'));
311         $cell2 = new html_table_cell(userdate($status->gradeddate));
312         $row->cells = array($cell1, $cell2);
313         $t->data[] = $row;
315         if ($status->grader) {
316             $row = new html_table_row();
317             $cell1 = new html_table_cell(get_string('gradedby', 'assign'));
318             $cell2 = new html_table_cell($this->output->user_picture($status->grader) . $this->output->spacer(array('width'=>30)) . fullname($status->grader));
319             $row->cells = array($cell1, $cell2);
320             $t->data[] = $row;
321         }
323         foreach ($status->feedbackplugins as $plugin) {
324             if ($plugin->is_enabled() && $plugin->is_visible() && !empty($status->grade) && !$plugin->is_empty($status->grade)) {
325                 $row = new html_table_row();
326                 $cell1 = new html_table_cell($plugin->get_name());
327                 $pluginfeedback = new assign_feedback_plugin_feedback($plugin, $status->grade, assign_feedback_plugin_feedback::SUMMARY, $status->coursemoduleid, $status->returnaction, $status->returnparams);
328                 $cell2 = new html_table_cell($this->render($pluginfeedback));
329                 $row->cells = array($cell1, $cell2);
330                 $t->data[] = $row;
331             }
332         }
335         $o .= html_writer::table($t);
336         $o .= $this->output->box_end();
338         $o .= $this->output->container_end();
339         return $o;
340     }
342     /**
343      * render a table containing the current status of the submission
344      *
345      * @param assign_submission_status $status
346      * @return string
347      */
348     public function render_assign_submission_status(assign_submission_status $status) {
349         $o = '';
350         $o .= $this->output->container_start('submissionstatustable');
351         $o .= $this->output->heading(get_string('submissionstatusheading', 'assign'), 3);
352         $time = time();
354         if ($status->allowsubmissionsfromdate &&
355                 $time <= $status->allowsubmissionsfromdate) {
356             $o .= $this->output->box_start('generalbox boxaligncenter submissionsalloweddates');
357             if ($status->alwaysshowdescription) {
358                 $o .= get_string('allowsubmissionsfromdatesummary', 'assign', userdate($status->allowsubmissionsfromdate));
359             } else {
360                 $o .= get_string('allowsubmissionsanddescriptionfromdatesummary', 'assign', userdate($status->allowsubmissionsfromdate));
361             }
362             $o .= $this->output->box_end();
363         }
364         $o .= $this->output->box_start('boxaligncenter submissionsummarytable');
366         $t = new html_table();
368         $row = new html_table_row();
369         $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
370         if ($status->submission) {
371             $cell2 = new html_table_cell(get_string('submissionstatus_' . $status->submission->status, 'assign'));
372             $cell2->attributes = array('class'=>'submissionstatus' . $status->submission->status);
373         } else {
374             if (!$status->submissionsenabled) {
375                 $cell2 = new html_table_cell(get_string('noonlinesubmissions', 'assign'));
376             } else {
377                 $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
378             }
379         }
380         $row->cells = array($cell1, $cell2);
381         $t->data[] = $row;
383         // status
384         if ($status->locked) {
385             $row = new html_table_row();
386             $cell1 = new html_table_cell();
387             $cell2 = new html_table_cell(get_string('submissionslocked', 'assign'));
388             $cell2->attributes = array('class'=>'submissionlocked');
389             $row->cells = array($cell1, $cell2);
390             $t->data[] = $row;
391         }
393         // grading status
394         $row = new html_table_row();
395         $cell1 = new html_table_cell(get_string('gradingstatus', 'assign'));
397         if ($status->graded) {
398             $cell2 = new html_table_cell(get_string('graded', 'assign'));
399             $cell2->attributes = array('class'=>'submissiongraded');
400         } else {
401             $cell2 = new html_table_cell(get_string('notgraded', 'assign'));
402             $cell2->attributes = array('class'=>'submissionnotgraded');
403         }
404         $row->cells = array($cell1, $cell2);
405         $t->data[] = $row;
408         $duedate = $status->duedate;
409         if ($duedate >= 1) {
410             $row = new html_table_row();
411             $cell1 = new html_table_cell(get_string('duedate', 'assign'));
412             $cell2 = new html_table_cell(userdate($duedate));
413             $row->cells = array($cell1, $cell2);
414             $t->data[] = $row;
416             // time remaining
417             $row = new html_table_row();
418             $cell1 = new html_table_cell(get_string('timeremaining', 'assign'));
419             if ($duedate - $time <= 0) {
420                 if (!$status->submission || $status->submission != ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
421                     if ($status->submissionsenabled) {
422                         $cell2 = new html_table_cell(get_string('overdue', 'assign', format_time($time - $duedate)));
423                         $cell2->attributes = array('class'=>'overdue');
424                     } else {
425                         $cell2 = new html_table_cell(get_string('duedatereached', 'assign'));
426                     }
427                 } else {
428                     if ($status->submission->timemodified > $duedate) {
429                         $cell2 = new html_table_cell(get_string('submittedlate', 'assign', format_time($status->submission->timemodified - $duedate)));
430                         $cell2->attributes = array('class'=>'latesubmission');
431                     } else {
432                         $cell2 = new html_table_cell(get_string('submittedearly', 'assign', format_time($status->submission->timemodified - $duedate)));
433                         $cell2->attributes = array('class'=>'earlysubmission');
434                     }
435                 }
436             } else {
437                 $cell2 = new html_table_cell(format_time($duedate - $time));
438             }
439             $row->cells = array($cell1, $cell2);
440             $t->data[] = $row;
441         }
443         // last modified
444         if ($status->submission) {
445             $row = new html_table_row();
446             $cell1 = new html_table_cell(get_string('timemodified', 'assign'));
447             $cell2 = new html_table_cell(userdate($status->submission->timemodified));
448             $row->cells = array($cell1, $cell2);
449             $t->data[] = $row;
451             foreach ($status->submissionplugins as $plugin) {
452                 if ($plugin->is_enabled() && $plugin->is_visible() && !$plugin->is_empty($status->submission)) {
453                     $row = new html_table_row();
454                     $cell1 = new html_table_cell($plugin->get_name());
455                     $pluginsubmission = new assign_submission_plugin_submission($plugin, $status->submission, assign_submission_plugin_submission::SUMMARY, $status->coursemoduleid, $status->returnaction, $status->returnparams);
456                     $cell2 = new html_table_cell($this->render($pluginsubmission));
457                     $row->cells = array($cell1, $cell2);
458                     $t->data[] = $row;
459                 }
460             }
461         }
464         $o .= html_writer::table($t);
465         $o .= $this->output->box_end();
467         // links
468         if ($status->canedit) {
469             if (!$status->submission) {
470                 $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php',
471                     array('id' => $status->coursemoduleid, 'action' => 'editsubmission')), get_string('addsubmission', 'assign'), 'get');
472             } else {
473                 $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php',
474                     array('id' => $status->coursemoduleid, 'action' => 'editsubmission')), get_string('editsubmission', 'assign'), 'get');
475             }
476         }
478         if ($status->cansubmit) {
479             // submission.php
480             $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php',
481                     array('id' => $status->coursemoduleid, 'action'=>'submit')), get_string('submitassignment', 'assign'), 'get');
482             $o .= $this->output->box_start('boxaligncenter submithelp');
483             $o .= get_string('submitassignment_help', 'assign');
484             $o .= $this->output->box_end();
485         }
487         $o .= $this->output->container_end();
488         return $o;
489     }
491     /**
492      * render a submission plugin submission
493      *
494      * @param assign_submission_plugin_submission $submissionplugin
495      * @return string
496      */
497     public function render_assign_submission_plugin_submission(assign_submission_plugin_submission $submissionplugin) {
498         $o = '';
500         if ($submissionplugin->view == assign_submission_plugin_submission::SUMMARY) {
501             $icon = $this->output->pix_icon('t/preview', get_string('view' . substr($submissionplugin->plugin->get_subtype(), strlen('assign')), 'mod_assign'));
502             $link = '';
503             $showviewlink = false;
504             $summary = $submissionplugin->plugin->view_summary($submissionplugin->submission, $showviewlink);
505             if ($showviewlink) {
506                 $link = $this->output->action_link(
507                                 new moodle_url('/mod/assign/view.php',
508                                                array('id' => $submissionplugin->coursemoduleid,
509                                                      'sid'=>$submissionplugin->submission->id,
510                                                      'plugin'=>$submissionplugin->plugin->get_type(),
511                                                      'action'=>'viewplugin' . $submissionplugin->plugin->get_subtype(),
512                                                      'returnaction'=>$submissionplugin->returnaction,
513                                                      'returnparams'=>http_build_query($submissionplugin->returnparams))),
514                                 $icon);
516                 $link .= $this->output->spacer(array('width'=>15));
517             }
519             $o .= $link . $summary;
520         } else if ($submissionplugin->view == assign_submission_plugin_submission::FULL) {
521             $o .= $this->output->box_start('boxaligncenter submissionfull');
522             $o .= $submissionplugin->plugin->view($submissionplugin->submission);
523             $o .= $this->output->box_end();
524         }
526         return $o;
527     }
529     /**
530      * render the grading table
531      *
532      * @param assign_grading_table $table
533      * @return string
534      */
535     public function render_assign_grading_table(assign_grading_table $table) {
536         $o = '';
537         $o .= $this->output->box_start('boxaligncenter gradingtable');
538         $this->page->requires->js_init_call('M.mod_assign.init_grading_table', array());
539         $this->page->requires->string_for_js('nousersselected', 'assign');
540         $this->page->requires->string_for_js('batchoperationconfirmlock', 'assign');
541         $this->page->requires->string_for_js('batchoperationconfirmunlock', 'assign');
542         $this->page->requires->string_for_js('batchoperationconfirmreverttodraft', 'assign');
543         $this->page->requires->string_for_js('editaction', 'assign');
544         // need to get from prefs
545         $o .= $this->flexible_table($table, $table->get_rows_per_page(), true);
546         $o .= $this->output->box_end();
548         return $o;
549    }
551     /**
552      * Render a feedback plugin feedback
553      *
554      * @param assign_feedback_plugin_feedback $feedbackplugin
555      * @return string
556      */
557     public function render_assign_feedback_plugin_feedback(assign_feedback_plugin_feedback $feedbackplugin) {
558         $o = '';
560         if ($feedbackplugin->view == assign_feedback_plugin_feedback::SUMMARY) {
561             $icon = $this->output->pix_icon('t/preview', get_string('view' . substr($feedbackplugin->plugin->get_subtype(), strlen('assign')), 'mod_assign'));
562             $link = '';
563             $showviewlink = false;
564             $summary = $feedbackplugin->plugin->view_summary($feedbackplugin->grade, $showviewlink);
565             if ($showviewlink) {
566                 $link = $this->output->action_link(
567                                 new moodle_url('/mod/assign/view.php',
568                                                array('id' => $feedbackplugin->coursemoduleid,
569                                                      'gid'=>$feedbackplugin->grade->id,
570                                                      'plugin'=>$feedbackplugin->plugin->get_type(),
571                                                      'action'=>'viewplugin' . $feedbackplugin->plugin->get_subtype(),
572                                                      'returnaction'=>$feedbackplugin->returnaction,
573                                                      'returnparams'=>http_build_query($feedbackplugin->returnparams))),
574                                 $icon);
575                 $link .= $this->output->spacer(array('width'=>15));
576             }
578             $o .= $link . $summary;
579         } else if ($feedbackplugin->view == assign_feedback_plugin_feedback::FULL) {
580             $o .= $this->output->box_start('boxaligncenter feedbackfull');
581             $o .= $feedbackplugin->plugin->view($feedbackplugin->grade);
582             $o .= $this->output->box_end();
583         }
585         return $o;
586     }
590     /**
591      * Internal function - creates htmls structure suitable for YUI tree.
592      *
593      * @param assign_files $tree
594      * @param array $dir
595      * @return string
596      */
597     protected function htmllize_tree(assign_files $tree, $dir) {
598         global $CFG;
599         $yuiconfig = array();
600         $yuiconfig['type'] = 'html';
602         if (empty($dir['subdirs']) and empty($dir['files'])) {
603             return '';
604         }
606         $result = '<ul>';
607         foreach ($dir['subdirs'] as $subdir) {
608             $image = $this->output->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle', array('class'=>'icon'));
609             $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
610         }
612         foreach ($dir['files'] as $file) {
613             $filename = $file->get_filename();
614             if ($CFG->enableplagiarism) {
615                 require_once($CFG->libdir.'/plagiarismlib.php');
616                 $plagiarsmlinks = plagiarism_get_links(array('userid'=>$file->get_userid(), 'file'=>$file, 'cmid'=>$tree->cm->id, 'course'=>$tree->course));
617             } else {
618                 $plagiarsmlinks = '';
619             }
620             $image = $this->output->pix_icon(file_file_icon($file), $filename, 'moodle', array('class'=>'icon'));
621             $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.$file->fileurl.' '.$plagiarsmlinks.$file->portfoliobutton.'</div></li>';
622         }
624         $result .= '</ul>';
626         return $result;
627     }
629     /**
630      * Helper method dealing with the fact we can not just fetch the output of flexible_table
631      *
632      * @param flexible_table $table The table to render
633      * @param int $rowsperpage How many assignments to render in a page
634      * @param bool $displaylinks - Whether to render links in the table (e.g. downloads would not enable this)
635      * @return string HTML
636      */
637     protected function flexible_table(flexible_table $table, $rowsperpage, $displaylinks) {
639         $o = '';
640         ob_start();
641         $table->out($rowsperpage, $displaylinks);
642         $o = ob_get_contents();
643         ob_end_clean();
645         return $o;
646     }
648     /**
649      * Helper method dealing with the fact we can not just fetch the output of moodleforms
650      *
651      * @param moodleform $mform
652      * @return string HTML
653      */
654     protected function moodleform(moodleform $mform) {
656         $o = '';
657         ob_start();
658         $mform->display();
659         $o = ob_get_contents();
660         ob_end_clean();
662         return $o;
663     }