MDL-50526 mod_assign: Highlight error message when user has no group
[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(
457                         html_writer::span(get_string('noteam', 'assign'), 'alert alert-error')
458                     );
459                 } else if (count($status->usergroups) > 1) {
460                     $cell2 = new html_table_cell(
461                         html_writer::span(get_string('multipleteams', 'assign'), 'alert alert-error')
462                     );
463                 }
464             } else {
465                 $cell2 = new html_table_cell(get_string('defaultteam', 'assign'));
466             }
467             $row->cells = array($cell1, $cell2);
468             $t->data[] = $row;
469         }
471         if ($status->attemptreopenmethod != ASSIGN_ATTEMPT_REOPEN_METHOD_NONE) {
472             $currentattempt = 1;
473             if (!$status->teamsubmissionenabled) {
474                 if ($status->submission) {
475                     $currentattempt = $status->submission->attemptnumber + 1;
476                 }
477             } else {
478                 if ($status->teamsubmission) {
479                     $currentattempt = $status->teamsubmission->attemptnumber + 1;
480                 }
481             }
483             $row = new html_table_row();
484             $cell1 = new html_table_cell(get_string('attemptnumber', 'assign'));
485             $maxattempts = $status->maxattempts;
486             if ($maxattempts == ASSIGN_UNLIMITED_ATTEMPTS) {
487                 $message = get_string('currentattempt', 'assign', $currentattempt);
488             } else {
489                 $message = get_string('currentattemptof', 'assign', array('attemptnumber'=>$currentattempt,
490                                                                           'maxattempts'=>$maxattempts));
491             }
492             $cell2 = new html_table_cell($message);
493             $row->cells = array($cell1, $cell2);
494             $t->data[] = $row;
495         }
497         $row = new html_table_row();
498         $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
499         if (!$status->teamsubmissionenabled) {
500             if ($status->submission && $status->submission->status != ASSIGN_SUBMISSION_STATUS_NEW) {
501                 $statusstr = get_string('submissionstatus_' . $status->submission->status, 'assign');
502                 $cell2 = new html_table_cell($statusstr);
503                 $cell2->attributes = array('class'=>'submissionstatus' . $status->submission->status);
504             } else {
505                 if (!$status->submissionsenabled) {
506                     $cell2 = new html_table_cell(get_string('noonlinesubmissions', 'assign'));
507                 } else {
508                     $cell2 = new html_table_cell(get_string('noattempt', 'assign'));
509                 }
510             }
511             $row->cells = array($cell1, $cell2);
512             $t->data[] = $row;
513         } else {
514             $row = new html_table_row();
515             $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
516             $group = $status->submissiongroup;
517             if (!$group && $status->preventsubmissionnotingroup) {
518                 $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
519             } else if ($status->teamsubmission && $status->teamsubmission->status != ASSIGN_SUBMISSION_STATUS_NEW) {
520                 $teamstatus = $status->teamsubmission->status;
521                 $submissionsummary = get_string('submissionstatus_' . $teamstatus, 'assign');
522                 $groupid = 0;
523                 if ($status->submissiongroup) {
524                     $groupid = $status->submissiongroup->id;
525                 }
527                 $members = $status->submissiongroupmemberswhoneedtosubmit;
528                 $userslist = array();
529                 foreach ($members as $member) {
530                     $urlparams = array('id' => $member->id, 'course'=>$status->courseid);
531                     $url = new moodle_url('/user/view.php', $urlparams);
532                     if ($status->view == assign_submission_status::GRADER_VIEW && $status->blindmarking) {
533                         $userslist[] = $member->alias;
534                     } else {
535                         $fullname = fullname($member, $status->canviewfullnames);
536                         $userslist[] = $this->output->action_link($url, $fullname);
537                     }
538                 }
539                 if (count($userslist) > 0) {
540                     $userstr = join(', ', $userslist);
541                     $formatteduserstr = get_string('userswhoneedtosubmit', 'assign', $userstr);
542                     $submissionsummary .= $this->output->container($formatteduserstr);
543                 }
545                 $cell2 = new html_table_cell($submissionsummary);
546                 $cell2->attributes = array('class'=>'submissionstatus' . $status->teamsubmission->status);
547             } else {
548                 $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
549                 if (!$status->submissionsenabled) {
550                     $cell2 = new html_table_cell(get_string('noonlinesubmissions', 'assign'));
551                 } else {
552                     $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
553                 }
554             }
555             $row->cells = array($cell1, $cell2);
556             $t->data[] = $row;
557         }
559         // Is locked?
560         if ($status->locked) {
561             $row = new html_table_row();
562             $cell1 = new html_table_cell();
563             $cell2 = new html_table_cell(get_string('submissionslocked', 'assign'));
564             $cell2->attributes = array('class'=>'submissionlocked');
565             $row->cells = array($cell1, $cell2);
566             $t->data[] = $row;
567         }
569         // Grading status.
570         $row = new html_table_row();
571         $cell1 = new html_table_cell(get_string('gradingstatus', 'assign'));
573         if ($status->gradingstatus == ASSIGN_GRADING_STATUS_GRADED ||
574             $status->gradingstatus == ASSIGN_GRADING_STATUS_NOT_GRADED) {
575             $cell2 = new html_table_cell(get_string($status->gradingstatus, 'assign'));
576         } else {
577             $gradingstatus = 'markingworkflowstate' . $status->gradingstatus;
578             $cell2 = new html_table_cell(get_string($gradingstatus, 'assign'));
579         }
580         if ($status->gradingstatus == ASSIGN_GRADING_STATUS_GRADED ||
581             $status->gradingstatus == ASSIGN_MARKING_WORKFLOW_STATE_RELEASED) {
582             $cell2->attributes = array('class' => 'submissiongraded');
583         } else {
584             $cell2->attributes = array('class' => 'submissionnotgraded');
585         }
586         $row->cells = array($cell1, $cell2);
587         $t->data[] = $row;
589         $submission = $status->teamsubmission ? $status->teamsubmission : $status->submission;
590         $duedate = $status->duedate;
591         if ($duedate > 0) {
592             // Due date.
593             $row = new html_table_row();
594             $cell1 = new html_table_cell(get_string('duedate', 'assign'));
595             $cell2 = new html_table_cell(userdate($duedate));
596             $row->cells = array($cell1, $cell2);
597             $t->data[] = $row;
599             if ($status->view == assign_submission_status::GRADER_VIEW) {
600                 if ($status->cutoffdate) {
601                     // Cut off date.
602                     $row = new html_table_row();
603                     $cell1 = new html_table_cell(get_string('cutoffdate', 'assign'));
604                     $cell2 = new html_table_cell(userdate($status->cutoffdate));
605                     $row->cells = array($cell1, $cell2);
606                     $t->data[] = $row;
607                 }
608             }
610             if ($status->extensionduedate) {
611                 // Extension date.
612                 $row = new html_table_row();
613                 $cell1 = new html_table_cell(get_string('extensionduedate', 'assign'));
614                 $cell2 = new html_table_cell(userdate($status->extensionduedate));
615                 $row->cells = array($cell1, $cell2);
616                 $t->data[] = $row;
617                 $duedate = $status->extensionduedate;
618             }
620             // Time remaining.
621             $row = new html_table_row();
622             $cell1 = new html_table_cell(get_string('timeremaining', 'assign'));
623             if ($duedate - $time <= 0) {
624                 if (!$submission ||
625                         $submission->status != ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
626                     if ($status->submissionsenabled) {
627                         $overduestr = get_string('overdue', 'assign', format_time($time - $duedate));
628                         $cell2 = new html_table_cell($overduestr);
629                         $cell2->attributes = array('class'=>'overdue');
630                     } else {
631                         $cell2 = new html_table_cell(get_string('duedatereached', 'assign'));
632                     }
633                 } else {
634                     if ($submission->timemodified > $duedate) {
635                         $latestr = get_string('submittedlate',
636                                               'assign',
637                                               format_time($submission->timemodified - $duedate));
638                         $cell2 = new html_table_cell($latestr);
639                         $cell2->attributes = array('class'=>'latesubmission');
640                     } else {
641                         $earlystr = get_string('submittedearly',
642                                                'assign',
643                                                format_time($submission->timemodified - $duedate));
644                         $cell2 = new html_table_cell($earlystr);
645                         $cell2->attributes = array('class'=>'earlysubmission');
646                     }
647                 }
648             } else {
649                 $cell2 = new html_table_cell(format_time($duedate - $time));
650             }
651             $row->cells = array($cell1, $cell2);
652             $t->data[] = $row;
653         }
655         // Show graders whether this submission is editable by students.
656         if ($status->view == assign_submission_status::GRADER_VIEW) {
657             $row = new html_table_row();
658             $cell1 = new html_table_cell(get_string('editingstatus', 'assign'));
659             if ($status->canedit) {
660                 $cell2 = new html_table_cell(get_string('submissioneditable', 'assign'));
661                 $cell2->attributes = array('class'=>'submissioneditable');
662             } else {
663                 $cell2 = new html_table_cell(get_string('submissionnoteditable', 'assign'));
664                 $cell2->attributes = array('class'=>'submissionnoteditable');
665             }
666             $row->cells = array($cell1, $cell2);
667             $t->data[] = $row;
668         }
670         // Grading criteria preview.
671         if (!empty($status->gradingcontrollerpreview)) {
672             $row = new html_table_row();
673             $cell1 = new html_table_cell(get_string('gradingmethodpreview', 'assign'));
674             $cell2 = new html_table_cell($status->gradingcontrollerpreview);
675             $row->cells = array($cell1, $cell2);
676             $t->data[] = $row;
677         }
679         // Last modified.
680         if ($submission) {
681             $row = new html_table_row();
682             $cell1 = new html_table_cell(get_string('timemodified', 'assign'));
683             $cell2 = new html_table_cell(userdate($submission->timemodified));
684             $row->cells = array($cell1, $cell2);
685             $t->data[] = $row;
687             if (!$status->teamsubmission || $status->submissiongroup != false || !$status->preventsubmissionnotingroup) {
688                 foreach ($status->submissionplugins as $plugin) {
689                     $pluginshowsummary = !$plugin->is_empty($submission) || !$plugin->allow_submissions();
690                     if ($plugin->is_enabled() &&
691                         $plugin->is_visible() &&
692                         $plugin->has_user_summary() &&
693                         $pluginshowsummary
694                     ) {
696                         $row = new html_table_row();
697                         $cell1 = new html_table_cell($plugin->get_name());
698                         $displaymode = assign_submission_plugin_submission::SUMMARY;
699                         $pluginsubmission = new assign_submission_plugin_submission($plugin,
700                             $submission,
701                             $displaymode,
702                             $status->coursemoduleid,
703                             $status->returnaction,
704                             $status->returnparams);
705                         $cell2 = new html_table_cell($this->render($pluginsubmission));
706                         $row->cells = array($cell1, $cell2);
707                         $t->data[] = $row;
708                     }
709                 }
710             }
711         }
713         $o .= html_writer::table($t);
714         $o .= $this->output->box_end();
716         // Links.
717         if ($status->view == assign_submission_status::STUDENT_VIEW) {
718             if ($status->canedit) {
719                 if (!$submission || $submission->status == ASSIGN_SUBMISSION_STATUS_NEW) {
720                     $o .= $this->output->box_start('generalbox submissionaction');
721                     $urlparams = array('id' => $status->coursemoduleid, 'action' => 'editsubmission');
722                     $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
723                                                        get_string('addsubmission', 'assign'), 'get');
724                     $o .= $this->output->box_start('boxaligncenter submithelp');
725                     $o .= get_string('editsubmission_help', 'assign');
726                     $o .= $this->output->box_end();
727                     $o .= $this->output->box_end();
728                 } else if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
729                     $o .= $this->output->box_start('generalbox submissionaction');
730                     $urlparams = array('id' => $status->coursemoduleid,
731                                        'action' => 'editprevioussubmission',
732                                        'sesskey'=>sesskey());
733                     $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
734                                                        get_string('addnewattemptfromprevious', 'assign'), 'get');
735                     $o .= $this->output->box_start('boxaligncenter submithelp');
736                     $o .= get_string('addnewattemptfromprevious_help', 'assign');
737                     $o .= $this->output->box_end();
738                     $o .= $this->output->box_end();
739                     $o .= $this->output->box_start('generalbox submissionaction');
740                     $urlparams = array('id' => $status->coursemoduleid, 'action' => 'editsubmission');
741                     $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
742                                                        get_string('addnewattempt', 'assign'), 'get');
743                     $o .= $this->output->box_start('boxaligncenter submithelp');
744                     $o .= get_string('addnewattempt_help', 'assign');
745                     $o .= $this->output->box_end();
746                     $o .= $this->output->box_end();
747                 } else {
748                     $o .= $this->output->box_start('generalbox submissionaction');
749                     $urlparams = array('id' => $status->coursemoduleid, 'action' => 'editsubmission');
750                     $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
751                                                        get_string('editsubmission', 'assign'), 'get');
752                     $o .= $this->output->box_start('boxaligncenter submithelp');
753                     $o .= get_string('editsubmission_help', 'assign');
754                     $o .= $this->output->box_end();
755                     $o .= $this->output->box_end();
756                 }
757             }
759             if ($status->cansubmit) {
760                 $urlparams = array('id' => $status->coursemoduleid, 'action'=>'submit');
761                 $o .= $this->output->box_start('generalbox submissionaction');
762                 $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php', $urlparams),
763                                                    get_string('submitassignment', 'assign'), 'get');
764                 $o .= $this->output->box_start('boxaligncenter submithelp');
765                 $o .= get_string('submitassignment_help', 'assign');
766                 $o .= $this->output->box_end();
767                 $o .= $this->output->box_end();
768             }
769         }
771         $o .= $this->output->container_end();
772         return $o;
773     }
775     /**
776      * Output the attempt history for this assignment
777      *
778      * @param assign_attempt_history $history
779      * @return string
780      */
781     public function render_assign_attempt_history(assign_attempt_history $history) {
782         $o = '';
784         $submittedstr = get_string('submitted', 'assign');
785         $gradestr = get_string('grade');
786         $gradedonstr = get_string('gradedon', 'assign');
787         $gradedbystr = get_string('gradedby', 'assign');
789         // Don't show the last one because it is the current submission.
790         array_pop($history->submissions);
792         // Show newest to oldest.
793         $history->submissions = array_reverse($history->submissions);
795         if (empty($history->submissions)) {
796             return '';
797         }
799         $containerid = 'attempthistory' . uniqid();
800         $o .= $this->heading(get_string('attempthistory', 'assign'), 3);
801         $o .= $this->box_start('attempthistory', $containerid);
803         foreach ($history->submissions as $i => $submission) {
804             $grade = null;
805             foreach ($history->grades as $onegrade) {
806                 if ($onegrade->attemptnumber == $submission->attemptnumber) {
807                     $grade = $onegrade;
808                     break;
809                 }
810             }
812             $editbtn = '';
814             if ($submission) {
815                 $submissionsummary = userdate($submission->timemodified);
816             } else {
817                 $submissionsummary = get_string('nosubmission', 'assign');
818             }
820             $attemptsummaryparams = array('attemptnumber'=>$submission->attemptnumber+1,
821                                           'submissionsummary'=>$submissionsummary);
822             $o .= $this->heading(get_string('attemptheading', 'assign', $attemptsummaryparams), 4);
824             $t = new html_table();
826             if ($submission) {
827                 $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
828                 $cell2 = new html_table_cell(get_string('submissionstatus_' . $submission->status, 'assign'));
829                 $t->data[] = new html_table_row(array($cell1, $cell2));
831                 foreach ($history->submissionplugins as $plugin) {
832                     $pluginshowsummary = !$plugin->is_empty($submission) || !$plugin->allow_submissions();
833                     if ($plugin->is_enabled() &&
834                             $plugin->is_visible() &&
835                             $plugin->has_user_summary() &&
836                             $pluginshowsummary) {
838                         $cell1 = new html_table_cell($plugin->get_name());
839                         $pluginsubmission = new assign_submission_plugin_submission($plugin,
840                                                                                     $submission,
841                                                                                     assign_submission_plugin_submission::SUMMARY,
842                                                                                     $history->coursemoduleid,
843                                                                                     $history->returnaction,
844                                                                                     $history->returnparams);
845                         $cell2 = new html_table_cell($this->render($pluginsubmission));
847                         $t->data[] = new html_table_row(array($cell1, $cell2));
848                     }
849                 }
850             }
852             if ($grade) {
853                 // Heading 'feedback'.
854                 $title = get_string('feedback', 'assign', $i);
855                 $title .= $this->output->spacer(array('width'=>10));
856                 if ($history->cangrade) {
857                     // Edit previous feedback.
858                     $returnparams = http_build_query($history->returnparams);
859                     $urlparams = array('id' => $history->coursemoduleid,
860                                    'rownum'=>$history->rownum,
861                                    'useridlistid'=>$history->useridlistid,
862                                    'attemptnumber'=>$grade->attemptnumber,
863                                    'action'=>'grade',
864                                    'returnaction'=>$history->returnaction,
865                                    'returnparams'=>$returnparams);
866                     $url = new moodle_url('/mod/assign/view.php', $urlparams);
867                     $icon = new pix_icon('gradefeedback',
868                                             get_string('editattemptfeedback', 'assign', $grade->attemptnumber+1),
869                                             'mod_assign');
870                     $title .= $this->output->action_icon($url, $icon);
871                 }
872                 $cell = new html_table_cell($title);
873                 $cell->attributes['class'] = 'feedbacktitle';
874                 $cell->colspan = 2;
875                 $t->data[] = new html_table_row(array($cell));
877                 // Grade.
878                 $cell1 = new html_table_cell($gradestr);
879                 $cell2 = $grade->gradefordisplay;
880                 $t->data[] = new html_table_row(array($cell1, $cell2));
882                 // Graded on.
883                 $cell1 = new html_table_cell($gradedonstr);
884                 $cell2 = new html_table_cell(userdate($grade->timemodified));
885                 $t->data[] = new html_table_row(array($cell1, $cell2));
887                 // Graded by.
888                 $cell1 = new html_table_cell($gradedbystr);
889                 $cell2 = new html_table_cell($this->output->user_picture($grade->grader) .
890                                              $this->output->spacer(array('width'=>30)) . fullname($grade->grader));
891                 $t->data[] = new html_table_row(array($cell1, $cell2));
893                 // Feedback from plugins.
894                 foreach ($history->feedbackplugins as $plugin) {
895                     if ($plugin->is_enabled() &&
896                         $plugin->is_visible() &&
897                         $plugin->has_user_summary() &&
898                         !$plugin->is_empty($grade)) {
900                         $cell1 = new html_table_cell($plugin->get_name());
901                         $pluginfeedback = new assign_feedback_plugin_feedback(
902                             $plugin, $grade, assign_feedback_plugin_feedback::SUMMARY, $history->coursemoduleid,
903                             $history->returnaction, $history->returnparams
904                         );
905                         $cell2 = new html_table_cell($this->render($pluginfeedback));
906                         $t->data[] = new html_table_row(array($cell1, $cell2));
907                     }
909                 }
911             }
913             $o .= html_writer::table($t);
914         }
915         $o .= $this->box_end();
916         $jsparams = array($containerid);
918         $this->page->requires->yui_module('moodle-mod_assign-history', 'Y.one("#' . $containerid . '").history');
920         return $o;
921     }
923     /**
924      * Render a submission plugin submission
925      *
926      * @param assign_submission_plugin_submission $submissionplugin
927      * @return string
928      */
929     public function render_assign_submission_plugin_submission(assign_submission_plugin_submission $submissionplugin) {
930         $o = '';
932         if ($submissionplugin->view == assign_submission_plugin_submission::SUMMARY) {
933             $showviewlink = false;
934             $summary = $submissionplugin->plugin->view_summary($submissionplugin->submission,
935                                                                $showviewlink);
937             $classsuffix = $submissionplugin->plugin->get_subtype() .
938                            '_' .
939                            $submissionplugin->plugin->get_type() .
940                            '_' .
941                            $submissionplugin->submission->id;
943             $o .= $this->output->box_start('boxaligncenter plugincontentsummary summary_' . $classsuffix);
945             $link = '';
946             if ($showviewlink) {
947                 $previewstr = get_string('viewsubmission', 'assign');
948                 $icon = $this->output->pix_icon('t/preview', $previewstr);
950                 $expandstr = get_string('viewfull', 'assign');
951                 $options = array('class'=>'expandsummaryicon expand_' . $classsuffix);
952                 $o .= $this->output->pix_icon('t/switch_plus', $expandstr, null, $options);
954                 $jsparams = array($submissionplugin->plugin->get_subtype(),
955                                   $submissionplugin->plugin->get_type(),
956                                   $submissionplugin->submission->id);
958                 $this->page->requires->js_init_call('M.mod_assign.init_plugin_summary', $jsparams);
960                 $action = 'viewplugin' . $submissionplugin->plugin->get_subtype();
961                 $returnparams = http_build_query($submissionplugin->returnparams);
962                 $link .= '<noscript>';
963                 $urlparams = array('id' => $submissionplugin->coursemoduleid,
964                                    'sid'=>$submissionplugin->submission->id,
965                                    'plugin'=>$submissionplugin->plugin->get_type(),
966                                    'action'=>$action,
967                                    'returnaction'=>$submissionplugin->returnaction,
968                                    'returnparams'=>$returnparams);
969                 $url = new moodle_url('/mod/assign/view.php', $urlparams);
970                 $link .= $this->output->action_link($url, $icon);
971                 $link .= '</noscript>';
973                 $link .= $this->output->spacer(array('width'=>15));
974             }
976             $o .= $link . $summary;
977             $o .= $this->output->box_end();
978             if ($showviewlink) {
979                 $o .= $this->output->box_start('boxaligncenter hidefull full_' . $classsuffix);
980                 $classes = 'expandsummaryicon contract_' . $classsuffix;
981                 $o .= $this->output->pix_icon('t/switch_minus',
982                                               get_string('viewsummary', 'assign'),
983                                               null,
984                                               array('class'=>$classes));
985                 $o .= $submissionplugin->plugin->view($submissionplugin->submission);
986                 $o .= $this->output->box_end();
987             }
988         } else if ($submissionplugin->view == assign_submission_plugin_submission::FULL) {
989             $o .= $this->output->box_start('boxaligncenter submissionfull');
990             $o .= $submissionplugin->plugin->view($submissionplugin->submission);
991             $o .= $this->output->box_end();
992         }
994         return $o;
995     }
997     /**
998      * Render the grading table.
999      *
1000      * @param assign_grading_table $table
1001      * @return string
1002      */
1003     public function render_assign_grading_table(assign_grading_table $table) {
1004         $o = '';
1005         $o .= $this->output->box_start('boxaligncenter gradingtable');
1007         $this->page->requires->js_init_call('M.mod_assign.init_grading_table', array());
1008         $this->page->requires->string_for_js('nousersselected', 'assign');
1009         $this->page->requires->string_for_js('batchoperationconfirmgrantextension', 'assign');
1010         $this->page->requires->string_for_js('batchoperationconfirmlock', 'assign');
1011         $this->page->requires->string_for_js('batchoperationconfirmreverttodraft', 'assign');
1012         $this->page->requires->string_for_js('batchoperationconfirmunlock', 'assign');
1013         $this->page->requires->string_for_js('batchoperationconfirmaddattempt', 'assign');
1014         $this->page->requires->string_for_js('batchoperationconfirmsetmarkingworkflowstate', 'assign');
1015         $this->page->requires->string_for_js('batchoperationconfirmsetmarkingallocation', 'assign');
1016         $this->page->requires->string_for_js('editaction', 'assign');
1017         foreach ($table->plugingradingbatchoperations as $plugin => $operations) {
1018             foreach ($operations as $operation => $description) {
1019                 $this->page->requires->string_for_js('batchoperationconfirm' . $operation,
1020                                                      'assignfeedback_' . $plugin);
1021             }
1022         }
1023         $o .= $this->flexible_table($table, $table->get_rows_per_page(), true);
1024         $o .= $this->output->box_end();
1026         return $o;
1027     }
1029     /**
1030      * Render a feedback plugin feedback
1031      *
1032      * @param assign_feedback_plugin_feedback $feedbackplugin
1033      * @return string
1034      */
1035     public function render_assign_feedback_plugin_feedback(assign_feedback_plugin_feedback $feedbackplugin) {
1036         $o = '';
1038         if ($feedbackplugin->view == assign_feedback_plugin_feedback::SUMMARY) {
1039             $showviewlink = false;
1040             $summary = $feedbackplugin->plugin->view_summary($feedbackplugin->grade, $showviewlink);
1042             $classsuffix = $feedbackplugin->plugin->get_subtype() .
1043                            '_' .
1044                            $feedbackplugin->plugin->get_type() .
1045                            '_' .
1046                            $feedbackplugin->grade->id;
1047             $o .= $this->output->box_start('boxaligncenter plugincontentsummary summary_' . $classsuffix);
1049             $link = '';
1050             if ($showviewlink) {
1051                 $previewstr = get_string('viewfeedback', 'assign');
1052                 $icon = $this->output->pix_icon('t/preview', $previewstr);
1054                 $expandstr = get_string('viewfull', 'assign');
1055                 $options = array('class'=>'expandsummaryicon expand_' . $classsuffix);
1056                 $o .= $this->output->pix_icon('t/switch_plus', $expandstr, null, $options);
1058                 $jsparams = array($feedbackplugin->plugin->get_subtype(),
1059                                   $feedbackplugin->plugin->get_type(),
1060                                   $feedbackplugin->grade->id);
1061                 $this->page->requires->js_init_call('M.mod_assign.init_plugin_summary', $jsparams);
1063                 $urlparams = array('id' => $feedbackplugin->coursemoduleid,
1064                                    'gid'=>$feedbackplugin->grade->id,
1065                                    'plugin'=>$feedbackplugin->plugin->get_type(),
1066                                    'action'=>'viewplugin' . $feedbackplugin->plugin->get_subtype(),
1067                                    'returnaction'=>$feedbackplugin->returnaction,
1068                                    'returnparams'=>http_build_query($feedbackplugin->returnparams));
1069                 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1070                 $link .= '<noscript>';
1071                 $link .= $this->output->action_link($url, $icon);
1072                 $link .= '</noscript>';
1074                 $link .= $this->output->spacer(array('width'=>15));
1075             }
1077             $o .= $link . $summary;
1078             $o .= $this->output->box_end();
1079             if ($showviewlink) {
1080                 $o .= $this->output->box_start('boxaligncenter hidefull full_' . $classsuffix);
1081                 $classes = 'expandsummaryicon contract_' . $classsuffix;
1082                 $o .= $this->output->pix_icon('t/switch_minus',
1083                                               get_string('viewsummary', 'assign'),
1084                                               null,
1085                                               array('class'=>$classes));
1086                 $o .= $feedbackplugin->plugin->view($feedbackplugin->grade);
1087                 $o .= $this->output->box_end();
1088             }
1089         } else if ($feedbackplugin->view == assign_feedback_plugin_feedback::FULL) {
1090             $o .= $this->output->box_start('boxaligncenter feedbackfull');
1091             $o .= $feedbackplugin->plugin->view($feedbackplugin->grade);
1092             $o .= $this->output->box_end();
1093         }
1095         return $o;
1096     }
1098     /**
1099      * Render a course index summary
1100      *
1101      * @param assign_course_index_summary $indexsummary
1102      * @return string
1103      */
1104     public function render_assign_course_index_summary(assign_course_index_summary $indexsummary) {
1105         $o = '';
1107         $strplural = get_string('modulenameplural', 'assign');
1108         $strsectionname  = $indexsummary->courseformatname;
1109         $strduedate = get_string('duedate', 'assign');
1110         $strsubmission = get_string('submission', 'assign');
1111         $strgrade = get_string('grade');
1113         $table = new html_table();
1114         if ($indexsummary->usesections) {
1115             $table->head  = array ($strsectionname, $strplural, $strduedate, $strsubmission, $strgrade);
1116             $table->align = array ('left', 'left', 'center', 'right', 'right');
1117         } else {
1118             $table->head  = array ($strplural, $strduedate, $strsubmission, $strgrade);
1119             $table->align = array ('left', 'left', 'center', 'right');
1120         }
1121         $table->data = array();
1123         $currentsection = '';
1124         foreach ($indexsummary->assignments as $info) {
1125             $params = array('id' => $info['cmid']);
1126             $link = html_writer::link(new moodle_url('/mod/assign/view.php', $params),
1127                                       $info['cmname']);
1128             $due = $info['timedue'] ? userdate($info['timedue']) : '-';
1130             $printsection = '';
1131             if ($indexsummary->usesections) {
1132                 if ($info['sectionname'] !== $currentsection) {
1133                     if ($info['sectionname']) {
1134                         $printsection = $info['sectionname'];
1135                     }
1136                     if ($currentsection !== '') {
1137                         $table->data[] = 'hr';
1138                     }
1139                     $currentsection = $info['sectionname'];
1140                 }
1141             }
1143             if ($indexsummary->usesections) {
1144                 $row = array($printsection, $link, $due, $info['submissioninfo'], $info['gradeinfo']);
1145             } else {
1146                 $row = array($link, $due, $info['submissioninfo'], $info['gradeinfo']);
1147             }
1148             $table->data[] = $row;
1149         }
1151         $o .= html_writer::table($table);
1153         return $o;
1154     }
1158     /**
1159      * Internal function - creates htmls structure suitable for YUI tree.
1160      *
1161      * @param assign_files $tree
1162      * @param array $dir
1163      * @return string
1164      */
1165     protected function htmllize_tree(assign_files $tree, $dir) {
1166         global $CFG;
1167         $yuiconfig = array();
1168         $yuiconfig['type'] = 'html';
1170         if (empty($dir['subdirs']) and empty($dir['files'])) {
1171             return '';
1172         }
1174         $result = '<ul>';
1175         foreach ($dir['subdirs'] as $subdir) {
1176             $image = $this->output->pix_icon(file_folder_icon(),
1177                                              $subdir['dirname'],
1178                                              'moodle',
1179                                              array('class'=>'icon'));
1180             $result .= '<li yuiConfig=\'' . json_encode($yuiconfig) . '\'>' .
1181                        '<div>' . $image . ' ' . s($subdir['dirname']) . '</div> ' .
1182                        $this->htmllize_tree($tree, $subdir) .
1183                        '</li>';
1184         }
1186         foreach ($dir['files'] as $file) {
1187             $filename = $file->get_filename();
1188             if ($CFG->enableplagiarism) {
1189                 require_once($CFG->libdir.'/plagiarismlib.php');
1190                 $plagiarismlinks = plagiarism_get_links(array('userid'=>$file->get_userid(),
1191                                                              'file'=>$file,
1192                                                              'cmid'=>$tree->cm->id,
1193                                                              'course'=>$tree->course));
1194             } else {
1195                 $plagiarismlinks = '';
1196             }
1197             $image = $this->output->pix_icon(file_file_icon($file),
1198                                              $filename,
1199                                              'moodle',
1200                                              array('class'=>'icon'));
1201             $result .= '<li yuiConfig=\'' . json_encode($yuiconfig) . '\'>' .
1202                        '<div>' . $image . ' ' .
1203                                  $file->fileurl . ' ' .
1204                                  $plagiarismlinks .
1205                                  $file->portfoliobutton . '</div>' .
1206                        '</li>';
1207         }
1209         $result .= '</ul>';
1211         return $result;
1212     }
1214     /**
1215      * Helper method dealing with the fact we can not just fetch the output of flexible_table
1216      *
1217      * @param flexible_table $table The table to render
1218      * @param int $rowsperpage How many assignments to render in a page
1219      * @param bool $displaylinks - Whether to render links in the table
1220      *                             (e.g. downloads would not enable this)
1221      * @return string HTML
1222      */
1223     protected function flexible_table(flexible_table $table, $rowsperpage, $displaylinks) {
1225         $o = '';
1226         ob_start();
1227         $table->out($rowsperpage, $displaylinks);
1228         $o = ob_get_contents();
1229         ob_end_clean();
1231         return $o;
1232     }
1234     /**
1235      * Helper method dealing with the fact we can not just fetch the output of moodleforms
1236      *
1237      * @param moodleform $mform
1238      * @return string HTML
1239      */
1240     protected function moodleform(moodleform $mform) {
1242         $o = '';
1243         ob_start();
1244         $mform->display();
1245         $o = ob_get_contents();
1246         ob_end_clean();
1248         return $o;
1249     }