MDL-66147 mod_assign: grading table status col shows relative dates
[moodle.git] / mod / assign / gradingtable.php
CommitLineData
bbd0e548
DW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This file contains the definition for the grading table which subclassses easy_table
19 *
20 * @package mod_assign
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27require_once($CFG->libdir.'/tablelib.php');
28require_once($CFG->libdir.'/gradelib.php');
29require_once($CFG->dirroot.'/mod/assign/locallib.php');
30
31/**
32 * Extends table_sql to provide a table of assignment submissions
33 *
34 * @package mod_assign
35 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class 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;
bf78ebd6
DW
51 /** @var boolean $quickgrading */
52 private $quickgrading = false;
9e795179
DW
53 /** @var boolean $hasgrantextension - Only do the capability check once for the entire table */
54 private $hasgrantextension = false;
4a47008c
DW
55 /** @var boolean $hasgrade - Only do the capability check once for the entire table */
56 private $hasgrade = false;
cd01491c
DW
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();
df47b77f
DW
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;
bbd0e548
DW
67
68 /**
69 * overridden constructor keeps a reference to the assignment class that is displaying this table
70 *
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
bf78ebd6 75 * @param bool $quickgrading Is this table wrapped in a quickgrading form?
1561a37c 76 * @param string $downloadfilename
bbd0e548 77 */
e5403f8c
DW
78 public function __construct(assign $assignment,
79 $perpage,
80 $filter,
81 $rowoffset,
82 $quickgrading,
83 $downloadfilename = null) {
f8d107b3 84 global $CFG, $PAGE, $DB, $USER;
bbd0e548 85 parent::__construct('mod_assign_grading');
f5ba1b93 86 $this->is_persistent(true);
bbd0e548 87 $this->assignment = $assignment;
df47b77f 88
4a47008c
DW
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();
93
323f221b
MH
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());
97
df47b77f
DW
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();
103 }
104 $this->plugingradingbatchoperations[$plugin->get_type()][$action] = $description;
105 }
106 }
107 }
bbd0e548 108 $this->perpage = $perpage;
4a47008c 109 $this->quickgrading = $quickgrading && $this->hasgrade;
bbd0e548
DW
110 $this->output = $PAGE->get_renderer('mod_assign');
111
37fc47f1 112 $urlparams = array('action' => 'grading', 'id' => $assignment->get_course_module()->id);
e5403f8c
DW
113 $url = new moodle_url($CFG->wwwroot . '/mod/assign/view.php', $urlparams);
114 $this->define_baseurl($url);
bbd0e548 115
e5403f8c 116 // Do some business - then set the sql.
bbd0e548
DW
117 $currentgroup = groups_get_activity_group($assignment->get_course_module(), true);
118
119 if ($rowoffset) {
120 $this->rownum = $rowoffset - 1;
121 }
122
123 $users = array_keys( $assignment->list_participants($currentgroup, true));
124 if (count($users) == 0) {
e5403f8c 125 // Insert a record that will never match to the sql is still valid.
bbd0e548
DW
126 $users[] = -1;
127 }
128
129 $params = array();
a3dbdc2c
DW
130 $params['assignmentid1'] = (int)$this->assignment->get_instance()->id;
131 $params['assignmentid2'] = (int)$this->assignment->get_instance()->id;
df211804 132 $params['assignmentid3'] = (int)$this->assignment->get_instance()->id;
79ea89fe 133 $params['newstatus'] = ASSIGN_SUBMISSION_STATUS_NEW;
bbd0e548 134
d08e6c31
DW
135 $extrauserfields = get_extra_user_fields($this->assignment->get_context());
136
137 $fields = user_picture::fields('u', $extrauserfields) . ', ';
9e795179
DW
138 $fields .= 'u.id as userid, ';
139 $fields .= 's.status as status, ';
140 $fields .= 's.id as submissionid, ';
141 $fields .= 's.timecreated as firstsubmission, ';
79ea89fe 142 $fields .= "CASE WHEN status <> :newstatus THEN s.timemodified ELSE NULL END as timesubmitted, ";
df211804 143 $fields .= 's.attemptnumber as attemptnumber, ';
9e795179
DW
144 $fields .= 'g.id as gradeid, ';
145 $fields .= 'g.grade as grade, ';
146 $fields .= 'g.timemodified as timemarked, ';
147 $fields .= 'g.timecreated as firstmarked, ';
df211804
DW
148 $fields .= 'uf.mailed as mailed, ';
149 $fields .= 'uf.locked as locked, ';
f8d107b3
DM
150 $fields .= 'uf.extensionduedate as extensionduedate, ';
151 $fields .= 'uf.workflowstate as workflowstate, ';
a13f1f49 152 $fields .= 'uf.allocatedmarker as allocatedmarker';
df211804 153
df211804 154 $from = '{user} u
d838640e
MN
155 LEFT JOIN {assign_submission} s
156 ON u.id = s.userid
157 AND s.assignment = :assignmentid1
334db377
DW
158 AND s.latest = 1 ';
159
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) ';
d838640e
MN
165
166 $from .= 'LEFT JOIN {assign_user_flags} uf
167 ON u.id = uf.userid
4ed4be49
AN
168 AND uf.assignment = :assignmentid3 ';
169
a13f1f49
KH
170 $hasoverrides = $this->assignment->has_overrides();
171
172 if ($hasoverrides) {
28cdba9d
EM
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;
178
86bc0ae4
NN
179 list($userwhere1, $userparams1) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'priorityuser');
180 list($userwhere2, $userparams2) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'effectiveuser');
181
182 $userwhere1 = "WHERE u.id {$userwhere1}";
183 $userwhere2 = "WHERE u.id {$userwhere2}";
184 $params = array_merge($params, $userparams1);
185 $params = array_merge($params, $userparams2);
186
a13f1f49
KH
187 $fields .= ', priority.priority, ';
188 $fields .= 'effective.allowsubmissionsfromdate, ';
189 $fields .= 'effective.duedate, ';
190 $fields .= 'effective.cutoffdate ';
191
192 $from .= ' LEFT JOIN (
193 SELECT merged.userid, min(merged.priority) priority FROM (
194 ( SELECT u.id as userid, 9999999 AS priority
86bc0ae4 195 FROM {user} u '.$userwhere1.'
a13f1f49
KH
196 )
197 UNION
198 ( SELECT uo.userid, 0 AS priority
199 FROM {assign_overrides} uo
200 WHERE uo.assignid = :assignmentid5
201 )
202 UNION
203 ( SELECT gm.userid, go.sortorder AS priority
204 FROM {assign_overrides} go
205 JOIN {groups} g ON g.id = go.groupid
206 JOIN {groups_members} gm ON gm.groupid = g.id
207 WHERE go.assignid = :assignmentid6
208 )
b5c24c99 209 ) merged
a13f1f49
KH
210 GROUP BY merged.userid
211 ) priority ON priority.userid = u.id
212
213 JOIN (
214 (SELECT 9999999 AS priority,
215 u.id AS userid,
a13f1f49
KH
216 a.allowsubmissionsfromdate,
217 a.duedate,
218 a.cutoffdate
219 FROM {user} u
220 JOIN {assign} a ON a.id = :assignmentid7
86bc0ae4 221 '.$userwhere2.'
d685b959
IT
222 )
223 UNION
a13f1f49
KH
224 (SELECT 0 AS priority,
225 uo.userid,
a13f1f49
KH
226 uo.allowsubmissionsfromdate,
227 uo.duedate,
228 uo.cutoffdate
229 FROM {assign_overrides} uo
230 WHERE uo.assignid = :assignmentid8
d685b959
IT
231 )
232 UNION
a13f1f49
KH
233 (SELECT go.sortorder AS priority,
234 gm.userid,
a13f1f49
KH
235 go.allowsubmissionsfromdate,
236 go.duedate,
237 go.cutoffdate
238 FROM {assign_overrides} go
239 JOIN {groups} g ON g.id = go.groupid
240 JOIN {groups_members} gm ON gm.groupid = g.id
241 WHERE go.assignid = :assignmentid9
d685b959 242 )
a13f1f49 243
b5c24c99 244 ) effective ON effective.priority = priority.priority AND effective.userid = priority.userid ';
a13f1f49 245 }
d685b959 246
4ed4be49
AN
247 if (!empty($this->assignment->get_instance()->blindmarking)) {
248 $from .= 'LEFT JOIN {assign_user_mapping} um
249 ON u.id = um.userid
28cdba9d
EM
250 AND um.assignment = :assignmentidblind ';
251 $params['assignmentidblind'] = (int)$this->assignment->get_instance()->id;
4ed4be49
AN
252 $fields .= ', um.id as recordid ';
253 }
bbd0e548 254
86bc0ae4 255 $userparams3 = array();
a3dbdc2c
DW
256 $userindex = 0;
257
86bc0ae4
NN
258 list($userwhere3, $userparams3) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
259 $where = 'u.id ' . $userwhere3;
260 $params = array_merge($params, $userparams3);
9682626e 261
93d1de6d
DW
262 // The filters do not make sense when there are no submissions, so do not apply them.
263 if ($this->assignment->is_any_submission_plugin_enabled()) {
264 if ($filter == ASSIGN_FILTER_SUBMITTED) {
77755451
DW
265 $where .= ' AND (s.timemodified IS NOT NULL AND
266 s.status = :submitted) ';
267 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
268
c80d59f2 269 } else if ($filter == ASSIGN_FILTER_NOT_SUBMITTED) {
79ea89fe 270 $where .= ' AND (s.timemodified IS NULL OR s.status <> :submitted) ';
8f165230 271 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
77755451 272 } else if ($filter == ASSIGN_FILTER_REQUIRE_GRADING) {
93d1de6d
DW
273 $where .= ' AND (s.timemodified IS NOT NULL AND
274 s.status = :submitted AND
c28c7645
BA
275 (s.timemodified >= g.timemodified OR g.timemodified IS NULL OR g.grade IS NULL';
276
07d23784
DW
277 // Assignment grade is set to the negative grade scale id when scales are used.
278 if ($this->assignment->get_instance()->grade < 0) {
c28c7645
BA
279 // Scale grades are set to -1 when not graded.
280 $where .= ' OR g.grade = -1';
281 }
282
283 $where .= '))';
93d1de6d 284 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
77755451 285
696bd4d6
TB
286 } else if ($filter == ASSIGN_FILTER_GRANTED_EXTENSION) {
287 $where .= ' AND uf.extensionduedate > 0 ';
288
77755451 289 } else if (strpos($filter, ASSIGN_FILTER_SINGLE_USER) === 0) {
93d1de6d
DW
290 $userfilter = (int) array_pop(explode('=', $filter));
291 $where .= ' AND (u.id = :userid)';
292 $params['userid'] = $userfilter;
293 }
bbd0e548 294 }
df211804 295
5a96486b
DM
296 if ($this->assignment->get_instance()->markingworkflow &&
297 $this->assignment->get_instance()->markingallocation) {
f8d107b3 298 if (has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
13e82f1c 299 // Check to see if marker filter is set.
f8d107b3
DM
300 $markerfilter = (int)get_user_preferences('assign_markerfilter', '');
301 if (!empty($markerfilter)) {
c80d59f2
JF
302 if ($markerfilter == ASSIGN_MARKER_FILTER_NO_MARKER) {
303 $where .= ' AND (uf.allocatedmarker IS NULL OR uf.allocatedmarker = 0)';
dd7de4ea
DW
304 } else {
305 $where .= ' AND uf.allocatedmarker = :markerid';
306 $params['markerid'] = $markerfilter;
c80d59f2 307 }
f8d107b3 308 }
f8d107b3
DM
309 }
310 }
311
312 if ($this->assignment->get_instance()->markingworkflow) {
313 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
314 if (!empty($workflowstates)) {
315 $workflowfilter = get_user_preferences('assign_workflowfilter', '');
13e82f1c
DW
316 if ($workflowfilter == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED) {
317 $where .= ' AND (uf.workflowstate = :workflowstate OR uf.workflowstate IS NULL OR '.
318 $DB->sql_isempty('assign_user_flags', 'workflowstate', true, true).')';
319 $params['workflowstate'] = $workflowfilter;
320 } else if (array_key_exists($workflowfilter, $workflowstates)) {
321 $where .= ' AND uf.workflowstate = :workflowstate';
322 $params['workflowstate'] = $workflowfilter;
f8d107b3
DM
323 }
324 }
325 }
326
bbd0e548
DW
327 $this->set_sql($fields, $from, $where, $params);
328
df47b77f
DW
329 if ($downloadfilename) {
330 $this->is_downloading('csv', $downloadfilename);
331 }
332
bbd0e548
DW
333 $columns = array();
334 $headers = array();
335
df47b77f 336 // Select.
4a47008c 337 if (!$this->is_downloading() && $this->hasgrade) {
df47b77f
DW
338 $columns[] = 'select';
339 $headers[] = get_string('select') .
7400be1b
RT
340 '<div class="selectall"><label class="accesshide" for="selectall">' . get_string('selectall') . '</label>
341 <input type="checkbox" id="selectall" name="selectall" title="' . get_string('selectall') . '"/></div>';
bbd0e548
DW
342 }
343
b473171a 344 // User picture.
323f221b 345 if ($this->hasviewblind || !$this->assignment->is_blind_marking()) {
df47b77f
DW
346 if (!$this->is_downloading()) {
347 $columns[] = 'picture';
348 $headers[] = get_string('pictureofuser');
349 } else {
350 $columns[] = 'recordid';
351 $headers[] = get_string('recordid', 'assign');
352 }
b473171a
DW
353
354 // Fullname.
355 $columns[] = 'fullname';
356 $headers[] = get_string('fullname');
d08e6c31 357
59e95487 358 // Participant # details if can view real identities.
18b79734
MH
359 if ($this->assignment->is_blind_marking()) {
360 if (!$this->is_downloading()) {
361 $columns[] = 'recordid';
362 $headers[] = get_string('recordid', 'assign');
363 }
364 }
365
d08e6c31
DW
366 foreach ($extrauserfields as $extrafield) {
367 $columns[] = $extrafield;
368 $headers[] = get_user_field_name($extrafield);
369 }
b473171a
DW
370 } else {
371 // Record ID.
372 $columns[] = 'recordid';
373 $headers[] = get_string('recordid', 'assign');
374 }
bbd0e548 375
e5403f8c 376 // Submission status.
24937b2c
GF
377 $columns[] = 'status';
378 $headers[] = get_string('status', 'assign');
bbd0e548 379
a13f1f49
KH
380 if ($hasoverrides) {
381 // Allowsubmissionsfromdate.
382 $columns[] = 'allowsubmissionsfromdate';
383 $headers[] = get_string('allowsubmissionsfromdate', 'assign');
d685b959 384
a13f1f49
KH
385 // Duedate.
386 $columns[] = 'duedate';
387 $headers[] = get_string('duedate', 'assign');
d685b959 388
a13f1f49
KH
389 // Cutoffdate.
390 $columns[] = 'cutoffdate';
391 $headers[] = get_string('cutoffdate', 'assign');
392 }
d685b959 393
e5403f8c 394 // Team submission columns.
12a1a0da
DW
395 if ($assignment->get_instance()->teamsubmission) {
396 $columns[] = 'team';
397 $headers[] = get_string('submissionteam', 'assign');
12a1a0da 398 }
13e82f1c 399 // Allocated marker.
5a96486b
DM
400 if ($this->assignment->get_instance()->markingworkflow &&
401 $this->assignment->get_instance()->markingallocation &&
f8d107b3
DM
402 has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
403 // Add a column for the allocated marker.
404 $columns[] = 'allocatedmarker';
405 $headers[] = get_string('marker', 'assign');
406 }
e5403f8c 407 // Grade.
bbd0e548
DW
408 $columns[] = 'grade';
409 $headers[] = get_string('grade');
df47b77f 410 if ($this->is_downloading()) {
1879a74e
DW
411 $gradetype = $this->assignment->get_instance()->grade;
412 if ($gradetype > 0) {
df47b77f
DW
413 $columns[] = 'grademax';
414 $headers[] = get_string('maxgrade', 'assign');
1879a74e 415 } else if ($gradetype < 0) {
df47b77f
DW
416 // This is a custom scale.
417 $columns[] = 'scale';
418 $headers[] = get_string('scale', 'assign');
419 }
f8d107b3
DM
420
421 if ($this->assignment->get_instance()->markingworkflow) {
422 // Add a column for the marking workflow state.
423 $columns[] = 'workflowstate';
424 $headers[] = get_string('markingworkflowstate', 'assign');
425 }
1879a74e 426 // Add a column to show if this grade can be changed.
f8d107b3
DM
427 $columns[] = 'gradecanbechanged';
428 $headers[] = get_string('gradecanbechanged', 'assign');
df47b77f 429 }
4a47008c 430 if (!$this->is_downloading() && $this->hasgrade) {
2bda0460
NK
431 // We have to call this column userid so we can use userid as a default sortable column.
432 $columns[] = 'userid';
433 $headers[] = get_string('edit');
434 }
bbd0e548 435
e5403f8c 436 // Submission plugins.
bbd0e548
DW
437 if ($assignment->is_any_submission_plugin_enabled()) {
438 $columns[] = 'timesubmitted';
439 $headers[] = get_string('lastmodifiedsubmission', 'assign');
440
441 foreach ($this->assignment->get_submission_plugins() as $plugin) {
df47b77f
DW
442 if ($this->is_downloading()) {
443 if ($plugin->is_visible() && $plugin->is_enabled()) {
444 foreach ($plugin->get_editor_fields() as $field => $description) {
445 $index = 'plugin' . count($this->plugincache);
446 $this->plugincache[$index] = array($plugin, $field);
447 $columns[] = $index;
448 $headers[] = $plugin->get_name();
449 }
450 }
451 } else {
452 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
453 $index = 'plugin' . count($this->plugincache);
454 $this->plugincache[$index] = array($plugin);
455 $columns[] = $index;
456 $headers[] = $plugin->get_name();
457 }
bbd0e548
DW
458 }
459 }
460 }
461
e5403f8c 462 // Time marked.
bbd0e548
DW
463 $columns[] = 'timemarked';
464 $headers[] = get_string('lastmodifiedgrade', 'assign');
465
e5403f8c 466 // Feedback plugins.
bbd0e548 467 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
df47b77f
DW
468 if ($this->is_downloading()) {
469 if ($plugin->is_visible() && $plugin->is_enabled()) {
470 foreach ($plugin->get_editor_fields() as $field => $description) {
471 $index = 'plugin' . count($this->plugincache);
472 $this->plugincache[$index] = array($plugin, $field);
473 $columns[] = $index;
474 $headers[] = $description;
475 }
476 }
477 } else if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
478 $index = 'plugin' . count($this->plugincache);
479 $this->plugincache[$index] = array($plugin);
480 $columns[] = $index;
bbd0e548
DW
481 $headers[] = $plugin->get_name();
482 }
483 }
484
9f484b98
RW
485 // Exclude 'Final grade' column in downloaded grading worksheets.
486 if (!$this->is_downloading()) {
487 // Final grade.
488 $columns[] = 'finalgrade';
489 $headers[] = get_string('finalgrade', 'grades');
490 }
bbd0e548 491
e5403f8c
DW
492 // Load the grading info for all users.
493 $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id,
494 'mod',
495 'assign',
496 $this->assignment->get_instance()->id,
497 $users);
a769a001
DW
498
499 if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
500 $columns[] = 'outcomes';
501 $headers[] = get_string('outcomes', 'grades');
502 }
bbd0e548 503
e5403f8c 504 // Set the columns.
bbd0e548
DW
505 $this->define_columns($columns);
506 $this->define_headers($headers);
d08e6c31
DW
507 foreach ($extrauserfields as $extrafield) {
508 $this->column_class($extrafield, $extrafield);
509 }
f9e62127 510 $this->no_sorting('recordid');
bbd0e548 511 $this->no_sorting('finalgrade');
f9e62127 512 $this->no_sorting('userid');
bbd0e548 513 $this->no_sorting('select');
a769a001
DW
514 $this->no_sorting('outcomes');
515
12a1a0da
DW
516 if ($assignment->get_instance()->teamsubmission) {
517 $this->no_sorting('team');
12a1a0da
DW
518 }
519
f9e62127 520 $plugincolumnindex = 0;
bbd0e548 521 foreach ($this->assignment->get_submission_plugins() as $plugin) {
df47b77f 522 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
f9e62127
DW
523 $submissionpluginindex = 'plugin' . $plugincolumnindex++;
524 $this->no_sorting($submissionpluginindex);
bbd0e548
DW
525 }
526 }
527 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
df47b77f 528 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
f9e62127
DW
529 $feedbackpluginindex = 'plugin' . $plugincolumnindex++;
530 $this->no_sorting($feedbackpluginindex);
bbd0e548
DW
531 }
532 }
533
5fc59630
RW
534 // When there is no data we still want the column headers printed in the csv file.
535 if ($this->is_downloading()) {
536 $this->start_output();
537 }
bbd0e548
DW
538 }
539
61cf3253 540 /**
e5403f8c 541 * Before adding each row to the table make sure rownum is incremented.
61cf3253
DW
542 *
543 * @param array $row row of data from db used to make one row of the table.
544 * @return array one row for the table
545 */
e5403f8c 546 public function format_row($row) {
61cf3253
DW
547 if ($this->rownum < 0) {
548 $this->rownum = $this->currpage * $this->pagesize;
549 } else {
550 $this->rownum += 1;
551 }
552
553 return parent::format_row($row);
554 }
555
b473171a 556 /**
e5403f8c 557 * Add a column with an ID that uniquely identifies this user in this assignment.
b473171a 558 *
1561a37c 559 * @param stdClass $row
b473171a
DW
560 * @return string
561 */
e5403f8c 562 public function col_recordid(stdClass $row) {
4ed4be49
AN
563 if (empty($row->recordid)) {
564 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
565 }
566 return get_string('hiddenuser', 'assign') . $row->recordid;
e5403f8c 567 }
b473171a
DW
568
569
bbd0e548 570 /**
e5403f8c 571 * Add the userid to the row class so it can be updated via ajax.
bbd0e548
DW
572 *
573 * @param stdClass $row The row of data
574 * @return string The row class
575 */
e5403f8c 576 public function get_row_class($row) {
bbd0e548
DW
577 return 'user' . $row->userid;
578 }
579
580 /**
e5403f8c 581 * Return the number of rows to display on a single page.
bbd0e548
DW
582 *
583 * @return int The number of rows per page
584 */
e5403f8c 585 public function get_rows_per_page() {
bbd0e548
DW
586 return $this->perpage;
587 }
588
f8d107b3
DM
589 /**
590 * list current marking workflow state
591 *
592 * @param stdClass $row
593 * @return string
594 */
595 public function col_workflowstatus(stdClass $row) {
596 $o = '';
597
598 $gradingdisabled = $this->assignment->grading_disabled($row->id);
e24c2c1a
AA
599 // The function in the assignment keeps a static cache of this list of states.
600 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
601 $workflowstate = $row->workflowstate;
602 if (empty($workflowstate)) {
603 $workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
604 }
605 if ($this->quickgrading && !$gradingdisabled) {
606 $notmarked = get_string('markingworkflowstatenotmarked', 'assign');
607 $name = 'quickgrade_' . $row->id . '_workflowstate';
608 $o .= html_writer::select($workflowstates, $name, $workflowstate, array('' => $notmarked));
609 // Check if this user is a marker that can't manage allocations and doesn't have the marker column added.
5a96486b
DM
610 if ($this->assignment->get_instance()->markingworkflow &&
611 $this->assignment->get_instance()->markingallocation &&
e24c2c1a
AA
612 !has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
613
614 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
615 $o .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $name,
616 'value' => $row->allocatedmarker));
f8d107b3 617 }
e24c2c1a
AA
618 } else {
619 $o .= $this->output->container(get_string('markingworkflowstate' . $workflowstate, 'assign'), $workflowstate);
f8d107b3
DM
620 }
621 return $o;
622 }
623
624 /**
625 * For download only - list current marking workflow state
626 *
627 * @param stdClass $row - The row of data
628 * @return string The current marking workflow state
629 */
630 public function col_workflowstate($row) {
631 $state = $row->workflowstate;
632 if (empty($state)) {
633 $state = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
634 }
635
636 return get_string('markingworkflowstate' . $state, 'assign');
637 }
638
639 /**
640 * list current marker
641 *
642 * @param stdClass $row - The row of data
643 * @return id the user->id of the marker.
644 */
13e82f1c 645 public function col_allocatedmarker(stdClass $row) {
f8d107b3
DM
646 static $markers = null;
647 static $markerlist = array();
648 if ($markers === null) {
84e3c97b 649 list($sort, $params) = users_order_by_sql('u');
3ffd1382
AM
650 // Only enrolled users could be assigned as potential markers.
651 $markers = get_enrolled_users($this->assignment->get_context(), 'mod/assign:grade', 0, 'u.*', $sort);
f8d107b3 652 $markerlist[0] = get_string('choosemarker', 'assign');
05fdbe37 653 $viewfullnames = has_capability('moodle/site:viewfullnames', $this->assignment->get_context());
f8d107b3 654 foreach ($markers as $marker) {
05fdbe37 655 $markerlist[$marker->id] = fullname($marker, $viewfullnames);
f8d107b3
DM
656 }
657 }
658 if (empty($markerlist)) {
659 // TODO: add some form of notification here that no markers are available.
660 return '';
661 }
662 if ($this->is_downloading()) {
60a601db 663 if (isset($markers[$row->allocatedmarker])) {
05fdbe37
SR
664 return fullname($markers[$row->allocatedmarker],
665 has_capability('moodle/site:viewfullnames', $this->assignment->get_context()));
60a601db
JF
666 } else {
667 return '';
668 }
f8d107b3
DM
669 }
670
671 if ($this->quickgrading && has_capability('mod/assign:manageallocations', $this->assignment->get_context()) &&
672 (empty($row->workflowstate) ||
673 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_INMARKING ||
674 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED)) {
675
676 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
677 return html_writer::select($markerlist, $name, $row->allocatedmarker, false);
678 } else if (!empty($row->allocatedmarker)) {
679 $output = '';
680 if ($this->quickgrading) { // Add hidden field for quickgrading page.
681 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
37fc47f1
DW
682 $attributes = ['type' => 'hidden', 'name' => $name, 'value' => $row->allocatedmarker];
683 $output .= html_writer::empty_tag('input', $attributes);
f8d107b3
DM
684 }
685 $output .= $markerlist[$row->allocatedmarker];
686 return $output;
687 }
688 }
df47b77f
DW
689 /**
690 * For download only - list all the valid options for this custom scale.
691 *
692 * @param stdClass $row - The row of data
693 * @return string A list of valid options for the current scale
694 */
7a2b911c 695 public function col_scale($row) {
df47b77f
DW
696 global $DB;
697
698 if (empty($this->scale)) {
37fc47f1 699 $dbparams = array('id' => -($this->assignment->get_instance()->grade));
e5403f8c 700 $this->scale = $DB->get_record('scale', $dbparams);
df47b77f
DW
701 }
702
703 if (!empty($this->scale->scale)) {
704 return implode("\n", explode(',', $this->scale->scale));
705 }
706 return '';
707 }
708
bbd0e548
DW
709 /**
710 * Display a grade with scales etc.
711 *
712 * @param string $grade
bf78ebd6 713 * @param boolean $editable
2a4fbc32
SH
714 * @param int $userid The user id of the user this grade belongs to
715 * @param int $modified Timestamp showing when the grade was last modified
bbd0e548
DW
716 * @return string The formatted grade
717 */
e5403f8c 718 public function display_grade($grade, $editable, $userid, $modified) {
bbd0e548 719 if ($this->is_downloading()) {
df47b77f
DW
720 if ($this->assignment->get_instance()->grade >= 0) {
721 if ($grade == -1 || $grade === null) {
722 return '';
723 }
23f2f644
TB
724 $gradeitem = $this->assignment->get_grade_item();
725 return format_float($grade, $gradeitem->get_decimals());
df47b77f
DW
726 } else {
727 // This is a custom scale.
728 $scale = $this->assignment->display_grade($grade, false);
729 if ($scale == '-') {
730 $scale = '';
731 }
732 return $scale;
733 }
bbd0e548 734 }
df47b77f 735 return $this->assignment->display_grade($grade, $editable, $userid, $modified);
bbd0e548
DW
736 }
737
12a1a0da 738 /**
e5403f8c 739 * Get the team info for this user.
12a1a0da
DW
740 *
741 * @param stdClass $row
742 * @return string The team name
743 */
e5403f8c 744 public function col_team(stdClass $row) {
cd01491c
DW
745 $submission = false;
746 $group = false;
5ef72f85 747 $this->get_group_and_submission($row->id, $group, $submission, -1);
12a1a0da
DW
748 if ($group) {
749 return $group->name;
ce449f63
AH
750 } else if ($this->assignment->get_instance()->preventsubmissionnotingroup) {
751 $usergroups = $this->assignment->get_all_groups($row->id);
752 if (count($usergroups) > 1) {
753 return get_string('multipleteamsgrader', 'assign');
754 } else {
755 return get_string('noteamgrader', 'assign');
756 }
12a1a0da
DW
757 }
758 return get_string('defaultteam', 'assign');
759 }
760
761 /**
cd01491c
DW
762 * Use a static cache to try and reduce DB calls.
763 *
764 * @param int $userid The user id for this submission
1561a37c
DW
765 * @param int $group The groupid (returned)
766 * @param stdClass|false $submission The stdClass submission or false (returned)
5ef72f85 767 * @param int $attemptnumber Return a specific attempt number (-1 for latest)
cd01491c 768 */
5ef72f85 769 protected function get_group_and_submission($userid, &$group, &$submission, $attemptnumber) {
cd01491c
DW
770 $group = false;
771 if (isset($this->submissiongroups[$userid])) {
772 $group = $this->submissiongroups[$userid];
773 } else {
774 $group = $this->assignment->get_submission_group($userid, false);
775 $this->submissiongroups[$userid] = $group;
776 }
777
778 $groupid = 0;
779 if ($group) {
780 $groupid = $group->id;
781 }
782
5ef72f85
DW
783 // Static cache is keyed by groupid and attemptnumber.
784 // We may need both the latest and previous attempt in the same page.
785 if (isset($this->groupsubmissions[$groupid . ':' . $attemptnumber])) {
786 $submission = $this->groupsubmissions[$groupid . ':' . $attemptnumber];
cd01491c 787 } else {
5ef72f85
DW
788 $submission = $this->assignment->get_group_submission($userid, $groupid, false, $attemptnumber);
789 $this->groupsubmissions[$groupid . ':' . $attemptnumber] = $submission;
cd01491c
DW
790 }
791 }
792
a769a001 793 /**
e5403f8c 794 * Format a list of outcomes.
a769a001
DW
795 *
796 * @param stdClass $row
797 * @return string
798 */
e5403f8c 799 public function col_outcomes(stdClass $row) {
a769a001 800 $outcomes = '';
e5403f8c 801 foreach ($this->gradinginfo->outcomes as $index => $outcome) {
a769a001
DW
802 $options = make_grades_menu(-$outcome->scaleid);
803
804 $options[0] = get_string('nooutcome', 'grades');
2c6a3dbf 805 if ($this->quickgrading && !($outcome->grades[$row->userid]->locked)) {
bf78ebd6
DW
806 $select = '<select name="outcome_' . $index . '_' . $row->userid . '" class="quickgrade">';
807 foreach ($options as $optionindex => $optionvalue) {
808 $selected = '';
809 if ($outcome->grades[$row->userid]->grade == $optionindex) {
810 $selected = 'selected="selected"';
811 }
812 $select .= '<option value="' . $optionindex . '"' . $selected . '>' . $optionvalue . '</option>';
813 }
814 $select .= '</select>';
815 $outcomes .= $this->output->container($outcome->name . ': ' . $select, 'outcome');
816 } else {
e5403f8c 817 $name = $outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade];
6b7aa837
TL
818 if ($this->is_downloading()) {
819 $outcomes .= $name;
820 } else {
821 $outcomes .= $this->output->container($name, 'outcome');
822 }
bf78ebd6 823 }
a769a001
DW
824 }
825
826 return $outcomes;
827 }
828
829
bbd0e548 830 /**
e5403f8c 831 * Format a user picture for display.
bbd0e548
DW
832 *
833 * @param stdClass $row
834 * @return string
835 */
e5403f8c 836 public function col_picture(stdClass $row) {
d19de800 837 return $this->output->user_picture($row);
bbd0e548
DW
838 }
839
840 /**
e5403f8c 841 * Format a user record for display (link to profile).
bbd0e548
DW
842 *
843 * @param stdClass $row
844 * @return string
845 */
e5403f8c 846 public function col_fullname($row) {
df47b77f
DW
847 if (!$this->is_downloading()) {
848 $courseid = $this->assignment->get_course()->id;
37fc47f1 849 $link = new moodle_url('/user/view.php', array('id' => $row->id, 'course' => $courseid));
d63e7470 850 $fullname = $this->output->action_link($link, $this->assignment->fullname($row));
df47b77f 851 } else {
d63e7470 852 $fullname = $this->assignment->fullname($row);
df47b77f 853 }
a69944eb 854
1ecb8044 855 if (!$this->assignment->is_active_user($row->id)) {
a69944eb 856 $suspendedstring = get_string('userenrolmentsuspended', 'grades');
663640f5 857 $fullname .= ' ' . $this->output->pix_icon('i/enrolmentsuspended', $suspendedstring);
a69944eb
RT
858 $fullname = html_writer::tag('span', $fullname, array('class' => 'usersuspended'));
859 }
860 return $fullname;
bbd0e548
DW
861 }
862
863 /**
e5403f8c 864 * Insert a checkbox for selecting the current row for batch operations.
bbd0e548
DW
865 *
866 * @param stdClass $row
867 * @return string
868 */
e5403f8c
DW
869 public function col_select(stdClass $row) {
870 $selectcol = '<label class="accesshide" for="selectuser_' . $row->userid . '">';
d63e7470 871 $selectcol .= get_string('selectuser', 'assign', $this->assignment->fullname($row));
e5403f8c
DW
872 $selectcol .= '</label>';
873 $selectcol .= '<input type="checkbox"
874 id="selectuser_' . $row->userid . '"
875 name="selectedusers"
876 value="' . $row->userid . '"/>';
a6fd2e2c
DW
877 $selectcol .= '<input type="hidden"
878 name="grademodified_' . $row->userid . '"
879 value="' . $row->timemarked . '"/>';
002864cc
EM
880 $selectcol .= '<input type="hidden"
881 name="gradeattempt_' . $row->userid . '"
882 value="' . $row->attemptnumber . '"/>';
e5403f8c 883 return $selectcol;
bbd0e548
DW
884 }
885
886 /**
e5403f8c 887 * Return a users grades from the listing of all grade data for this assignment.
bbd0e548
DW
888 *
889 * @param int $userid
890 * @return mixed stdClass or false
891 */
892 private function get_gradebook_data_for_user($userid) {
893 if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
894 return $this->gradinginfo->items[0]->grades[$userid];
895 }
896 return false;
897 }
898
df47b77f 899 /**
e5403f8c 900 * Format a column of data for display.
df47b77f
DW
901 *
902 * @param stdClass $row
903 * @return string
904 */
f8d107b3
DM
905 public function col_gradecanbechanged(stdClass $row) {
906 $gradingdisabled = $this->assignment->grading_disabled($row->id);
907 if ($gradingdisabled) {
908 return get_string('no');
909 } else {
910 return get_string('yes');
911 }
912 }
913
914 /**
915 * Format a column of data for display
916 *
917 * @param stdClass $row
918 * @return string
919 */
7a2b911c 920 public function col_grademax(stdClass $row) {
1879a74e
DW
921 if ($this->assignment->get_instance()->grade > 0) {
922 $gradeitem = $this->assignment->get_grade_item();
923 return format_float($this->assignment->get_instance()->grade, $gradeitem->get_decimals());
924 } else {
925 return '';
926 }
df47b77f
DW
927 }
928
bbd0e548 929 /**
e5403f8c 930 * Format a column of data for display.
bbd0e548
DW
931 *
932 * @param stdClass $row
933 * @return string
934 */
e5403f8c 935 public function col_grade(stdClass $row) {
bbd0e548
DW
936 $o = '';
937
938 $link = '';
f8d107b3 939 $separator = $this->output->spacer(array(), true);
bbd0e548 940 $grade = '';
df47b77f 941 $gradingdisabled = $this->assignment->grading_disabled($row->id);
bbd0e548 942
4a47008c 943 if (!$this->is_downloading() && $this->hasgrade) {
e5403f8c 944 $urlparams = array('id' => $this->assignment->get_course_module()->id,
6853cd5e 945 'rownum' => 0,
1b2f9dc6
RW
946 'action' => 'grader');
947
948 if ($this->assignment->is_blind_marking()) {
4ed4be49
AN
949 if (empty($row->recordid)) {
950 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
951 }
952 $urlparams['blindid'] = $row->recordid;
1b2f9dc6
RW
953 } else {
954 $urlparams['userid'] = $row->userid;
955 }
956
e5403f8c 957 $url = new moodle_url('/mod/assign/view.php', $urlparams);
bb690849 958 $link = '<a href="' . $url . '" class="btn btn-primary">' . get_string('grade') . '</a>';
f8d107b3 959 $grade .= $link . $separator;
bbd0e548 960 }
bbd0e548 961
f8d107b3
DM
962 $grade .= $this->display_grade($row->grade,
963 $this->quickgrading && !$gradingdisabled,
964 $row->userid,
965 $row->timemarked);
966
967 return $grade;
bbd0e548
DW
968 }
969
970 /**
e5403f8c 971 * Format a column of data for display.
bbd0e548
DW
972 *
973 * @param stdClass $row
974 * @return string
975 */
e5403f8c 976 public function col_finalgrade(stdClass $row) {
bbd0e548
DW
977 $o = '';
978
979 $grade = $this->get_gradebook_data_for_user($row->userid);
980 if ($grade) {
bf78ebd6 981 $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
bbd0e548
DW
982 }
983
984 return $o;
985 }
986
987 /**
e5403f8c 988 * Format a column of data for display.
bbd0e548
DW
989 *
990 * @param stdClass $row
991 * @return string
992 */
e5403f8c 993 public function col_timemarked(stdClass $row) {
bbd0e548
DW
994 $o = '-';
995
e5403f8c 996 if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
bbd0e548
DW
997 $o = userdate($row->timemarked);
998 }
7688a319
DW
999 if ($row->timemarked && $this->is_downloading()) {
1000 // Force it for downloads as it affects import.
1001 $o = userdate($row->timemarked);
1002 }
bbd0e548
DW
1003
1004 return $o;
1005 }
1006
1007 /**
e5403f8c 1008 * Format a column of data for display.
bbd0e548
DW
1009 *
1010 * @param stdClass $row
1011 * @return string
1012 */
e5403f8c 1013 public function col_timesubmitted(stdClass $row) {
bbd0e548
DW
1014 $o = '-';
1015
79397b56
DB
1016 $group = false;
1017 $submission = false;
1018 $this->get_group_and_submission($row->id, $group, $submission, -1);
e43f1d51 1019 if ($submission && $submission->timemodified && $submission->status != ASSIGN_SUBMISSION_STATUS_NEW) {
79397b56 1020 $o = userdate($submission->timemodified);
232b26f0 1021 } else if ($row->timesubmitted && $row->status != ASSIGN_SUBMISSION_STATUS_NEW) {
bbd0e548
DW
1022 $o = userdate($row->timesubmitted);
1023 }
1024
1025 return $o;
1026 }
1027
1028 /**
1029 * Format a column of data for display
1030 *
1031 * @param stdClass $row
1032 * @return string
1033 */
e5403f8c 1034 public function col_status(stdClass $row) {
bbd0e548
DW
1035 $o = '';
1036
9bf91ea8 1037 $instance = $this->assignment->get_instance($row->userid);
e5403f8c 1038
556483d4
GF
1039 $due = $instance->duedate;
1040 if ($row->extensionduedate) {
1041 $due = $row->extensionduedate;
7dfc65db
EM
1042 } else if (!empty($row->duedate)) {
1043 // The override due date.
1044 $due = $row->duedate;
556483d4
GF
1045 }
1046
79397b56
DB
1047 $group = false;
1048 $submission = false;
4b4280f3
AA
1049
1050 if ($instance->teamsubmission) {
1051 $this->get_group_and_submission($row->id, $group, $submission, -1);
1052 }
e528997a
AH
1053
1054 if ($instance->teamsubmission && !$group && !$instance->preventsubmissionnotingroup) {
1055 $group = true;
1056 }
1057
1058 if ($group && $submission) {
79397b56
DB
1059 $timesubmitted = $submission->timemodified;
1060 $status = $submission->status;
1061 } else {
1062 $timesubmitted = $row->timesubmitted;
1063 $status = $row->status;
1064 }
1065
232b26f0
CS
1066 $displaystatus = $status;
1067 if ($displaystatus == 'new') {
1068 $displaystatus = '';
1069 }
1070
bbd0e548
DW
1071 if ($this->assignment->is_any_submission_plugin_enabled()) {
1072
232b26f0 1073 $o .= $this->output->container(get_string('submissionstatus_' . $displaystatus, 'assign'),
37fc47f1 1074 array('class' => 'submissionstatus' .$displaystatus));
c78e567c 1075 if ($due && $timesubmitted > $due && $status != ASSIGN_SUBMISSION_STATUS_NEW) {
79397b56 1076 $usertime = format_time($timesubmitted - $due);
556483d4
GF
1077 $latemessage = get_string('submittedlateshort',
1078 'assign',
1079 $usertime);
1080 $o .= $this->output->container($latemessage, 'latesubmission');
bbd0e548
DW
1081 }
1082 if ($row->locked) {
e5403f8c
DW
1083 $lockedstr = get_string('submissionslockedshort', 'assign');
1084 $o .= $this->output->container($lockedstr, 'lockedsubmission');
bbd0e548 1085 }
e24c2c1a 1086
24937b2c
GF
1087 // Add status of "grading" if markflow is not enabled.
1088 if (!$instance->markingworkflow) {
1089 if ($row->grade !== null && $row->grade >= 0) {
fcb7d2a9
ZT
1090 if ($row->timemarked < $row->timesubmitted) {
1091 $o .= $this->output->container(get_string('gradedfollowupsubmit', 'assign'), 'gradingreminder');
1092 } else {
1093 $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
1094 }
c78e567c 1095 } else if (!$timesubmitted || $status == ASSIGN_SUBMISSION_STATUS_NEW) {
24937b2c
GF
1096 $now = time();
1097 if ($due && ($now > $due)) {
1098 $overduestr = get_string('overdue', 'assign', format_time($now - $due));
1099 $o .= $this->output->container($overduestr, 'overduesubmission');
1100 }
9e795179
DW
1101 }
1102 }
24937b2c 1103 }
79397b56 1104
24937b2c
GF
1105 if ($instance->markingworkflow) {
1106 $o .= $this->col_workflowstatus($row);
1107 }
1108 if ($row->extensionduedate) {
1109 $userdate = userdate($row->extensionduedate);
1110 $extensionstr = get_string('userextensiondate', 'assign', $userdate);
1111 $o .= $this->output->container($extensionstr, 'extensiondate');
bbd0e548
DW
1112 }
1113
df47b77f 1114 if ($this->is_downloading()) {
b58d0595 1115 $o = strip_tags(rtrim(str_replace('</div>', ' - ', $o), '- '));
df47b77f
DW
1116 }
1117
bbd0e548
DW
1118 return $o;
1119 }
1120
d685b959
IT
1121 /**
1122 * Format a column of data for display.
1123 *
1124 * @param stdClass $row
1125 * @return string
1126 */
1127 public function col_allowsubmissionsfromdate(stdClass $row) {
1128 $o = '';
1129
1130 if ($row->allowsubmissionsfromdate) {
1131 $userdate = userdate($row->allowsubmissionsfromdate);
8be71d39 1132 $o = ($this->is_downloading()) ? $userdate : $this->output->container($userdate, 'allowsubmissionsfromdate');
d685b959
IT
1133 }
1134
1135 return $o;
d685b959
IT
1136 }
1137
1138 /**
1139 * Format a column of data for display.
1140 *
1141 * @param stdClass $row
1142 * @return string
1143 */
1144 public function col_duedate(stdClass $row) {
1145 $o = '';
1146
1147 if ($row->duedate) {
1148 $userdate = userdate($row->duedate);
8be71d39 1149 $o = ($this->is_downloading()) ? $userdate : $this->output->container($userdate, 'duedate');
d685b959
IT
1150 }
1151
1152 return $o;
d685b959
IT
1153 }
1154
1155 /**
1156 * Format a column of data for display.
1157 *
1158 * @param stdClass $row
1159 * @return string
1160 */
1161 public function col_cutoffdate(stdClass $row) {
1162 $o = '';
1163
1164 if ($row->cutoffdate) {
1165 $userdate = userdate($row->cutoffdate);
8be71d39 1166 $o = ($this->is_downloading()) ? $userdate : $this->output->container($userdate, 'cutoffdate');
d685b959
IT
1167 }
1168
1169 return $o;
d685b959
IT
1170 }
1171
bbd0e548 1172 /**
e5403f8c 1173 * Format a column of data for display.
bbd0e548
DW
1174 *
1175 * @param stdClass $row
1176 * @return string
1177 */
e5403f8c 1178 public function col_userid(stdClass $row) {
57fbd5f9
DW
1179 global $USER;
1180
bbd0e548 1181 $edit = '';
bbd0e548
DW
1182
1183 $actions = array();
1184
5e84acc2
CB
1185 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1186 'rownum' => 0,
1187 'action' => 'grader');
1188
1189 if ($this->assignment->is_blind_marking()) {
4ed4be49
AN
1190 if (empty($row->recordid)) {
1191 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
1192 }
1193 $urlparams['blindid'] = $row->recordid;
5e84acc2
CB
1194 } else {
1195 $urlparams['userid'] = $row->userid;
1196 }
e5403f8c 1197 $url = new moodle_url('/mod/assign/view.php', $urlparams);
e9ddbae9 1198 $noimage = null;
e5403f8c 1199
bbd0e548 1200 if (!$row->grade) {
e5403f8c
DW
1201 $description = get_string('grade');
1202 } else {
1203 $description = get_string('updategrade', 'assign');
bbd0e548 1204 }
e9ddbae9
DW
1205 $actions['grade'] = new action_menu_link_secondary(
1206 $url,
1207 $noimage,
1208 $description
1209 );
bbd0e548 1210
c70de965
DW
1211 // Everything we need is in the row.
1212 $submission = $row;
1213 $flags = $row;
1214 if ($this->assignment->get_instance()->teamsubmission) {
1215 // Use the cache for this.
1216 $submission = false;
1217 $group = false;
1218 $this->get_group_and_submission($row->id, $group, $submission, -1);
1219 }
1220
1221 $submissionsopen = $this->assignment->submissions_open($row->id,
1222 true,
1223 $submission,
1224 $flags,
1225 $this->gradinginfo);
57fbd5f9
DW
1226 $caneditsubmission = $this->assignment->can_edit_submission($row->id, $USER->id);
1227
9e795179
DW
1228 // Hide for offline assignments.
1229 if ($this->assignment->is_any_submission_plugin_enabled()) {
1230 if (!$row->status ||
1231 $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
1232 !$this->assignment->get_instance()->submissiondrafts) {
1233
1234 if (!$row->locked) {
e5403f8c 1235 $urlparams = array('id' => $this->assignment->get_course_module()->id,
37fc47f1
DW
1236 'userid' => $row->id,
1237 'action' => 'lock',
1238 'sesskey' => sesskey(),
1239 'page' => $this->currpage);
e5403f8c
DW
1240 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1241
9e795179 1242 $description = get_string('preventsubmissionsshort', 'assign');
e9ddbae9
DW
1243 $actions['lock'] = new action_menu_link_secondary(
1244 $url,
1245 $noimage,
1246 $description
1247 );
9e795179 1248 } else {
e5403f8c 1249 $urlparams = array('id' => $this->assignment->get_course_module()->id,
37fc47f1
DW
1250 'userid' => $row->id,
1251 'action' => 'unlock',
1252 'sesskey' => sesskey(),
1253 'page' => $this->currpage);
e5403f8c 1254 $url = new moodle_url('/mod/assign/view.php', $urlparams);
9e795179 1255 $description = get_string('allowsubmissionsshort', 'assign');
e9ddbae9
DW
1256 $actions['unlock'] = new action_menu_link_secondary(
1257 $url,
1258 $noimage,
1259 $description
1260 );
9e795179
DW
1261 }
1262 }
1263
57fbd5f9
DW
1264 if ($submissionsopen &&
1265 $USER->id != $row->id &&
1266 $caneditsubmission) {
1267 $urlparams = array('id' => $this->assignment->get_course_module()->id,
37fc47f1
DW
1268 'userid' => $row->id,
1269 'action' => 'editsubmission',
1270 'sesskey' => sesskey(),
1271 'page' => $this->currpage);
57fbd5f9
DW
1272 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1273 $description = get_string('editsubmission', 'assign');
e9ddbae9
DW
1274 $actions['editsubmission'] = new action_menu_link_secondary(
1275 $url,
1276 $noimage,
1277 $description
1278 );
57fbd5f9 1279 }
37fc47f1
DW
1280 if ($USER->id != $row->id &&
1281 $caneditsubmission &&
1282 !empty($row->status)) {
1283 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1284 'userid' => $row->id,
1285 'action' => 'removesubmissionconfirm',
1286 'sesskey' => sesskey(),
1287 'page' => $this->currpage);
1288 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1289 $description = get_string('removesubmission', 'assign');
1290 $actions['removesubmission'] = new action_menu_link_secondary(
1291 $url,
1292 $noimage,
1293 $description
1294 );
1295 }
bbd0e548 1296 }
24937b2c
GF
1297 if (($this->assignment->get_instance()->duedate ||
1298 $this->assignment->get_instance()->cutoffdate) &&
1299 $this->hasgrantextension) {
1300 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1301 'userid' => $row->id,
1302 'action' => 'grantextension',
1303 'sesskey' => sesskey(),
1304 'page' => $this->currpage);
1305 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1306 $description = get_string('grantextension', 'assign');
1307 $actions['grantextension'] = new action_menu_link_secondary(
1308 $url,
1309 $noimage,
1310 $description
1311 );
1312 }
e5403f8c
DW
1313 if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
1314 $this->assignment->get_instance()->submissiondrafts) {
1315 $urlparams = array('id' => $this->assignment->get_course_module()->id,
37fc47f1
DW
1316 'userid' => $row->id,
1317 'action' => 'reverttodraft',
1318 'sesskey' => sesskey(),
1319 'page' => $this->currpage);
e5403f8c 1320 $url = new moodle_url('/mod/assign/view.php', $urlparams);
bbd0e548 1321 $description = get_string('reverttodraftshort', 'assign');
e9ddbae9
DW
1322 $actions['reverttodraft'] = new action_menu_link_secondary(
1323 $url,
1324 $noimage,
1325 $description
1326 );
bbd0e548 1327 }
57fbd5f9
DW
1328 if ($row->status == ASSIGN_SUBMISSION_STATUS_DRAFT &&
1329 $this->assignment->get_instance()->submissiondrafts &&
1330 $caneditsubmission &&
1331 $submissionsopen &&
1332 $row->id != $USER->id) {
1333 $urlparams = array('id' => $this->assignment->get_course_module()->id,
37fc47f1
DW
1334 'userid' => $row->id,
1335 'action' => 'submitotherforgrading',
1336 'sesskey' => sesskey(),
1337 'page' => $this->currpage);
57fbd5f9
DW
1338 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1339 $description = get_string('submitforgrading', 'assign');
e9ddbae9
DW
1340 $actions['submitforgrading'] = new action_menu_link_secondary(
1341 $url,
1342 $noimage,
1343 $description
1344 );
57fbd5f9 1345 }
bbd0e548 1346
df211804
DW
1347 $ismanual = $this->assignment->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
1348 $hassubmission = !empty($row->status);
1349 $notreopened = $hassubmission && $row->status != ASSIGN_SUBMISSION_STATUS_REOPENED;
1350 $isunlimited = $this->assignment->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS;
1351 $hasattempts = $isunlimited || $row->attemptnumber < $this->assignment->get_instance()->maxattempts - 1;
1352
1353 if ($ismanual && $hassubmission && $notreopened && $hasattempts) {
1354 $urlparams = array('id' => $this->assignment->get_course_module()->id,
37fc47f1
DW
1355 'userid' => $row->id,
1356 'action' => 'addattempt',
1357 'sesskey' => sesskey(),
1358 'page' => $this->currpage);
df211804
DW
1359 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1360 $description = get_string('addattempt', 'assign');
e9ddbae9
DW
1361 $actions['addattempt'] = new action_menu_link_secondary(
1362 $url,
1363 $noimage,
1364 $description
1365 );
df211804
DW
1366 }
1367
e9ddbae9
DW
1368 $menu = new action_menu();
1369 $menu->set_owner_selector('.gradingtable-actionmenu');
1370 $menu->set_alignment(action_menu::TL, action_menu::BL);
1371 $menu->set_constraint('.gradingtable > .no-overflow');
1372 $menu->set_menu_trigger(get_string('edit'));
1373 foreach ($actions as $action) {
1374 $menu->add($action);
1375 }
bbd0e548 1376
e9ddbae9
DW
1377 // Prioritise the menu ahead of all other actions.
1378 $menu->prioritise = true;
bbd0e548 1379
e9ddbae9 1380 $edit .= $this->output->render($menu);
bbd0e548
DW
1381
1382 return $edit;
1383 }
1384
1385 /**
1386 * Write the plugin summary with an optional link to view the full feedback/submission.
1387 *
1388 * @param assign_plugin $plugin Submission plugin or feedback plugin
1389 * @param stdClass $item Submission or grade
e5403f8c
DW
1390 * @param string $returnaction The return action to pass to the
1391 * view_submission page (the current page)
1392 * @param string $returnparams The return params to pass to the view_submission
1393 * page (the current page)
bbd0e548
DW
1394 * @return string The summary with an optional link
1395 */
e5403f8c
DW
1396 private function format_plugin_summary_with_link(assign_plugin $plugin,
1397 stdClass $item,
1398 $returnaction,
1399 $returnparams) {
bbd0e548
DW
1400 $link = '';
1401 $showviewlink = false;
2c6a3dbf 1402
bbd0e548
DW
1403 $summary = $plugin->view_summary($item, $showviewlink);
1404 $separator = '';
1405 if ($showviewlink) {
e5403f8c
DW
1406 $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
1407 $icon = $this->output->pix_icon('t/preview', $viewstr);
1408 $urlparams = array('id' => $this->assignment->get_course_module()->id,
37fc47f1
DW
1409 'sid' => $item->id,
1410 'gid' => $item->id,
1411 'plugin' => $plugin->get_type(),
1412 'action' => 'viewplugin' . $plugin->get_subtype(),
1413 'returnaction' => $returnaction,
1414 'returnparams' => http_build_query($returnparams));
e5403f8c
DW
1415 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1416 $link = $this->output->action_link($url, $icon);
bbd0e548
DW
1417 $separator = $this->output->spacer(array(), true);
1418 }
1419
1420 return $link . $separator . $summary;
1421 }
1422
1423
1424 /**
e5403f8c 1425 * Format the submission and feedback columns.
bbd0e548
DW
1426 *
1427 * @param string $colname The column name
1428 * @param stdClass $row The submission row
1429 * @return mixed string or NULL
1430 */
e5403f8c 1431 public function other_cols($colname, $row) {
d08e6c31
DW
1432 // For extra user fields the result is already in $row.
1433 if (empty($this->plugincache[$colname])) {
1434 return $row->$colname;
1435 }
1436
1437 // This must be a plugin field.
df47b77f 1438 $plugincache = $this->plugincache[$colname];
2c6a3dbf 1439
df47b77f
DW
1440 $plugin = $plugincache[0];
1441
1442 $field = null;
1443 if (isset($plugincache[1])) {
1444 $field = $plugincache[1];
1445 }
1446
1447 if ($plugin->is_visible() && $plugin->is_enabled()) {
1448 if ($plugin->get_subtype() == 'assignsubmission') {
cd01491c
DW
1449 if ($this->assignment->get_instance()->teamsubmission) {
1450 $group = false;
1451 $submission = false;
5ef72f85
DW
1452
1453 $this->get_group_and_submission($row->id, $group, $submission, -1);
cd01491c 1454 if ($submission) {
5ef72f85
DW
1455 if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1456 // For a newly reopened submission - we want to show the previous submission in the table.
1457 $this->get_group_and_submission($row->id, $group, $submission, $submission->attemptnumber-1);
1458 }
df47b77f
DW
1459 if (isset($field)) {
1460 return $plugin->get_editor_text($field, $submission->id);
1461 }
e5403f8c
DW
1462 return $this->format_plugin_summary_with_link($plugin,
1463 $submission,
1464 'grading',
1465 array());
cd01491c
DW
1466 }
1467 } else if ($row->submissionid) {
5ef72f85
DW
1468 if ($row->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1469 // For a newly reopened submission - we want to show the previous submission in the table.
1470 $submission = $this->assignment->get_user_submission($row->userid, false, $row->attemptnumber - 1);
1471 } else {
1472 $submission = new stdClass();
1473 $submission->id = $row->submissionid;
1474 $submission->timecreated = $row->firstsubmission;
1475 $submission->timemodified = $row->timesubmitted;
1476 $submission->assignment = $this->assignment->get_instance()->id;
1477 $submission->userid = $row->userid;
5c386472 1478 $submission->attemptnumber = $row->attemptnumber;
5ef72f85
DW
1479 }
1480 // Field is used for only for import/export and refers the the fieldname for the text editor.
df47b77f 1481 if (isset($field)) {
5ef72f85 1482 return $plugin->get_editor_text($field, $submission->id);
df47b77f 1483 }
e5403f8c
DW
1484 return $this->format_plugin_summary_with_link($plugin,
1485 $submission,
1486 'grading',
1487 array());
bbd0e548 1488 }
df47b77f 1489 } else {
2c6a3dbf 1490 $grade = null;
df47b77f
DW
1491 if (isset($field)) {
1492 return $plugin->get_editor_text($field, $row->gradeid);
1493 }
1494
bbd0e548
DW
1495 if ($row->gradeid) {
1496 $grade = new stdClass();
1497 $grade->id = $row->gradeid;
1498 $grade->timecreated = $row->firstmarked;
1499 $grade->timemodified = $row->timemarked;
1500 $grade->assignment = $this->assignment->get_instance()->id;
1501 $grade->userid = $row->userid;
1502 $grade->grade = $row->grade;
1503 $grade->mailed = $row->mailed;
5c386472 1504 $grade->attemptnumber = $row->attemptnumber;
2c6a3dbf
DW
1505 }
1506 if ($this->quickgrading && $plugin->supports_quickgrading()) {
1507 return $plugin->get_quickgrading_html($row->userid, $grade);
1508 } else if ($grade) {
e5403f8c
DW
1509 return $this->format_plugin_summary_with_link($plugin,
1510 $grade,
1511 'grading',
1512 array());
bbd0e548
DW
1513 }
1514 }
bbd0e548 1515 }
df47b77f 1516 return '';
bbd0e548
DW
1517 }
1518
1519 /**
e5403f8c 1520 * Using the current filtering and sorting - load all rows and return a single column from them.
bbd0e548
DW
1521 *
1522 * @param string $columnname The name of the raw column data
1523 * @return array of data
1524 */
e5403f8c 1525 public function get_column_data($columnname) {
bbd0e548
DW
1526 $this->setup();
1527 $this->currpage = 0;
1528 $this->query_db($this->tablemaxrows);
1529 $result = array();
1530 foreach ($this->rawdata as $row) {
1531 $result[] = $row->$columnname;
1532 }
1533 return $result;
1534 }
bbd0e548
DW
1535
1536 /**
e5403f8c 1537 * Return things to the renderer.
bbd0e548
DW
1538 *
1539 * @return string the assignment name
1540 */
e5403f8c 1541 public function get_assignment_name() {
bbd0e548
DW
1542 return $this->assignment->get_instance()->name;
1543 }
1544
1545 /**
e5403f8c 1546 * Return things to the renderer.
bbd0e548
DW
1547 *
1548 * @return int the course module id
1549 */
e5403f8c 1550 public function get_course_module_id() {
bbd0e548
DW
1551 return $this->assignment->get_course_module()->id;
1552 }
1553
1554 /**
e5403f8c 1555 * Return things to the renderer.
bbd0e548
DW
1556 *
1557 * @return int the course id
1558 */
e5403f8c 1559 public function get_course_id() {
bbd0e548
DW
1560 return $this->assignment->get_course()->id;
1561 }
1562
1563 /**
e5403f8c 1564 * Return things to the renderer.
bbd0e548
DW
1565 *
1566 * @return stdClass The course context
1567 */
e5403f8c 1568 public function get_course_context() {
bbd0e548
DW
1569 return $this->assignment->get_course_context();
1570 }
1571
1572 /**
e5403f8c 1573 * Return things to the renderer.
bbd0e548
DW
1574 *
1575 * @return bool Does this assignment accept submissions
1576 */
e5403f8c 1577 public function submissions_enabled() {
bbd0e548
DW
1578 return $this->assignment->is_any_submission_plugin_enabled();
1579 }
1580
1581 /**
e5403f8c 1582 * Return things to the renderer.
bbd0e548
DW
1583 *
1584 * @return bool Can this user view all grades (the gradebook)
1585 */
e5403f8c
DW
1586 public function can_view_all_grades() {
1587 $context = $this->assignment->get_course_context();
1588 return has_capability('gradereport/grader:view', $context) &&
1589 has_capability('moodle/grade:viewall', $context);
bbd0e548
DW
1590 }
1591
85d0696d
DW
1592 /**
1593 * Always return a valid sort - even if the userid column is missing.
1594 * @return array column name => SORT_... constant.
1595 */
1596 public function get_sort_columns() {
1597 $result = parent::get_sort_columns();
eb4c4661
AN
1598
1599 $assignment = $this->assignment->get_instance();
1600 if (empty($assignment->blindmarking)) {
1601 $result = array_merge($result, array('userid' => SORT_ASC));
1602 } else {
1603 $result = array_merge($result, [
1604 'COALESCE(s.timecreated, ' . time() . ')' => SORT_ASC,
1605 'COALESCE(s.id, ' . PHP_INT_MAX . ')' => SORT_ASC,
1606 'um.id' => SORT_ASC,
1607 ]);
1608 }
85d0696d
DW
1609 return $result;
1610 }
1611
bbd0e548 1612 /**
e5403f8c 1613 * Override the table show_hide_link to not show for select column.
bbd0e548
DW
1614 *
1615 * @param string $column the column name, index into various names.
1616 * @param int $index numerical index of the column.
1617 * @return string HTML fragment.
1618 */
1619 protected function show_hide_link($column, $index) {
4a47008c 1620 if ($index > 0 || !$this->hasgrade) {
bbd0e548
DW
1621 return parent::show_hide_link($column, $index);
1622 }
1623 return '';
1624 }
3fca6937
NM
1625
1626 /**
1627 * Overides setup to ensure it will only run a single time.
1628 */
1629 public function setup() {
1630 // Check if the setup function has been called before, we should not run it twice.
1631 // If we do the sortorder of the table will be broken.
1632 if (!empty($this->setup)) {
1633 return;
1634 }
1635 parent::setup();
1636 }
bbd0e548 1637}