MDL-48778 assign: Use proper latest attempt for quickgrading prechecks
[moodle.git] / mod / assign / gradingtable.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * This file contains the definition for the grading table which subclassses easy_table
19  *
20  * @package   mod_assign
21  * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
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
33  *
34  * @package   mod_assign
35  * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
36  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  */
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
70      *
71      * @param assign $assignment The assignment class
72      * @param int $perpage how many per page
73      * @param string $filter The current filter
74      * @param int $rowoffset For showing a subsequent page of results
75      * @param bool $quickgrading Is this table wrapped in a quickgrading form?
76      * @param string $downloadfilename
77      */
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();
103                     }
104                     $this->plugingradingbatchoperations[$plugin->get_type()][$action] = $description;
105                 }
106             }
107         }
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;
121         }
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;
127         }
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 ';
175         }
177         $from .= 'LEFT JOIN {assign_user_flags} uf
178                          ON u.id = uf.userid
179                         AND uf.assignment = :assignmentid3';
181         $userparams = array();
182         $userindex = 0;
184         list($userwhere, $userparams) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
185         $where = 'u.id ' . $userwhere;
186         $params = array_merge($params, $userparams);
188         // The filters do not make sense when there are no submissions, so do not apply them.
189         if ($this->assignment->is_any_submission_plugin_enabled()) {
190             if ($filter == ASSIGN_FILTER_SUBMITTED) {
191                 $where .= ' AND (s.timemodified IS NOT NULL AND
192                                  s.status = :submitted) ';
193                 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
195             } else if ($filter == ASSIGN_FILTER_NOT_SUBMITTED) {
196                 $where .= ' AND (s.timemodified IS NULL OR s.status != :submitted) ';
197                 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
198             } else if ($filter == ASSIGN_FILTER_REQUIRE_GRADING) {
199                 $where .= ' AND (s.timemodified IS NOT NULL AND
200                                  s.status = :submitted AND
201                                  (s.timemodified >= g.timemodified OR g.timemodified IS NULL OR g.grade IS NULL))';
202                 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
204             } else if (strpos($filter, ASSIGN_FILTER_SINGLE_USER) === 0) {
205                 $userfilter = (int) array_pop(explode('=', $filter));
206                 $where .= ' AND (u.id = :userid)';
207                 $params['userid'] = $userfilter;
208             }
209         }
211         if ($this->assignment->get_instance()->markingworkflow &&
212             $this->assignment->get_instance()->markingallocation) {
213             if (has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
214                 // Check to see if marker filter is set.
215                 $markerfilter = (int)get_user_preferences('assign_markerfilter', '');
216                 if (!empty($markerfilter)) {
217                     if ($markerfilter == ASSIGN_MARKER_FILTER_NO_MARKER) {
218                         $where .= ' AND (uf.allocatedmarker IS NULL OR uf.allocatedmarker = 0)';
219                     } else {
220                         $where .= ' AND uf.allocatedmarker = :markerid';
221                         $params['markerid'] = $markerfilter;
222                     }
223                 }
224             } else { // Only show users allocated to this marker.
225                 $where .= ' AND uf.allocatedmarker = :markerid';
226                 $params['markerid'] = $USER->id;
227             }
228         }
230         if ($this->assignment->get_instance()->markingworkflow) {
231             $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
232             if (!empty($workflowstates)) {
233                 $workflowfilter = get_user_preferences('assign_workflowfilter', '');
234                 if ($workflowfilter == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED) {
235                     $where .= ' AND (uf.workflowstate = :workflowstate OR uf.workflowstate IS NULL OR '.
236                         $DB->sql_isempty('assign_user_flags', 'workflowstate', true, true).')';
237                     $params['workflowstate'] = $workflowfilter;
238                 } else if (array_key_exists($workflowfilter, $workflowstates)) {
239                     $where .= ' AND uf.workflowstate = :workflowstate';
240                     $params['workflowstate'] = $workflowfilter;
241                 }
242             }
243         }
245         $this->set_sql($fields, $from, $where, $params);
247         if ($downloadfilename) {
248             $this->is_downloading('csv', $downloadfilename);
249         }
251         $columns = array();
252         $headers = array();
254         // Select.
255         if (!$this->is_downloading() && $this->hasgrade) {
256             $columns[] = 'select';
257             $headers[] = get_string('select') .
258                     '<div class="selectall"><label class="accesshide" for="selectall">' . get_string('selectall') . '</label>
259                     <input type="checkbox" id="selectall" name="selectall" title="' . get_string('selectall') . '"/></div>';
260         }
262         // User picture.
263         if ($this->hasviewblind || !$this->assignment->is_blind_marking()) {
264             if (!$this->is_downloading()) {
265                 $columns[] = 'picture';
266                 $headers[] = get_string('pictureofuser');
267             } else {
268                 $columns[] = 'recordid';
269                 $headers[] = get_string('recordid', 'assign');
270             }
272             // Fullname.
273             $columns[] = 'fullname';
274             $headers[] = get_string('fullname');
276             // Participant # details if can view real identities.
277             if ($this->assignment->is_blind_marking()) {
278                 if (!$this->is_downloading()) {
279                     $columns[] = 'recordid';
280                     $headers[] = get_string('recordid', 'assign');
281                 }
282             }
284             foreach ($extrauserfields as $extrafield) {
285                 $columns[] = $extrafield;
286                 $headers[] = get_user_field_name($extrafield);
287             }
288         } else {
289             // Record ID.
290             $columns[] = 'recordid';
291             $headers[] = get_string('recordid', 'assign');
292         }
294         // Submission status.
295         $columns[] = 'status';
296         $headers[] = get_string('status', 'assign');
298         // Team submission columns.
299         if ($assignment->get_instance()->teamsubmission) {
300             $columns[] = 'team';
301             $headers[] = get_string('submissionteam', 'assign');
302         }
303         // Allocated marker.
304         if ($this->assignment->get_instance()->markingworkflow &&
305             $this->assignment->get_instance()->markingallocation &&
306             has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
307             // Add a column for the allocated marker.
308             $columns[] = 'allocatedmarker';
309             $headers[] = get_string('marker', 'assign');
310         }
311         // Grade.
312         $columns[] = 'grade';
313         $headers[] = get_string('grade');
314         if ($this->is_downloading()) {
315             if ($this->assignment->get_instance()->grade >= 0) {
316                 $columns[] = 'grademax';
317                 $headers[] = get_string('maxgrade', 'assign');
318             } else {
319                 // This is a custom scale.
320                 $columns[] = 'scale';
321                 $headers[] = get_string('scale', 'assign');
322             }
324             if ($this->assignment->get_instance()->markingworkflow) {
325                 // Add a column for the marking workflow state.
326                 $columns[] = 'workflowstate';
327                 $headers[] = get_string('markingworkflowstate', 'assign');
328             }
329             // Add a column for the list of valid marking workflow states.
330             $columns[] = 'gradecanbechanged';
331             $headers[] = get_string('gradecanbechanged', 'assign');
332         }
333         if (!$this->is_downloading() && $this->hasgrade) {
334             // We have to call this column userid so we can use userid as a default sortable column.
335             $columns[] = 'userid';
336             $headers[] = get_string('edit');
337         }
339         // Submission plugins.
340         if ($assignment->is_any_submission_plugin_enabled()) {
341             $columns[] = 'timesubmitted';
342             $headers[] = get_string('lastmodifiedsubmission', 'assign');
344             foreach ($this->assignment->get_submission_plugins() as $plugin) {
345                 if ($this->is_downloading()) {
346                     if ($plugin->is_visible() && $plugin->is_enabled()) {
347                         foreach ($plugin->get_editor_fields() as $field => $description) {
348                             $index = 'plugin' . count($this->plugincache);
349                             $this->plugincache[$index] = array($plugin, $field);
350                             $columns[] = $index;
351                             $headers[] = $plugin->get_name();
352                         }
353                     }
354                 } else {
355                     if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
356                         $index = 'plugin' . count($this->plugincache);
357                         $this->plugincache[$index] = array($plugin);
358                         $columns[] = $index;
359                         $headers[] = $plugin->get_name();
360                     }
361                 }
362             }
363         }
365         // Time marked.
366         $columns[] = 'timemarked';
367         $headers[] = get_string('lastmodifiedgrade', 'assign');
369         // Feedback plugins.
370         foreach ($this->assignment->get_feedback_plugins() as $plugin) {
371             if ($this->is_downloading()) {
372                 if ($plugin->is_visible() && $plugin->is_enabled()) {
373                     foreach ($plugin->get_editor_fields() as $field => $description) {
374                         $index = 'plugin' . count($this->plugincache);
375                         $this->plugincache[$index] = array($plugin, $field);
376                         $columns[] = $index;
377                         $headers[] = $description;
378                     }
379                 }
380             } else if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
381                 $index = 'plugin' . count($this->plugincache);
382                 $this->plugincache[$index] = array($plugin);
383                 $columns[] = $index;
384                 $headers[] = $plugin->get_name();
385             }
386         }
388         // Exclude 'Final grade' column in downloaded grading worksheets.
389         if (!$this->is_downloading()) {
390             // Final grade.
391             $columns[] = 'finalgrade';
392             $headers[] = get_string('finalgrade', 'grades');
393         }
395         // Load the grading info for all users.
396         $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id,
397                                               'mod',
398                                               'assign',
399                                               $this->assignment->get_instance()->id,
400                                               $users);
402         if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
403             $columns[] = 'outcomes';
404             $headers[] = get_string('outcomes', 'grades');
405         }
407         // Set the columns.
408         $this->define_columns($columns);
409         $this->define_headers($headers);
410         foreach ($extrauserfields as $extrafield) {
411              $this->column_class($extrafield, $extrafield);
412         }
413         $this->no_sorting('recordid');
414         $this->no_sorting('finalgrade');
415         $this->no_sorting('userid');
416         $this->no_sorting('select');
417         $this->no_sorting('outcomes');
419         if ($assignment->get_instance()->teamsubmission) {
420             $this->no_sorting('team');
421         }
423         $plugincolumnindex = 0;
424         foreach ($this->assignment->get_submission_plugins() as $plugin) {
425             if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
426                 $submissionpluginindex = 'plugin' . $plugincolumnindex++;
427                 $this->no_sorting($submissionpluginindex);
428             }
429         }
430         foreach ($this->assignment->get_feedback_plugins() as $plugin) {
431             if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
432                 $feedbackpluginindex = 'plugin' . $plugincolumnindex++;
433                 $this->no_sorting($feedbackpluginindex);
434             }
435         }
437         // When there is no data we still want the column headers printed in the csv file.
438         if ($this->is_downloading()) {
439             $this->start_output();
440         }
441     }
443     /**
444      * Before adding each row to the table make sure rownum is incremented.
445      *
446      * @param array $row row of data from db used to make one row of the table.
447      * @return array one row for the table
448      */
449     public function format_row($row) {
450         if ($this->rownum < 0) {
451             $this->rownum = $this->currpage * $this->pagesize;
452         } else {
453             $this->rownum += 1;
454         }
456         return parent::format_row($row);
457     }
459     /**
460      * Add a column with an ID that uniquely identifies this user in this assignment.
461      *
462      * @param stdClass $row
463      * @return string
464      */
465     public function col_recordid(stdClass $row) {
466         return get_string('hiddenuser', 'assign') .
467                $this->assignment->get_uniqueid_for_user($row->userid);
468     }
471     /**
472      * Add the userid to the row class so it can be updated via ajax.
473      *
474      * @param stdClass $row The row of data
475      * @return string The row class
476      */
477     public function get_row_class($row) {
478         return 'user' . $row->userid;
479     }
481     /**
482      * Return the number of rows to display on a single page.
483      *
484      * @return int The number of rows per page
485      */
486     public function get_rows_per_page() {
487         return $this->perpage;
488     }
490     /**
491      * list current marking workflow state
492      *
493      * @param stdClass $row
494      * @return string
495      */
496     public function col_workflowstatus(stdClass $row) {
497         $o = '';
499         $gradingdisabled = $this->assignment->grading_disabled($row->id);
500         // The function in the assignment keeps a static cache of this list of states.
501         $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
502         $workflowstate = $row->workflowstate;
503         if (empty($workflowstate)) {
504             $workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
505         }
506         if ($this->quickgrading && !$gradingdisabled) {
507             $notmarked = get_string('markingworkflowstatenotmarked', 'assign');
508             $name = 'quickgrade_' . $row->id . '_workflowstate';
509             $o .= html_writer::select($workflowstates, $name, $workflowstate, array('' => $notmarked));
510             // Check if this user is a marker that can't manage allocations and doesn't have the marker column added.
511             if ($this->assignment->get_instance()->markingworkflow &&
512                 $this->assignment->get_instance()->markingallocation &&
513                 !has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
515                 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
516                 $o .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $name,
517                         'value' => $row->allocatedmarker));
518             }
519         } else {
520             $o .= $this->output->container(get_string('markingworkflowstate' . $workflowstate, 'assign'), $workflowstate);
521         }
522         return $o;
523     }
525     /**
526      * For download only - list current marking workflow state
527      *
528      * @param stdClass $row - The row of data
529      * @return string The current marking workflow state
530      */
531     public function col_workflowstate($row) {
532         $state = $row->workflowstate;
533         if (empty($state)) {
534             $state = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
535         }
537         return get_string('markingworkflowstate' . $state, 'assign');
538     }
540     /**
541      * list current marker
542      *
543      * @param stdClass $row - The row of data
544      * @return id the user->id of the marker.
545      */
546     public function col_allocatedmarker(stdClass $row) {
547         static $markers = null;
548         static $markerlist = array();
549         if ($markers === null) {
550             list($sort, $params) = users_order_by_sql();
551             $markers = get_users_by_capability($this->assignment->get_context(), 'mod/assign:grade', '', $sort);
552             $markerlist[0] = get_string('choosemarker', 'assign');
553             foreach ($markers as $marker) {
554                 $markerlist[$marker->id] = fullname($marker);
555             }
556         }
557         if (empty($markerlist)) {
558             // TODO: add some form of notification here that no markers are available.
559             return '';
560         }
561         if ($this->is_downloading()) {
562             if (isset($markers[$row->allocatedmarker])) {
563                 return fullname($markers[$row->allocatedmarker]);
564             } else {
565                 return '';
566             }
567         }
569         if ($this->quickgrading && has_capability('mod/assign:manageallocations', $this->assignment->get_context()) &&
570             (empty($row->workflowstate) ||
571              $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_INMARKING ||
572              $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED)) {
574             $name = 'quickgrade_' . $row->id . '_allocatedmarker';
575             return  html_writer::select($markerlist, $name, $row->allocatedmarker, false);
576         } else if (!empty($row->allocatedmarker)) {
577             $output = '';
578             if ($this->quickgrading) { // Add hidden field for quickgrading page.
579                 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
580                 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$row->allocatedmarker));
581             }
582             $output .= $markerlist[$row->allocatedmarker];
583             return $output;
584         }
585     }
586     /**
587      * For download only - list all the valid options for this custom scale.
588      *
589      * @param stdClass $row - The row of data
590      * @return string A list of valid options for the current scale
591      */
592     public function col_scale($row) {
593         global $DB;
595         if (empty($this->scale)) {
596             $dbparams = array('id'=>-($this->assignment->get_instance()->grade));
597             $this->scale = $DB->get_record('scale', $dbparams);
598         }
600         if (!empty($this->scale->scale)) {
601             return implode("\n", explode(',', $this->scale->scale));
602         }
603         return '';
604     }
606     /**
607      * Display a grade with scales etc.
608      *
609      * @param string $grade
610      * @param boolean $editable
611      * @param int $userid The user id of the user this grade belongs to
612      * @param int $modified Timestamp showing when the grade was last modified
613      * @return string The formatted grade
614      */
615     public function display_grade($grade, $editable, $userid, $modified) {
616         if ($this->is_downloading()) {
617             if ($this->assignment->get_instance()->grade >= 0) {
618                 if ($grade == -1 || $grade === null) {
619                     return '';
620                 }
621                 return format_float($grade, 2);
622             } else {
623                 // This is a custom scale.
624                 $scale = $this->assignment->display_grade($grade, false);
625                 if ($scale == '-') {
626                     $scale = '';
627                 }
628                 return $scale;
629             }
630         }
631         return $this->assignment->display_grade($grade, $editable, $userid, $modified);
632     }
634     /**
635      * Get the team info for this user.
636      *
637      * @param stdClass $row
638      * @return string The team name
639      */
640     public function col_team(stdClass $row) {
641         $submission = false;
642         $group = false;
643         $this->get_group_and_submission($row->id, $group, $submission, -1);
644         if ($group) {
645             return $group->name;
646         } else if ($this->assignment->get_instance()->preventsubmissionnotingroup) {
647             $usergroups = $this->assignment->get_all_groups($row->id);
648             if (count($usergroups) > 1) {
649                 return get_string('multipleteamsgrader', 'assign');
650             } else {
651                 return get_string('noteamgrader', 'assign');
652             }
653         }
654         return get_string('defaultteam', 'assign');
655     }
657     /**
658      * Use a static cache to try and reduce DB calls.
659      *
660      * @param int $userid The user id for this submission
661      * @param int $group The groupid (returned)
662      * @param stdClass|false $submission The stdClass submission or false (returned)
663      * @param int $attemptnumber Return a specific attempt number (-1 for latest)
664      */
665     protected function get_group_and_submission($userid, &$group, &$submission, $attemptnumber) {
666         $group = false;
667         if (isset($this->submissiongroups[$userid])) {
668             $group = $this->submissiongroups[$userid];
669         } else {
670             $group = $this->assignment->get_submission_group($userid, false);
671             $this->submissiongroups[$userid] = $group;
672         }
674         $groupid = 0;
675         if ($group) {
676             $groupid = $group->id;
677         }
679         // Static cache is keyed by groupid and attemptnumber.
680         // We may need both the latest and previous attempt in the same page.
681         if (isset($this->groupsubmissions[$groupid . ':' . $attemptnumber])) {
682             $submission = $this->groupsubmissions[$groupid . ':' . $attemptnumber];
683         } else {
684             $submission = $this->assignment->get_group_submission($userid, $groupid, false, $attemptnumber);
685             $this->groupsubmissions[$groupid . ':' . $attemptnumber] = $submission;
686         }
687     }
689     /**
690      * Format a list of outcomes.
691      *
692      * @param stdClass $row
693      * @return string
694      */
695     public function col_outcomes(stdClass $row) {
696         $outcomes = '';
697         foreach ($this->gradinginfo->outcomes as $index => $outcome) {
698             $options = make_grades_menu(-$outcome->scaleid);
700             $options[0] = get_string('nooutcome', 'grades');
701             if ($this->quickgrading && !($outcome->grades[$row->userid]->locked)) {
702                 $select = '<select name="outcome_' . $index . '_' . $row->userid . '" class="quickgrade">';
703                 foreach ($options as $optionindex => $optionvalue) {
704                     $selected = '';
705                     if ($outcome->grades[$row->userid]->grade == $optionindex) {
706                         $selected = 'selected="selected"';
707                     }
708                     $select .= '<option value="' . $optionindex . '"' . $selected . '>' . $optionvalue . '</option>';
709                 }
710                 $select .= '</select>';
711                 $outcomes .= $this->output->container($outcome->name . ': ' . $select, 'outcome');
712             } else {
713                 $name = $outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade];
714                 if ($this->is_downloading()) {
715                     $outcomes .= $name;
716                 } else {
717                     $outcomes .= $this->output->container($name, 'outcome');
718                 }
719             }
720         }
722         return $outcomes;
723     }
726     /**
727      * Format a user picture for display.
728      *
729      * @param stdClass $row
730      * @return string
731      */
732     public function col_picture(stdClass $row) {
733         return $this->output->user_picture($row);
734     }
736     /**
737      * Format a user record for display (link to profile).
738      *
739      * @param stdClass $row
740      * @return string
741      */
742     public function col_fullname($row) {
743         if (!$this->is_downloading()) {
744             $courseid = $this->assignment->get_course()->id;
745             $link= new moodle_url('/user/view.php', array('id' =>$row->id, 'course'=>$courseid));
746             $fullname = $this->output->action_link($link, $this->assignment->fullname($row));
747         } else {
748             $fullname = $this->assignment->fullname($row);
749         }
751         if (!$this->assignment->is_active_user($row->id)) {
752             $suspendedstring = get_string('userenrolmentsuspended', 'grades');
753             $fullname .= ' ' . html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/enrolmentsuspended'),
754                 'title' => $suspendedstring, 'alt' => $suspendedstring, 'class' => 'usersuspendedicon'));
755             $fullname = html_writer::tag('span', $fullname, array('class' => 'usersuspended'));
756         }
757         return $fullname;
758     }
760     /**
761      * Insert a checkbox for selecting the current row for batch operations.
762      *
763      * @param stdClass $row
764      * @return string
765      */
766     public function col_select(stdClass $row) {
767         $selectcol = '<label class="accesshide" for="selectuser_' . $row->userid . '">';
768         $selectcol .= get_string('selectuser', 'assign', $this->assignment->fullname($row));
769         $selectcol .= '</label>';
770         $selectcol .= '<input type="checkbox"
771                               id="selectuser_' . $row->userid . '"
772                               name="selectedusers"
773                               value="' . $row->userid . '"/>';
774         $selectcol .= '<input type="hidden"
775                               name="grademodified_' . $row->userid . '"
776                               value="' . $row->timemarked . '"/>';
777         $selectcol .= '<input type="hidden"
778                               name="gradeattempt_' . $row->userid . '"
779                               value="' . $row->attemptnumber . '"/>';
780         return $selectcol;
781     }
783     /**
784      * Return a users grades from the listing of all grade data for this assignment.
785      *
786      * @param int $userid
787      * @return mixed stdClass or false
788      */
789     private function get_gradebook_data_for_user($userid) {
790         if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
791             return $this->gradinginfo->items[0]->grades[$userid];
792         }
793         return false;
794     }
796     /**
797      * Format a column of data for display.
798      *
799      * @param stdClass $row
800      * @return string
801      */
802     public function col_gradecanbechanged(stdClass $row) {
803         $gradingdisabled = $this->assignment->grading_disabled($row->id);
804         if ($gradingdisabled) {
805             return get_string('no');
806         } else {
807             return get_string('yes');
808         }
809     }
811     /**
812      * Format a column of data for display
813      *
814      * @param stdClass $row
815      * @return string
816      */
817     public function col_grademax(stdClass $row) {
818         return format_float($this->assignment->get_instance()->grade, 2);
819     }
821     /**
822      * Format a column of data for display.
823      *
824      * @param stdClass $row
825      * @return string
826      */
827     public function col_grade(stdClass $row) {
828         $o = '';
830         $link = '';
831         $separator = $this->output->spacer(array(), true);
832         $grade = '';
833         $gradingdisabled = $this->assignment->grading_disabled($row->id);
835         if (!$this->is_downloading() && $this->hasgrade) {
836             $name = $this->assignment->fullname($row);
837             $icon = $this->output->pix_icon('gradefeedback',
838                                             get_string('gradeuser', 'assign', $name),
839                                             'mod_assign');
840             $urlparams = array('id' => $this->assignment->get_course_module()->id,
841                                'rownum'=>$this->rownum,
842                                'action' => 'grade',
843                                'useridlistid' => $this->assignment->get_useridlist_key_id());
844             $url = new moodle_url('/mod/assign/view.php', $urlparams);
845             $link = $this->output->action_link($url, $icon);
846             $grade .= $link . $separator;
847         }
849         $grade .= $this->display_grade($row->grade,
850                                        $this->quickgrading && !$gradingdisabled,
851                                        $row->userid,
852                                        $row->timemarked);
854         return $grade;
855     }
857     /**
858      * Format a column of data for display.
859      *
860      * @param stdClass $row
861      * @return string
862      */
863     public function col_finalgrade(stdClass $row) {
864         $o = '';
866         $grade = $this->get_gradebook_data_for_user($row->userid);
867         if ($grade) {
868             $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
869         }
871         return $o;
872     }
874     /**
875      * Format a column of data for display.
876      *
877      * @param stdClass $row
878      * @return string
879      */
880     public function col_timemarked(stdClass $row) {
881         $o = '-';
883         if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
884             $o = userdate($row->timemarked);
885         }
886         if ($row->timemarked && $this->is_downloading()) {
887             // Force it for downloads as it affects import.
888             $o = userdate($row->timemarked);
889         }
891         return $o;
892     }
894     /**
895      * Format a column of data for display.
896      *
897      * @param stdClass $row
898      * @return string
899      */
900     public function col_timesubmitted(stdClass $row) {
901         $o = '-';
903         $group = false;
904         $submission = false;
905         $this->get_group_and_submission($row->id, $group, $submission, -1);
906         if ($submission && $submission->timemodified && $submission->status != ASSIGN_SUBMISSION_STATUS_NEW) {
907             $o = userdate($submission->timemodified);
908         } else if ($row->timesubmitted) {
909             $o = userdate($row->timesubmitted);
910         }
912         return $o;
913     }
915     /**
916      * Format a column of data for display
917      *
918      * @param stdClass $row
919      * @return string
920      */
921     public function col_status(stdClass $row) {
922         $o = '';
924         $instance = $this->assignment->get_instance();
926         $due = $instance->duedate;
927         if ($row->extensionduedate) {
928             $due = $row->extensionduedate;
929         }
931         $group = false;
932         $submission = false;
933         $this->get_group_and_submission($row->id, $group, $submission, -1);
935         if ($instance->teamsubmission && !$group && !$instance->preventsubmissionnotingroup) {
936             $group = true;
937         }
939         if ($group && $submission) {
940             $timesubmitted = $submission->timemodified;
941             $status = $submission->status;
942         } else {
943             $timesubmitted = $row->timesubmitted;
944             $status = $row->status;
945         }
947         if ($this->assignment->is_any_submission_plugin_enabled()) {
949             $o .= $this->output->container(get_string('submissionstatus_' . $status, 'assign'),
950                                            array('class'=>'submissionstatus' .$status));
951             if ($due && $timesubmitted > $due) {
952                 $usertime = format_time($timesubmitted - $due);
953                 $latemessage = get_string('submittedlateshort',
954                                           'assign',
955                                           $usertime);
956                 $o .= $this->output->container($latemessage, 'latesubmission');
957             }
958             if ($row->locked) {
959                 $lockedstr = get_string('submissionslockedshort', 'assign');
960                 $o .= $this->output->container($lockedstr, 'lockedsubmission');
961             }
963             // Add status of "grading" if markflow is not enabled.
964             if (!$instance->markingworkflow) {
965                 if ($row->grade !== null && $row->grade >= 0) {
966                     $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
967                 } else if (!$timesubmitted) {
968                     $now = time();
969                     if ($due && ($now > $due)) {
970                         $overduestr = get_string('overdue', 'assign', format_time($now - $due));
971                         $o .= $this->output->container($overduestr, 'overduesubmission');
972                     }
973                 }
974             }
975         }
977         if ($instance->markingworkflow) {
978             $o .= $this->col_workflowstatus($row);
979         }
980         if ($row->extensionduedate) {
981             $userdate = userdate($row->extensionduedate);
982             $extensionstr = get_string('userextensiondate', 'assign', $userdate);
983             $o .= $this->output->container($extensionstr, 'extensiondate');
984         }
986         if ($this->is_downloading()) {
987             $o = strip_tags(rtrim(str_replace('</div>', ' - ', $o), '- '));
988         }
990         return $o;
991     }
993     /**
994      * Format a column of data for display.
995      *
996      * @param stdClass $row
997      * @return string
998      */
999     public function col_userid(stdClass $row) {
1000         global $USER;
1002         $edit = '';
1004         $actions = array();
1006         $urlparams = array('id'=>$this->assignment->get_course_module()->id,
1007                            'rownum'=>$this->rownum,
1008                            'action' => 'grade',
1009                            'useridlistid' => $this->assignment->get_useridlist_key_id());
1010         $url = new moodle_url('/mod/assign/view.php', $urlparams);
1011         $noimage = null;
1013         if (!$row->grade) {
1014             $description = get_string('grade');
1015         } else {
1016             $description = get_string('updategrade', 'assign');
1017         }
1018         $actions['grade'] = new action_menu_link_secondary(
1019             $url,
1020             $noimage,
1021             $description
1022         );
1024         // Everything we need is in the row.
1025         $submission = $row;
1026         $flags = $row;
1027         if ($this->assignment->get_instance()->teamsubmission) {
1028             // Use the cache for this.
1029             $submission = false;
1030             $group = false;
1031             $this->get_group_and_submission($row->id, $group, $submission, -1);
1032         }
1034         $submissionsopen = $this->assignment->submissions_open($row->id,
1035                                                                true,
1036                                                                $submission,
1037                                                                $flags,
1038                                                                $this->gradinginfo);
1039         $caneditsubmission = $this->assignment->can_edit_submission($row->id, $USER->id);
1041         // Hide for offline assignments.
1042         if ($this->assignment->is_any_submission_plugin_enabled()) {
1043             if (!$row->status ||
1044                     $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
1045                     !$this->assignment->get_instance()->submissiondrafts) {
1047                 if (!$row->locked) {
1048                     $urlparams = array('id' => $this->assignment->get_course_module()->id,
1049                                        'userid'=>$row->id,
1050                                        'action'=>'lock',
1051                                        'sesskey'=>sesskey(),
1052                                        'page'=>$this->currpage);
1053                     $url = new moodle_url('/mod/assign/view.php', $urlparams);
1055                     $description = get_string('preventsubmissionsshort', 'assign');
1056                     $actions['lock'] = new action_menu_link_secondary(
1057                         $url,
1058                         $noimage,
1059                         $description
1060                     );
1061                 } else {
1062                     $urlparams = array('id' => $this->assignment->get_course_module()->id,
1063                                        'userid'=>$row->id,
1064                                        'action'=>'unlock',
1065                                        'sesskey'=>sesskey(),
1066                                        'page'=>$this->currpage);
1067                     $url = new moodle_url('/mod/assign/view.php', $urlparams);
1068                     $description = get_string('allowsubmissionsshort', 'assign');
1069                     $actions['unlock'] = new action_menu_link_secondary(
1070                         $url,
1071                         $noimage,
1072                         $description
1073                     );
1074                 }
1075             }
1077             if ($submissionsopen &&
1078                     $USER->id != $row->id &&
1079                     $caneditsubmission) {
1080                 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1081                                    'userid'=>$row->id,
1082                                    'action'=>'editsubmission',
1083                                    'sesskey'=>sesskey(),
1084                                    'page'=>$this->currpage);
1085                 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1086                 $description = get_string('editsubmission', 'assign');
1087                 $actions['editsubmission'] = new action_menu_link_secondary(
1088                     $url,
1089                     $noimage,
1090                     $description
1091                 );
1092             }
1093         }
1094         if (($this->assignment->get_instance()->duedate ||
1095                 $this->assignment->get_instance()->cutoffdate) &&
1096                 $this->hasgrantextension) {
1097              $urlparams = array('id' => $this->assignment->get_course_module()->id,
1098                                 'userid' => $row->id,
1099                                 'action' => 'grantextension',
1100                                 'sesskey' => sesskey(),
1101                                 'page' => $this->currpage);
1102              $url = new moodle_url('/mod/assign/view.php', $urlparams);
1103              $description = get_string('grantextension', 'assign');
1104              $actions['grantextension'] = new action_menu_link_secondary(
1105                  $url,
1106                  $noimage,
1107                  $description
1108              );
1109         }
1110         if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
1111                 $this->assignment->get_instance()->submissiondrafts) {
1112             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1113                                'userid'=>$row->id,
1114                                'action'=>'reverttodraft',
1115                                'sesskey'=>sesskey(),
1116                                'page'=>$this->currpage);
1117             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1118             $description = get_string('reverttodraftshort', 'assign');
1119             $actions['reverttodraft'] = new action_menu_link_secondary(
1120                 $url,
1121                 $noimage,
1122                 $description
1123             );
1124         }
1125         if ($row->status == ASSIGN_SUBMISSION_STATUS_DRAFT &&
1126                 $this->assignment->get_instance()->submissiondrafts &&
1127                 $caneditsubmission &&
1128                 $submissionsopen &&
1129                 $row->id != $USER->id) {
1130             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1131                                'userid'=>$row->id,
1132                                'action'=>'submitotherforgrading',
1133                                'sesskey'=>sesskey(),
1134                                'page'=>$this->currpage);
1135             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1136             $description = get_string('submitforgrading', 'assign');
1137             $actions['submitforgrading'] = new action_menu_link_secondary(
1138                 $url,
1139                 $noimage,
1140                 $description
1141             );
1142         }
1144         $ismanual = $this->assignment->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
1145         $hassubmission = !empty($row->status);
1146         $notreopened = $hassubmission && $row->status != ASSIGN_SUBMISSION_STATUS_REOPENED;
1147         $isunlimited = $this->assignment->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS;
1148         $hasattempts = $isunlimited || $row->attemptnumber < $this->assignment->get_instance()->maxattempts - 1;
1150         if ($ismanual && $hassubmission && $notreopened && $hasattempts) {
1151             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1152                                'userid'=>$row->id,
1153                                'action'=>'addattempt',
1154                                'sesskey'=>sesskey(),
1155                                'page'=>$this->currpage);
1156             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1157             $description = get_string('addattempt', 'assign');
1158             $actions['addattempt'] = new action_menu_link_secondary(
1159                 $url,
1160                 $noimage,
1161                 $description
1162             );
1163         }
1165         $menu = new action_menu();
1166         $menu->set_owner_selector('.gradingtable-actionmenu');
1167         $menu->set_alignment(action_menu::TL, action_menu::BL);
1168         $menu->set_constraint('.gradingtable > .no-overflow');
1169         $menu->set_menu_trigger(get_string('edit'));
1170         foreach ($actions as $action) {
1171             $menu->add($action);
1172         }
1174         // Prioritise the menu ahead of all other actions.
1175         $menu->prioritise = true;
1177         $edit .= $this->output->render($menu);
1179         return $edit;
1180     }
1182     /**
1183      * Write the plugin summary with an optional link to view the full feedback/submission.
1184      *
1185      * @param assign_plugin $plugin Submission plugin or feedback plugin
1186      * @param stdClass $item Submission or grade
1187      * @param string $returnaction The return action to pass to the
1188      *                             view_submission page (the current page)
1189      * @param string $returnparams The return params to pass to the view_submission
1190      *                             page (the current page)
1191      * @return string The summary with an optional link
1192      */
1193     private function format_plugin_summary_with_link(assign_plugin $plugin,
1194                                                      stdClass $item,
1195                                                      $returnaction,
1196                                                      $returnparams) {
1197         $link = '';
1198         $showviewlink = false;
1200         $summary = $plugin->view_summary($item, $showviewlink);
1201         $separator = '';
1202         if ($showviewlink) {
1203             $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
1204             $icon = $this->output->pix_icon('t/preview', $viewstr);
1205             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1206                                                      'sid'=>$item->id,
1207                                                      'gid'=>$item->id,
1208                                                      'plugin'=>$plugin->get_type(),
1209                                                      'action'=>'viewplugin' . $plugin->get_subtype(),
1210                                                      'returnaction'=>$returnaction,
1211                                                      'returnparams'=>http_build_query($returnparams));
1212             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1213             $link = $this->output->action_link($url, $icon);
1214             $separator = $this->output->spacer(array(), true);
1215         }
1217         return $link . $separator . $summary;
1218     }
1221     /**
1222      * Format the submission and feedback columns.
1223      *
1224      * @param string $colname The column name
1225      * @param stdClass $row The submission row
1226      * @return mixed string or NULL
1227      */
1228     public function other_cols($colname, $row) {
1229         // For extra user fields the result is already in $row.
1230         if (empty($this->plugincache[$colname])) {
1231             return $row->$colname;
1232         }
1234         // This must be a plugin field.
1235         $plugincache = $this->plugincache[$colname];
1237         $plugin = $plugincache[0];
1239         $field = null;
1240         if (isset($plugincache[1])) {
1241             $field = $plugincache[1];
1242         }
1244         if ($plugin->is_visible() && $plugin->is_enabled()) {
1245             if ($plugin->get_subtype() == 'assignsubmission') {
1246                 if ($this->assignment->get_instance()->teamsubmission) {
1247                     $group = false;
1248                     $submission = false;
1250                     $this->get_group_and_submission($row->id, $group, $submission, -1);
1251                     if ($submission) {
1252                         if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1253                             // For a newly reopened submission - we want to show the previous submission in the table.
1254                             $this->get_group_and_submission($row->id, $group, $submission, $submission->attemptnumber-1);
1255                         }
1256                         if (isset($field)) {
1257                             return $plugin->get_editor_text($field, $submission->id);
1258                         }
1259                         return $this->format_plugin_summary_with_link($plugin,
1260                                                                       $submission,
1261                                                                       'grading',
1262                                                                       array());
1263                     }
1264                 } else if ($row->submissionid) {
1265                     if ($row->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1266                         // For a newly reopened submission - we want to show the previous submission in the table.
1267                         $submission = $this->assignment->get_user_submission($row->userid, false, $row->attemptnumber - 1);
1268                     } else {
1269                         $submission = new stdClass();
1270                         $submission->id = $row->submissionid;
1271                         $submission->timecreated = $row->firstsubmission;
1272                         $submission->timemodified = $row->timesubmitted;
1273                         $submission->assignment = $this->assignment->get_instance()->id;
1274                         $submission->userid = $row->userid;
1275                         $submission->attemptnumber = $row->attemptnumber;
1276                     }
1277                     // Field is used for only for import/export and refers the the fieldname for the text editor.
1278                     if (isset($field)) {
1279                         return $plugin->get_editor_text($field, $submission->id);
1280                     }
1281                     return $this->format_plugin_summary_with_link($plugin,
1282                                                                   $submission,
1283                                                                   'grading',
1284                                                                   array());
1285                 }
1286             } else {
1287                 $grade = null;
1288                 if (isset($field)) {
1289                     return $plugin->get_editor_text($field, $row->gradeid);
1290                 }
1292                 if ($row->gradeid) {
1293                     $grade = new stdClass();
1294                     $grade->id = $row->gradeid;
1295                     $grade->timecreated = $row->firstmarked;
1296                     $grade->timemodified = $row->timemarked;
1297                     $grade->assignment = $this->assignment->get_instance()->id;
1298                     $grade->userid = $row->userid;
1299                     $grade->grade = $row->grade;
1300                     $grade->mailed = $row->mailed;
1301                     $grade->attemptnumber = $row->attemptnumber;
1302                 }
1303                 if ($this->quickgrading && $plugin->supports_quickgrading()) {
1304                     return $plugin->get_quickgrading_html($row->userid, $grade);
1305                 } else if ($grade) {
1306                     return $this->format_plugin_summary_with_link($plugin,
1307                                                                   $grade,
1308                                                                   'grading',
1309                                                                   array());
1310                 }
1311             }
1312         }
1313         return '';
1314     }
1316     /**
1317      * Using the current filtering and sorting - load all rows and return a single column from them.
1318      *
1319      * @param string $columnname The name of the raw column data
1320      * @return array of data
1321      */
1322     public function get_column_data($columnname) {
1323         $this->setup();
1324         $this->currpage = 0;
1325         $this->query_db($this->tablemaxrows);
1326         $result = array();
1327         foreach ($this->rawdata as $row) {
1328             $result[] = $row->$columnname;
1329         }
1330         return $result;
1331     }
1333     /**
1334      * Return things to the renderer.
1335      *
1336      * @return string the assignment name
1337      */
1338     public function get_assignment_name() {
1339         return $this->assignment->get_instance()->name;
1340     }
1342     /**
1343      * Return things to the renderer.
1344      *
1345      * @return int the course module id
1346      */
1347     public function get_course_module_id() {
1348         return $this->assignment->get_course_module()->id;
1349     }
1351     /**
1352      * Return things to the renderer.
1353      *
1354      * @return int the course id
1355      */
1356     public function get_course_id() {
1357         return $this->assignment->get_course()->id;
1358     }
1360     /**
1361      * Return things to the renderer.
1362      *
1363      * @return stdClass The course context
1364      */
1365     public function get_course_context() {
1366         return $this->assignment->get_course_context();
1367     }
1369     /**
1370      * Return things to the renderer.
1371      *
1372      * @return bool Does this assignment accept submissions
1373      */
1374     public function submissions_enabled() {
1375         return $this->assignment->is_any_submission_plugin_enabled();
1376     }
1378     /**
1379      * Return things to the renderer.
1380      *
1381      * @return bool Can this user view all grades (the gradebook)
1382      */
1383     public function can_view_all_grades() {
1384         $context = $this->assignment->get_course_context();
1385         return has_capability('gradereport/grader:view', $context) &&
1386                has_capability('moodle/grade:viewall', $context);
1387     }
1389     /**
1390      * Always return a valid sort - even if the userid column is missing.
1391      * @return array column name => SORT_... constant.
1392      */
1393     public function get_sort_columns() {
1394         $result = parent::get_sort_columns();
1395         $result = array_merge($result, array('userid' => SORT_ASC));
1396         return $result;
1397     }
1399     /**
1400      * Override the table show_hide_link to not show for select column.
1401      *
1402      * @param string $column the column name, index into various names.
1403      * @param int $index numerical index of the column.
1404      * @return string HTML fragment.
1405      */
1406     protected function show_hide_link($column, $index) {
1407         if ($index > 0 || !$this->hasgrade) {
1408             return parent::show_hide_link($column, $index);
1409         }
1410         return '';
1411     }
1413     /**
1414      * Overides setup to ensure it will only run a single time.
1415      */
1416     public function setup() {
1417         // Check if the setup function has been called before, we should not run it twice.
1418         // If we do the sortorder of the table will be broken.
1419         if (!empty($this->setup)) {
1420             return;
1421         }
1422         parent::setup();
1423     }