MDL-33850: Assignment: Change text to "Add submission" before anything has been submitted
[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 - confirm the student really does want to submit for marking
172             $continueurl = new moodle_url('/mod/assign/view.php', array('id' => $page->coursemoduleid,
173                                                                         'action' => 'confirmsubmit',
174                                                                         'sesskey' => sesskey()));
175             $o .= $this->output->confirm(get_string('confirmsubmission', 'mod_assign'), $continueurl, $cancelurl);
176         }
177         $o .= $this->output->container_end();
180         return $o;
181     }
183     /**
184      * Page is done - render the footer
185      *
186      * @return void
187      */
188     public function render_footer() {
189         return $this->output->footer();
190     }
192     /**
193      * render the header
194      *
195      * @param assign_header $header
196      * @return string
197      */
198     public function render_assign_header(assign_header $header) {
199         $o = '';
201         if ($header->subpage) {
202             $this->page->navbar->add($header->subpage);
203         }
205         $this->page->set_title(get_string('pluginname', 'assign'));
206         $this->page->set_heading($header->assign->name);
208         $o .= $this->output->header();
209         if ($header->preface) {
210             $o .= $header->preface;
211         }
212         $o .= $this->output->heading(format_string($header->assign->name,false, array('context' => $header->context)));
214         if ($header->showintro) {
215             $o .= $this->output->box_start('generalbox boxaligncenter', 'intro');
216             $o .= format_module_intro('assign', $header->assign, $header->coursemoduleid);
217             $o .= $this->output->box_end();
218         }
220         return $o;
221     }
223     /**
224      * render a table containing the current status of the grading process
225      *
226      * @param assign_grading_summary $summary
227      * @return string
228      */
229     public function render_assign_grading_summary(assign_grading_summary $summary) {
230         // create a table for the data
231         $o = '';
232         $o .= $this->output->container_start('gradingsummary');
233         $o .= $this->output->heading(get_string('gradingsummary', 'assign'), 3);
234         $o .= $this->output->box_start('boxaligncenter gradingsummarytable');
235         $t = new html_table();
237         // status
238         $this->add_table_row_tuple($t, get_string('numberofparticipants', 'assign'),
239                                    $summary->participantcount);
241         // drafts
242         if ($summary->submissiondraftsenabled) {
243             $this->add_table_row_tuple($t, get_string('numberofdraftsubmissions', 'assign'),
244                                        $summary->submissiondraftscount);
245        }
247         // submitted for grading
248         if ($summary->submissionsenabled) {
249             $this->add_table_row_tuple($t, get_string('numberofsubmittedassignments', 'assign'),
250                                        $summary->submissionssubmittedcount);
251         }
253         $time = time();
254         if ($summary->duedate) {
255             // due date
256             // submitted for grading
257             $duedate = $summary->duedate;
258             $this->add_table_row_tuple($t, get_string('duedate', 'assign'),
259                                        userdate($duedate));
261             // time remaining
262             $due = '';
263             if ($duedate - $time <= 0) {
264                 $due = get_string('assignmentisdue', 'assign');
265             } else {
266                 $due = format_time($duedate - $time);
267             }
268             $this->add_table_row_tuple($t, get_string('timeremaining', 'assign'), $due);
269         }
271         // all done - write the table
272         $o .= html_writer::table($t);
273         $o .= $this->output->box_end();
275         // link to the grading page
276         $o .= $this->output->container_start('submissionlinks');
277         $o .= $this->output->action_link(new moodle_url('/mod/assign/view.php',
278                                                           array('id' => $summary->coursemoduleid,
279                                                                 'action'=>'grading')),
280                                                           get_string('viewgrading', 'assign'));
281         $o .= $this->output->container_end();
283         // close the container and insert a spacer
284         $o .= $this->output->container_end();
286         return $o;
287     }
289     /**
290      * render a table containing all the current grades and feedback
291      *
292      * @param assign_feedback_status $status
293      * @return string
294      */
295     public function render_assign_feedback_status(assign_feedback_status $status) {
296         global $DB, $CFG;
297         $o = '';
299         $o .= $this->output->container_start('feedback');
300         $o .= $this->output->heading(get_string('feedback', 'assign'), 3);
301         $o .= $this->output->box_start('boxaligncenter feedbacktable');
302         $t = new html_table();
304         $row = new html_table_row();
305         $cell1 = new html_table_cell(get_string('grade'));
306         $cell2 = new html_table_cell($status->gradefordisplay);
307         $row->cells = array($cell1, $cell2);
308         $t->data[] = $row;
310         $row = new html_table_row();
311         $cell1 = new html_table_cell(get_string('gradedon', 'assign'));
312         $cell2 = new html_table_cell(userdate($status->gradeddate));
313         $row->cells = array($cell1, $cell2);
314         $t->data[] = $row;
316         if ($status->grader) {
317             $row = new html_table_row();
318             $cell1 = new html_table_cell(get_string('gradedby', 'assign'));
319             $cell2 = new html_table_cell($this->output->user_picture($status->grader) . $this->output->spacer(array('width'=>30)) . fullname($status->grader));
320             $row->cells = array($cell1, $cell2);
321             $t->data[] = $row;
322         }
324         foreach ($status->feedbackplugins as $plugin) {
325             if ($plugin->is_enabled() && $plugin->is_visible() && !$plugin->is_empty($status->grade)) {
326                 $row = new html_table_row();
327                 $cell1 = new html_table_cell($plugin->get_name());
328                 $pluginfeedback = new assign_feedback_plugin_feedback($plugin, $status->grade, assign_feedback_plugin_feedback::SUMMARY, $status->coursemoduleid, $status->returnaction, $status->returnparams);
329                 $cell2 = new html_table_cell($this->render($pluginfeedback));
330                 $row->cells = array($cell1, $cell2);
331                 $t->data[] = $row;
332             }
333         }
336         $o .= html_writer::table($t);
337         $o .= $this->output->box_end();
339         $o .= $this->output->container_end();
340         return $o;
341     }
343     /**
344      * render a table containing the current status of the submission
345      *
346      * @param assign_submission_status $status
347      * @return string
348      */
349     public function render_assign_submission_status(assign_submission_status $status) {
350         $o = '';
351         $o .= $this->output->container_start('submissionstatustable');
352         $o .= $this->output->heading(get_string('submissionstatusheading', 'assign'), 3);
353         $time = time();
355         if ($status->allowsubmissionsfromdate &&
356                 $time <= $status->allowsubmissionsfromdate) {
357             $o .= $this->output->box_start('generalbox boxaligncenter submissionsalloweddates');
358             if ($status->alwaysshowdescription) {
359                 $o .= get_string('allowsubmissionsfromdatesummary', 'assign', userdate($status->allowsubmissionsfromdate));
360             } else {
361                 $o .= get_string('allowsubmissionsanddescriptionfromdatesummary', 'assign', userdate($status->allowsubmissionsfromdate));
362             }
363             $o .= $this->output->box_end();
364         }
365         $o .= $this->output->box_start('boxaligncenter submissionsummarytable');
367         $t = new html_table();
369         $row = new html_table_row();
370         $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
371         if ($status->submission) {
372             $cell2 = new html_table_cell(get_string('submissionstatus_' . $status->submission->status, 'assign'));
373             $cell2->attributes = array('class'=>'submissionstatus' . $status->submission->status);
374         } else {
375             if (!$status->submissionsenabled) {
376                 $cell2 = new html_table_cell(get_string('noonlinesubmissions', 'assign'));
377             } else {
378                 $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
379             }
380         }
381         $row->cells = array($cell1, $cell2);
382         $t->data[] = $row;
384         // status
385         if ($status->locked) {
386             $row = new html_table_row();
387             $cell1 = new html_table_cell();
388             $cell2 = new html_table_cell(get_string('submissionslocked', 'assign'));
389             $cell2->attributes = array('class'=>'submissionlocked');
390             $row->cells = array($cell1, $cell2);
391             $t->data[] = $row;
392         }
394         // grading status
395         $row = new html_table_row();
396         $cell1 = new html_table_cell(get_string('gradingstatus', 'assign'));
398         if ($status->graded) {
399             $cell2 = new html_table_cell(get_string('graded', 'assign'));
400             $cell2->attributes = array('class'=>'submissiongraded');
401         } else {
402             $cell2 = new html_table_cell(get_string('notgraded', 'assign'));
403             $cell2->attributes = array('class'=>'submissionnotgraded');
404         }
405         $row->cells = array($cell1, $cell2);
406         $t->data[] = $row;
409         $duedate = $status->duedate;
410         if ($duedate >= 1) {
411             $row = new html_table_row();
412             $cell1 = new html_table_cell(get_string('duedate', 'assign'));
413             $cell2 = new html_table_cell(userdate($duedate));
414             $row->cells = array($cell1, $cell2);
415             $t->data[] = $row;
417             // time remaining
418             $row = new html_table_row();
419             $cell1 = new html_table_cell(get_string('timeremaining', 'assign'));
420             if ($duedate - $time <= 0) {
421                 if (!$status->submission || $status->submission != ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
422                     if ($status->submissionsenabled) {
423                         $cell2 = new html_table_cell(get_string('overdue', 'assign', format_time($time - $duedate)));
424                         $cell2->attributes = array('class'=>'overdue');
425                     } else {
426                         $cell2 = new html_table_cell(get_string('duedatereached', 'assign'));
427                     }
428                 } else {
429                     if ($status->submission->timemodified > $duedate) {
430                         $cell2 = new html_table_cell(get_string('submittedlate', 'assign', format_time($status->submission->timemodified - $duedate)));
431                         $cell2->attributes = array('class'=>'latesubmission');
432                     } else {
433                         $cell2 = new html_table_cell(get_string('submittedearly', 'assign', format_time($status->submission->timemodified - $duedate)));
434                         $cell2->attributes = array('class'=>'earlysubmission');
435                     }
436                 }
437             } else {
438                 $cell2 = new html_table_cell(format_time($duedate - $time));
439             }
440             $row->cells = array($cell1, $cell2);
441             $t->data[] = $row;
442         }
444         // last modified
445         if ($status->submission) {
446             $row = new html_table_row();
447             $cell1 = new html_table_cell(get_string('timemodified', 'assign'));
448             $cell2 = new html_table_cell(userdate($status->submission->timemodified));
449             $row->cells = array($cell1, $cell2);
450             $t->data[] = $row;
452             foreach ($status->submissionplugins as $plugin) {
453                 if ($plugin->is_enabled() && $plugin->is_visible() && !$plugin->is_empty($status->submission)) {
454                     $row = new html_table_row();
455                     $cell1 = new html_table_cell($plugin->get_name());
456                     $pluginsubmission = new assign_submission_plugin_submission($plugin, $status->submission, assign_submission_plugin_submission::SUMMARY, $status->coursemoduleid, $status->returnaction, $status->returnparams);
457                     $cell2 = new html_table_cell($this->render($pluginsubmission));
458                     $row->cells = array($cell1, $cell2);
459                     $t->data[] = $row;
460                 }
461             }
462         }
465         $o .= html_writer::table($t);
466         $o .= $this->output->box_end();
468         // links
469         if ($status->canedit) {
470             if (!$status->submission) {
471                 $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php',
472                     array('id' => $status->coursemoduleid, 'action' => 'editsubmission')), get_string('addsubmission', 'assign'), 'get');
473             } else {
474                 $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php',
475                     array('id' => $status->coursemoduleid, 'action' => 'editsubmission')), get_string('editsubmission', 'assign'), 'get');
476             }
477         }
479         if ($status->cansubmit) {
480             // submission.php
481             $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php',
482                     array('id' => $status->coursemoduleid, 'action'=>'submit')), get_string('submitassignment', 'assign'), 'get');
483             $o .= $this->output->box_start('boxaligncenter submithelp');
484             $o .= get_string('submitassignment_help', 'assign');
485             $o .= $this->output->box_end();
486         }
488         $o .= $this->output->container_end();
489         return $o;
490     }
492     /**
493      * render a submission plugin submission
494      *
495      * @param assign_submission_plugin_submission $submissionplugin
496      * @return string
497      */
498     public function render_assign_submission_plugin_submission(assign_submission_plugin_submission $submissionplugin) {
499         $o = '';
501         if ($submissionplugin->view == assign_submission_plugin_submission::SUMMARY) {
502             $icon = $this->output->pix_icon('t/preview', get_string('view' . substr($submissionplugin->plugin->get_subtype(), strlen('assign')), 'mod_assign'));
503             $link = '';
504             $showviewlink = false;
505             $summary = $submissionplugin->plugin->view_summary($submissionplugin->submission, $showviewlink);
506             if ($showviewlink) {
507                 $link = $this->output->action_link(
508                                 new moodle_url('/mod/assign/view.php',
509                                                array('id' => $submissionplugin->coursemoduleid,
510                                                      'sid'=>$submissionplugin->submission->id,
511                                                      'plugin'=>$submissionplugin->plugin->get_type(),
512                                                      'action'=>'viewplugin' . $submissionplugin->plugin->get_subtype(),
513                                                      'returnaction'=>$submissionplugin->returnaction,
514                                                      'returnparams'=>http_build_query($submissionplugin->returnparams))),
515                                 $icon);
517                 $link .= $this->output->spacer(array('width'=>15));
518             }
520             $o .= $link . $summary;
521         } else if ($submissionplugin->view == assign_submission_plugin_submission::FULL) {
522             $o .= $this->output->box_start('boxaligncenter submissionfull');
523             $o .= $submissionplugin->plugin->view($submissionplugin->submission);
524             $o .= $this->output->box_end();
525         }
527         return $o;
528     }
530     /**
531      * render the grading table
532      *
533      * @param assign_grading_table $table
534      * @return string
535      */
536     public function render_assign_grading_table(assign_grading_table $table) {
537         $o = '';
538         $o .= $this->output->box_start('boxaligncenter gradingtable');
539         $this->page->requires->js_init_call('M.mod_assign.init_grading_table', array());
540         $this->page->requires->string_for_js('nousersselected', 'assign');
541         $this->page->requires->string_for_js('batchoperationconfirmlock', 'assign');
542         $this->page->requires->string_for_js('batchoperationconfirmunlock', 'assign');
543         $this->page->requires->string_for_js('batchoperationconfirmreverttodraft', 'assign');
544         $this->page->requires->string_for_js('editaction', 'assign');
545         // need to get from prefs
546         $o .= $this->flexible_table($table, $table->get_rows_per_page(), true);
547         $o .= $this->output->box_end();
549         return $o;
550    }
552     /**
553      * Render a feedback plugin feedback
554      *
555      * @param assign_feedback_plugin_feedback $feedbackplugin
556      * @return string
557      */
558     public function render_assign_feedback_plugin_feedback(assign_feedback_plugin_feedback $feedbackplugin) {
559         $o = '';
561         if ($feedbackplugin->view == assign_feedback_plugin_feedback::SUMMARY) {
562             $icon = $this->output->pix_icon('t/preview', get_string('view' . substr($feedbackplugin->plugin->get_subtype(), strlen('assign')), 'mod_assign'));
563             $link = '';
564             $showviewlink = false;
565             $summary = $feedbackplugin->plugin->view_summary($feedbackplugin->grade, $showviewlink);
566             if ($showviewlink) {
567                 $link = $this->output->action_link(
568                                 new moodle_url('/mod/assign/view.php',
569                                                array('id' => $feedbackplugin->coursemoduleid,
570                                                      'gid'=>$feedbackplugin->grade->id,
571                                                      'plugin'=>$feedbackplugin->plugin->get_type(),
572                                                      'action'=>'viewplugin' . $feedbackplugin->plugin->get_subtype(),
573                                                      'returnaction'=>$feedbackplugin->returnaction,
574                                                      'returnparams'=>http_build_query($feedbackplugin->returnparams))),
575                                 $icon);
576                 $link .= $this->output->spacer(array('width'=>15));
577             }
579             $o .= $link . $summary;
580         } else if ($feedbackplugin->view == assign_feedback_plugin_feedback::FULL) {
581             $o .= $this->output->box_start('boxaligncenter feedbackfull');
582             $o .= $feedbackplugin->plugin->view($feedbackplugin->grade);
583             $o .= $this->output->box_end();
584         }
586         return $o;
587     }
591     /**
592      * Internal function - creates htmls structure suitable for YUI tree.
593      *
594      * @param assign_files $tree
595      * @param array $dir
596      * @return string
597      */
598     protected function htmllize_tree(assign_files $tree, $dir) {
599         global $CFG;
600         $yuiconfig = array();
601         $yuiconfig['type'] = 'html';
603         if (empty($dir['subdirs']) and empty($dir['files'])) {
604             return '';
605         }
607         $result = '<ul>';
608         foreach ($dir['subdirs'] as $subdir) {
609             $image = $this->output->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle', array('class'=>'icon'));
610             $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
611         }
613         foreach ($dir['files'] as $file) {
614             $filename = $file->get_filename();
615             if ($CFG->enableplagiarism) {
616                 require_once($CFG->libdir.'/plagiarismlib.php');
617                 $plagiarsmlinks = plagiarism_get_links(array('userid'=>$file->get_userid(), 'file'=>$file, 'cmid'=>$tree->cm->id, 'course'=>$tree->course));
618             } else {
619                 $plagiarsmlinks = '';
620             }
621             $image = $this->output->pix_icon(file_file_icon($file), $filename, 'moodle', array('class'=>'icon'));
622             $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.$file->fileurl.' '.$plagiarsmlinks.$file->portfoliobutton.'</div></li>';
623         }
625         $result .= '</ul>';
627         return $result;
628     }
630     /**
631      * Helper method dealing with the fact we can not just fetch the output of flexible_table
632      *
633      * @param flexible_table $table The table to render
634      * @param int $rowsperpage How many assignments to render in a page
635      * @param bool $displaylinks - Whether to render links in the table (e.g. downloads would not enable this)
636      * @return string HTML
637      */
638     protected function flexible_table(flexible_table $table, $rowsperpage, $displaylinks) {
640         $o = '';
641         ob_start();
642         $table->out($rowsperpage, $displaylinks);
643         $o = ob_get_contents();
644         ob_end_clean();
646         return $o;
647     }
649     /**
650      * Helper method dealing with the fact we can not just fetch the output of moodleforms
651      *
652      * @param moodleform $mform
653      * @return string HTML
654      */
655     protected function moodleform(moodleform $mform) {
657         $o = '';
658         ob_start();
659         $mform->display();
660         $o = ob_get_contents();
661         ob_end_clean();
663         return $o;
664     }