Merge branch 'MDL-57812-master' of git://github.com/jleyva/moodle
[moodle.git] / mod / assign / gradingtable.php
blob06a9b2d171f4c975d92fb8d25940ee2d109ad3db
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This file contains the definition for the grading table which subclassses easy_table
20 * @package mod_assign
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->libdir.'/tablelib.php');
28 require_once($CFG->libdir.'/gradelib.php');
29 require_once($CFG->dirroot.'/mod/assign/locallib.php');
31 /**
32 * Extends table_sql to provide a table of assignment submissions
34 * @package mod_assign
35 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class assign_grading_table extends table_sql implements renderable {
39 /** @var assign $assignment */
40 private $assignment = null;
41 /** @var int $perpage */
42 private $perpage = 10;
43 /** @var int $rownum (global index of current row in table) */
44 private $rownum = -1;
45 /** @var renderer_base for getting output */
46 private $output = null;
47 /** @var stdClass gradinginfo */
48 private $gradinginfo = null;
49 /** @var int $tablemaxrows */
50 private $tablemaxrows = 10000;
51 /** @var boolean $quickgrading */
52 private $quickgrading = false;
53 /** @var boolean $hasgrantextension - Only do the capability check once for the entire table */
54 private $hasgrantextension = false;
55 /** @var boolean $hasgrade - Only do the capability check once for the entire table */
56 private $hasgrade = false;
57 /** @var array $groupsubmissions - A static cache of group submissions */
58 private $groupsubmissions = array();
59 /** @var array $submissiongroups - A static cache of submission groups */
60 private $submissiongroups = array();
61 /** @var string $plugingradingbatchoperations - List of plugin supported batch operations */
62 public $plugingradingbatchoperations = array();
63 /** @var array $plugincache - A cache of plugin lookups to match a column name to a plugin efficiently */
64 private $plugincache = array();
65 /** @var array $scale - A list of the keys and descriptions for the custom scale */
66 private $scale = null;
68 /**
69 * overridden constructor keeps a reference to the assignment class that is displaying this table
71 * @param assign $assignment The assignment class
72 * @param int $perpage how many per page
73 * @param string $filter The current filter
74 * @param int $rowoffset For showing a subsequent page of results
75 * @param bool $quickgrading Is this table wrapped in a quickgrading form?
76 * @param string $downloadfilename
78 public function __construct(assign $assignment,
79 $perpage,
80 $filter,
81 $rowoffset,
82 $quickgrading,
83 $downloadfilename = null) {
84 global $CFG, $PAGE, $DB, $USER;
85 parent::__construct('mod_assign_grading');
86 $this->is_persistent(true);
87 $this->assignment = $assignment;
89 // Check permissions up front.
90 $this->hasgrantextension = has_capability('mod/assign:grantextension',
91 $this->assignment->get_context());
92 $this->hasgrade = $this->assignment->can_grade();
94 // Check if we have the elevated view capablities to see the blind details.
95 $this->hasviewblind = has_capability('mod/assign:viewblinddetails',
96 $this->assignment->get_context());
98 foreach ($assignment->get_feedback_plugins() as $plugin) {
99 if ($plugin->is_visible() && $plugin->is_enabled()) {
100 foreach ($plugin->get_grading_batch_operations() as $action => $description) {
101 if (empty($this->plugingradingbatchoperations)) {
102 $this->plugingradingbatchoperations[$plugin->get_type()] = array();
104 $this->plugingradingbatchoperations[$plugin->get_type()][$action] = $description;
108 $this->perpage = $perpage;
109 $this->quickgrading = $quickgrading && $this->hasgrade;
110 $this->output = $PAGE->get_renderer('mod_assign');
112 $urlparams = array('action'=>'grading', 'id'=>$assignment->get_course_module()->id);
113 $url = new moodle_url($CFG->wwwroot . '/mod/assign/view.php', $urlparams);
114 $this->define_baseurl($url);
116 // Do some business - then set the sql.
117 $currentgroup = groups_get_activity_group($assignment->get_course_module(), true);
119 if ($rowoffset) {
120 $this->rownum = $rowoffset - 1;
123 $users = array_keys( $assignment->list_participants($currentgroup, true));
124 if (count($users) == 0) {
125 // Insert a record that will never match to the sql is still valid.
126 $users[] = -1;
129 $params = array();
130 $params['assignmentid1'] = (int)$this->assignment->get_instance()->id;
131 $params['assignmentid2'] = (int)$this->assignment->get_instance()->id;
132 $params['assignmentid3'] = (int)$this->assignment->get_instance()->id;
134 $params['assignmentid5'] = (int)$this->assignment->get_instance()->id;
135 $params['assignmentid6'] = (int)$this->assignment->get_instance()->id;
136 $params['assignmentid7'] = (int)$this->assignment->get_instance()->id;
137 $params['assignmentid8'] = (int)$this->assignment->get_instance()->id;
138 $params['assignmentid9'] = (int)$this->assignment->get_instance()->id;
140 $extrauserfields = get_extra_user_fields($this->assignment->get_context());
142 $fields = user_picture::fields('u', $extrauserfields) . ', ';
143 $fields .= 'u.id as userid, ';
144 $fields .= 's.status as status, ';
145 $fields .= 's.id as submissionid, ';
146 $fields .= 's.timecreated as firstsubmission, ';
147 $fields .= 's.timemodified as timesubmitted, ';
148 $fields .= 's.attemptnumber as attemptnumber, ';
149 $fields .= 'g.id as gradeid, ';
150 $fields .= 'g.grade as grade, ';
151 $fields .= 'g.timemodified as timemarked, ';
152 $fields .= 'g.timecreated as firstmarked, ';
153 $fields .= 'uf.mailed as mailed, ';
154 $fields .= 'uf.locked as locked, ';
155 $fields .= 'uf.extensionduedate as extensionduedate, ';
156 $fields .= 'uf.workflowstate as workflowstate, ';
157 $fields .= 'uf.allocatedmarker as allocatedmarker';
159 $from = '{user} u
160 LEFT JOIN {assign_submission} s
161 ON u.id = s.userid
162 AND s.assignment = :assignmentid1
163 AND s.latest = 1
164 LEFT JOIN {assign_grades} g
165 ON u.id = g.userid
166 AND g.assignment = :assignmentid2 ';
168 // For group submissions we don't immediately create an entry in the assign_submission table for each user,
169 // instead the userid is set to 0. In this case we use a different query to retrieve the grade for the user.
170 if ($this->assignment->get_instance()->teamsubmission) {
171 $params['assignmentid4'] = (int) $this->assignment->get_instance()->id;
172 $grademaxattempt = 'SELECT mxg.userid, MAX(mxg.attemptnumber) AS maxattempt
173 FROM {assign_grades} mxg
174 WHERE mxg.assignment = :assignmentid4
175 GROUP BY mxg.userid';
176 $from .= 'LEFT JOIN (' . $grademaxattempt . ') gmx
177 ON u.id = gmx.userid
178 AND g.attemptnumber = gmx.maxattempt ';
179 } else {
180 $from .= 'AND g.attemptnumber = s.attemptnumber ';
183 $from .= 'LEFT JOIN {assign_user_flags} uf
184 ON u.id = uf.userid
185 AND uf.assignment = :assignmentid3 ';
187 $hasoverrides = $this->assignment->has_overrides();
189 if ($hasoverrides) {
190 $fields .= ', priority.priority, ';
191 $fields .= 'effective.allowsubmissionsfromdate, ';
192 $fields .= 'effective.duedate, ';
193 $fields .= 'effective.cutoffdate ';
195 $from .= ' LEFT JOIN (
196 SELECT merged.userid, min(merged.priority) priority FROM (
197 ( SELECT u.id as userid, 9999999 AS priority
198 FROM {user} u
200 UNION
201 ( SELECT uo.userid, 0 AS priority
202 FROM {assign_overrides} uo
203 WHERE uo.assignid = :assignmentid5
205 UNION
206 ( SELECT gm.userid, go.sortorder AS priority
207 FROM {assign_overrides} go
208 JOIN {groups} g ON g.id = go.groupid
209 JOIN {groups_members} gm ON gm.groupid = g.id
210 WHERE go.assignid = :assignmentid6
212 ) AS merged
213 GROUP BY merged.userid
214 ) priority ON priority.userid = u.id
216 JOIN (
217 (SELECT 9999999 AS priority,
218 u.id AS userid,
220 a.allowsubmissionsfromdate,
221 a.duedate,
222 a.cutoffdate
223 FROM {user} u
224 JOIN {assign} a ON a.id = :assignmentid7
226 UNION
227 (SELECT 0 AS priority,
228 uo.userid,
230 uo.allowsubmissionsfromdate,
231 uo.duedate,
232 uo.cutoffdate
233 FROM {assign_overrides} uo
234 WHERE uo.assignid = :assignmentid8
236 UNION
237 (SELECT go.sortorder AS priority,
238 gm.userid,
240 go.allowsubmissionsfromdate,
241 go.duedate,
242 go.cutoffdate
243 FROM {assign_overrides} go
244 JOIN {groups} g ON g.id = go.groupid
245 JOIN {groups_members} gm ON gm.groupid = g.id
246 WHERE go.assignid = :assignmentid9
249 ) AS effective ON effective.priority = priority.priority AND effective.userid = priority.userid ';
252 if (!empty($this->assignment->get_instance()->blindmarking)) {
253 $from .= 'LEFT JOIN {assign_user_mapping} um
254 ON u.id = um.userid
255 AND um.assignment = :assignmentid5 ';
256 $params['assignmentid5'] = (int)$this->assignment->get_instance()->id;
257 $fields .= ', um.id as recordid ';
260 $userparams = array();
261 $userindex = 0;
263 list($userwhere, $userparams) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
264 $where = 'u.id ' . $userwhere;
265 $params = array_merge($params, $userparams);
267 // The filters do not make sense when there are no submissions, so do not apply them.
268 if ($this->assignment->is_any_submission_plugin_enabled()) {
269 if ($filter == ASSIGN_FILTER_SUBMITTED) {
270 $where .= ' AND (s.timemodified IS NOT NULL AND
271 s.status = :submitted) ';
272 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
274 } else if ($filter == ASSIGN_FILTER_NOT_SUBMITTED) {
275 $where .= ' AND (s.timemodified IS NULL OR s.status != :submitted) ';
276 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
277 } else if ($filter == ASSIGN_FILTER_REQUIRE_GRADING) {
278 $where .= ' AND (s.timemodified IS NOT NULL AND
279 s.status = :submitted AND
280 (s.timemodified >= g.timemodified OR g.timemodified IS NULL OR g.grade IS NULL';
282 if ($this->assignment->get_grade_item()->gradetype == GRADE_TYPE_SCALE) {
283 // Scale grades are set to -1 when not graded.
284 $where .= ' OR g.grade = -1';
287 $where .= '))';
288 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
290 } else if ($filter == ASSIGN_FILTER_GRANTED_EXTENSION) {
291 $where .= ' AND uf.extensionduedate > 0 ';
293 } else if (strpos($filter, ASSIGN_FILTER_SINGLE_USER) === 0) {
294 $userfilter = (int) array_pop(explode('=', $filter));
295 $where .= ' AND (u.id = :userid)';
296 $params['userid'] = $userfilter;
300 if ($this->assignment->get_instance()->markingworkflow &&
301 $this->assignment->get_instance()->markingallocation) {
302 if (has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
303 // Check to see if marker filter is set.
304 $markerfilter = (int)get_user_preferences('assign_markerfilter', '');
305 if (!empty($markerfilter)) {
306 if ($markerfilter == ASSIGN_MARKER_FILTER_NO_MARKER) {
307 $where .= ' AND (uf.allocatedmarker IS NULL OR uf.allocatedmarker = 0)';
308 } else {
309 $where .= ' AND uf.allocatedmarker = :markerid';
310 $params['markerid'] = $markerfilter;
316 if ($this->assignment->get_instance()->markingworkflow) {
317 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
318 if (!empty($workflowstates)) {
319 $workflowfilter = get_user_preferences('assign_workflowfilter', '');
320 if ($workflowfilter == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED) {
321 $where .= ' AND (uf.workflowstate = :workflowstate OR uf.workflowstate IS NULL OR '.
322 $DB->sql_isempty('assign_user_flags', 'workflowstate', true, true).')';
323 $params['workflowstate'] = $workflowfilter;
324 } else if (array_key_exists($workflowfilter, $workflowstates)) {
325 $where .= ' AND uf.workflowstate = :workflowstate';
326 $params['workflowstate'] = $workflowfilter;
331 $this->set_sql($fields, $from, $where, $params);
333 if ($downloadfilename) {
334 $this->is_downloading('csv', $downloadfilename);
337 $columns = array();
338 $headers = array();
340 // Select.
341 if (!$this->is_downloading() && $this->hasgrade) {
342 $columns[] = 'select';
343 $headers[] = get_string('select') .
344 '<div class="selectall"><label class="accesshide" for="selectall">' . get_string('selectall') . '</label>
345 <input type="checkbox" id="selectall" name="selectall" title="' . get_string('selectall') . '"/></div>';
348 // User picture.
349 if ($this->hasviewblind || !$this->assignment->is_blind_marking()) {
350 if (!$this->is_downloading()) {
351 $columns[] = 'picture';
352 $headers[] = get_string('pictureofuser');
353 } else {
354 $columns[] = 'recordid';
355 $headers[] = get_string('recordid', 'assign');
358 // Fullname.
359 $columns[] = 'fullname';
360 $headers[] = get_string('fullname');
362 // Participant # details if can view real identities.
363 if ($this->assignment->is_blind_marking()) {
364 if (!$this->is_downloading()) {
365 $columns[] = 'recordid';
366 $headers[] = get_string('recordid', 'assign');
370 foreach ($extrauserfields as $extrafield) {
371 $columns[] = $extrafield;
372 $headers[] = get_user_field_name($extrafield);
374 } else {
375 // Record ID.
376 $columns[] = 'recordid';
377 $headers[] = get_string('recordid', 'assign');
380 // Submission status.
381 $columns[] = 'status';
382 $headers[] = get_string('status', 'assign');
384 if ($hasoverrides) {
385 // Allowsubmissionsfromdate.
386 $columns[] = 'allowsubmissionsfromdate';
387 $headers[] = get_string('allowsubmissionsfromdate', 'assign');
389 // Duedate.
390 $columns[] = 'duedate';
391 $headers[] = get_string('duedate', 'assign');
393 // Cutoffdate.
394 $columns[] = 'cutoffdate';
395 $headers[] = get_string('cutoffdate', 'assign');
398 // Team submission columns.
399 if ($assignment->get_instance()->teamsubmission) {
400 $columns[] = 'team';
401 $headers[] = get_string('submissionteam', 'assign');
403 // Allocated marker.
404 if ($this->assignment->get_instance()->markingworkflow &&
405 $this->assignment->get_instance()->markingallocation &&
406 has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
407 // Add a column for the allocated marker.
408 $columns[] = 'allocatedmarker';
409 $headers[] = get_string('marker', 'assign');
411 // Grade.
412 $columns[] = 'grade';
413 $headers[] = get_string('grade');
414 if ($this->is_downloading()) {
415 if ($this->assignment->get_instance()->grade >= 0) {
416 $columns[] = 'grademax';
417 $headers[] = get_string('maxgrade', 'assign');
418 } else {
419 // This is a custom scale.
420 $columns[] = 'scale';
421 $headers[] = get_string('scale', 'assign');
424 if ($this->assignment->get_instance()->markingworkflow) {
425 // Add a column for the marking workflow state.
426 $columns[] = 'workflowstate';
427 $headers[] = get_string('markingworkflowstate', 'assign');
429 // Add a column for the list of valid marking workflow states.
430 $columns[] = 'gradecanbechanged';
431 $headers[] = get_string('gradecanbechanged', 'assign');
433 if (!$this->is_downloading() && $this->hasgrade) {
434 // We have to call this column userid so we can use userid as a default sortable column.
435 $columns[] = 'userid';
436 $headers[] = get_string('edit');
439 // Submission plugins.
440 if ($assignment->is_any_submission_plugin_enabled()) {
441 $columns[] = 'timesubmitted';
442 $headers[] = get_string('lastmodifiedsubmission', 'assign');
444 foreach ($this->assignment->get_submission_plugins() as $plugin) {
445 if ($this->is_downloading()) {
446 if ($plugin->is_visible() && $plugin->is_enabled()) {
447 foreach ($plugin->get_editor_fields() as $field => $description) {
448 $index = 'plugin' . count($this->plugincache);
449 $this->plugincache[$index] = array($plugin, $field);
450 $columns[] = $index;
451 $headers[] = $plugin->get_name();
454 } else {
455 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
456 $index = 'plugin' . count($this->plugincache);
457 $this->plugincache[$index] = array($plugin);
458 $columns[] = $index;
459 $headers[] = $plugin->get_name();
465 // Time marked.
466 $columns[] = 'timemarked';
467 $headers[] = get_string('lastmodifiedgrade', 'assign');
469 // Feedback plugins.
470 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
471 if ($this->is_downloading()) {
472 if ($plugin->is_visible() && $plugin->is_enabled()) {
473 foreach ($plugin->get_editor_fields() as $field => $description) {
474 $index = 'plugin' . count($this->plugincache);
475 $this->plugincache[$index] = array($plugin, $field);
476 $columns[] = $index;
477 $headers[] = $description;
480 } else if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
481 $index = 'plugin' . count($this->plugincache);
482 $this->plugincache[$index] = array($plugin);
483 $columns[] = $index;
484 $headers[] = $plugin->get_name();
488 // Exclude 'Final grade' column in downloaded grading worksheets.
489 if (!$this->is_downloading()) {
490 // Final grade.
491 $columns[] = 'finalgrade';
492 $headers[] = get_string('finalgrade', 'grades');
495 // Load the grading info for all users.
496 $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id,
497 'mod',
498 'assign',
499 $this->assignment->get_instance()->id,
500 $users);
502 if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
503 $columns[] = 'outcomes';
504 $headers[] = get_string('outcomes', 'grades');
507 // Set the columns.
508 $this->define_columns($columns);
509 $this->define_headers($headers);
510 foreach ($extrauserfields as $extrafield) {
511 $this->column_class($extrafield, $extrafield);
513 $this->no_sorting('recordid');
514 $this->no_sorting('finalgrade');
515 $this->no_sorting('userid');
516 $this->no_sorting('select');
517 $this->no_sorting('outcomes');
519 if ($assignment->get_instance()->teamsubmission) {
520 $this->no_sorting('team');
523 $plugincolumnindex = 0;
524 foreach ($this->assignment->get_submission_plugins() as $plugin) {
525 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
526 $submissionpluginindex = 'plugin' . $plugincolumnindex++;
527 $this->no_sorting($submissionpluginindex);
530 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
531 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
532 $feedbackpluginindex = 'plugin' . $plugincolumnindex++;
533 $this->no_sorting($feedbackpluginindex);
537 // When there is no data we still want the column headers printed in the csv file.
538 if ($this->is_downloading()) {
539 $this->start_output();
544 * Before adding each row to the table make sure rownum is incremented.
546 * @param array $row row of data from db used to make one row of the table.
547 * @return array one row for the table
549 public function format_row($row) {
550 if ($this->rownum < 0) {
551 $this->rownum = $this->currpage * $this->pagesize;
552 } else {
553 $this->rownum += 1;
556 return parent::format_row($row);
560 * Add a column with an ID that uniquely identifies this user in this assignment.
562 * @param stdClass $row
563 * @return string
565 public function col_recordid(stdClass $row) {
566 if (empty($row->recordid)) {
567 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
569 return get_string('hiddenuser', 'assign') . $row->recordid;
574 * Add the userid to the row class so it can be updated via ajax.
576 * @param stdClass $row The row of data
577 * @return string The row class
579 public function get_row_class($row) {
580 return 'user' . $row->userid;
584 * Return the number of rows to display on a single page.
586 * @return int The number of rows per page
588 public function get_rows_per_page() {
589 return $this->perpage;
593 * list current marking workflow state
595 * @param stdClass $row
596 * @return string
598 public function col_workflowstatus(stdClass $row) {
599 $o = '';
601 $gradingdisabled = $this->assignment->grading_disabled($row->id);
602 // The function in the assignment keeps a static cache of this list of states.
603 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
604 $workflowstate = $row->workflowstate;
605 if (empty($workflowstate)) {
606 $workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
608 if ($this->quickgrading && !$gradingdisabled) {
609 $notmarked = get_string('markingworkflowstatenotmarked', 'assign');
610 $name = 'quickgrade_' . $row->id . '_workflowstate';
611 $o .= html_writer::select($workflowstates, $name, $workflowstate, array('' => $notmarked));
612 // Check if this user is a marker that can't manage allocations and doesn't have the marker column added.
613 if ($this->assignment->get_instance()->markingworkflow &&
614 $this->assignment->get_instance()->markingallocation &&
615 !has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
617 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
618 $o .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $name,
619 'value' => $row->allocatedmarker));
621 } else {
622 $o .= $this->output->container(get_string('markingworkflowstate' . $workflowstate, 'assign'), $workflowstate);
624 return $o;
628 * For download only - list current marking workflow state
630 * @param stdClass $row - The row of data
631 * @return string The current marking workflow state
633 public function col_workflowstate($row) {
634 $state = $row->workflowstate;
635 if (empty($state)) {
636 $state = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
639 return get_string('markingworkflowstate' . $state, 'assign');
643 * list current marker
645 * @param stdClass $row - The row of data
646 * @return id the user->id of the marker.
648 public function col_allocatedmarker(stdClass $row) {
649 static $markers = null;
650 static $markerlist = array();
651 if ($markers === null) {
652 list($sort, $params) = users_order_by_sql();
653 $markers = get_users_by_capability($this->assignment->get_context(), 'mod/assign:grade', '', $sort);
654 $markerlist[0] = get_string('choosemarker', 'assign');
655 $viewfullnames = has_capability('moodle/site:viewfullnames', $this->assignment->get_context());
656 foreach ($markers as $marker) {
657 $markerlist[$marker->id] = fullname($marker, $viewfullnames);
660 if (empty($markerlist)) {
661 // TODO: add some form of notification here that no markers are available.
662 return '';
664 if ($this->is_downloading()) {
665 if (isset($markers[$row->allocatedmarker])) {
666 return fullname($markers[$row->allocatedmarker],
667 has_capability('moodle/site:viewfullnames', $this->assignment->get_context()));
668 } else {
669 return '';
673 if ($this->quickgrading && has_capability('mod/assign:manageallocations', $this->assignment->get_context()) &&
674 (empty($row->workflowstate) ||
675 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_INMARKING ||
676 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED)) {
678 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
679 return html_writer::select($markerlist, $name, $row->allocatedmarker, false);
680 } else if (!empty($row->allocatedmarker)) {
681 $output = '';
682 if ($this->quickgrading) { // Add hidden field for quickgrading page.
683 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
684 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$row->allocatedmarker));
686 $output .= $markerlist[$row->allocatedmarker];
687 return $output;
691 * For download only - list all the valid options for this custom scale.
693 * @param stdClass $row - The row of data
694 * @return string A list of valid options for the current scale
696 public function col_scale($row) {
697 global $DB;
699 if (empty($this->scale)) {
700 $dbparams = array('id'=>-($this->assignment->get_instance()->grade));
701 $this->scale = $DB->get_record('scale', $dbparams);
704 if (!empty($this->scale->scale)) {
705 return implode("\n", explode(',', $this->scale->scale));
707 return '';
711 * Display a grade with scales etc.
713 * @param string $grade
714 * @param boolean $editable
715 * @param int $userid The user id of the user this grade belongs to
716 * @param int $modified Timestamp showing when the grade was last modified
717 * @return string The formatted grade
719 public function display_grade($grade, $editable, $userid, $modified) {
720 if ($this->is_downloading()) {
721 if ($this->assignment->get_instance()->grade >= 0) {
722 if ($grade == -1 || $grade === null) {
723 return '';
725 $gradeitem = $this->assignment->get_grade_item();
726 return format_float($grade, $gradeitem->get_decimals());
727 } else {
728 // This is a custom scale.
729 $scale = $this->assignment->display_grade($grade, false);
730 if ($scale == '-') {
731 $scale = '';
733 return $scale;
736 return $this->assignment->display_grade($grade, $editable, $userid, $modified);
740 * Get the team info for this user.
742 * @param stdClass $row
743 * @return string The team name
745 public function col_team(stdClass $row) {
746 $submission = false;
747 $group = false;
748 $this->get_group_and_submission($row->id, $group, $submission, -1);
749 if ($group) {
750 return $group->name;
751 } else if ($this->assignment->get_instance()->preventsubmissionnotingroup) {
752 $usergroups = $this->assignment->get_all_groups($row->id);
753 if (count($usergroups) > 1) {
754 return get_string('multipleteamsgrader', 'assign');
755 } else {
756 return get_string('noteamgrader', 'assign');
759 return get_string('defaultteam', 'assign');
763 * Use a static cache to try and reduce DB calls.
765 * @param int $userid The user id for this submission
766 * @param int $group The groupid (returned)
767 * @param stdClass|false $submission The stdClass submission or false (returned)
768 * @param int $attemptnumber Return a specific attempt number (-1 for latest)
770 protected function get_group_and_submission($userid, &$group, &$submission, $attemptnumber) {
771 $group = false;
772 if (isset($this->submissiongroups[$userid])) {
773 $group = $this->submissiongroups[$userid];
774 } else {
775 $group = $this->assignment->get_submission_group($userid, false);
776 $this->submissiongroups[$userid] = $group;
779 $groupid = 0;
780 if ($group) {
781 $groupid = $group->id;
784 // Static cache is keyed by groupid and attemptnumber.
785 // We may need both the latest and previous attempt in the same page.
786 if (isset($this->groupsubmissions[$groupid . ':' . $attemptnumber])) {
787 $submission = $this->groupsubmissions[$groupid . ':' . $attemptnumber];
788 } else {
789 $submission = $this->assignment->get_group_submission($userid, $groupid, false, $attemptnumber);
790 $this->groupsubmissions[$groupid . ':' . $attemptnumber] = $submission;
795 * Format a list of outcomes.
797 * @param stdClass $row
798 * @return string
800 public function col_outcomes(stdClass $row) {
801 $outcomes = '';
802 foreach ($this->gradinginfo->outcomes as $index => $outcome) {
803 $options = make_grades_menu(-$outcome->scaleid);
805 $options[0] = get_string('nooutcome', 'grades');
806 if ($this->quickgrading && !($outcome->grades[$row->userid]->locked)) {
807 $select = '<select name="outcome_' . $index . '_' . $row->userid . '" class="quickgrade">';
808 foreach ($options as $optionindex => $optionvalue) {
809 $selected = '';
810 if ($outcome->grades[$row->userid]->grade == $optionindex) {
811 $selected = 'selected="selected"';
813 $select .= '<option value="' . $optionindex . '"' . $selected . '>' . $optionvalue . '</option>';
815 $select .= '</select>';
816 $outcomes .= $this->output->container($outcome->name . ': ' . $select, 'outcome');
817 } else {
818 $name = $outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade];
819 if ($this->is_downloading()) {
820 $outcomes .= $name;
821 } else {
822 $outcomes .= $this->output->container($name, 'outcome');
827 return $outcomes;
832 * Format a user picture for display.
834 * @param stdClass $row
835 * @return string
837 public function col_picture(stdClass $row) {
838 return $this->output->user_picture($row);
842 * Format a user record for display (link to profile).
844 * @param stdClass $row
845 * @return string
847 public function col_fullname($row) {
848 if (!$this->is_downloading()) {
849 $courseid = $this->assignment->get_course()->id;
850 $link= new moodle_url('/user/view.php', array('id' =>$row->id, 'course'=>$courseid));
851 $fullname = $this->output->action_link($link, $this->assignment->fullname($row));
852 } else {
853 $fullname = $this->assignment->fullname($row);
856 if (!$this->assignment->is_active_user($row->id)) {
857 $suspendedstring = get_string('userenrolmentsuspended', 'grades');
858 $fullname .= ' ' . $this->output->pix_icon('i/enrolmentsuspended', $suspendedstring);
859 $fullname = html_writer::tag('span', $fullname, array('class' => 'usersuspended'));
861 return $fullname;
865 * Insert a checkbox for selecting the current row for batch operations.
867 * @param stdClass $row
868 * @return string
870 public function col_select(stdClass $row) {
871 $selectcol = '<label class="accesshide" for="selectuser_' . $row->userid . '">';
872 $selectcol .= get_string('selectuser', 'assign', $this->assignment->fullname($row));
873 $selectcol .= '</label>';
874 $selectcol .= '<input type="checkbox"
875 id="selectuser_' . $row->userid . '"
876 name="selectedusers"
877 value="' . $row->userid . '"/>';
878 $selectcol .= '<input type="hidden"
879 name="grademodified_' . $row->userid . '"
880 value="' . $row->timemarked . '"/>';
881 $selectcol .= '<input type="hidden"
882 name="gradeattempt_' . $row->userid . '"
883 value="' . $row->attemptnumber . '"/>';
884 return $selectcol;
888 * Return a users grades from the listing of all grade data for this assignment.
890 * @param int $userid
891 * @return mixed stdClass or false
893 private function get_gradebook_data_for_user($userid) {
894 if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
895 return $this->gradinginfo->items[0]->grades[$userid];
897 return false;
901 * Format a column of data for display.
903 * @param stdClass $row
904 * @return string
906 public function col_gradecanbechanged(stdClass $row) {
907 $gradingdisabled = $this->assignment->grading_disabled($row->id);
908 if ($gradingdisabled) {
909 return get_string('no');
910 } else {
911 return get_string('yes');
916 * Format a column of data for display
918 * @param stdClass $row
919 * @return string
921 public function col_grademax(stdClass $row) {
922 $gradeitem = $this->assignment->get_grade_item();
923 return format_float($this->assignment->get_instance()->grade, $gradeitem->get_decimals());
927 * Format a column of data for display.
929 * @param stdClass $row
930 * @return string
932 public function col_grade(stdClass $row) {
933 $o = '';
935 $link = '';
936 $separator = $this->output->spacer(array(), true);
937 $grade = '';
938 $gradingdisabled = $this->assignment->grading_disabled($row->id);
940 if (!$this->is_downloading() && $this->hasgrade) {
941 $urlparams = array('id' => $this->assignment->get_course_module()->id,
942 'rownum' => 0,
943 'action' => 'grader');
945 if ($this->assignment->is_blind_marking()) {
946 if (empty($row->recordid)) {
947 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
949 $urlparams['blindid'] = $row->recordid;
950 } else {
951 $urlparams['userid'] = $row->userid;
954 $url = new moodle_url('/mod/assign/view.php', $urlparams);
955 $link = '<a href="' . $url . '" class="btn btn-primary">' . get_string('grade') . '</a>';
956 $grade .= $link . $separator;
959 $grade .= $this->display_grade($row->grade,
960 $this->quickgrading && !$gradingdisabled,
961 $row->userid,
962 $row->timemarked);
964 return $grade;
968 * Format a column of data for display.
970 * @param stdClass $row
971 * @return string
973 public function col_finalgrade(stdClass $row) {
974 $o = '';
976 $grade = $this->get_gradebook_data_for_user($row->userid);
977 if ($grade) {
978 $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
981 return $o;
985 * Format a column of data for display.
987 * @param stdClass $row
988 * @return string
990 public function col_timemarked(stdClass $row) {
991 $o = '-';
993 if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
994 $o = userdate($row->timemarked);
996 if ($row->timemarked && $this->is_downloading()) {
997 // Force it for downloads as it affects import.
998 $o = userdate($row->timemarked);
1001 return $o;
1005 * Format a column of data for display.
1007 * @param stdClass $row
1008 * @return string
1010 public function col_timesubmitted(stdClass $row) {
1011 $o = '-';
1013 $group = false;
1014 $submission = false;
1015 $this->get_group_and_submission($row->id, $group, $submission, -1);
1016 if ($submission && $submission->timemodified && $submission->status != ASSIGN_SUBMISSION_STATUS_NEW) {
1017 $o = userdate($submission->timemodified);
1018 } else if ($row->timesubmitted && $row->status != ASSIGN_SUBMISSION_STATUS_NEW) {
1019 $o = userdate($row->timesubmitted);
1022 return $o;
1026 * Format a column of data for display
1028 * @param stdClass $row
1029 * @return string
1031 public function col_status(stdClass $row) {
1032 $o = '';
1034 $instance = $this->assignment->get_instance();
1036 $due = $instance->duedate;
1037 if ($row->extensionduedate) {
1038 $due = $row->extensionduedate;
1039 } else if (!empty($row->duedate)) {
1040 // The override due date.
1041 $due = $row->duedate;
1044 $group = false;
1045 $submission = false;
1046 $this->get_group_and_submission($row->id, $group, $submission, -1);
1048 if ($instance->teamsubmission && !$group && !$instance->preventsubmissionnotingroup) {
1049 $group = true;
1052 if ($group && $submission) {
1053 $timesubmitted = $submission->timemodified;
1054 $status = $submission->status;
1055 } else {
1056 $timesubmitted = $row->timesubmitted;
1057 $status = $row->status;
1060 $displaystatus = $status;
1061 if ($displaystatus == 'new') {
1062 $displaystatus = '';
1065 if ($this->assignment->is_any_submission_plugin_enabled()) {
1067 $o .= $this->output->container(get_string('submissionstatus_' . $displaystatus, 'assign'),
1068 array('class'=>'submissionstatus' .$displaystatus));
1069 if ($due && $timesubmitted > $due && $status != ASSIGN_SUBMISSION_STATUS_NEW) {
1070 $usertime = format_time($timesubmitted - $due);
1071 $latemessage = get_string('submittedlateshort',
1072 'assign',
1073 $usertime);
1074 $o .= $this->output->container($latemessage, 'latesubmission');
1076 if ($row->locked) {
1077 $lockedstr = get_string('submissionslockedshort', 'assign');
1078 $o .= $this->output->container($lockedstr, 'lockedsubmission');
1081 // Add status of "grading" if markflow is not enabled.
1082 if (!$instance->markingworkflow) {
1083 if ($row->grade !== null && $row->grade >= 0) {
1084 $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
1085 } else if (!$timesubmitted || $status == ASSIGN_SUBMISSION_STATUS_NEW) {
1086 $now = time();
1087 if ($due && ($now > $due)) {
1088 $overduestr = get_string('overdue', 'assign', format_time($now - $due));
1089 $o .= $this->output->container($overduestr, 'overduesubmission');
1095 if ($instance->markingworkflow) {
1096 $o .= $this->col_workflowstatus($row);
1098 if ($row->extensionduedate) {
1099 $userdate = userdate($row->extensionduedate);
1100 $extensionstr = get_string('userextensiondate', 'assign', $userdate);
1101 $o .= $this->output->container($extensionstr, 'extensiondate');
1104 if ($this->is_downloading()) {
1105 $o = strip_tags(rtrim(str_replace('</div>', ' - ', $o), '- '));
1108 return $o;
1112 * Format a column of data for display.
1114 * @param stdClass $row
1115 * @return string
1117 public function col_allowsubmissionsfromdate(stdClass $row) {
1118 $o = '';
1120 if ($row->allowsubmissionsfromdate) {
1121 $userdate = userdate($row->allowsubmissionsfromdate);
1122 $o = $this->output->container($userdate, 'allowsubmissionsfromdate');
1125 return $o;
1130 * Format a column of data for display.
1132 * @param stdClass $row
1133 * @return string
1135 public function col_duedate(stdClass $row) {
1136 $o = '';
1138 if ($row->duedate) {
1139 $userdate = userdate($row->duedate);
1140 $o = $this->output->container($userdate, 'duedate');
1143 return $o;
1148 * Format a column of data for display.
1150 * @param stdClass $row
1151 * @return string
1153 public function col_cutoffdate(stdClass $row) {
1154 $o = '';
1156 if ($row->cutoffdate) {
1157 $userdate = userdate($row->cutoffdate);
1158 $o = $this->output->container($userdate, 'cutoffdate');
1161 return $o;
1166 * Format a column of data for display.
1168 * @param stdClass $row
1169 * @return string
1171 public function col_userid(stdClass $row) {
1172 global $USER;
1174 $edit = '';
1176 $actions = array();
1178 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1179 'rownum' => 0,
1180 'action' => 'grader');
1182 if ($this->assignment->is_blind_marking()) {
1183 if (empty($row->recordid)) {
1184 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
1186 $urlparams['blindid'] = $row->recordid;
1187 } else {
1188 $urlparams['userid'] = $row->userid;
1190 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1191 $noimage = null;
1193 if (!$row->grade) {
1194 $description = get_string('grade');
1195 } else {
1196 $description = get_string('updategrade', 'assign');
1198 $actions['grade'] = new action_menu_link_secondary(
1199 $url,
1200 $noimage,
1201 $description
1204 // Everything we need is in the row.
1205 $submission = $row;
1206 $flags = $row;
1207 if ($this->assignment->get_instance()->teamsubmission) {
1208 // Use the cache for this.
1209 $submission = false;
1210 $group = false;
1211 $this->get_group_and_submission($row->id, $group, $submission, -1);
1214 $submissionsopen = $this->assignment->submissions_open($row->id,
1215 true,
1216 $submission,
1217 $flags,
1218 $this->gradinginfo);
1219 $caneditsubmission = $this->assignment->can_edit_submission($row->id, $USER->id);
1221 // Hide for offline assignments.
1222 if ($this->assignment->is_any_submission_plugin_enabled()) {
1223 if (!$row->status ||
1224 $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
1225 !$this->assignment->get_instance()->submissiondrafts) {
1227 if (!$row->locked) {
1228 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1229 'userid'=>$row->id,
1230 'action'=>'lock',
1231 'sesskey'=>sesskey(),
1232 'page'=>$this->currpage);
1233 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1235 $description = get_string('preventsubmissionsshort', 'assign');
1236 $actions['lock'] = new action_menu_link_secondary(
1237 $url,
1238 $noimage,
1239 $description
1241 } else {
1242 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1243 'userid'=>$row->id,
1244 'action'=>'unlock',
1245 'sesskey'=>sesskey(),
1246 'page'=>$this->currpage);
1247 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1248 $description = get_string('allowsubmissionsshort', 'assign');
1249 $actions['unlock'] = new action_menu_link_secondary(
1250 $url,
1251 $noimage,
1252 $description
1257 if ($submissionsopen &&
1258 $USER->id != $row->id &&
1259 $caneditsubmission) {
1260 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1261 'userid'=>$row->id,
1262 'action'=>'editsubmission',
1263 'sesskey'=>sesskey(),
1264 'page'=>$this->currpage);
1265 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1266 $description = get_string('editsubmission', 'assign');
1267 $actions['editsubmission'] = new action_menu_link_secondary(
1268 $url,
1269 $noimage,
1270 $description
1274 if (($this->assignment->get_instance()->duedate ||
1275 $this->assignment->get_instance()->cutoffdate) &&
1276 $this->hasgrantextension) {
1277 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1278 'userid' => $row->id,
1279 'action' => 'grantextension',
1280 'sesskey' => sesskey(),
1281 'page' => $this->currpage);
1282 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1283 $description = get_string('grantextension', 'assign');
1284 $actions['grantextension'] = new action_menu_link_secondary(
1285 $url,
1286 $noimage,
1287 $description
1290 if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
1291 $this->assignment->get_instance()->submissiondrafts) {
1292 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1293 'userid'=>$row->id,
1294 'action'=>'reverttodraft',
1295 'sesskey'=>sesskey(),
1296 'page'=>$this->currpage);
1297 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1298 $description = get_string('reverttodraftshort', 'assign');
1299 $actions['reverttodraft'] = new action_menu_link_secondary(
1300 $url,
1301 $noimage,
1302 $description
1305 if ($row->status == ASSIGN_SUBMISSION_STATUS_DRAFT &&
1306 $this->assignment->get_instance()->submissiondrafts &&
1307 $caneditsubmission &&
1308 $submissionsopen &&
1309 $row->id != $USER->id) {
1310 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1311 'userid'=>$row->id,
1312 'action'=>'submitotherforgrading',
1313 'sesskey'=>sesskey(),
1314 'page'=>$this->currpage);
1315 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1316 $description = get_string('submitforgrading', 'assign');
1317 $actions['submitforgrading'] = new action_menu_link_secondary(
1318 $url,
1319 $noimage,
1320 $description
1324 $ismanual = $this->assignment->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
1325 $hassubmission = !empty($row->status);
1326 $notreopened = $hassubmission && $row->status != ASSIGN_SUBMISSION_STATUS_REOPENED;
1327 $isunlimited = $this->assignment->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS;
1328 $hasattempts = $isunlimited || $row->attemptnumber < $this->assignment->get_instance()->maxattempts - 1;
1330 if ($ismanual && $hassubmission && $notreopened && $hasattempts) {
1331 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1332 'userid'=>$row->id,
1333 'action'=>'addattempt',
1334 'sesskey'=>sesskey(),
1335 'page'=>$this->currpage);
1336 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1337 $description = get_string('addattempt', 'assign');
1338 $actions['addattempt'] = new action_menu_link_secondary(
1339 $url,
1340 $noimage,
1341 $description
1345 $menu = new action_menu();
1346 $menu->set_owner_selector('.gradingtable-actionmenu');
1347 $menu->set_alignment(action_menu::TL, action_menu::BL);
1348 $menu->set_constraint('.gradingtable > .no-overflow');
1349 $menu->set_menu_trigger(get_string('edit'));
1350 foreach ($actions as $action) {
1351 $menu->add($action);
1354 // Prioritise the menu ahead of all other actions.
1355 $menu->prioritise = true;
1357 $edit .= $this->output->render($menu);
1359 return $edit;
1363 * Write the plugin summary with an optional link to view the full feedback/submission.
1365 * @param assign_plugin $plugin Submission plugin or feedback plugin
1366 * @param stdClass $item Submission or grade
1367 * @param string $returnaction The return action to pass to the
1368 * view_submission page (the current page)
1369 * @param string $returnparams The return params to pass to the view_submission
1370 * page (the current page)
1371 * @return string The summary with an optional link
1373 private function format_plugin_summary_with_link(assign_plugin $plugin,
1374 stdClass $item,
1375 $returnaction,
1376 $returnparams) {
1377 $link = '';
1378 $showviewlink = false;
1380 $summary = $plugin->view_summary($item, $showviewlink);
1381 $separator = '';
1382 if ($showviewlink) {
1383 $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
1384 $icon = $this->output->pix_icon('t/preview', $viewstr);
1385 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1386 'sid'=>$item->id,
1387 'gid'=>$item->id,
1388 'plugin'=>$plugin->get_type(),
1389 'action'=>'viewplugin' . $plugin->get_subtype(),
1390 'returnaction'=>$returnaction,
1391 'returnparams'=>http_build_query($returnparams));
1392 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1393 $link = $this->output->action_link($url, $icon);
1394 $separator = $this->output->spacer(array(), true);
1397 return $link . $separator . $summary;
1402 * Format the submission and feedback columns.
1404 * @param string $colname The column name
1405 * @param stdClass $row The submission row
1406 * @return mixed string or NULL
1408 public function other_cols($colname, $row) {
1409 // For extra user fields the result is already in $row.
1410 if (empty($this->plugincache[$colname])) {
1411 return $row->$colname;
1414 // This must be a plugin field.
1415 $plugincache = $this->plugincache[$colname];
1417 $plugin = $plugincache[0];
1419 $field = null;
1420 if (isset($plugincache[1])) {
1421 $field = $plugincache[1];
1424 if ($plugin->is_visible() && $plugin->is_enabled()) {
1425 if ($plugin->get_subtype() == 'assignsubmission') {
1426 if ($this->assignment->get_instance()->teamsubmission) {
1427 $group = false;
1428 $submission = false;
1430 $this->get_group_and_submission($row->id, $group, $submission, -1);
1431 if ($submission) {
1432 if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1433 // For a newly reopened submission - we want to show the previous submission in the table.
1434 $this->get_group_and_submission($row->id, $group, $submission, $submission->attemptnumber-1);
1436 if (isset($field)) {
1437 return $plugin->get_editor_text($field, $submission->id);
1439 return $this->format_plugin_summary_with_link($plugin,
1440 $submission,
1441 'grading',
1442 array());
1444 } else if ($row->submissionid) {
1445 if ($row->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1446 // For a newly reopened submission - we want to show the previous submission in the table.
1447 $submission = $this->assignment->get_user_submission($row->userid, false, $row->attemptnumber - 1);
1448 } else {
1449 $submission = new stdClass();
1450 $submission->id = $row->submissionid;
1451 $submission->timecreated = $row->firstsubmission;
1452 $submission->timemodified = $row->timesubmitted;
1453 $submission->assignment = $this->assignment->get_instance()->id;
1454 $submission->userid = $row->userid;
1455 $submission->attemptnumber = $row->attemptnumber;
1457 // Field is used for only for import/export and refers the the fieldname for the text editor.
1458 if (isset($field)) {
1459 return $plugin->get_editor_text($field, $submission->id);
1461 return $this->format_plugin_summary_with_link($plugin,
1462 $submission,
1463 'grading',
1464 array());
1466 } else {
1467 $grade = null;
1468 if (isset($field)) {
1469 return $plugin->get_editor_text($field, $row->gradeid);
1472 if ($row->gradeid) {
1473 $grade = new stdClass();
1474 $grade->id = $row->gradeid;
1475 $grade->timecreated = $row->firstmarked;
1476 $grade->timemodified = $row->timemarked;
1477 $grade->assignment = $this->assignment->get_instance()->id;
1478 $grade->userid = $row->userid;
1479 $grade->grade = $row->grade;
1480 $grade->mailed = $row->mailed;
1481 $grade->attemptnumber = $row->attemptnumber;
1483 if ($this->quickgrading && $plugin->supports_quickgrading()) {
1484 return $plugin->get_quickgrading_html($row->userid, $grade);
1485 } else if ($grade) {
1486 return $this->format_plugin_summary_with_link($plugin,
1487 $grade,
1488 'grading',
1489 array());
1493 return '';
1497 * Using the current filtering and sorting - load all rows and return a single column from them.
1499 * @param string $columnname The name of the raw column data
1500 * @return array of data
1502 public function get_column_data($columnname) {
1503 $this->setup();
1504 $this->currpage = 0;
1505 $this->query_db($this->tablemaxrows);
1506 $result = array();
1507 foreach ($this->rawdata as $row) {
1508 $result[] = $row->$columnname;
1510 return $result;
1514 * Return things to the renderer.
1516 * @return string the assignment name
1518 public function get_assignment_name() {
1519 return $this->assignment->get_instance()->name;
1523 * Return things to the renderer.
1525 * @return int the course module id
1527 public function get_course_module_id() {
1528 return $this->assignment->get_course_module()->id;
1532 * Return things to the renderer.
1534 * @return int the course id
1536 public function get_course_id() {
1537 return $this->assignment->get_course()->id;
1541 * Return things to the renderer.
1543 * @return stdClass The course context
1545 public function get_course_context() {
1546 return $this->assignment->get_course_context();
1550 * Return things to the renderer.
1552 * @return bool Does this assignment accept submissions
1554 public function submissions_enabled() {
1555 return $this->assignment->is_any_submission_plugin_enabled();
1559 * Return things to the renderer.
1561 * @return bool Can this user view all grades (the gradebook)
1563 public function can_view_all_grades() {
1564 $context = $this->assignment->get_course_context();
1565 return has_capability('gradereport/grader:view', $context) &&
1566 has_capability('moodle/grade:viewall', $context);
1570 * Always return a valid sort - even if the userid column is missing.
1571 * @return array column name => SORT_... constant.
1573 public function get_sort_columns() {
1574 $result = parent::get_sort_columns();
1576 $assignment = $this->assignment->get_instance();
1577 if (empty($assignment->blindmarking)) {
1578 $result = array_merge($result, array('userid' => SORT_ASC));
1579 } else {
1580 $result = array_merge($result, [
1581 'COALESCE(s.timecreated, ' . time() . ')' => SORT_ASC,
1582 'COALESCE(s.id, ' . PHP_INT_MAX . ')' => SORT_ASC,
1583 'um.id' => SORT_ASC,
1586 return $result;
1590 * Override the table show_hide_link to not show for select column.
1592 * @param string $column the column name, index into various names.
1593 * @param int $index numerical index of the column.
1594 * @return string HTML fragment.
1596 protected function show_hide_link($column, $index) {
1597 if ($index > 0 || !$this->hasgrade) {
1598 return parent::show_hide_link($column, $index);
1600 return '';
1604 * Overides setup to ensure it will only run a single time.
1606 public function setup() {
1607 // Check if the setup function has been called before, we should not run it twice.
1608 // If we do the sortorder of the table will be broken.
1609 if (!empty($this->setup)) {
1610 return;
1612 parent::setup();