1d5ec06d94c6e723055d741fa4fd5a3a6bff09f8
[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         return $selectcol;
778     }
780     /**
781      * Return a users grades from the listing of all grade data for this assignment.
782      *
783      * @param int $userid
784      * @return mixed stdClass or false
785      */
786     private function get_gradebook_data_for_user($userid) {
787         if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
788             return $this->gradinginfo->items[0]->grades[$userid];
789         }
790         return false;
791     }
793     /**
794      * Format a column of data for display.
795      *
796      * @param stdClass $row
797      * @return string
798      */
799     public function col_gradecanbechanged(stdClass $row) {
800         $gradingdisabled = $this->assignment->grading_disabled($row->id);
801         if ($gradingdisabled) {
802             return get_string('no');
803         } else {
804             return get_string('yes');
805         }
806     }
808     /**
809      * Format a column of data for display
810      *
811      * @param stdClass $row
812      * @return string
813      */
814     public function col_grademax(stdClass $row) {
815         return format_float($this->assignment->get_instance()->grade, 2);
816     }
818     /**
819      * Format a column of data for display.
820      *
821      * @param stdClass $row
822      * @return string
823      */
824     public function col_grade(stdClass $row) {
825         $o = '';
827         $link = '';
828         $separator = $this->output->spacer(array(), true);
829         $grade = '';
830         $gradingdisabled = $this->assignment->grading_disabled($row->id);
832         if (!$this->is_downloading() && $this->hasgrade) {
833             $name = $this->assignment->fullname($row);
834             $icon = $this->output->pix_icon('gradefeedback',
835                                             get_string('gradeuser', 'assign', $name),
836                                             'mod_assign');
837             $urlparams = array('id' => $this->assignment->get_course_module()->id,
838                                'rownum'=>$this->rownum,
839                                'action' => 'grade',
840                                'useridlistid' => $this->assignment->get_useridlist_key_id());
841             $url = new moodle_url('/mod/assign/view.php', $urlparams);
842             $link = $this->output->action_link($url, $icon);
843             $grade .= $link . $separator;
844         }
846         $grade .= $this->display_grade($row->grade,
847                                        $this->quickgrading && !$gradingdisabled,
848                                        $row->userid,
849                                        $row->timemarked);
851         return $grade;
852     }
854     /**
855      * Format a column of data for display.
856      *
857      * @param stdClass $row
858      * @return string
859      */
860     public function col_finalgrade(stdClass $row) {
861         $o = '';
863         $grade = $this->get_gradebook_data_for_user($row->userid);
864         if ($grade) {
865             $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
866         }
868         return $o;
869     }
871     /**
872      * Format a column of data for display.
873      *
874      * @param stdClass $row
875      * @return string
876      */
877     public function col_timemarked(stdClass $row) {
878         $o = '-';
880         if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
881             $o = userdate($row->timemarked);
882         }
883         if ($row->timemarked && $this->is_downloading()) {
884             // Force it for downloads as it affects import.
885             $o = userdate($row->timemarked);
886         }
888         return $o;
889     }
891     /**
892      * Format a column of data for display.
893      *
894      * @param stdClass $row
895      * @return string
896      */
897     public function col_timesubmitted(stdClass $row) {
898         $o = '-';
900         $group = false;
901         $submission = false;
902         $this->get_group_and_submission($row->id, $group, $submission, -1);
903         if ($submission && $submission->timemodified && $submission->status != ASSIGN_SUBMISSION_STATUS_NEW) {
904             $o = userdate($submission->timemodified);
905         } else if ($row->timesubmitted) {
906             $o = userdate($row->timesubmitted);
907         }
909         return $o;
910     }
912     /**
913      * Format a column of data for display
914      *
915      * @param stdClass $row
916      * @return string
917      */
918     public function col_status(stdClass $row) {
919         $o = '';
921         $instance = $this->assignment->get_instance();
923         $due = $instance->duedate;
924         if ($row->extensionduedate) {
925             $due = $row->extensionduedate;
926         }
928         $group = false;
929         $submission = false;
930         $this->get_group_and_submission($row->id, $group, $submission, -1);
932         if ($instance->teamsubmission && !$group && !$instance->preventsubmissionnotingroup) {
933             $group = true;
934         }
936         if ($group && $submission) {
937             $timesubmitted = $submission->timemodified;
938             $status = $submission->status;
939         } else {
940             $timesubmitted = $row->timesubmitted;
941             $status = $row->status;
942         }
944         if ($this->assignment->is_any_submission_plugin_enabled()) {
946             $o .= $this->output->container(get_string('submissionstatus_' . $status, 'assign'),
947                                            array('class'=>'submissionstatus' .$status));
948             if ($due && $timesubmitted > $due) {
949                 $usertime = format_time($timesubmitted - $due);
950                 $latemessage = get_string('submittedlateshort',
951                                           'assign',
952                                           $usertime);
953                 $o .= $this->output->container($latemessage, 'latesubmission');
954             }
955             if ($row->locked) {
956                 $lockedstr = get_string('submissionslockedshort', 'assign');
957                 $o .= $this->output->container($lockedstr, 'lockedsubmission');
958             }
960             // Add status of "grading" if markflow is not enabled.
961             if (!$instance->markingworkflow) {
962                 if ($row->grade !== null && $row->grade >= 0) {
963                     $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
964                 } else if (!$timesubmitted) {
965                     $now = time();
966                     if ($due && ($now > $due)) {
967                         $overduestr = get_string('overdue', 'assign', format_time($now - $due));
968                         $o .= $this->output->container($overduestr, 'overduesubmission');
969                     }
970                 }
971             }
972         }
974         if ($instance->markingworkflow) {
975             $o .= $this->col_workflowstatus($row);
976         }
977         if ($row->extensionduedate) {
978             $userdate = userdate($row->extensionduedate);
979             $extensionstr = get_string('userextensiondate', 'assign', $userdate);
980             $o .= $this->output->container($extensionstr, 'extensiondate');
981         }
983         if ($this->is_downloading()) {
984             $o = strip_tags(rtrim(str_replace('</div>', ' - ', $o), '- '));
985         }
987         return $o;
988     }
990     /**
991      * Format a column of data for display.
992      *
993      * @param stdClass $row
994      * @return string
995      */
996     public function col_userid(stdClass $row) {
997         global $USER;
999         $edit = '';
1001         $actions = array();
1003         $urlparams = array('id'=>$this->assignment->get_course_module()->id,
1004                            'rownum'=>$this->rownum,
1005                            'action' => 'grade',
1006                            'useridlistid' => $this->assignment->get_useridlist_key_id());
1007         $url = new moodle_url('/mod/assign/view.php', $urlparams);
1008         $noimage = null;
1010         if (!$row->grade) {
1011             $description = get_string('grade');
1012         } else {
1013             $description = get_string('updategrade', 'assign');
1014         }
1015         $actions['grade'] = new action_menu_link_secondary(
1016             $url,
1017             $noimage,
1018             $description
1019         );
1021         // Everything we need is in the row.
1022         $submission = $row;
1023         $flags = $row;
1024         if ($this->assignment->get_instance()->teamsubmission) {
1025             // Use the cache for this.
1026             $submission = false;
1027             $group = false;
1028             $this->get_group_and_submission($row->id, $group, $submission, -1);
1029         }
1031         $submissionsopen = $this->assignment->submissions_open($row->id,
1032                                                                true,
1033                                                                $submission,
1034                                                                $flags,
1035                                                                $this->gradinginfo);
1036         $caneditsubmission = $this->assignment->can_edit_submission($row->id, $USER->id);
1038         // Hide for offline assignments.
1039         if ($this->assignment->is_any_submission_plugin_enabled()) {
1040             if (!$row->status ||
1041                     $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
1042                     !$this->assignment->get_instance()->submissiondrafts) {
1044                 if (!$row->locked) {
1045                     $urlparams = array('id' => $this->assignment->get_course_module()->id,
1046                                        'userid'=>$row->id,
1047                                        'action'=>'lock',
1048                                        'sesskey'=>sesskey(),
1049                                        'page'=>$this->currpage);
1050                     $url = new moodle_url('/mod/assign/view.php', $urlparams);
1052                     $description = get_string('preventsubmissionsshort', 'assign');
1053                     $actions['lock'] = new action_menu_link_secondary(
1054                         $url,
1055                         $noimage,
1056                         $description
1057                     );
1058                 } else {
1059                     $urlparams = array('id' => $this->assignment->get_course_module()->id,
1060                                        'userid'=>$row->id,
1061                                        'action'=>'unlock',
1062                                        'sesskey'=>sesskey(),
1063                                        'page'=>$this->currpage);
1064                     $url = new moodle_url('/mod/assign/view.php', $urlparams);
1065                     $description = get_string('allowsubmissionsshort', 'assign');
1066                     $actions['unlock'] = new action_menu_link_secondary(
1067                         $url,
1068                         $noimage,
1069                         $description
1070                     );
1071                 }
1072             }
1074             if ($submissionsopen &&
1075                     $USER->id != $row->id &&
1076                     $caneditsubmission) {
1077                 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1078                                    'userid'=>$row->id,
1079                                    'action'=>'editsubmission',
1080                                    'sesskey'=>sesskey(),
1081                                    'page'=>$this->currpage);
1082                 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1083                 $description = get_string('editsubmission', 'assign');
1084                 $actions['editsubmission'] = new action_menu_link_secondary(
1085                     $url,
1086                     $noimage,
1087                     $description
1088                 );
1089             }
1090         }
1091         if (($this->assignment->get_instance()->duedate ||
1092                 $this->assignment->get_instance()->cutoffdate) &&
1093                 $this->hasgrantextension) {
1094              $urlparams = array('id' => $this->assignment->get_course_module()->id,
1095                                 'userid' => $row->id,
1096                                 'action' => 'grantextension',
1097                                 'sesskey' => sesskey(),
1098                                 'page' => $this->currpage);
1099              $url = new moodle_url('/mod/assign/view.php', $urlparams);
1100              $description = get_string('grantextension', 'assign');
1101              $actions['grantextension'] = new action_menu_link_secondary(
1102                  $url,
1103                  $noimage,
1104                  $description
1105              );
1106         }
1107         if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
1108                 $this->assignment->get_instance()->submissiondrafts) {
1109             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1110                                'userid'=>$row->id,
1111                                'action'=>'reverttodraft',
1112                                'sesskey'=>sesskey(),
1113                                'page'=>$this->currpage);
1114             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1115             $description = get_string('reverttodraftshort', 'assign');
1116             $actions['reverttodraft'] = new action_menu_link_secondary(
1117                 $url,
1118                 $noimage,
1119                 $description
1120             );
1121         }
1122         if ($row->status == ASSIGN_SUBMISSION_STATUS_DRAFT &&
1123                 $this->assignment->get_instance()->submissiondrafts &&
1124                 $caneditsubmission &&
1125                 $submissionsopen &&
1126                 $row->id != $USER->id) {
1127             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1128                                'userid'=>$row->id,
1129                                'action'=>'submitotherforgrading',
1130                                'sesskey'=>sesskey(),
1131                                'page'=>$this->currpage);
1132             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1133             $description = get_string('submitforgrading', 'assign');
1134             $actions['submitforgrading'] = new action_menu_link_secondary(
1135                 $url,
1136                 $noimage,
1137                 $description
1138             );
1139         }
1141         $ismanual = $this->assignment->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
1142         $hassubmission = !empty($row->status);
1143         $notreopened = $hassubmission && $row->status != ASSIGN_SUBMISSION_STATUS_REOPENED;
1144         $isunlimited = $this->assignment->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS;
1145         $hasattempts = $isunlimited || $row->attemptnumber < $this->assignment->get_instance()->maxattempts - 1;
1147         if ($ismanual && $hassubmission && $notreopened && $hasattempts) {
1148             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1149                                'userid'=>$row->id,
1150                                'action'=>'addattempt',
1151                                'sesskey'=>sesskey(),
1152                                'page'=>$this->currpage);
1153             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1154             $description = get_string('addattempt', 'assign');
1155             $actions['addattempt'] = new action_menu_link_secondary(
1156                 $url,
1157                 $noimage,
1158                 $description
1159             );
1160         }
1162         $menu = new action_menu();
1163         $menu->set_owner_selector('.gradingtable-actionmenu');
1164         $menu->set_alignment(action_menu::TL, action_menu::BL);
1165         $menu->set_constraint('.gradingtable > .no-overflow');
1166         $menu->set_menu_trigger(get_string('edit'));
1167         foreach ($actions as $action) {
1168             $menu->add($action);
1169         }
1171         // Prioritise the menu ahead of all other actions.
1172         $menu->prioritise = true;
1174         $edit .= $this->output->render($menu);
1176         return $edit;
1177     }
1179     /**
1180      * Write the plugin summary with an optional link to view the full feedback/submission.
1181      *
1182      * @param assign_plugin $plugin Submission plugin or feedback plugin
1183      * @param stdClass $item Submission or grade
1184      * @param string $returnaction The return action to pass to the
1185      *                             view_submission page (the current page)
1186      * @param string $returnparams The return params to pass to the view_submission
1187      *                             page (the current page)
1188      * @return string The summary with an optional link
1189      */
1190     private function format_plugin_summary_with_link(assign_plugin $plugin,
1191                                                      stdClass $item,
1192                                                      $returnaction,
1193                                                      $returnparams) {
1194         $link = '';
1195         $showviewlink = false;
1197         $summary = $plugin->view_summary($item, $showviewlink);
1198         $separator = '';
1199         if ($showviewlink) {
1200             $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
1201             $icon = $this->output->pix_icon('t/preview', $viewstr);
1202             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1203                                                      'sid'=>$item->id,
1204                                                      'gid'=>$item->id,
1205                                                      'plugin'=>$plugin->get_type(),
1206                                                      'action'=>'viewplugin' . $plugin->get_subtype(),
1207                                                      'returnaction'=>$returnaction,
1208                                                      'returnparams'=>http_build_query($returnparams));
1209             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1210             $link = $this->output->action_link($url, $icon);
1211             $separator = $this->output->spacer(array(), true);
1212         }
1214         return $link . $separator . $summary;
1215     }
1218     /**
1219      * Format the submission and feedback columns.
1220      *
1221      * @param string $colname The column name
1222      * @param stdClass $row The submission row
1223      * @return mixed string or NULL
1224      */
1225     public function other_cols($colname, $row) {
1226         // For extra user fields the result is already in $row.
1227         if (empty($this->plugincache[$colname])) {
1228             return $row->$colname;
1229         }
1231         // This must be a plugin field.
1232         $plugincache = $this->plugincache[$colname];
1234         $plugin = $plugincache[0];
1236         $field = null;
1237         if (isset($plugincache[1])) {
1238             $field = $plugincache[1];
1239         }
1241         if ($plugin->is_visible() && $plugin->is_enabled()) {
1242             if ($plugin->get_subtype() == 'assignsubmission') {
1243                 if ($this->assignment->get_instance()->teamsubmission) {
1244                     $group = false;
1245                     $submission = false;
1247                     $this->get_group_and_submission($row->id, $group, $submission, -1);
1248                     if ($submission) {
1249                         if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1250                             // For a newly reopened submission - we want to show the previous submission in the table.
1251                             $this->get_group_and_submission($row->id, $group, $submission, $submission->attemptnumber-1);
1252                         }
1253                         if (isset($field)) {
1254                             return $plugin->get_editor_text($field, $submission->id);
1255                         }
1256                         return $this->format_plugin_summary_with_link($plugin,
1257                                                                       $submission,
1258                                                                       'grading',
1259                                                                       array());
1260                     }
1261                 } else if ($row->submissionid) {
1262                     if ($row->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1263                         // For a newly reopened submission - we want to show the previous submission in the table.
1264                         $submission = $this->assignment->get_user_submission($row->userid, false, $row->attemptnumber - 1);
1265                     } else {
1266                         $submission = new stdClass();
1267                         $submission->id = $row->submissionid;
1268                         $submission->timecreated = $row->firstsubmission;
1269                         $submission->timemodified = $row->timesubmitted;
1270                         $submission->assignment = $this->assignment->get_instance()->id;
1271                         $submission->userid = $row->userid;
1272                         $submission->attemptnumber = $row->attemptnumber;
1273                     }
1274                     // Field is used for only for import/export and refers the the fieldname for the text editor.
1275                     if (isset($field)) {
1276                         return $plugin->get_editor_text($field, $submission->id);
1277                     }
1278                     return $this->format_plugin_summary_with_link($plugin,
1279                                                                   $submission,
1280                                                                   'grading',
1281                                                                   array());
1282                 }
1283             } else {
1284                 $grade = null;
1285                 if (isset($field)) {
1286                     return $plugin->get_editor_text($field, $row->gradeid);
1287                 }
1289                 if ($row->gradeid) {
1290                     $grade = new stdClass();
1291                     $grade->id = $row->gradeid;
1292                     $grade->timecreated = $row->firstmarked;
1293                     $grade->timemodified = $row->timemarked;
1294                     $grade->assignment = $this->assignment->get_instance()->id;
1295                     $grade->userid = $row->userid;
1296                     $grade->grade = $row->grade;
1297                     $grade->mailed = $row->mailed;
1298                     $grade->attemptnumber = $row->attemptnumber;
1299                 }
1300                 if ($this->quickgrading && $plugin->supports_quickgrading()) {
1301                     return $plugin->get_quickgrading_html($row->userid, $grade);
1302                 } else if ($grade) {
1303                     return $this->format_plugin_summary_with_link($plugin,
1304                                                                   $grade,
1305                                                                   'grading',
1306                                                                   array());
1307                 }
1308             }
1309         }
1310         return '';
1311     }
1313     /**
1314      * Using the current filtering and sorting - load all rows and return a single column from them.
1315      *
1316      * @param string $columnname The name of the raw column data
1317      * @return array of data
1318      */
1319     public function get_column_data($columnname) {
1320         $this->setup();
1321         $this->currpage = 0;
1322         $this->query_db($this->tablemaxrows);
1323         $result = array();
1324         foreach ($this->rawdata as $row) {
1325             $result[] = $row->$columnname;
1326         }
1327         return $result;
1328     }
1330     /**
1331      * Return things to the renderer.
1332      *
1333      * @return string the assignment name
1334      */
1335     public function get_assignment_name() {
1336         return $this->assignment->get_instance()->name;
1337     }
1339     /**
1340      * Return things to the renderer.
1341      *
1342      * @return int the course module id
1343      */
1344     public function get_course_module_id() {
1345         return $this->assignment->get_course_module()->id;
1346     }
1348     /**
1349      * Return things to the renderer.
1350      *
1351      * @return int the course id
1352      */
1353     public function get_course_id() {
1354         return $this->assignment->get_course()->id;
1355     }
1357     /**
1358      * Return things to the renderer.
1359      *
1360      * @return stdClass The course context
1361      */
1362     public function get_course_context() {
1363         return $this->assignment->get_course_context();
1364     }
1366     /**
1367      * Return things to the renderer.
1368      *
1369      * @return bool Does this assignment accept submissions
1370      */
1371     public function submissions_enabled() {
1372         return $this->assignment->is_any_submission_plugin_enabled();
1373     }
1375     /**
1376      * Return things to the renderer.
1377      *
1378      * @return bool Can this user view all grades (the gradebook)
1379      */
1380     public function can_view_all_grades() {
1381         $context = $this->assignment->get_course_context();
1382         return has_capability('gradereport/grader:view', $context) &&
1383                has_capability('moodle/grade:viewall', $context);
1384     }
1386     /**
1387      * Always return a valid sort - even if the userid column is missing.
1388      * @return array column name => SORT_... constant.
1389      */
1390     public function get_sort_columns() {
1391         $result = parent::get_sort_columns();
1392         $result = array_merge($result, array('userid' => SORT_ASC));
1393         return $result;
1394     }
1396     /**
1397      * Override the table show_hide_link to not show for select column.
1398      *
1399      * @param string $column the column name, index into various names.
1400      * @param int $index numerical index of the column.
1401      * @return string HTML fragment.
1402      */
1403     protected function show_hide_link($column, $index) {
1404         if ($index > 0 || !$this->hasgrade) {
1405             return parent::show_hide_link($column, $index);
1406         }
1407         return '';
1408     }
1410     /**
1411      * Overides setup to ensure it will only run a single time.
1412      */
1413     public function setup() {
1414         // Check if the setup function has been called before, we should not run it twice.
1415         // If we do the sortorder of the table will be broken.
1416         if (!empty($this->setup)) {
1417             return;
1418         }
1419         parent::setup();
1420     }