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 the definition for the grading table which subclassses easy_table
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 require_once($CFG->libdir.'/tablelib.php');
28 require_once($CFG->libdir.'/gradelib.php');
29 require_once($CFG->dirroot.'/mod/assign/locallib.php');
32 * Extends table_sql to provide a table of assignment submissions
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 assign_grading_table extends table_sql implements renderable {
39 /** @var assign $assignment */
40 private $assignment = null;
41 /** @var int $perpage */
42 private $perpage = 10;
43 /** @var int $rownum (global index of current row in table) */
45 /** @var renderer_base for getting output */
46 private $output = null;
47 /** @var stdClass gradinginfo */
48 private $gradinginfo = null;
49 /** @var int $tablemaxrows */
50 private $tablemaxrows = 10000;
51 /** @var boolean $quickgrading */
52 private $quickgrading = false;
55 * overridden constructor keeps a reference to the assignment class that is displaying this table
57 * @param assign $assignment The assignment class
58 * @param int $perpage how many per page
59 * @param string $filter The current filter
60 * @param int $rowoffset For showing a subsequent page of results
61 * @param bool $quickgrading Is this table wrapped in a quickgrading form?
63 function __construct(assign $assignment, $perpage, $filter, $rowoffset, $quickgrading) {
64 global $CFG, $PAGE, $DB;
65 parent::__construct('mod_assign_grading');
66 $this->assignment = $assignment;
67 $this->perpage = $perpage;
68 $this->quickgrading = $quickgrading;
69 $this->output = $PAGE->get_renderer('mod_assign');
71 $this->define_baseurl(new moodle_url($CFG->wwwroot . '/mod/assign/view.php', array('action'=>'grading', 'id'=>$assignment->get_course_module()->id)));
73 // do some business - then set the sql
75 $currentgroup = groups_get_activity_group($assignment->get_course_module(), true);
78 $this->rownum = $rowoffset - 1;
81 $users = array_keys( $assignment->list_participants($currentgroup, true));
82 if (count($users) == 0) {
83 // insert a record that will never match to the sql is still valid.
88 $params['assignmentid1'] = (int)$this->assignment->get_instance()->id;
89 $params['assignmentid2'] = (int)$this->assignment->get_instance()->id;
91 $fields = user_picture::fields('u') . ', u.id as userid, u.firstname as firstname, u.lastname as lastname, ';
92 $fields .= 's.status as status, s.id as submissionid, s.timecreated as firstsubmission, s.timemodified as timesubmitted, ';
93 $fields .= 'g.id as gradeid, g.grade as grade, g.timemodified as timemarked, g.timecreated as firstmarked, g.mailed as mailed, g.locked as locked';
94 $from = '{user} u LEFT JOIN {assign_submission} s ON u.id = s.userid AND s.assignment = :assignmentid1' .
95 ' LEFT JOIN {assign_grades} g ON u.id = g.userid AND g.assignment = :assignmentid2';
97 $userparams = array();
100 list($userwhere, $userparams) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
101 $where = 'u.id ' . $userwhere;
102 $params = array_merge($params, $userparams);
104 if ($filter == ASSIGN_FILTER_SUBMITTED) {
105 $where .= ' AND s.timecreated > 0 ';
107 if ($filter == ASSIGN_FILTER_REQUIRE_GRADING) {
108 $where .= ' AND (s.timemodified > g.timemodified OR (s.timemodified IS NOT NULL AND g.timemodified IS NULL))';
110 if (strpos($filter, ASSIGN_FILTER_SINGLE_USER) === 0) {
111 $userfilter = (int) array_pop(explode('=', $filter));
112 $where .= ' AND (u.id = :userid)';
113 $params['userid'] = $userfilter;
115 $this->set_sql($fields, $from, $where, $params);
121 $columns[] = 'select';
122 $headers[] = get_string('select') . '<div class="selectall"><input type="checkbox" name="selectall" title="' . get_string('selectall') . '"/></div>';
125 if (!$this->is_downloading()) {
127 $headers[] = get_string('edit');
131 $columns[] = 'picture';
132 $headers[] = get_string('pictureofuser');
135 $columns[] = 'fullname';
136 $headers[] = get_string('fullname');
139 if ($assignment->is_any_submission_plugin_enabled()) {
140 $columns[] = 'status';
141 $headers[] = get_string('status');
146 $columns[] = 'grade';
147 $headers[] = get_string('grade');
149 // Submission plugins
150 if ($assignment->is_any_submission_plugin_enabled()) {
151 $columns[] = 'timesubmitted';
152 $headers[] = get_string('lastmodifiedsubmission', 'assign');
154 foreach ($this->assignment->get_submission_plugins() as $plugin) {
155 if ($plugin->is_visible() && $plugin->is_enabled()) {
156 $columns[] = 'assignsubmission_' . $plugin->get_type();
157 $headers[] = $plugin->get_name();
163 $columns[] = 'timemarked';
164 $headers[] = get_string('lastmodifiedgrade', 'assign');
167 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
168 if ($plugin->is_visible() && $plugin->is_enabled()) {
169 $columns[] = 'assignfeedback_' . $plugin->get_type();
170 $headers[] = $plugin->get_name();
175 $columns[] = 'finalgrade';
176 $headers[] = get_string('finalgrade', 'grades');
178 // load the grading info for all users
179 $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id, 'mod', 'assign', $this->assignment->get_instance()->id, $users);
181 if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
182 $columns[] = 'outcomes';
183 $headers[] = get_string('outcomes', 'grades');
188 $this->define_columns($columns);
189 $this->define_headers($headers);
190 $this->no_sorting('finalgrade');
191 $this->no_sorting('edit');
192 $this->no_sorting('select');
193 $this->no_sorting('outcomes');
195 foreach ($this->assignment->get_submission_plugins() as $plugin) {
196 if ($plugin->is_visible() && $plugin->is_enabled()) {
197 $this->no_sorting('assignsubmission_' . $plugin->get_type());
200 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
201 if ($plugin->is_visible() && $plugin->is_enabled()) {
202 $this->no_sorting('assignfeedback_' . $plugin->get_type());
209 * Add the userid to the row class so it can be updated via ajax
211 * @param stdClass $row The row of data
212 * @return string The row class
214 function get_row_class($row) {
215 return 'user' . $row->userid;
219 * Return the number of rows to display on a single page
221 * @return int The number of rows per page
223 function get_rows_per_page() {
224 return $this->perpage;
228 * Display a grade with scales etc.
230 * @param string $grade
231 * @param boolean $editable
232 * @param int $userid The user id of the user this grade belongs to
233 * @param int $modified Timestamp showing when the grade was last modified
234 * @return string The formatted grade
236 function display_grade($grade, $editable, $userid, $modified) {
237 if ($this->is_downloading()) {
240 $o = $this->assignment->display_grade($grade, $editable, $userid, $modified);
245 * Format a list of outcomes
247 * @param stdClass $row
250 function col_outcomes(stdClass $row) {
252 foreach($this->gradinginfo->outcomes as $index=>$outcome) {
253 $options = make_grades_menu(-$outcome->scaleid);
255 $options[0] = get_string('nooutcome', 'grades');
256 if ($this->quickgrading && !($outcome->grades[$row->userid]->locked)) {
257 $select = '<select name="outcome_' . $index . '_' . $row->userid . '" class="quickgrade">';
258 foreach ($options as $optionindex => $optionvalue) {
260 if ($outcome->grades[$row->userid]->grade == $optionindex) {
261 $selected = 'selected="selected"';
263 $select .= '<option value="' . $optionindex . '"' . $selected . '>' . $optionvalue . '</option>';
265 $select .= '</select>';
266 $outcomes .= $this->output->container($outcome->name . ': ' . $select, 'outcome');
268 $outcomes .= $this->output->container($outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade], 'outcome');
277 * Format a user picture for display (and update rownum as a sideeffect)
279 * @param stdClass $row
282 function col_picture(stdClass $row) {
284 return $this->output->user_picture($row);
290 * Format a user record for display (link to profile)
292 * @param stdClass $row
295 function col_fullname($row) {
296 $courseid = $this->assignment->get_course()->id;
297 $link= new moodle_url('/user/view.php', array('id' =>$row->id, 'course'=>$courseid));
298 return $this->output->action_link($link, fullname($row));
302 * Insert a checkbox for selecting the current row for batch operations
304 * @param stdClass $row
307 function col_select(stdClass $row) {
308 return '<input type="checkbox" name="selectedusers" value="' . $row->userid . '"/>';
312 * Return a users grades from the listing of all grade data for this assignment
315 * @return mixed stdClass or false
317 private function get_gradebook_data_for_user($userid) {
318 if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
319 return $this->gradinginfo->items[0]->grades[$userid];
325 * Format a column of data for display
327 * @param stdClass $row
330 function col_grade(stdClass $row) {
337 if (!$this->is_downloading()) {
338 $icon = $this->output->pix_icon('gradefeedback', get_string('grade'), 'mod_assign');
339 $url = new moodle_url('/mod/assign/view.php',
340 array('id' => $this->assignment->get_course_module()->id,
341 'rownum'=>$this->rownum,'action'=>'grade'));
342 $link = $this->output->action_link($url, $icon);
343 $separator = $this->output->spacer(array(), true);
345 $gradingdisabled = $this->assignment->grading_disabled($row->id);
346 $grade = $this->display_grade($row->grade, $this->quickgrading && !$gradingdisabled, $row->userid, $row->timemarked);
348 //return $grade . $separator . $link;
349 return $link . $separator . $grade;
353 * Format a column of data for display
355 * @param stdClass $row
358 function col_finalgrade(stdClass $row) {
361 $grade = $this->get_gradebook_data_for_user($row->userid);
363 $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
370 * Format a column of data for display
372 * @param stdClass $row
375 function col_timemarked(stdClass $row) {
378 if ($row->timemarked && $row->grade !== NULL && $row->grade >= 0) {
379 $o = userdate($row->timemarked);
386 * Format a column of data for display
388 * @param stdClass $row
391 function col_timesubmitted(stdClass $row) {
394 if ($row->timesubmitted) {
395 $o = userdate($row->timesubmitted);
402 * Format a column of data for display
404 * @param stdClass $row
407 function col_status(stdClass $row) {
410 if ($this->assignment->is_any_submission_plugin_enabled()) {
412 $o .= $this->output->container(get_string('submissionstatus_' . $row->status, 'assign'), array('class'=>'submissionstatus' .$row->status));
413 if ($this->assignment->get_instance()->duedate && $row->timesubmitted > $this->assignment->get_instance()->duedate) {
414 $o .= $this->output->container(get_string('submittedlateshort', 'assign', format_time($row->timesubmitted - $this->assignment->get_instance()->duedate)), 'latesubmission');
417 $o .= $this->output->container(get_string('submissionslockedshort', 'assign'), 'lockedsubmission');
419 if ($row->grade !== NULL && $row->grade >= 0) {
420 $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
428 * Format a column of data for display
430 * @param stdClass $row
433 function col_edit(stdClass $row) {
435 if ($this->rownum < 0) {
436 $this->rownum = $this->currpage * $this->pagesize;
443 $url = new moodle_url('/mod/assign/view.php',
444 array('id' => $this->assignment->get_course_module()->id,
445 'rownum'=>$this->rownum,'action'=>'grade'));
447 $description = get_string('grade');
449 $description = get_string('updategrade','assign');
451 $actions[$url->out(false)] = $description;
453 if (!$row->status || $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT || !$this->assignment->get_instance()->submissiondrafts) {
455 $url = new moodle_url('/mod/assign/view.php', array('id' => $this->assignment->get_course_module()->id,
458 'sesskey'=>sesskey(),
459 'page'=>$this->currpage));
460 $description = get_string('preventsubmissionsshort', 'assign');
461 $actions[$url->out(false)] = $description;
463 $url = new moodle_url('/mod/assign/view.php', array('id' => $this->assignment->get_course_module()->id,
466 'sesskey'=>sesskey(),
467 'page'=>$this->currpage));
468 $description = get_string('allowsubmissionsshort', 'assign');
469 $actions[$url->out(false)] = $description;
472 if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED && $this->assignment->get_instance()->submissiondrafts) {
473 $url = new moodle_url('/mod/assign/view.php', array('id' => $this->assignment->get_course_module()->id,
475 'action'=>'reverttodraft',
476 'sesskey'=>sesskey(),
477 'page'=>$this->currpage));
478 $description = get_string('reverttodraftshort', 'assign');
479 $actions[$url->out(false)] = $description;
482 $edit .= $this->output->container_start(array('yui3-menu', 'actionmenu'), 'actionselect' . $row->id);
483 $edit .= $this->output->container_start(array('yui3-menu-content'));
484 $edit .= html_writer::start_tag('ul');
485 $edit .= html_writer::start_tag('li', array('class'=>'menuicon'));
487 $menuicon = $this->output->pix_icon('i/menu', get_string('actions'));
488 $edit .= $this->output->action_link('#menu' . $row->id, $menuicon, null, array('class'=>'yui3-menu-label'));
489 $edit .= $this->output->container_start(array('yui3-menu', 'yui3-loading'), 'menu' . $row->id);
490 $edit .= $this->output->container_start(array('yui3-menu-content'));
491 $edit .= html_writer::start_tag('ul');
493 foreach ($actions as $url => $description) {
494 $edit .= html_writer::start_tag('li', array('class'=>'yui3-menuitem'));
496 $edit .= $this->output->action_link($url, $description, null, array('class'=>'yui3-menuitem-content'));
498 $edit .= html_writer::end_tag('li');
500 $edit .= html_writer::end_tag('ul');
501 $edit .= $this->output->container_end();
502 $edit .= $this->output->container_end();
503 $edit .= html_writer::end_tag('li');
504 $edit .= html_writer::end_tag('ul');
506 $edit .= $this->output->container_end();
507 $edit .= $this->output->container_end();
513 * Write the plugin summary with an optional link to view the full feedback/submission.
515 * @param assign_plugin $plugin Submission plugin or feedback plugin
516 * @param stdClass $item Submission or grade
517 * @param string $returnaction The return action to pass to the view_submission page (the current page)
518 * @param string $returnparams The return params to pass to the view_submission page (the current page)
519 * @return string The summary with an optional link
521 private function format_plugin_summary_with_link(assign_plugin $plugin, stdClass $item, $returnaction, $returnparams) {
523 $showviewlink = false;
525 $summary = $plugin->view_summary($item, $showviewlink);
528 $icon = $this->output->pix_icon('t/preview', get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'mod_assign'));
529 $link = $this->output->action_link(
530 new moodle_url('/mod/assign/view.php',
531 array('id' => $this->assignment->get_course_module()->id,
534 'plugin'=>$plugin->get_type(),
535 'action'=>'viewplugin' . $plugin->get_subtype(),
536 'returnaction'=>$returnaction,
537 'returnparams'=>http_build_query($returnparams))),
539 $separator = $this->output->spacer(array(), true);
542 return $link . $separator . $summary;
547 * Format the submission and feedback columns
549 * @param string $colname The column name
550 * @param stdClass $row The submission row
551 * @return mixed string or NULL
553 function other_cols($colname, $row){
554 if (($pos = strpos($colname, 'assignsubmission_')) !== false) {
555 $plugin = $this->assignment->get_submission_plugin_by_type(substr($colname, strlen('assignsubmission_')));
557 if ($plugin->is_visible() && $plugin->is_enabled()) {
558 if ($row->submissionid) {
559 $submission = new stdClass();
560 $submission->id = $row->submissionid;
561 $submission->timecreated = $row->firstsubmission;
562 $submission->timemodified = $row->timesubmitted;
563 $submission->assignment = $this->assignment->get_instance()->id;
564 $submission->userid = $row->userid;
565 return $this->format_plugin_summary_with_link($plugin, $submission, 'grading', array());
570 if (($pos = strpos($colname, 'feedback_')) !== false) {
571 $plugin = $this->assignment->get_feedback_plugin_by_type(substr($colname, strlen('assignfeedback_')));
572 if ($plugin->is_visible() && $plugin->is_enabled()) {
575 $grade = new stdClass();
576 $grade->id = $row->gradeid;
577 $grade->timecreated = $row->firstmarked;
578 $grade->timemodified = $row->timemarked;
579 $grade->assignment = $this->assignment->get_instance()->id;
580 $grade->userid = $row->userid;
581 $grade->grade = $row->grade;
582 $grade->mailed = $row->mailed;
584 if ($this->quickgrading && $plugin->supports_quickgrading()) {
585 return $plugin->get_quickgrading_html($row->userid, $grade);
587 return $this->format_plugin_summary_with_link($plugin, $grade, 'grading', array());
596 * Using the current filtering and sorting - load all rows and return a single column from them
598 * @param string $columnname The name of the raw column data
599 * @return array of data
601 function get_column_data($columnname) {
604 $this->query_db($this->tablemaxrows);
606 foreach ($this->rawdata as $row) {
607 $result[] = $row->$columnname;
612 * Using the current filtering and sorting - load a single row and return a single column from it
614 * @param int $rownumber The rownumber to load
615 * @param string $columnname The name of the raw column data
616 * @param bool $lastrow Set to true if this is the last row in the table
617 * @return mixed string or false
619 function get_cell_data($rownumber, $columnname, $lastrow) {
621 $this->currpage = $rownumber;
623 if ($rownumber == $this->totalrows-1) {
626 foreach ($this->rawdata as $row) {
627 return $row->$columnname;
633 * Return things to the renderer
635 * @return string the assignment name
637 function get_assignment_name() {
638 return $this->assignment->get_instance()->name;
642 * Return things to the renderer
644 * @return int the course module id
646 function get_course_module_id() {
647 return $this->assignment->get_course_module()->id;
651 * Return things to the renderer
653 * @return int the course id
655 function get_course_id() {
656 return $this->assignment->get_course()->id;
660 * Return things to the renderer
662 * @return stdClass The course context
664 function get_course_context() {
665 return $this->assignment->get_course_context();
669 * Return things to the renderer
671 * @return bool Does this assignment accept submissions
673 function submissions_enabled() {
674 return $this->assignment->is_any_submission_plugin_enabled();
678 * Return things to the renderer
680 * @return bool Can this user view all grades (the gradebook)
682 function can_view_all_grades() {
683 return has_capability('gradereport/grader:view', $this->assignment->get_course_context()) && has_capability('moodle/grade:viewall', $this->assignment->get_course_context());
687 * Override the table show_hide_link to not show for select column
689 * @param string $column the column name, index into various names.
690 * @param int $index numerical index of the column.
691 * @return string HTML fragment.
693 protected function show_hide_link($column, $index) {
695 return parent::show_hide_link($column, $index);