MDL-40479 mod_assign PHPDoc: Fix error, improve consistency.
[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);
95         $classes = $result->gradingerror ? 'notifyproblem' : 'notifysuccess';
97         $o = '';
98         $o .= $this->output->heading($result->heading, 4);
99         $o .= $this->output->notification($result->message, $classes);
100         $o .= $this->output->continue_button($url);
101         return $o;
102     }
104     /**
105      * Render the generic form
106      * @param assign_form $form The form to render
107      * @return string
108      */
109     public function render_assign_form(assign_form $form) {
110         $o = '';
111         if ($form->jsinitfunction) {
112             $this->page->requires->js_init_call($form->jsinitfunction, array());
113         }
114         $o .= $this->output->box_start('boxaligncenter ' . $form->classname);
115         $o .= $this->moodleform($form->form);
116         $o .= $this->output->box_end();
117         return $o;
118     }
120     /**
121      * Render the user summary
122      *
123      * @param assign_user_summary $summary The user summary to render
124      * @return string
125      */
126     public function render_assign_user_summary(assign_user_summary $summary) {
127         $o = '';
128         $supendedclass = '';
129         $suspendedicon = '';
131         if (!$summary->user) {
132             return;
133         }
135         if ($summary->suspendeduser) {
136             $supendedclass = ' usersuspended';
137             $suspendedstring = get_string('userenrolmentsuspended', 'grades');
138             $suspendedicon = ' ' . html_writer::empty_tag('img', array('src' => $this->pix_url('i/enrolmentsuspended'),
139                 'title' => $suspendedstring, 'alt' => $suspendedstring, 'class' => 'usersuspendedicon'));
140         }
141         $o .= $this->output->container_start('usersummary');
142         $o .= $this->output->box_start('boxaligncenter usersummarysection'.$supendedclass);
143         if ($summary->blindmarking) {
144             $o .= get_string('hiddenuser', 'assign') . $summary->uniqueidforuser.$suspendedicon;
145         } else {
146             $o .= $this->output->user_picture($summary->user);
147             $o .= $this->output->spacer(array('width'=>30));
148             $urlparams = array('id' => $summary->user->id, 'course'=>$summary->courseid);
149             $url = new moodle_url('/user/view.php', $urlparams);
150             $fullname = fullname($summary->user, $summary->viewfullnames);
151             $extrainfo = array();
152             foreach ($summary->extrauserfields as $extrafield) {
153                 $extrainfo[] = $summary->user->$extrafield;
154             }
155             if (count($extrainfo)) {
156                 $fullname .= ' (' . implode(', ', $extrainfo) . ')';
157             }
158             $fullname .= $suspendedicon;
159             $o .= $this->output->action_link($url, $fullname);
160         }
161         $o .= $this->output->box_end();
162         $o .= $this->output->container_end();
164         return $o;
165     }
167     /**
168      * Render the submit for grading page
169      *
170      * @param assign_submit_for_grading_page $page
171      * @return string
172      */
173     public function render_assign_submit_for_grading_page($page) {
174         $o = '';
176         $o .= $this->output->container_start('submitforgrading');
177         $o .= $this->output->heading(get_string('submitassignment', 'assign'), 3);
178         $o .= $this->output->spacer(array('height'=>30));
180         $cancelurl = new moodle_url('/mod/assign/view.php', array('id' => $page->coursemoduleid));
181         if (count($page->notifications)) {
182             // At least one of the submission plugins is not ready for submission.
184             $o .= $this->output->heading(get_string('submissionnotready', 'assign'), 4);
186             foreach ($page->notifications as $notification) {
187                 $o .= $this->output->notification($notification);
188             }
190             $o .= $this->output->continue_button($cancelurl);
191         } else {
192             // All submission plugins ready - show the confirmation form.
193             $o .= $this->output->box_start('generalbox submitconfirm');
194             $o .= $this->moodleform($page->confirmform);
195             $o .= $this->output->box_end();
196         }
197         $o .= $this->output->container_end();
199         return $o;
200     }
202     /**
203      * Page is done - render the footer.
204      *
205      * @return void
206      */
207     public function render_footer() {
208         return $this->output->footer();
209     }
211     /**
212      * Render the header.
213      *
214      * @param assign_header $header
215      * @return string
216      */
217     public function render_assign_header(assign_header $header) {
218         $o = '';
220         if ($header->subpage) {
221             $this->page->navbar->add($header->subpage);
222         }
224         $this->page->set_title(get_string('pluginname', 'assign'));
225         $this->page->set_heading($this->page->course->fullname);
227         $o .= $this->output->header();
228         $heading = format_string($header->assign->name, false, array('context' => $header->context));
229         $o .= $this->output->heading($heading);
230         if ($header->preface) {
231             $o .= $header->preface;
232         }
234         if ($header->showintro) {
235             $o .= $this->output->box_start('generalbox boxaligncenter', 'intro');
236             $o .= format_module_intro('assign', $header->assign, $header->coursemoduleid);
237             $o .= $this->output->box_end();
238         }
240         return $o;
241     }
243     /**
244      * Render the header for an individual plugin.
245      *
246      * @param assign_plugin_header $header
247      * @return string
248      */
249     public function render_assign_plugin_header(assign_plugin_header $header) {
250         $o = $header->plugin->view_header();
251         return $o;
252     }
254     /**
255      * Render a table containing the current status of the grading process.
256      *
257      * @param assign_grading_summary $summary
258      * @return string
259      */
260     public function render_assign_grading_summary(assign_grading_summary $summary) {
261         // Create a table for the data.
262         $o = '';
263         $o .= $this->output->container_start('gradingsummary');
264         $o .= $this->output->heading(get_string('gradingsummary', 'assign'), 3);
265         $o .= $this->output->box_start('boxaligncenter gradingsummarytable');
266         $t = new html_table();
268         // Status.
269         if ($summary->teamsubmission) {
270             $this->add_table_row_tuple($t, get_string('numberofteams', 'assign'),
271                                        $summary->participantcount);
272         } else {
273             $this->add_table_row_tuple($t, get_string('numberofparticipants', 'assign'),
274                                        $summary->participantcount);
275         }
277         // Drafts count and dont show drafts count when using offline assignment.
278         if ($summary->submissiondraftsenabled && $summary->submissionsenabled) {
279             $this->add_table_row_tuple($t, get_string('numberofdraftsubmissions', 'assign'),
280                                        $summary->submissiondraftscount);
281         }
283         // Submitted for grading.
284         if ($summary->submissionsenabled) {
285             $this->add_table_row_tuple($t, get_string('numberofsubmittedassignments', 'assign'),
286                                        $summary->submissionssubmittedcount);
287             if (!$summary->teamsubmission) {
288                 $this->add_table_row_tuple($t, get_string('numberofsubmissionsneedgrading', 'assign'),
289                                            $summary->submissionsneedgradingcount);
290             }
291         }
293         $time = time();
294         if ($summary->duedate) {
295             // Due date.
296             $duedate = $summary->duedate;
297             $this->add_table_row_tuple($t, get_string('duedate', 'assign'),
298                                        userdate($duedate));
300             // Time remaining.
301             $due = '';
302             if ($duedate - $time <= 0) {
303                 $due = get_string('assignmentisdue', 'assign');
304             } else {
305                 $due = format_time($duedate - $time);
306             }
307             $this->add_table_row_tuple($t, get_string('timeremaining', 'assign'), $due);
309             if ($duedate < $time) {
310                 $cutoffdate = $summary->cutoffdate;
311                 if ($cutoffdate) {
312                     if ($cutoffdate > $time) {
313                         $late = get_string('latesubmissionsaccepted', 'assign');
314                     } else {
315                         $late = get_string('nomoresubmissionsaccepted', 'assign');
316                     }
317                     $this->add_table_row_tuple($t, get_string('latesubmissions', 'assign'), $late);
318                 }
319             }
321         }
323         // All done - write the table.
324         $o .= html_writer::table($t);
325         $o .= $this->output->box_end();
327         // Link to the grading page.
328         $o .= $this->output->container_start('submissionlinks');
329         $urlparams = array('id' => $summary->coursemoduleid, 'action'=>'grading');
330         $url = new moodle_url('/mod/assign/view.php', $urlparams);
331         $o .= $this->output->action_link($url, get_string('viewgrading', 'assign'));
332         $o .= $this->output->container_end();
334         // Close the container and insert a spacer.
335         $o .= $this->output->container_end();
337         return $o;
338     }
340     /**
341      * Render a table containing all the current grades and feedback.
342      *
343      * @param assign_feedback_status $status
344      * @return string
345      */
346     public function render_assign_feedback_status(assign_feedback_status $status) {
347         global $DB, $CFG;
348         $o = '';
350         $o .= $this->output->container_start('feedback');
351         $o .= $this->output->heading(get_string('feedback', 'assign'), 3);
352         $o .= $this->output->box_start('boxaligncenter feedbacktable');
353         $t = new html_table();
355         // Grade.
356         if (isset($status->gradefordisplay)) {
357             $row = new html_table_row();
358             $cell1 = new html_table_cell(get_string('grade'));
359             $cell2 = new html_table_cell($status->gradefordisplay);
360             $row->cells = array($cell1, $cell2);
361             $t->data[] = $row;
363             // Grade date.
364             $row = new html_table_row();
365             $cell1 = new html_table_cell(get_string('gradedon', 'assign'));
366             $cell2 = new html_table_cell(userdate($status->gradeddate));
367             $row->cells = array($cell1, $cell2);
368             $t->data[] = $row;
369         }
371         if ($status->grader) {
372             // Grader.
373             $row = new html_table_row();
374             $cell1 = new html_table_cell(get_string('gradedby', 'assign'));
375             $userdescription = $this->output->user_picture($status->grader) .
376                                $this->output->spacer(array('width'=>30)) .
377                                fullname($status->grader);
378             $cell2 = new html_table_cell($userdescription);
379             $row->cells = array($cell1, $cell2);
380             $t->data[] = $row;
381         }
383         foreach ($status->feedbackplugins as $plugin) {
384             if ($plugin->is_enabled() &&
385                     $plugin->is_visible() &&
386                     $plugin->has_user_summary() &&
387                     !empty($status->grade) &&
388                     !$plugin->is_empty($status->grade)) {
390                 $row = new html_table_row();
391                 $cell1 = new html_table_cell($plugin->get_name());
392                 $displaymode = assign_feedback_plugin_feedback::SUMMARY;
393                 $pluginfeedback = new assign_feedback_plugin_feedback($plugin,
394                                                                       $status->grade,
395                                                                       $displaymode,
396                                                                       $status->coursemoduleid,
397                                                                       $status->returnaction,
398                                                                       $status->returnparams);
399                 $cell2 = new html_table_cell($this->render($pluginfeedback));
400                 $row->cells = array($cell1, $cell2);
401                 $t->data[] = $row;
402             }
403         }
405         $o .= html_writer::table($t);
406         $o .= $this->output->box_end();
408         $o .= $this->output->container_end();
409         return $o;
410     }
412     /**
413      * Render a table containing the current status of the submission.
414      *
415      * @param assign_submission_status $status
416      * @return string
417      */
418     public function render_assign_submission_status(assign_submission_status $status) {
419         $o = '';
420         $o .= $this->output->container_start('submissionstatustable');
421         $o .= $this->output->heading(get_string('submissionstatusheading', 'assign'), 3);
422         $time = time();
424         if ($status->allowsubmissionsfromdate &&
425                 $time <= $status->allowsubmissionsfromdate) {
426             $o .= $this->output->box_start('generalbox boxaligncenter submissionsalloweddates');
427             if ($status->alwaysshowdescription) {
428                 $date = userdate($status->allowsubmissionsfromdate);
429                 $o .= get_string('allowsubmissionsfromdatesummary', 'assign', $date);
430             } else {
431                 $date = userdate($status->allowsubmissionsfromdate);
432                 $o .= get_string('allowsubmissionsanddescriptionfromdatesummary', 'assign', $date);
433             }
434             $o .= $this->output->box_end();
435         }
436         $o .= $this->output->box_start('boxaligncenter submissionsummarytable');
438         $t = new html_table();
440         if ($status->teamsubmissionenabled) {
441             $row = new html_table_row();
442             $cell1 = new html_table_cell(get_string('submissionteam', 'assign'));
443             $group = $status->submissiongroup;
444             if ($group) {
445                 $cell2 = new html_table_cell(format_string($group->name, false, $status->context));
446             } else {
447                 $cell2 = new html_table_cell(get_string('defaultteam', 'assign'));
448             }
449             $row->cells = array($cell1, $cell2);
450             $t->data[] = $row;
451         }
453         if ($status->attemptreopenmethod != ASSIGN_ATTEMPT_REOPEN_METHOD_NONE) {
454             $currentattempt = 1;
455             if (!$status->teamsubmissionenabled) {
456                 if ($status->submission) {
457                     $currentattempt = $status->submission->attemptnumber + 1;
458                 }
459             } else {
460                 if ($status->teamsubmission) {
461                     $currentattempt = $status->teamsubmission->attemptnumber + 1;
462                 }
463             }
465             $row = new html_table_row();
466             $cell1 = new html_table_cell(get_string('attemptnumber', 'assign'));
467             $maxattempts = $status->maxattempts;
468             if ($maxattempts == ASSIGN_UNLIMITED_ATTEMPTS) {
469                 $message = get_string('currentattempt', 'assign', $currentattempt);
470             } else {
471                 $message = get_string('currentattemptof', 'assign', array('attemptnumber'=>$currentattempt,
472                                                                           'maxattempts'=>$maxattempts));
473             }
474             $cell2 = new html_table_cell($message);
475             $row->cells = array($cell1, $cell2);
476             $t->data[] = $row;
477         }
479         $row = new html_table_row();
480         $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
481         if (!$status->teamsubmissionenabled) {
482             if ($status->submission) {
483                 $statusstr = get_string('submissionstatus_' . $status->submission->status, 'assign');
484                 $cell2 = new html_table_cell($statusstr);
485                 $cell2->attributes = array('class'=>'submissionstatus' . $status->submission->status);
486             } else {
487                 if (!$status->submissionsenabled) {
488                     $cell2 = new html_table_cell(get_string('noonlinesubmissions', 'assign'));
489                 } else {
490                     $cell2 = new html_table_cell(get_string('noattempt', 'assign'));
491                 }
492             }
493             $row->cells = array($cell1, $cell2);
494             $t->data[] = $row;
495         } else {
496             $row = new html_table_row();
497             $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
498             if ($status->teamsubmission) {
499                 $teamstatus = $status->teamsubmission->status;
500                 $submissionsummary = get_string('submissionstatus_' . $teamstatus, 'assign');
501                 $groupid = 0;
502                 if ($status->submissiongroup) {
503                     $groupid = $status->submissiongroup->id;
504                 }
506                 $members = $status->submissiongroupmemberswhoneedtosubmit;
507                 $userslist = array();
508                 foreach ($members as $member) {
509                     $urlparams = array('id' => $member->id, 'course'=>$status->courseid);
510                     $url = new moodle_url('/user/view.php', $urlparams);
511                     if ($status->view == assign_submission_status::GRADER_VIEW && $status->blindmarking) {
512                         $userslist[] = $member->alias;
513                     } else {
514                         $fullname = fullname($member, $status->canviewfullnames);
515                         $userslist[] = $this->output->action_link($url, $fullname);
516                     }
517                 }
518                 if (count($userslist) > 0) {
519                     $userstr = join(', ', $userslist);
520                     $formatteduserstr = get_string('userswhoneedtosubmit', 'assign', $userstr);
521                     $submissionsummary .= $this->output->container($formatteduserstr);
522                 }
524                 $cell2 = new html_table_cell($submissionsummary);
525                 $cell2->attributes = array('class'=>'submissionstatus' . $status->teamsubmission->status);
526             } else {
527                 $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
528                 if (!$status->submissionsenabled) {
529                     $cell2 = new html_table_cell(get_string('noonlinesubmissions', 'assign'));
530                 } else {
531                     $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
532                 }
533             }
534             $row->cells = array($cell1, $cell2);
535             $t->data[] = $row;
536         }
538         // Is locked?
539         if ($status->locked) {
540             $row = new html_table_row();
541             $cell1 = new html_table_cell();
542             $cell2 = new html_table_cell(get_string('submissionslocked', 'assign'));
543             $cell2->attributes = array('class'=>'submissionlocked');
544             $row->cells = array($cell1, $cell2);
545             $t->data[] = $row;
546         }
548         // Grading status.
549         $row = new html_table_row();
550         $cell1 = new html_table_cell(get_string('gradingstatus', 'assign'));
552         if ($status->graded) {
553             $cell2 = new html_table_cell(get_string('graded', 'assign'));
554             $cell2->attributes = array('class'=>'submissiongraded');
555         } else {
556             $cell2 = new html_table_cell(get_string('notgraded', 'assign'));
557             $cell2->attributes = array('class'=>'submissionnotgraded');
558         }
559         $row->cells = array($cell1, $cell2);
560         $t->data[] = $row;
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 (!$status->submission ||
597                         $status->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 ($status->submission->timemodified > $duedate) {
607                         $latestr = get_string('submittedlate',
608                                               'assign',
609                                               format_time($status->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($status->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         $submission = $status->teamsubmission ? $status->teamsubmission : $status->submission;
653         if ($submission) {
654             $row = new html_table_row();
655             $cell1 = new html_table_cell(get_string('timemodified', 'assign'));
656             $cell2 = new html_table_cell(userdate($submission->timemodified));
657             $row->cells = array($cell1, $cell2);
658             $t->data[] = $row;
660             foreach ($status->submissionplugins as $plugin) {
661                 $pluginshowsummary = !$plugin->is_empty($submission) || !$plugin->allow_submissions();
662                 if ($plugin->is_enabled() &&
663                     $plugin->is_visible() &&
664                     $plugin->has_user_summary() &&
665                     $pluginshowsummary) {
667                     $row = new html_table_row();
668                     $cell1 = new html_table_cell($plugin->get_name());
669                     $displaymode = assign_submission_plugin_submission::SUMMARY;
670                     $pluginsubmission = new assign_submission_plugin_submission($plugin,
671                                                                                 $submission,
672                                                                                 $displaymode,
673                                                                                 $status->coursemoduleid,
674                                                                                 $status->returnaction,
675                                                                                 $status->returnparams);
676                     $cell2 = new html_table_cell($this->render($pluginsubmission));
677                     $row->cells = array($cell1, $cell2);
678                     $t->data[] = $row;
679                 }
680             }
681         }
683         $o .= html_writer::table($t);
684         $o .= $this->output->box_end();
686         // Links.
687         if ($status->view == assign_submission_status::STUDENT_VIEW) {
688             if ($status->canedit) {
689                 if (!$submission) {
690                     $o .= $this->output->box_start('generalbox submissionaction');
691                     $urlparams = array('id' => $status->coursemoduleid, 'action' => 'editsubmission');
692                     $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
693                                                        get_string('addsubmission', 'assign'), 'get');
694                     $o .= $this->output->box_start('boxaligncenter submithelp');
695                     $o .= get_string('editsubmission_help', 'assign');
696                     $o .= $this->output->box_end();
697                     $o .= $this->output->box_end();
698                 } else if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
699                     $o .= $this->output->box_start('generalbox submissionaction');
700                     $urlparams = array('id' => $status->coursemoduleid,
701                                        'action' => 'editprevioussubmission',
702                                        'sesskey'=>sesskey());
703                     $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
704                                                        get_string('addnewattemptfromprevious', 'assign'), 'get');
705                     $o .= $this->output->box_start('boxaligncenter submithelp');
706                     $o .= get_string('addnewattemptfromprevious_help', 'assign');
707                     $o .= $this->output->box_end();
708                     $o .= $this->output->box_end();
709                     $o .= $this->output->box_start('generalbox submissionaction');
710                     $urlparams = array('id' => $status->coursemoduleid, 'action' => 'editsubmission');
711                     $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
712                                                        get_string('addnewattempt', 'assign'), 'get');
713                     $o .= $this->output->box_start('boxaligncenter submithelp');
714                     $o .= get_string('addnewattempt_help', 'assign');
715                     $o .= $this->output->box_end();
716                     $o .= $this->output->box_end();
717                 } else {
718                     $o .= $this->output->box_start('generalbox submissionaction');
719                     $urlparams = array('id' => $status->coursemoduleid, 'action' => 'editsubmission');
720                     $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
721                                                        get_string('editsubmission', 'assign'), 'get');
722                     $o .= $this->output->box_start('boxaligncenter submithelp');
723                     $o .= get_string('editsubmission_help', 'assign');
724                     $o .= $this->output->box_end();
725                     $o .= $this->output->box_end();
726                 }
727             }
729             if ($status->cansubmit) {
730                 $urlparams = array('id' => $status->coursemoduleid, 'action'=>'submit');
731                 $o .= $this->output->box_start('generalbox submissionaction');
732                 $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
733                                                    get_string('submitassignment', 'assign'), 'get');
734                 $o .= $this->output->box_start('boxaligncenter submithelp');
735                 $o .= get_string('submitassignment_help', 'assign');
736                 $o .= $this->output->box_end();
737                 $o .= $this->output->box_end();
738             }
739         }
741         $o .= $this->output->container_end();
742         return $o;
743     }
745     /**
746      * Output the attempt history for this assignment
747      *
748      * @param assign_attempt_history $history
749      * @return string
750      */
751     public function render_assign_attempt_history(assign_attempt_history $history) {
752         $o = '';
754         $submittedstr = get_string('submitted', 'assign');
755         $gradestr = get_string('grade');
756         $gradedonstr = get_string('gradedon', 'assign');
757         $gradedbystr = get_string('gradedby', 'assign');
759         // Don't show the last one because it is the current submission.
760         array_pop($history->submissions);
762         // Show newest to oldest.
763         $history->submissions = array_reverse($history->submissions);
765         if (empty($history->submissions)) {
766             return '';
767         }
769         $containerid = 'attempthistory' . uniqid();
770         $o .= $this->heading(get_string('attempthistory', 'assign'), 3);
771         $o .= $this->box_start('attempthistory', $containerid);
773         foreach ($history->submissions as $i => $submission) {
774             $grade = null;
775             foreach ($history->grades as $onegrade) {
776                 if ($onegrade->attemptnumber == $submission->attemptnumber) {
777                     $grade = $onegrade;
778                     break;
779                 }
780             }
782             $editbtn = '';
784             if ($submission) {
785                 $submissionsummary = userdate($submission->timemodified);
786             } else {
787                 $submissionsummary = get_string('nosubmission', 'assign');
788             }
790             $attemptsummaryparams = array('attemptnumber'=>$submission->attemptnumber+1,
791                                           'submissionsummary'=>$submissionsummary);
792             $o .= $this->heading(get_string('attemptheading', 'assign', $attemptsummaryparams), 4);
794             $t = new html_table();
796             if ($submission) {
797                 $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
798                 $cell2 = new html_table_cell(get_string('submissionstatus_' . $submission->status, 'assign'));
799                 $t->data[] = new html_table_row(array($cell1, $cell2));
801                 foreach ($history->submissionplugins as $plugin) {
802                     $pluginshowsummary = !$plugin->is_empty($submission) || !$plugin->allow_submissions();
803                     if ($plugin->is_enabled() &&
804                             $plugin->is_visible() &&
805                             $plugin->has_user_summary() &&
806                             $pluginshowsummary) {
808                         $cell1 = new html_table_cell($plugin->get_name());
809                         $pluginsubmission = new assign_submission_plugin_submission($plugin,
810                                                                                     $submission,
811                                                                                     assign_submission_plugin_submission::SUMMARY,
812                                                                                     $history->coursemoduleid,
813                                                                                     $history->returnaction,
814                                                                                     $history->returnparams);
815                         $cell2 = new html_table_cell($this->render($pluginsubmission));
817                         $t->data[] = new html_table_row(array($cell1, $cell2));
818                     }
819                 }
820             }
822             if ($grade) {
823                 // Heading 'feedback'.
824                 $title = get_string('feedback', 'assign', $i);
825                 $title .= $this->output->spacer(array('width'=>10));
826                 if ($history->cangrade) {
827                     // Edit previous feedback.
828                     $returnparams = http_build_query($history->returnparams);
829                     $urlparams = array('id' => $history->coursemoduleid,
830                                    'rownum'=>$history->rownum,
831                                    'useridlistid'=>$history->useridlistid,
832                                    'attemptnumber'=>$grade->attemptnumber,
833                                    'action'=>'grade',
834                                    'returnaction'=>$history->returnaction,
835                                    'returnparams'=>$returnparams);
836                     $url = new moodle_url('/mod/assign/view.php', $urlparams);
837                     $icon = new pix_icon('gradefeedback',
838                                             get_string('editattemptfeedback', 'assign', $grade->attemptnumber+1),
839                                             'mod_assign');
840                     $title .= $this->output->action_icon($url, $icon);
841                 }
842                 $cell = new html_table_cell($title);
843                 $cell->attributes['class'] = 'feedbacktitle';
844                 $cell->colspan = 2;
845                 $t->data[] = new html_table_row(array($cell));
847                 // Grade.
848                 $cell1 = new html_table_cell($gradestr);
849                 $cell2 = $grade->gradefordisplay;
850                 $t->data[] = new html_table_row(array($cell1, $cell2));
852                 // Graded on.
853                 $cell1 = new html_table_cell($gradedonstr);
854                 $cell2 = new html_table_cell(userdate($grade->timemodified));
855                 $t->data[] = new html_table_row(array($cell1, $cell2));
857                 // Graded by.
858                 $cell1 = new html_table_cell($gradedbystr);
859                 $cell2 = new html_table_cell($this->output->user_picture($grade->grader) .
860                                              $this->output->spacer(array('width'=>30)) . fullname($grade->grader));
861                 $t->data[] = new html_table_row(array($cell1, $cell2));
863                 // Feedback from plugins.
864                 foreach ($history->feedbackplugins as $plugin) {
865                     if ($plugin->is_enabled() &&
866                         $plugin->is_visible() &&
867                         $plugin->has_user_summary() &&
868                         !$plugin->is_empty($grade)) {
870                         $cell1 = new html_table_cell($plugin->get_name());
871                         $pluginfeedback = new assign_feedback_plugin_feedback(
872                             $plugin, $grade, assign_feedback_plugin_feedback::SUMMARY, $history->coursemoduleid,
873                             $history->returnaction, $history->returnparams
874                         );
875                         $cell2 = new html_table_cell($this->render($pluginfeedback));
876                         $t->data[] = new html_table_row(array($cell1, $cell2));
877                     }
879                 }
881             }
883             $o .= html_writer::table($t);
884         }
885         $o .= $this->box_end();
886         $jsparams = array($containerid);
888         $this->page->requires->yui_module('moodle-mod_assign-history', 'Y.one("#' . $containerid . '").history');
890         return $o;
891     }
893     /**
894      * Render a submission plugin submission
895      *
896      * @param assign_submission_plugin_submission $submissionplugin
897      * @return string
898      */
899     public function render_assign_submission_plugin_submission(assign_submission_plugin_submission $submissionplugin) {
900         $o = '';
902         if ($submissionplugin->view == assign_submission_plugin_submission::SUMMARY) {
903             $showviewlink = false;
904             $summary = $submissionplugin->plugin->view_summary($submissionplugin->submission,
905                                                                $showviewlink);
907             $classsuffix = $submissionplugin->plugin->get_subtype() .
908                            '_' .
909                            $submissionplugin->plugin->get_type() .
910                            '_' .
911                            $submissionplugin->submission->id;
913             $o .= $this->output->box_start('boxaligncenter plugincontentsummary summary_' . $classsuffix);
915             $link = '';
916             if ($showviewlink) {
917                 $previewstr = get_string('viewsubmission', 'assign');
918                 $icon = $this->output->pix_icon('t/preview', $previewstr);
920                 $expandstr = get_string('viewfull', 'assign');
921                 $options = array('class'=>'expandsummaryicon expand_' . $classsuffix);
922                 $o .= $this->output->pix_icon('t/switch_plus', $expandstr, null, $options);
924                 $jsparams = array($submissionplugin->plugin->get_subtype(),
925                                   $submissionplugin->plugin->get_type(),
926                                   $submissionplugin->submission->id);
928                 $this->page->requires->js_init_call('M.mod_assign.init_plugin_summary', $jsparams);
930                 $action = 'viewplugin' . $submissionplugin->plugin->get_subtype();
931                 $returnparams = http_build_query($submissionplugin->returnparams);
932                 $link .= '<noscript>';
933                 $urlparams = array('id' => $submissionplugin->coursemoduleid,
934                                    'sid'=>$submissionplugin->submission->id,
935                                    'plugin'=>$submissionplugin->plugin->get_type(),
936                                    'action'=>$action,
937                                    'returnaction'=>$submissionplugin->returnaction,
938                                    'returnparams'=>$returnparams);
939                 $url = new moodle_url('/mod/assign/view.php', $urlparams);
940                 $link .= $this->output->action_link($url, $icon);
941                 $link .= '</noscript>';
943                 $link .= $this->output->spacer(array('width'=>15));
944             }
946             $o .= $link . $summary;
947             $o .= $this->output->box_end();
948             if ($showviewlink) {
949                 $o .= $this->output->box_start('boxaligncenter hidefull full_' . $classsuffix);
950                 $classes = 'expandsummaryicon contract_' . $classsuffix;
951                 $o .= $this->output->pix_icon('t/switch_minus',
952                                               get_string('viewsummary', 'assign'),
953                                               null,
954                                               array('class'=>$classes));
955                 $o .= $submissionplugin->plugin->view($submissionplugin->submission);
956                 $o .= $this->output->box_end();
957             }
958         } else if ($submissionplugin->view == assign_submission_plugin_submission::FULL) {
959             $o .= $this->output->box_start('boxaligncenter submissionfull');
960             $o .= $submissionplugin->plugin->view($submissionplugin->submission);
961             $o .= $this->output->box_end();
962         }
964         return $o;
965     }
967     /**
968      * Render the grading table.
969      *
970      * @param assign_grading_table $table
971      * @return string
972      */
973     public function render_assign_grading_table(assign_grading_table $table) {
974         $o = '';
975         $o .= $this->output->box_start('boxaligncenter gradingtable');
977         $this->page->requires->js_init_call('M.mod_assign.init_grading_table', array());
978         $this->page->requires->string_for_js('nousersselected', 'assign');
979         $this->page->requires->string_for_js('batchoperationconfirmgrantextension', 'assign');
980         $this->page->requires->string_for_js('batchoperationconfirmlock', 'assign');
981         $this->page->requires->string_for_js('batchoperationconfirmreverttodraft', 'assign');
982         $this->page->requires->string_for_js('batchoperationconfirmunlock', 'assign');
983         $this->page->requires->string_for_js('batchoperationconfirmaddattempt', 'assign');
984         $this->page->requires->string_for_js('batchoperationconfirmsetmarkingworkflowstate', 'assign');
985         $this->page->requires->string_for_js('batchoperationconfirmsetmarkingallocation', 'assign');
986         $this->page->requires->string_for_js('editaction', 'assign');
987         foreach ($table->plugingradingbatchoperations as $plugin => $operations) {
988             foreach ($operations as $operation => $description) {
989                 $this->page->requires->string_for_js('batchoperationconfirm' . $operation,
990                                                      'assignfeedback_' . $plugin);
991             }
992         }
993         $o .= $this->flexible_table($table, $table->get_rows_per_page(), true);
994         $o .= $this->output->box_end();
996         return $o;
997     }
999     /**
1000      * Render a feedback plugin feedback
1001      *
1002      * @param assign_feedback_plugin_feedback $feedbackplugin
1003      * @return string
1004      */
1005     public function render_assign_feedback_plugin_feedback(assign_feedback_plugin_feedback $feedbackplugin) {
1006         $o = '';
1008         if ($feedbackplugin->view == assign_feedback_plugin_feedback::SUMMARY) {
1009             $showviewlink = false;
1010             $summary = $feedbackplugin->plugin->view_summary($feedbackplugin->grade, $showviewlink);
1012             $classsuffix = $feedbackplugin->plugin->get_subtype() .
1013                            '_' .
1014                            $feedbackplugin->plugin->get_type() .
1015                            '_' .
1016                            $feedbackplugin->grade->id;
1017             $o .= $this->output->box_start('boxaligncenter plugincontentsummary summary_' . $classsuffix);
1019             $link = '';
1020             if ($showviewlink) {
1021                 $previewstr = get_string('viewfeedback', 'assign');
1022                 $icon = $this->output->pix_icon('t/preview', $previewstr);
1024                 $expandstr = get_string('viewfull', 'assign');
1025                 $options = array('class'=>'expandsummaryicon expand_' . $classsuffix);
1026                 $o .= $this->output->pix_icon('t/switch_plus', $expandstr, null, $options);
1028                 $jsparams = array($feedbackplugin->plugin->get_subtype(),
1029                                   $feedbackplugin->plugin->get_type(),
1030                                   $feedbackplugin->grade->id);
1031                 $this->page->requires->js_init_call('M.mod_assign.init_plugin_summary', $jsparams);
1033                 $urlparams = array('id' => $feedbackplugin->coursemoduleid,
1034                                    'gid'=>$feedbackplugin->grade->id,
1035                                    'plugin'=>$feedbackplugin->plugin->get_type(),
1036                                    'action'=>'viewplugin' . $feedbackplugin->plugin->get_subtype(),
1037                                    'returnaction'=>$feedbackplugin->returnaction,
1038                                    'returnparams'=>http_build_query($feedbackplugin->returnparams));
1039                 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1040                 $link .= '<noscript>';
1041                 $link .= $this->output->action_link($url, $icon);
1042                 $link .= '</noscript>';
1044                 $link .= $this->output->spacer(array('width'=>15));
1045             }
1047             $o .= $link . $summary;
1048             $o .= $this->output->box_end();
1049             if ($showviewlink) {
1050                 $o .= $this->output->box_start('boxaligncenter hidefull full_' . $classsuffix);
1051                 $classes = 'expandsummaryicon contract_' . $classsuffix;
1052                 $o .= $this->output->pix_icon('t/switch_minus',
1053                                               get_string('viewsummary', 'assign'),
1054                                               null,
1055                                               array('class'=>$classes));
1056                 $o .= $feedbackplugin->plugin->view($feedbackplugin->grade);
1057                 $o .= $this->output->box_end();
1058             }
1059         } else if ($feedbackplugin->view == assign_feedback_plugin_feedback::FULL) {
1060             $o .= $this->output->box_start('boxaligncenter feedbackfull');
1061             $o .= $feedbackplugin->plugin->view($feedbackplugin->grade);
1062             $o .= $this->output->box_end();
1063         }
1065         return $o;
1066     }
1068     /**
1069      * Render a course index summary
1070      *
1071      * @param assign_course_index_summary $indexsummary
1072      * @return string
1073      */
1074     public function render_assign_course_index_summary(assign_course_index_summary $indexsummary) {
1075         $o = '';
1077         $strplural = get_string('modulenameplural', 'assign');
1078         $strsectionname  = $indexsummary->courseformatname;
1079         $strduedate = get_string('duedate', 'assign');
1080         $strsubmission = get_string('submission', 'assign');
1081         $strgrade = get_string('grade');
1083         $table = new html_table();
1084         if ($indexsummary->usesections) {
1085             $table->head  = array ($strsectionname, $strplural, $strduedate, $strsubmission, $strgrade);
1086             $table->align = array ('left', 'left', 'center', 'right', 'right');
1087         } else {
1088             $table->head  = array ($strplural, $strduedate, $strsubmission, $strgrade);
1089             $table->align = array ('left', 'left', 'center', 'right');
1090         }
1091         $table->data = array();
1093         $currentsection = '';
1094         foreach ($indexsummary->assignments as $info) {
1095             $params = array('id' => $info['cmid']);
1096             $link = html_writer::link(new moodle_url('/mod/assign/view.php', $params),
1097                                       $info['cmname']);
1098             $due = $info['timedue'] ? userdate($info['timedue']) : '-';
1100             $printsection = '';
1101             if ($indexsummary->usesections) {
1102                 if ($info['sectionname'] !== $currentsection) {
1103                     if ($info['sectionname']) {
1104                         $printsection = $info['sectionname'];
1105                     }
1106                     if ($currentsection !== '') {
1107                         $table->data[] = 'hr';
1108                     }
1109                     $currentsection = $info['sectionname'];
1110                 }
1111             }
1113             if ($indexsummary->usesections) {
1114                 $row = array($printsection, $link, $due, $info['submissioninfo'], $info['gradeinfo']);
1115             } else {
1116                 $row = array($link, $due, $info['submissioninfo'], $info['gradeinfo']);
1117             }
1118             $table->data[] = $row;
1119         }
1121         $o .= html_writer::table($table);
1123         return $o;
1124     }
1128     /**
1129      * Internal function - creates htmls structure suitable for YUI tree.
1130      *
1131      * @param assign_files $tree
1132      * @param array $dir
1133      * @return string
1134      */
1135     protected function htmllize_tree(assign_files $tree, $dir) {
1136         global $CFG;
1137         $yuiconfig = array();
1138         $yuiconfig['type'] = 'html';
1140         if (empty($dir['subdirs']) and empty($dir['files'])) {
1141             return '';
1142         }
1144         $result = '<ul>';
1145         foreach ($dir['subdirs'] as $subdir) {
1146             $image = $this->output->pix_icon(file_folder_icon(),
1147                                              $subdir['dirname'],
1148                                              'moodle',
1149                                              array('class'=>'icon'));
1150             $result .= '<li yuiConfig=\'' . json_encode($yuiconfig) . '\'>' .
1151                        '<div>' . $image . ' ' . s($subdir['dirname']) . '</div> ' .
1152                        $this->htmllize_tree($tree, $subdir) .
1153                        '</li>';
1154         }
1156         foreach ($dir['files'] as $file) {
1157             $filename = $file->get_filename();
1158             if ($CFG->enableplagiarism) {
1159                 require_once($CFG->libdir.'/plagiarismlib.php');
1160                 $plagiarismlinks = plagiarism_get_links(array('userid'=>$file->get_userid(),
1161                                                              'file'=>$file,
1162                                                              'cmid'=>$tree->cm->id,
1163                                                              'course'=>$tree->course));
1164             } else {
1165                 $plagiarismlinks = '';
1166             }
1167             $image = $this->output->pix_icon(file_file_icon($file),
1168                                              $filename,
1169                                              'moodle',
1170                                              array('class'=>'icon'));
1171             $result .= '<li yuiConfig=\'' . json_encode($yuiconfig) . '\'>' .
1172                        '<div>' . $image . ' ' .
1173                                  $file->fileurl . ' ' .
1174                                  $plagiarismlinks .
1175                                  $file->portfoliobutton . '</div>' .
1176                        '</li>';
1177         }
1179         $result .= '</ul>';
1181         return $result;
1182     }
1184     /**
1185      * Helper method dealing with the fact we can not just fetch the output of flexible_table
1186      *
1187      * @param flexible_table $table The table to render
1188      * @param int $rowsperpage How many assignments to render in a page
1189      * @param bool $displaylinks - Whether to render links in the table
1190      *                             (e.g. downloads would not enable this)
1191      * @return string HTML
1192      */
1193     protected function flexible_table(flexible_table $table, $rowsperpage, $displaylinks) {
1195         $o = '';
1196         ob_start();
1197         $table->out($rowsperpage, $displaylinks);
1198         $o = ob_get_contents();
1199         ob_end_clean();
1201         return $o;
1202     }
1204     /**
1205      * Helper method dealing with the fact we can not just fetch the output of moodleforms
1206      *
1207      * @param moodleform $mform
1208      * @return string HTML
1209      */
1210     protected function moodleform(moodleform $mform) {
1212         $o = '';
1213         ob_start();
1214         $mform->display();
1215         $o = ob_get_contents();
1216         ob_end_clean();
1218         return $o;
1219     }