Merge branch 'MDL-44330' of git://github.com/NeillM/moodle
[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             foreach ($extrauserfields as $extrafield) {
277                 $columns[] = $extrafield;
278                 $headers[] = get_user_field_name($extrafield);
279             }
280         } else {
281             // Record ID.
282             $columns[] = 'recordid';
283             $headers[] = get_string('recordid', 'assign');
284         }
286         // Submission status.
287         $columns[] = 'status';
288         $headers[] = get_string('status', 'assign');
290         // Team submission columns.
291         if ($assignment->get_instance()->teamsubmission) {
292             $columns[] = 'team';
293             $headers[] = get_string('submissionteam', 'assign');
294         }
295         // Allocated marker.
296         if ($this->assignment->get_instance()->markingworkflow &&
297             $this->assignment->get_instance()->markingallocation &&
298             has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
299             // Add a column for the allocated marker.
300             $columns[] = 'allocatedmarker';
301             $headers[] = get_string('marker', 'assign');
302         }
303         // Grade.
304         $columns[] = 'grade';
305         $headers[] = get_string('grade');
306         if ($this->is_downloading()) {
307             if ($this->assignment->get_instance()->grade >= 0) {
308                 $columns[] = 'grademax';
309                 $headers[] = get_string('maxgrade', 'assign');
310             } else {
311                 // This is a custom scale.
312                 $columns[] = 'scale';
313                 $headers[] = get_string('scale', 'assign');
314             }
316             if ($this->assignment->get_instance()->markingworkflow) {
317                 // Add a column for the marking workflow state.
318                 $columns[] = 'workflowstate';
319                 $headers[] = get_string('markingworkflowstate', 'assign');
320             }
321             // Add a column for the list of valid marking workflow states.
322             $columns[] = 'gradecanbechanged';
323             $headers[] = get_string('gradecanbechanged', 'assign');
324         }
325         if (!$this->is_downloading() && $this->hasgrade) {
326             // We have to call this column userid so we can use userid as a default sortable column.
327             $columns[] = 'userid';
328             $headers[] = get_string('edit');
329         }
331         // Submission plugins.
332         if ($assignment->is_any_submission_plugin_enabled()) {
333             $columns[] = 'timesubmitted';
334             $headers[] = get_string('lastmodifiedsubmission', 'assign');
336             foreach ($this->assignment->get_submission_plugins() as $plugin) {
337                 if ($this->is_downloading()) {
338                     if ($plugin->is_visible() && $plugin->is_enabled()) {
339                         foreach ($plugin->get_editor_fields() as $field => $description) {
340                             $index = 'plugin' . count($this->plugincache);
341                             $this->plugincache[$index] = array($plugin, $field);
342                             $columns[] = $index;
343                             $headers[] = $plugin->get_name();
344                         }
345                     }
346                 } else {
347                     if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
348                         $index = 'plugin' . count($this->plugincache);
349                         $this->plugincache[$index] = array($plugin);
350                         $columns[] = $index;
351                         $headers[] = $plugin->get_name();
352                     }
353                 }
354             }
355         }
357         // Time marked.
358         $columns[] = 'timemarked';
359         $headers[] = get_string('lastmodifiedgrade', 'assign');
361         // Feedback plugins.
362         foreach ($this->assignment->get_feedback_plugins() as $plugin) {
363             if ($this->is_downloading()) {
364                 if ($plugin->is_visible() && $plugin->is_enabled()) {
365                     foreach ($plugin->get_editor_fields() as $field => $description) {
366                         $index = 'plugin' . count($this->plugincache);
367                         $this->plugincache[$index] = array($plugin, $field);
368                         $columns[] = $index;
369                         $headers[] = $description;
370                     }
371                 }
372             } else if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
373                 $index = 'plugin' . count($this->plugincache);
374                 $this->plugincache[$index] = array($plugin);
375                 $columns[] = $index;
376                 $headers[] = $plugin->get_name();
377             }
378         }
380         // Exclude 'Final grade' column in downloaded grading worksheets.
381         if (!$this->is_downloading()) {
382             // Final grade.
383             $columns[] = 'finalgrade';
384             $headers[] = get_string('finalgrade', 'grades');
385         }
387         // Load the grading info for all users.
388         $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id,
389                                               'mod',
390                                               'assign',
391                                               $this->assignment->get_instance()->id,
392                                               $users);
394         if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
395             $columns[] = 'outcomes';
396             $headers[] = get_string('outcomes', 'grades');
397         }
399         // Set the columns.
400         $this->define_columns($columns);
401         $this->define_headers($headers);
402         foreach ($extrauserfields as $extrafield) {
403              $this->column_class($extrafield, $extrafield);
404         }
405         $this->no_sorting('recordid');
406         $this->no_sorting('finalgrade');
407         $this->no_sorting('userid');
408         $this->no_sorting('select');
409         $this->no_sorting('outcomes');
411         if ($assignment->get_instance()->teamsubmission) {
412             $this->no_sorting('team');
413         }
415         $plugincolumnindex = 0;
416         foreach ($this->assignment->get_submission_plugins() as $plugin) {
417             if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
418                 $submissionpluginindex = 'plugin' . $plugincolumnindex++;
419                 $this->no_sorting($submissionpluginindex);
420             }
421         }
422         foreach ($this->assignment->get_feedback_plugins() as $plugin) {
423             if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
424                 $feedbackpluginindex = 'plugin' . $plugincolumnindex++;
425                 $this->no_sorting($feedbackpluginindex);
426             }
427         }
429         // When there is no data we still want the column headers printed in the csv file.
430         if ($this->is_downloading()) {
431             $this->start_output();
432         }
433     }
435     /**
436      * Before adding each row to the table make sure rownum is incremented.
437      *
438      * @param array $row row of data from db used to make one row of the table.
439      * @return array one row for the table
440      */
441     public function format_row($row) {
442         if ($this->rownum < 0) {
443             $this->rownum = $this->currpage * $this->pagesize;
444         } else {
445             $this->rownum += 1;
446         }
448         return parent::format_row($row);
449     }
451     /**
452      * Add a column with an ID that uniquely identifies this user in this assignment.
453      *
454      * @param stdClass $row
455      * @return string
456      */
457     public function col_recordid(stdClass $row) {
458         return get_string('hiddenuser', 'assign') .
459                $this->assignment->get_uniqueid_for_user($row->userid);
460     }
463     /**
464      * Add the userid to the row class so it can be updated via ajax.
465      *
466      * @param stdClass $row The row of data
467      * @return string The row class
468      */
469     public function get_row_class($row) {
470         return 'user' . $row->userid;
471     }
473     /**
474      * Return the number of rows to display on a single page.
475      *
476      * @return int The number of rows per page
477      */
478     public function get_rows_per_page() {
479         return $this->perpage;
480     }
482     /**
483      * list current marking workflow state
484      *
485      * @param stdClass $row
486      * @return string
487      */
488     public function col_workflowstatus(stdClass $row) {
489         $o = '';
491         $gradingdisabled = $this->assignment->grading_disabled($row->id);
492         // The function in the assignment keeps a static cache of this list of states.
493         $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
494         $workflowstate = $row->workflowstate;
495         if (empty($workflowstate)) {
496             $workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
497         }
498         if ($this->quickgrading && !$gradingdisabled) {
499             $notmarked = get_string('markingworkflowstatenotmarked', 'assign');
500             $name = 'quickgrade_' . $row->id . '_workflowstate';
501             $o .= html_writer::select($workflowstates, $name, $workflowstate, array('' => $notmarked));
502             // Check if this user is a marker that can't manage allocations and doesn't have the marker column added.
503             if ($this->assignment->get_instance()->markingworkflow &&
504                 $this->assignment->get_instance()->markingallocation &&
505                 !has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
507                 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
508                 $o .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $name,
509                         'value' => $row->allocatedmarker));
510             }
511         } else {
512             $o .= $this->output->container(get_string('markingworkflowstate' . $workflowstate, 'assign'), $workflowstate);
513         }
514         return $o;
515     }
517     /**
518      * For download only - list current marking workflow state
519      *
520      * @param stdClass $row - The row of data
521      * @return string The current marking workflow state
522      */
523     public function col_workflowstate($row) {
524         $state = $row->workflowstate;
525         if (empty($state)) {
526             $state = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
527         }
529         return get_string('markingworkflowstate' . $state, 'assign');
530     }
532     /**
533      * list current marker
534      *
535      * @param stdClass $row - The row of data
536      * @return id the user->id of the marker.
537      */
538     public function col_allocatedmarker(stdClass $row) {
539         static $markers = null;
540         static $markerlist = array();
541         if ($markers === null) {
542             list($sort, $params) = users_order_by_sql();
543             $markers = get_users_by_capability($this->assignment->get_context(), 'mod/assign:grade', '', $sort);
544             $markerlist[0] = get_string('choosemarker', 'assign');
545             foreach ($markers as $marker) {
546                 $markerlist[$marker->id] = fullname($marker);
547             }
548         }
549         if (empty($markerlist)) {
550             // TODO: add some form of notification here that no markers are available.
551             return '';
552         }
553         if ($this->is_downloading()) {
554             if (isset($markers[$row->allocatedmarker])) {
555                 return fullname($markers[$row->allocatedmarker]);
556             } else {
557                 return '';
558             }
559         }
561         if ($this->quickgrading && has_capability('mod/assign:manageallocations', $this->assignment->get_context()) &&
562             (empty($row->workflowstate) ||
563              $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_INMARKING ||
564              $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED)) {
566             $name = 'quickgrade_' . $row->id . '_allocatedmarker';
567             return  html_writer::select($markerlist, $name, $row->allocatedmarker, false);
568         } else if (!empty($row->allocatedmarker)) {
569             $output = '';
570             if ($this->quickgrading) { // Add hidden field for quickgrading page.
571                 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
572                 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$row->allocatedmarker));
573             }
574             $output .= $markerlist[$row->allocatedmarker];
575             return $output;
576         }
577     }
578     /**
579      * For download only - list all the valid options for this custom scale.
580      *
581      * @param stdClass $row - The row of data
582      * @return string A list of valid options for the current scale
583      */
584     public function col_scale($row) {
585         global $DB;
587         if (empty($this->scale)) {
588             $dbparams = array('id'=>-($this->assignment->get_instance()->grade));
589             $this->scale = $DB->get_record('scale', $dbparams);
590         }
592         if (!empty($this->scale->scale)) {
593             return implode("\n", explode(',', $this->scale->scale));
594         }
595         return '';
596     }
598     /**
599      * Display a grade with scales etc.
600      *
601      * @param string $grade
602      * @param boolean $editable
603      * @param int $userid The user id of the user this grade belongs to
604      * @param int $modified Timestamp showing when the grade was last modified
605      * @return string The formatted grade
606      */
607     public function display_grade($grade, $editable, $userid, $modified) {
608         if ($this->is_downloading()) {
609             if ($this->assignment->get_instance()->grade >= 0) {
610                 if ($grade == -1 || $grade === null) {
611                     return '';
612                 }
613                 return format_float($grade, 2);
614             } else {
615                 // This is a custom scale.
616                 $scale = $this->assignment->display_grade($grade, false);
617                 if ($scale == '-') {
618                     $scale = '';
619                 }
620                 return $scale;
621             }
622         }
623         return $this->assignment->display_grade($grade, $editable, $userid, $modified);
624     }
626     /**
627      * Get the team info for this user.
628      *
629      * @param stdClass $row
630      * @return string The team name
631      */
632     public function col_team(stdClass $row) {
633         $submission = false;
634         $group = false;
635         $this->get_group_and_submission($row->id, $group, $submission, -1);
636         if ($group) {
637             return $group->name;
638         } else if ($this->assignment->get_instance()->preventsubmissionnotingroup) {
639             $usergroups = $this->assignment->get_all_groups($row->id);
640             if (count($usergroups) > 1) {
641                 return get_string('multipleteamsgrader', 'assign');
642             } else {
643                 return get_string('noteamgrader', 'assign');
644             }
645         }
646         return get_string('defaultteam', 'assign');
647     }
649     /**
650      * Use a static cache to try and reduce DB calls.
651      *
652      * @param int $userid The user id for this submission
653      * @param int $group The groupid (returned)
654      * @param stdClass|false $submission The stdClass submission or false (returned)
655      * @param int $attemptnumber Return a specific attempt number (-1 for latest)
656      */
657     protected function get_group_and_submission($userid, &$group, &$submission, $attemptnumber) {
658         $group = false;
659         if (isset($this->submissiongroups[$userid])) {
660             $group = $this->submissiongroups[$userid];
661         } else {
662             $group = $this->assignment->get_submission_group($userid, false);
663             $this->submissiongroups[$userid] = $group;
664         }
666         $groupid = 0;
667         if ($group) {
668             $groupid = $group->id;
669         }
671         // Static cache is keyed by groupid and attemptnumber.
672         // We may need both the latest and previous attempt in the same page.
673         if (isset($this->groupsubmissions[$groupid . ':' . $attemptnumber])) {
674             $submission = $this->groupsubmissions[$groupid . ':' . $attemptnumber];
675         } else {
676             $submission = $this->assignment->get_group_submission($userid, $groupid, false, $attemptnumber);
677             $this->groupsubmissions[$groupid . ':' . $attemptnumber] = $submission;
678         }
679     }
681     /**
682      * Format a list of outcomes.
683      *
684      * @param stdClass $row
685      * @return string
686      */
687     public function col_outcomes(stdClass $row) {
688         $outcomes = '';
689         foreach ($this->gradinginfo->outcomes as $index => $outcome) {
690             $options = make_grades_menu(-$outcome->scaleid);
692             $options[0] = get_string('nooutcome', 'grades');
693             if ($this->quickgrading && !($outcome->grades[$row->userid]->locked)) {
694                 $select = '<select name="outcome_' . $index . '_' . $row->userid . '" class="quickgrade">';
695                 foreach ($options as $optionindex => $optionvalue) {
696                     $selected = '';
697                     if ($outcome->grades[$row->userid]->grade == $optionindex) {
698                         $selected = 'selected="selected"';
699                     }
700                     $select .= '<option value="' . $optionindex . '"' . $selected . '>' . $optionvalue . '</option>';
701                 }
702                 $select .= '</select>';
703                 $outcomes .= $this->output->container($outcome->name . ': ' . $select, 'outcome');
704             } else {
705                 $name = $outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade];
706                 if ($this->is_downloading()) {
707                     $outcomes .= $name;
708                 } else {
709                     $outcomes .= $this->output->container($name, 'outcome');
710                 }
711             }
712         }
714         return $outcomes;
715     }
718     /**
719      * Format a user picture for display.
720      *
721      * @param stdClass $row
722      * @return string
723      */
724     public function col_picture(stdClass $row) {
725         return $this->output->user_picture($row);
726     }
728     /**
729      * Format a user record for display (link to profile).
730      *
731      * @param stdClass $row
732      * @return string
733      */
734     public function col_fullname($row) {
735         if (!$this->is_downloading()) {
736             $courseid = $this->assignment->get_course()->id;
737             $link= new moodle_url('/user/view.php', array('id' =>$row->id, 'course'=>$courseid));
738             $fullname = $this->output->action_link($link, $this->assignment->fullname($row));
739         } else {
740             $fullname = $this->assignment->fullname($row);
741         }
743         if (!$this->assignment->is_active_user($row->id)) {
744             $suspendedstring = get_string('userenrolmentsuspended', 'grades');
745             $fullname .= ' ' . html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/enrolmentsuspended'),
746                 'title' => $suspendedstring, 'alt' => $suspendedstring, 'class' => 'usersuspendedicon'));
747             $fullname = html_writer::tag('span', $fullname, array('class' => 'usersuspended'));
748         }
749         return $fullname;
750     }
752     /**
753      * Insert a checkbox for selecting the current row for batch operations.
754      *
755      * @param stdClass $row
756      * @return string
757      */
758     public function col_select(stdClass $row) {
759         $selectcol = '<label class="accesshide" for="selectuser_' . $row->userid . '">';
760         $selectcol .= get_string('selectuser', 'assign', $this->assignment->fullname($row));
761         $selectcol .= '</label>';
762         $selectcol .= '<input type="checkbox"
763                               id="selectuser_' . $row->userid . '"
764                               name="selectedusers"
765                               value="' . $row->userid . '"/>';
766         $selectcol .= '<input type="hidden"
767                               name="grademodified_' . $row->userid . '"
768                               value="' . $row->timemarked . '"/>';
769         return $selectcol;
770     }
772     /**
773      * Return a users grades from the listing of all grade data for this assignment.
774      *
775      * @param int $userid
776      * @return mixed stdClass or false
777      */
778     private function get_gradebook_data_for_user($userid) {
779         if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
780             return $this->gradinginfo->items[0]->grades[$userid];
781         }
782         return false;
783     }
785     /**
786      * Format a column of data for display.
787      *
788      * @param stdClass $row
789      * @return string
790      */
791     public function col_gradecanbechanged(stdClass $row) {
792         $gradingdisabled = $this->assignment->grading_disabled($row->id);
793         if ($gradingdisabled) {
794             return get_string('no');
795         } else {
796             return get_string('yes');
797         }
798     }
800     /**
801      * Format a column of data for display
802      *
803      * @param stdClass $row
804      * @return string
805      */
806     public function col_grademax(stdClass $row) {
807         return format_float($this->assignment->get_instance()->grade, 2);
808     }
810     /**
811      * Format a column of data for display.
812      *
813      * @param stdClass $row
814      * @return string
815      */
816     public function col_grade(stdClass $row) {
817         $o = '';
819         $link = '';
820         $separator = $this->output->spacer(array(), true);
821         $grade = '';
822         $gradingdisabled = $this->assignment->grading_disabled($row->id);
824         if (!$this->is_downloading() && $this->hasgrade) {
825             $name = $this->assignment->fullname($row);
826             $icon = $this->output->pix_icon('gradefeedback',
827                                             get_string('gradeuser', 'assign', $name),
828                                             'mod_assign');
829             $urlparams = array('id' => $this->assignment->get_course_module()->id,
830                                'rownum'=>$this->rownum,
831                                'action' => 'grade',
832                                'useridlistid' => $this->assignment->get_useridlist_key_id());
833             $url = new moodle_url('/mod/assign/view.php', $urlparams);
834             $link = $this->output->action_link($url, $icon);
835             $grade .= $link . $separator;
836         }
838         $grade .= $this->display_grade($row->grade,
839                                        $this->quickgrading && !$gradingdisabled,
840                                        $row->userid,
841                                        $row->timemarked);
843         return $grade;
844     }
846     /**
847      * Format a column of data for display.
848      *
849      * @param stdClass $row
850      * @return string
851      */
852     public function col_finalgrade(stdClass $row) {
853         $o = '';
855         $grade = $this->get_gradebook_data_for_user($row->userid);
856         if ($grade) {
857             $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
858         }
860         return $o;
861     }
863     /**
864      * Format a column of data for display.
865      *
866      * @param stdClass $row
867      * @return string
868      */
869     public function col_timemarked(stdClass $row) {
870         $o = '-';
872         if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
873             $o = userdate($row->timemarked);
874         }
875         if ($row->timemarked && $this->is_downloading()) {
876             // Force it for downloads as it affects import.
877             $o = userdate($row->timemarked);
878         }
880         return $o;
881     }
883     /**
884      * Format a column of data for display.
885      *
886      * @param stdClass $row
887      * @return string
888      */
889     public function col_timesubmitted(stdClass $row) {
890         $o = '-';
892         $group = false;
893         $submission = false;
894         $this->get_group_and_submission($row->id, $group, $submission, -1);
895         if ($submission && $submission->timemodified && $submission->status != ASSIGN_SUBMISSION_STATUS_NEW) {
896             $o = userdate($submission->timemodified);
897         } else if ($row->timesubmitted) {
898             $o = userdate($row->timesubmitted);
899         }
901         return $o;
902     }
904     /**
905      * Format a column of data for display
906      *
907      * @param stdClass $row
908      * @return string
909      */
910     public function col_status(stdClass $row) {
911         $o = '';
913         $instance = $this->assignment->get_instance();
915         $due = $instance->duedate;
916         if ($row->extensionduedate) {
917             $due = $row->extensionduedate;
918         }
920         $group = false;
921         $submission = false;
922         $this->get_group_and_submission($row->id, $group, $submission, -1);
924         if ($instance->teamsubmission && !$group && !$instance->preventsubmissionnotingroup) {
925             $group = true;
926         }
928         if ($group && $submission) {
929             $timesubmitted = $submission->timemodified;
930             $status = $submission->status;
931         } else {
932             $timesubmitted = $row->timesubmitted;
933             $status = $row->status;
934         }
936         if ($this->assignment->is_any_submission_plugin_enabled()) {
938             $o .= $this->output->container(get_string('submissionstatus_' . $status, 'assign'),
939                                            array('class'=>'submissionstatus' .$status));
940             if ($due && $timesubmitted > $due) {
941                 $usertime = format_time($timesubmitted - $due);
942                 $latemessage = get_string('submittedlateshort',
943                                           'assign',
944                                           $usertime);
945                 $o .= $this->output->container($latemessage, 'latesubmission');
946             }
947             if ($row->locked) {
948                 $lockedstr = get_string('submissionslockedshort', 'assign');
949                 $o .= $this->output->container($lockedstr, 'lockedsubmission');
950             }
952             // Add status of "grading" if markflow is not enabled.
953             if (!$instance->markingworkflow) {
954                 if ($row->grade !== null && $row->grade >= 0) {
955                     $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
956                 } else if (!$timesubmitted) {
957                     $now = time();
958                     if ($due && ($now > $due)) {
959                         $overduestr = get_string('overdue', 'assign', format_time($now - $due));
960                         $o .= $this->output->container($overduestr, 'overduesubmission');
961                     }
962                 }
963             }
964         }
966         if ($instance->markingworkflow) {
967             $o .= $this->col_workflowstatus($row);
968         }
969         if ($row->extensionduedate) {
970             $userdate = userdate($row->extensionduedate);
971             $extensionstr = get_string('userextensiondate', 'assign', $userdate);
972             $o .= $this->output->container($extensionstr, 'extensiondate');
973         }
975         if ($this->is_downloading()) {
976             $o = strip_tags(rtrim(str_replace('</div>', ' - ', $o), '- '));
977         }
979         return $o;
980     }
982     /**
983      * Format a column of data for display.
984      *
985      * @param stdClass $row
986      * @return string
987      */
988     public function col_userid(stdClass $row) {
989         global $USER;
991         $edit = '';
993         $actions = array();
995         $urlparams = array('id'=>$this->assignment->get_course_module()->id,
996                            'rownum'=>$this->rownum,
997                            'action' => 'grade',
998                            'useridlistid' => $this->assignment->get_useridlist_key_id());
999         $url = new moodle_url('/mod/assign/view.php', $urlparams);
1000         $noimage = null;
1002         if (!$row->grade) {
1003             $description = get_string('grade');
1004         } else {
1005             $description = get_string('updategrade', 'assign');
1006         }
1007         $actions['grade'] = new action_menu_link_secondary(
1008             $url,
1009             $noimage,
1010             $description
1011         );
1013         // Everything we need is in the row.
1014         $submission = $row;
1015         $flags = $row;
1016         if ($this->assignment->get_instance()->teamsubmission) {
1017             // Use the cache for this.
1018             $submission = false;
1019             $group = false;
1020             $this->get_group_and_submission($row->id, $group, $submission, -1);
1021         }
1023         $submissionsopen = $this->assignment->submissions_open($row->id,
1024                                                                true,
1025                                                                $submission,
1026                                                                $flags,
1027                                                                $this->gradinginfo);
1028         $caneditsubmission = $this->assignment->can_edit_submission($row->id, $USER->id);
1030         // Hide for offline assignments.
1031         if ($this->assignment->is_any_submission_plugin_enabled()) {
1032             if (!$row->status ||
1033                     $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
1034                     !$this->assignment->get_instance()->submissiondrafts) {
1036                 if (!$row->locked) {
1037                     $urlparams = array('id' => $this->assignment->get_course_module()->id,
1038                                        'userid'=>$row->id,
1039                                        'action'=>'lock',
1040                                        'sesskey'=>sesskey(),
1041                                        'page'=>$this->currpage);
1042                     $url = new moodle_url('/mod/assign/view.php', $urlparams);
1044                     $description = get_string('preventsubmissionsshort', 'assign');
1045                     $actions['lock'] = new action_menu_link_secondary(
1046                         $url,
1047                         $noimage,
1048                         $description
1049                     );
1050                 } else {
1051                     $urlparams = array('id' => $this->assignment->get_course_module()->id,
1052                                        'userid'=>$row->id,
1053                                        'action'=>'unlock',
1054                                        'sesskey'=>sesskey(),
1055                                        'page'=>$this->currpage);
1056                     $url = new moodle_url('/mod/assign/view.php', $urlparams);
1057                     $description = get_string('allowsubmissionsshort', 'assign');
1058                     $actions['unlock'] = new action_menu_link_secondary(
1059                         $url,
1060                         $noimage,
1061                         $description
1062                     );
1063                 }
1064             }
1066             if ($submissionsopen &&
1067                     $USER->id != $row->id &&
1068                     $caneditsubmission) {
1069                 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1070                                    'userid'=>$row->id,
1071                                    'action'=>'editsubmission',
1072                                    'sesskey'=>sesskey(),
1073                                    'page'=>$this->currpage);
1074                 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1075                 $description = get_string('editsubmission', 'assign');
1076                 $actions['editsubmission'] = new action_menu_link_secondary(
1077                     $url,
1078                     $noimage,
1079                     $description
1080                 );
1081             }
1082         }
1083         if (($this->assignment->get_instance()->duedate ||
1084                 $this->assignment->get_instance()->cutoffdate) &&
1085                 $this->hasgrantextension) {
1086              $urlparams = array('id' => $this->assignment->get_course_module()->id,
1087                                 'userid' => $row->id,
1088                                 'action' => 'grantextension',
1089                                 'sesskey' => sesskey(),
1090                                 'page' => $this->currpage);
1091              $url = new moodle_url('/mod/assign/view.php', $urlparams);
1092              $description = get_string('grantextension', 'assign');
1093              $actions['grantextension'] = new action_menu_link_secondary(
1094                  $url,
1095                  $noimage,
1096                  $description
1097              );
1098         }
1099         if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
1100                 $this->assignment->get_instance()->submissiondrafts) {
1101             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1102                                'userid'=>$row->id,
1103                                'action'=>'reverttodraft',
1104                                'sesskey'=>sesskey(),
1105                                'page'=>$this->currpage);
1106             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1107             $description = get_string('reverttodraftshort', 'assign');
1108             $actions['reverttodraft'] = new action_menu_link_secondary(
1109                 $url,
1110                 $noimage,
1111                 $description
1112             );
1113         }
1114         if ($row->status == ASSIGN_SUBMISSION_STATUS_DRAFT &&
1115                 $this->assignment->get_instance()->submissiondrafts &&
1116                 $caneditsubmission &&
1117                 $submissionsopen &&
1118                 $row->id != $USER->id) {
1119             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1120                                'userid'=>$row->id,
1121                                'action'=>'submitotherforgrading',
1122                                'sesskey'=>sesskey(),
1123                                'page'=>$this->currpage);
1124             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1125             $description = get_string('submitforgrading', 'assign');
1126             $actions['submitforgrading'] = new action_menu_link_secondary(
1127                 $url,
1128                 $noimage,
1129                 $description
1130             );
1131         }
1133         $ismanual = $this->assignment->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
1134         $hassubmission = !empty($row->status);
1135         $notreopened = $hassubmission && $row->status != ASSIGN_SUBMISSION_STATUS_REOPENED;
1136         $isunlimited = $this->assignment->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS;
1137         $hasattempts = $isunlimited || $row->attemptnumber < $this->assignment->get_instance()->maxattempts - 1;
1139         if ($ismanual && $hassubmission && $notreopened && $hasattempts) {
1140             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1141                                'userid'=>$row->id,
1142                                'action'=>'addattempt',
1143                                'sesskey'=>sesskey(),
1144                                'page'=>$this->currpage);
1145             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1146             $description = get_string('addattempt', 'assign');
1147             $actions['addattempt'] = new action_menu_link_secondary(
1148                 $url,
1149                 $noimage,
1150                 $description
1151             );
1152         }
1154         $menu = new action_menu();
1155         $menu->set_owner_selector('.gradingtable-actionmenu');
1156         $menu->set_alignment(action_menu::TL, action_menu::BL);
1157         $menu->set_constraint('.gradingtable > .no-overflow');
1158         $menu->set_menu_trigger(get_string('edit'));
1159         foreach ($actions as $action) {
1160             $menu->add($action);
1161         }
1163         // Prioritise the menu ahead of all other actions.
1164         $menu->prioritise = true;
1166         $edit .= $this->output->render($menu);
1168         return $edit;
1169     }
1171     /**
1172      * Write the plugin summary with an optional link to view the full feedback/submission.
1173      *
1174      * @param assign_plugin $plugin Submission plugin or feedback plugin
1175      * @param stdClass $item Submission or grade
1176      * @param string $returnaction The return action to pass to the
1177      *                             view_submission page (the current page)
1178      * @param string $returnparams The return params to pass to the view_submission
1179      *                             page (the current page)
1180      * @return string The summary with an optional link
1181      */
1182     private function format_plugin_summary_with_link(assign_plugin $plugin,
1183                                                      stdClass $item,
1184                                                      $returnaction,
1185                                                      $returnparams) {
1186         $link = '';
1187         $showviewlink = false;
1189         $summary = $plugin->view_summary($item, $showviewlink);
1190         $separator = '';
1191         if ($showviewlink) {
1192             $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
1193             $icon = $this->output->pix_icon('t/preview', $viewstr);
1194             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1195                                                      'sid'=>$item->id,
1196                                                      'gid'=>$item->id,
1197                                                      'plugin'=>$plugin->get_type(),
1198                                                      'action'=>'viewplugin' . $plugin->get_subtype(),
1199                                                      'returnaction'=>$returnaction,
1200                                                      'returnparams'=>http_build_query($returnparams));
1201             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1202             $link = $this->output->action_link($url, $icon);
1203             $separator = $this->output->spacer(array(), true);
1204         }
1206         return $link . $separator . $summary;
1207     }
1210     /**
1211      * Format the submission and feedback columns.
1212      *
1213      * @param string $colname The column name
1214      * @param stdClass $row The submission row
1215      * @return mixed string or NULL
1216      */
1217     public function other_cols($colname, $row) {
1218         // For extra user fields the result is already in $row.
1219         if (empty($this->plugincache[$colname])) {
1220             return $row->$colname;
1221         }
1223         // This must be a plugin field.
1224         $plugincache = $this->plugincache[$colname];
1226         $plugin = $plugincache[0];
1228         $field = null;
1229         if (isset($plugincache[1])) {
1230             $field = $plugincache[1];
1231         }
1233         if ($plugin->is_visible() && $plugin->is_enabled()) {
1234             if ($plugin->get_subtype() == 'assignsubmission') {
1235                 if ($this->assignment->get_instance()->teamsubmission) {
1236                     $group = false;
1237                     $submission = false;
1239                     $this->get_group_and_submission($row->id, $group, $submission, -1);
1240                     if ($submission) {
1241                         if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1242                             // For a newly reopened submission - we want to show the previous submission in the table.
1243                             $this->get_group_and_submission($row->id, $group, $submission, $submission->attemptnumber-1);
1244                         }
1245                         if (isset($field)) {
1246                             return $plugin->get_editor_text($field, $submission->id);
1247                         }
1248                         return $this->format_plugin_summary_with_link($plugin,
1249                                                                       $submission,
1250                                                                       'grading',
1251                                                                       array());
1252                     }
1253                 } else if ($row->submissionid) {
1254                     if ($row->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1255                         // For a newly reopened submission - we want to show the previous submission in the table.
1256                         $submission = $this->assignment->get_user_submission($row->userid, false, $row->attemptnumber - 1);
1257                     } else {
1258                         $submission = new stdClass();
1259                         $submission->id = $row->submissionid;
1260                         $submission->timecreated = $row->firstsubmission;
1261                         $submission->timemodified = $row->timesubmitted;
1262                         $submission->assignment = $this->assignment->get_instance()->id;
1263                         $submission->userid = $row->userid;
1264                         $submission->attemptnumber = $row->attemptnumber;
1265                     }
1266                     // Field is used for only for import/export and refers the the fieldname for the text editor.
1267                     if (isset($field)) {
1268                         return $plugin->get_editor_text($field, $submission->id);
1269                     }
1270                     return $this->format_plugin_summary_with_link($plugin,
1271                                                                   $submission,
1272                                                                   'grading',
1273                                                                   array());
1274                 }
1275             } else {
1276                 $grade = null;
1277                 if (isset($field)) {
1278                     return $plugin->get_editor_text($field, $row->gradeid);
1279                 }
1281                 if ($row->gradeid) {
1282                     $grade = new stdClass();
1283                     $grade->id = $row->gradeid;
1284                     $grade->timecreated = $row->firstmarked;
1285                     $grade->timemodified = $row->timemarked;
1286                     $grade->assignment = $this->assignment->get_instance()->id;
1287                     $grade->userid = $row->userid;
1288                     $grade->grade = $row->grade;
1289                     $grade->mailed = $row->mailed;
1290                     $grade->attemptnumber = $row->attemptnumber;
1291                 }
1292                 if ($this->quickgrading && $plugin->supports_quickgrading()) {
1293                     return $plugin->get_quickgrading_html($row->userid, $grade);
1294                 } else if ($grade) {
1295                     return $this->format_plugin_summary_with_link($plugin,
1296                                                                   $grade,
1297                                                                   'grading',
1298                                                                   array());
1299                 }
1300             }
1301         }
1302         return '';
1303     }
1305     /**
1306      * Using the current filtering and sorting - load all rows and return a single column from them.
1307      *
1308      * @param string $columnname The name of the raw column data
1309      * @return array of data
1310      */
1311     public function get_column_data($columnname) {
1312         $this->setup();
1313         $this->currpage = 0;
1314         $this->query_db($this->tablemaxrows);
1315         $result = array();
1316         foreach ($this->rawdata as $row) {
1317             $result[] = $row->$columnname;
1318         }
1319         return $result;
1320     }
1322     /**
1323      * Return things to the renderer.
1324      *
1325      * @return string the assignment name
1326      */
1327     public function get_assignment_name() {
1328         return $this->assignment->get_instance()->name;
1329     }
1331     /**
1332      * Return things to the renderer.
1333      *
1334      * @return int the course module id
1335      */
1336     public function get_course_module_id() {
1337         return $this->assignment->get_course_module()->id;
1338     }
1340     /**
1341      * Return things to the renderer.
1342      *
1343      * @return int the course id
1344      */
1345     public function get_course_id() {
1346         return $this->assignment->get_course()->id;
1347     }
1349     /**
1350      * Return things to the renderer.
1351      *
1352      * @return stdClass The course context
1353      */
1354     public function get_course_context() {
1355         return $this->assignment->get_course_context();
1356     }
1358     /**
1359      * Return things to the renderer.
1360      *
1361      * @return bool Does this assignment accept submissions
1362      */
1363     public function submissions_enabled() {
1364         return $this->assignment->is_any_submission_plugin_enabled();
1365     }
1367     /**
1368      * Return things to the renderer.
1369      *
1370      * @return bool Can this user view all grades (the gradebook)
1371      */
1372     public function can_view_all_grades() {
1373         $context = $this->assignment->get_course_context();
1374         return has_capability('gradereport/grader:view', $context) &&
1375                has_capability('moodle/grade:viewall', $context);
1376     }
1378     /**
1379      * Always return a valid sort - even if the userid column is missing.
1380      * @return array column name => SORT_... constant.
1381      */
1382     public function get_sort_columns() {
1383         $result = parent::get_sort_columns();
1384         $result = array_merge($result, array('userid' => SORT_ASC));
1385         return $result;
1386     }
1388     /**
1389      * Override the table show_hide_link to not show for select column.
1390      *
1391      * @param string $column the column name, index into various names.
1392      * @param int $index numerical index of the column.
1393      * @return string HTML fragment.
1394      */
1395     protected function show_hide_link($column, $index) {
1396         if ($index > 0 || !$this->hasgrade) {
1397             return parent::show_hide_link($column, $index);
1398         }
1399         return '';
1400     }
1402     /**
1403      * Overides setup to ensure it will only run a single time.
1404      */
1405     public function setup() {
1406         // Check if the setup function has been called before, we should not run it twice.
1407         // If we do the sortorder of the table will be broken.
1408         if (!empty($this->setup)) {
1409             return;
1410         }
1411         parent::setup();
1412     }