MDL-63185 mod_quiz: replace existing tests to use new step
[moodle.git] / mod / assign / gradingtable.php
blobdc847b75641153c67bccd3030aabdabe1b892363
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.
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 the definition for the grading table which subclassses easy_table
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
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');
31 /**
32 * Extends table_sql to provide a table of assignment submissions
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
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) */
44 private $rownum = -1;
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;
68 /**
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,
79 $perpage,
80 $filter,
81 $rowoffset,
82 $quickgrading,
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);
119 if ($rowoffset) {
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.
126 $users[] = -1;
129 $params = array();
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;
133 $params['newstatus'] = ASSIGN_SUBMISSION_STATUS_NEW;
135 $extrauserfields = get_extra_user_fields($this->assignment->get_context());
137 $fields = user_picture::fields('u', $extrauserfields) . ', ';
138 $fields .= 'u.id as userid, ';
139 $fields .= 's.status as status, ';
140 $fields .= 's.id as submissionid, ';
141 $fields .= 's.timecreated as firstsubmission, ';
142 $fields .= "CASE WHEN status <> :newstatus THEN s.timemodified ELSE NULL END as timesubmitted, ";
143 $fields .= 's.attemptnumber as attemptnumber, ';
144 $fields .= 'g.id as gradeid, ';
145 $fields .= 'g.grade as grade, ';
146 $fields .= 'g.timemodified as timemarked, ';
147 $fields .= 'g.timecreated as firstmarked, ';
148 $fields .= 'uf.mailed as mailed, ';
149 $fields .= 'uf.locked as locked, ';
150 $fields .= 'uf.extensionduedate as extensionduedate, ';
151 $fields .= 'uf.workflowstate as workflowstate, ';
152 $fields .= 'uf.allocatedmarker as allocatedmarker';
154 $from = '{user} u
155 LEFT JOIN {assign_submission} s
156 ON u.id = s.userid
157 AND s.assignment = :assignmentid1
158 AND s.latest = 1 ';
160 // For group assignments, there can be a grade with no submission.
161 $from .= ' LEFT JOIN {assign_grades} g
162 ON g.assignment = :assignmentid2
163 AND u.id = g.userid
164 AND (g.attemptnumber = s.attemptnumber OR s.attemptnumber IS NULL) ';
166 $from .= 'LEFT JOIN {assign_user_flags} uf
167 ON u.id = uf.userid
168 AND uf.assignment = :assignmentid3 ';
170 $hasoverrides = $this->assignment->has_overrides();
172 if ($hasoverrides) {
173 $params['assignmentid5'] = (int)$this->assignment->get_instance()->id;
174 $params['assignmentid6'] = (int)$this->assignment->get_instance()->id;
175 $params['assignmentid7'] = (int)$this->assignment->get_instance()->id;
176 $params['assignmentid8'] = (int)$this->assignment->get_instance()->id;
177 $params['assignmentid9'] = (int)$this->assignment->get_instance()->id;
179 $fields .= ', priority.priority, ';
180 $fields .= 'effective.allowsubmissionsfromdate, ';
181 $fields .= 'effective.duedate, ';
182 $fields .= 'effective.cutoffdate ';
184 $from .= ' LEFT JOIN (
185 SELECT merged.userid, min(merged.priority) priority FROM (
186 ( SELECT u.id as userid, 9999999 AS priority
187 FROM {user} u
189 UNION
190 ( SELECT uo.userid, 0 AS priority
191 FROM {assign_overrides} uo
192 WHERE uo.assignid = :assignmentid5
194 UNION
195 ( SELECT gm.userid, go.sortorder AS priority
196 FROM {assign_overrides} go
197 JOIN {groups} g ON g.id = go.groupid
198 JOIN {groups_members} gm ON gm.groupid = g.id
199 WHERE go.assignid = :assignmentid6
201 ) merged
202 GROUP BY merged.userid
203 ) priority ON priority.userid = u.id
205 JOIN (
206 (SELECT 9999999 AS priority,
207 u.id AS userid,
208 a.allowsubmissionsfromdate,
209 a.duedate,
210 a.cutoffdate
211 FROM {user} u
212 JOIN {assign} a ON a.id = :assignmentid7
214 UNION
215 (SELECT 0 AS priority,
216 uo.userid,
217 uo.allowsubmissionsfromdate,
218 uo.duedate,
219 uo.cutoffdate
220 FROM {assign_overrides} uo
221 WHERE uo.assignid = :assignmentid8
223 UNION
224 (SELECT go.sortorder AS priority,
225 gm.userid,
226 go.allowsubmissionsfromdate,
227 go.duedate,
228 go.cutoffdate
229 FROM {assign_overrides} go
230 JOIN {groups} g ON g.id = go.groupid
231 JOIN {groups_members} gm ON gm.groupid = g.id
232 WHERE go.assignid = :assignmentid9
235 ) effective ON effective.priority = priority.priority AND effective.userid = priority.userid ';
238 if (!empty($this->assignment->get_instance()->blindmarking)) {
239 $from .= 'LEFT JOIN {assign_user_mapping} um
240 ON u.id = um.userid
241 AND um.assignment = :assignmentidblind ';
242 $params['assignmentidblind'] = (int)$this->assignment->get_instance()->id;
243 $fields .= ', um.id as recordid ';
246 $userparams = array();
247 $userindex = 0;
249 list($userwhere, $userparams) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
250 $where = 'u.id ' . $userwhere;
251 $params = array_merge($params, $userparams);
253 // The filters do not make sense when there are no submissions, so do not apply them.
254 if ($this->assignment->is_any_submission_plugin_enabled()) {
255 if ($filter == ASSIGN_FILTER_SUBMITTED) {
256 $where .= ' AND (s.timemodified IS NOT NULL AND
257 s.status = :submitted) ';
258 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
260 } else if ($filter == ASSIGN_FILTER_NOT_SUBMITTED) {
261 $where .= ' AND (s.timemodified IS NULL OR s.status <> :submitted) ';
262 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
263 } else if ($filter == ASSIGN_FILTER_REQUIRE_GRADING) {
264 $where .= ' AND (s.timemodified IS NOT NULL AND
265 s.status = :submitted AND
266 (s.timemodified >= g.timemodified OR g.timemodified IS NULL OR g.grade IS NULL';
268 // Assignment grade is set to the negative grade scale id when scales are used.
269 if ($this->assignment->get_instance()->grade < 0) {
270 // Scale grades are set to -1 when not graded.
271 $where .= ' OR g.grade = -1';
274 $where .= '))';
275 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
277 } else if ($filter == ASSIGN_FILTER_GRANTED_EXTENSION) {
278 $where .= ' AND uf.extensionduedate > 0 ';
280 } else if (strpos($filter, ASSIGN_FILTER_SINGLE_USER) === 0) {
281 $userfilter = (int) array_pop(explode('=', $filter));
282 $where .= ' AND (u.id = :userid)';
283 $params['userid'] = $userfilter;
287 if ($this->assignment->get_instance()->markingworkflow &&
288 $this->assignment->get_instance()->markingallocation) {
289 if (has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
290 // Check to see if marker filter is set.
291 $markerfilter = (int)get_user_preferences('assign_markerfilter', '');
292 if (!empty($markerfilter)) {
293 if ($markerfilter == ASSIGN_MARKER_FILTER_NO_MARKER) {
294 $where .= ' AND (uf.allocatedmarker IS NULL OR uf.allocatedmarker = 0)';
295 } else {
296 $where .= ' AND uf.allocatedmarker = :markerid';
297 $params['markerid'] = $markerfilter;
303 if ($this->assignment->get_instance()->markingworkflow) {
304 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
305 if (!empty($workflowstates)) {
306 $workflowfilter = get_user_preferences('assign_workflowfilter', '');
307 if ($workflowfilter == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED) {
308 $where .= ' AND (uf.workflowstate = :workflowstate OR uf.workflowstate IS NULL OR '.
309 $DB->sql_isempty('assign_user_flags', 'workflowstate', true, true).')';
310 $params['workflowstate'] = $workflowfilter;
311 } else if (array_key_exists($workflowfilter, $workflowstates)) {
312 $where .= ' AND uf.workflowstate = :workflowstate';
313 $params['workflowstate'] = $workflowfilter;
318 $this->set_sql($fields, $from, $where, $params);
320 if ($downloadfilename) {
321 $this->is_downloading('csv', $downloadfilename);
324 $columns = array();
325 $headers = array();
327 // Select.
328 if (!$this->is_downloading() && $this->hasgrade) {
329 $columns[] = 'select';
330 $headers[] = get_string('select') .
331 '<div class="selectall"><label class="accesshide" for="selectall">' . get_string('selectall') . '</label>
332 <input type="checkbox" id="selectall" name="selectall" title="' . get_string('selectall') . '"/></div>';
335 // User picture.
336 if ($this->hasviewblind || !$this->assignment->is_blind_marking()) {
337 if (!$this->is_downloading()) {
338 $columns[] = 'picture';
339 $headers[] = get_string('pictureofuser');
340 } else {
341 $columns[] = 'recordid';
342 $headers[] = get_string('recordid', 'assign');
345 // Fullname.
346 $columns[] = 'fullname';
347 $headers[] = get_string('fullname');
349 // Participant # details if can view real identities.
350 if ($this->assignment->is_blind_marking()) {
351 if (!$this->is_downloading()) {
352 $columns[] = 'recordid';
353 $headers[] = get_string('recordid', 'assign');
357 foreach ($extrauserfields as $extrafield) {
358 $columns[] = $extrafield;
359 $headers[] = get_user_field_name($extrafield);
361 } else {
362 // Record ID.
363 $columns[] = 'recordid';
364 $headers[] = get_string('recordid', 'assign');
367 // Submission status.
368 $columns[] = 'status';
369 $headers[] = get_string('status', 'assign');
371 if ($hasoverrides) {
372 // Allowsubmissionsfromdate.
373 $columns[] = 'allowsubmissionsfromdate';
374 $headers[] = get_string('allowsubmissionsfromdate', 'assign');
376 // Duedate.
377 $columns[] = 'duedate';
378 $headers[] = get_string('duedate', 'assign');
380 // Cutoffdate.
381 $columns[] = 'cutoffdate';
382 $headers[] = get_string('cutoffdate', 'assign');
385 // Team submission columns.
386 if ($assignment->get_instance()->teamsubmission) {
387 $columns[] = 'team';
388 $headers[] = get_string('submissionteam', 'assign');
390 // Allocated marker.
391 if ($this->assignment->get_instance()->markingworkflow &&
392 $this->assignment->get_instance()->markingallocation &&
393 has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
394 // Add a column for the allocated marker.
395 $columns[] = 'allocatedmarker';
396 $headers[] = get_string('marker', 'assign');
398 // Grade.
399 $columns[] = 'grade';
400 $headers[] = get_string('grade');
401 if ($this->is_downloading()) {
402 if ($this->assignment->get_instance()->grade >= 0) {
403 $columns[] = 'grademax';
404 $headers[] = get_string('maxgrade', 'assign');
405 } else {
406 // This is a custom scale.
407 $columns[] = 'scale';
408 $headers[] = get_string('scale', 'assign');
411 if ($this->assignment->get_instance()->markingworkflow) {
412 // Add a column for the marking workflow state.
413 $columns[] = 'workflowstate';
414 $headers[] = get_string('markingworkflowstate', 'assign');
416 // Add a column for the list of valid marking workflow states.
417 $columns[] = 'gradecanbechanged';
418 $headers[] = get_string('gradecanbechanged', 'assign');
420 if (!$this->is_downloading() && $this->hasgrade) {
421 // We have to call this column userid so we can use userid as a default sortable column.
422 $columns[] = 'userid';
423 $headers[] = get_string('edit');
426 // Submission plugins.
427 if ($assignment->is_any_submission_plugin_enabled()) {
428 $columns[] = 'timesubmitted';
429 $headers[] = get_string('lastmodifiedsubmission', 'assign');
431 foreach ($this->assignment->get_submission_plugins() as $plugin) {
432 if ($this->is_downloading()) {
433 if ($plugin->is_visible() && $plugin->is_enabled()) {
434 foreach ($plugin->get_editor_fields() as $field => $description) {
435 $index = 'plugin' . count($this->plugincache);
436 $this->plugincache[$index] = array($plugin, $field);
437 $columns[] = $index;
438 $headers[] = $plugin->get_name();
441 } else {
442 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
443 $index = 'plugin' . count($this->plugincache);
444 $this->plugincache[$index] = array($plugin);
445 $columns[] = $index;
446 $headers[] = $plugin->get_name();
452 // Time marked.
453 $columns[] = 'timemarked';
454 $headers[] = get_string('lastmodifiedgrade', 'assign');
456 // Feedback plugins.
457 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
458 if ($this->is_downloading()) {
459 if ($plugin->is_visible() && $plugin->is_enabled()) {
460 foreach ($plugin->get_editor_fields() as $field => $description) {
461 $index = 'plugin' . count($this->plugincache);
462 $this->plugincache[$index] = array($plugin, $field);
463 $columns[] = $index;
464 $headers[] = $description;
467 } else if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
468 $index = 'plugin' . count($this->plugincache);
469 $this->plugincache[$index] = array($plugin);
470 $columns[] = $index;
471 $headers[] = $plugin->get_name();
475 // Exclude 'Final grade' column in downloaded grading worksheets.
476 if (!$this->is_downloading()) {
477 // Final grade.
478 $columns[] = 'finalgrade';
479 $headers[] = get_string('finalgrade', 'grades');
482 // Load the grading info for all users.
483 $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id,
484 'mod',
485 'assign',
486 $this->assignment->get_instance()->id,
487 $users);
489 if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
490 $columns[] = 'outcomes';
491 $headers[] = get_string('outcomes', 'grades');
494 // Set the columns.
495 $this->define_columns($columns);
496 $this->define_headers($headers);
497 foreach ($extrauserfields as $extrafield) {
498 $this->column_class($extrafield, $extrafield);
500 $this->no_sorting('recordid');
501 $this->no_sorting('finalgrade');
502 $this->no_sorting('userid');
503 $this->no_sorting('select');
504 $this->no_sorting('outcomes');
506 if ($assignment->get_instance()->teamsubmission) {
507 $this->no_sorting('team');
510 $plugincolumnindex = 0;
511 foreach ($this->assignment->get_submission_plugins() as $plugin) {
512 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
513 $submissionpluginindex = 'plugin' . $plugincolumnindex++;
514 $this->no_sorting($submissionpluginindex);
517 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
518 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
519 $feedbackpluginindex = 'plugin' . $plugincolumnindex++;
520 $this->no_sorting($feedbackpluginindex);
524 // When there is no data we still want the column headers printed in the csv file.
525 if ($this->is_downloading()) {
526 $this->start_output();
531 * Before adding each row to the table make sure rownum is incremented.
533 * @param array $row row of data from db used to make one row of the table.
534 * @return array one row for the table
536 public function format_row($row) {
537 if ($this->rownum < 0) {
538 $this->rownum = $this->currpage * $this->pagesize;
539 } else {
540 $this->rownum += 1;
543 return parent::format_row($row);
547 * Add a column with an ID that uniquely identifies this user in this assignment.
549 * @param stdClass $row
550 * @return string
552 public function col_recordid(stdClass $row) {
553 if (empty($row->recordid)) {
554 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
556 return get_string('hiddenuser', 'assign') . $row->recordid;
561 * Add the userid to the row class so it can be updated via ajax.
563 * @param stdClass $row The row of data
564 * @return string The row class
566 public function get_row_class($row) {
567 return 'user' . $row->userid;
571 * Return the number of rows to display on a single page.
573 * @return int The number of rows per page
575 public function get_rows_per_page() {
576 return $this->perpage;
580 * list current marking workflow state
582 * @param stdClass $row
583 * @return string
585 public function col_workflowstatus(stdClass $row) {
586 $o = '';
588 $gradingdisabled = $this->assignment->grading_disabled($row->id);
589 // The function in the assignment keeps a static cache of this list of states.
590 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
591 $workflowstate = $row->workflowstate;
592 if (empty($workflowstate)) {
593 $workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
595 if ($this->quickgrading && !$gradingdisabled) {
596 $notmarked = get_string('markingworkflowstatenotmarked', 'assign');
597 $name = 'quickgrade_' . $row->id . '_workflowstate';
598 $o .= html_writer::select($workflowstates, $name, $workflowstate, array('' => $notmarked));
599 // Check if this user is a marker that can't manage allocations and doesn't have the marker column added.
600 if ($this->assignment->get_instance()->markingworkflow &&
601 $this->assignment->get_instance()->markingallocation &&
602 !has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
604 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
605 $o .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $name,
606 'value' => $row->allocatedmarker));
608 } else {
609 $o .= $this->output->container(get_string('markingworkflowstate' . $workflowstate, 'assign'), $workflowstate);
611 return $o;
615 * For download only - list current marking workflow state
617 * @param stdClass $row - The row of data
618 * @return string The current marking workflow state
620 public function col_workflowstate($row) {
621 $state = $row->workflowstate;
622 if (empty($state)) {
623 $state = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
626 return get_string('markingworkflowstate' . $state, 'assign');
630 * list current marker
632 * @param stdClass $row - The row of data
633 * @return id the user->id of the marker.
635 public function col_allocatedmarker(stdClass $row) {
636 static $markers = null;
637 static $markerlist = array();
638 if ($markers === null) {
639 list($sort, $params) = users_order_by_sql('u');
640 // Only enrolled users could be assigned as potential markers.
641 $markers = get_enrolled_users($this->assignment->get_context(), 'mod/assign:grade', 0, 'u.*', $sort);
642 $markerlist[0] = get_string('choosemarker', 'assign');
643 $viewfullnames = has_capability('moodle/site:viewfullnames', $this->assignment->get_context());
644 foreach ($markers as $marker) {
645 $markerlist[$marker->id] = fullname($marker, $viewfullnames);
648 if (empty($markerlist)) {
649 // TODO: add some form of notification here that no markers are available.
650 return '';
652 if ($this->is_downloading()) {
653 if (isset($markers[$row->allocatedmarker])) {
654 return fullname($markers[$row->allocatedmarker],
655 has_capability('moodle/site:viewfullnames', $this->assignment->get_context()));
656 } else {
657 return '';
661 if ($this->quickgrading && has_capability('mod/assign:manageallocations', $this->assignment->get_context()) &&
662 (empty($row->workflowstate) ||
663 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_INMARKING ||
664 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED)) {
666 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
667 return html_writer::select($markerlist, $name, $row->allocatedmarker, false);
668 } else if (!empty($row->allocatedmarker)) {
669 $output = '';
670 if ($this->quickgrading) { // Add hidden field for quickgrading page.
671 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
672 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$row->allocatedmarker));
674 $output .= $markerlist[$row->allocatedmarker];
675 return $output;
679 * For download only - list all the valid options for this custom scale.
681 * @param stdClass $row - The row of data
682 * @return string A list of valid options for the current scale
684 public function col_scale($row) {
685 global $DB;
687 if (empty($this->scale)) {
688 $dbparams = array('id'=>-($this->assignment->get_instance()->grade));
689 $this->scale = $DB->get_record('scale', $dbparams);
692 if (!empty($this->scale->scale)) {
693 return implode("\n", explode(',', $this->scale->scale));
695 return '';
699 * Display a grade with scales etc.
701 * @param string $grade
702 * @param boolean $editable
703 * @param int $userid The user id of the user this grade belongs to
704 * @param int $modified Timestamp showing when the grade was last modified
705 * @return string The formatted grade
707 public function display_grade($grade, $editable, $userid, $modified) {
708 if ($this->is_downloading()) {
709 if ($this->assignment->get_instance()->grade >= 0) {
710 if ($grade == -1 || $grade === null) {
711 return '';
713 $gradeitem = $this->assignment->get_grade_item();
714 return format_float($grade, $gradeitem->get_decimals());
715 } else {
716 // This is a custom scale.
717 $scale = $this->assignment->display_grade($grade, false);
718 if ($scale == '-') {
719 $scale = '';
721 return $scale;
724 return $this->assignment->display_grade($grade, $editable, $userid, $modified);
728 * Get the team info for this user.
730 * @param stdClass $row
731 * @return string The team name
733 public function col_team(stdClass $row) {
734 $submission = false;
735 $group = false;
736 $this->get_group_and_submission($row->id, $group, $submission, -1);
737 if ($group) {
738 return $group->name;
739 } else if ($this->assignment->get_instance()->preventsubmissionnotingroup) {
740 $usergroups = $this->assignment->get_all_groups($row->id);
741 if (count($usergroups) > 1) {
742 return get_string('multipleteamsgrader', 'assign');
743 } else {
744 return get_string('noteamgrader', 'assign');
747 return get_string('defaultteam', 'assign');
751 * Use a static cache to try and reduce DB calls.
753 * @param int $userid The user id for this submission
754 * @param int $group The groupid (returned)
755 * @param stdClass|false $submission The stdClass submission or false (returned)
756 * @param int $attemptnumber Return a specific attempt number (-1 for latest)
758 protected function get_group_and_submission($userid, &$group, &$submission, $attemptnumber) {
759 $group = false;
760 if (isset($this->submissiongroups[$userid])) {
761 $group = $this->submissiongroups[$userid];
762 } else {
763 $group = $this->assignment->get_submission_group($userid, false);
764 $this->submissiongroups[$userid] = $group;
767 $groupid = 0;
768 if ($group) {
769 $groupid = $group->id;
772 // Static cache is keyed by groupid and attemptnumber.
773 // We may need both the latest and previous attempt in the same page.
774 if (isset($this->groupsubmissions[$groupid . ':' . $attemptnumber])) {
775 $submission = $this->groupsubmissions[$groupid . ':' . $attemptnumber];
776 } else {
777 $submission = $this->assignment->get_group_submission($userid, $groupid, false, $attemptnumber);
778 $this->groupsubmissions[$groupid . ':' . $attemptnumber] = $submission;
783 * Format a list of outcomes.
785 * @param stdClass $row
786 * @return string
788 public function col_outcomes(stdClass $row) {
789 $outcomes = '';
790 foreach ($this->gradinginfo->outcomes as $index => $outcome) {
791 $options = make_grades_menu(-$outcome->scaleid);
793 $options[0] = get_string('nooutcome', 'grades');
794 if ($this->quickgrading && !($outcome->grades[$row->userid]->locked)) {
795 $select = '<select name="outcome_' . $index . '_' . $row->userid . '" class="quickgrade">';
796 foreach ($options as $optionindex => $optionvalue) {
797 $selected = '';
798 if ($outcome->grades[$row->userid]->grade == $optionindex) {
799 $selected = 'selected="selected"';
801 $select .= '<option value="' . $optionindex . '"' . $selected . '>' . $optionvalue . '</option>';
803 $select .= '</select>';
804 $outcomes .= $this->output->container($outcome->name . ': ' . $select, 'outcome');
805 } else {
806 $name = $outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade];
807 if ($this->is_downloading()) {
808 $outcomes .= $name;
809 } else {
810 $outcomes .= $this->output->container($name, 'outcome');
815 return $outcomes;
820 * Format a user picture for display.
822 * @param stdClass $row
823 * @return string
825 public function col_picture(stdClass $row) {
826 return $this->output->user_picture($row);
830 * Format a user record for display (link to profile).
832 * @param stdClass $row
833 * @return string
835 public function col_fullname($row) {
836 if (!$this->is_downloading()) {
837 $courseid = $this->assignment->get_course()->id;
838 $link= new moodle_url('/user/view.php', array('id' =>$row->id, 'course'=>$courseid));
839 $fullname = $this->output->action_link($link, $this->assignment->fullname($row));
840 } else {
841 $fullname = $this->assignment->fullname($row);
844 if (!$this->assignment->is_active_user($row->id)) {
845 $suspendedstring = get_string('userenrolmentsuspended', 'grades');
846 $fullname .= ' ' . $this->output->pix_icon('i/enrolmentsuspended', $suspendedstring);
847 $fullname = html_writer::tag('span', $fullname, array('class' => 'usersuspended'));
849 return $fullname;
853 * Insert a checkbox for selecting the current row for batch operations.
855 * @param stdClass $row
856 * @return string
858 public function col_select(stdClass $row) {
859 $selectcol = '<label class="accesshide" for="selectuser_' . $row->userid . '">';
860 $selectcol .= get_string('selectuser', 'assign', $this->assignment->fullname($row));
861 $selectcol .= '</label>';
862 $selectcol .= '<input type="checkbox"
863 id="selectuser_' . $row->userid . '"
864 name="selectedusers"
865 value="' . $row->userid . '"/>';
866 $selectcol .= '<input type="hidden"
867 name="grademodified_' . $row->userid . '"
868 value="' . $row->timemarked . '"/>';
869 $selectcol .= '<input type="hidden"
870 name="gradeattempt_' . $row->userid . '"
871 value="' . $row->attemptnumber . '"/>';
872 return $selectcol;
876 * Return a users grades from the listing of all grade data for this assignment.
878 * @param int $userid
879 * @return mixed stdClass or false
881 private function get_gradebook_data_for_user($userid) {
882 if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
883 return $this->gradinginfo->items[0]->grades[$userid];
885 return false;
889 * Format a column of data for display.
891 * @param stdClass $row
892 * @return string
894 public function col_gradecanbechanged(stdClass $row) {
895 $gradingdisabled = $this->assignment->grading_disabled($row->id);
896 if ($gradingdisabled) {
897 return get_string('no');
898 } else {
899 return get_string('yes');
904 * Format a column of data for display
906 * @param stdClass $row
907 * @return string
909 public function col_grademax(stdClass $row) {
910 $gradeitem = $this->assignment->get_grade_item();
911 return format_float($this->assignment->get_instance()->grade, $gradeitem->get_decimals());
915 * Format a column of data for display.
917 * @param stdClass $row
918 * @return string
920 public function col_grade(stdClass $row) {
921 $o = '';
923 $link = '';
924 $separator = $this->output->spacer(array(), true);
925 $grade = '';
926 $gradingdisabled = $this->assignment->grading_disabled($row->id);
928 if (!$this->is_downloading() && $this->hasgrade) {
929 $urlparams = array('id' => $this->assignment->get_course_module()->id,
930 'rownum' => 0,
931 'action' => 'grader');
933 if ($this->assignment->is_blind_marking()) {
934 if (empty($row->recordid)) {
935 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
937 $urlparams['blindid'] = $row->recordid;
938 } else {
939 $urlparams['userid'] = $row->userid;
942 $url = new moodle_url('/mod/assign/view.php', $urlparams);
943 $link = '<a href="' . $url . '" class="btn btn-primary">' . get_string('grade') . '</a>';
944 $grade .= $link . $separator;
947 $grade .= $this->display_grade($row->grade,
948 $this->quickgrading && !$gradingdisabled,
949 $row->userid,
950 $row->timemarked);
952 return $grade;
956 * Format a column of data for display.
958 * @param stdClass $row
959 * @return string
961 public function col_finalgrade(stdClass $row) {
962 $o = '';
964 $grade = $this->get_gradebook_data_for_user($row->userid);
965 if ($grade) {
966 $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
969 return $o;
973 * Format a column of data for display.
975 * @param stdClass $row
976 * @return string
978 public function col_timemarked(stdClass $row) {
979 $o = '-';
981 if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
982 $o = userdate($row->timemarked);
984 if ($row->timemarked && $this->is_downloading()) {
985 // Force it for downloads as it affects import.
986 $o = userdate($row->timemarked);
989 return $o;
993 * Format a column of data for display.
995 * @param stdClass $row
996 * @return string
998 public function col_timesubmitted(stdClass $row) {
999 $o = '-';
1001 $group = false;
1002 $submission = false;
1003 $this->get_group_and_submission($row->id, $group, $submission, -1);
1004 if ($submission && $submission->timemodified && $submission->status != ASSIGN_SUBMISSION_STATUS_NEW) {
1005 $o = userdate($submission->timemodified);
1006 } else if ($row->timesubmitted && $row->status != ASSIGN_SUBMISSION_STATUS_NEW) {
1007 $o = userdate($row->timesubmitted);
1010 return $o;
1014 * Format a column of data for display
1016 * @param stdClass $row
1017 * @return string
1019 public function col_status(stdClass $row) {
1020 $o = '';
1022 $instance = $this->assignment->get_instance();
1024 $due = $instance->duedate;
1025 if ($row->extensionduedate) {
1026 $due = $row->extensionduedate;
1027 } else if (!empty($row->duedate)) {
1028 // The override due date.
1029 $due = $row->duedate;
1032 $group = false;
1033 $submission = false;
1035 if ($instance->teamsubmission) {
1036 $this->get_group_and_submission($row->id, $group, $submission, -1);
1039 if ($instance->teamsubmission && !$group && !$instance->preventsubmissionnotingroup) {
1040 $group = true;
1043 if ($group && $submission) {
1044 $timesubmitted = $submission->timemodified;
1045 $status = $submission->status;
1046 } else {
1047 $timesubmitted = $row->timesubmitted;
1048 $status = $row->status;
1051 $displaystatus = $status;
1052 if ($displaystatus == 'new') {
1053 $displaystatus = '';
1056 if ($this->assignment->is_any_submission_plugin_enabled()) {
1058 $o .= $this->output->container(get_string('submissionstatus_' . $displaystatus, 'assign'),
1059 array('class'=>'submissionstatus' .$displaystatus));
1060 if ($due && $timesubmitted > $due && $status != ASSIGN_SUBMISSION_STATUS_NEW) {
1061 $usertime = format_time($timesubmitted - $due);
1062 $latemessage = get_string('submittedlateshort',
1063 'assign',
1064 $usertime);
1065 $o .= $this->output->container($latemessage, 'latesubmission');
1067 if ($row->locked) {
1068 $lockedstr = get_string('submissionslockedshort', 'assign');
1069 $o .= $this->output->container($lockedstr, 'lockedsubmission');
1072 // Add status of "grading" if markflow is not enabled.
1073 if (!$instance->markingworkflow) {
1074 if ($row->grade !== null && $row->grade >= 0) {
1075 if ($row->timemarked < $row->timesubmitted) {
1076 $o .= $this->output->container(get_string('gradedfollowupsubmit', 'assign'), 'gradingreminder');
1077 } else {
1078 $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
1080 } else if (!$timesubmitted || $status == ASSIGN_SUBMISSION_STATUS_NEW) {
1081 $now = time();
1082 if ($due && ($now > $due)) {
1083 $overduestr = get_string('overdue', 'assign', format_time($now - $due));
1084 $o .= $this->output->container($overduestr, 'overduesubmission');
1090 if ($instance->markingworkflow) {
1091 $o .= $this->col_workflowstatus($row);
1093 if ($row->extensionduedate) {
1094 $userdate = userdate($row->extensionduedate);
1095 $extensionstr = get_string('userextensiondate', 'assign', $userdate);
1096 $o .= $this->output->container($extensionstr, 'extensiondate');
1099 if ($this->is_downloading()) {
1100 $o = strip_tags(rtrim(str_replace('</div>', ' - ', $o), '- '));
1103 return $o;
1107 * Format a column of data for display.
1109 * @param stdClass $row
1110 * @return string
1112 public function col_allowsubmissionsfromdate(stdClass $row) {
1113 $o = '';
1115 if ($row->allowsubmissionsfromdate) {
1116 $userdate = userdate($row->allowsubmissionsfromdate);
1117 $o = ($this->is_downloading()) ? $userdate : $this->output->container($userdate, 'allowsubmissionsfromdate');
1120 return $o;
1124 * Format a column of data for display.
1126 * @param stdClass $row
1127 * @return string
1129 public function col_duedate(stdClass $row) {
1130 $o = '';
1132 if ($row->duedate) {
1133 $userdate = userdate($row->duedate);
1134 $o = ($this->is_downloading()) ? $userdate : $this->output->container($userdate, 'duedate');
1137 return $o;
1141 * Format a column of data for display.
1143 * @param stdClass $row
1144 * @return string
1146 public function col_cutoffdate(stdClass $row) {
1147 $o = '';
1149 if ($row->cutoffdate) {
1150 $userdate = userdate($row->cutoffdate);
1151 $o = ($this->is_downloading()) ? $userdate : $this->output->container($userdate, 'cutoffdate');
1154 return $o;
1158 * Format a column of data for display.
1160 * @param stdClass $row
1161 * @return string
1163 public function col_userid(stdClass $row) {
1164 global $USER;
1166 $edit = '';
1168 $actions = array();
1170 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1171 'rownum' => 0,
1172 'action' => 'grader');
1174 if ($this->assignment->is_blind_marking()) {
1175 if (empty($row->recordid)) {
1176 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
1178 $urlparams['blindid'] = $row->recordid;
1179 } else {
1180 $urlparams['userid'] = $row->userid;
1182 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1183 $noimage = null;
1185 if (!$row->grade) {
1186 $description = get_string('grade');
1187 } else {
1188 $description = get_string('updategrade', 'assign');
1190 $actions['grade'] = new action_menu_link_secondary(
1191 $url,
1192 $noimage,
1193 $description
1196 // Everything we need is in the row.
1197 $submission = $row;
1198 $flags = $row;
1199 if ($this->assignment->get_instance()->teamsubmission) {
1200 // Use the cache for this.
1201 $submission = false;
1202 $group = false;
1203 $this->get_group_and_submission($row->id, $group, $submission, -1);
1206 $submissionsopen = $this->assignment->submissions_open($row->id,
1207 true,
1208 $submission,
1209 $flags,
1210 $this->gradinginfo);
1211 $caneditsubmission = $this->assignment->can_edit_submission($row->id, $USER->id);
1213 // Hide for offline assignments.
1214 if ($this->assignment->is_any_submission_plugin_enabled()) {
1215 if (!$row->status ||
1216 $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
1217 !$this->assignment->get_instance()->submissiondrafts) {
1219 if (!$row->locked) {
1220 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1221 'userid'=>$row->id,
1222 'action'=>'lock',
1223 'sesskey'=>sesskey(),
1224 'page'=>$this->currpage);
1225 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1227 $description = get_string('preventsubmissionsshort', 'assign');
1228 $actions['lock'] = new action_menu_link_secondary(
1229 $url,
1230 $noimage,
1231 $description
1233 } else {
1234 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1235 'userid'=>$row->id,
1236 'action'=>'unlock',
1237 'sesskey'=>sesskey(),
1238 'page'=>$this->currpage);
1239 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1240 $description = get_string('allowsubmissionsshort', 'assign');
1241 $actions['unlock'] = new action_menu_link_secondary(
1242 $url,
1243 $noimage,
1244 $description
1249 if ($submissionsopen &&
1250 $USER->id != $row->id &&
1251 $caneditsubmission) {
1252 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1253 'userid'=>$row->id,
1254 'action'=>'editsubmission',
1255 'sesskey'=>sesskey(),
1256 'page'=>$this->currpage);
1257 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1258 $description = get_string('editsubmission', 'assign');
1259 $actions['editsubmission'] = new action_menu_link_secondary(
1260 $url,
1261 $noimage,
1262 $description
1266 if (($this->assignment->get_instance()->duedate ||
1267 $this->assignment->get_instance()->cutoffdate) &&
1268 $this->hasgrantextension) {
1269 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1270 'userid' => $row->id,
1271 'action' => 'grantextension',
1272 'sesskey' => sesskey(),
1273 'page' => $this->currpage);
1274 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1275 $description = get_string('grantextension', 'assign');
1276 $actions['grantextension'] = new action_menu_link_secondary(
1277 $url,
1278 $noimage,
1279 $description
1282 if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
1283 $this->assignment->get_instance()->submissiondrafts) {
1284 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1285 'userid'=>$row->id,
1286 'action'=>'reverttodraft',
1287 'sesskey'=>sesskey(),
1288 'page'=>$this->currpage);
1289 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1290 $description = get_string('reverttodraftshort', 'assign');
1291 $actions['reverttodraft'] = new action_menu_link_secondary(
1292 $url,
1293 $noimage,
1294 $description
1297 if ($row->status == ASSIGN_SUBMISSION_STATUS_DRAFT &&
1298 $this->assignment->get_instance()->submissiondrafts &&
1299 $caneditsubmission &&
1300 $submissionsopen &&
1301 $row->id != $USER->id) {
1302 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1303 'userid'=>$row->id,
1304 'action'=>'submitotherforgrading',
1305 'sesskey'=>sesskey(),
1306 'page'=>$this->currpage);
1307 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1308 $description = get_string('submitforgrading', 'assign');
1309 $actions['submitforgrading'] = new action_menu_link_secondary(
1310 $url,
1311 $noimage,
1312 $description
1316 $ismanual = $this->assignment->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
1317 $hassubmission = !empty($row->status);
1318 $notreopened = $hassubmission && $row->status != ASSIGN_SUBMISSION_STATUS_REOPENED;
1319 $isunlimited = $this->assignment->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS;
1320 $hasattempts = $isunlimited || $row->attemptnumber < $this->assignment->get_instance()->maxattempts - 1;
1322 if ($ismanual && $hassubmission && $notreopened && $hasattempts) {
1323 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1324 'userid'=>$row->id,
1325 'action'=>'addattempt',
1326 'sesskey'=>sesskey(),
1327 'page'=>$this->currpage);
1328 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1329 $description = get_string('addattempt', 'assign');
1330 $actions['addattempt'] = new action_menu_link_secondary(
1331 $url,
1332 $noimage,
1333 $description
1337 $menu = new action_menu();
1338 $menu->set_owner_selector('.gradingtable-actionmenu');
1339 $menu->set_alignment(action_menu::TL, action_menu::BL);
1340 $menu->set_constraint('.gradingtable > .no-overflow');
1341 $menu->set_menu_trigger(get_string('edit'));
1342 foreach ($actions as $action) {
1343 $menu->add($action);
1346 // Prioritise the menu ahead of all other actions.
1347 $menu->prioritise = true;
1349 $edit .= $this->output->render($menu);
1351 return $edit;
1355 * Write the plugin summary with an optional link to view the full feedback/submission.
1357 * @param assign_plugin $plugin Submission plugin or feedback plugin
1358 * @param stdClass $item Submission or grade
1359 * @param string $returnaction The return action to pass to the
1360 * view_submission page (the current page)
1361 * @param string $returnparams The return params to pass to the view_submission
1362 * page (the current page)
1363 * @return string The summary with an optional link
1365 private function format_plugin_summary_with_link(assign_plugin $plugin,
1366 stdClass $item,
1367 $returnaction,
1368 $returnparams) {
1369 $link = '';
1370 $showviewlink = false;
1372 $summary = $plugin->view_summary($item, $showviewlink);
1373 $separator = '';
1374 if ($showviewlink) {
1375 $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
1376 $icon = $this->output->pix_icon('t/preview', $viewstr);
1377 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1378 'sid'=>$item->id,
1379 'gid'=>$item->id,
1380 'plugin'=>$plugin->get_type(),
1381 'action'=>'viewplugin' . $plugin->get_subtype(),
1382 'returnaction'=>$returnaction,
1383 'returnparams'=>http_build_query($returnparams));
1384 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1385 $link = $this->output->action_link($url, $icon);
1386 $separator = $this->output->spacer(array(), true);
1389 return $link . $separator . $summary;
1394 * Format the submission and feedback columns.
1396 * @param string $colname The column name
1397 * @param stdClass $row The submission row
1398 * @return mixed string or NULL
1400 public function other_cols($colname, $row) {
1401 // For extra user fields the result is already in $row.
1402 if (empty($this->plugincache[$colname])) {
1403 return $row->$colname;
1406 // This must be a plugin field.
1407 $plugincache = $this->plugincache[$colname];
1409 $plugin = $plugincache[0];
1411 $field = null;
1412 if (isset($plugincache[1])) {
1413 $field = $plugincache[1];
1416 if ($plugin->is_visible() && $plugin->is_enabled()) {
1417 if ($plugin->get_subtype() == 'assignsubmission') {
1418 if ($this->assignment->get_instance()->teamsubmission) {
1419 $group = false;
1420 $submission = false;
1422 $this->get_group_and_submission($row->id, $group, $submission, -1);
1423 if ($submission) {
1424 if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1425 // For a newly reopened submission - we want to show the previous submission in the table.
1426 $this->get_group_and_submission($row->id, $group, $submission, $submission->attemptnumber-1);
1428 if (isset($field)) {
1429 return $plugin->get_editor_text($field, $submission->id);
1431 return $this->format_plugin_summary_with_link($plugin,
1432 $submission,
1433 'grading',
1434 array());
1436 } else if ($row->submissionid) {
1437 if ($row->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1438 // For a newly reopened submission - we want to show the previous submission in the table.
1439 $submission = $this->assignment->get_user_submission($row->userid, false, $row->attemptnumber - 1);
1440 } else {
1441 $submission = new stdClass();
1442 $submission->id = $row->submissionid;
1443 $submission->timecreated = $row->firstsubmission;
1444 $submission->timemodified = $row->timesubmitted;
1445 $submission->assignment = $this->assignment->get_instance()->id;
1446 $submission->userid = $row->userid;
1447 $submission->attemptnumber = $row->attemptnumber;
1449 // Field is used for only for import/export and refers the the fieldname for the text editor.
1450 if (isset($field)) {
1451 return $plugin->get_editor_text($field, $submission->id);
1453 return $this->format_plugin_summary_with_link($plugin,
1454 $submission,
1455 'grading',
1456 array());
1458 } else {
1459 $grade = null;
1460 if (isset($field)) {
1461 return $plugin->get_editor_text($field, $row->gradeid);
1464 if ($row->gradeid) {
1465 $grade = new stdClass();
1466 $grade->id = $row->gradeid;
1467 $grade->timecreated = $row->firstmarked;
1468 $grade->timemodified = $row->timemarked;
1469 $grade->assignment = $this->assignment->get_instance()->id;
1470 $grade->userid = $row->userid;
1471 $grade->grade = $row->grade;
1472 $grade->mailed = $row->mailed;
1473 $grade->attemptnumber = $row->attemptnumber;
1475 if ($this->quickgrading && $plugin->supports_quickgrading()) {
1476 return $plugin->get_quickgrading_html($row->userid, $grade);
1477 } else if ($grade) {
1478 return $this->format_plugin_summary_with_link($plugin,
1479 $grade,
1480 'grading',
1481 array());
1485 return '';
1489 * Using the current filtering and sorting - load all rows and return a single column from them.
1491 * @param string $columnname The name of the raw column data
1492 * @return array of data
1494 public function get_column_data($columnname) {
1495 $this->setup();
1496 $this->currpage = 0;
1497 $this->query_db($this->tablemaxrows);
1498 $result = array();
1499 foreach ($this->rawdata as $row) {
1500 $result[] = $row->$columnname;
1502 return $result;
1506 * Return things to the renderer.
1508 * @return string the assignment name
1510 public function get_assignment_name() {
1511 return $this->assignment->get_instance()->name;
1515 * Return things to the renderer.
1517 * @return int the course module id
1519 public function get_course_module_id() {
1520 return $this->assignment->get_course_module()->id;
1524 * Return things to the renderer.
1526 * @return int the course id
1528 public function get_course_id() {
1529 return $this->assignment->get_course()->id;
1533 * Return things to the renderer.
1535 * @return stdClass The course context
1537 public function get_course_context() {
1538 return $this->assignment->get_course_context();
1542 * Return things to the renderer.
1544 * @return bool Does this assignment accept submissions
1546 public function submissions_enabled() {
1547 return $this->assignment->is_any_submission_plugin_enabled();
1551 * Return things to the renderer.
1553 * @return bool Can this user view all grades (the gradebook)
1555 public function can_view_all_grades() {
1556 $context = $this->assignment->get_course_context();
1557 return has_capability('gradereport/grader:view', $context) &&
1558 has_capability('moodle/grade:viewall', $context);
1562 * Always return a valid sort - even if the userid column is missing.
1563 * @return array column name => SORT_... constant.
1565 public function get_sort_columns() {
1566 $result = parent::get_sort_columns();
1568 $assignment = $this->assignment->get_instance();
1569 if (empty($assignment->blindmarking)) {
1570 $result = array_merge($result, array('userid' => SORT_ASC));
1571 } else {
1572 $result = array_merge($result, [
1573 'COALESCE(s.timecreated, ' . time() . ')' => SORT_ASC,
1574 'COALESCE(s.id, ' . PHP_INT_MAX . ')' => SORT_ASC,
1575 'um.id' => SORT_ASC,
1578 return $result;
1582 * Override the table show_hide_link to not show for select column.
1584 * @param string $column the column name, index into various names.
1585 * @param int $index numerical index of the column.
1586 * @return string HTML fragment.
1588 protected function show_hide_link($column, $index) {
1589 if ($index > 0 || !$this->hasgrade) {
1590 return parent::show_hide_link($column, $index);
1592 return '';
1596 * Overides setup to ensure it will only run a single time.
1598 public function setup() {
1599 // Check if the setup function has been called before, we should not run it twice.
1600 // If we do the sortorder of the table will be broken.
1601 if (!empty($this->setup)) {
1602 return;
1604 parent::setup();