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