MDL-33785 theme_base: RTL fixes added to style/filemanager.css
[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 /** Include locallib.php */
28 require_once($CFG->dirroot . '/mod/assign/locallib.php');
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  */
38 class mod_assign_renderer extends plugin_renderer_base {
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     }
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>';
66         if ($tree->portfolioform) {
67             $html .= $tree->portfolioform;
68         }
69         return $html;
70     }
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     }
89     /**
90      * Render a grading error notification
91      * @param assign_quickgrading_result $result The result to render
92      * @return string
93      */
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'));
97         $o = '';
98         $o .= $this->output->heading(get_string('quickgradingresult', 'assign'), 4);
99         $o .= $this->output->notification($result->message);
100         $o .= $this->output->continue_button($url);
101         return $o;
102     }
104     /**
105      * Render the generic form
106      * @param assign_form $form The form to render
107      * @return string
108      */
109     public function render_assign_form(assign_form $form) {
110         $o = '';
111         if ($form->jsinitfunction) {
112             $this->page->requires->js_init_call($form->jsinitfunction, array());
113         }
114         $o .= $this->output->box_start('boxaligncenter ' . $form->classname);
115         $o .= $this->moodleform($form->form);
116         $o .= $this->output->box_end();
117         return $o;
118     }
120     /**
121      * Render the user summary
122      *
123      * @param assign_user_summary $summary The user summary to render
124      * @return string
125      */
126     public function render_assign_user_summary(assign_user_summary $summary) {
127         $o = '';
129         if (!$summary->user) {
130             return;
131         }
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();
143         return $o;
144     }
146     /**
147      * Render the submit for grading page
148      *
149      * @param assign_submit_for_grading_page $page
150      * @return string
151      */
152     public function render_assign_submit_for_grading_page($page) {
153         $o = '';
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);
167             }
169             $o .= $this->output->continue_button($cancelurl);
170         } else {
171             // All submission plugins ready - confirm the student really does want to submit for marking
172             $continueurl = new moodle_url('/mod/assign/view.php', array('id' => $page->coursemoduleid,
173                                                                         'action' => 'confirmsubmit',
174                                                                         'sesskey' => sesskey()));
175             $o .= $this->output->confirm(get_string('confirmsubmission', 'mod_assign'), $continueurl, $cancelurl);
176         }
177         $o .= $this->output->container_end();
180         return $o;
181     }
183     /**
184      * Page is done - render the footer
185      *
186      * @return void
187      */
188     public function render_footer() {
189         return $this->output->footer();
190     }
192     /**
193      * render the header
194      *
195      * @param assign_header $header
196      * @return string
197      */
198     public function render_assign_header(assign_header $header) {
199         $o = '';
201         if ($header->subpage) {
202             $this->page->navbar->add($header->subpage);
203         }
205         $this->page->set_title(get_string('pluginname', 'assign'));
206         $this->page->set_heading($header->assign->name);
208         $o .= $this->output->header();
209         if ($header->preface) {
210             $o .= $header->preface;
211         }
212         $o .= $this->output->heading(format_string($header->assign->name,false, array('context' => $header->context)));
214         if ($header->showintro) {
215             $o .= $this->output->box_start('generalbox boxaligncenter', 'intro');
216             $o .= format_module_intro('assign', $header->assign, $header->coursemoduleid);
217             $o .= $this->output->box_end();
218         }
220         return $o;
221     }
223     /**
224      * render a table containing the current status of the grading process
225      *
226      * @param assign_grading_summary $summary
227      * @return string
228      */
229     public function render_assign_grading_summary(assign_grading_summary $summary) {
230         // create a table for the data
231         $o = '';
232         $o .= $this->output->container_start('gradingsummary');
233         $o .= $this->output->heading(get_string('gradingsummary', 'assign'), 3);
234         $o .= $this->output->box_start('boxaligncenter gradingsummarytable');
235         $t = new html_table();
237         // status
238         $this->add_table_row_tuple($t, get_string('numberofparticipants', 'assign'),
239                                    $summary->participantcount);
241         // drafts
242         if ($summary->submissiondraftsenabled) {
243             $this->add_table_row_tuple($t, get_string('numberofdraftsubmissions', 'assign'),
244                                        $summary->submissiondraftscount);
245        }
247         // submitted for grading
248         if ($summary->submissionsenabled) {
249             $this->add_table_row_tuple($t, get_string('numberofsubmittedassignments', 'assign'),
250                                        $summary->submissionssubmittedcount);
251         }
253         $time = time();
254         if ($summary->duedate) {
255             // due date
256             // submitted for grading
257             $duedate = $summary->duedate;
258             $this->add_table_row_tuple($t, get_string('duedate', 'assign'),
259                                        userdate($duedate));
261             // time remaining
262             $due = '';
263             if ($duedate - $time <= 0) {
264                 $due = get_string('assignmentisdue', 'assign');
265             } else {
266                 $due = format_time($duedate - $time);
267             }
268             $this->add_table_row_tuple($t, get_string('timeremaining', 'assign'), $due);
269         }
271         // all done - write the table
272         $o .= html_writer::table($t);
273         $o .= $this->output->box_end();
275         // link to the grading page
276         $o .= $this->output->container_start('submissionlinks');
277         $o .= $this->output->action_link(new moodle_url('/mod/assign/view.php',
278                                                           array('id' => $summary->coursemoduleid,
279                                                                 'action'=>'grading')),
280                                                           get_string('viewgrading', 'assign'));
281         $o .= $this->output->container_end();
283         // close the container and insert a spacer
284         $o .= $this->output->container_end();
286         return $o;
287     }
289     /**
290      * render a table containing all the current grades and feedback
291      *
292      * @param assign_feedback_status $status
293      * @return string
294      */
295     public function render_assign_feedback_status(assign_feedback_status $status) {
296         global $DB, $CFG;
297         $o = '';
299         $o .= $this->output->container_start('feedback');
300         $o .= $this->output->heading(get_string('feedback', 'assign'), 3);
301         $o .= $this->output->box_start('boxaligncenter feedbacktable');
302         $t = new html_table();
304         $row = new html_table_row();
305         $cell1 = new html_table_cell(get_string('grade'));
306         $cell2 = new html_table_cell($status->gradefordisplay);
307         $row->cells = array($cell1, $cell2);
308         $t->data[] = $row;
310         $row = new html_table_row();
311         $cell1 = new html_table_cell(get_string('gradedon', 'assign'));
312         $cell2 = new html_table_cell(userdate($status->gradeddate));
313         $row->cells = array($cell1, $cell2);
314         $t->data[] = $row;
316         if ($status->grader) {
317             $row = new html_table_row();
318             $cell1 = new html_table_cell(get_string('gradedby', 'assign'));
319             $cell2 = new html_table_cell($this->output->user_picture($status->grader) . $this->output->spacer(array('width'=>30)) . fullname($status->grader));
320             $row->cells = array($cell1, $cell2);
321             $t->data[] = $row;
322         }
324         foreach ($status->feedbackplugins as $plugin) {
325             if ($plugin->is_enabled() && $plugin->is_visible() && !$plugin->is_empty($status->grade)) {
326                 $row = new html_table_row();
327                 $cell1 = new html_table_cell($plugin->get_name());
328                 $pluginfeedback = new assign_feedback_plugin_feedback($plugin, $status->grade, assign_feedback_plugin_feedback::SUMMARY, $status->coursemoduleid, $status->returnaction, $status->returnparams);
329                 $cell2 = new html_table_cell($this->render($pluginfeedback));
330                 $row->cells = array($cell1, $cell2);
331                 $t->data[] = $row;
332             }
333         }
336         $o .= html_writer::table($t);
337         $o .= $this->output->box_end();
339         $o .= $this->output->container_end();
340         return $o;
341     }
343     /**
344      * render a table containing the current status of the submission
345      *
346      * @param assign_submission_status $status
347      * @return string
348      */
349     public function render_assign_submission_status(assign_submission_status $status) {
350         $o = '';
351         $o .= $this->output->container_start('submissionstatustable');
352         $o .= $this->output->heading(get_string('submissionstatusheading', 'assign'), 3);
353         $time = time();
355         if ($status->allowsubmissionsfromdate &&
356                 $time <= $status->allowsubmissionsfromdate) {
357             $o .= $this->output->box_start('generalbox boxaligncenter submissionsalloweddates');
358             if ($status->alwaysshowdescription) {
359                 $o .= get_string('allowsubmissionsfromdatesummary', 'assign', userdate($status->allowsubmissionsfromdate));
360             } else {
361                 $o .= get_string('allowsubmissionsanddescriptionfromdatesummary', 'assign', userdate($status->allowsubmissionsfromdate));
362             }
363             $o .= $this->output->box_end();
364         }
365         $o .= $this->output->box_start('boxaligncenter submissionsummarytable');
367         $t = new html_table();
369         $row = new html_table_row();
370         $cell1 = new html_table_cell(get_string('submissionstatus', 'assign'));
371         if ($status->submission) {
372             $cell2 = new html_table_cell(get_string('submissionstatus_' . $status->submission->status, 'assign'));
373             $cell2->attributes = array('class'=>'submissionstatus' . $status->submission->status);
374         } else {
375             if (!$status->submissionsenabled) {
376                 $cell2 = new html_table_cell(get_string('noonlinesubmissions', 'assign'));
377             } else {
378                 $cell2 = new html_table_cell(get_string('nosubmission', 'assign'));
379             }
380         }
381         $row->cells = array($cell1, $cell2);
382         $t->data[] = $row;
384         // status
385         if ($status->locked) {
386             $row = new html_table_row();
387             $cell1 = new html_table_cell();
388             $cell2 = new html_table_cell(get_string('submissionslocked', 'assign'));
389             $cell2->attributes = array('class'=>'submissionlocked');
390             $row->cells = array($cell1, $cell2);
391             $t->data[] = $row;
392         }
394         // grading status
395         $row = new html_table_row();
396         $cell1 = new html_table_cell(get_string('gradingstatus', 'assign'));
398         if ($status->graded) {
399             $cell2 = new html_table_cell(get_string('graded', 'assign'));
400             $cell2->attributes = array('class'=>'submissiongraded');
401         } else {
402             $cell2 = new html_table_cell(get_string('notgraded', 'assign'));
403             $cell2->attributes = array('class'=>'submissionnotgraded');
404         }
405         $row->cells = array($cell1, $cell2);
406         $t->data[] = $row;
409         $duedate = $status->duedate;
410         if ($duedate >= 1) {
411             $row = new html_table_row();
412             $cell1 = new html_table_cell(get_string('duedate', 'assign'));
413             $cell2 = new html_table_cell(userdate($duedate));
414             $row->cells = array($cell1, $cell2);
415             $t->data[] = $row;
417             // time remaining
418             $row = new html_table_row();
419             $cell1 = new html_table_cell(get_string('timeremaining', 'assign'));
420             if ($duedate - $time <= 0) {
421                 if (!$status->submission || $status->submission != ASSIGN_SUBMISSION_STATUS_SUBMITTED) {
422                     if ($status->submissionsenabled) {
423                         $cell2 = new html_table_cell(get_string('overdue', 'assign', format_time($time - $duedate)));
424                         $cell2->attributes = array('class'=>'overdue');
425                     } else {
426                         $cell2 = new html_table_cell(get_string('duedatereached', 'assign'));
427                     }
428                 } else {
429                     if ($status->submission->timemodified > $duedate) {
430                         $cell2 = new html_table_cell(get_string('submittedlate', 'assign', format_time($status->submission->timemodified - $duedate)));
431                         $cell2->attributes = array('class'=>'latesubmission');
432                     } else {
433                         $cell2 = new html_table_cell(get_string('submittedearly', 'assign', format_time($status->submission->timemodified - $duedate)));
434                         $cell2->attributes = array('class'=>'earlysubmission');
435                     }
436                 }
437             } else {
438                 $cell2 = new html_table_cell(format_time($duedate - $time));
439             }
440             $row->cells = array($cell1, $cell2);
441             $t->data[] = $row;
442         }
444         // last modified
445         if ($status->submission) {
446             $row = new html_table_row();
447             $cell1 = new html_table_cell(get_string('timemodified', 'assign'));
448             $cell2 = new html_table_cell(userdate($status->submission->timemodified));
449             $row->cells = array($cell1, $cell2);
450             $t->data[] = $row;
452             foreach ($status->submissionplugins as $plugin) {
453                 if ($plugin->is_enabled() && $plugin->is_visible() && !$plugin->is_empty($status->submission)) {
454                     $row = new html_table_row();
455                     $cell1 = new html_table_cell($plugin->get_name());
456                     $pluginsubmission = new assign_submission_plugin_submission($plugin, $status->submission, assign_submission_plugin_submission::SUMMARY, $status->coursemoduleid, $status->returnaction, $status->returnparams);
457                     $cell2 = new html_table_cell($this->render($pluginsubmission));
458                     $row->cells = array($cell1, $cell2);
459                     $t->data[] = $row;
460                 }
461             }
462         }
465         $o .= html_writer::table($t);
466         $o .= $this->output->box_end();
468         // links
469         if ($status->canedit) {
470             $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php',
471                 array('id' => $status->coursemoduleid, 'action' => 'editsubmission')), get_string('editsubmission', 'assign'), 'get');
472         }
474         if ($status->cansubmit) {
475             // submission.php
476             $o .= $this->output->single_button(new moodle_url('/mod/assign/view.php',
477                     array('id' => $status->coursemoduleid, 'action'=>'submit')), get_string('submitassignment', 'assign'), 'get');
478             $o .= $this->output->box_start('boxaligncenter submithelp');
479             $o .= get_string('submitassignment_help', 'assign');
480             $o .= $this->output->box_end();
481         }
483         $o .= $this->output->container_end();
484         return $o;
485     }
487     /**
488      * render a submission plugin submission
489      *
490      * @param assign_submission_plugin_submission $submissionplugin
491      * @return string
492      */
493     public function render_assign_submission_plugin_submission(assign_submission_plugin_submission $submissionplugin) {
494         $o = '';
496         if ($submissionplugin->view == assign_submission_plugin_submission::SUMMARY) {
497             $icon = $this->output->pix_icon('t/preview', get_string('view' . substr($submissionplugin->plugin->get_subtype(), strlen('assign')), 'mod_assign'));
498             $link = '';
499             $showviewlink = false;
500             $summary = $submissionplugin->plugin->view_summary($submissionplugin->submission, $showviewlink);
501             if ($showviewlink) {
502                 $link = $this->output->action_link(
503                                 new moodle_url('/mod/assign/view.php',
504                                                array('id' => $submissionplugin->coursemoduleid,
505                                                      'sid'=>$submissionplugin->submission->id,
506                                                      'plugin'=>$submissionplugin->plugin->get_type(),
507                                                      'action'=>'viewplugin' . $submissionplugin->plugin->get_subtype(),
508                                                      'returnaction'=>$submissionplugin->returnaction,
509                                                      'returnparams'=>http_build_query($submissionplugin->returnparams))),
510                                 $icon);
512                 $link .= $this->output->spacer(array('width'=>15));
513             }
515             $o .= $link . $summary;
516         } else if ($submissionplugin->view == assign_submission_plugin_submission::FULL) {
517             $o .= $this->output->box_start('boxaligncenter submissionfull');
518             $o .= $submissionplugin->plugin->view($submissionplugin->submission);
519             $o .= $this->output->box_end();
520         }
522         return $o;
523     }
525     /**
526      * render the grading table
527      *
528      * @param assign_grading_table $table
529      * @return string
530      */
531     public function render_assign_grading_table(assign_grading_table $table) {
532         $o = '';
533         $o .= $this->output->box_start('boxaligncenter gradingtable');
534         $this->page->requires->js_init_call('M.mod_assign.init_grading_table', array());
535         $this->page->requires->string_for_js('nousersselected', 'assign');
536         $this->page->requires->string_for_js('batchoperationconfirmlock', 'assign');
537         $this->page->requires->string_for_js('batchoperationconfirmunlock', 'assign');
538         $this->page->requires->string_for_js('batchoperationconfirmreverttodraft', 'assign');
539         $this->page->requires->string_for_js('editaction', 'assign');
540         // need to get from prefs
541         $o .= $this->flexible_table($table, $table->get_rows_per_page(), true);
542         $o .= $this->output->box_end();
544         return $o;
545    }
547     /**
548      * Render a feedback plugin feedback
549      *
550      * @param assign_feedback_plugin_feedback $feedbackplugin
551      * @return string
552      */
553     public function render_assign_feedback_plugin_feedback(assign_feedback_plugin_feedback $feedbackplugin) {
554         $o = '';
556         if ($feedbackplugin->view == assign_feedback_plugin_feedback::SUMMARY) {
557             $icon = $this->output->pix_icon('t/preview', get_string('view' . substr($feedbackplugin->plugin->get_subtype(), strlen('assign')), 'mod_assign'));
558             $link = '';
559             $showviewlink = false;
560             $summary = $feedbackplugin->plugin->view_summary($feedbackplugin->grade, $showviewlink);
561             if ($showviewlink) {
562                 $link = $this->output->action_link(
563                                 new moodle_url('/mod/assign/view.php',
564                                                array('id' => $feedbackplugin->coursemoduleid,
565                                                      'gid'=>$feedbackplugin->grade->id,
566                                                      'plugin'=>$feedbackplugin->plugin->get_type(),
567                                                      'action'=>'viewplugin' . $feedbackplugin->plugin->get_subtype(),
568                                                      'returnaction'=>$feedbackplugin->returnaction,
569                                                      'returnparams'=>http_build_query($feedbackplugin->returnparams))),
570                                 $icon);
571                 $link .= $this->output->spacer(array('width'=>15));
572             }
574             $o .= $link . $summary;
575         } else if ($feedbackplugin->view == assign_feedback_plugin_feedback::FULL) {
576             $o .= $this->output->box_start('boxaligncenter feedbackfull');
577             $o .= $feedbackplugin->plugin->view($feedbackplugin->grade);
578             $o .= $this->output->box_end();
579         }
581         return $o;
582     }
586     /**
587      * Internal function - creates htmls structure suitable for YUI tree.
588      *
589      * @param assign_files $tree
590      * @param array $dir
591      * @return string
592      */
593     protected function htmllize_tree(assign_files $tree, $dir) {
594         global $CFG;
595         $yuiconfig = array();
596         $yuiconfig['type'] = 'html';
598         if (empty($dir['subdirs']) and empty($dir['files'])) {
599             return '';
600         }
602         $result = '<ul>';
603         foreach ($dir['subdirs'] as $subdir) {
604             $image = $this->output->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle', array('class'=>'icon'));
605             $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
606         }
608         foreach ($dir['files'] as $file) {
609             $filename = $file->get_filename();
610             if ($CFG->enableplagiarism) {
611                 require_once($CFG->libdir.'/plagiarismlib.php');
612                 $plagiarsmlinks = plagiarism_get_links(array('userid'=>$file->get_userid(), 'file'=>$file, 'cmid'=>$tree->cm->id, 'course'=>$tree->course));
613             } else {
614                 $plagiarsmlinks = '';
615             }
616             $image = $this->output->pix_icon(file_file_icon($file), $filename, 'moodle', array('class'=>'icon'));
617             $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.$file->fileurl.' '.$plagiarsmlinks.$file->portfoliobutton.'</div></li>';
618         }
620         $result .= '</ul>';
622         return $result;
623     }
625     /**
626      * Helper method dealing with the fact we can not just fetch the output of flexible_table
627      *
628      * @param flexible_table $table The table to render
629      * @param int $rowsperpage How many assignments to render in a page
630      * @param bool $displaylinks - Whether to render links in the table (e.g. downloads would not enable this)
631      * @return string HTML
632      */
633     protected function flexible_table(flexible_table $table, $rowsperpage, $displaylinks) {
635         $o = '';
636         ob_start();
637         $table->out($rowsperpage, $displaylinks);
638         $o = ob_get_contents();
639         ob_end_clean();
641         return $o;
642     }
644     /**
645      * Helper method dealing with the fact we can not just fetch the output of moodleforms
646      *
647      * @param moodleform $mform
648      * @return string HTML
649      */
650     protected function moodleform(moodleform $mform) {
652         $o = '';
653         ob_start();
654         $mform->display();
655         $o = ob_get_contents();
656         ob_end_clean();
658         return $o;
659     }