2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains a renderer for the assignment class
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /** Include locallib.php */
28 require_once($CFG->dirroot . '/mod/assign/locallib.php');
32 * A custom renderer class that extends the plugin_renderer_base and is used by the assign module.
35 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class mod_assign_renderer extends plugin_renderer_base {
41 * rendering assignment files
43 * @param context $context
45 * @param string $filearea
46 * @param string $component
49 public function assign_files(context $context, $userid, $filearea, $component) {
50 return $this->render(new assign_files($context, $userid, $filearea, $component));
54 * rendering assignment files
56 * @param assign_files $tree
59 public function render_assign_files(assign_files $tree) {
60 $this->htmlid = 'assign_files_tree_'.uniqid();
61 $this->page->requires->js_init_call('M.mod_assign.init_tree', array(true, $this->htmlid));
62 $html = '<div id="'.$this->htmlid.'">';
63 $html .= $this->htmllize_tree($tree, $tree->dir);
66 if ($tree->portfolioform) {
67 $html .= $tree->portfolioform;
73 * Utility function to add a row of data to a table with 2 columns. Modified
74 * the table param and does not return a value
76 * @param html_table $table The table to append the row of data to
77 * @param string $first The first column text
78 * @param string $second The second column text
81 private function add_table_row_tuple(html_table $table, $first, $second) {
82 $row = new html_table_row();
83 $cell1 = new html_table_cell($first);
84 $cell2 = new html_table_cell($second);
85 $row->cells = array($cell1, $cell2);
86 $table->data[] = $row;
90 * Render a grading error notification
91 * @param assign_quickgrading_result $result The result to render
94 public function render_assign_quickgrading_result(assign_quickgrading_result $result) {
95 $url = new moodle_url('/mod/assign/view.php', array('id' => $result->coursemoduleid, 'action'=>'grading'));
98 $o .= $this->output->heading(get_string('quickgradingresult', 'assign'), 4);
99 $o .= $this->output->notification($result->message);
100 $o .= $this->output->continue_button($url);
105 * Render the generic form
106 * @param assign_form $form The form to render
109 public function render_assign_form(assign_form $form) {
111 if ($form->jsinitfunction) {
112 $this->page->requires->js_init_call($form->jsinitfunction, array());
114 $o .= $this->output->box_start('boxaligncenter ' . $form->classname);
115 $o .= $this->moodleform($form->form);
116 $o .= $this->output->box_end();
121 * Render the user summary
123 * @param assign_user_summary $summary The user summary to render
126 public function render_assign_user_summary(assign_user_summary $summary) {
129 if (!$summary->user) {
132 $o .= $this->output->container_start('usersummary');
133 $o .= $this->output->box_start('boxaligncenter usersummarysection');
134 $o .= $this->output->user_picture($summary->user);
135 $o .= $this->output->spacer(array('width'=>30));
136 $o .= $this->output->action_link(new moodle_url('/user/view.php',
137 array('id' => $summary->user->id,
138 'course'=>$summary->courseid)),
139 fullname($summary->user, $summary->viewfullnames));
140 $o .= $this->output->box_end();
141 $o .= $this->output->container_end();
147 * Render the submit for grading page
149 * @param assign_submit_for_grading_page $page
152 public function render_assign_submit_for_grading_page($page) {
155 $o .= $this->output->container_start('submitforgrading');
156 $o .= $this->output->heading(get_string('submitassignment', 'assign'), 3);
157 $o .= $this->output->spacer(array('height'=>30));
159 $cancelurl = new moodle_url('/mod/assign/view.php', array('id' => $page->coursemoduleid));
160 if (count($page->notifications)) {
161 // At least one of the submission plugins is not ready for submission
163 $o .= $this->output->heading(get_string('submissionnotready', 'assign'), 4);
165 foreach ($page->notifications as $notification) {
166 $o .= $this->output->notification($notification);
169 $o .= $this->output->continue_button($cancelurl);
171 // All submission plugins ready - show the confirmation form (may contain submission statement)
172 $o .= $this->moodleform($page->confirmform);
174 $o .= $this->output->container_end();
181 * Page is done - render the footer
185 public function render_footer() {
186 return $this->output->footer();
192 * @param assign_header $header
195 public function render_assign_header(assign_header $header) {
198 if ($header->subpage) {
199 $this->page->navbar->add($header->subpage);
202 $this->page->set_title(get_string('pluginname', 'assign'));
203 $this->page->set_heading($header->assign->name);
205 $o .= $this->output->header();
206 if ($header->preface) {
207 $o .= $header->preface;
209 $o .= $this->output->heading(format_string($header->assign->name,false, array('context' => $header->context)));
211 if ($header->showintro) {
212 $o .= $this->output->box_start('generalbox boxaligncenter', 'intro');
213 $o .= format_module_intro('assign', $header->assign, $header->coursemoduleid);
214 $o .= $this->output->box_end();
221 * render a table containing the current status of the grading process
223 * @param assign_grading_summary $summary
226 public function render_assign_grading_summary(assign_grading_summary $summary) {
227 // create a table for the data
229 $o .= $this->output->container_start('gradingsummary');
230 $o .= $this->output->heading(get_string('gradingsummary', 'assign'), 3);
231 $o .= $this->output->box_start('boxaligncenter gradingsummarytable');
232 $t = new html_table();
235 $this->add_table_row_tuple($t, get_string('numberofparticipants', 'assign'),
236 $summary->participantcount);
239 if ($summary->submissiondraftsenabled) {
240 $this->add_table_row_tuple($t, get_string('numberofdraftsubmissions', 'assign'),
241 $summary->submissiondraftscount);
244 // submitted for grading
245 if ($summary->submissionsenabled) {
246 $this->add_table_row_tuple($t, get_string('numberofsubmittedassignments', 'assign'),
247 $summary->submissionssubmittedcount);
248 $this->add_table_row_tuple($t, get_string('numberofsubmissionsneedgrading', 'assign'),
249 $summary->submissionsneedgradingcount);
253 if ($summary->duedate) {
255 // submitted for grading
256 $duedate = $summary->duedate;
257 $this->add_table_row_tuple($t, get_string('duedate', 'assign'),
262 if ($duedate - $time <= 0) {
263 $due = get_string('assignmentisdue', 'assign');
265 $due = format_time($duedate - $time);
267 $this->add_table_row_tuple($t, get_string('timeremaining', 'assign'), $due);
270 // all done - write the table
271 $o .= html_writer::table($t);
272 $o .= $this->output->box_end();
274 // link to the grading page
275 $o .= $this->output->container_start('submissionlinks');
276 $o .= $this->output->action_link(new moodle_url('/mod/assign/view.php',
277 array('id' => $summary->coursemoduleid,
278 'action'=>'grading')),
279 get_string('viewgrading', 'assign'));
280 $o .= $this->output->container_end();
282 // close the container and insert a spacer
283 $o .= $this->output->container_end();
289 * render a table containing all the current grades and feedback
291 * @param assign_feedback_status $status
294 public function render_assign_feedback_status(assign_feedback_status $status) {
298 $o .= $this->output->container_start('feedback');
299 $o .= $this->output->heading(get_string('feedback', 'assign'), 3);
300 $o .= $this->output->box_start('boxaligncenter feedbacktable');
301 $t = new html_table();
303 $row = new html_table_row();
304 $cell1 = new html_table_cell(get_string('grade'));
305 $cell2 = new html_table_cell($status->gradefordisplay);
306 $row->cells = array($cell1, $cell2);
309 $row = new html_table_row();
310 $cell1 = new html_table_cell(get_string('gradedon', 'assign'));
311 $cell2 = new html_table_cell(userdate($status->gradeddate));
312 $row->cells = array($cell1, $cell2);
315 if ($status->grader) {
316 $row = new html_table_row();
317 $cell1 = new html_table_cell(get_string('gradedby', 'assign'));
318 $cell2 = new html_table_cell($this->output->user_picture($status->grader) . $this->output->spacer(array('width'=>30)) . fullname($status->grader));
319 $row->cells = array($cell1, $cell2);
323 foreach ($status->feedbackplugins as $plugin) {
324 if ($plugin->is_enabled() && $plugin->is_visible() && !empty($status->grade) && !$plugin->is_empty($status->grade)) {
325 $row = new html_table_row();
326 $cell1 = new html_table_cell($plugin->get_name());
327 $pluginfeedback = new assign_feedback_plugin_feedback($plugin, $status->grade, assign_feedback_plugin_feedback::SUMMARY, $status->coursemoduleid, $status->returnaction, $status->returnparams);
328 $cell2 = new html_table_cell($this->render($pluginfeedback));
329 $row->cells = array($cell1, $cell2);
335 $o .= html_writer::table($t);
336 $o .= $this->output->box_end();
338 $o .= $this->output->container_end();
343 * render a table containing the current status of the submission
345 * @param assign_submission_status $status
348 public function render_assign_submission_status(assign_submission_status $status) {
350 $o .= $this->output->container_start('submissionstatustable');
351 $o .= $this->output->heading(get_string('submissionstatusheading', 'assign'), 3);
354 if ($status->allowsubmissionsfromdate &&
355 $time <= $status->allowsubmissionsfromdate) {
356 $o .= $this->output->box_start('generalbox boxaligncenter submissionsalloweddates');
357 if ($status->alwaysshowdescription) {
358 $o .= get_string('allowsubmissionsfromdatesummary', 'assign', userdate($status->allowsubmissionsfromdate));
360 $o .= get_string('allowsubmissionsanddescriptionfromdatesummary', 'assign', userdate($status->allowsubmissionsfromdate));
362 $o .= $this->output->box_end();
364 $o .= $this->output->box_start('boxaligncenter submissionsummarytable');
366 $t = new html_table();
368 $row = new html_table_row();
369 $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
370 if ($status->submission) {
371 $cell2 = new html_table_cell(get_string('submissionstatus_' . $status->submission->status, 'assign'));
372 $cell2->attributes = array('class'=>'submissionstatus' . $status->submission->status);
374 if (!$status->submissionsenabled) {
375 $cell2 = new html_table_cell(get_string('noonlinesubmissions', 'assign'));
377 $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
380 $row->cells = array($cell1, $cell2);
384 if ($status->locked) {
385 $row = new html_table_row();
386 $cell1 = new html_table_cell();
387 $cell2 = new html_table_cell(get_string('submissionslocked', 'assign'));
388 $cell2->attributes = array('class'=>'submissionlocked');
389 $row->cells = array($cell1, $cell2);
394 $row = new html_table_row();
395 $cell1 = new html_table_cell(get_string('gradingstatus', 'assign'));
397 if ($status->graded) {
398 $cell2 = new html_table_cell(get_string('graded', 'assign'));
399 $cell2->attributes = array('class'=>'submissiongraded');
401 $cell2 = new html_table_cell(get_string('notgraded', 'assign'));
402 $cell2->attributes = array('class'=>'submissionnotgraded');
404 $row->cells = array($cell1, $cell2);
408 $duedate = $status->duedate;
410 $row = new html_table_row();
411 $cell1 = new html_table_cell(get_string('duedate', 'assign'));
412 $cell2 = new html_table_cell(userdate($duedate));
413 $row->cells = array($cell1, $cell2);
417 $row = new html_table_row();
418 $cell1 = new html_table_cell(get_string('timeremaining', 'assign'));
419 if ($duedate - $time <= 0) {
420 if (!$status->submission || $status->submission != ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
421 if ($status->submissionsenabled) {
422 $cell2 = new html_table_cell(get_string('overdue', 'assign', format_time($time - $duedate)));
423 $cell2->attributes = array('class'=>'overdue');
425 $cell2 = new html_table_cell(get_string('duedatereached', 'assign'));
428 if ($status->submission->timemodified > $duedate) {
429 $cell2 = new html_table_cell(get_string('submittedlate', 'assign', format_time($status->submission->timemodified - $duedate)));
430 $cell2->attributes = array('class'=>'latesubmission');
432 $cell2 = new html_table_cell(get_string('submittedearly', 'assign', format_time($status->submission->timemodified - $duedate)));
433 $cell2->attributes = array('class'=>'earlysubmission');
437 $cell2 = new html_table_cell(format_time($duedate - $time));
439 $row->cells = array($cell1, $cell2);
444 if ($status->submission) {
445 $row = new html_table_row();
446 $cell1 = new html_table_cell(get_string('timemodified', 'assign'));
447 $cell2 = new html_table_cell(userdate($status->submission->timemodified));
448 $row->cells = array($cell1, $cell2);
451 foreach ($status->submissionplugins as $plugin) {
452 if ($plugin->is_enabled() && $plugin->is_visible() && !$plugin->is_empty($status->submission)) {
453 $row = new html_table_row();
454 $cell1 = new html_table_cell($plugin->get_name());
455 $pluginsubmission = new assign_submission_plugin_submission($plugin, $status->submission, assign_submission_plugin_submission::SUMMARY, $status->coursemoduleid, $status->returnaction, $status->returnparams);
456 $cell2 = new html_table_cell($this->render($pluginsubmission));
457 $row->cells = array($cell1, $cell2);
464 $o .= html_writer::table($t);
465 $o .= $this->output->box_end();
468 if ($status->canedit) {
469 if (!$status->submission) {
470 $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php',
471 array('id' => $status->coursemoduleid, 'action' => 'editsubmission')), get_string('addsubmission', 'assign'), 'get');
473 $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php',
474 array('id' => $status->coursemoduleid, 'action' => 'editsubmission')), get_string('editsubmission', 'assign'), 'get');
478 if ($status->cansubmit) {
480 $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php',
481 array('id' => $status->coursemoduleid, 'action'=>'submit')), get_string('submitassignment', 'assign'), 'get');
482 $o .= $this->output->box_start('boxaligncenter submithelp');
483 $o .= get_string('submitassignment_help', 'assign');
484 $o .= $this->output->box_end();
487 $o .= $this->output->container_end();
492 * render a submission plugin submission
494 * @param assign_submission_plugin_submission $submissionplugin
497 public function render_assign_submission_plugin_submission(assign_submission_plugin_submission $submissionplugin) {
500 if ($submissionplugin->view == assign_submission_plugin_submission::SUMMARY) {
501 $icon = $this->output->pix_icon('t/preview', get_string('view' . substr($submissionplugin->plugin->get_subtype(), strlen('assign')), 'mod_assign'));
503 $showviewlink = false;
504 $summary = $submissionplugin->plugin->view_summary($submissionplugin->submission, $showviewlink);
506 $link = $this->output->action_link(
507 new moodle_url('/mod/assign/view.php',
508 array('id' => $submissionplugin->coursemoduleid,
509 'sid'=>$submissionplugin->submission->id,
510 'plugin'=>$submissionplugin->plugin->get_type(),
511 'action'=>'viewplugin' . $submissionplugin->plugin->get_subtype(),
512 'returnaction'=>$submissionplugin->returnaction,
513 'returnparams'=>http_build_query($submissionplugin->returnparams))),
516 $link .= $this->output->spacer(array('width'=>15));
519 $o .= $link . $summary;
520 } else if ($submissionplugin->view == assign_submission_plugin_submission::FULL) {
521 $o .= $this->output->box_start('boxaligncenter submissionfull');
522 $o .= $submissionplugin->plugin->view($submissionplugin->submission);
523 $o .= $this->output->box_end();
530 * render the grading table
532 * @param assign_grading_table $table
535 public function render_assign_grading_table(assign_grading_table $table) {
537 $o .= $this->output->box_start('boxaligncenter gradingtable');
538 $this->page->requires->js_init_call('M.mod_assign.init_grading_table', array());
539 $this->page->requires->string_for_js('nousersselected', 'assign');
540 $this->page->requires->string_for_js('batchoperationconfirmlock', 'assign');
541 $this->page->requires->string_for_js('batchoperationconfirmunlock', 'assign');
542 $this->page->requires->string_for_js('batchoperationconfirmreverttodraft', 'assign');
543 $this->page->requires->string_for_js('editaction', 'assign');
544 // need to get from prefs
545 $o .= $this->flexible_table($table, $table->get_rows_per_page(), true);
546 $o .= $this->output->box_end();
552 * Render a feedback plugin feedback
554 * @param assign_feedback_plugin_feedback $feedbackplugin
557 public function render_assign_feedback_plugin_feedback(assign_feedback_plugin_feedback $feedbackplugin) {
560 if ($feedbackplugin->view == assign_feedback_plugin_feedback::SUMMARY) {
561 $icon = $this->output->pix_icon('t/preview', get_string('view' . substr($feedbackplugin->plugin->get_subtype(), strlen('assign')), 'mod_assign'));
563 $showviewlink = false;
564 $summary = $feedbackplugin->plugin->view_summary($feedbackplugin->grade, $showviewlink);
566 $link = $this->output->action_link(
567 new moodle_url('/mod/assign/view.php',
568 array('id' => $feedbackplugin->coursemoduleid,
569 'gid'=>$feedbackplugin->grade->id,
570 'plugin'=>$feedbackplugin->plugin->get_type(),
571 'action'=>'viewplugin' . $feedbackplugin->plugin->get_subtype(),
572 'returnaction'=>$feedbackplugin->returnaction,
573 'returnparams'=>http_build_query($feedbackplugin->returnparams))),
575 $link .= $this->output->spacer(array('width'=>15));
578 $o .= $link . $summary;
579 } else if ($feedbackplugin->view == assign_feedback_plugin_feedback::FULL) {
580 $o .= $this->output->box_start('boxaligncenter feedbackfull');
581 $o .= $feedbackplugin->plugin->view($feedbackplugin->grade);
582 $o .= $this->output->box_end();
591 * Internal function - creates htmls structure suitable for YUI tree.
593 * @param assign_files $tree
597 protected function htmllize_tree(assign_files $tree, $dir) {
599 $yuiconfig = array();
600 $yuiconfig['type'] = 'html';
602 if (empty($dir['subdirs']) and empty($dir['files'])) {
607 foreach ($dir['subdirs'] as $subdir) {
608 $image = $this->output->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle', array('class'=>'icon'));
609 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
612 foreach ($dir['files'] as $file) {
613 $filename = $file->get_filename();
614 if ($CFG->enableplagiarism) {
615 require_once($CFG->libdir.'/plagiarismlib.php');
616 $plagiarsmlinks = plagiarism_get_links(array('userid'=>$file->get_userid(), 'file'=>$file, 'cmid'=>$tree->cm->id, 'course'=>$tree->course));
618 $plagiarsmlinks = '';
620 $image = $this->output->pix_icon(file_file_icon($file), $filename, 'moodle', array('class'=>'icon'));
621 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.$file->fileurl.' '.$plagiarsmlinks.$file->portfoliobutton.'</div></li>';
630 * Helper method dealing with the fact we can not just fetch the output of flexible_table
632 * @param flexible_table $table The table to render
633 * @param int $rowsperpage How many assignments to render in a page
634 * @param bool $displaylinks - Whether to render links in the table (e.g. downloads would not enable this)
635 * @return string HTML
637 protected function flexible_table(flexible_table $table, $rowsperpage, $displaylinks) {
641 $table->out($rowsperpage, $displaylinks);
642 $o = ob_get_contents();
649 * Helper method dealing with the fact we can not just fetch the output of moodleforms
651 * @param moodleform $mform
652 * @return string HTML
654 protected function moodleform(moodleform $mform) {
659 $o = ob_get_contents();