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, ';
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 if (!$this->is_downloading()) {
122 $columns[] = 'select';
123 $headers[] = get_string('select') . '<div class="selectall"><input type="checkbox" name="selectall" title="' . get_string('selectall') . '"/></div>';
127 $columns[] = 'picture';
128 $headers[] = get_string('pictureofuser');
131 $columns[] = 'fullname';
132 $headers[] = get_string('fullname');
135 if ($assignment->is_any_submission_plugin_enabled()) {
136 $columns[] = 'status';
137 $headers[] = get_string('status');
142 $columns[] = 'grade';
143 $headers[] = get_string('grade');
144 if (!$this->is_downloading()) {
145 // We have to call this column userid so we can use userid as a default sortable column.
146 $columns[] = 'userid';
147 $headers[] = get_string('edit');
150 // Submission plugins
151 if ($assignment->is_any_submission_plugin_enabled()) {
152 $columns[] = 'timesubmitted';
153 $headers[] = get_string('lastmodifiedsubmission', 'assign');
155 foreach ($this->assignment->get_submission_plugins() as $plugin) {
156 if ($plugin->is_visible() && $plugin->is_enabled()) {
157 $columns[] = 'assignsubmission_' . $plugin->get_type();
158 $headers[] = $plugin->get_name();
164 $columns[] = 'timemarked';
165 $headers[] = get_string('lastmodifiedgrade', 'assign');
168 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
169 if ($plugin->is_visible() && $plugin->is_enabled()) {
170 $columns[] = 'assignfeedback_' . $plugin->get_type();
171 $headers[] = $plugin->get_name();
176 $columns[] = 'finalgrade';
177 $headers[] = get_string('finalgrade', 'grades');
179 // load the grading info for all users
180 $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id, 'mod', 'assign', $this->assignment->get_instance()->id, $users);
182 if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
183 $columns[] = 'outcomes';
184 $headers[] = get_string('outcomes', 'grades');
189 $this->define_columns($columns);
190 $this->define_headers($headers);
191 // We require at least one unique column for the sort.
192 $this->sortable(true, 'userid');
193 $this->no_sorting('finalgrade');
194 $this->no_sorting('userid');
195 $this->no_sorting('select');
196 $this->no_sorting('outcomes');
198 foreach ($this->assignment->get_submission_plugins() as $plugin) {
199 if ($plugin->is_visible() && $plugin->is_enabled()) {
200 $this->no_sorting('assignsubmission_' . $plugin->get_type());
203 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
204 if ($plugin->is_visible() && $plugin->is_enabled()) {
205 $this->no_sorting('assignfeedback_' . $plugin->get_type());
212 * Before adding each row to the table make sure rownum is incremented
214 * @param array $row row of data from db used to make one row of the table.
215 * @return array one row for the table
217 function format_row($row) {
218 if ($this->rownum < 0) {
219 $this->rownum = $this->currpage * $this->pagesize;
224 return parent::format_row($row);
228 * Add the userid to the row class so it can be updated via ajax
230 * @param stdClass $row The row of data
231 * @return string The row class
233 function get_row_class($row) {
234 return 'user' . $row->userid;
238 * Return the number of rows to display on a single page
240 * @return int The number of rows per page
242 function get_rows_per_page() {
243 return $this->perpage;
247 * Display a grade with scales etc.
249 * @param string $grade
250 * @param boolean $editable
251 * @param int $userid The user id of the user this grade belongs to
252 * @param int $modified Timestamp showing when the grade was last modified
253 * @return string The formatted grade
255 function display_grade($grade, $editable, $userid, $modified) {
256 if ($this->is_downloading()) {
259 $o = $this->assignment->display_grade($grade, $editable, $userid, $modified);
264 * Format a list of outcomes
266 * @param stdClass $row
269 function col_outcomes(stdClass $row) {
271 foreach($this->gradinginfo->outcomes as $index=>$outcome) {
272 $options = make_grades_menu(-$outcome->scaleid);
274 $options[0] = get_string('nooutcome', 'grades');
275 if ($this->quickgrading && !($outcome->grades[$row->userid]->locked)) {
276 $select = '<select name="outcome_' . $index . '_' . $row->userid . '" class="quickgrade">';
277 foreach ($options as $optionindex => $optionvalue) {
279 if ($outcome->grades[$row->userid]->grade == $optionindex) {
280 $selected = 'selected="selected"';
282 $select .= '<option value="' . $optionindex . '"' . $selected . '>' . $optionvalue . '</option>';
284 $select .= '</select>';
285 $outcomes .= $this->output->container($outcome->name . ': ' . $select, 'outcome');
287 $outcomes .= $this->output->container($outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade], 'outcome');
296 * Format a user picture for display
298 * @param stdClass $row
301 function col_picture(stdClass $row) {
303 return $this->output->user_picture($row);
309 * Format a user record for display (link to profile)
311 * @param stdClass $row
314 function col_fullname($row) {
315 $courseid = $this->assignment->get_course()->id;
316 $link= new moodle_url('/user/view.php', array('id' =>$row->id, 'course'=>$courseid));
317 return $this->output->action_link($link, fullname($row));
321 * Insert a checkbox for selecting the current row for batch operations
323 * @param stdClass $row
326 function col_select(stdClass $row) {
327 return '<input type="checkbox" name="selectedusers" value="' . $row->userid . '"/>';
331 * Return a users grades from the listing of all grade data for this assignment
334 * @return mixed stdClass or false
336 private function get_gradebook_data_for_user($userid) {
337 if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
338 return $this->gradinginfo->items[0]->grades[$userid];
344 * Format a column of data for display
346 * @param stdClass $row
349 function col_grade(stdClass $row) {
356 if (!$this->is_downloading()) {
357 $icon = $this->output->pix_icon('gradefeedback', get_string('grade'), 'mod_assign');
358 $url = new moodle_url('/mod/assign/view.php',
359 array('id' => $this->assignment->get_course_module()->id,
360 'rownum'=>$this->rownum,'action'=>'grade'));
361 $link = $this->output->action_link($url, $icon);
362 $separator = $this->output->spacer(array(), true);
364 $gradingdisabled = $this->assignment->grading_disabled($row->id);
365 $grade = $this->display_grade($row->grade, $this->quickgrading && !$gradingdisabled, $row->userid, $row->timemarked);
367 //return $grade . $separator . $link;
368 return $link . $separator . $grade;
372 * Format a column of data for display
374 * @param stdClass $row
377 function col_finalgrade(stdClass $row) {
380 $grade = $this->get_gradebook_data_for_user($row->userid);
382 $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
389 * Format a column of data for display
391 * @param stdClass $row
394 function col_timemarked(stdClass $row) {
397 if ($row->timemarked && $row->grade !== NULL && $row->grade >= 0) {
398 $o = userdate($row->timemarked);
405 * Format a column of data for display
407 * @param stdClass $row
410 function col_timesubmitted(stdClass $row) {
413 if ($row->timesubmitted) {
414 $o = userdate($row->timesubmitted);
421 * Format a column of data for display
423 * @param stdClass $row
426 function col_status(stdClass $row) {
429 if ($this->assignment->is_any_submission_plugin_enabled()) {
431 $o .= $this->output->container(get_string('submissionstatus_' . $row->status, 'assign'), array('class'=>'submissionstatus' .$row->status));
432 if ($this->assignment->get_instance()->duedate && $row->timesubmitted > $this->assignment->get_instance()->duedate) {
433 $o .= $this->output->container(get_string('submittedlateshort', 'assign', format_time($row->timesubmitted - $this->assignment->get_instance()->duedate)), 'latesubmission');
436 $o .= $this->output->container(get_string('submissionslockedshort', 'assign'), 'lockedsubmission');
438 if ($row->grade !== NULL && $row->grade >= 0) {
439 $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
447 * Format a column of data for display
449 * @param stdClass $row
452 function col_userid(stdClass $row) {
457 $url = new moodle_url('/mod/assign/view.php',
458 array('id' => $this->assignment->get_course_module()->id,
459 'rownum'=>$this->rownum,'action'=>'grade'));
461 $description = get_string('grade');
463 $description = get_string('updategrade','assign');
465 $actions[$url->out(false)] = $description;
467 if (!$row->status || $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT || !$this->assignment->get_instance()->submissiondrafts) {
469 $url = new moodle_url('/mod/assign/view.php', array('id' => $this->assignment->get_course_module()->id,
472 'sesskey'=>sesskey(),
473 'page'=>$this->currpage));
474 $description = get_string('preventsubmissionsshort', 'assign');
475 $actions[$url->out(false)] = $description;
477 $url = new moodle_url('/mod/assign/view.php', array('id' => $this->assignment->get_course_module()->id,
480 'sesskey'=>sesskey(),
481 'page'=>$this->currpage));
482 $description = get_string('allowsubmissionsshort', 'assign');
483 $actions[$url->out(false)] = $description;
486 if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED && $this->assignment->get_instance()->submissiondrafts) {
487 $url = new moodle_url('/mod/assign/view.php', array('id' => $this->assignment->get_course_module()->id,
489 'action'=>'reverttodraft',
490 'sesskey'=>sesskey(),
491 'page'=>$this->currpage));
492 $description = get_string('reverttodraftshort', 'assign');
493 $actions[$url->out(false)] = $description;
496 $edit .= $this->output->container_start(array('yui3-menu', 'actionmenu'), 'actionselect' . $row->id);
497 $edit .= $this->output->container_start(array('yui3-menu-content'));
498 $edit .= html_writer::start_tag('ul');
499 $edit .= html_writer::start_tag('li', array('class'=>'menuicon'));
501 $menuicon = $this->output->pix_icon('i/menu', get_string('actions'));
502 $edit .= $this->output->action_link('#menu' . $row->id, $menuicon, null, array('class'=>'yui3-menu-label'));
503 $edit .= $this->output->container_start(array('yui3-menu', 'yui3-loading'), 'menu' . $row->id);
504 $edit .= $this->output->container_start(array('yui3-menu-content'));
505 $edit .= html_writer::start_tag('ul');
507 foreach ($actions as $url => $description) {
508 $edit .= html_writer::start_tag('li', array('class'=>'yui3-menuitem'));
510 $edit .= $this->output->action_link($url, $description, null, array('class'=>'yui3-menuitem-content'));
512 $edit .= html_writer::end_tag('li');
514 $edit .= html_writer::end_tag('ul');
515 $edit .= $this->output->container_end();
516 $edit .= $this->output->container_end();
517 $edit .= html_writer::end_tag('li');
518 $edit .= html_writer::end_tag('ul');
520 $edit .= $this->output->container_end();
521 $edit .= $this->output->container_end();
527 * Write the plugin summary with an optional link to view the full feedback/submission.
529 * @param assign_plugin $plugin Submission plugin or feedback plugin
530 * @param stdClass $item Submission or grade
531 * @param string $returnaction The return action to pass to the view_submission page (the current page)
532 * @param string $returnparams The return params to pass to the view_submission page (the current page)
533 * @return string The summary with an optional link
535 private function format_plugin_summary_with_link(assign_plugin $plugin, stdClass $item, $returnaction, $returnparams) {
537 $showviewlink = false;
539 $summary = $plugin->view_summary($item, $showviewlink);
542 $icon = $this->output->pix_icon('t/preview', get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'mod_assign'));
543 $link = $this->output->action_link(
544 new moodle_url('/mod/assign/view.php',
545 array('id' => $this->assignment->get_course_module()->id,
548 'plugin'=>$plugin->get_type(),
549 'action'=>'viewplugin' . $plugin->get_subtype(),
550 'returnaction'=>$returnaction,
551 'returnparams'=>http_build_query($returnparams))),
553 $separator = $this->output->spacer(array(), true);
556 return $link . $separator . $summary;
561 * Format the submission and feedback columns
563 * @param string $colname The column name
564 * @param stdClass $row The submission row
565 * @return mixed string or NULL
567 function other_cols($colname, $row){
568 if (($pos = strpos($colname, 'assignsubmission_')) !== false) {
569 $plugin = $this->assignment->get_submission_plugin_by_type(substr($colname, strlen('assignsubmission_')));
571 if ($plugin->is_visible() && $plugin->is_enabled()) {
572 if ($row->submissionid) {
573 $submission = new stdClass();
574 $submission->id = $row->submissionid;
575 $submission->timecreated = $row->firstsubmission;
576 $submission->timemodified = $row->timesubmitted;
577 $submission->assignment = $this->assignment->get_instance()->id;
578 $submission->userid = $row->userid;
579 return $this->format_plugin_summary_with_link($plugin, $submission, 'grading', array());
584 if (($pos = strpos($colname, 'feedback_')) !== false) {
585 $plugin = $this->assignment->get_feedback_plugin_by_type(substr($colname, strlen('assignfeedback_')));
586 if ($plugin->is_visible() && $plugin->is_enabled()) {
589 $grade = new stdClass();
590 $grade->id = $row->gradeid;
591 $grade->timecreated = $row->firstmarked;
592 $grade->timemodified = $row->timemarked;
593 $grade->assignment = $this->assignment->get_instance()->id;
594 $grade->userid = $row->userid;
595 $grade->grade = $row->grade;
596 $grade->mailed = $row->mailed;
598 if ($this->quickgrading && $plugin->supports_quickgrading()) {
599 return $plugin->get_quickgrading_html($row->userid, $grade);
601 return $this->format_plugin_summary_with_link($plugin, $grade, 'grading', array());
610 * Using the current filtering and sorting - load all rows and return a single column from them
612 * @param string $columnname The name of the raw column data
613 * @return array of data
615 function get_column_data($columnname) {
618 $this->query_db($this->tablemaxrows);
620 foreach ($this->rawdata as $row) {
621 $result[] = $row->$columnname;
626 * Using the current filtering and sorting - load a single row and return a single column from it
628 * @param int $rownumber The rownumber to load
629 * @param string $columnname The name of the raw column data
630 * @param bool $lastrow Set to true if this is the last row in the table
631 * @return mixed string or false
633 function get_cell_data($rownumber, $columnname, $lastrow) {
635 $this->currpage = $rownumber;
637 if ($rownumber == $this->totalrows-1) {
640 foreach ($this->rawdata as $row) {
641 return $row->$columnname;
647 * Return things to the renderer
649 * @return string the assignment name
651 function get_assignment_name() {
652 return $this->assignment->get_instance()->name;
656 * Return things to the renderer
658 * @return int the course module id
660 function get_course_module_id() {
661 return $this->assignment->get_course_module()->id;
665 * Return things to the renderer
667 * @return int the course id
669 function get_course_id() {
670 return $this->assignment->get_course()->id;
674 * Return things to the renderer
676 * @return stdClass The course context
678 function get_course_context() {
679 return $this->assignment->get_course_context();
683 * Return things to the renderer
685 * @return bool Does this assignment accept submissions
687 function submissions_enabled() {
688 return $this->assignment->is_any_submission_plugin_enabled();
692 * Return things to the renderer
694 * @return bool Can this user view all grades (the gradebook)
696 function can_view_all_grades() {
697 return has_capability('gradereport/grader:view', $this->assignment->get_course_context()) && has_capability('moodle/grade:viewall', $this->assignment->get_course_context());
701 * Override the table show_hide_link to not show for select column
703 * @param string $column the column name, index into various names.
704 * @param int $index numerical index of the column.
705 * @return string HTML fragment.
707 protected function show_hide_link($column, $index) {
709 return parent::show_hide_link($column, $index);