MDL-58697: Only get group submission if enabled
[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
e5403f8c
DW
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);
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
158 AND s.latest = 1
159 LEFT JOIN {assign_grades} g
160 ON u.id = g.userid
161 AND g.assignment = :assignmentid2 ';
162
163 // For group submissions we don't immediately create an entry in the assign_submission table for each user,
164 // instead the userid is set to 0. In this case we use a different query to retrieve the grade for the user.
165 if ($this->assignment->get_instance()->teamsubmission) {
166 $params['assignmentid4'] = (int) $this->assignment->get_instance()->id;
167 $grademaxattempt = 'SELECT mxg.userid, MAX(mxg.attemptnumber) AS maxattempt
168 FROM {assign_grades} mxg
169 WHERE mxg.assignment = :assignmentid4
170 GROUP BY mxg.userid';
171 $from .= 'LEFT JOIN (' . $grademaxattempt . ') gmx
172 ON u.id = gmx.userid
173 AND g.attemptnumber = gmx.maxattempt ';
174 } else {
175 $from .= 'AND g.attemptnumber = s.attemptnumber ';
176 }
177
178 $from .= 'LEFT JOIN {assign_user_flags} uf
179 ON u.id = uf.userid
4ed4be49
AN
180 AND uf.assignment = :assignmentid3 ';
181
a13f1f49
KH
182 $hasoverrides = $this->assignment->has_overrides();
183
184 if ($hasoverrides) {
28cdba9d
EM
185 $params['assignmentid5'] = (int)$this->assignment->get_instance()->id;
186 $params['assignmentid6'] = (int)$this->assignment->get_instance()->id;
187 $params['assignmentid7'] = (int)$this->assignment->get_instance()->id;
188 $params['assignmentid8'] = (int)$this->assignment->get_instance()->id;
189 $params['assignmentid9'] = (int)$this->assignment->get_instance()->id;
190
a13f1f49
KH
191 $fields .= ', priority.priority, ';
192 $fields .= 'effective.allowsubmissionsfromdate, ';
193 $fields .= 'effective.duedate, ';
194 $fields .= 'effective.cutoffdate ';
195
196 $from .= ' LEFT JOIN (
197 SELECT merged.userid, min(merged.priority) priority FROM (
198 ( SELECT u.id as userid, 9999999 AS priority
199 FROM {user} u
200 )
201 UNION
202 ( SELECT uo.userid, 0 AS priority
203 FROM {assign_overrides} uo
204 WHERE uo.assignid = :assignmentid5
205 )
206 UNION
207 ( SELECT gm.userid, go.sortorder AS priority
208 FROM {assign_overrides} go
209 JOIN {groups} g ON g.id = go.groupid
210 JOIN {groups_members} gm ON gm.groupid = g.id
211 WHERE go.assignid = :assignmentid6
212 )
b5c24c99 213 ) merged
a13f1f49
KH
214 GROUP BY merged.userid
215 ) priority ON priority.userid = u.id
216
217 JOIN (
218 (SELECT 9999999 AS priority,
219 u.id AS userid,
a13f1f49
KH
220 a.allowsubmissionsfromdate,
221 a.duedate,
222 a.cutoffdate
223 FROM {user} u
224 JOIN {assign} a ON a.id = :assignmentid7
d685b959
IT
225 )
226 UNION
a13f1f49
KH
227 (SELECT 0 AS priority,
228 uo.userid,
a13f1f49
KH
229 uo.allowsubmissionsfromdate,
230 uo.duedate,
231 uo.cutoffdate
232 FROM {assign_overrides} uo
233 WHERE uo.assignid = :assignmentid8
d685b959
IT
234 )
235 UNION
a13f1f49
KH
236 (SELECT go.sortorder AS priority,
237 gm.userid,
a13f1f49
KH
238 go.allowsubmissionsfromdate,
239 go.duedate,
240 go.cutoffdate
241 FROM {assign_overrides} go
242 JOIN {groups} g ON g.id = go.groupid
243 JOIN {groups_members} gm ON gm.groupid = g.id
244 WHERE go.assignid = :assignmentid9
d685b959 245 )
a13f1f49 246
b5c24c99 247 ) effective ON effective.priority = priority.priority AND effective.userid = priority.userid ';
a13f1f49 248 }
d685b959 249
4ed4be49
AN
250 if (!empty($this->assignment->get_instance()->blindmarking)) {
251 $from .= 'LEFT JOIN {assign_user_mapping} um
252 ON u.id = um.userid
28cdba9d
EM
253 AND um.assignment = :assignmentidblind ';
254 $params['assignmentidblind'] = (int)$this->assignment->get_instance()->id;
4ed4be49
AN
255 $fields .= ', um.id as recordid ';
256 }
bbd0e548
DW
257
258 $userparams = array();
a3dbdc2c
DW
259 $userindex = 0;
260
261 list($userwhere, $userparams) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
262 $where = 'u.id ' . $userwhere;
263 $params = array_merge($params, $userparams);
9682626e 264
93d1de6d
DW
265 // The filters do not make sense when there are no submissions, so do not apply them.
266 if ($this->assignment->is_any_submission_plugin_enabled()) {
267 if ($filter == ASSIGN_FILTER_SUBMITTED) {
77755451
DW
268 $where .= ' AND (s.timemodified IS NOT NULL AND
269 s.status = :submitted) ';
270 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
271
c80d59f2 272 } else if ($filter == ASSIGN_FILTER_NOT_SUBMITTED) {
79ea89fe 273 $where .= ' AND (s.timemodified IS NULL OR s.status <> :submitted) ';
8f165230 274 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
77755451 275 } else if ($filter == ASSIGN_FILTER_REQUIRE_GRADING) {
93d1de6d
DW
276 $where .= ' AND (s.timemodified IS NOT NULL AND
277 s.status = :submitted AND
c28c7645
BA
278 (s.timemodified >= g.timemodified OR g.timemodified IS NULL OR g.grade IS NULL';
279
280 if ($this->assignment->get_grade_item()->gradetype == GRADE_TYPE_SCALE) {
281 // Scale grades are set to -1 when not graded.
282 $where .= ' OR g.grade = -1';
283 }
284
285 $where .= '))';
93d1de6d 286 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
77755451 287
696bd4d6
TB
288 } else if ($filter == ASSIGN_FILTER_GRANTED_EXTENSION) {
289 $where .= ' AND uf.extensionduedate > 0 ';
290
77755451 291 } else if (strpos($filter, ASSIGN_FILTER_SINGLE_USER) === 0) {
93d1de6d
DW
292 $userfilter = (int) array_pop(explode('=', $filter));
293 $where .= ' AND (u.id = :userid)';
294 $params['userid'] = $userfilter;
295 }
bbd0e548 296 }
df211804 297
5a96486b
DM
298 if ($this->assignment->get_instance()->markingworkflow &&
299 $this->assignment->get_instance()->markingallocation) {
f8d107b3 300 if (has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
13e82f1c 301 // Check to see if marker filter is set.
f8d107b3
DM
302 $markerfilter = (int)get_user_preferences('assign_markerfilter', '');
303 if (!empty($markerfilter)) {
c80d59f2
JF
304 if ($markerfilter == ASSIGN_MARKER_FILTER_NO_MARKER) {
305 $where .= ' AND (uf.allocatedmarker IS NULL OR uf.allocatedmarker = 0)';
dd7de4ea
DW
306 } else {
307 $where .= ' AND uf.allocatedmarker = :markerid';
308 $params['markerid'] = $markerfilter;
c80d59f2 309 }
f8d107b3 310 }
f8d107b3
DM
311 }
312 }
313
314 if ($this->assignment->get_instance()->markingworkflow) {
315 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
316 if (!empty($workflowstates)) {
317 $workflowfilter = get_user_preferences('assign_workflowfilter', '');
13e82f1c
DW
318 if ($workflowfilter == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED) {
319 $where .= ' AND (uf.workflowstate = :workflowstate OR uf.workflowstate IS NULL OR '.
320 $DB->sql_isempty('assign_user_flags', 'workflowstate', true, true).')';
321 $params['workflowstate'] = $workflowfilter;
322 } else if (array_key_exists($workflowfilter, $workflowstates)) {
323 $where .= ' AND uf.workflowstate = :workflowstate';
324 $params['workflowstate'] = $workflowfilter;
f8d107b3
DM
325 }
326 }
327 }
328
bbd0e548
DW
329 $this->set_sql($fields, $from, $where, $params);
330
df47b77f
DW
331 if ($downloadfilename) {
332 $this->is_downloading('csv', $downloadfilename);
333 }
334
bbd0e548
DW
335 $columns = array();
336 $headers = array();
337
df47b77f 338 // Select.
4a47008c 339 if (!$this->is_downloading() && $this->hasgrade) {
df47b77f
DW
340 $columns[] = 'select';
341 $headers[] = get_string('select') .
7400be1b
RT
342 '<div class="selectall"><label class="accesshide" for="selectall">' . get_string('selectall') . '</label>
343 <input type="checkbox" id="selectall" name="selectall" title="' . get_string('selectall') . '"/></div>';
bbd0e548
DW
344 }
345
b473171a 346 // User picture.
323f221b 347 if ($this->hasviewblind || !$this->assignment->is_blind_marking()) {
df47b77f
DW
348 if (!$this->is_downloading()) {
349 $columns[] = 'picture';
350 $headers[] = get_string('pictureofuser');
351 } else {
352 $columns[] = 'recordid';
353 $headers[] = get_string('recordid', 'assign');
354 }
b473171a
DW
355
356 // Fullname.
357 $columns[] = 'fullname';
358 $headers[] = get_string('fullname');
d08e6c31 359
59e95487 360 // Participant # details if can view real identities.
18b79734
MH
361 if ($this->assignment->is_blind_marking()) {
362 if (!$this->is_downloading()) {
363 $columns[] = 'recordid';
364 $headers[] = get_string('recordid', 'assign');
365 }
366 }
367
d08e6c31
DW
368 foreach ($extrauserfields as $extrafield) {
369 $columns[] = $extrafield;
370 $headers[] = get_user_field_name($extrafield);
371 }
b473171a
DW
372 } else {
373 // Record ID.
374 $columns[] = 'recordid';
375 $headers[] = get_string('recordid', 'assign');
376 }
bbd0e548 377
e5403f8c 378 // Submission status.
24937b2c
GF
379 $columns[] = 'status';
380 $headers[] = get_string('status', 'assign');
bbd0e548 381
a13f1f49
KH
382 if ($hasoverrides) {
383 // Allowsubmissionsfromdate.
384 $columns[] = 'allowsubmissionsfromdate';
385 $headers[] = get_string('allowsubmissionsfromdate', 'assign');
d685b959 386
a13f1f49
KH
387 // Duedate.
388 $columns[] = 'duedate';
389 $headers[] = get_string('duedate', 'assign');
d685b959 390
a13f1f49
KH
391 // Cutoffdate.
392 $columns[] = 'cutoffdate';
393 $headers[] = get_string('cutoffdate', 'assign');
394 }
d685b959 395
e5403f8c 396 // Team submission columns.
12a1a0da
DW
397 if ($assignment->get_instance()->teamsubmission) {
398 $columns[] = 'team';
399 $headers[] = get_string('submissionteam', 'assign');
12a1a0da 400 }
13e82f1c 401 // Allocated marker.
5a96486b
DM
402 if ($this->assignment->get_instance()->markingworkflow &&
403 $this->assignment->get_instance()->markingallocation &&
f8d107b3
DM
404 has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
405 // Add a column for the allocated marker.
406 $columns[] = 'allocatedmarker';
407 $headers[] = get_string('marker', 'assign');
408 }
e5403f8c 409 // Grade.
bbd0e548
DW
410 $columns[] = 'grade';
411 $headers[] = get_string('grade');
df47b77f
DW
412 if ($this->is_downloading()) {
413 if ($this->assignment->get_instance()->grade >= 0) {
414 $columns[] = 'grademax';
415 $headers[] = get_string('maxgrade', 'assign');
416 } else {
417 // This is a custom scale.
418 $columns[] = 'scale';
419 $headers[] = get_string('scale', 'assign');
420 }
f8d107b3
DM
421
422 if ($this->assignment->get_instance()->markingworkflow) {
423 // Add a column for the marking workflow state.
424 $columns[] = 'workflowstate';
425 $headers[] = get_string('markingworkflowstate', 'assign');
426 }
427 // Add a column for the list of valid marking workflow states.
428 $columns[] = 'gradecanbechanged';
429 $headers[] = get_string('gradecanbechanged', 'assign');
df47b77f 430 }
4a47008c 431 if (!$this->is_downloading() && $this->hasgrade) {
2bda0460
NK
432 // We have to call this column userid so we can use userid as a default sortable column.
433 $columns[] = 'userid';
434 $headers[] = get_string('edit');
435 }
bbd0e548 436
e5403f8c 437 // Submission plugins.
bbd0e548
DW
438 if ($assignment->is_any_submission_plugin_enabled()) {
439 $columns[] = 'timesubmitted';
440 $headers[] = get_string('lastmodifiedsubmission', 'assign');
441
442 foreach ($this->assignment->get_submission_plugins() as $plugin) {
df47b77f
DW
443 if ($this->is_downloading()) {
444 if ($plugin->is_visible() && $plugin->is_enabled()) {
445 foreach ($plugin->get_editor_fields() as $field => $description) {
446 $index = 'plugin' . count($this->plugincache);
447 $this->plugincache[$index] = array($plugin, $field);
448 $columns[] = $index;
449 $headers[] = $plugin->get_name();
450 }
451 }
452 } else {
453 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
454 $index = 'plugin' . count($this->plugincache);
455 $this->plugincache[$index] = array($plugin);
456 $columns[] = $index;
457 $headers[] = $plugin->get_name();
458 }
bbd0e548
DW
459 }
460 }
461 }
462
e5403f8c 463 // Time marked.
bbd0e548
DW
464 $columns[] = 'timemarked';
465 $headers[] = get_string('lastmodifiedgrade', 'assign');
466
e5403f8c 467 // Feedback plugins.
bbd0e548 468 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
df47b77f
DW
469 if ($this->is_downloading()) {
470 if ($plugin->is_visible() && $plugin->is_enabled()) {
471 foreach ($plugin->get_editor_fields() as $field => $description) {
472 $index = 'plugin' . count($this->plugincache);
473 $this->plugincache[$index] = array($plugin, $field);
474 $columns[] = $index;
475 $headers[] = $description;
476 }
477 }
478 } else if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
479 $index = 'plugin' . count($this->plugincache);
480 $this->plugincache[$index] = array($plugin);
481 $columns[] = $index;
bbd0e548
DW
482 $headers[] = $plugin->get_name();
483 }
484 }
485
9f484b98
RW
486 // Exclude 'Final grade' column in downloaded grading worksheets.
487 if (!$this->is_downloading()) {
488 // Final grade.
489 $columns[] = 'finalgrade';
490 $headers[] = get_string('finalgrade', 'grades');
491 }
bbd0e548 492
e5403f8c
DW
493 // Load the grading info for all users.
494 $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id,
495 'mod',
496 'assign',
497 $this->assignment->get_instance()->id,
498 $users);
a769a001
DW
499
500 if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
501 $columns[] = 'outcomes';
502 $headers[] = get_string('outcomes', 'grades');
503 }
bbd0e548 504
e5403f8c 505 // Set the columns.
bbd0e548
DW
506 $this->define_columns($columns);
507 $this->define_headers($headers);
d08e6c31
DW
508 foreach ($extrauserfields as $extrafield) {
509 $this->column_class($extrafield, $extrafield);
510 }
f9e62127 511 $this->no_sorting('recordid');
bbd0e548 512 $this->no_sorting('finalgrade');
f9e62127 513 $this->no_sorting('userid');
bbd0e548 514 $this->no_sorting('select');
a769a001
DW
515 $this->no_sorting('outcomes');
516
12a1a0da
DW
517 if ($assignment->get_instance()->teamsubmission) {
518 $this->no_sorting('team');
12a1a0da
DW
519 }
520
f9e62127 521 $plugincolumnindex = 0;
bbd0e548 522 foreach ($this->assignment->get_submission_plugins() as $plugin) {
df47b77f 523 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
f9e62127
DW
524 $submissionpluginindex = 'plugin' . $plugincolumnindex++;
525 $this->no_sorting($submissionpluginindex);
bbd0e548
DW
526 }
527 }
528 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
df47b77f 529 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
f9e62127
DW
530 $feedbackpluginindex = 'plugin' . $plugincolumnindex++;
531 $this->no_sorting($feedbackpluginindex);
bbd0e548
DW
532 }
533 }
534
5fc59630
RW
535 // When there is no data we still want the column headers printed in the csv file.
536 if ($this->is_downloading()) {
537 $this->start_output();
538 }
bbd0e548
DW
539 }
540
61cf3253 541 /**
e5403f8c 542 * Before adding each row to the table make sure rownum is incremented.
61cf3253
DW
543 *
544 * @param array $row row of data from db used to make one row of the table.
545 * @return array one row for the table
546 */
e5403f8c 547 public function format_row($row) {
61cf3253
DW
548 if ($this->rownum < 0) {
549 $this->rownum = $this->currpage * $this->pagesize;
550 } else {
551 $this->rownum += 1;
552 }
553
554 return parent::format_row($row);
555 }
556
b473171a 557 /**
e5403f8c 558 * Add a column with an ID that uniquely identifies this user in this assignment.
b473171a 559 *
1561a37c 560 * @param stdClass $row
b473171a
DW
561 * @return string
562 */
e5403f8c 563 public function col_recordid(stdClass $row) {
4ed4be49
AN
564 if (empty($row->recordid)) {
565 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
566 }
567 return get_string('hiddenuser', 'assign') . $row->recordid;
e5403f8c 568 }
b473171a
DW
569
570
bbd0e548 571 /**
e5403f8c 572 * Add the userid to the row class so it can be updated via ajax.
bbd0e548
DW
573 *
574 * @param stdClass $row The row of data
575 * @return string The row class
576 */
e5403f8c 577 public function get_row_class($row) {
bbd0e548
DW
578 return 'user' . $row->userid;
579 }
580
581 /**
e5403f8c 582 * Return the number of rows to display on a single page.
bbd0e548
DW
583 *
584 * @return int The number of rows per page
585 */
e5403f8c 586 public function get_rows_per_page() {
bbd0e548
DW
587 return $this->perpage;
588 }
589
f8d107b3
DM
590 /**
591 * list current marking workflow state
592 *
593 * @param stdClass $row
594 * @return string
595 */
596 public function col_workflowstatus(stdClass $row) {
597 $o = '';
598
599 $gradingdisabled = $this->assignment->grading_disabled($row->id);
e24c2c1a
AA
600 // The function in the assignment keeps a static cache of this list of states.
601 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
602 $workflowstate = $row->workflowstate;
603 if (empty($workflowstate)) {
604 $workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
605 }
606 if ($this->quickgrading && !$gradingdisabled) {
607 $notmarked = get_string('markingworkflowstatenotmarked', 'assign');
608 $name = 'quickgrade_' . $row->id . '_workflowstate';
609 $o .= html_writer::select($workflowstates, $name, $workflowstate, array('' => $notmarked));
610 // Check if this user is a marker that can't manage allocations and doesn't have the marker column added.
5a96486b
DM
611 if ($this->assignment->get_instance()->markingworkflow &&
612 $this->assignment->get_instance()->markingallocation &&
e24c2c1a
AA
613 !has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
614
615 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
616 $o .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $name,
617 'value' => $row->allocatedmarker));
f8d107b3 618 }
e24c2c1a
AA
619 } else {
620 $o .= $this->output->container(get_string('markingworkflowstate' . $workflowstate, 'assign'), $workflowstate);
f8d107b3
DM
621 }
622 return $o;
623 }
624
625 /**
626 * For download only - list current marking workflow state
627 *
628 * @param stdClass $row - The row of data
629 * @return string The current marking workflow state
630 */
631 public function col_workflowstate($row) {
632 $state = $row->workflowstate;
633 if (empty($state)) {
634 $state = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
635 }
636
637 return get_string('markingworkflowstate' . $state, 'assign');
638 }
639
640 /**
641 * list current marker
642 *
643 * @param stdClass $row - The row of data
644 * @return id the user->id of the marker.
645 */
13e82f1c 646 public function col_allocatedmarker(stdClass $row) {
f8d107b3
DM
647 static $markers = null;
648 static $markerlist = array();
649 if ($markers === null) {
84e3c97b 650 list($sort, $params) = users_order_by_sql('u');
3ffd1382
AM
651 // Only enrolled users could be assigned as potential markers.
652 $markers = get_enrolled_users($this->assignment->get_context(), 'mod/assign:grade', 0, 'u.*', $sort);
f8d107b3 653 $markerlist[0] = get_string('choosemarker', 'assign');
05fdbe37 654 $viewfullnames = has_capability('moodle/site:viewfullnames', $this->assignment->get_context());
f8d107b3 655 foreach ($markers as $marker) {
05fdbe37 656 $markerlist[$marker->id] = fullname($marker, $viewfullnames);
f8d107b3
DM
657 }
658 }
659 if (empty($markerlist)) {
660 // TODO: add some form of notification here that no markers are available.
661 return '';
662 }
663 if ($this->is_downloading()) {
60a601db 664 if (isset($markers[$row->allocatedmarker])) {
05fdbe37
SR
665 return fullname($markers[$row->allocatedmarker],
666 has_capability('moodle/site:viewfullnames', $this->assignment->get_context()));
60a601db
JF
667 } else {
668 return '';
669 }
f8d107b3
DM
670 }
671
672 if ($this->quickgrading && has_capability('mod/assign:manageallocations', $this->assignment->get_context()) &&
673 (empty($row->workflowstate) ||
674 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_INMARKING ||
675 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED)) {
676
677 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
678 return html_writer::select($markerlist, $name, $row->allocatedmarker, false);
679 } else if (!empty($row->allocatedmarker)) {
680 $output = '';
681 if ($this->quickgrading) { // Add hidden field for quickgrading page.
682 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
683 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$row->allocatedmarker));
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)) {
e5403f8c
DW
699 $dbparams = array('id'=>-($this->assignment->get_instance()->grade));
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;
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) {
23f2f644
TB
921 $gradeitem = $this->assignment->get_grade_item();
922 return format_float($this->assignment->get_instance()->grade, $gradeitem->get_decimals());
df47b77f
DW
923 }
924
bbd0e548 925 /**
e5403f8c 926 * Format a column of data for display.
bbd0e548
DW
927 *
928 * @param stdClass $row
929 * @return string
930 */
e5403f8c 931 public function col_grade(stdClass $row) {
bbd0e548
DW
932 $o = '';
933
934 $link = '';
f8d107b3 935 $separator = $this->output->spacer(array(), true);
bbd0e548 936 $grade = '';
df47b77f 937 $gradingdisabled = $this->assignment->grading_disabled($row->id);
bbd0e548 938
4a47008c 939 if (!$this->is_downloading() && $this->hasgrade) {
e5403f8c 940 $urlparams = array('id' => $this->assignment->get_course_module()->id,
6853cd5e 941 'rownum' => 0,
1b2f9dc6
RW
942 'action' => 'grader');
943
944 if ($this->assignment->is_blind_marking()) {
4ed4be49
AN
945 if (empty($row->recordid)) {
946 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
947 }
948 $urlparams['blindid'] = $row->recordid;
1b2f9dc6
RW
949 } else {
950 $urlparams['userid'] = $row->userid;
951 }
952
e5403f8c 953 $url = new moodle_url('/mod/assign/view.php', $urlparams);
bb690849 954 $link = '<a href="' . $url . '" class="btn btn-primary">' . get_string('grade') . '</a>';
f8d107b3 955 $grade .= $link . $separator;
bbd0e548 956 }
bbd0e548 957
f8d107b3
DM
958 $grade .= $this->display_grade($row->grade,
959 $this->quickgrading && !$gradingdisabled,
960 $row->userid,
961 $row->timemarked);
962
963 return $grade;
bbd0e548
DW
964 }
965
966 /**
e5403f8c 967 * Format a column of data for display.
bbd0e548
DW
968 *
969 * @param stdClass $row
970 * @return string
971 */
e5403f8c 972 public function col_finalgrade(stdClass $row) {
bbd0e548
DW
973 $o = '';
974
975 $grade = $this->get_gradebook_data_for_user($row->userid);
976 if ($grade) {
bf78ebd6 977 $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
bbd0e548
DW
978 }
979
980 return $o;
981 }
982
983 /**
e5403f8c 984 * Format a column of data for display.
bbd0e548
DW
985 *
986 * @param stdClass $row
987 * @return string
988 */
e5403f8c 989 public function col_timemarked(stdClass $row) {
bbd0e548
DW
990 $o = '-';
991
e5403f8c 992 if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
bbd0e548
DW
993 $o = userdate($row->timemarked);
994 }
7688a319
DW
995 if ($row->timemarked && $this->is_downloading()) {
996 // Force it for downloads as it affects import.
997 $o = userdate($row->timemarked);
998 }
bbd0e548
DW
999
1000 return $o;
1001 }
1002
1003 /**
e5403f8c 1004 * Format a column of data for display.
bbd0e548
DW
1005 *
1006 * @param stdClass $row
1007 * @return string
1008 */
e5403f8c 1009 public function col_timesubmitted(stdClass $row) {
bbd0e548
DW
1010 $o = '-';
1011
79397b56
DB
1012 $group = false;
1013 $submission = false;
1014 $this->get_group_and_submission($row->id, $group, $submission, -1);
e43f1d51 1015 if ($submission && $submission->timemodified && $submission->status != ASSIGN_SUBMISSION_STATUS_NEW) {
79397b56 1016 $o = userdate($submission->timemodified);
232b26f0 1017 } else if ($row->timesubmitted && $row->status != ASSIGN_SUBMISSION_STATUS_NEW) {
bbd0e548
DW
1018 $o = userdate($row->timesubmitted);
1019 }
1020
1021 return $o;
1022 }
1023
1024 /**
1025 * Format a column of data for display
1026 *
1027 * @param stdClass $row
1028 * @return string
1029 */
e5403f8c 1030 public function col_status(stdClass $row) {
bbd0e548
DW
1031 $o = '';
1032
e5403f8c
DW
1033 $instance = $this->assignment->get_instance();
1034
556483d4
GF
1035 $due = $instance->duedate;
1036 if ($row->extensionduedate) {
1037 $due = $row->extensionduedate;
7dfc65db
EM
1038 } else if (!empty($row->duedate)) {
1039 // The override due date.
1040 $due = $row->duedate;
556483d4
GF
1041 }
1042
79397b56
DB
1043 $group = false;
1044 $submission = false;
4b4280f3
AA
1045
1046 if ($instance->teamsubmission) {
1047 $this->get_group_and_submission($row->id, $group, $submission, -1);
1048 }
e528997a
AH
1049
1050 if ($instance->teamsubmission && !$group && !$instance->preventsubmissionnotingroup) {
1051 $group = true;
1052 }
1053
1054 if ($group && $submission) {
79397b56
DB
1055 $timesubmitted = $submission->timemodified;
1056 $status = $submission->status;
1057 } else {
1058 $timesubmitted = $row->timesubmitted;
1059 $status = $row->status;
1060 }
1061
232b26f0
CS
1062 $displaystatus = $status;
1063 if ($displaystatus == 'new') {
1064 $displaystatus = '';
1065 }
1066
bbd0e548
DW
1067 if ($this->assignment->is_any_submission_plugin_enabled()) {
1068
232b26f0
CS
1069 $o .= $this->output->container(get_string('submissionstatus_' . $displaystatus, 'assign'),
1070 array('class'=>'submissionstatus' .$displaystatus));
c78e567c 1071 if ($due && $timesubmitted > $due && $status != ASSIGN_SUBMISSION_STATUS_NEW) {
79397b56 1072 $usertime = format_time($timesubmitted - $due);
556483d4
GF
1073 $latemessage = get_string('submittedlateshort',
1074 'assign',
1075 $usertime);
1076 $o .= $this->output->container($latemessage, 'latesubmission');
bbd0e548
DW
1077 }
1078 if ($row->locked) {
e5403f8c
DW
1079 $lockedstr = get_string('submissionslockedshort', 'assign');
1080 $o .= $this->output->container($lockedstr, 'lockedsubmission');
bbd0e548 1081 }
e24c2c1a 1082
24937b2c
GF
1083 // Add status of "grading" if markflow is not enabled.
1084 if (!$instance->markingworkflow) {
1085 if ($row->grade !== null && $row->grade >= 0) {
1086 $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
c78e567c 1087 } else if (!$timesubmitted || $status == ASSIGN_SUBMISSION_STATUS_NEW) {
24937b2c
GF
1088 $now = time();
1089 if ($due && ($now > $due)) {
1090 $overduestr = get_string('overdue', 'assign', format_time($now - $due));
1091 $o .= $this->output->container($overduestr, 'overduesubmission');
1092 }
9e795179
DW
1093 }
1094 }
24937b2c 1095 }
79397b56 1096
24937b2c
GF
1097 if ($instance->markingworkflow) {
1098 $o .= $this->col_workflowstatus($row);
1099 }
1100 if ($row->extensionduedate) {
1101 $userdate = userdate($row->extensionduedate);
1102 $extensionstr = get_string('userextensiondate', 'assign', $userdate);
1103 $o .= $this->output->container($extensionstr, 'extensiondate');
bbd0e548
DW
1104 }
1105
df47b77f 1106 if ($this->is_downloading()) {
b58d0595 1107 $o = strip_tags(rtrim(str_replace('</div>', ' - ', $o), '- '));
df47b77f
DW
1108 }
1109
bbd0e548
DW
1110 return $o;
1111 }
1112
d685b959
IT
1113 /**
1114 * Format a column of data for display.
1115 *
1116 * @param stdClass $row
1117 * @return string
1118 */
1119 public function col_allowsubmissionsfromdate(stdClass $row) {
1120 $o = '';
1121
1122 if ($row->allowsubmissionsfromdate) {
1123 $userdate = userdate($row->allowsubmissionsfromdate);
1124 $o = $this->output->container($userdate, 'allowsubmissionsfromdate');
1125 }
1126
1127 return $o;
1128
1129 }
1130
1131 /**
1132 * Format a column of data for display.
1133 *
1134 * @param stdClass $row
1135 * @return string
1136 */
1137 public function col_duedate(stdClass $row) {
1138 $o = '';
1139
1140 if ($row->duedate) {
1141 $userdate = userdate($row->duedate);
1142 $o = $this->output->container($userdate, 'duedate');
1143 }
1144
1145 return $o;
1146
1147 }
1148
1149 /**
1150 * Format a column of data for display.
1151 *
1152 * @param stdClass $row
1153 * @return string
1154 */
1155 public function col_cutoffdate(stdClass $row) {
1156 $o = '';
1157
1158 if ($row->cutoffdate) {
1159 $userdate = userdate($row->cutoffdate);
1160 $o = $this->output->container($userdate, 'cutoffdate');
1161 }
1162
1163 return $o;
1164
1165 }
1166
bbd0e548 1167 /**
e5403f8c 1168 * Format a column of data for display.
bbd0e548
DW
1169 *
1170 * @param stdClass $row
1171 * @return string
1172 */
e5403f8c 1173 public function col_userid(stdClass $row) {
57fbd5f9
DW
1174 global $USER;
1175
bbd0e548 1176 $edit = '';
bbd0e548
DW
1177
1178 $actions = array();
1179
5e84acc2
CB
1180 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1181 'rownum' => 0,
1182 'action' => 'grader');
1183
1184 if ($this->assignment->is_blind_marking()) {
4ed4be49
AN
1185 if (empty($row->recordid)) {
1186 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
1187 }
1188 $urlparams['blindid'] = $row->recordid;
5e84acc2
CB
1189 } else {
1190 $urlparams['userid'] = $row->userid;
1191 }
e5403f8c 1192 $url = new moodle_url('/mod/assign/view.php', $urlparams);
e9ddbae9 1193 $noimage = null;
e5403f8c 1194
bbd0e548 1195 if (!$row->grade) {
e5403f8c
DW
1196 $description = get_string('grade');
1197 } else {
1198 $description = get_string('updategrade', 'assign');
bbd0e548 1199 }
e9ddbae9
DW
1200 $actions['grade'] = new action_menu_link_secondary(
1201 $url,
1202 $noimage,
1203 $description
1204 );
bbd0e548 1205
c70de965
DW
1206 // Everything we need is in the row.
1207 $submission = $row;
1208 $flags = $row;
1209 if ($this->assignment->get_instance()->teamsubmission) {
1210 // Use the cache for this.
1211 $submission = false;
1212 $group = false;
1213 $this->get_group_and_submission($row->id, $group, $submission, -1);
1214 }
1215
1216 $submissionsopen = $this->assignment->submissions_open($row->id,
1217 true,
1218 $submission,
1219 $flags,
1220 $this->gradinginfo);
57fbd5f9
DW
1221 $caneditsubmission = $this->assignment->can_edit_submission($row->id, $USER->id);
1222
9e795179
DW
1223 // Hide for offline assignments.
1224 if ($this->assignment->is_any_submission_plugin_enabled()) {
1225 if (!$row->status ||
1226 $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
1227 !$this->assignment->get_instance()->submissiondrafts) {
1228
1229 if (!$row->locked) {
e5403f8c
DW
1230 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1231 'userid'=>$row->id,
1232 'action'=>'lock',
1233 'sesskey'=>sesskey(),
1234 'page'=>$this->currpage);
1235 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1236
9e795179 1237 $description = get_string('preventsubmissionsshort', 'assign');
e9ddbae9
DW
1238 $actions['lock'] = new action_menu_link_secondary(
1239 $url,
1240 $noimage,
1241 $description
1242 );
9e795179 1243 } else {
e5403f8c
DW
1244 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1245 'userid'=>$row->id,
1246 'action'=>'unlock',
1247 'sesskey'=>sesskey(),
1248 'page'=>$this->currpage);
1249 $url = new moodle_url('/mod/assign/view.php', $urlparams);
9e795179 1250 $description = get_string('allowsubmissionsshort', 'assign');
e9ddbae9
DW
1251 $actions['unlock'] = new action_menu_link_secondary(
1252 $url,
1253 $noimage,
1254 $description
1255 );
9e795179
DW
1256 }
1257 }
1258
57fbd5f9
DW
1259 if ($submissionsopen &&
1260 $USER->id != $row->id &&
1261 $caneditsubmission) {
1262 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1263 'userid'=>$row->id,
1264 'action'=>'editsubmission',
1265 'sesskey'=>sesskey(),
1266 'page'=>$this->currpage);
1267 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1268 $description = get_string('editsubmission', 'assign');
e9ddbae9
DW
1269 $actions['editsubmission'] = new action_menu_link_secondary(
1270 $url,
1271 $noimage,
1272 $description
1273 );
57fbd5f9 1274 }
bbd0e548 1275 }
24937b2c
GF
1276 if (($this->assignment->get_instance()->duedate ||
1277 $this->assignment->get_instance()->cutoffdate) &&
1278 $this->hasgrantextension) {
1279 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1280 'userid' => $row->id,
1281 'action' => 'grantextension',
1282 'sesskey' => sesskey(),
1283 'page' => $this->currpage);
1284 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1285 $description = get_string('grantextension', 'assign');
1286 $actions['grantextension'] = new action_menu_link_secondary(
1287 $url,
1288 $noimage,
1289 $description
1290 );
1291 }
e5403f8c
DW
1292 if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
1293 $this->assignment->get_instance()->submissiondrafts) {
1294 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1295 'userid'=>$row->id,
1296 'action'=>'reverttodraft',
1297 'sesskey'=>sesskey(),
1298 'page'=>$this->currpage);
1299 $url = new moodle_url('/mod/assign/view.php', $urlparams);
bbd0e548 1300 $description = get_string('reverttodraftshort', 'assign');
e9ddbae9
DW
1301 $actions['reverttodraft'] = new action_menu_link_secondary(
1302 $url,
1303 $noimage,
1304 $description
1305 );
bbd0e548 1306 }
57fbd5f9
DW
1307 if ($row->status == ASSIGN_SUBMISSION_STATUS_DRAFT &&
1308 $this->assignment->get_instance()->submissiondrafts &&
1309 $caneditsubmission &&
1310 $submissionsopen &&
1311 $row->id != $USER->id) {
1312 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1313 'userid'=>$row->id,
1314 'action'=>'submitotherforgrading',
1315 'sesskey'=>sesskey(),
1316 'page'=>$this->currpage);
1317 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1318 $description = get_string('submitforgrading', 'assign');
e9ddbae9
DW
1319 $actions['submitforgrading'] = new action_menu_link_secondary(
1320 $url,
1321 $noimage,
1322 $description
1323 );
57fbd5f9 1324 }
bbd0e548 1325
df211804
DW
1326 $ismanual = $this->assignment->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
1327 $hassubmission = !empty($row->status);
1328 $notreopened = $hassubmission && $row->status != ASSIGN_SUBMISSION_STATUS_REOPENED;
1329 $isunlimited = $this->assignment->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS;
1330 $hasattempts = $isunlimited || $row->attemptnumber < $this->assignment->get_instance()->maxattempts - 1;
1331
1332 if ($ismanual && $hassubmission && $notreopened && $hasattempts) {
1333 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1334 'userid'=>$row->id,
1335 'action'=>'addattempt',
1336 'sesskey'=>sesskey(),
1337 'page'=>$this->currpage);
1338 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1339 $description = get_string('addattempt', 'assign');
e9ddbae9
DW
1340 $actions['addattempt'] = new action_menu_link_secondary(
1341 $url,
1342 $noimage,
1343 $description
1344 );
df211804
DW
1345 }
1346
e9ddbae9
DW
1347 $menu = new action_menu();
1348 $menu->set_owner_selector('.gradingtable-actionmenu');
1349 $menu->set_alignment(action_menu::TL, action_menu::BL);
1350 $menu->set_constraint('.gradingtable > .no-overflow');
1351 $menu->set_menu_trigger(get_string('edit'));
1352 foreach ($actions as $action) {
1353 $menu->add($action);
1354 }
bbd0e548 1355
e9ddbae9
DW
1356 // Prioritise the menu ahead of all other actions.
1357 $menu->prioritise = true;
bbd0e548 1358
e9ddbae9 1359 $edit .= $this->output->render($menu);
bbd0e548
DW
1360
1361 return $edit;
1362 }
1363
1364 /**
1365 * Write the plugin summary with an optional link to view the full feedback/submission.
1366 *
1367 * @param assign_plugin $plugin Submission plugin or feedback plugin
1368 * @param stdClass $item Submission or grade
e5403f8c
DW
1369 * @param string $returnaction The return action to pass to the
1370 * view_submission page (the current page)
1371 * @param string $returnparams The return params to pass to the view_submission
1372 * page (the current page)
bbd0e548
DW
1373 * @return string The summary with an optional link
1374 */
e5403f8c
DW
1375 private function format_plugin_summary_with_link(assign_plugin $plugin,
1376 stdClass $item,
1377 $returnaction,
1378 $returnparams) {
bbd0e548
DW
1379 $link = '';
1380 $showviewlink = false;
2c6a3dbf 1381
bbd0e548
DW
1382 $summary = $plugin->view_summary($item, $showviewlink);
1383 $separator = '';
1384 if ($showviewlink) {
e5403f8c
DW
1385 $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
1386 $icon = $this->output->pix_icon('t/preview', $viewstr);
1387 $urlparams = array('id' => $this->assignment->get_course_module()->id,
bbd0e548
DW
1388 'sid'=>$item->id,
1389 'gid'=>$item->id,
1390 'plugin'=>$plugin->get_type(),
1391 'action'=>'viewplugin' . $plugin->get_subtype(),
1392 'returnaction'=>$returnaction,
e5403f8c
DW
1393 'returnparams'=>http_build_query($returnparams));
1394 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1395 $link = $this->output->action_link($url, $icon);
bbd0e548
DW
1396 $separator = $this->output->spacer(array(), true);
1397 }
1398
1399 return $link . $separator . $summary;
1400 }
1401
1402
1403 /**
e5403f8c 1404 * Format the submission and feedback columns.
bbd0e548
DW
1405 *
1406 * @param string $colname The column name
1407 * @param stdClass $row The submission row
1408 * @return mixed string or NULL
1409 */
e5403f8c 1410 public function other_cols($colname, $row) {
d08e6c31
DW
1411 // For extra user fields the result is already in $row.
1412 if (empty($this->plugincache[$colname])) {
1413 return $row->$colname;
1414 }
1415
1416 // This must be a plugin field.
df47b77f 1417 $plugincache = $this->plugincache[$colname];
2c6a3dbf 1418
df47b77f
DW
1419 $plugin = $plugincache[0];
1420
1421 $field = null;
1422 if (isset($plugincache[1])) {
1423 $field = $plugincache[1];
1424 }
1425
1426 if ($plugin->is_visible() && $plugin->is_enabled()) {
1427 if ($plugin->get_subtype() == 'assignsubmission') {
cd01491c
DW
1428 if ($this->assignment->get_instance()->teamsubmission) {
1429 $group = false;
1430 $submission = false;
5ef72f85
DW
1431
1432 $this->get_group_and_submission($row->id, $group, $submission, -1);
cd01491c 1433 if ($submission) {
5ef72f85
DW
1434 if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1435 // For a newly reopened submission - we want to show the previous submission in the table.
1436 $this->get_group_and_submission($row->id, $group, $submission, $submission->attemptnumber-1);
1437 }
df47b77f
DW
1438 if (isset($field)) {
1439 return $plugin->get_editor_text($field, $submission->id);
1440 }
e5403f8c
DW
1441 return $this->format_plugin_summary_with_link($plugin,
1442 $submission,
1443 'grading',
1444 array());
cd01491c
DW
1445 }
1446 } else if ($row->submissionid) {
5ef72f85
DW
1447 if ($row->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1448 // For a newly reopened submission - we want to show the previous submission in the table.
1449 $submission = $this->assignment->get_user_submission($row->userid, false, $row->attemptnumber - 1);
1450 } else {
1451 $submission = new stdClass();
1452 $submission->id = $row->submissionid;
1453 $submission->timecreated = $row->firstsubmission;
1454 $submission->timemodified = $row->timesubmitted;
1455 $submission->assignment = $this->assignment->get_instance()->id;
1456 $submission->userid = $row->userid;
5c386472 1457 $submission->attemptnumber = $row->attemptnumber;
5ef72f85
DW
1458 }
1459 // Field is used for only for import/export and refers the the fieldname for the text editor.
df47b77f 1460 if (isset($field)) {
5ef72f85 1461 return $plugin->get_editor_text($field, $submission->id);
df47b77f 1462 }
e5403f8c
DW
1463 return $this->format_plugin_summary_with_link($plugin,
1464 $submission,
1465 'grading',
1466 array());
bbd0e548 1467 }
df47b77f 1468 } else {
2c6a3dbf 1469 $grade = null;
df47b77f
DW
1470 if (isset($field)) {
1471 return $plugin->get_editor_text($field, $row->gradeid);
1472 }
1473
bbd0e548
DW
1474 if ($row->gradeid) {
1475 $grade = new stdClass();
1476 $grade->id = $row->gradeid;
1477 $grade->timecreated = $row->firstmarked;
1478 $grade->timemodified = $row->timemarked;
1479 $grade->assignment = $this->assignment->get_instance()->id;
1480 $grade->userid = $row->userid;
1481 $grade->grade = $row->grade;
1482 $grade->mailed = $row->mailed;
5c386472 1483 $grade->attemptnumber = $row->attemptnumber;
2c6a3dbf
DW
1484 }
1485 if ($this->quickgrading && $plugin->supports_quickgrading()) {
1486 return $plugin->get_quickgrading_html($row->userid, $grade);
1487 } else if ($grade) {
e5403f8c
DW
1488 return $this->format_plugin_summary_with_link($plugin,
1489 $grade,
1490 'grading',
1491 array());
bbd0e548
DW
1492 }
1493 }
bbd0e548 1494 }
df47b77f 1495 return '';
bbd0e548
DW
1496 }
1497
1498 /**
e5403f8c 1499 * Using the current filtering and sorting - load all rows and return a single column from them.
bbd0e548
DW
1500 *
1501 * @param string $columnname The name of the raw column data
1502 * @return array of data
1503 */
e5403f8c 1504 public function get_column_data($columnname) {
bbd0e548
DW
1505 $this->setup();
1506 $this->currpage = 0;
1507 $this->query_db($this->tablemaxrows);
1508 $result = array();
1509 foreach ($this->rawdata as $row) {
1510 $result[] = $row->$columnname;
1511 }
1512 return $result;
1513 }
bbd0e548
DW
1514
1515 /**
e5403f8c 1516 * Return things to the renderer.
bbd0e548
DW
1517 *
1518 * @return string the assignment name
1519 */
e5403f8c 1520 public function get_assignment_name() {
bbd0e548
DW
1521 return $this->assignment->get_instance()->name;
1522 }
1523
1524 /**
e5403f8c 1525 * Return things to the renderer.
bbd0e548
DW
1526 *
1527 * @return int the course module id
1528 */
e5403f8c 1529 public function get_course_module_id() {
bbd0e548
DW
1530 return $this->assignment->get_course_module()->id;
1531 }
1532
1533 /**
e5403f8c 1534 * Return things to the renderer.
bbd0e548
DW
1535 *
1536 * @return int the course id
1537 */
e5403f8c 1538 public function get_course_id() {
bbd0e548
DW
1539 return $this->assignment->get_course()->id;
1540 }
1541
1542 /**
e5403f8c 1543 * Return things to the renderer.
bbd0e548
DW
1544 *
1545 * @return stdClass The course context
1546 */
e5403f8c 1547 public function get_course_context() {
bbd0e548
DW
1548 return $this->assignment->get_course_context();
1549 }
1550
1551 /**
e5403f8c 1552 * Return things to the renderer.
bbd0e548
DW
1553 *
1554 * @return bool Does this assignment accept submissions
1555 */
e5403f8c 1556 public function submissions_enabled() {
bbd0e548
DW
1557 return $this->assignment->is_any_submission_plugin_enabled();
1558 }
1559
1560 /**
e5403f8c 1561 * Return things to the renderer.
bbd0e548
DW
1562 *
1563 * @return bool Can this user view all grades (the gradebook)
1564 */
e5403f8c
DW
1565 public function can_view_all_grades() {
1566 $context = $this->assignment->get_course_context();
1567 return has_capability('gradereport/grader:view', $context) &&
1568 has_capability('moodle/grade:viewall', $context);
bbd0e548
DW
1569 }
1570
85d0696d
DW
1571 /**
1572 * Always return a valid sort - even if the userid column is missing.
1573 * @return array column name => SORT_... constant.
1574 */
1575 public function get_sort_columns() {
1576 $result = parent::get_sort_columns();
eb4c4661
AN
1577
1578 $assignment = $this->assignment->get_instance();
1579 if (empty($assignment->blindmarking)) {
1580 $result = array_merge($result, array('userid' => SORT_ASC));
1581 } else {
1582 $result = array_merge($result, [
1583 'COALESCE(s.timecreated, ' . time() . ')' => SORT_ASC,
1584 'COALESCE(s.id, ' . PHP_INT_MAX . ')' => SORT_ASC,
1585 'um.id' => SORT_ASC,
1586 ]);
1587 }
85d0696d
DW
1588 return $result;
1589 }
1590
bbd0e548 1591 /**
e5403f8c 1592 * Override the table show_hide_link to not show for select column.
bbd0e548
DW
1593 *
1594 * @param string $column the column name, index into various names.
1595 * @param int $index numerical index of the column.
1596 * @return string HTML fragment.
1597 */
1598 protected function show_hide_link($column, $index) {
4a47008c 1599 if ($index > 0 || !$this->hasgrade) {
bbd0e548
DW
1600 return parent::show_hide_link($column, $index);
1601 }
1602 return '';
1603 }
3fca6937
NM
1604
1605 /**
1606 * Overides setup to ensure it will only run a single time.
1607 */
1608 public function setup() {
1609 // Check if the setup function has been called before, we should not run it twice.
1610 // If we do the sortorder of the table will be broken.
1611 if (!empty($this->setup)) {
1612 return;
1613 }
1614 parent::setup();
1615 }
bbd0e548 1616}