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;
53 /** @var boolean $hasgrantextension - Only do the capability check once for the entire table */
54 private $hasgrantextension = false;
55 /** @var boolean $hasgrade - Only do the capability check once for the entire table */
56 private $hasgrade = false;
57 /** @var array $groupsubmissions - A static cache of group submissions */
58 private $groupsubmissions = array();
59 /** @var array $submissiongroups - A static cache of submission groups */
60 private $submissiongroups = array();
61 /** @var string $plugingradingbatchoperations - List of plugin supported batch operations */
62 public $plugingradingbatchoperations = array();
63 /** @var array $plugincache - A cache of plugin lookups to match a column name to a plugin efficiently */
64 private $plugincache = array();
65 /** @var array $scale - A list of the keys and descriptions for the custom scale */
66 private $scale = null;
69 * overridden constructor keeps a reference to the assignment class that is displaying this table
71 * @param assign $assignment The assignment class
72 * @param int $perpage how many per page
73 * @param string $filter The current filter
74 * @param int $rowoffset For showing a subsequent page of results
75 * @param bool $quickgrading Is this table wrapped in a quickgrading form?
76 * @param string $downloadfilename
78 public function __construct(assign $assignment,
83 $downloadfilename = null) {
84 global $CFG, $PAGE, $DB, $USER;
85 parent::__construct('mod_assign_grading');
86 $this->is_persistent(true);
87 $this->assignment = $assignment;
89 // Check permissions up front.
90 $this->hasgrantextension = has_capability('mod/assign:grantextension',
91 $this->assignment->get_context());
92 $this->hasgrade = $this->assignment->can_grade();
94 // Check if we have the elevated view capablities to see the blind details.
95 $this->hasviewblind = has_capability('mod/assign:viewblinddetails',
96 $this->assignment->get_context());
98 foreach ($assignment->get_feedback_plugins() as $plugin) {
99 if ($plugin->is_visible() && $plugin->is_enabled()) {
100 foreach ($plugin->get_grading_batch_operations() as $action => $description) {
101 if (empty($this->plugingradingbatchoperations)) {
102 $this->plugingradingbatchoperations[$plugin->get_type()] = array();
104 $this->plugingradingbatchoperations[$plugin->get_type()][$action] = $description;
108 $this->perpage = $perpage;
109 $this->quickgrading = $quickgrading && $this->hasgrade;
110 $this->output = $PAGE->get_renderer('mod_assign');
112 $urlparams = array('action'=>'grading', 'id'=>$assignment->get_course_module()->id);
113 $url = new moodle_url($CFG->wwwroot . '/mod/assign/view.php', $urlparams);
114 $this->define_baseurl($url);
116 // Do some business - then set the sql.
117 $currentgroup = groups_get_activity_group($assignment->get_course_module(), true);
120 $this->rownum = $rowoffset - 1;
123 $users = array_keys( $assignment->list_participants($currentgroup, true));
124 if (count($users) == 0) {
125 // Insert a record that will never match to the sql is still valid.
130 $params['assignmentid1'] = (int)$this->assignment->get_instance()->id;
131 $params['assignmentid2'] = (int)$this->assignment->get_instance()->id;
132 $params['assignmentid3'] = (int)$this->assignment->get_instance()->id;
134 $extrauserfields = get_extra_user_fields($this->assignment->get_context());
136 $fields = user_picture::fields('u', $extrauserfields) . ', ';
137 $fields .= 'u.id as userid, ';
138 $fields .= 's.status as status, ';
139 $fields .= 's.id as submissionid, ';
140 $fields .= 's.timecreated as firstsubmission, ';
141 $fields .= 's.timemodified as timesubmitted, ';
142 $fields .= 's.attemptnumber as attemptnumber, ';
143 $fields .= 'g.id as gradeid, ';
144 $fields .= 'g.grade as grade, ';
145 $fields .= 'g.timemodified as timemarked, ';
146 $fields .= 'g.timecreated as firstmarked, ';
147 $fields .= 'uf.mailed as mailed, ';
148 $fields .= 'uf.locked as locked, ';
149 $fields .= 'uf.extensionduedate as extensionduedate, ';
150 $fields .= 'uf.workflowstate as workflowstate, ';
151 $fields .= 'uf.allocatedmarker as allocatedmarker ';
154 LEFT JOIN {assign_submission} s
156 AND s.assignment = :assignmentid1
158 LEFT JOIN {assign_grades} g
160 AND g.assignment = :assignmentid2 ';
162 // For group submissions we don't immediately create an entry in the assign_submission table for each user,
163 // instead the userid is set to 0. In this case we use a different query to retrieve the grade for the user.
164 if ($this->assignment->get_instance()->teamsubmission) {
165 $params['assignmentid4'] = (int) $this->assignment->get_instance()->id;
166 $grademaxattempt = 'SELECT mxg.userid, MAX(mxg.attemptnumber) AS maxattempt
167 FROM {assign_grades} mxg
168 WHERE mxg.assignment = :assignmentid4
169 GROUP BY mxg.userid';
170 $from .= 'LEFT JOIN (' . $grademaxattempt . ') gmx
172 AND g.attemptnumber = gmx.maxattempt ';
174 $from .= 'AND g.attemptnumber = s.attemptnumber ';
177 $from .= 'LEFT JOIN {assign_user_flags} uf
179 AND uf.assignment = :assignmentid3';
181 $userparams = array();
184 list($userwhere, $userparams) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
185 $where = 'u.id ' . $userwhere;
186 $params = array_merge($params, $userparams);
188 // The filters do not make sense when there are no submissions, so do not apply them.
189 if ($this->assignment->is_any_submission_plugin_enabled()) {
190 if ($filter == ASSIGN_FILTER_SUBMITTED) {
191 $where .= ' AND (s.timemodified IS NOT NULL AND
192 s.status = :submitted) ';
193 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
195 } else if ($filter == ASSIGN_FILTER_NOT_SUBMITTED) {
196 $where .= ' AND (s.timemodified IS NULL OR s.status != :submitted) ';
197 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
198 } else if ($filter == ASSIGN_FILTER_REQUIRE_GRADING) {
199 $where .= ' AND (s.timemodified IS NOT NULL AND
200 s.status = :submitted AND
201 (s.timemodified >= g.timemodified OR g.timemodified IS NULL OR g.grade IS NULL))';
202 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
204 } else if (strpos($filter, ASSIGN_FILTER_SINGLE_USER) === 0) {
205 $userfilter = (int) array_pop(explode('=', $filter));
206 $where .= ' AND (u.id = :userid)';
207 $params['userid'] = $userfilter;
211 if ($this->assignment->get_instance()->markingworkflow &&
212 $this->assignment->get_instance()->markingallocation) {
213 if (has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
214 // Check to see if marker filter is set.
215 $markerfilter = (int)get_user_preferences('assign_markerfilter', '');
216 if (!empty($markerfilter)) {
217 if ($markerfilter == ASSIGN_MARKER_FILTER_NO_MARKER) {
218 $where .= ' AND (uf.allocatedmarker IS NULL OR uf.allocatedmarker = 0)';
220 $where .= ' AND uf.allocatedmarker = :markerid';
221 $params['markerid'] = $markerfilter;
224 } else { // Only show users allocated to this marker.
225 $where .= ' AND uf.allocatedmarker = :markerid';
226 $params['markerid'] = $USER->id;
230 if ($this->assignment->get_instance()->markingworkflow) {
231 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
232 if (!empty($workflowstates)) {
233 $workflowfilter = get_user_preferences('assign_workflowfilter', '');
234 if ($workflowfilter == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED) {
235 $where .= ' AND (uf.workflowstate = :workflowstate OR uf.workflowstate IS NULL OR '.
236 $DB->sql_isempty('assign_user_flags', 'workflowstate', true, true).')';
237 $params['workflowstate'] = $workflowfilter;
238 } else if (array_key_exists($workflowfilter, $workflowstates)) {
239 $where .= ' AND uf.workflowstate = :workflowstate';
240 $params['workflowstate'] = $workflowfilter;
245 $this->set_sql($fields, $from, $where, $params);
247 if ($downloadfilename) {
248 $this->is_downloading('csv', $downloadfilename);
255 if (!$this->is_downloading() && $this->hasgrade) {
256 $columns[] = 'select';
257 $headers[] = get_string('select') .
258 '<div class="selectall"><label class="accesshide" for="selectall">' . get_string('selectall') . '</label>
259 <input type="checkbox" id="selectall" name="selectall" title="' . get_string('selectall') . '"/></div>';
263 if ($this->hasviewblind || !$this->assignment->is_blind_marking()) {
264 if (!$this->is_downloading()) {
265 $columns[] = 'picture';
266 $headers[] = get_string('pictureofuser');
268 $columns[] = 'recordid';
269 $headers[] = get_string('recordid', 'assign');
273 $columns[] = 'fullname';
274 $headers[] = get_string('fullname');
276 foreach ($extrauserfields as $extrafield) {
277 $columns[] = $extrafield;
278 $headers[] = get_user_field_name($extrafield);
282 $columns[] = 'recordid';
283 $headers[] = get_string('recordid', 'assign');
286 // Submission status.
287 $columns[] = 'status';
288 $headers[] = get_string('status', 'assign');
290 // Team submission columns.
291 if ($assignment->get_instance()->teamsubmission) {
293 $headers[] = get_string('submissionteam', 'assign');
296 if ($this->assignment->get_instance()->markingworkflow &&
297 $this->assignment->get_instance()->markingallocation &&
298 has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
299 // Add a column for the allocated marker.
300 $columns[] = 'allocatedmarker';
301 $headers[] = get_string('marker', 'assign');
304 $columns[] = 'grade';
305 $headers[] = get_string('grade');
306 if ($this->is_downloading()) {
307 if ($this->assignment->get_instance()->grade >= 0) {
308 $columns[] = 'grademax';
309 $headers[] = get_string('maxgrade', 'assign');
311 // This is a custom scale.
312 $columns[] = 'scale';
313 $headers[] = get_string('scale', 'assign');
316 if ($this->assignment->get_instance()->markingworkflow) {
317 // Add a column for the marking workflow state.
318 $columns[] = 'workflowstate';
319 $headers[] = get_string('markingworkflowstate', 'assign');
321 // Add a column for the list of valid marking workflow states.
322 $columns[] = 'gradecanbechanged';
323 $headers[] = get_string('gradecanbechanged', 'assign');
325 if (!$this->is_downloading() && $this->hasgrade) {
326 // We have to call this column userid so we can use userid as a default sortable column.
327 $columns[] = 'userid';
328 $headers[] = get_string('edit');
331 // Submission plugins.
332 if ($assignment->is_any_submission_plugin_enabled()) {
333 $columns[] = 'timesubmitted';
334 $headers[] = get_string('lastmodifiedsubmission', 'assign');
336 foreach ($this->assignment->get_submission_plugins() as $plugin) {
337 if ($this->is_downloading()) {
338 if ($plugin->is_visible() && $plugin->is_enabled()) {
339 foreach ($plugin->get_editor_fields() as $field => $description) {
340 $index = 'plugin' . count($this->plugincache);
341 $this->plugincache[$index] = array($plugin, $field);
343 $headers[] = $plugin->get_name();
347 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
348 $index = 'plugin' . count($this->plugincache);
349 $this->plugincache[$index] = array($plugin);
351 $headers[] = $plugin->get_name();
358 $columns[] = 'timemarked';
359 $headers[] = get_string('lastmodifiedgrade', 'assign');
362 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
363 if ($this->is_downloading()) {
364 if ($plugin->is_visible() && $plugin->is_enabled()) {
365 foreach ($plugin->get_editor_fields() as $field => $description) {
366 $index = 'plugin' . count($this->plugincache);
367 $this->plugincache[$index] = array($plugin, $field);
369 $headers[] = $description;
372 } else if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
373 $index = 'plugin' . count($this->plugincache);
374 $this->plugincache[$index] = array($plugin);
376 $headers[] = $plugin->get_name();
380 // Exclude 'Final grade' column in downloaded grading worksheets.
381 if (!$this->is_downloading()) {
383 $columns[] = 'finalgrade';
384 $headers[] = get_string('finalgrade', 'grades');
387 // Load the grading info for all users.
388 $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id,
391 $this->assignment->get_instance()->id,
394 if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
395 $columns[] = 'outcomes';
396 $headers[] = get_string('outcomes', 'grades');
400 $this->define_columns($columns);
401 $this->define_headers($headers);
402 foreach ($extrauserfields as $extrafield) {
403 $this->column_class($extrafield, $extrafield);
405 $this->no_sorting('recordid');
406 $this->no_sorting('finalgrade');
407 $this->no_sorting('userid');
408 $this->no_sorting('select');
409 $this->no_sorting('outcomes');
411 if ($assignment->get_instance()->teamsubmission) {
412 $this->no_sorting('team');
415 $plugincolumnindex = 0;
416 foreach ($this->assignment->get_submission_plugins() as $plugin) {
417 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
418 $submissionpluginindex = 'plugin' . $plugincolumnindex++;
419 $this->no_sorting($submissionpluginindex);
422 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
423 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
424 $feedbackpluginindex = 'plugin' . $plugincolumnindex++;
425 $this->no_sorting($feedbackpluginindex);
429 // When there is no data we still want the column headers printed in the csv file.
430 if ($this->is_downloading()) {
431 $this->start_output();
436 * Before adding each row to the table make sure rownum is incremented.
438 * @param array $row row of data from db used to make one row of the table.
439 * @return array one row for the table
441 public function format_row($row) {
442 if ($this->rownum < 0) {
443 $this->rownum = $this->currpage * $this->pagesize;
448 return parent::format_row($row);
452 * Add a column with an ID that uniquely identifies this user in this assignment.
454 * @param stdClass $row
457 public function col_recordid(stdClass $row) {
458 return get_string('hiddenuser', 'assign') .
459 $this->assignment->get_uniqueid_for_user($row->userid);
464 * Add the userid to the row class so it can be updated via ajax.
466 * @param stdClass $row The row of data
467 * @return string The row class
469 public function get_row_class($row) {
470 return 'user' . $row->userid;
474 * Return the number of rows to display on a single page.
476 * @return int The number of rows per page
478 public function get_rows_per_page() {
479 return $this->perpage;
483 * list current marking workflow state
485 * @param stdClass $row
488 public function col_workflowstatus(stdClass $row) {
491 $gradingdisabled = $this->assignment->grading_disabled($row->id);
492 // The function in the assignment keeps a static cache of this list of states.
493 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
494 $workflowstate = $row->workflowstate;
495 if (empty($workflowstate)) {
496 $workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
498 if ($this->quickgrading && !$gradingdisabled) {
499 $notmarked = get_string('markingworkflowstatenotmarked', 'assign');
500 $name = 'quickgrade_' . $row->id . '_workflowstate';
501 $o .= html_writer::select($workflowstates, $name, $workflowstate, array('' => $notmarked));
502 // Check if this user is a marker that can't manage allocations and doesn't have the marker column added.
503 if ($this->assignment->get_instance()->markingworkflow &&
504 $this->assignment->get_instance()->markingallocation &&
505 !has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
507 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
508 $o .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $name,
509 'value' => $row->allocatedmarker));
512 $o .= $this->output->container(get_string('markingworkflowstate' . $workflowstate, 'assign'), $workflowstate);
518 * For download only - list current marking workflow state
520 * @param stdClass $row - The row of data
521 * @return string The current marking workflow state
523 public function col_workflowstate($row) {
524 $state = $row->workflowstate;
526 $state = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
529 return get_string('markingworkflowstate' . $state, 'assign');
533 * list current marker
535 * @param stdClass $row - The row of data
536 * @return id the user->id of the marker.
538 public function col_allocatedmarker(stdClass $row) {
539 static $markers = null;
540 static $markerlist = array();
541 if ($markers === null) {
542 list($sort, $params) = users_order_by_sql();
543 $markers = get_users_by_capability($this->assignment->get_context(), 'mod/assign:grade', '', $sort);
544 $markerlist[0] = get_string('choosemarker', 'assign');
545 foreach ($markers as $marker) {
546 $markerlist[$marker->id] = fullname($marker);
549 if (empty($markerlist)) {
550 // TODO: add some form of notification here that no markers are available.
553 if ($this->is_downloading()) {
554 if (isset($markers[$row->allocatedmarker])) {
555 return fullname($markers[$row->allocatedmarker]);
561 if ($this->quickgrading && has_capability('mod/assign:manageallocations', $this->assignment->get_context()) &&
562 (empty($row->workflowstate) ||
563 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_INMARKING ||
564 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED)) {
566 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
567 return html_writer::select($markerlist, $name, $row->allocatedmarker, false);
568 } else if (!empty($row->allocatedmarker)) {
570 if ($this->quickgrading) { // Add hidden field for quickgrading page.
571 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
572 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$row->allocatedmarker));
574 $output .= $markerlist[$row->allocatedmarker];
579 * For download only - list all the valid options for this custom scale.
581 * @param stdClass $row - The row of data
582 * @return string A list of valid options for the current scale
584 public function col_scale($row) {
587 if (empty($this->scale)) {
588 $dbparams = array('id'=>-($this->assignment->get_instance()->grade));
589 $this->scale = $DB->get_record('scale', $dbparams);
592 if (!empty($this->scale->scale)) {
593 return implode("\n", explode(',', $this->scale->scale));
599 * Display a grade with scales etc.
601 * @param string $grade
602 * @param boolean $editable
603 * @param int $userid The user id of the user this grade belongs to
604 * @param int $modified Timestamp showing when the grade was last modified
605 * @return string The formatted grade
607 public function display_grade($grade, $editable, $userid, $modified) {
608 if ($this->is_downloading()) {
609 if ($this->assignment->get_instance()->grade >= 0) {
610 if ($grade == -1 || $grade === null) {
613 return format_float($grade, 2);
615 // This is a custom scale.
616 $scale = $this->assignment->display_grade($grade, false);
623 return $this->assignment->display_grade($grade, $editable, $userid, $modified);
627 * Get the team info for this user.
629 * @param stdClass $row
630 * @return string The team name
632 public function col_team(stdClass $row) {
635 $this->get_group_and_submission($row->id, $group, $submission, -1);
638 } else if ($this->assignment->get_instance()->preventsubmissionnotingroup) {
639 $usergroups = $this->assignment->get_all_groups($row->id);
640 if (count($usergroups) > 1) {
641 return get_string('multipleteamsgrader', 'assign');
643 return get_string('noteamgrader', 'assign');
646 return get_string('defaultteam', 'assign');
650 * Use a static cache to try and reduce DB calls.
652 * @param int $userid The user id for this submission
653 * @param int $group The groupid (returned)
654 * @param stdClass|false $submission The stdClass submission or false (returned)
655 * @param int $attemptnumber Return a specific attempt number (-1 for latest)
657 protected function get_group_and_submission($userid, &$group, &$submission, $attemptnumber) {
659 if (isset($this->submissiongroups[$userid])) {
660 $group = $this->submissiongroups[$userid];
662 $group = $this->assignment->get_submission_group($userid, false);
663 $this->submissiongroups[$userid] = $group;
668 $groupid = $group->id;
671 // Static cache is keyed by groupid and attemptnumber.
672 // We may need both the latest and previous attempt in the same page.
673 if (isset($this->groupsubmissions[$groupid . ':' . $attemptnumber])) {
674 $submission = $this->groupsubmissions[$groupid . ':' . $attemptnumber];
676 $submission = $this->assignment->get_group_submission($userid, $groupid, false, $attemptnumber);
677 $this->groupsubmissions[$groupid . ':' . $attemptnumber] = $submission;
682 * Format a list of outcomes.
684 * @param stdClass $row
687 public function col_outcomes(stdClass $row) {
689 foreach ($this->gradinginfo->outcomes as $index => $outcome) {
690 $options = make_grades_menu(-$outcome->scaleid);
692 $options[0] = get_string('nooutcome', 'grades');
693 if ($this->quickgrading && !($outcome->grades[$row->userid]->locked)) {
694 $select = '<select name="outcome_' . $index . '_' . $row->userid . '" class="quickgrade">';
695 foreach ($options as $optionindex => $optionvalue) {
697 if ($outcome->grades[$row->userid]->grade == $optionindex) {
698 $selected = 'selected="selected"';
700 $select .= '<option value="' . $optionindex . '"' . $selected . '>' . $optionvalue . '</option>';
702 $select .= '</select>';
703 $outcomes .= $this->output->container($outcome->name . ': ' . $select, 'outcome');
705 $name = $outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade];
706 if ($this->is_downloading()) {
709 $outcomes .= $this->output->container($name, 'outcome');
719 * Format a user picture for display.
721 * @param stdClass $row
724 public function col_picture(stdClass $row) {
725 return $this->output->user_picture($row);
729 * Format a user record for display (link to profile).
731 * @param stdClass $row
734 public function col_fullname($row) {
735 if (!$this->is_downloading()) {
736 $courseid = $this->assignment->get_course()->id;
737 $link= new moodle_url('/user/view.php', array('id' =>$row->id, 'course'=>$courseid));
738 $fullname = $this->output->action_link($link, $this->assignment->fullname($row));
740 $fullname = $this->assignment->fullname($row);
743 if (!$this->assignment->is_active_user($row->id)) {
744 $suspendedstring = get_string('userenrolmentsuspended', 'grades');
745 $fullname .= ' ' . html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/enrolmentsuspended'),
746 'title' => $suspendedstring, 'alt' => $suspendedstring, 'class' => 'usersuspendedicon'));
747 $fullname = html_writer::tag('span', $fullname, array('class' => 'usersuspended'));
753 * Insert a checkbox for selecting the current row for batch operations.
755 * @param stdClass $row
758 public function col_select(stdClass $row) {
759 $selectcol = '<label class="accesshide" for="selectuser_' . $row->userid . '">';
760 $selectcol .= get_string('selectuser', 'assign', $this->assignment->fullname($row));
761 $selectcol .= '</label>';
762 $selectcol .= '<input type="checkbox"
763 id="selectuser_' . $row->userid . '"
765 value="' . $row->userid . '"/>';
766 $selectcol .= '<input type="hidden"
767 name="grademodified_' . $row->userid . '"
768 value="' . $row->timemarked . '"/>';
773 * Return a users grades from the listing of all grade data for this assignment.
776 * @return mixed stdClass or false
778 private function get_gradebook_data_for_user($userid) {
779 if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
780 return $this->gradinginfo->items[0]->grades[$userid];
786 * Format a column of data for display.
788 * @param stdClass $row
791 public function col_gradecanbechanged(stdClass $row) {
792 $gradingdisabled = $this->assignment->grading_disabled($row->id);
793 if ($gradingdisabled) {
794 return get_string('no');
796 return get_string('yes');
801 * Format a column of data for display
803 * @param stdClass $row
806 public function col_grademax(stdClass $row) {
807 return format_float($this->assignment->get_instance()->grade, 2);
811 * Format a column of data for display.
813 * @param stdClass $row
816 public function col_grade(stdClass $row) {
820 $separator = $this->output->spacer(array(), true);
822 $gradingdisabled = $this->assignment->grading_disabled($row->id);
824 if (!$this->is_downloading() && $this->hasgrade) {
825 $name = $this->assignment->fullname($row);
826 $icon = $this->output->pix_icon('gradefeedback',
827 get_string('gradeuser', 'assign', $name),
829 $urlparams = array('id' => $this->assignment->get_course_module()->id,
830 'rownum'=>$this->rownum,
832 'useridlistid' => $this->assignment->get_useridlist_key_id());
833 $url = new moodle_url('/mod/assign/view.php', $urlparams);
834 $link = $this->output->action_link($url, $icon);
835 $grade .= $link . $separator;
838 $grade .= $this->display_grade($row->grade,
839 $this->quickgrading && !$gradingdisabled,
847 * Format a column of data for display.
849 * @param stdClass $row
852 public function col_finalgrade(stdClass $row) {
855 $grade = $this->get_gradebook_data_for_user($row->userid);
857 $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
864 * Format a column of data for display.
866 * @param stdClass $row
869 public function col_timemarked(stdClass $row) {
872 if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
873 $o = userdate($row->timemarked);
875 if ($row->timemarked && $this->is_downloading()) {
876 // Force it for downloads as it affects import.
877 $o = userdate($row->timemarked);
884 * Format a column of data for display.
886 * @param stdClass $row
889 public function col_timesubmitted(stdClass $row) {
894 $this->get_group_and_submission($row->id, $group, $submission, -1);
895 if ($submission && $submission->timemodified && $submission->status != ASSIGN_SUBMISSION_STATUS_NEW) {
896 $o = userdate($submission->timemodified);
897 } else if ($row->timesubmitted) {
898 $o = userdate($row->timesubmitted);
905 * Format a column of data for display
907 * @param stdClass $row
910 public function col_status(stdClass $row) {
913 $instance = $this->assignment->get_instance();
915 $due = $instance->duedate;
916 if ($row->extensionduedate) {
917 $due = $row->extensionduedate;
922 $this->get_group_and_submission($row->id, $group, $submission, -1);
924 if ($instance->teamsubmission && !$group && !$instance->preventsubmissionnotingroup) {
928 if ($group && $submission) {
929 $timesubmitted = $submission->timemodified;
930 $status = $submission->status;
932 $timesubmitted = $row->timesubmitted;
933 $status = $row->status;
936 if ($this->assignment->is_any_submission_plugin_enabled()) {
938 $o .= $this->output->container(get_string('submissionstatus_' . $status, 'assign'),
939 array('class'=>'submissionstatus' .$status));
940 if ($due && $timesubmitted > $due) {
941 $usertime = format_time($timesubmitted - $due);
942 $latemessage = get_string('submittedlateshort',
945 $o .= $this->output->container($latemessage, 'latesubmission');
948 $lockedstr = get_string('submissionslockedshort', 'assign');
949 $o .= $this->output->container($lockedstr, 'lockedsubmission');
952 // Add status of "grading" if markflow is not enabled.
953 if (!$instance->markingworkflow) {
954 if ($row->grade !== null && $row->grade >= 0) {
955 $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
956 } else if (!$timesubmitted) {
958 if ($due && ($now > $due)) {
959 $overduestr = get_string('overdue', 'assign', format_time($now - $due));
960 $o .= $this->output->container($overduestr, 'overduesubmission');
966 if ($instance->markingworkflow) {
967 $o .= $this->col_workflowstatus($row);
969 if ($row->extensionduedate) {
970 $userdate = userdate($row->extensionduedate);
971 $extensionstr = get_string('userextensiondate', 'assign', $userdate);
972 $o .= $this->output->container($extensionstr, 'extensiondate');
975 if ($this->is_downloading()) {
976 $o = strip_tags(rtrim(str_replace('</div>', ' - ', $o), '- '));
983 * Format a column of data for display.
985 * @param stdClass $row
988 public function col_userid(stdClass $row) {
995 $urlparams = array('id'=>$this->assignment->get_course_module()->id,
996 'rownum'=>$this->rownum,
998 'useridlistid' => $this->assignment->get_useridlist_key_id());
999 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1003 $description = get_string('grade');
1005 $description = get_string('updategrade', 'assign');
1007 $actions['grade'] = new action_menu_link_secondary(
1013 // Everything we need is in the row.
1016 if ($this->assignment->get_instance()->teamsubmission) {
1017 // Use the cache for this.
1018 $submission = false;
1020 $this->get_group_and_submission($row->id, $group, $submission, -1);
1023 $submissionsopen = $this->assignment->submissions_open($row->id,
1027 $this->gradinginfo);
1028 $caneditsubmission = $this->assignment->can_edit_submission($row->id, $USER->id);
1030 // Hide for offline assignments.
1031 if ($this->assignment->is_any_submission_plugin_enabled()) {
1032 if (!$row->status ||
1033 $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
1034 !$this->assignment->get_instance()->submissiondrafts) {
1036 if (!$row->locked) {
1037 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1040 'sesskey'=>sesskey(),
1041 'page'=>$this->currpage);
1042 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1044 $description = get_string('preventsubmissionsshort', 'assign');
1045 $actions['lock'] = new action_menu_link_secondary(
1051 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1054 'sesskey'=>sesskey(),
1055 'page'=>$this->currpage);
1056 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1057 $description = get_string('allowsubmissionsshort', 'assign');
1058 $actions['unlock'] = new action_menu_link_secondary(
1066 if ($submissionsopen &&
1067 $USER->id != $row->id &&
1068 $caneditsubmission) {
1069 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1071 'action'=>'editsubmission',
1072 'sesskey'=>sesskey(),
1073 'page'=>$this->currpage);
1074 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1075 $description = get_string('editsubmission', 'assign');
1076 $actions['editsubmission'] = new action_menu_link_secondary(
1083 if (($this->assignment->get_instance()->duedate ||
1084 $this->assignment->get_instance()->cutoffdate) &&
1085 $this->hasgrantextension) {
1086 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1087 'userid' => $row->id,
1088 'action' => 'grantextension',
1089 'sesskey' => sesskey(),
1090 'page' => $this->currpage);
1091 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1092 $description = get_string('grantextension', 'assign');
1093 $actions['grantextension'] = new action_menu_link_secondary(
1099 if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
1100 $this->assignment->get_instance()->submissiondrafts) {
1101 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1103 'action'=>'reverttodraft',
1104 'sesskey'=>sesskey(),
1105 'page'=>$this->currpage);
1106 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1107 $description = get_string('reverttodraftshort', 'assign');
1108 $actions['reverttodraft'] = new action_menu_link_secondary(
1114 if ($row->status == ASSIGN_SUBMISSION_STATUS_DRAFT &&
1115 $this->assignment->get_instance()->submissiondrafts &&
1116 $caneditsubmission &&
1118 $row->id != $USER->id) {
1119 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1121 'action'=>'submitotherforgrading',
1122 'sesskey'=>sesskey(),
1123 'page'=>$this->currpage);
1124 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1125 $description = get_string('submitforgrading', 'assign');
1126 $actions['submitforgrading'] = new action_menu_link_secondary(
1133 $ismanual = $this->assignment->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
1134 $hassubmission = !empty($row->status);
1135 $notreopened = $hassubmission && $row->status != ASSIGN_SUBMISSION_STATUS_REOPENED;
1136 $isunlimited = $this->assignment->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS;
1137 $hasattempts = $isunlimited || $row->attemptnumber < $this->assignment->get_instance()->maxattempts - 1;
1139 if ($ismanual && $hassubmission && $notreopened && $hasattempts) {
1140 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1142 'action'=>'addattempt',
1143 'sesskey'=>sesskey(),
1144 'page'=>$this->currpage);
1145 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1146 $description = get_string('addattempt', 'assign');
1147 $actions['addattempt'] = new action_menu_link_secondary(
1154 $menu = new action_menu();
1155 $menu->set_owner_selector('.gradingtable-actionmenu');
1156 $menu->set_alignment(action_menu::TL, action_menu::BL);
1157 $menu->set_constraint('.gradingtable > .no-overflow');
1158 $menu->set_menu_trigger(get_string('edit'));
1159 foreach ($actions as $action) {
1160 $menu->add($action);
1163 // Prioritise the menu ahead of all other actions.
1164 $menu->prioritise = true;
1166 $edit .= $this->output->render($menu);
1172 * Write the plugin summary with an optional link to view the full feedback/submission.
1174 * @param assign_plugin $plugin Submission plugin or feedback plugin
1175 * @param stdClass $item Submission or grade
1176 * @param string $returnaction The return action to pass to the
1177 * view_submission page (the current page)
1178 * @param string $returnparams The return params to pass to the view_submission
1179 * page (the current page)
1180 * @return string The summary with an optional link
1182 private function format_plugin_summary_with_link(assign_plugin $plugin,
1187 $showviewlink = false;
1189 $summary = $plugin->view_summary($item, $showviewlink);
1191 if ($showviewlink) {
1192 $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
1193 $icon = $this->output->pix_icon('t/preview', $viewstr);
1194 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1197 'plugin'=>$plugin->get_type(),
1198 'action'=>'viewplugin' . $plugin->get_subtype(),
1199 'returnaction'=>$returnaction,
1200 'returnparams'=>http_build_query($returnparams));
1201 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1202 $link = $this->output->action_link($url, $icon);
1203 $separator = $this->output->spacer(array(), true);
1206 return $link . $separator . $summary;
1211 * Format the submission and feedback columns.
1213 * @param string $colname The column name
1214 * @param stdClass $row The submission row
1215 * @return mixed string or NULL
1217 public function other_cols($colname, $row) {
1218 // For extra user fields the result is already in $row.
1219 if (empty($this->plugincache[$colname])) {
1220 return $row->$colname;
1223 // This must be a plugin field.
1224 $plugincache = $this->plugincache[$colname];
1226 $plugin = $plugincache[0];
1229 if (isset($plugincache[1])) {
1230 $field = $plugincache[1];
1233 if ($plugin->is_visible() && $plugin->is_enabled()) {
1234 if ($plugin->get_subtype() == 'assignsubmission') {
1235 if ($this->assignment->get_instance()->teamsubmission) {
1237 $submission = false;
1239 $this->get_group_and_submission($row->id, $group, $submission, -1);
1241 if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1242 // For a newly reopened submission - we want to show the previous submission in the table.
1243 $this->get_group_and_submission($row->id, $group, $submission, $submission->attemptnumber-1);
1245 if (isset($field)) {
1246 return $plugin->get_editor_text($field, $submission->id);
1248 return $this->format_plugin_summary_with_link($plugin,
1253 } else if ($row->submissionid) {
1254 if ($row->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1255 // For a newly reopened submission - we want to show the previous submission in the table.
1256 $submission = $this->assignment->get_user_submission($row->userid, false, $row->attemptnumber - 1);
1258 $submission = new stdClass();
1259 $submission->id = $row->submissionid;
1260 $submission->timecreated = $row->firstsubmission;
1261 $submission->timemodified = $row->timesubmitted;
1262 $submission->assignment = $this->assignment->get_instance()->id;
1263 $submission->userid = $row->userid;
1264 $submission->attemptnumber = $row->attemptnumber;
1266 // Field is used for only for import/export and refers the the fieldname for the text editor.
1267 if (isset($field)) {
1268 return $plugin->get_editor_text($field, $submission->id);
1270 return $this->format_plugin_summary_with_link($plugin,
1277 if (isset($field)) {
1278 return $plugin->get_editor_text($field, $row->gradeid);
1281 if ($row->gradeid) {
1282 $grade = new stdClass();
1283 $grade->id = $row->gradeid;
1284 $grade->timecreated = $row->firstmarked;
1285 $grade->timemodified = $row->timemarked;
1286 $grade->assignment = $this->assignment->get_instance()->id;
1287 $grade->userid = $row->userid;
1288 $grade->grade = $row->grade;
1289 $grade->mailed = $row->mailed;
1290 $grade->attemptnumber = $row->attemptnumber;
1292 if ($this->quickgrading && $plugin->supports_quickgrading()) {
1293 return $plugin->get_quickgrading_html($row->userid, $grade);
1294 } else if ($grade) {
1295 return $this->format_plugin_summary_with_link($plugin,
1306 * Using the current filtering and sorting - load all rows and return a single column from them.
1308 * @param string $columnname The name of the raw column data
1309 * @return array of data
1311 public function get_column_data($columnname) {
1313 $this->currpage = 0;
1314 $this->query_db($this->tablemaxrows);
1316 foreach ($this->rawdata as $row) {
1317 $result[] = $row->$columnname;
1323 * Return things to the renderer.
1325 * @return string the assignment name
1327 public function get_assignment_name() {
1328 return $this->assignment->get_instance()->name;
1332 * Return things to the renderer.
1334 * @return int the course module id
1336 public function get_course_module_id() {
1337 return $this->assignment->get_course_module()->id;
1341 * Return things to the renderer.
1343 * @return int the course id
1345 public function get_course_id() {
1346 return $this->assignment->get_course()->id;
1350 * Return things to the renderer.
1352 * @return stdClass The course context
1354 public function get_course_context() {
1355 return $this->assignment->get_course_context();
1359 * Return things to the renderer.
1361 * @return bool Does this assignment accept submissions
1363 public function submissions_enabled() {
1364 return $this->assignment->is_any_submission_plugin_enabled();
1368 * Return things to the renderer.
1370 * @return bool Can this user view all grades (the gradebook)
1372 public function can_view_all_grades() {
1373 $context = $this->assignment->get_course_context();
1374 return has_capability('gradereport/grader:view', $context) &&
1375 has_capability('moodle/grade:viewall', $context);
1379 * Always return a valid sort - even if the userid column is missing.
1380 * @return array column name => SORT_... constant.
1382 public function get_sort_columns() {
1383 $result = parent::get_sort_columns();
1384 $result = array_merge($result, array('userid' => SORT_ASC));
1389 * Override the table show_hide_link to not show for select column.
1391 * @param string $column the column name, index into various names.
1392 * @param int $index numerical index of the column.
1393 * @return string HTML fragment.
1395 protected function show_hide_link($column, $index) {
1396 if ($index > 0 || !$this->hasgrade) {
1397 return parent::show_hide_link($column, $index);
1403 * Overides setup to ensure it will only run a single time.
1405 public function setup() {
1406 // Check if the setup function has been called before, we should not run it twice.
1407 // If we do the sortorder of the table will be broken.
1408 if (!empty($this->setup)) {