d371eb0f1b012dda9bf16c4db18a3e0d62c58328
[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 = html_writer::random_id('assign_files_tree');
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 message notification
89      * @param assign_gradingmessage $result The result to render
90      * @return string
91      */
92     public function render_assign_gradingmessage(assign_gradingmessage $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($result->heading, 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 = '';
127         $supendedclass = '';
128         $suspendedicon = '';
130         if (!$summary->user) {
131             return;
132         }
134         if ($summary->suspendeduser) {
135             $supendedclass = ' usersuspended';
136             $suspendedstring = get_string('userenrolmentsuspended', 'grades');
137             $suspendedicon = ' ' . html_writer::empty_tag('img', array('src' => $this->pix_url('i/enrolmentsuspended'),
138                 'title' => $suspendedstring, 'alt' => $suspendedstring, 'class' => 'usersuspendedicon'));
139         }
140         $o .= $this->output->container_start('usersummary');
141         $o .= $this->output->box_start('boxaligncenter usersummarysection'.$supendedclass);
142         if ($summary->blindmarking) {
143             $o .= get_string('hiddenuser', 'assign') . $summary->uniqueidforuser.$suspendedicon;
144         } else {
145             $o .= $this->output->user_picture($summary->user);
146             $o .= $this->output->spacer(array('width'=>30));
147             $urlparams = array('id' => $summary->user->id, 'course'=>$summary->courseid);
148             $url = new moodle_url('/user/view.php', $urlparams);
149             $fullname = fullname($summary->user, $summary->viewfullnames);
150             $extrainfo = array();
151             foreach ($summary->extrauserfields as $extrafield) {
152                 $extrainfo[] = $summary->user->$extrafield;
153             }
154             if (count($extrainfo)) {
155                 $fullname .= ' (' . implode(', ', $extrainfo) . ')';
156             }
157             $fullname .= $suspendedicon;
158             $o .= $this->output->action_link($url, $fullname);
159         }
160         $o .= $this->output->box_end();
161         $o .= $this->output->container_end();
163         return $o;
164     }
166     /**
167      * Render the submit for grading page
168      *
169      * @param assign_submit_for_grading_page $page
170      * @return string
171      */
172     public function render_assign_submit_for_grading_page($page) {
173         $o = '';
175         $o .= $this->output->container_start('submitforgrading');
176         $o .= $this->output->heading(get_string('submitassignment', 'assign'), 3);
177         $o .= $this->output->spacer(array('height'=>30));
179         $cancelurl = new moodle_url('/mod/assign/view.php', array('id' => $page->coursemoduleid));
180         if (count($page->notifications)) {
181             // At least one of the submission plugins is not ready for submission.
183             $o .= $this->output->heading(get_string('submissionnotready', 'assign'), 4);
185             foreach ($page->notifications as $notification) {
186                 $o .= $this->output->notification($notification);
187             }
189             $o .= $this->output->continue_button($cancelurl);
190         } else {
191             // All submission plugins ready - show the confirmation form.
192             $o .= $this->output->box_start('generalbox submitconfirm');
193             $o .= $this->moodleform($page->confirmform);
194             $o .= $this->output->box_end();
195         }
196         $o .= $this->output->container_end();
198         return $o;
199     }
201     /**
202      * Page is done - render the footer.
203      *
204      * @return void
205      */
206     public function render_footer() {
207         return $this->output->footer();
208     }
210     /**
211      * Render the header.
212      *
213      * @param assign_header $header
214      * @return string
215      */
216     public function render_assign_header(assign_header $header) {
217         $o = '';
219         if ($header->subpage) {
220             $this->page->navbar->add($header->subpage);
221         }
223         $this->page->set_title(get_string('pluginname', 'assign'));
224         $this->page->set_heading($this->page->course->fullname);
226         $o .= $this->output->header();
227         $heading = format_string($header->assign->name, false, array('context' => $header->context));
228         $o .= $this->output->heading($heading);
229         if ($header->preface) {
230             $o .= $header->preface;
231         }
233         if ($header->showintro) {
234             $o .= $this->output->box_start('generalbox boxaligncenter', 'intro');
235             $o .= format_module_intro('assign', $header->assign, $header->coursemoduleid);
236             $o .= $this->output->box_end();
237         }
239         return $o;
240     }
242     /**
243      * Render the header for an individual plugin.
244      *
245      * @param assign_plugin_header $header
246      * @return string
247      */
248     public function render_assign_plugin_header(assign_plugin_header $header) {
249         $o = $header->plugin->view_header();
250         return $o;
251     }
253     /**
254      * Render a table containing the current status of the grading process.
255      *
256      * @param assign_grading_summary $summary
257      * @return string
258      */
259     public function render_assign_grading_summary(assign_grading_summary $summary) {
260         // Create a table for the data.
261         $o = '';
262         $o .= $this->output->container_start('gradingsummary');
263         $o .= $this->output->heading(get_string('gradingsummary', 'assign'), 3);
264         $o .= $this->output->box_start('boxaligncenter gradingsummarytable');
265         $t = new html_table();
267         // Status.
268         if ($summary->teamsubmission) {
269             $this->add_table_row_tuple($t, get_string('numberofteams', 'assign'),
270                                        $summary->participantcount);
271         } else {
272             $this->add_table_row_tuple($t, get_string('numberofparticipants', 'assign'),
273                                        $summary->participantcount);
274         }
276         // Drafts count and dont show drafts count when using offline assignment.
277         if ($summary->submissiondraftsenabled && $summary->submissionsenabled) {
278             $this->add_table_row_tuple($t, get_string('numberofdraftsubmissions', 'assign'),
279                                        $summary->submissiondraftscount);
280         }
282         // Submitted for grading.
283         if ($summary->submissionsenabled) {
284             $this->add_table_row_tuple($t, get_string('numberofsubmittedassignments', 'assign'),
285                                        $summary->submissionssubmittedcount);
286             if (!$summary->teamsubmission) {
287                 $this->add_table_row_tuple($t, get_string('numberofsubmissionsneedgrading', 'assign'),
288                                            $summary->submissionsneedgradingcount);
289             }
290         }
292         $time = time();
293         if ($summary->duedate) {
294             // Due date.
295             $duedate = $summary->duedate;
296             $this->add_table_row_tuple($t, get_string('duedate', 'assign'),
297                                        userdate($duedate));
299             // Time remaining.
300             $due = '';
301             if ($duedate - $time <= 0) {
302                 $due = get_string('assignmentisdue', 'assign');
303             } else {
304                 $due = format_time($duedate - $time);
305             }
306             $this->add_table_row_tuple($t, get_string('timeremaining', 'assign'), $due);
308             if ($duedate < $time) {
309                 $cutoffdate = $summary->cutoffdate;
310                 if ($cutoffdate) {
311                     if ($cutoffdate > $time) {
312                         $late = get_string('latesubmissionsaccepted', 'assign');
313                     } else {
314                         $late = get_string('nomoresubmissionsaccepted', 'assign');
315                     }
316                     $this->add_table_row_tuple($t, get_string('latesubmissions', 'assign'), $late);
317                 }
318             }
320         }
322         // All done - write the table.
323         $o .= html_writer::table($t);
324         $o .= $this->output->box_end();
326         // Link to the grading page.
327         $o .= $this->output->container_start('submissionlinks');
328         $urlparams = array('id' => $summary->coursemoduleid, 'action'=>'grading');
329         $url = new moodle_url('/mod/assign/view.php', $urlparams);
330         $o .= $this->output->action_link($url, get_string('viewgrading', 'assign'));
331         $o .= $this->output->container_end();
333         // Close the container and insert a spacer.
334         $o .= $this->output->container_end();
336         return $o;
337     }
339     /**
340      * Render a table containing all the current grades and feedback.
341      *
342      * @param assign_feedback_status $status
343      * @return string
344      */
345     public function render_assign_feedback_status(assign_feedback_status $status) {
346         global $DB, $CFG;
347         $o = '';
349         $o .= $this->output->container_start('feedback');
350         $o .= $this->output->heading(get_string('feedback', 'assign'), 3);
351         $o .= $this->output->box_start('boxaligncenter feedbacktable');
352         $t = new html_table();
354         // Grade.
355         if (isset($status->gradefordisplay)) {
356             $row = new html_table_row();
357             $cell1 = new html_table_cell(get_string('grade'));
358             $cell2 = new html_table_cell($status->gradefordisplay);
359             $row->cells = array($cell1, $cell2);
360             $t->data[] = $row;
362             // Grade date.
363             $row = new html_table_row();
364             $cell1 = new html_table_cell(get_string('gradedon', 'assign'));
365             $cell2 = new html_table_cell(userdate($status->gradeddate));
366             $row->cells = array($cell1, $cell2);
367             $t->data[] = $row;
368         }
370         if ($status->grader) {
371             // Grader.
372             $row = new html_table_row();
373             $cell1 = new html_table_cell(get_string('gradedby', 'assign'));
374             $userdescription = $this->output->user_picture($status->grader) .
375                                $this->output->spacer(array('width'=>30)) .
376                                fullname($status->grader);
377             $cell2 = new html_table_cell($userdescription);
378             $row->cells = array($cell1, $cell2);
379             $t->data[] = $row;
380         }
382         foreach ($status->feedbackplugins as $plugin) {
383             if ($plugin->is_enabled() &&
384                     $plugin->is_visible() &&
385                     $plugin->has_user_summary() &&
386                     !empty($status->grade) &&
387                     !$plugin->is_empty($status->grade)) {
389                 $row = new html_table_row();
390                 $cell1 = new html_table_cell($plugin->get_name());
391                 $displaymode = assign_feedback_plugin_feedback::SUMMARY;
392                 $pluginfeedback = new assign_feedback_plugin_feedback($plugin,
393                                                                       $status->grade,
394                                                                       $displaymode,
395                                                                       $status->coursemoduleid,
396                                                                       $status->returnaction,
397                                                                       $status->returnparams);
398                 $cell2 = new html_table_cell($this->render($pluginfeedback));
399                 $row->cells = array($cell1, $cell2);
400                 $t->data[] = $row;
401             }
402         }
404         $o .= html_writer::table($t);
405         $o .= $this->output->box_end();
407         $o .= $this->output->container_end();
408         return $o;
409     }
411     /**
412      * Render a table containing the current status of the submission.
413      *
414      * @param assign_submission_status $status
415      * @return string
416      */
417     public function render_assign_submission_status(assign_submission_status $status) {
418         $o = '';
419         $o .= $this->output->container_start('submissionstatustable');
420         $o .= $this->output->heading(get_string('submissionstatusheading', 'assign'), 3);
421         $time = time();
423         if ($status->allowsubmissionsfromdate &&
424                 $time <= $status->allowsubmissionsfromdate) {
425             $o .= $this->output->box_start('generalbox boxaligncenter submissionsalloweddates');
426             if ($status->alwaysshowdescription) {
427                 $date = userdate($status->allowsubmissionsfromdate);
428                 $o .= get_string('allowsubmissionsfromdatesummary', 'assign', $date);
429             } else {
430                 $date = userdate($status->allowsubmissionsfromdate);
431                 $o .= get_string('allowsubmissionsanddescriptionfromdatesummary', 'assign', $date);
432             }
433             $o .= $this->output->box_end();
434         }
435         $o .= $this->output->box_start('boxaligncenter submissionsummarytable');
437         $t = new html_table();
439         if ($status->teamsubmissionenabled) {
440             $row = new html_table_row();
441             $cell1 = new html_table_cell(get_string('submissionteam', 'assign'));
442             $group = $status->submissiongroup;
443             if ($group) {
444                 $cell2 = new html_table_cell(format_string($group->name, false, $status->context));
445             } else {
446                 $cell2 = new html_table_cell(get_string('defaultteam', 'assign'));
447             }
448             $row->cells = array($cell1, $cell2);
449             $t->data[] = $row;
450         }
452         if ($status->attemptreopenmethod != ASSIGN_ATTEMPT_REOPEN_METHOD_NONE) {
453             $currentattempt = 1;
454             if (!$status->teamsubmissionenabled) {
455                 if ($status->submission) {
456                     $currentattempt = $status->submission->attemptnumber + 1;
457                 }
458             } else {
459                 if ($status->teamsubmission) {
460                     $currentattempt = $status->teamsubmission->attemptnumber + 1;
461                 }
462             }
464             $row = new html_table_row();
465             $cell1 = new html_table_cell(get_string('attemptnumber', 'assign'));
466             $maxattempts = $status->maxattempts;
467             if ($maxattempts == ASSIGN_UNLIMITED_ATTEMPTS) {
468                 $message = get_string('currentattempt', 'assign', $currentattempt);
469             } else {
470                 $message = get_string('currentattemptof', 'assign', array('attemptnumber'=>$currentattempt,
471                                                                           'maxattempts'=>$maxattempts));
472             }
473             $cell2 = new html_table_cell($message);
474             $row->cells = array($cell1, $cell2);
475             $t->data[] = $row;
476         }
478         $row = new html_table_row();
479         $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
480         if (!$status->teamsubmissionenabled) {
481             if ($status->submission) {
482                 $statusstr = get_string('submissionstatus_' . $status->submission->status, 'assign');
483                 $cell2 = new html_table_cell($statusstr);
484                 $cell2->attributes = array('class'=>'submissionstatus' . $status->submission->status);
485             } else {
486                 if (!$status->submissionsenabled) {
487                     $cell2 = new html_table_cell(get_string('noonlinesubmissions', 'assign'));
488                 } else {
489                     $cell2 = new html_table_cell(get_string('noattempt', 'assign'));
490                 }
491             }
492             $row->cells = array($cell1, $cell2);
493             $t->data[] = $row;
494         } else {
495             $row = new html_table_row();
496             $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
497             if ($status->teamsubmission) {
498                 $teamstatus = $status->teamsubmission->status;
499                 $submissionsummary = get_string('submissionstatus_' . $teamstatus, 'assign');
500                 $groupid = 0;
501                 if ($status->submissiongroup) {
502                     $groupid = $status->submissiongroup->id;
503                 }
505                 $members = $status->submissiongroupmemberswhoneedtosubmit;
506                 $userslist = array();
507                 foreach ($members as $member) {
508                     $urlparams = array('id' => $member->id, 'course'=>$status->courseid);
509                     $url = new moodle_url('/user/view.php', $urlparams);
510                     if ($status->view == assign_submission_status::GRADER_VIEW && $status->blindmarking) {
511                         $userslist[] = $member->alias;
512                     } else {
513                         $fullname = fullname($member, $status->canviewfullnames);
514                         $userslist[] = $this->output->action_link($url, $fullname);
515                     }
516                 }
517                 if (count($userslist) > 0) {
518                     $userstr = join(', ', $userslist);
519                     $formatteduserstr = get_string('userswhoneedtosubmit', 'assign', $userstr);
520                     $submissionsummary .= $this->output->container($formatteduserstr);
521                 }
523                 $cell2 = new html_table_cell($submissionsummary);
524                 $cell2->attributes = array('class'=>'submissionstatus' . $status->teamsubmission->status);
525             } else {
526                 $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
527                 if (!$status->submissionsenabled) {
528                     $cell2 = new html_table_cell(get_string('noonlinesubmissions', 'assign'));
529                 } else {
530                     $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
531                 }
532             }
533             $row->cells = array($cell1, $cell2);
534             $t->data[] = $row;
535         }
537         // Is locked?
538         if ($status->locked) {
539             $row = new html_table_row();
540             $cell1 = new html_table_cell();
541             $cell2 = new html_table_cell(get_string('submissionslocked', 'assign'));
542             $cell2->attributes = array('class'=>'submissionlocked');
543             $row->cells = array($cell1, $cell2);
544             $t->data[] = $row;
545         }
547         // Grading status.
548         $row = new html_table_row();
549         $cell1 = new html_table_cell(get_string('gradingstatus', 'assign'));
551         if ($status->graded) {
552             $cell2 = new html_table_cell(get_string('graded', 'assign'));
553             $cell2->attributes = array('class'=>'submissiongraded');
554         } else {
555             $cell2 = new html_table_cell(get_string('notgraded', 'assign'));
556             $cell2->attributes = array('class'=>'submissionnotgraded');
557         }
558         $row->cells = array($cell1, $cell2);
559         $t->data[] = $row;
561         $submission = $status->teamsubmission ? $status->teamsubmission : $status->submission;
562         $duedate = $status->duedate;
563         if ($duedate > 0) {
564             // Due date.
565             $row = new html_table_row();
566             $cell1 = new html_table_cell(get_string('duedate', 'assign'));
567             $cell2 = new html_table_cell(userdate($duedate));
568             $row->cells = array($cell1, $cell2);
569             $t->data[] = $row;
571             if ($status->view == assign_submission_status::GRADER_VIEW) {
572                 if ($status->cutoffdate) {
573                     // Cut off date.
574                     $row = new html_table_row();
575                     $cell1 = new html_table_cell(get_string('cutoffdate', 'assign'));
576                     $cell2 = new html_table_cell(userdate($status->cutoffdate));
577                     $row->cells = array($cell1, $cell2);
578                     $t->data[] = $row;
579                 }
580             }
582             if ($status->extensionduedate) {
583                 // Extension date.
584                 $row = new html_table_row();
585                 $cell1 = new html_table_cell(get_string('extensionduedate', 'assign'));
586                 $cell2 = new html_table_cell(userdate($status->extensionduedate));
587                 $row->cells = array($cell1, $cell2);
588                 $t->data[] = $row;
589                 $duedate = $status->extensionduedate;
590             }
592             // Time remaining.
593             $row = new html_table_row();
594             $cell1 = new html_table_cell(get_string('timeremaining', 'assign'));
595             if ($duedate - $time <= 0) {
596                 if (!$submission ||
597                         $submission->status != ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
598                     if ($status->submissionsenabled) {
599                         $overduestr = get_string('overdue', 'assign', format_time($time - $duedate));
600                         $cell2 = new html_table_cell($overduestr);
601                         $cell2->attributes = array('class'=>'overdue');
602                     } else {
603                         $cell2 = new html_table_cell(get_string('duedatereached', 'assign'));
604                     }
605                 } else {
606                     if ($submission->timemodified > $duedate) {
607                         $latestr = get_string('submittedlate',
608                                               'assign',
609                                               format_time($submission->timemodified - $duedate));
610                         $cell2 = new html_table_cell($latestr);
611                         $cell2->attributes = array('class'=>'latesubmission');
612                     } else {
613                         $earlystr = get_string('submittedearly',
614                                                'assign',
615                                                format_time($submission->timemodified - $duedate));
616                         $cell2 = new html_table_cell($earlystr);
617                         $cell2->attributes = array('class'=>'earlysubmission');
618                     }
619                 }
620             } else {
621                 $cell2 = new html_table_cell(format_time($duedate - $time));
622             }
623             $row->cells = array($cell1, $cell2);
624             $t->data[] = $row;
625         }
627         // Show graders whether this submission is editable by students.
628         if ($status->view == assign_submission_status::GRADER_VIEW) {
629             $row = new html_table_row();
630             $cell1 = new html_table_cell(get_string('editingstatus', 'assign'));
631             if ($status->canedit) {
632                 $cell2 = new html_table_cell(get_string('submissioneditable', 'assign'));
633                 $cell2->attributes = array('class'=>'submissioneditable');
634             } else {
635                 $cell2 = new html_table_cell(get_string('submissionnoteditable', 'assign'));
636                 $cell2->attributes = array('class'=>'submissionnoteditable');
637             }
638             $row->cells = array($cell1, $cell2);
639             $t->data[] = $row;
640         }
642         // Grading criteria preview.
643         if (!empty($status->gradingcontrollerpreview)) {
644             $row = new html_table_row();
645             $cell1 = new html_table_cell(get_string('gradingmethodpreview', 'assign'));
646             $cell2 = new html_table_cell($status->gradingcontrollerpreview);
647             $row->cells = array($cell1, $cell2);
648             $t->data[] = $row;
649         }
651         // Last modified.
652         if ($submission) {
653             $row = new html_table_row();
654             $cell1 = new html_table_cell(get_string('timemodified', 'assign'));
655             $cell2 = new html_table_cell(userdate($submission->timemodified));
656             $row->cells = array($cell1, $cell2);
657             $t->data[] = $row;
659             foreach ($status->submissionplugins as $plugin) {
660                 $pluginshowsummary = !$plugin->is_empty($submission) || !$plugin->allow_submissions();
661                 if ($plugin->is_enabled() &&
662                     $plugin->is_visible() &&
663                     $plugin->has_user_summary() &&
664                     $pluginshowsummary) {
666                     $row = new html_table_row();
667                     $cell1 = new html_table_cell($plugin->get_name());
668                     $displaymode = assign_submission_plugin_submission::SUMMARY;
669                     $pluginsubmission = new assign_submission_plugin_submission($plugin,
670                                                                                 $submission,
671                                                                                 $displaymode,
672                                                                                 $status->coursemoduleid,
673                                                                                 $status->returnaction,
674                                                                                 $status->returnparams);
675                     $cell2 = new html_table_cell($this->render($pluginsubmission));
676                     $row->cells = array($cell1, $cell2);
677                     $t->data[] = $row;
678                 }
679             }
680         }
682         $o .= html_writer::table($t);
683         $o .= $this->output->box_end();
685         // Links.
686         if ($status->view == assign_submission_status::STUDENT_VIEW) {
687             if ($status->canedit) {
688                 if (!$submission) {
689                     $o .= $this->output->box_start('generalbox submissionaction');
690                     $urlparams = array('id' => $status->coursemoduleid, 'action' => 'editsubmission');
691                     $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
692                                                        get_string('addsubmission', 'assign'), 'get');
693                     $o .= $this->output->box_start('boxaligncenter submithelp');
694                     $o .= get_string('editsubmission_help', 'assign');
695                     $o .= $this->output->box_end();
696                     $o .= $this->output->box_end();
697                 } else if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
698                     $o .= $this->output->box_start('generalbox submissionaction');
699                     $urlparams = array('id' => $status->coursemoduleid,
700                                        'action' => 'editprevioussubmission',
701                                        'sesskey'=>sesskey());
702                     $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
703                                                        get_string('addnewattemptfromprevious', 'assign'), 'get');
704                     $o .= $this->output->box_start('boxaligncenter submithelp');
705                     $o .= get_string('addnewattemptfromprevious_help', 'assign');
706                     $o .= $this->output->box_end();
707                     $o .= $this->output->box_end();
708                     $o .= $this->output->box_start('generalbox submissionaction');
709                     $urlparams = array('id' => $status->coursemoduleid, 'action' => 'editsubmission');
710                     $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
711                                                        get_string('addnewattempt', 'assign'), 'get');
712                     $o .= $this->output->box_start('boxaligncenter submithelp');
713                     $o .= get_string('addnewattempt_help', 'assign');
714                     $o .= $this->output->box_end();
715                     $o .= $this->output->box_end();
716                 } else {
717                     $o .= $this->output->box_start('generalbox submissionaction');
718                     $urlparams = array('id' => $status->coursemoduleid, 'action' => 'editsubmission');
719                     $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
720                                                        get_string('editsubmission', 'assign'), 'get');
721                     $o .= $this->output->box_start('boxaligncenter submithelp');
722                     $o .= get_string('editsubmission_help', 'assign');
723                     $o .= $this->output->box_end();
724                     $o .= $this->output->box_end();
725                 }
726             }
728             if ($status->cansubmit) {
729                 $urlparams = array('id' => $status->coursemoduleid, 'action'=>'submit');
730                 $o .= $this->output->box_start('generalbox submissionaction');
731                 $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
732                                                    get_string('submitassignment', 'assign'), 'get');
733                 $o .= $this->output->box_start('boxaligncenter submithelp');
734                 $o .= get_string('submitassignment_help', 'assign');
735                 $o .= $this->output->box_end();
736                 $o .= $this->output->box_end();
737             }
738         }
740         $o .= $this->output->container_end();
741         return $o;
742     }
744     /**
745      * Output the attempt history for this assignment
746      *
747      * @param assign_attempt_history $history
748      * @return string
749      */
750     public function render_assign_attempt_history(assign_attempt_history $history) {
751         $o = '';
753         $submittedstr = get_string('submitted', 'assign');
754         $gradestr = get_string('grade');
755         $gradedonstr = get_string('gradedon', 'assign');
756         $gradedbystr = get_string('gradedby', 'assign');
758         // Don't show the last one because it is the current submission.
759         array_pop($history->submissions);
761         // Show newest to oldest.
762         $history->submissions = array_reverse($history->submissions);
764         if (empty($history->submissions)) {
765             return '';
766         }
768         $containerid = 'attempthistory' . uniqid();
769         $o .= $this->heading(get_string('attempthistory', 'assign'), 3);
770         $o .= $this->box_start('attempthistory', $containerid);
772         foreach ($history->submissions as $i => $submission) {
773             $grade = null;
774             foreach ($history->grades as $onegrade) {
775                 if ($onegrade->attemptnumber == $submission->attemptnumber) {
776                     $grade = $onegrade;
777                     break;
778                 }
779             }
781             $editbtn = '';
783             if ($submission) {
784                 $submissionsummary = userdate($submission->timemodified);
785             } else {
786                 $submissionsummary = get_string('nosubmission', 'assign');
787             }
789             $attemptsummaryparams = array('attemptnumber'=>$submission->attemptnumber+1,
790                                           'submissionsummary'=>$submissionsummary);
791             $o .= $this->heading(get_string('attemptheading', 'assign', $attemptsummaryparams), 4);
793             $t = new html_table();
795             if ($submission) {
796                 $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
797                 $cell2 = new html_table_cell(get_string('submissionstatus_' . $submission->status, 'assign'));
798                 $t->data[] = new html_table_row(array($cell1, $cell2));
800                 foreach ($history->submissionplugins as $plugin) {
801                     $pluginshowsummary = !$plugin->is_empty($submission) || !$plugin->allow_submissions();
802                     if ($plugin->is_enabled() &&
803                             $plugin->is_visible() &&
804                             $plugin->has_user_summary() &&
805                             $pluginshowsummary) {
807                         $cell1 = new html_table_cell($plugin->get_name());
808                         $pluginsubmission = new assign_submission_plugin_submission($plugin,
809                                                                                     $submission,
810                                                                                     assign_submission_plugin_submission::SUMMARY,
811                                                                                     $history->coursemoduleid,
812                                                                                     $history->returnaction,
813                                                                                     $history->returnparams);
814                         $cell2 = new html_table_cell($this->render($pluginsubmission));
816                         $t->data[] = new html_table_row(array($cell1, $cell2));
817                     }
818                 }
819             }
821             if ($grade) {
822                 // Heading 'feedback'.
823                 $title = get_string('feedback', 'assign', $i);
824                 $title .= $this->output->spacer(array('width'=>10));
825                 if ($history->cangrade) {
826                     // Edit previous feedback.
827                     $returnparams = http_build_query($history->returnparams);
828                     $urlparams = array('id' => $history->coursemoduleid,
829                                    'rownum'=>$history->rownum,
830                                    'useridlistid'=>$history->useridlistid,
831                                    'attemptnumber'=>$grade->attemptnumber,
832                                    'action'=>'grade',
833                                    'returnaction'=>$history->returnaction,
834                                    'returnparams'=>$returnparams);
835                     $url = new moodle_url('/mod/assign/view.php', $urlparams);
836                     $icon = new pix_icon('gradefeedback',
837                                             get_string('editattemptfeedback', 'assign', $grade->attemptnumber+1),
838                                             'mod_assign');
839                     $title .= $this->output->action_icon($url, $icon);
840                 }
841                 $cell = new html_table_cell($title);
842                 $cell->attributes['class'] = 'feedbacktitle';
843                 $cell->colspan = 2;
844                 $t->data[] = new html_table_row(array($cell));
846                 // Grade.
847                 $cell1 = new html_table_cell($gradestr);
848                 $cell2 = $grade->gradefordisplay;
849                 $t->data[] = new html_table_row(array($cell1, $cell2));
851                 // Graded on.
852                 $cell1 = new html_table_cell($gradedonstr);
853                 $cell2 = new html_table_cell(userdate($grade->timemodified));
854                 $t->data[] = new html_table_row(array($cell1, $cell2));
856                 // Graded by.
857                 $cell1 = new html_table_cell($gradedbystr);
858                 $cell2 = new html_table_cell($this->output->user_picture($grade->grader) .
859                                              $this->output->spacer(array('width'=>30)) . fullname($grade->grader));
860                 $t->data[] = new html_table_row(array($cell1, $cell2));
862                 // Feedback from plugins.
863                 foreach ($history->feedbackplugins as $plugin) {
864                     if ($plugin->is_enabled() &&
865                         $plugin->is_visible() &&
866                         $plugin->has_user_summary() &&
867                         !$plugin->is_empty($grade)) {
869                         $cell1 = new html_table_cell($plugin->get_name());
870                         $pluginfeedback = new assign_feedback_plugin_feedback(
871                             $plugin, $grade, assign_feedback_plugin_feedback::SUMMARY, $history->coursemoduleid,
872                             $history->returnaction, $history->returnparams
873                         );
874                         $cell2 = new html_table_cell($this->render($pluginfeedback));
875                         $t->data[] = new html_table_row(array($cell1, $cell2));
876                     }
878                 }
880             }
882             $o .= html_writer::table($t);
883         }
884         $o .= $this->box_end();
885         $jsparams = array($containerid);
887         $this->page->requires->yui_module('moodle-mod_assign-history', 'Y.one("#' . $containerid . '").history');
889         return $o;
890     }
892     /**
893      * Render a submission plugin submission
894      *
895      * @param assign_submission_plugin_submission $submissionplugin
896      * @return string
897      */
898     public function render_assign_submission_plugin_submission(assign_submission_plugin_submission $submissionplugin) {
899         $o = '';
901         if ($submissionplugin->view == assign_submission_plugin_submission::SUMMARY) {
902             $showviewlink = false;
903             $summary = $submissionplugin->plugin->view_summary($submissionplugin->submission,
904                                                                $showviewlink);
906             $classsuffix = $submissionplugin->plugin->get_subtype() .
907                            '_' .
908                            $submissionplugin->plugin->get_type() .
909                            '_' .
910                            $submissionplugin->submission->id;
912             $o .= $this->output->box_start('boxaligncenter plugincontentsummary summary_' . $classsuffix);
914             $link = '';
915             if ($showviewlink) {
916                 $previewstr = get_string('viewsubmission', 'assign');
917                 $icon = $this->output->pix_icon('t/preview', $previewstr);
919                 $expandstr = get_string('viewfull', 'assign');
920                 $options = array('class'=>'expandsummaryicon expand_' . $classsuffix);
921                 $o .= $this->output->pix_icon('t/switch_plus', $expandstr, null, $options);
923                 $jsparams = array($submissionplugin->plugin->get_subtype(),
924                                   $submissionplugin->plugin->get_type(),
925                                   $submissionplugin->submission->id);
927                 $this->page->requires->js_init_call('M.mod_assign.init_plugin_summary', $jsparams);
929                 $action = 'viewplugin' . $submissionplugin->plugin->get_subtype();
930                 $returnparams = http_build_query($submissionplugin->returnparams);
931                 $link .= '<noscript>';
932                 $urlparams = array('id' => $submissionplugin->coursemoduleid,
933                                    'sid'=>$submissionplugin->submission->id,
934                                    'plugin'=>$submissionplugin->plugin->get_type(),
935                                    'action'=>$action,
936                                    'returnaction'=>$submissionplugin->returnaction,
937                                    'returnparams'=>$returnparams);
938                 $url = new moodle_url('/mod/assign/view.php', $urlparams);
939                 $link .= $this->output->action_link($url, $icon);
940                 $link .= '</noscript>';
942                 $link .= $this->output->spacer(array('width'=>15));
943             }
945             $o .= $link . $summary;
946             $o .= $this->output->box_end();
947             if ($showviewlink) {
948                 $o .= $this->output->box_start('boxaligncenter hidefull full_' . $classsuffix);
949                 $classes = 'expandsummaryicon contract_' . $classsuffix;
950                 $o .= $this->output->pix_icon('t/switch_minus',
951                                               get_string('viewsummary', 'assign'),
952                                               null,
953                                               array('class'=>$classes));
954                 $o .= $submissionplugin->plugin->view($submissionplugin->submission);
955                 $o .= $this->output->box_end();
956             }
957         } else if ($submissionplugin->view == assign_submission_plugin_submission::FULL) {
958             $o .= $this->output->box_start('boxaligncenter submissionfull');
959             $o .= $submissionplugin->plugin->view($submissionplugin->submission);
960             $o .= $this->output->box_end();
961         }
963         return $o;
964     }
966     /**
967      * Render the grading table.
968      *
969      * @param assign_grading_table $table
970      * @return string
971      */
972     public function render_assign_grading_table(assign_grading_table $table) {
973         $o = '';
974         $o .= $this->output->box_start('boxaligncenter gradingtable');
976         $this->page->requires->js_init_call('M.mod_assign.init_grading_table', array());
977         $this->page->requires->string_for_js('nousersselected', 'assign');
978         $this->page->requires->string_for_js('batchoperationconfirmgrantextension', 'assign');
979         $this->page->requires->string_for_js('batchoperationconfirmlock', 'assign');
980         $this->page->requires->string_for_js('batchoperationconfirmreverttodraft', 'assign');
981         $this->page->requires->string_for_js('batchoperationconfirmunlock', 'assign');
982         $this->page->requires->string_for_js('batchoperationconfirmaddattempt', 'assign');
983         $this->page->requires->string_for_js('batchoperationconfirmsetmarkingworkflowstate', 'assign');
984         $this->page->requires->string_for_js('batchoperationconfirmsetmarkingallocation', 'assign');
985         $this->page->requires->string_for_js('editaction', 'assign');
986         foreach ($table->plugingradingbatchoperations as $plugin => $operations) {
987             foreach ($operations as $operation => $description) {
988                 $this->page->requires->string_for_js('batchoperationconfirm' . $operation,
989                                                      'assignfeedback_' . $plugin);
990             }
991         }
992         $o .= $this->flexible_table($table, $table->get_rows_per_page(), true);
993         $o .= $this->output->box_end();
995         return $o;
996     }
998     /**
999      * Render a feedback plugin feedback
1000      *
1001      * @param assign_feedback_plugin_feedback $feedbackplugin
1002      * @return string
1003      */
1004     public function render_assign_feedback_plugin_feedback(assign_feedback_plugin_feedback $feedbackplugin) {
1005         $o = '';
1007         if ($feedbackplugin->view == assign_feedback_plugin_feedback::SUMMARY) {
1008             $showviewlink = false;
1009             $summary = $feedbackplugin->plugin->view_summary($feedbackplugin->grade, $showviewlink);
1011             $classsuffix = $feedbackplugin->plugin->get_subtype() .
1012                            '_' .
1013                            $feedbackplugin->plugin->get_type() .
1014                            '_' .
1015                            $feedbackplugin->grade->id;
1016             $o .= $this->output->box_start('boxaligncenter plugincontentsummary summary_' . $classsuffix);
1018             $link = '';
1019             if ($showviewlink) {
1020                 $previewstr = get_string('viewfeedback', 'assign');
1021                 $icon = $this->output->pix_icon('t/preview', $previewstr);
1023                 $expandstr = get_string('viewfull', 'assign');
1024                 $options = array('class'=>'expandsummaryicon expand_' . $classsuffix);
1025                 $o .= $this->output->pix_icon('t/switch_plus', $expandstr, null, $options);
1027                 $jsparams = array($feedbackplugin->plugin->get_subtype(),
1028                                   $feedbackplugin->plugin->get_type(),
1029                                   $feedbackplugin->grade->id);
1030                 $this->page->requires->js_init_call('M.mod_assign.init_plugin_summary', $jsparams);
1032                 $urlparams = array('id' => $feedbackplugin->coursemoduleid,
1033                                    'gid'=>$feedbackplugin->grade->id,
1034                                    'plugin'=>$feedbackplugin->plugin->get_type(),
1035                                    'action'=>'viewplugin' . $feedbackplugin->plugin->get_subtype(),
1036                                    'returnaction'=>$feedbackplugin->returnaction,
1037                                    'returnparams'=>http_build_query($feedbackplugin->returnparams));
1038                 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1039                 $link .= '<noscript>';
1040                 $link .= $this->output->action_link($url, $icon);
1041                 $link .= '</noscript>';
1043                 $link .= $this->output->spacer(array('width'=>15));
1044             }
1046             $o .= $link . $summary;
1047             $o .= $this->output->box_end();
1048             if ($showviewlink) {
1049                 $o .= $this->output->box_start('boxaligncenter hidefull full_' . $classsuffix);
1050                 $classes = 'expandsummaryicon contract_' . $classsuffix;
1051                 $o .= $this->output->pix_icon('t/switch_minus',
1052                                               get_string('viewsummary', 'assign'),
1053                                               null,
1054                                               array('class'=>$classes));
1055                 $o .= $feedbackplugin->plugin->view($feedbackplugin->grade);
1056                 $o .= $this->output->box_end();
1057             }
1058         } else if ($feedbackplugin->view == assign_feedback_plugin_feedback::FULL) {
1059             $o .= $this->output->box_start('boxaligncenter feedbackfull');
1060             $o .= $feedbackplugin->plugin->view($feedbackplugin->grade);
1061             $o .= $this->output->box_end();
1062         }
1064         return $o;
1065     }
1067     /**
1068      * Render a course index summary
1069      *
1070      * @param assign_course_index_summary $indexsummary
1071      * @return string
1072      */
1073     public function render_assign_course_index_summary(assign_course_index_summary $indexsummary) {
1074         $o = '';
1076         $strplural = get_string('modulenameplural', 'assign');
1077         $strsectionname  = $indexsummary->courseformatname;
1078         $strduedate = get_string('duedate', 'assign');
1079         $strsubmission = get_string('submission', 'assign');
1080         $strgrade = get_string('grade');
1082         $table = new html_table();
1083         if ($indexsummary->usesections) {
1084             $table->head  = array ($strsectionname, $strplural, $strduedate, $strsubmission, $strgrade);
1085             $table->align = array ('left', 'left', 'center', 'right', 'right');
1086         } else {
1087             $table->head  = array ($strplural, $strduedate, $strsubmission, $strgrade);
1088             $table->align = array ('left', 'left', 'center', 'right');
1089         }
1090         $table->data = array();
1092         $currentsection = '';
1093         foreach ($indexsummary->assignments as $info) {
1094             $params = array('id' => $info['cmid']);
1095             $link = html_writer::link(new moodle_url('/mod/assign/view.php', $params),
1096                                       $info['cmname']);
1097             $due = $info['timedue'] ? userdate($info['timedue']) : '-';
1099             $printsection = '';
1100             if ($indexsummary->usesections) {
1101                 if ($info['sectionname'] !== $currentsection) {
1102                     if ($info['sectionname']) {
1103                         $printsection = $info['sectionname'];
1104                     }
1105                     if ($currentsection !== '') {
1106                         $table->data[] = 'hr';
1107                     }
1108                     $currentsection = $info['sectionname'];
1109                 }
1110             }
1112             if ($indexsummary->usesections) {
1113                 $row = array($printsection, $link, $due, $info['submissioninfo'], $info['gradeinfo']);
1114             } else {
1115                 $row = array($link, $due, $info['submissioninfo'], $info['gradeinfo']);
1116             }
1117             $table->data[] = $row;
1118         }
1120         $o .= html_writer::table($table);
1122         return $o;
1123     }
1127     /**
1128      * Internal function - creates htmls structure suitable for YUI tree.
1129      *
1130      * @param assign_files $tree
1131      * @param array $dir
1132      * @return string
1133      */
1134     protected function htmllize_tree(assign_files $tree, $dir) {
1135         global $CFG;
1136         $yuiconfig = array();
1137         $yuiconfig['type'] = 'html';
1139         if (empty($dir['subdirs']) and empty($dir['files'])) {
1140             return '';
1141         }
1143         $result = '<ul>';
1144         foreach ($dir['subdirs'] as $subdir) {
1145             $image = $this->output->pix_icon(file_folder_icon(),
1146                                              $subdir['dirname'],
1147                                              'moodle',
1148                                              array('class'=>'icon'));
1149             $result .= '<li yuiConfig=\'' . json_encode($yuiconfig) . '\'>' .
1150                        '<div>' . $image . ' ' . s($subdir['dirname']) . '</div> ' .
1151                        $this->htmllize_tree($tree, $subdir) .
1152                        '</li>';
1153         }
1155         foreach ($dir['files'] as $file) {
1156             $filename = $file->get_filename();
1157             if ($CFG->enableplagiarism) {
1158                 require_once($CFG->libdir.'/plagiarismlib.php');
1159                 $plagiarismlinks = plagiarism_get_links(array('userid'=>$file->get_userid(),
1160                                                              'file'=>$file,
1161                                                              'cmid'=>$tree->cm->id,
1162                                                              'course'=>$tree->course));
1163             } else {
1164                 $plagiarismlinks = '';
1165             }
1166             $image = $this->output->pix_icon(file_file_icon($file),
1167                                              $filename,
1168                                              'moodle',
1169                                              array('class'=>'icon'));
1170             $result .= '<li yuiConfig=\'' . json_encode($yuiconfig) . '\'>' .
1171                        '<div>' . $image . ' ' .
1172                                  $file->fileurl . ' ' .
1173                                  $plagiarismlinks .
1174                                  $file->portfoliobutton . '</div>' .
1175                        '</li>';
1176         }
1178         $result .= '</ul>';
1180         return $result;
1181     }
1183     /**
1184      * Helper method dealing with the fact we can not just fetch the output of flexible_table
1185      *
1186      * @param flexible_table $table The table to render
1187      * @param int $rowsperpage How many assignments to render in a page
1188      * @param bool $displaylinks - Whether to render links in the table
1189      *                             (e.g. downloads would not enable this)
1190      * @return string HTML
1191      */
1192     protected function flexible_table(flexible_table $table, $rowsperpage, $displaylinks) {
1194         $o = '';
1195         ob_start();
1196         $table->out($rowsperpage, $displaylinks);
1197         $o = ob_get_contents();
1198         ob_end_clean();
1200         return $o;
1201     }
1203     /**
1204      * Helper method dealing with the fact we can not just fetch the output of moodleforms
1205      *
1206      * @param moodleform $mform
1207      * @return string HTML
1208      */
1209     protected function moodleform(moodleform $mform) {
1211         $o = '';
1212         ob_start();
1213         $mform->display();
1214         $o = ob_get_contents();
1215         ob_end_clean();
1217         return $o;
1218     }