MDL-56062 mod_assign: Use $gradeitem->get_decimals() for grade display
[moodle.git] / mod / assign / gradingtable.php
blobe787658fbb11e69e2a003227acd3c52ead22dd6d
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 $extrauserfields = get_extra_user_fields($this->assignment->get_context());
136 $fields = user_picture::fields('u', $extrauserfields) . ', ';
137 $fields .= 'u.id as userid, ';
138 $fields .= 's.status as status, ';
139 $fields .= 's.id as submissionid, ';
140 $fields .= 's.timecreated as firstsubmission, ';
141 $fields .= 's.timemodified as timesubmitted, ';
142 $fields .= 's.attemptnumber as attemptnumber, ';
143 $fields .= 'g.id as gradeid, ';
144 $fields .= 'g.grade as grade, ';
145 $fields .= 'g.timemodified as timemarked, ';
146 $fields .= 'g.timecreated as firstmarked, ';
147 $fields .= 'uf.mailed as mailed, ';
148 $fields .= 'uf.locked as locked, ';
149 $fields .= 'uf.extensionduedate as extensionduedate, ';
150 $fields .= 'uf.workflowstate as workflowstate, ';
151 $fields .= 'uf.allocatedmarker as allocatedmarker ';
153 $from = '{user} u
154 LEFT JOIN {assign_submission} s
155 ON u.id = s.userid
156 AND s.assignment = :assignmentid1
157 AND s.latest = 1
158 LEFT JOIN {assign_grades} g
159 ON u.id = g.userid
160 AND g.assignment = :assignmentid2 ';
162 // For group submissions we don't immediately create an entry in the assign_submission table for each user,
163 // instead the userid is set to 0. In this case we use a different query to retrieve the grade for the user.
164 if ($this->assignment->get_instance()->teamsubmission) {
165 $params['assignmentid4'] = (int) $this->assignment->get_instance()->id;
166 $grademaxattempt = 'SELECT mxg.userid, MAX(mxg.attemptnumber) AS maxattempt
167 FROM {assign_grades} mxg
168 WHERE mxg.assignment = :assignmentid4
169 GROUP BY mxg.userid';
170 $from .= 'LEFT JOIN (' . $grademaxattempt . ') gmx
171 ON u.id = gmx.userid
172 AND g.attemptnumber = gmx.maxattempt ';
173 } else {
174 $from .= 'AND g.attemptnumber = s.attemptnumber ';
177 $from .= 'LEFT JOIN {assign_user_flags} uf
178 ON u.id = uf.userid
179 AND uf.assignment = :assignmentid3 ';
181 if (!empty($this->assignment->get_instance()->blindmarking)) {
182 $from .= 'LEFT JOIN {assign_user_mapping} um
183 ON u.id = um.userid
184 AND um.assignment = :assignmentid4 ';
185 $params['assignmentid4'] = (int)$this->assignment->get_instance()->id;
186 $fields .= ', um.id as recordid ';
189 $userparams = array();
190 $userindex = 0;
192 list($userwhere, $userparams) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
193 $where = 'u.id ' . $userwhere;
194 $params = array_merge($params, $userparams);
196 // The filters do not make sense when there are no submissions, so do not apply them.
197 if ($this->assignment->is_any_submission_plugin_enabled()) {
198 if ($filter == ASSIGN_FILTER_SUBMITTED) {
199 $where .= ' AND (s.timemodified IS NOT NULL AND
200 s.status = :submitted) ';
201 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
203 } else if ($filter == ASSIGN_FILTER_NOT_SUBMITTED) {
204 $where .= ' AND (s.timemodified IS NULL OR s.status != :submitted) ';
205 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
206 } else if ($filter == ASSIGN_FILTER_REQUIRE_GRADING) {
207 $where .= ' AND (s.timemodified IS NOT NULL AND
208 s.status = :submitted AND
209 (s.timemodified >= g.timemodified OR g.timemodified IS NULL OR g.grade IS NULL))';
210 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
212 } else if (strpos($filter, ASSIGN_FILTER_SINGLE_USER) === 0) {
213 $userfilter = (int) array_pop(explode('=', $filter));
214 $where .= ' AND (u.id = :userid)';
215 $params['userid'] = $userfilter;
219 if ($this->assignment->get_instance()->markingworkflow &&
220 $this->assignment->get_instance()->markingallocation) {
221 if (has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
222 // Check to see if marker filter is set.
223 $markerfilter = (int)get_user_preferences('assign_markerfilter', '');
224 if (!empty($markerfilter)) {
225 if ($markerfilter == ASSIGN_MARKER_FILTER_NO_MARKER) {
226 $where .= ' AND (uf.allocatedmarker IS NULL OR uf.allocatedmarker = 0)';
227 } else {
228 $where .= ' AND uf.allocatedmarker = :markerid';
229 $params['markerid'] = $markerfilter;
232 } else { // Only show users allocated to this marker.
233 $where .= ' AND uf.allocatedmarker = :markerid';
234 $params['markerid'] = $USER->id;
238 if ($this->assignment->get_instance()->markingworkflow) {
239 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
240 if (!empty($workflowstates)) {
241 $workflowfilter = get_user_preferences('assign_workflowfilter', '');
242 if ($workflowfilter == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED) {
243 $where .= ' AND (uf.workflowstate = :workflowstate OR uf.workflowstate IS NULL OR '.
244 $DB->sql_isempty('assign_user_flags', 'workflowstate', true, true).')';
245 $params['workflowstate'] = $workflowfilter;
246 } else if (array_key_exists($workflowfilter, $workflowstates)) {
247 $where .= ' AND uf.workflowstate = :workflowstate';
248 $params['workflowstate'] = $workflowfilter;
253 $this->set_sql($fields, $from, $where, $params);
255 if ($downloadfilename) {
256 $this->is_downloading('csv', $downloadfilename);
259 $columns = array();
260 $headers = array();
262 // Select.
263 if (!$this->is_downloading() && $this->hasgrade) {
264 $columns[] = 'select';
265 $headers[] = get_string('select') .
266 '<div class="selectall"><label class="accesshide" for="selectall">' . get_string('selectall') . '</label>
267 <input type="checkbox" id="selectall" name="selectall" title="' . get_string('selectall') . '"/></div>';
270 // User picture.
271 if ($this->hasviewblind || !$this->assignment->is_blind_marking()) {
272 if (!$this->is_downloading()) {
273 $columns[] = 'picture';
274 $headers[] = get_string('pictureofuser');
275 } else {
276 $columns[] = 'recordid';
277 $headers[] = get_string('recordid', 'assign');
280 // Fullname.
281 $columns[] = 'fullname';
282 $headers[] = get_string('fullname');
284 // Participant # details if can view real identities.
285 if ($this->assignment->is_blind_marking()) {
286 if (!$this->is_downloading()) {
287 $columns[] = 'recordid';
288 $headers[] = get_string('recordid', 'assign');
292 foreach ($extrauserfields as $extrafield) {
293 $columns[] = $extrafield;
294 $headers[] = get_user_field_name($extrafield);
296 } else {
297 // Record ID.
298 $columns[] = 'recordid';
299 $headers[] = get_string('recordid', 'assign');
302 // Submission status.
303 $columns[] = 'status';
304 $headers[] = get_string('status', 'assign');
306 // Team submission columns.
307 if ($assignment->get_instance()->teamsubmission) {
308 $columns[] = 'team';
309 $headers[] = get_string('submissionteam', 'assign');
311 // Allocated marker.
312 if ($this->assignment->get_instance()->markingworkflow &&
313 $this->assignment->get_instance()->markingallocation &&
314 has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
315 // Add a column for the allocated marker.
316 $columns[] = 'allocatedmarker';
317 $headers[] = get_string('marker', 'assign');
319 // Grade.
320 $columns[] = 'grade';
321 $headers[] = get_string('grade');
322 if ($this->is_downloading()) {
323 if ($this->assignment->get_instance()->grade >= 0) {
324 $columns[] = 'grademax';
325 $headers[] = get_string('maxgrade', 'assign');
326 } else {
327 // This is a custom scale.
328 $columns[] = 'scale';
329 $headers[] = get_string('scale', 'assign');
332 if ($this->assignment->get_instance()->markingworkflow) {
333 // Add a column for the marking workflow state.
334 $columns[] = 'workflowstate';
335 $headers[] = get_string('markingworkflowstate', 'assign');
337 // Add a column for the list of valid marking workflow states.
338 $columns[] = 'gradecanbechanged';
339 $headers[] = get_string('gradecanbechanged', 'assign');
341 if (!$this->is_downloading() && $this->hasgrade) {
342 // We have to call this column userid so we can use userid as a default sortable column.
343 $columns[] = 'userid';
344 $headers[] = get_string('edit');
347 // Submission plugins.
348 if ($assignment->is_any_submission_plugin_enabled()) {
349 $columns[] = 'timesubmitted';
350 $headers[] = get_string('lastmodifiedsubmission', 'assign');
352 foreach ($this->assignment->get_submission_plugins() as $plugin) {
353 if ($this->is_downloading()) {
354 if ($plugin->is_visible() && $plugin->is_enabled()) {
355 foreach ($plugin->get_editor_fields() as $field => $description) {
356 $index = 'plugin' . count($this->plugincache);
357 $this->plugincache[$index] = array($plugin, $field);
358 $columns[] = $index;
359 $headers[] = $plugin->get_name();
362 } else {
363 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
364 $index = 'plugin' . count($this->plugincache);
365 $this->plugincache[$index] = array($plugin);
366 $columns[] = $index;
367 $headers[] = $plugin->get_name();
373 // Time marked.
374 $columns[] = 'timemarked';
375 $headers[] = get_string('lastmodifiedgrade', 'assign');
377 // Feedback plugins.
378 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
379 if ($this->is_downloading()) {
380 if ($plugin->is_visible() && $plugin->is_enabled()) {
381 foreach ($plugin->get_editor_fields() as $field => $description) {
382 $index = 'plugin' . count($this->plugincache);
383 $this->plugincache[$index] = array($plugin, $field);
384 $columns[] = $index;
385 $headers[] = $description;
388 } else if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
389 $index = 'plugin' . count($this->plugincache);
390 $this->plugincache[$index] = array($plugin);
391 $columns[] = $index;
392 $headers[] = $plugin->get_name();
396 // Exclude 'Final grade' column in downloaded grading worksheets.
397 if (!$this->is_downloading()) {
398 // Final grade.
399 $columns[] = 'finalgrade';
400 $headers[] = get_string('finalgrade', 'grades');
403 // Load the grading info for all users.
404 $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id,
405 'mod',
406 'assign',
407 $this->assignment->get_instance()->id,
408 $users);
410 if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
411 $columns[] = 'outcomes';
412 $headers[] = get_string('outcomes', 'grades');
415 // Set the columns.
416 $this->define_columns($columns);
417 $this->define_headers($headers);
418 foreach ($extrauserfields as $extrafield) {
419 $this->column_class($extrafield, $extrafield);
421 $this->no_sorting('recordid');
422 $this->no_sorting('finalgrade');
423 $this->no_sorting('userid');
424 $this->no_sorting('select');
425 $this->no_sorting('outcomes');
427 if ($assignment->get_instance()->teamsubmission) {
428 $this->no_sorting('team');
431 $plugincolumnindex = 0;
432 foreach ($this->assignment->get_submission_plugins() as $plugin) {
433 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
434 $submissionpluginindex = 'plugin' . $plugincolumnindex++;
435 $this->no_sorting($submissionpluginindex);
438 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
439 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
440 $feedbackpluginindex = 'plugin' . $plugincolumnindex++;
441 $this->no_sorting($feedbackpluginindex);
445 // When there is no data we still want the column headers printed in the csv file.
446 if ($this->is_downloading()) {
447 $this->start_output();
452 * Before adding each row to the table make sure rownum is incremented.
454 * @param array $row row of data from db used to make one row of the table.
455 * @return array one row for the table
457 public function format_row($row) {
458 if ($this->rownum < 0) {
459 $this->rownum = $this->currpage * $this->pagesize;
460 } else {
461 $this->rownum += 1;
464 return parent::format_row($row);
468 * Add a column with an ID that uniquely identifies this user in this assignment.
470 * @param stdClass $row
471 * @return string
473 public function col_recordid(stdClass $row) {
474 if (empty($row->recordid)) {
475 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
477 return get_string('hiddenuser', 'assign') . $row->recordid;
482 * Add the userid to the row class so it can be updated via ajax.
484 * @param stdClass $row The row of data
485 * @return string The row class
487 public function get_row_class($row) {
488 return 'user' . $row->userid;
492 * Return the number of rows to display on a single page.
494 * @return int The number of rows per page
496 public function get_rows_per_page() {
497 return $this->perpage;
501 * list current marking workflow state
503 * @param stdClass $row
504 * @return string
506 public function col_workflowstatus(stdClass $row) {
507 $o = '';
509 $gradingdisabled = $this->assignment->grading_disabled($row->id);
510 // The function in the assignment keeps a static cache of this list of states.
511 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
512 $workflowstate = $row->workflowstate;
513 if (empty($workflowstate)) {
514 $workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
516 if ($this->quickgrading && !$gradingdisabled) {
517 $notmarked = get_string('markingworkflowstatenotmarked', 'assign');
518 $name = 'quickgrade_' . $row->id . '_workflowstate';
519 $o .= html_writer::select($workflowstates, $name, $workflowstate, array('' => $notmarked));
520 // Check if this user is a marker that can't manage allocations and doesn't have the marker column added.
521 if ($this->assignment->get_instance()->markingworkflow &&
522 $this->assignment->get_instance()->markingallocation &&
523 !has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
525 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
526 $o .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $name,
527 'value' => $row->allocatedmarker));
529 } else {
530 $o .= $this->output->container(get_string('markingworkflowstate' . $workflowstate, 'assign'), $workflowstate);
532 return $o;
536 * For download only - list current marking workflow state
538 * @param stdClass $row - The row of data
539 * @return string The current marking workflow state
541 public function col_workflowstate($row) {
542 $state = $row->workflowstate;
543 if (empty($state)) {
544 $state = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
547 return get_string('markingworkflowstate' . $state, 'assign');
551 * list current marker
553 * @param stdClass $row - The row of data
554 * @return id the user->id of the marker.
556 public function col_allocatedmarker(stdClass $row) {
557 static $markers = null;
558 static $markerlist = array();
559 if ($markers === null) {
560 list($sort, $params) = users_order_by_sql();
561 $markers = get_users_by_capability($this->assignment->get_context(), 'mod/assign:grade', '', $sort);
562 $markerlist[0] = get_string('choosemarker', 'assign');
563 foreach ($markers as $marker) {
564 $markerlist[$marker->id] = fullname($marker);
567 if (empty($markerlist)) {
568 // TODO: add some form of notification here that no markers are available.
569 return '';
571 if ($this->is_downloading()) {
572 if (isset($markers[$row->allocatedmarker])) {
573 return fullname($markers[$row->allocatedmarker]);
574 } else {
575 return '';
579 if ($this->quickgrading && has_capability('mod/assign:manageallocations', $this->assignment->get_context()) &&
580 (empty($row->workflowstate) ||
581 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_INMARKING ||
582 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED)) {
584 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
585 return html_writer::select($markerlist, $name, $row->allocatedmarker, false);
586 } else if (!empty($row->allocatedmarker)) {
587 $output = '';
588 if ($this->quickgrading) { // Add hidden field for quickgrading page.
589 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
590 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$row->allocatedmarker));
592 $output .= $markerlist[$row->allocatedmarker];
593 return $output;
597 * For download only - list all the valid options for this custom scale.
599 * @param stdClass $row - The row of data
600 * @return string A list of valid options for the current scale
602 public function col_scale($row) {
603 global $DB;
605 if (empty($this->scale)) {
606 $dbparams = array('id'=>-($this->assignment->get_instance()->grade));
607 $this->scale = $DB->get_record('scale', $dbparams);
610 if (!empty($this->scale->scale)) {
611 return implode("\n", explode(',', $this->scale->scale));
613 return '';
617 * Display a grade with scales etc.
619 * @param string $grade
620 * @param boolean $editable
621 * @param int $userid The user id of the user this grade belongs to
622 * @param int $modified Timestamp showing when the grade was last modified
623 * @return string The formatted grade
625 public function display_grade($grade, $editable, $userid, $modified) {
626 if ($this->is_downloading()) {
627 if ($this->assignment->get_instance()->grade >= 0) {
628 if ($grade == -1 || $grade === null) {
629 return '';
631 $gradeitem = $this->assignment->get_grade_item();
632 return format_float($grade, $gradeitem->get_decimals());
633 } else {
634 // This is a custom scale.
635 $scale = $this->assignment->display_grade($grade, false);
636 if ($scale == '-') {
637 $scale = '';
639 return $scale;
642 return $this->assignment->display_grade($grade, $editable, $userid, $modified);
646 * Get the team info for this user.
648 * @param stdClass $row
649 * @return string The team name
651 public function col_team(stdClass $row) {
652 $submission = false;
653 $group = false;
654 $this->get_group_and_submission($row->id, $group, $submission, -1);
655 if ($group) {
656 return $group->name;
657 } else if ($this->assignment->get_instance()->preventsubmissionnotingroup) {
658 $usergroups = $this->assignment->get_all_groups($row->id);
659 if (count($usergroups) > 1) {
660 return get_string('multipleteamsgrader', 'assign');
661 } else {
662 return get_string('noteamgrader', 'assign');
665 return get_string('defaultteam', 'assign');
669 * Use a static cache to try and reduce DB calls.
671 * @param int $userid The user id for this submission
672 * @param int $group The groupid (returned)
673 * @param stdClass|false $submission The stdClass submission or false (returned)
674 * @param int $attemptnumber Return a specific attempt number (-1 for latest)
676 protected function get_group_and_submission($userid, &$group, &$submission, $attemptnumber) {
677 $group = false;
678 if (isset($this->submissiongroups[$userid])) {
679 $group = $this->submissiongroups[$userid];
680 } else {
681 $group = $this->assignment->get_submission_group($userid, false);
682 $this->submissiongroups[$userid] = $group;
685 $groupid = 0;
686 if ($group) {
687 $groupid = $group->id;
690 // Static cache is keyed by groupid and attemptnumber.
691 // We may need both the latest and previous attempt in the same page.
692 if (isset($this->groupsubmissions[$groupid . ':' . $attemptnumber])) {
693 $submission = $this->groupsubmissions[$groupid . ':' . $attemptnumber];
694 } else {
695 $submission = $this->assignment->get_group_submission($userid, $groupid, false, $attemptnumber);
696 $this->groupsubmissions[$groupid . ':' . $attemptnumber] = $submission;
701 * Format a list of outcomes.
703 * @param stdClass $row
704 * @return string
706 public function col_outcomes(stdClass $row) {
707 $outcomes = '';
708 foreach ($this->gradinginfo->outcomes as $index => $outcome) {
709 $options = make_grades_menu(-$outcome->scaleid);
711 $options[0] = get_string('nooutcome', 'grades');
712 if ($this->quickgrading && !($outcome->grades[$row->userid]->locked)) {
713 $select = '<select name="outcome_' . $index . '_' . $row->userid . '" class="quickgrade">';
714 foreach ($options as $optionindex => $optionvalue) {
715 $selected = '';
716 if ($outcome->grades[$row->userid]->grade == $optionindex) {
717 $selected = 'selected="selected"';
719 $select .= '<option value="' . $optionindex . '"' . $selected . '>' . $optionvalue . '</option>';
721 $select .= '</select>';
722 $outcomes .= $this->output->container($outcome->name . ': ' . $select, 'outcome');
723 } else {
724 $name = $outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade];
725 if ($this->is_downloading()) {
726 $outcomes .= $name;
727 } else {
728 $outcomes .= $this->output->container($name, 'outcome');
733 return $outcomes;
738 * Format a user picture for display.
740 * @param stdClass $row
741 * @return string
743 public function col_picture(stdClass $row) {
744 return $this->output->user_picture($row);
748 * Format a user record for display (link to profile).
750 * @param stdClass $row
751 * @return string
753 public function col_fullname($row) {
754 if (!$this->is_downloading()) {
755 $courseid = $this->assignment->get_course()->id;
756 $link= new moodle_url('/user/view.php', array('id' =>$row->id, 'course'=>$courseid));
757 $fullname = $this->output->action_link($link, $this->assignment->fullname($row));
758 } else {
759 $fullname = $this->assignment->fullname($row);
762 if (!$this->assignment->is_active_user($row->id)) {
763 $suspendedstring = get_string('userenrolmentsuspended', 'grades');
764 $fullname .= ' ' . html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/enrolmentsuspended'),
765 'title' => $suspendedstring, 'alt' => $suspendedstring, 'class' => 'usersuspendedicon'));
766 $fullname = html_writer::tag('span', $fullname, array('class' => 'usersuspended'));
768 return $fullname;
772 * Insert a checkbox for selecting the current row for batch operations.
774 * @param stdClass $row
775 * @return string
777 public function col_select(stdClass $row) {
778 $selectcol = '<label class="accesshide" for="selectuser_' . $row->userid . '">';
779 $selectcol .= get_string('selectuser', 'assign', $this->assignment->fullname($row));
780 $selectcol .= '</label>';
781 $selectcol .= '<input type="checkbox"
782 id="selectuser_' . $row->userid . '"
783 name="selectedusers"
784 value="' . $row->userid . '"/>';
785 $selectcol .= '<input type="hidden"
786 name="grademodified_' . $row->userid . '"
787 value="' . $row->timemarked . '"/>';
788 $selectcol .= '<input type="hidden"
789 name="gradeattempt_' . $row->userid . '"
790 value="' . $row->attemptnumber . '"/>';
791 return $selectcol;
795 * Return a users grades from the listing of all grade data for this assignment.
797 * @param int $userid
798 * @return mixed stdClass or false
800 private function get_gradebook_data_for_user($userid) {
801 if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
802 return $this->gradinginfo->items[0]->grades[$userid];
804 return false;
808 * Format a column of data for display.
810 * @param stdClass $row
811 * @return string
813 public function col_gradecanbechanged(stdClass $row) {
814 $gradingdisabled = $this->assignment->grading_disabled($row->id);
815 if ($gradingdisabled) {
816 return get_string('no');
817 } else {
818 return get_string('yes');
823 * Format a column of data for display
825 * @param stdClass $row
826 * @return string
828 public function col_grademax(stdClass $row) {
829 $gradeitem = $this->assignment->get_grade_item();
830 return format_float($this->assignment->get_instance()->grade, $gradeitem->get_decimals());
834 * Format a column of data for display.
836 * @param stdClass $row
837 * @return string
839 public function col_grade(stdClass $row) {
840 $o = '';
842 $link = '';
843 $separator = $this->output->spacer(array(), true);
844 $grade = '';
845 $gradingdisabled = $this->assignment->grading_disabled($row->id);
847 if (!$this->is_downloading() && $this->hasgrade) {
848 $urlparams = array('id' => $this->assignment->get_course_module()->id,
849 'rownum' => 0,
850 'action' => 'grader');
852 if ($this->assignment->is_blind_marking()) {
853 if (empty($row->recordid)) {
854 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
856 $urlparams['blindid'] = $row->recordid;
857 } else {
858 $urlparams['userid'] = $row->userid;
861 $url = new moodle_url('/mod/assign/view.php', $urlparams);
862 $link = '<a href="' . $url . '" class="btn btn-primary">' . get_string('grade') . '</a>';
863 $grade .= $link . $separator;
866 $grade .= $this->display_grade($row->grade,
867 $this->quickgrading && !$gradingdisabled,
868 $row->userid,
869 $row->timemarked);
871 return $grade;
875 * Format a column of data for display.
877 * @param stdClass $row
878 * @return string
880 public function col_finalgrade(stdClass $row) {
881 $o = '';
883 $grade = $this->get_gradebook_data_for_user($row->userid);
884 if ($grade) {
885 $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
888 return $o;
892 * Format a column of data for display.
894 * @param stdClass $row
895 * @return string
897 public function col_timemarked(stdClass $row) {
898 $o = '-';
900 if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
901 $o = userdate($row->timemarked);
903 if ($row->timemarked && $this->is_downloading()) {
904 // Force it for downloads as it affects import.
905 $o = userdate($row->timemarked);
908 return $o;
912 * Format a column of data for display.
914 * @param stdClass $row
915 * @return string
917 public function col_timesubmitted(stdClass $row) {
918 $o = '-';
920 $group = false;
921 $submission = false;
922 $this->get_group_and_submission($row->id, $group, $submission, -1);
923 if ($submission && $submission->timemodified && $submission->status != ASSIGN_SUBMISSION_STATUS_NEW) {
924 $o = userdate($submission->timemodified);
925 } else if ($row->timesubmitted && $row->status != ASSIGN_SUBMISSION_STATUS_NEW) {
926 $o = userdate($row->timesubmitted);
929 return $o;
933 * Format a column of data for display
935 * @param stdClass $row
936 * @return string
938 public function col_status(stdClass $row) {
939 $o = '';
941 $instance = $this->assignment->get_instance();
943 $due = $instance->duedate;
944 if ($row->extensionduedate) {
945 $due = $row->extensionduedate;
948 $group = false;
949 $submission = false;
950 $this->get_group_and_submission($row->id, $group, $submission, -1);
952 if ($instance->teamsubmission && !$group && !$instance->preventsubmissionnotingroup) {
953 $group = true;
956 if ($group && $submission) {
957 $timesubmitted = $submission->timemodified;
958 $status = $submission->status;
959 } else {
960 $timesubmitted = $row->timesubmitted;
961 $status = $row->status;
964 $displaystatus = $status;
965 if ($displaystatus == 'new') {
966 $displaystatus = '';
969 if ($this->assignment->is_any_submission_plugin_enabled()) {
971 $o .= $this->output->container(get_string('submissionstatus_' . $displaystatus, 'assign'),
972 array('class'=>'submissionstatus' .$displaystatus));
973 if ($due && $timesubmitted > $due && $status != ASSIGN_SUBMISSION_STATUS_NEW) {
974 $usertime = format_time($timesubmitted - $due);
975 $latemessage = get_string('submittedlateshort',
976 'assign',
977 $usertime);
978 $o .= $this->output->container($latemessage, 'latesubmission');
980 if ($row->locked) {
981 $lockedstr = get_string('submissionslockedshort', 'assign');
982 $o .= $this->output->container($lockedstr, 'lockedsubmission');
985 // Add status of "grading" if markflow is not enabled.
986 if (!$instance->markingworkflow) {
987 if ($row->grade !== null && $row->grade >= 0) {
988 $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
989 } else if (!$timesubmitted || $status == ASSIGN_SUBMISSION_STATUS_NEW) {
990 $now = time();
991 if ($due && ($now > $due)) {
992 $overduestr = get_string('overdue', 'assign', format_time($now - $due));
993 $o .= $this->output->container($overduestr, 'overduesubmission');
999 if ($instance->markingworkflow) {
1000 $o .= $this->col_workflowstatus($row);
1002 if ($row->extensionduedate) {
1003 $userdate = userdate($row->extensionduedate);
1004 $extensionstr = get_string('userextensiondate', 'assign', $userdate);
1005 $o .= $this->output->container($extensionstr, 'extensiondate');
1008 if ($this->is_downloading()) {
1009 $o = strip_tags(rtrim(str_replace('</div>', ' - ', $o), '- '));
1012 return $o;
1016 * Format a column of data for display.
1018 * @param stdClass $row
1019 * @return string
1021 public function col_userid(stdClass $row) {
1022 global $USER;
1024 $edit = '';
1026 $actions = array();
1028 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1029 'rownum' => 0,
1030 'action' => 'grader');
1032 if ($this->assignment->is_blind_marking()) {
1033 if (empty($row->recordid)) {
1034 $row->recordid = $this->assignment->get_uniqueid_for_user($row->userid);
1036 $urlparams['blindid'] = $row->recordid;
1037 } else {
1038 $urlparams['userid'] = $row->userid;
1040 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1041 $noimage = null;
1043 if (!$row->grade) {
1044 $description = get_string('grade');
1045 } else {
1046 $description = get_string('updategrade', 'assign');
1048 $actions['grade'] = new action_menu_link_secondary(
1049 $url,
1050 $noimage,
1051 $description
1054 // Everything we need is in the row.
1055 $submission = $row;
1056 $flags = $row;
1057 if ($this->assignment->get_instance()->teamsubmission) {
1058 // Use the cache for this.
1059 $submission = false;
1060 $group = false;
1061 $this->get_group_and_submission($row->id, $group, $submission, -1);
1064 $submissionsopen = $this->assignment->submissions_open($row->id,
1065 true,
1066 $submission,
1067 $flags,
1068 $this->gradinginfo);
1069 $caneditsubmission = $this->assignment->can_edit_submission($row->id, $USER->id);
1071 // Hide for offline assignments.
1072 if ($this->assignment->is_any_submission_plugin_enabled()) {
1073 if (!$row->status ||
1074 $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
1075 !$this->assignment->get_instance()->submissiondrafts) {
1077 if (!$row->locked) {
1078 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1079 'userid'=>$row->id,
1080 'action'=>'lock',
1081 'sesskey'=>sesskey(),
1082 'page'=>$this->currpage);
1083 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1085 $description = get_string('preventsubmissionsshort', 'assign');
1086 $actions['lock'] = new action_menu_link_secondary(
1087 $url,
1088 $noimage,
1089 $description
1091 } else {
1092 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1093 'userid'=>$row->id,
1094 'action'=>'unlock',
1095 'sesskey'=>sesskey(),
1096 'page'=>$this->currpage);
1097 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1098 $description = get_string('allowsubmissionsshort', 'assign');
1099 $actions['unlock'] = new action_menu_link_secondary(
1100 $url,
1101 $noimage,
1102 $description
1107 if ($submissionsopen &&
1108 $USER->id != $row->id &&
1109 $caneditsubmission) {
1110 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1111 'userid'=>$row->id,
1112 'action'=>'editsubmission',
1113 'sesskey'=>sesskey(),
1114 'page'=>$this->currpage);
1115 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1116 $description = get_string('editsubmission', 'assign');
1117 $actions['editsubmission'] = new action_menu_link_secondary(
1118 $url,
1119 $noimage,
1120 $description
1124 if (($this->assignment->get_instance()->duedate ||
1125 $this->assignment->get_instance()->cutoffdate) &&
1126 $this->hasgrantextension) {
1127 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1128 'userid' => $row->id,
1129 'action' => 'grantextension',
1130 'sesskey' => sesskey(),
1131 'page' => $this->currpage);
1132 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1133 $description = get_string('grantextension', 'assign');
1134 $actions['grantextension'] = new action_menu_link_secondary(
1135 $url,
1136 $noimage,
1137 $description
1140 if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
1141 $this->assignment->get_instance()->submissiondrafts) {
1142 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1143 'userid'=>$row->id,
1144 'action'=>'reverttodraft',
1145 'sesskey'=>sesskey(),
1146 'page'=>$this->currpage);
1147 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1148 $description = get_string('reverttodraftshort', 'assign');
1149 $actions['reverttodraft'] = new action_menu_link_secondary(
1150 $url,
1151 $noimage,
1152 $description
1155 if ($row->status == ASSIGN_SUBMISSION_STATUS_DRAFT &&
1156 $this->assignment->get_instance()->submissiondrafts &&
1157 $caneditsubmission &&
1158 $submissionsopen &&
1159 $row->id != $USER->id) {
1160 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1161 'userid'=>$row->id,
1162 'action'=>'submitotherforgrading',
1163 'sesskey'=>sesskey(),
1164 'page'=>$this->currpage);
1165 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1166 $description = get_string('submitforgrading', 'assign');
1167 $actions['submitforgrading'] = new action_menu_link_secondary(
1168 $url,
1169 $noimage,
1170 $description
1174 $ismanual = $this->assignment->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
1175 $hassubmission = !empty($row->status);
1176 $notreopened = $hassubmission && $row->status != ASSIGN_SUBMISSION_STATUS_REOPENED;
1177 $isunlimited = $this->assignment->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS;
1178 $hasattempts = $isunlimited || $row->attemptnumber < $this->assignment->get_instance()->maxattempts - 1;
1180 if ($ismanual && $hassubmission && $notreopened && $hasattempts) {
1181 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1182 'userid'=>$row->id,
1183 'action'=>'addattempt',
1184 'sesskey'=>sesskey(),
1185 'page'=>$this->currpage);
1186 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1187 $description = get_string('addattempt', 'assign');
1188 $actions['addattempt'] = new action_menu_link_secondary(
1189 $url,
1190 $noimage,
1191 $description
1195 $menu = new action_menu();
1196 $menu->set_owner_selector('.gradingtable-actionmenu');
1197 $menu->set_alignment(action_menu::TL, action_menu::BL);
1198 $menu->set_constraint('.gradingtable > .no-overflow');
1199 $menu->set_menu_trigger(get_string('edit'));
1200 foreach ($actions as $action) {
1201 $menu->add($action);
1204 // Prioritise the menu ahead of all other actions.
1205 $menu->prioritise = true;
1207 $edit .= $this->output->render($menu);
1209 return $edit;
1213 * Write the plugin summary with an optional link to view the full feedback/submission.
1215 * @param assign_plugin $plugin Submission plugin or feedback plugin
1216 * @param stdClass $item Submission or grade
1217 * @param string $returnaction The return action to pass to the
1218 * view_submission page (the current page)
1219 * @param string $returnparams The return params to pass to the view_submission
1220 * page (the current page)
1221 * @return string The summary with an optional link
1223 private function format_plugin_summary_with_link(assign_plugin $plugin,
1224 stdClass $item,
1225 $returnaction,
1226 $returnparams) {
1227 $link = '';
1228 $showviewlink = false;
1230 $summary = $plugin->view_summary($item, $showviewlink);
1231 $separator = '';
1232 if ($showviewlink) {
1233 $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
1234 $icon = $this->output->pix_icon('t/preview', $viewstr);
1235 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1236 'sid'=>$item->id,
1237 'gid'=>$item->id,
1238 'plugin'=>$plugin->get_type(),
1239 'action'=>'viewplugin' . $plugin->get_subtype(),
1240 'returnaction'=>$returnaction,
1241 'returnparams'=>http_build_query($returnparams));
1242 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1243 $link = $this->output->action_link($url, $icon);
1244 $separator = $this->output->spacer(array(), true);
1247 return $link . $separator . $summary;
1252 * Format the submission and feedback columns.
1254 * @param string $colname The column name
1255 * @param stdClass $row The submission row
1256 * @return mixed string or NULL
1258 public function other_cols($colname, $row) {
1259 // For extra user fields the result is already in $row.
1260 if (empty($this->plugincache[$colname])) {
1261 return $row->$colname;
1264 // This must be a plugin field.
1265 $plugincache = $this->plugincache[$colname];
1267 $plugin = $plugincache[0];
1269 $field = null;
1270 if (isset($plugincache[1])) {
1271 $field = $plugincache[1];
1274 if ($plugin->is_visible() && $plugin->is_enabled()) {
1275 if ($plugin->get_subtype() == 'assignsubmission') {
1276 if ($this->assignment->get_instance()->teamsubmission) {
1277 $group = false;
1278 $submission = false;
1280 $this->get_group_and_submission($row->id, $group, $submission, -1);
1281 if ($submission) {
1282 if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1283 // For a newly reopened submission - we want to show the previous submission in the table.
1284 $this->get_group_and_submission($row->id, $group, $submission, $submission->attemptnumber-1);
1286 if (isset($field)) {
1287 return $plugin->get_editor_text($field, $submission->id);
1289 return $this->format_plugin_summary_with_link($plugin,
1290 $submission,
1291 'grading',
1292 array());
1294 } else if ($row->submissionid) {
1295 if ($row->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1296 // For a newly reopened submission - we want to show the previous submission in the table.
1297 $submission = $this->assignment->get_user_submission($row->userid, false, $row->attemptnumber - 1);
1298 } else {
1299 $submission = new stdClass();
1300 $submission->id = $row->submissionid;
1301 $submission->timecreated = $row->firstsubmission;
1302 $submission->timemodified = $row->timesubmitted;
1303 $submission->assignment = $this->assignment->get_instance()->id;
1304 $submission->userid = $row->userid;
1305 $submission->attemptnumber = $row->attemptnumber;
1307 // Field is used for only for import/export and refers the the fieldname for the text editor.
1308 if (isset($field)) {
1309 return $plugin->get_editor_text($field, $submission->id);
1311 return $this->format_plugin_summary_with_link($plugin,
1312 $submission,
1313 'grading',
1314 array());
1316 } else {
1317 $grade = null;
1318 if (isset($field)) {
1319 return $plugin->get_editor_text($field, $row->gradeid);
1322 if ($row->gradeid) {
1323 $grade = new stdClass();
1324 $grade->id = $row->gradeid;
1325 $grade->timecreated = $row->firstmarked;
1326 $grade->timemodified = $row->timemarked;
1327 $grade->assignment = $this->assignment->get_instance()->id;
1328 $grade->userid = $row->userid;
1329 $grade->grade = $row->grade;
1330 $grade->mailed = $row->mailed;
1331 $grade->attemptnumber = $row->attemptnumber;
1333 if ($this->quickgrading && $plugin->supports_quickgrading()) {
1334 return $plugin->get_quickgrading_html($row->userid, $grade);
1335 } else if ($grade) {
1336 return $this->format_plugin_summary_with_link($plugin,
1337 $grade,
1338 'grading',
1339 array());
1343 return '';
1347 * Using the current filtering and sorting - load all rows and return a single column from them.
1349 * @param string $columnname The name of the raw column data
1350 * @return array of data
1352 public function get_column_data($columnname) {
1353 $this->setup();
1354 $this->currpage = 0;
1355 $this->query_db($this->tablemaxrows);
1356 $result = array();
1357 foreach ($this->rawdata as $row) {
1358 $result[] = $row->$columnname;
1360 return $result;
1364 * Return things to the renderer.
1366 * @return string the assignment name
1368 public function get_assignment_name() {
1369 return $this->assignment->get_instance()->name;
1373 * Return things to the renderer.
1375 * @return int the course module id
1377 public function get_course_module_id() {
1378 return $this->assignment->get_course_module()->id;
1382 * Return things to the renderer.
1384 * @return int the course id
1386 public function get_course_id() {
1387 return $this->assignment->get_course()->id;
1391 * Return things to the renderer.
1393 * @return stdClass The course context
1395 public function get_course_context() {
1396 return $this->assignment->get_course_context();
1400 * Return things to the renderer.
1402 * @return bool Does this assignment accept submissions
1404 public function submissions_enabled() {
1405 return $this->assignment->is_any_submission_plugin_enabled();
1409 * Return things to the renderer.
1411 * @return bool Can this user view all grades (the gradebook)
1413 public function can_view_all_grades() {
1414 $context = $this->assignment->get_course_context();
1415 return has_capability('gradereport/grader:view', $context) &&
1416 has_capability('moodle/grade:viewall', $context);
1420 * Always return a valid sort - even if the userid column is missing.
1421 * @return array column name => SORT_... constant.
1423 public function get_sort_columns() {
1424 $result = parent::get_sort_columns();
1426 $assignment = $this->assignment->get_instance();
1427 if (empty($assignment->blindmarking)) {
1428 $result = array_merge($result, array('userid' => SORT_ASC));
1429 } else {
1430 $result = array_merge($result, [
1431 'COALESCE(s.timecreated, ' . time() . ')' => SORT_ASC,
1432 'COALESCE(s.id, ' . PHP_INT_MAX . ')' => SORT_ASC,
1433 'um.id' => SORT_ASC,
1436 return $result;
1440 * Override the table show_hide_link to not show for select column.
1442 * @param string $column the column name, index into various names.
1443 * @param int $index numerical index of the column.
1444 * @return string HTML fragment.
1446 protected function show_hide_link($column, $index) {
1447 if ($index > 0 || !$this->hasgrade) {
1448 return parent::show_hide_link($column, $index);
1450 return '';
1454 * Overides setup to ensure it will only run a single time.
1456 public function setup() {
1457 // Check if the setup function has been called before, we should not run it twice.
1458 // If we do the sortorder of the table will be broken.
1459 if (!empty($this->setup)) {
1460 return;
1462 parent::setup();