6ffecb8f5ffb6e42e2486b673a5d7325374ba3da
[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             $url = new moodle_url('/mod/assign/view.php', $urlparams);
833             $link = $this->output->action_link($url, $icon);
834             $grade .= $link . $separator;
835         }
837         $grade .= $this->display_grade($row->grade,
838                                        $this->quickgrading && !$gradingdisabled,
839                                        $row->userid,
840                                        $row->timemarked);
842         return $grade;
843     }
845     /**
846      * Format a column of data for display.
847      *
848      * @param stdClass $row
849      * @return string
850      */
851     public function col_finalgrade(stdClass $row) {
852         $o = '';
854         $grade = $this->get_gradebook_data_for_user($row->userid);
855         if ($grade) {
856             $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
857         }
859         return $o;
860     }
862     /**
863      * Format a column of data for display.
864      *
865      * @param stdClass $row
866      * @return string
867      */
868     public function col_timemarked(stdClass $row) {
869         $o = '-';
871         if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
872             $o = userdate($row->timemarked);
873         }
874         if ($row->timemarked && $this->is_downloading()) {
875             // Force it for downloads as it affects import.
876             $o = userdate($row->timemarked);
877         }
879         return $o;
880     }
882     /**
883      * Format a column of data for display.
884      *
885      * @param stdClass $row
886      * @return string
887      */
888     public function col_timesubmitted(stdClass $row) {
889         $o = '-';
891         $group = false;
892         $submission = false;
893         $this->get_group_and_submission($row->id, $group, $submission, -1);
894         if ($submission && $submission->timemodified && $submission->status != ASSIGN_SUBMISSION_STATUS_NEW) {
895             $o = userdate($submission->timemodified);
896         } else if ($row->timesubmitted) {
897             $o = userdate($row->timesubmitted);
898         }
900         return $o;
901     }
903     /**
904      * Format a column of data for display
905      *
906      * @param stdClass $row
907      * @return string
908      */
909     public function col_status(stdClass $row) {
910         $o = '';
912         $instance = $this->assignment->get_instance();
914         $due = $instance->duedate;
915         if ($row->extensionduedate) {
916             $due = $row->extensionduedate;
917         }
919         $group = false;
920         $submission = false;
921         $this->get_group_and_submission($row->id, $group, $submission, -1);
923         if ($instance->teamsubmission && !$group && !$instance->preventsubmissionnotingroup) {
924             $group = true;
925         }
927         if ($group && $submission) {
928             $timesubmitted = $submission->timemodified;
929             $status = $submission->status;
930         } else {
931             $timesubmitted = $row->timesubmitted;
932             $status = $row->status;
933         }
935         if ($this->assignment->is_any_submission_plugin_enabled()) {
937             $o .= $this->output->container(get_string('submissionstatus_' . $status, 'assign'),
938                                            array('class'=>'submissionstatus' .$status));
939             if ($due && $timesubmitted > $due) {
940                 $usertime = format_time($timesubmitted - $due);
941                 $latemessage = get_string('submittedlateshort',
942                                           'assign',
943                                           $usertime);
944                 $o .= $this->output->container($latemessage, 'latesubmission');
945             }
946             if ($row->locked) {
947                 $lockedstr = get_string('submissionslockedshort', 'assign');
948                 $o .= $this->output->container($lockedstr, 'lockedsubmission');
949             }
951             // Add status of "grading" if markflow is not enabled.
952             if (!$instance->markingworkflow) {
953                 if ($row->grade !== null && $row->grade >= 0) {
954                     $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
955                 } else if (!$timesubmitted) {
956                     $now = time();
957                     if ($due && ($now > $due)) {
958                         $overduestr = get_string('overdue', 'assign', format_time($now - $due));
959                         $o .= $this->output->container($overduestr, 'overduesubmission');
960                     }
961                 }
962             }
963         }
965         if ($instance->markingworkflow) {
966             $o .= $this->col_workflowstatus($row);
967         }
968         if ($row->extensionduedate) {
969             $userdate = userdate($row->extensionduedate);
970             $extensionstr = get_string('userextensiondate', 'assign', $userdate);
971             $o .= $this->output->container($extensionstr, 'extensiondate');
972         }
974         if ($this->is_downloading()) {
975             $o = strip_tags(str_replace('</div>', "\n", $o));
976         }
978         return $o;
979     }
981     /**
982      * Format a column of data for display.
983      *
984      * @param stdClass $row
985      * @return string
986      */
987     public function col_userid(stdClass $row) {
988         global $USER;
990         $edit = '';
992         $actions = array();
994         $urlparams = array('id'=>$this->assignment->get_course_module()->id,
995                            'rownum'=>$this->rownum,
996                            'action'=>'grade');
997         $url = new moodle_url('/mod/assign/view.php', $urlparams);
998         $noimage = null;
1000         if (!$row->grade) {
1001             $description = get_string('grade');
1002         } else {
1003             $description = get_string('updategrade', 'assign');
1004         }
1005         $actions['grade'] = new action_menu_link_secondary(
1006             $url,
1007             $noimage,
1008             $description
1009         );
1011         // Everything we need is in the row.
1012         $submission = $row;
1013         $flags = $row;
1014         if ($this->assignment->get_instance()->teamsubmission) {
1015             // Use the cache for this.
1016             $submission = false;
1017             $group = false;
1018             $this->get_group_and_submission($row->id, $group, $submission, -1);
1019         }
1021         $submissionsopen = $this->assignment->submissions_open($row->id,
1022                                                                true,
1023                                                                $submission,
1024                                                                $flags,
1025                                                                $this->gradinginfo);
1026         $caneditsubmission = $this->assignment->can_edit_submission($row->id, $USER->id);
1028         // Hide for offline assignments.
1029         if ($this->assignment->is_any_submission_plugin_enabled()) {
1030             if (!$row->status ||
1031                     $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
1032                     !$this->assignment->get_instance()->submissiondrafts) {
1034                 if (!$row->locked) {
1035                     $urlparams = array('id' => $this->assignment->get_course_module()->id,
1036                                        'userid'=>$row->id,
1037                                        'action'=>'lock',
1038                                        'sesskey'=>sesskey(),
1039                                        'page'=>$this->currpage);
1040                     $url = new moodle_url('/mod/assign/view.php', $urlparams);
1042                     $description = get_string('preventsubmissionsshort', 'assign');
1043                     $actions['lock'] = new action_menu_link_secondary(
1044                         $url,
1045                         $noimage,
1046                         $description
1047                     );
1048                 } else {
1049                     $urlparams = array('id' => $this->assignment->get_course_module()->id,
1050                                        'userid'=>$row->id,
1051                                        'action'=>'unlock',
1052                                        'sesskey'=>sesskey(),
1053                                        'page'=>$this->currpage);
1054                     $url = new moodle_url('/mod/assign/view.php', $urlparams);
1055                     $description = get_string('allowsubmissionsshort', 'assign');
1056                     $actions['unlock'] = new action_menu_link_secondary(
1057                         $url,
1058                         $noimage,
1059                         $description
1060                     );
1061                 }
1062             }
1064             if ($submissionsopen &&
1065                     $USER->id != $row->id &&
1066                     $caneditsubmission) {
1067                 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1068                                    'userid'=>$row->id,
1069                                    'action'=>'editsubmission',
1070                                    'sesskey'=>sesskey(),
1071                                    'page'=>$this->currpage);
1072                 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1073                 $description = get_string('editsubmission', 'assign');
1074                 $actions['editsubmission'] = new action_menu_link_secondary(
1075                     $url,
1076                     $noimage,
1077                     $description
1078                 );
1079             }
1080         }
1081         if (($this->assignment->get_instance()->duedate ||
1082                 $this->assignment->get_instance()->cutoffdate) &&
1083                 $this->hasgrantextension) {
1084              $urlparams = array('id' => $this->assignment->get_course_module()->id,
1085                                 'userid' => $row->id,
1086                                 'action' => 'grantextension',
1087                                 'sesskey' => sesskey(),
1088                                 'page' => $this->currpage);
1089              $url = new moodle_url('/mod/assign/view.php', $urlparams);
1090              $description = get_string('grantextension', 'assign');
1091              $actions['grantextension'] = new action_menu_link_secondary(
1092                  $url,
1093                  $noimage,
1094                  $description
1095              );
1096         }
1097         if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
1098                 $this->assignment->get_instance()->submissiondrafts) {
1099             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1100                                'userid'=>$row->id,
1101                                'action'=>'reverttodraft',
1102                                'sesskey'=>sesskey(),
1103                                'page'=>$this->currpage);
1104             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1105             $description = get_string('reverttodraftshort', 'assign');
1106             $actions['reverttodraft'] = new action_menu_link_secondary(
1107                 $url,
1108                 $noimage,
1109                 $description
1110             );
1111         }
1112         if ($row->status == ASSIGN_SUBMISSION_STATUS_DRAFT &&
1113                 $this->assignment->get_instance()->submissiondrafts &&
1114                 $caneditsubmission &&
1115                 $submissionsopen &&
1116                 $row->id != $USER->id) {
1117             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1118                                'userid'=>$row->id,
1119                                'action'=>'submitotherforgrading',
1120                                'sesskey'=>sesskey(),
1121                                'page'=>$this->currpage);
1122             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1123             $description = get_string('submitforgrading', 'assign');
1124             $actions['submitforgrading'] = new action_menu_link_secondary(
1125                 $url,
1126                 $noimage,
1127                 $description
1128             );
1129         }
1131         $ismanual = $this->assignment->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
1132         $hassubmission = !empty($row->status);
1133         $notreopened = $hassubmission && $row->status != ASSIGN_SUBMISSION_STATUS_REOPENED;
1134         $isunlimited = $this->assignment->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS;
1135         $hasattempts = $isunlimited || $row->attemptnumber < $this->assignment->get_instance()->maxattempts - 1;
1137         if ($ismanual && $hassubmission && $notreopened && $hasattempts) {
1138             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1139                                'userid'=>$row->id,
1140                                'action'=>'addattempt',
1141                                'sesskey'=>sesskey(),
1142                                'page'=>$this->currpage);
1143             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1144             $description = get_string('addattempt', 'assign');
1145             $actions['addattempt'] = new action_menu_link_secondary(
1146                 $url,
1147                 $noimage,
1148                 $description
1149             );
1150         }
1152         $menu = new action_menu();
1153         $menu->set_owner_selector('.gradingtable-actionmenu');
1154         $menu->set_alignment(action_menu::TL, action_menu::BL);
1155         $menu->set_constraint('.gradingtable > .no-overflow');
1156         $menu->set_menu_trigger(get_string('edit'));
1157         foreach ($actions as $action) {
1158             $menu->add($action);
1159         }
1161         // Prioritise the menu ahead of all other actions.
1162         $menu->prioritise = true;
1164         $edit .= $this->output->render($menu);
1166         return $edit;
1167     }
1169     /**
1170      * Write the plugin summary with an optional link to view the full feedback/submission.
1171      *
1172      * @param assign_plugin $plugin Submission plugin or feedback plugin
1173      * @param stdClass $item Submission or grade
1174      * @param string $returnaction The return action to pass to the
1175      *                             view_submission page (the current page)
1176      * @param string $returnparams The return params to pass to the view_submission
1177      *                             page (the current page)
1178      * @return string The summary with an optional link
1179      */
1180     private function format_plugin_summary_with_link(assign_plugin $plugin,
1181                                                      stdClass $item,
1182                                                      $returnaction,
1183                                                      $returnparams) {
1184         $link = '';
1185         $showviewlink = false;
1187         $summary = $plugin->view_summary($item, $showviewlink);
1188         $separator = '';
1189         if ($showviewlink) {
1190             $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
1191             $icon = $this->output->pix_icon('t/preview', $viewstr);
1192             $urlparams = array('id' => $this->assignment->get_course_module()->id,
1193                                                      'sid'=>$item->id,
1194                                                      'gid'=>$item->id,
1195                                                      'plugin'=>$plugin->get_type(),
1196                                                      'action'=>'viewplugin' . $plugin->get_subtype(),
1197                                                      'returnaction'=>$returnaction,
1198                                                      'returnparams'=>http_build_query($returnparams));
1199             $url = new moodle_url('/mod/assign/view.php', $urlparams);
1200             $link = $this->output->action_link($url, $icon);
1201             $separator = $this->output->spacer(array(), true);
1202         }
1204         return $link . $separator . $summary;
1205     }
1208     /**
1209      * Format the submission and feedback columns.
1210      *
1211      * @param string $colname The column name
1212      * @param stdClass $row The submission row
1213      * @return mixed string or NULL
1214      */
1215     public function other_cols($colname, $row) {
1216         // For extra user fields the result is already in $row.
1217         if (empty($this->plugincache[$colname])) {
1218             return $row->$colname;
1219         }
1221         // This must be a plugin field.
1222         $plugincache = $this->plugincache[$colname];
1224         $plugin = $plugincache[0];
1226         $field = null;
1227         if (isset($plugincache[1])) {
1228             $field = $plugincache[1];
1229         }
1231         if ($plugin->is_visible() && $plugin->is_enabled()) {
1232             if ($plugin->get_subtype() == 'assignsubmission') {
1233                 if ($this->assignment->get_instance()->teamsubmission) {
1234                     $group = false;
1235                     $submission = false;
1237                     $this->get_group_and_submission($row->id, $group, $submission, -1);
1238                     if ($submission) {
1239                         if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1240                             // For a newly reopened submission - we want to show the previous submission in the table.
1241                             $this->get_group_and_submission($row->id, $group, $submission, $submission->attemptnumber-1);
1242                         }
1243                         if (isset($field)) {
1244                             return $plugin->get_editor_text($field, $submission->id);
1245                         }
1246                         return $this->format_plugin_summary_with_link($plugin,
1247                                                                       $submission,
1248                                                                       'grading',
1249                                                                       array());
1250                     }
1251                 } else if ($row->submissionid) {
1252                     if ($row->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1253                         // For a newly reopened submission - we want to show the previous submission in the table.
1254                         $submission = $this->assignment->get_user_submission($row->userid, false, $row->attemptnumber - 1);
1255                     } else {
1256                         $submission = new stdClass();
1257                         $submission->id = $row->submissionid;
1258                         $submission->timecreated = $row->firstsubmission;
1259                         $submission->timemodified = $row->timesubmitted;
1260                         $submission->assignment = $this->assignment->get_instance()->id;
1261                         $submission->userid = $row->userid;
1262                         $submission->attemptnumber = $row->attemptnumber;
1263                     }
1264                     // Field is used for only for import/export and refers the the fieldname for the text editor.
1265                     if (isset($field)) {
1266                         return $plugin->get_editor_text($field, $submission->id);
1267                     }
1268                     return $this->format_plugin_summary_with_link($plugin,
1269                                                                   $submission,
1270                                                                   'grading',
1271                                                                   array());
1272                 }
1273             } else {
1274                 $grade = null;
1275                 if (isset($field)) {
1276                     return $plugin->get_editor_text($field, $row->gradeid);
1277                 }
1279                 if ($row->gradeid) {
1280                     $grade = new stdClass();
1281                     $grade->id = $row->gradeid;
1282                     $grade->timecreated = $row->firstmarked;
1283                     $grade->timemodified = $row->timemarked;
1284                     $grade->assignment = $this->assignment->get_instance()->id;
1285                     $grade->userid = $row->userid;
1286                     $grade->grade = $row->grade;
1287                     $grade->mailed = $row->mailed;
1288                     $grade->attemptnumber = $row->attemptnumber;
1289                 }
1290                 if ($this->quickgrading && $plugin->supports_quickgrading()) {
1291                     return $plugin->get_quickgrading_html($row->userid, $grade);
1292                 } else if ($grade) {
1293                     return $this->format_plugin_summary_with_link($plugin,
1294                                                                   $grade,
1295                                                                   'grading',
1296                                                                   array());
1297                 }
1298             }
1299         }
1300         return '';
1301     }
1303     /**
1304      * Using the current filtering and sorting - load all rows and return a single column from them.
1305      *
1306      * @param string $columnname The name of the raw column data
1307      * @return array of data
1308      */
1309     public function get_column_data($columnname) {
1310         $this->setup();
1311         $this->currpage = 0;
1312         $this->query_db($this->tablemaxrows);
1313         $result = array();
1314         foreach ($this->rawdata as $row) {
1315             $result[] = $row->$columnname;
1316         }
1317         return $result;
1318     }
1320     /**
1321      * Return things to the renderer.
1322      *
1323      * @return string the assignment name
1324      */
1325     public function get_assignment_name() {
1326         return $this->assignment->get_instance()->name;
1327     }
1329     /**
1330      * Return things to the renderer.
1331      *
1332      * @return int the course module id
1333      */
1334     public function get_course_module_id() {
1335         return $this->assignment->get_course_module()->id;
1336     }
1338     /**
1339      * Return things to the renderer.
1340      *
1341      * @return int the course id
1342      */
1343     public function get_course_id() {
1344         return $this->assignment->get_course()->id;
1345     }
1347     /**
1348      * Return things to the renderer.
1349      *
1350      * @return stdClass The course context
1351      */
1352     public function get_course_context() {
1353         return $this->assignment->get_course_context();
1354     }
1356     /**
1357      * Return things to the renderer.
1358      *
1359      * @return bool Does this assignment accept submissions
1360      */
1361     public function submissions_enabled() {
1362         return $this->assignment->is_any_submission_plugin_enabled();
1363     }
1365     /**
1366      * Return things to the renderer.
1367      *
1368      * @return bool Can this user view all grades (the gradebook)
1369      */
1370     public function can_view_all_grades() {
1371         $context = $this->assignment->get_course_context();
1372         return has_capability('gradereport/grader:view', $context) &&
1373                has_capability('moodle/grade:viewall', $context);
1374     }
1376     /**
1377      * Always return a valid sort - even if the userid column is missing.
1378      * @return array column name => SORT_... constant.
1379      */
1380     public function get_sort_columns() {
1381         $result = parent::get_sort_columns();
1382         $result = array_merge($result, array('userid' => SORT_ASC));
1383         return $result;
1384     }
1386     /**
1387      * Override the table show_hide_link to not show for select column.
1388      *
1389      * @param string $column the column name, index into various names.
1390      * @param int $index numerical index of the column.
1391      * @return string HTML fragment.
1392      */
1393     protected function show_hide_link($column, $index) {
1394         if ($index > 0 || !$this->hasgrade) {
1395             return parent::show_hide_link($column, $index);
1396         }
1397         return '';
1398     }