MDL-31270 mod_assign: introducing the assignment module
[moodle.git] / mod / assign / renderer.php
CommitLineData
bbd0e548
DW
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/>.
16
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 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/** Include locallib.php */
28require_once($CFG->dirroot . '/mod/assign/locallib.php');
29
30
31/**
32 * A custom renderer class that extends the plugin_renderer_base and is used by the assign module.
33 *
34 * @package mod_assign
35 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class mod_assign_renderer extends plugin_renderer_base {
39
40 /**
41 * rendering assignment files
42 *
43 * @param context $context
44 * @param int $userid
45 * @param string $filearea
46 * @param string $component
47 * @return string
48 */
49 public function assign_files(context $context, $userid, $filearea, $component) {
50 return $this->render(new assign_files($context, $userid, $filearea, $component));
51 }
52
53 /**
54 * rendering assignment files
55 *
56 * @param assign_files $tree
57 * @return string
58 */
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);
64 $html .= '</div>';
65
66 if ($tree->portfolioform) {
67 $html .= $tree->portfolioform;
68 }
69 return $html;
70 }
71
72 /**
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
75 *
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
79 * @return void
80 */
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;
87 }
88
89 /**
90 * Render the generic form
91 * @param assign_form $form The form to render
92 * @return string
93 */
94 public function render_assign_form(assign_form $form) {
95 $o = '';
96 $o .= $this->output->box_start('boxaligncenter ' . $form->classname);
97 $o .= $this->moodleform($form->form);
98 $o .= $this->output->box_end();
99 return $o;
100 }
101
102 /**
103 * Render the user summary
104 *
105 * @param assign_user_summary $summary The user summary to render
106 * @return string
107 */
108 public function render_assign_user_summary(assign_user_summary $summary) {
109 $o = '';
110
111 if (!$summary->user) {
112 return;
113 }
114 $o .= $this->output->container_start('usersummary');
115 $o .= $this->output->box_start('boxaligncenter usersummarysection');
116 $o .= $this->output->user_picture($summary->user);
117 $o .= $this->output->spacer(array('width'=>30));
118 $o .= $this->output->action_link(new moodle_url('/user/view.php',
119 array('id' => $summary->user->id,
120 'course'=>$summary->courseid)),
121 fullname($summary->user, $summary->viewfullnames));
122 $o .= $this->output->box_end();
123 $o .= $this->output->container_end();
124
125 return $o;
126 }
127
128 /**
129 * Render the submit for grading page
130 *
131 * @param assign_submit_for_grading_page $page
132 * @return string
133 */
134 public function render_assign_submit_for_grading_page($page) {
135 $o = '';
136
137 $o .= $this->output->container_start('submitforgrading');
138 $o .= $this->output->heading(get_string('submitassignment', 'assign'), 3);
139 $o .= $this->output->spacer(array('height'=>30));
140
141 $cancelurl = new moodle_url('/mod/assign/view.php', array('id' => $page->coursemoduleid));
142 if (count($page->notifications)) {
143 // At least one of the submission plugins is not ready for submission
144
145 $o .= $this->output->heading(get_string('submissionnotready', 'assign'), 4);
146
147 foreach ($page->notifications as $notification) {
148 $o .= $this->output->notification($notification);
149 }
150
151 $o .= $this->output->continue_button($cancelurl);
152 } else {
153 // All submission plugins ready - confirm the student really does want to submit for marking
154 $continueurl = new moodle_url('/mod/assign/view.php', array('id' => $page->coursemoduleid,
155 'action' => 'confirmsubmit',
156 'sesskey' => sesskey()));
157 $o .= $this->output->confirm(get_string('confirmsubmission', 'mod_assign'), $continueurl, $cancelurl);
158 }
159 $o .= $this->output->container_end();
160
161
162 return $o;
163 }
164
165 /**
166 * Page is done - render the footer
167 *
168 * @return void
169 */
170 public function render_footer() {
171 return $this->output->footer();
172 }
173
174 /**
175 * render the header
176 *
177 * @param assign_header $header
178 * @return string
179 */
180 public function render_assign_header(assign_header $header) {
181 $o = '';
182
183 if ($header->subpage) {
184 $this->page->navbar->add($header->subpage);
185 }
186
187 $this->page->set_title(get_string('pluginname', 'assign'));
188 $this->page->set_heading($header->assign->name);
189
190 $o .= $this->output->header();
191 $o .= $this->output->heading(format_string($header->assign->name,false, array('context' => $header->context)));
192
193 if ($header->showintro) {
194 $o .= $this->output->box_start('generalbox boxaligncenter', 'intro');
195 $o .= format_module_intro('assign', $header->assign, $header->coursemoduleid);
196 $o .= $this->output->box_end();
197 }
198
199 return $o;
200 }
201
202 /**
203 * render a table containing the current status of the grading process
204 *
205 * @param assign_grading_summary $summary
206 * @return string
207 */
208 public function render_assign_grading_summary(assign_grading_summary $summary) {
209 // create a table for the data
210 $o = '';
211 $o .= $this->output->container_start('gradingsummary');
212 $o .= $this->output->heading(get_string('gradingsummary', 'assign'), 3);
213 $o .= $this->output->box_start('boxaligncenter gradingsummarytable');
214 $t = new html_table();
215
216 // status
217 $this->add_table_row_tuple($t, get_string('numberofparticipants', 'assign'),
218 $summary->participantcount);
219
220 // drafts
221 if ($summary->submissiondraftsenabled) {
222 $this->add_table_row_tuple($t, get_string('numberofdraftsubmissions', 'assign'),
223 $summary->submissiondraftscount);
224 }
225
226 // submitted for grading
227 if ($summary->submissionsenabled) {
228 $this->add_table_row_tuple($t, get_string('numberofsubmittedassignments', 'assign'),
229 $summary->submissionssubmittedcount);
230 }
231
232 $time = time();
233 if ($summary->duedate) {
234 // due date
235 // submitted for grading
236 $duedate = $summary->duedate;
237 $this->add_table_row_tuple($t, get_string('duedate', 'assign'),
238 userdate($duedate));
239
240 // time remaining
241 $due = '';
242 if ($duedate - $time <= 0) {
243 $due = get_string('assignmentisdue', 'assign');
244 } else {
245 $due = format_time($duedate - $time);
246 }
247 $this->add_table_row_tuple($t, get_string('timeremaining', 'assign'), $due);
248 }
249
250 // all done - write the table
251 $o .= html_writer::table($t);
252 $o .= $this->output->box_end();
253
254 // link to the grading page
255 $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php',
256 array('id' => $summary->coursemoduleid,
257 'action'=>'grading')),
258 get_string('viewgrading', 'assign'),
259 'get');
260
261 // close the container and insert a spacer
262 $o .= $this->output->container_end();
263
264 return $o;
265 }
266
267 /**
268 * render a table containing all the current grades and feedback
269 *
270 * @param assign_feedback_status $status
271 * @return string
272 */
273 public function render_assign_feedback_status(assign_feedback_status $status) {
274 global $DB, $CFG;
275 $o = '';
276
277 $o .= $this->output->container_start('feedback');
278 $o .= $this->output->heading(get_string('feedback', 'assign'), 3);
279 $o .= $this->output->box_start('boxaligncenter feedbacktable');
280 $t = new html_table();
281
282 $row = new html_table_row();
283 $cell1 = new html_table_cell(get_string('grade'));
284 $cell2 = new html_table_cell($status->gradefordisplay);
285 $row->cells = array($cell1, $cell2);
286 $t->data[] = $row;
287
288 $row = new html_table_row();
289 $cell1 = new html_table_cell(get_string('gradedon', 'assign'));
290 $cell2 = new html_table_cell(userdate($status->gradeddate));
291 $row->cells = array($cell1, $cell2);
292 $t->data[] = $row;
293
294 if ($status->grader) {
295 $row = new html_table_row();
296 $cell1 = new html_table_cell(get_string('gradedby', 'assign'));
297 $cell2 = new html_table_cell($this->output->user_picture($status->grader) . $this->output->spacer(array('width'=>30)) . fullname($status->grader));
298 $row->cells = array($cell1, $cell2);
299 $t->data[] = $row;
300 }
301
302 foreach ($status->feedbackplugins as $plugin) {
303 if ($plugin->is_enabled() && $plugin->is_visible() && !$plugin->is_empty($status->grade)) {
304 $row = new html_table_row();
305 $cell1 = new html_table_cell($plugin->get_name());
306 $pluginfeedback = new assign_feedback_plugin_feedback($plugin, $status->grade, assign_feedback_plugin_feedback::SUMMARY, $status->coursemoduleid, $status->returnaction, $status->returnparams);
307 $cell2 = new html_table_cell($this->render($pluginfeedback));
308 $row->cells = array($cell1, $cell2);
309 $t->data[] = $row;
310 }
311 }
312
313
314 $o .= html_writer::table($t);
315 $o .= $this->output->box_end();
316
317 $o .= $this->output->container_end();
318 return $o;
319 }
320
321 /**
322 * render a table containing the current status of the submission
323 *
324 * @param assign_submission_status $status
325 * @return string
326 */
327 public function render_assign_submission_status(assign_submission_status $status) {
328 $o = '';
329 $o .= $this->output->container_start('submissionstatustable');
330 $o .= $this->output->heading(get_string('submissionstatusheading', 'assign'), 3);
331 $time = time();
332
333 if ($status->allowsubmissionsfromdate &&
334 $time <= $status->allowsubmissionsfromdate) {
335 $o .= $this->output->box_start('generalbox boxaligncenter submissionsalloweddates');
336 if ($status->alwaysshowdescription) {
337 $o .= get_string('allowsubmissionsfromdatesummary', 'assign', userdate($status->allowsubmissionsfromdate));
338 } else {
339 $o .= get_string('allowsubmissionsanddescriptionfromdatesummary', 'assign', userdate($status->allowsubmissionsfromdate));
340 }
341 $o .= $this->output->box_end();
342 }
343 $o .= $this->output->box_start('boxaligncenter submissionsummarytable');
344
345 $t = new html_table();
346
347 $row = new html_table_row();
348 $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
349 if ($status->submission) {
350 $cell2 = new html_table_cell(get_string('submissionstatus_' . $status->submission->status, 'assign'));
351 $cell2->attributes = array('class'=>'submissionstatus' . $status->submission->status);
352 } else {
353 if (!$status->submissionsenabled) {
354 $cell2 = new html_table_cell(get_string('noonlinesubmissions', 'assign'));
355 } else {
356 $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
357 }
358 }
359 $row->cells = array($cell1, $cell2);
360 $t->data[] = $row;
361
362 // status
363 if ($status->locked) {
364 $row = new html_table_row();
365 $cell1 = new html_table_cell();
366 $cell2 = new html_table_cell(get_string('submissionslocked', 'assign'));
367 $cell2->attributes = array('class'=>'submissionlocked');
368 $row->cells = array($cell1, $cell2);
369 $t->data[] = $row;
370 }
371
372 // grading status
373 $row = new html_table_row();
374 $cell1 = new html_table_cell(get_string('gradingstatus', 'assign'));
375
376 if ($status->graded) {
377 $cell2 = new html_table_cell(get_string('graded', 'assign'));
378 $cell2->attributes = array('class'=>'submissiongraded');
379 } else {
380 $cell2 = new html_table_cell(get_string('notgraded', 'assign'));
381 $cell2->attributes = array('class'=>'submissionnotgraded');
382 }
383 $row->cells = array($cell1, $cell2);
384 $t->data[] = $row;
385
386
387 $duedate = $status->duedate;
388 if ($duedate >= 1) {
389 $row = new html_table_row();
390 $cell1 = new html_table_cell(get_string('duedate', 'assign'));
391 $cell2 = new html_table_cell(userdate($duedate));
392 $row->cells = array($cell1, $cell2);
393 $t->data[] = $row;
394
395 // time remaining
396 $row = new html_table_row();
397 $cell1 = new html_table_cell(get_string('timeremaining', 'assign'));
398 if ($duedate - $time <= 0) {
399 if (!$status->submission || $status->submission != ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
400 if ($status->submissionsenabled) {
401 $cell2 = new html_table_cell(get_string('overdue', 'assign', format_time($time - $duedate)));
402 $cell2->attributes = array('class'=>'overdue');
403 } else {
404 $cell2 = new html_table_cell(get_string('duedatereached', 'assign'));
405 }
406 } else {
407 if ($status->submission->timemodified > $duedate) {
408 $cell2 = new html_table_cell(get_string('submittedlate', 'assign', format_time($status->submission->timemodified - $duedate)));
409 $cell2->attributes = array('class'=>'latesubmission');
410 } else {
411 $cell2 = new html_table_cell(get_string('submittedearly', 'assign', format_time($status->submission->timemodified - $duedate)));
412 $cell2->attributes = array('class'=>'earlysubmission');
413 }
414 }
415 } else {
416 $cell2 = new html_table_cell(format_time($duedate - $time));
417 }
418 $row->cells = array($cell1, $cell2);
419 $t->data[] = $row;
420 }
421
422 // last modified
423 if ($status->submission) {
424 $row = new html_table_row();
425 $cell1 = new html_table_cell(get_string('timemodified', 'assign'));
426 $cell2 = new html_table_cell(userdate($status->submission->timemodified));
427 $row->cells = array($cell1, $cell2);
428 $t->data[] = $row;
429
430 foreach ($status->submissionplugins as $plugin) {
431 if ($plugin->is_enabled() && $plugin->is_visible() && !$plugin->is_empty($status->submission)) {
432 $row = new html_table_row();
433 $cell1 = new html_table_cell($plugin->get_name());
434 $pluginsubmission = new assign_submission_plugin_submission($plugin, $status->submission, assign_submission_plugin_submission::SUMMARY, $status->coursemoduleid, $status->returnaction, $status->returnparams);
435 $cell2 = new html_table_cell($this->render($pluginsubmission));
436 $row->cells = array($cell1, $cell2);
437 $t->data[] = $row;
438 }
439 }
440 }
441
442
443 $o .= html_writer::table($t);
444 $o .= $this->output->box_end();
445
446 // links
447 if ($status->canedit) {
448 $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php',
449 array('id' => $status->coursemoduleid, 'action' => 'editsubmission')), get_string('editsubmission', 'assign'), 'get');
450 }
451
452 if ($status->cansubmit) {
453 // submission.php
454 $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php',
455 array('id' => $status->coursemoduleid, 'action'=>'submit')), get_string('submitassignment', 'assign'), 'get');
456 $o .= $this->output->box_start('boxaligncenter submithelp');
457 $o .= get_string('submitassignment_help', 'assign');
458 $o .= $this->output->box_end();
459 }
460
461 $o .= $this->output->container_end();
462 return $o;
463 }
464
465 /**
466 * render a submission plugin submission
467 *
468 * @param assign_submission_plugin_submission $submissionplugin
469 * @return string
470 */
471 public function render_assign_submission_plugin_submission(assign_submission_plugin_submission $submissionplugin) {
472 $o = '';
473
474 if ($submissionplugin->view == assign_submission_plugin_submission::SUMMARY) {
475 $icon = $this->output->pix_icon('t/preview', get_string('view' . substr($submissionplugin->plugin->get_subtype(), strlen('assign')), 'mod_assign'));
476 $link = '';
477 $showviewlink = false;
478 $summary = $submissionplugin->plugin->view_summary($submissionplugin->submission, $showviewlink);
479 if ($showviewlink) {
480 $link = $this->output->action_link(
481 new moodle_url('/mod/assign/view.php',
482 array('id' => $submissionplugin->coursemoduleid,
483 'sid'=>$submissionplugin->submission->id,
484 'plugin'=>$submissionplugin->plugin->get_type(),
485 'action'=>'viewplugin' . $submissionplugin->plugin->get_subtype(),
486 'returnaction'=>$submissionplugin->returnaction,
487 'returnparams'=>http_build_query($submissionplugin->returnparams))),
488 $icon);
489
490 $link .= $this->output->spacer(array('width'=>15));
491 }
492
493 $o .= $link . $summary;
494 } else if ($submissionplugin->view == assign_submission_plugin_submission::FULL) {
495 $o .= $this->output->box_start('boxaligncenter submissionfull');
496 $o .= $submissionplugin->plugin->view($submissionplugin->submission);
497 $o .= $this->output->box_end();
498 }
499
500 return $o;
501 }
502
503 /**
504 * render the grading table
505 *
506 * @param assign_grading_table $table
507 * @return string
508 */
509 public function render_assign_grading_table(assign_grading_table $table) {
510 $o = '';
511 $o .= $this->output->box_start('boxaligncenter gradingtable');
512 $this->page->requires->js_init_call('M.mod_assign.init_grading_table', array());
513 $this->page->requires->string_for_js('nousersselected', 'assign');
514 $this->page->requires->string_for_js('batchoperationconfirmlock', 'assign');
515 $this->page->requires->string_for_js('batchoperationconfirmunlock', 'assign');
516 $this->page->requires->string_for_js('batchoperationconfirmreverttodraft', 'assign');
517 $this->page->requires->string_for_js('editaction', 'assign');
518 // need to get from prefs
519 $o .= $this->flexible_table($table, $table->get_rows_per_page(), true);
520 $o .= $this->output->box_end();
521
522 return $o;
523 }
524
525 /**
526 * Render a feedback plugin feedback
527 *
528 * @param assign_feedback_plugin_feedback $feedbackplugin
529 * @return string
530 */
531 public function render_assign_feedback_plugin_feedback(assign_feedback_plugin_feedback $feedbackplugin) {
532 $o = '';
533
534 if ($feedbackplugin->view == assign_feedback_plugin_feedback::SUMMARY) {
535 $icon = $this->output->pix_icon('t/preview', get_string('view' . substr($feedbackplugin->plugin->get_subtype(), strlen('assign')), 'mod_assign'));
536 $link = '';
537 $showviewlink = false;
538 $summary = $feedbackplugin->plugin->view_summary($feedbackplugin->grade, $showviewlink);
539 if ($showviewlink) {
540 $link = $this->output->action_link(
541 new moodle_url('/mod/assign/view.php',
542 array('id' => $feedbackplugin->coursemoduleid,
543 'gid'=>$feedbackplugin->grade->id,
544 'plugin'=>$feedbackplugin->plugin->get_type(),
545 'action'=>'viewplugin' . $feedbackplugin->plugin->get_subtype(),
546 'returnaction'=>$feedbackplugin->returnaction,
547 'returnparams'=>http_build_query($feedbackplugin->returnparams))),
548 $icon);
549 $link .= $this->output->spacer(array('width'=>15));
550 }
551
552 $o .= $link . $summary;
553 } else if ($feedbackplugin->view == assign_feedback_plugin_feedback::FULL) {
554 $o .= $this->output->box_start('boxaligncenter feedbackfull');
555 $o .= $feedbackplugin->plugin->view($feedbackplugin->grade);
556 $o .= $this->output->box_end();
557 }
558
559 return $o;
560 }
561
562
563
564 /**
565 * Internal function - creates htmls structure suitable for YUI tree.
566 *
567 * @param assign_files $tree
568 * @param array $dir
569 * @return string
570 */
571 protected function htmllize_tree(assign_files $tree, $dir) {
572 global $CFG;
573 $yuiconfig = array();
574 $yuiconfig['type'] = 'html';
575
576 if (empty($dir['subdirs']) and empty($dir['files'])) {
577 return '';
578 }
579
580 $result = '<ul>';
581 foreach ($dir['subdirs'] as $subdir) {
582 $image = $this->output->pix_icon("f/folder", $subdir['dirname'], 'moodle', array('class'=>'icon'));
583 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
584 }
585
586 foreach ($dir['files'] as $file) {
587 $filename = $file->get_filename();
588 $icon = mimeinfo("icon", $filename);
589 if ($CFG->enableplagiarism) {
590 require_once($CFG->libdir.'/plagiarismlib.php');
591 $plagiarsmlinks = plagiarism_get_links(array('userid'=>$file->get_userid(), 'file'=>$file, 'cmid'=>$tree->cm->id, 'course'=>$tree->course));
592 } else {
593 $plagiarsmlinks = '';
594 }
595 $image = $this->output->pix_icon("f/$icon", $filename, 'moodle', array('class'=>'icon'));
596 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.$file->fileurl.' '.$plagiarsmlinks.$file->portfoliobutton.'</div></li>';
597 }
598
599 $result .= '</ul>';
600
601 return $result;
602 }
603
604 /**
605 * Helper method dealing with the fact we can not just fetch the output of flexible_table
606 *
607 * @param flexible_table $table The table to render
608 * @param int $rowsperpage How many assignments to render in a page
609 * @param bool $displaylinks - Whether to render links in the table (e.g. downloads would not enable this)
610 * @return string HTML
611 */
612 protected function flexible_table(flexible_table $table, $rowsperpage, $displaylinks) {
613
614 $o = '';
615 ob_start();
616 $table->out($rowsperpage, $displaylinks);
617 $o = ob_get_contents();
618 ob_end_clean();
619
620 return $o;
621 }
622
623 /**
624 * Helper method dealing with the fact we can not just fetch the output of moodleforms
625 *
626 * @param moodleform $mform
627 * @return string HTML
628 */
629 protected function moodleform(moodleform $mform) {
630
631 $o = '';
632 ob_start();
633 $mform->display();
634 $o = ob_get_contents();
635 ob_end_clean();
636
637 return $o;
638 }
639
640}
641