Merge branch 'MDL-42411-master' of git://github.com/damyon/moodle
[moodle.git] / mod / assign / gradingtable.php
blob20b08add3995f443a273cbd29641a6615b3b3a2d
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This file contains the definition for the grading table which subclassses easy_table
20 * @package mod_assign
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->libdir.'/tablelib.php');
28 require_once($CFG->libdir.'/gradelib.php');
29 require_once($CFG->dirroot.'/mod/assign/locallib.php');
31 /**
32 * Extends table_sql to provide a table of assignment submissions
34 * @package mod_assign
35 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class assign_grading_table extends table_sql implements renderable {
39 /** @var assign $assignment */
40 private $assignment = null;
41 /** @var int $perpage */
42 private $perpage = 10;
43 /** @var int $rownum (global index of current row in table) */
44 private $rownum = -1;
45 /** @var renderer_base for getting output */
46 private $output = null;
47 /** @var stdClass gradinginfo */
48 private $gradinginfo = null;
49 /** @var int $tablemaxrows */
50 private $tablemaxrows = 10000;
51 /** @var boolean $quickgrading */
52 private $quickgrading = false;
53 /** @var boolean $hasgrantextension - Only do the capability check once for the entire table */
54 private $hasgrantextension = false;
55 /** @var array $groupsubmissions - A static cache of group submissions */
56 private $groupsubmissions = array();
57 /** @var array $submissiongroups - A static cache of submission groups */
58 private $submissiongroups = array();
59 /** @var string $plugingradingbatchoperations - List of plugin supported batch operations */
60 public $plugingradingbatchoperations = array();
61 /** @var array $plugincache - A cache of plugin lookups to match a column name to a plugin efficiently */
62 private $plugincache = array();
63 /** @var array $scale - A list of the keys and descriptions for the custom scale */
64 private $scale = null;
66 /**
67 * overridden constructor keeps a reference to the assignment class that is displaying this table
69 * @param assign $assignment The assignment class
70 * @param int $perpage how many per page
71 * @param string $filter The current filter
72 * @param int $rowoffset For showing a subsequent page of results
73 * @param bool $quickgrading Is this table wrapped in a quickgrading form?
74 * @param string $downloadfilename
76 public function __construct(assign $assignment,
77 $perpage,
78 $filter,
79 $rowoffset,
80 $quickgrading,
81 $downloadfilename = null) {
82 global $CFG, $PAGE, $DB, $USER;
83 parent::__construct('mod_assign_grading');
84 $this->assignment = $assignment;
86 foreach ($assignment->get_feedback_plugins() as $plugin) {
87 if ($plugin->is_visible() && $plugin->is_enabled()) {
88 foreach ($plugin->get_grading_batch_operations() as $action => $description) {
89 if (empty($this->plugingradingbatchoperations)) {
90 $this->plugingradingbatchoperations[$plugin->get_type()] = array();
92 $this->plugingradingbatchoperations[$plugin->get_type()][$action] = $description;
96 $this->perpage = $perpage;
97 $this->quickgrading = $quickgrading;
98 $this->output = $PAGE->get_renderer('mod_assign');
100 $urlparams = array('action'=>'grading', 'id'=>$assignment->get_course_module()->id);
101 $url = new moodle_url($CFG->wwwroot . '/mod/assign/view.php', $urlparams);
102 $this->define_baseurl($url);
104 // Do some business - then set the sql.
105 $currentgroup = groups_get_activity_group($assignment->get_course_module(), true);
107 if ($rowoffset) {
108 $this->rownum = $rowoffset - 1;
111 $users = array_keys( $assignment->list_participants($currentgroup, true));
112 if (count($users) == 0) {
113 // Insert a record that will never match to the sql is still valid.
114 $users[] = -1;
117 $params = array();
118 $params['assignmentid1'] = (int)$this->assignment->get_instance()->id;
119 $params['assignmentid2'] = (int)$this->assignment->get_instance()->id;
120 $params['assignmentid3'] = (int)$this->assignment->get_instance()->id;
121 $params['assignmentid4'] = (int)$this->assignment->get_instance()->id;
122 $params['assignmentid5'] = (int)$this->assignment->get_instance()->id;
124 $extrauserfields = get_extra_user_fields($this->assignment->get_context());
126 $fields = user_picture::fields('u', $extrauserfields) . ', ';
127 $fields .= 'u.id as userid, ';
128 $fields .= 's.status as status, ';
129 $fields .= 's.id as submissionid, ';
130 $fields .= 's.timecreated as firstsubmission, ';
131 $fields .= 's.timemodified as timesubmitted, ';
132 $fields .= 's.attemptnumber as attemptnumber, ';
133 $fields .= 'g.id as gradeid, ';
134 $fields .= 'g.grade as grade, ';
135 $fields .= 'g.timemodified as timemarked, ';
136 $fields .= 'g.timecreated as firstmarked, ';
137 $fields .= 'uf.mailed as mailed, ';
138 $fields .= 'uf.locked as locked, ';
139 $fields .= 'uf.extensionduedate as extensionduedate, ';
140 $fields .= 'uf.workflowstate as workflowstate, ';
141 $fields .= 'uf.allocatedmarker as allocatedmarker ';
143 $submissionmaxattempt = 'SELECT mxs.userid, MAX(mxs.attemptnumber) AS maxattempt
144 FROM {assign_submission} mxs
145 WHERE mxs.assignment = :assignmentid4 GROUP BY mxs.userid';
146 $grademaxattempt = 'SELECT mxg.userid, MAX(mxg.attemptnumber) AS maxattempt
147 FROM {assign_grades} mxg
148 WHERE mxg.assignment = :assignmentid5 GROUP BY mxg.userid';
149 $from = '{user} u
150 LEFT JOIN ( ' . $submissionmaxattempt . ' ) smx ON u.id = smx.userid
151 LEFT JOIN ( ' . $grademaxattempt . ' ) gmx ON u.id = gmx.userid
152 LEFT JOIN {assign_submission} s ON
153 u.id = s.userid AND
154 s.assignment = :assignmentid1 AND
155 s.attemptnumber = smx.maxattempt
156 LEFT JOIN {assign_grades} g ON
157 u.id = g.userid AND
158 g.assignment = :assignmentid2 AND
159 g.attemptnumber = gmx.maxattempt
160 LEFT JOIN {assign_user_flags} uf ON u.id = uf.userid AND uf.assignment = :assignmentid3';
162 $userparams = array();
163 $userindex = 0;
165 list($userwhere, $userparams) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
166 $where = 'u.id ' . $userwhere;
167 $params = array_merge($params, $userparams);
169 // The filters do not make sense when there are no submissions, so do not apply them.
170 if ($this->assignment->is_any_submission_plugin_enabled()) {
171 if ($filter == ASSIGN_FILTER_SUBMITTED) {
172 $where .= ' AND (s.timemodified IS NOT NULL AND
173 s.status = :submitted) ';
174 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
176 } else if ($filter == ASSIGN_FILTER_REQUIRE_GRADING) {
177 $where .= ' AND (s.timemodified IS NOT NULL AND
178 s.status = :submitted AND
179 (s.timemodified > g.timemodified OR g.timemodified IS NULL))';
180 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
182 } else if (strpos($filter, ASSIGN_FILTER_SINGLE_USER) === 0) {
183 $userfilter = (int) array_pop(explode('=', $filter));
184 $where .= ' AND (u.id = :userid)';
185 $params['userid'] = $userfilter;
189 if ($this->assignment->get_instance()->markingallocation) {
190 if (has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
191 // Check to see if marker filter is set.
192 $markerfilter = (int)get_user_preferences('assign_markerfilter', '');
193 if (!empty($markerfilter)) {
194 $where .= ' AND uf.allocatedmarker = :markerid';
195 $params['markerid'] = $markerfilter;
197 } else { // Only show users allocated to this marker.
198 $where .= ' AND uf.allocatedmarker = :markerid';
199 $params['markerid'] = $USER->id;
203 if ($this->assignment->get_instance()->markingworkflow) {
204 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
205 if (!empty($workflowstates)) {
206 $workflowfilter = get_user_preferences('assign_workflowfilter', '');
207 if ($workflowfilter == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED) {
208 $where .= ' AND (uf.workflowstate = :workflowstate OR uf.workflowstate IS NULL OR '.
209 $DB->sql_isempty('assign_user_flags', 'workflowstate', true, true).')';
210 $params['workflowstate'] = $workflowfilter;
211 } else if (array_key_exists($workflowfilter, $workflowstates)) {
212 $where .= ' AND uf.workflowstate = :workflowstate';
213 $params['workflowstate'] = $workflowfilter;
218 $this->set_sql($fields, $from, $where, $params);
220 if ($downloadfilename) {
221 $this->is_downloading('csv', $downloadfilename);
224 $columns = array();
225 $headers = array();
227 // Select.
228 if (!$this->is_downloading()) {
229 $columns[] = 'select';
230 $headers[] = get_string('select') .
231 '<div class="selectall"><label class="accesshide" for="selectall">' . get_string('selectall') . '</label>
232 <input type="checkbox" id="selectall" name="selectall" title="' . get_string('selectall') . '"/></div>';
235 // User picture.
236 if (!$this->assignment->is_blind_marking()) {
237 if (!$this->is_downloading()) {
238 $columns[] = 'picture';
239 $headers[] = get_string('pictureofuser');
240 } else {
241 $columns[] = 'recordid';
242 $headers[] = get_string('recordid', 'assign');
245 // Fullname.
246 $columns[] = 'fullname';
247 $headers[] = get_string('fullname');
249 foreach ($extrauserfields as $extrafield) {
250 $columns[] = $extrafield;
251 $headers[] = get_user_field_name($extrafield);
253 } else {
254 // Record ID.
255 $columns[] = 'recordid';
256 $headers[] = get_string('recordid', 'assign');
259 // Submission status.
260 if ($assignment->is_any_submission_plugin_enabled()) {
261 $columns[] = 'status';
262 $headers[] = get_string('status', 'assign');
263 } else if ($this->assignment->get_instance()->markingworkflow) {
264 $columns[] = 'workflowstatus';
265 $headers[] = get_string('status', 'assign');
268 // Team submission columns.
269 if ($assignment->get_instance()->teamsubmission) {
270 $columns[] = 'team';
271 $headers[] = get_string('submissionteam', 'assign');
273 $columns[] = 'teamstatus';
274 $headers[] = get_string('teamsubmissionstatus', 'assign');
276 // Allocated marker.
277 if ($this->assignment->get_instance()->markingallocation &&
278 has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
279 // Add a column for the allocated marker.
280 $columns[] = 'allocatedmarker';
281 $headers[] = get_string('marker', 'assign');
283 // Grade.
284 $columns[] = 'grade';
285 $headers[] = get_string('grade');
286 if ($this->is_downloading()) {
287 if ($this->assignment->get_instance()->grade >= 0) {
288 $columns[] = 'grademax';
289 $headers[] = get_string('maxgrade', 'assign');
290 } else {
291 // This is a custom scale.
292 $columns[] = 'scale';
293 $headers[] = get_string('scale', 'assign');
296 if ($this->assignment->get_instance()->markingworkflow) {
297 // Add a column for the marking workflow state.
298 $columns[] = 'workflowstate';
299 $headers[] = get_string('markingworkflowstate', 'assign');
301 // Add a column for the list of valid marking workflow states.
302 $columns[] = 'gradecanbechanged';
303 $headers[] = get_string('gradecanbechanged', 'assign');
305 if (!$this->is_downloading()) {
306 // We have to call this column userid so we can use userid as a default sortable column.
307 $columns[] = 'userid';
308 $headers[] = get_string('edit');
311 // Submission plugins.
312 if ($assignment->is_any_submission_plugin_enabled()) {
313 $columns[] = 'timesubmitted';
314 $headers[] = get_string('lastmodifiedsubmission', 'assign');
316 foreach ($this->assignment->get_submission_plugins() as $plugin) {
317 if ($this->is_downloading()) {
318 if ($plugin->is_visible() && $plugin->is_enabled()) {
319 foreach ($plugin->get_editor_fields() as $field => $description) {
320 $index = 'plugin' . count($this->plugincache);
321 $this->plugincache[$index] = array($plugin, $field);
322 $columns[] = $index;
323 $headers[] = $plugin->get_name();
326 } else {
327 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
328 $index = 'plugin' . count($this->plugincache);
329 $this->plugincache[$index] = array($plugin);
330 $columns[] = $index;
331 $headers[] = $plugin->get_name();
337 // Time marked.
338 $columns[] = 'timemarked';
339 $headers[] = get_string('lastmodifiedgrade', 'assign');
341 // Feedback plugins.
342 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
343 if ($this->is_downloading()) {
344 if ($plugin->is_visible() && $plugin->is_enabled()) {
345 foreach ($plugin->get_editor_fields() as $field => $description) {
346 $index = 'plugin' . count($this->plugincache);
347 $this->plugincache[$index] = array($plugin, $field);
348 $columns[] = $index;
349 $headers[] = $description;
352 } else if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
353 $index = 'plugin' . count($this->plugincache);
354 $this->plugincache[$index] = array($plugin);
355 $columns[] = $index;
356 $headers[] = $plugin->get_name();
360 // Exclude 'Final grade' column in downloaded grading worksheets.
361 if (!$this->is_downloading()) {
362 // Final grade.
363 $columns[] = 'finalgrade';
364 $headers[] = get_string('finalgrade', 'grades');
367 // Load the grading info for all users.
368 $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id,
369 'mod',
370 'assign',
371 $this->assignment->get_instance()->id,
372 $users);
373 $this->hasgrantextension = has_capability('mod/assign:grantextension',
374 $this->assignment->get_context());
376 if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
377 $columns[] = 'outcomes';
378 $headers[] = get_string('outcomes', 'grades');
381 // Set the columns.
382 $this->define_columns($columns);
383 $this->define_headers($headers);
384 foreach ($extrauserfields as $extrafield) {
385 $this->column_class($extrafield, $extrafield);
387 // We require at least one unique column for the sort.
388 $this->sortable(true, 'userid');
389 $this->no_sorting('recordid');
390 $this->no_sorting('finalgrade');
391 $this->no_sorting('userid');
392 $this->no_sorting('select');
393 $this->no_sorting('outcomes');
395 if ($assignment->get_instance()->teamsubmission) {
396 $this->no_sorting('team');
397 $this->no_sorting('teamstatus');
400 $plugincolumnindex = 0;
401 foreach ($this->assignment->get_submission_plugins() as $plugin) {
402 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
403 $submissionpluginindex = 'plugin' . $plugincolumnindex++;
404 $this->no_sorting($submissionpluginindex);
407 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
408 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
409 $feedbackpluginindex = 'plugin' . $plugincolumnindex++;
410 $this->no_sorting($feedbackpluginindex);
414 // When there is no data we still want the column headers printed in the csv file.
415 if ($this->is_downloading()) {
416 $this->start_output();
421 * Before adding each row to the table make sure rownum is incremented.
423 * @param array $row row of data from db used to make one row of the table.
424 * @return array one row for the table
426 public function format_row($row) {
427 if ($this->rownum < 0) {
428 $this->rownum = $this->currpage * $this->pagesize;
429 } else {
430 $this->rownum += 1;
433 return parent::format_row($row);
437 * Add a column with an ID that uniquely identifies this user in this assignment.
439 * @param stdClass $row
440 * @return string
442 public function col_recordid(stdClass $row) {
443 return get_string('hiddenuser', 'assign') .
444 $this->assignment->get_uniqueid_for_user($row->userid);
449 * Add the userid to the row class so it can be updated via ajax.
451 * @param stdClass $row The row of data
452 * @return string The row class
454 public function get_row_class($row) {
455 return 'user' . $row->userid;
459 * Return the number of rows to display on a single page.
461 * @return int The number of rows per page
463 public function get_rows_per_page() {
464 return $this->perpage;
468 * list current marking workflow state
470 * @param stdClass $row
471 * @return string
473 public function col_workflowstatus(stdClass $row) {
474 $o = '';
476 $gradingdisabled = $this->assignment->grading_disabled($row->id);
477 if ($this->assignment->get_instance()->markingworkflow) {
478 // The function in the assignment keeps a static cache of this list of states.
479 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
480 $workflowstate = $row->workflowstate;
481 if (empty($workflowstate)) {
482 $workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
484 if ($this->quickgrading && !$gradingdisabled) {
485 $notmarked = get_string('markingworkflowstatenotmarked', 'assign');
486 $name = 'quickgrade_' . $row->id . '_workflowstate';
487 $o .= html_writer::select($workflowstates, $name, $workflowstate, array('' => $notmarked));
488 // Check if this user is a marker that can't manage allocations and doesn't have the marker column added.
489 if ($this->assignment->get_instance()->markingallocation &&
490 !has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
492 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
493 $o .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$row->allocatedmarker));
495 } else {
496 $o .= $this->output->container(get_string('markingworkflowstate' . $workflowstate, 'assign'), $workflowstate);
499 return $o;
503 * For download only - list current marking workflow state
505 * @param stdClass $row - The row of data
506 * @return string The current marking workflow state
508 public function col_workflowstate($row) {
509 $state = $row->workflowstate;
510 if (empty($state)) {
511 $state = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
514 return get_string('markingworkflowstate' . $state, 'assign');
518 * list current marker
520 * @param stdClass $row - The row of data
521 * @return id the user->id of the marker.
523 public function col_allocatedmarker(stdClass $row) {
524 static $markers = null;
525 static $markerlist = array();
526 if ($markers === null) {
527 $markers = get_users_by_capability($this->assignment->get_context(), 'mod/assign:grade');
528 $markerlist[0] = get_string('choosemarker', 'assign');
529 foreach ($markers as $marker) {
530 $markerlist[$marker->id] = fullname($marker);
533 if (empty($markerlist)) {
534 // TODO: add some form of notification here that no markers are available.
535 return '';
537 if ($this->is_downloading()) {
538 return $markers[$row->allocatedmarker];
541 if ($this->quickgrading && has_capability('mod/assign:manageallocations', $this->assignment->get_context()) &&
542 (empty($row->workflowstate) ||
543 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_INMARKING ||
544 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED)) {
546 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
547 return html_writer::select($markerlist, $name, $row->allocatedmarker, false);
548 } else if (!empty($row->allocatedmarker)) {
549 $output = '';
550 if ($this->quickgrading) { // Add hidden field for quickgrading page.
551 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
552 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$row->allocatedmarker));
554 $output .= $markerlist[$row->allocatedmarker];
555 return $output;
559 * For download only - list all the valid options for this custom scale.
561 * @param stdClass $row - The row of data
562 * @return string A list of valid options for the current scale
564 public function col_scale($row) {
565 global $DB;
567 if (empty($this->scale)) {
568 $dbparams = array('id'=>-($this->assignment->get_instance()->grade));
569 $this->scale = $DB->get_record('scale', $dbparams);
572 if (!empty($this->scale->scale)) {
573 return implode("\n", explode(',', $this->scale->scale));
575 return '';
579 * Display a grade with scales etc.
581 * @param string $grade
582 * @param boolean $editable
583 * @param int $userid The user id of the user this grade belongs to
584 * @param int $modified Timestamp showing when the grade was last modified
585 * @return string The formatted grade
587 public function display_grade($grade, $editable, $userid, $modified) {
588 if ($this->is_downloading()) {
589 if ($this->assignment->get_instance()->grade >= 0) {
590 if ($grade == -1 || $grade === null) {
591 return '';
593 return format_float($grade, 2);
594 } else {
595 // This is a custom scale.
596 $scale = $this->assignment->display_grade($grade, false);
597 if ($scale == '-') {
598 $scale = '';
600 return $scale;
603 return $this->assignment->display_grade($grade, $editable, $userid, $modified);
607 * Get the team info for this user.
609 * @param stdClass $row
610 * @return string The team name
612 public function col_team(stdClass $row) {
613 $submission = false;
614 $group = false;
615 $this->get_group_and_submission($row->id, $group, $submission, -1);
616 if ($group) {
617 return $group->name;
619 return get_string('defaultteam', 'assign');
623 * Use a static cache to try and reduce DB calls.
625 * @param int $userid The user id for this submission
626 * @param int $group The groupid (returned)
627 * @param stdClass|false $submission The stdClass submission or false (returned)
628 * @param int $attemptnumber Return a specific attempt number (-1 for latest)
630 protected function get_group_and_submission($userid, &$group, &$submission, $attemptnumber) {
631 $group = false;
632 if (isset($this->submissiongroups[$userid])) {
633 $group = $this->submissiongroups[$userid];
634 } else {
635 $group = $this->assignment->get_submission_group($userid, false);
636 $this->submissiongroups[$userid] = $group;
639 $groupid = 0;
640 if ($group) {
641 $groupid = $group->id;
644 // Static cache is keyed by groupid and attemptnumber.
645 // We may need both the latest and previous attempt in the same page.
646 if (isset($this->groupsubmissions[$groupid . ':' . $attemptnumber])) {
647 $submission = $this->groupsubmissions[$groupid . ':' . $attemptnumber];
648 } else {
649 $submission = $this->assignment->get_group_submission($userid, $groupid, false, $attemptnumber);
650 $this->groupsubmissions[$groupid . ':' . $attemptnumber] = $submission;
656 * Get the team status for this user.
658 * @param stdClass $row
659 * @return string The team name
661 public function col_teamstatus(stdClass $row) {
662 $submission = false;
663 $group = false;
664 $this->get_group_and_submission($row->id, $group, $submission, -1);
666 $status = '';
667 if ($submission) {
668 $status = $submission->status;
670 return get_string('submissionstatus_' . $status, 'assign');
675 * Format a list of outcomes.
677 * @param stdClass $row
678 * @return string
680 public function col_outcomes(stdClass $row) {
681 $outcomes = '';
682 foreach ($this->gradinginfo->outcomes as $index => $outcome) {
683 $options = make_grades_menu(-$outcome->scaleid);
685 $options[0] = get_string('nooutcome', 'grades');
686 if ($this->quickgrading && !($outcome->grades[$row->userid]->locked)) {
687 $select = '<select name="outcome_' . $index . '_' . $row->userid . '" class="quickgrade">';
688 foreach ($options as $optionindex => $optionvalue) {
689 $selected = '';
690 if ($outcome->grades[$row->userid]->grade == $optionindex) {
691 $selected = 'selected="selected"';
693 $select .= '<option value="' . $optionindex . '"' . $selected . '>' . $optionvalue . '</option>';
695 $select .= '</select>';
696 $outcomes .= $this->output->container($outcome->name . ': ' . $select, 'outcome');
697 } else {
698 $name = $outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade];
699 $outcomes .= $this->output->container($name, 'outcome');
703 return $outcomes;
708 * Format a user picture for display.
710 * @param stdClass $row
711 * @return string
713 public function col_picture(stdClass $row) {
714 if ($row->picture) {
715 return $this->output->user_picture($row);
717 return '';
721 * Format a user record for display (link to profile).
723 * @param stdClass $row
724 * @return string
726 public function col_fullname($row) {
727 if (!$this->is_downloading()) {
728 $courseid = $this->assignment->get_course()->id;
729 $link= new moodle_url('/user/view.php', array('id' =>$row->id, 'course'=>$courseid));
730 $fullname = $this->output->action_link($link, fullname($row));
731 } else {
732 $fullname = fullname($row);
735 if (!$this->assignment->is_active_user($row->id)) {
736 $suspendedstring = get_string('userenrolmentsuspended', 'grades');
737 $fullname .= ' ' . html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/enrolmentsuspended'),
738 'title' => $suspendedstring, 'alt' => $suspendedstring, 'class' => 'usersuspendedicon'));
739 $fullname = html_writer::tag('span', $fullname, array('class' => 'usersuspended'));
741 return $fullname;
745 * Insert a checkbox for selecting the current row for batch operations.
747 * @param stdClass $row
748 * @return string
750 public function col_select(stdClass $row) {
751 $selectcol = '<label class="accesshide" for="selectuser_' . $row->userid . '">';
752 $selectcol .= get_string('selectuser', 'assign', fullname($row));
753 $selectcol .= '</label>';
754 $selectcol .= '<input type="checkbox"
755 id="selectuser_' . $row->userid . '"
756 name="selectedusers"
757 value="' . $row->userid . '"/>';
758 return $selectcol;
762 * Return a users grades from the listing of all grade data for this assignment.
764 * @param int $userid
765 * @return mixed stdClass or false
767 private function get_gradebook_data_for_user($userid) {
768 if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
769 return $this->gradinginfo->items[0]->grades[$userid];
771 return false;
775 * Format a column of data for display.
777 * @param stdClass $row
778 * @return string
780 public function col_gradecanbechanged(stdClass $row) {
781 $gradingdisabled = $this->assignment->grading_disabled($row->id);
782 if ($gradingdisabled) {
783 return get_string('no');
784 } else {
785 return get_string('yes');
790 * Format a column of data for display
792 * @param stdClass $row
793 * @return string
795 public function col_grademax(stdClass $row) {
796 return format_float($this->assignment->get_instance()->grade, 2);
800 * Format a column of data for display.
802 * @param stdClass $row
803 * @return string
805 public function col_grade(stdClass $row) {
806 $o = '';
808 $link = '';
809 $separator = $this->output->spacer(array(), true);
810 $grade = '';
811 $gradingdisabled = $this->assignment->grading_disabled($row->id);
813 if (!$this->is_downloading()) {
814 $name = fullname($row);
815 if ($this->assignment->is_blind_marking()) {
816 $name = get_string('hiddenuser', 'assign') .
817 $this->assignment->get_uniqueid_for_user($row->userid);
819 $icon = $this->output->pix_icon('gradefeedback',
820 get_string('gradeuser', 'assign', $name),
821 'mod_assign');
822 $urlparams = array('id' => $this->assignment->get_course_module()->id,
823 'rownum'=>$this->rownum,
824 'action'=>'grade');
825 $url = new moodle_url('/mod/assign/view.php', $urlparams);
826 $link = $this->output->action_link($url, $icon);
827 $grade .= $link . $separator;
830 $grade .= $this->display_grade($row->grade,
831 $this->quickgrading && !$gradingdisabled,
832 $row->userid,
833 $row->timemarked);
835 return $grade;
839 * Format a column of data for display.
841 * @param stdClass $row
842 * @return string
844 public function col_finalgrade(stdClass $row) {
845 $o = '';
847 $grade = $this->get_gradebook_data_for_user($row->userid);
848 if ($grade) {
849 $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
852 return $o;
856 * Format a column of data for display.
858 * @param stdClass $row
859 * @return string
861 public function col_timemarked(stdClass $row) {
862 $o = '-';
864 if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
865 $o = userdate($row->timemarked);
867 if ($row->timemarked && $this->is_downloading()) {
868 // Force it for downloads as it affects import.
869 $o = userdate($row->timemarked);
872 return $o;
876 * Format a column of data for display.
878 * @param stdClass $row
879 * @return string
881 public function col_timesubmitted(stdClass $row) {
882 $o = '-';
884 if ($row->timesubmitted) {
885 $o = userdate($row->timesubmitted);
888 return $o;
892 * Format a column of data for display
894 * @param stdClass $row
895 * @return string
897 public function col_status(stdClass $row) {
898 $o = '';
900 $instance = $this->assignment->get_instance();
902 if ($this->assignment->is_any_submission_plugin_enabled()) {
904 $o .= $this->output->container(get_string('submissionstatus_' . $row->status, 'assign'),
905 array('class'=>'submissionstatus' .$row->status));
906 if ($instance->duedate &&
907 $row->timesubmitted > $instance->duedate) {
908 if (!$row->extensionduedate ||
909 $row->timesubmitted > $row->extensionduedate) {
910 $usertime = format_time($row->timesubmitted - $instance->duedate);
911 $latemessage = get_string('submittedlateshort',
912 'assign',
913 $usertime);
914 $o .= $this->output->container($latemessage, 'latesubmission');
917 if ($row->locked) {
918 $lockedstr = get_string('submissionslockedshort', 'assign');
919 $o .= $this->output->container($lockedstr, 'lockedsubmission');
921 $o .= $this->col_workflowstatus($row);
922 if (!$row->timesubmitted) {
923 $now = time();
924 $due = $instance->duedate;
925 if ($row->extensionduedate) {
926 $due = $row->extensionduedate;
928 if ($due && ($now > $due)) {
929 $overduestr = get_string('overdue', 'assign', format_time($now - $due));
930 $o .= $this->output->container($overduestr, 'overduesubmission');
933 if ($row->extensionduedate) {
934 $userdate = userdate($row->extensionduedate);
935 $extensionstr = get_string('userextensiondate', 'assign', $userdate);
936 $o .= $this->output->container($extensionstr, 'extensiondate');
940 if ($this->is_downloading()) {
941 $o = strip_tags(str_replace('</div>', "\n", $o));
944 return $o;
948 * Format a column of data for display.
950 * @param stdClass $row
951 * @return string
953 public function col_userid(stdClass $row) {
954 $edit = '';
956 $actions = array();
958 $urlparams = array('id'=>$this->assignment->get_course_module()->id,
959 'rownum'=>$this->rownum,
960 'action'=>'grade');
961 $url = new moodle_url('/mod/assign/view.php', $urlparams);
963 if (!$row->grade) {
964 $description = get_string('grade');
965 } else {
966 $description = get_string('updategrade', 'assign');
968 $actions[$url->out(false)] = $description;
970 // Hide for offline assignments.
971 if ($this->assignment->is_any_submission_plugin_enabled()) {
972 if (!$row->status ||
973 $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
974 !$this->assignment->get_instance()->submissiondrafts) {
976 if (!$row->locked) {
977 $urlparams = array('id' => $this->assignment->get_course_module()->id,
978 'userid'=>$row->id,
979 'action'=>'lock',
980 'sesskey'=>sesskey(),
981 'page'=>$this->currpage);
982 $url = new moodle_url('/mod/assign/view.php', $urlparams);
984 $description = get_string('preventsubmissionsshort', 'assign');
985 $actions[$url->out(false)] = $description;
986 } else {
987 $urlparams = array('id' => $this->assignment->get_course_module()->id,
988 'userid'=>$row->id,
989 'action'=>'unlock',
990 'sesskey'=>sesskey(),
991 'page'=>$this->currpage);
992 $url = new moodle_url('/mod/assign/view.php', $urlparams);
993 $description = get_string('allowsubmissionsshort', 'assign');
994 $actions[$url->out(false)] = $description;
998 if (($this->assignment->get_instance()->duedate ||
999 $this->assignment->get_instance()->cutoffdate) &&
1000 $this->hasgrantextension) {
1001 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1002 'userid'=>$row->id,
1003 'action'=>'grantextension',
1004 'sesskey'=>sesskey(),
1005 'page'=>$this->currpage);
1006 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1007 $description = get_string('grantextension', 'assign');
1008 $actions[$url->out(false)] = $description;
1011 if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
1012 $this->assignment->get_instance()->submissiondrafts) {
1013 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1014 'userid'=>$row->id,
1015 'action'=>'reverttodraft',
1016 'sesskey'=>sesskey(),
1017 'page'=>$this->currpage);
1018 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1019 $description = get_string('reverttodraftshort', 'assign');
1020 $actions[$url->out(false)] = $description;
1023 $ismanual = $this->assignment->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
1024 $hassubmission = !empty($row->status);
1025 $notreopened = $hassubmission && $row->status != ASSIGN_SUBMISSION_STATUS_REOPENED;
1026 $isunlimited = $this->assignment->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS;
1027 $hasattempts = $isunlimited || $row->attemptnumber < $this->assignment->get_instance()->maxattempts - 1;
1029 if ($ismanual && $hassubmission && $notreopened && $hasattempts) {
1030 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1031 'userid'=>$row->id,
1032 'action'=>'addattempt',
1033 'sesskey'=>sesskey(),
1034 'page'=>$this->currpage);
1035 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1036 $description = get_string('addattempt', 'assign');
1037 $actions[$url->out(false)] = $description;
1040 $edit .= $this->output->container_start(array('yui3-menu', 'actionmenu'), 'actionselect' . $row->id);
1041 $edit .= $this->output->container_start(array('yui3-menu-content'));
1042 $edit .= html_writer::start_tag('ul');
1043 $edit .= html_writer::start_tag('li', array('class'=>'menuicon'));
1045 $menuicon = $this->output->pix_icon('t/contextmenu', get_string('actions'));
1046 $edit .= $this->output->action_link('#menu' . $row->id, $menuicon, null, array('class'=>'yui3-menu-label'));
1047 $edit .= $this->output->container_start(array('yui3-menu', 'yui3-loading'), 'menu' . $row->id);
1048 $edit .= $this->output->container_start(array('yui3-menu-content'));
1049 $edit .= html_writer::start_tag('ul');
1051 foreach ($actions as $url => $description) {
1052 $edit .= html_writer::start_tag('li', array('class'=>'yui3-menuitem'));
1054 $edit .= $this->output->action_link($url, $description, null, array('class'=>'yui3-menuitem-content'));
1056 $edit .= html_writer::end_tag('li');
1058 $edit .= html_writer::end_tag('ul');
1059 $edit .= $this->output->container_end();
1060 $edit .= $this->output->container_end();
1061 $edit .= html_writer::end_tag('li');
1062 $edit .= html_writer::end_tag('ul');
1064 $edit .= $this->output->container_end();
1065 $edit .= $this->output->container_end();
1067 return $edit;
1071 * Write the plugin summary with an optional link to view the full feedback/submission.
1073 * @param assign_plugin $plugin Submission plugin or feedback plugin
1074 * @param stdClass $item Submission or grade
1075 * @param string $returnaction The return action to pass to the
1076 * view_submission page (the current page)
1077 * @param string $returnparams The return params to pass to the view_submission
1078 * page (the current page)
1079 * @return string The summary with an optional link
1081 private function format_plugin_summary_with_link(assign_plugin $plugin,
1082 stdClass $item,
1083 $returnaction,
1084 $returnparams) {
1085 $link = '';
1086 $showviewlink = false;
1088 $summary = $plugin->view_summary($item, $showviewlink);
1089 $separator = '';
1090 if ($showviewlink) {
1091 $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
1092 $icon = $this->output->pix_icon('t/preview', $viewstr);
1093 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1094 'sid'=>$item->id,
1095 'gid'=>$item->id,
1096 'plugin'=>$plugin->get_type(),
1097 'action'=>'viewplugin' . $plugin->get_subtype(),
1098 'returnaction'=>$returnaction,
1099 'returnparams'=>http_build_query($returnparams));
1100 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1101 $link = $this->output->action_link($url, $icon);
1102 $separator = $this->output->spacer(array(), true);
1105 return $link . $separator . $summary;
1110 * Format the submission and feedback columns.
1112 * @param string $colname The column name
1113 * @param stdClass $row The submission row
1114 * @return mixed string or NULL
1116 public function other_cols($colname, $row) {
1117 // For extra user fields the result is already in $row.
1118 if (empty($this->plugincache[$colname])) {
1119 return $row->$colname;
1122 // This must be a plugin field.
1123 $plugincache = $this->plugincache[$colname];
1125 $plugin = $plugincache[0];
1127 $field = null;
1128 if (isset($plugincache[1])) {
1129 $field = $plugincache[1];
1132 if ($plugin->is_visible() && $plugin->is_enabled()) {
1133 if ($plugin->get_subtype() == 'assignsubmission') {
1134 if ($this->assignment->get_instance()->teamsubmission) {
1135 $group = false;
1136 $submission = false;
1138 $this->get_group_and_submission($row->id, $group, $submission, -1);
1139 if ($submission) {
1140 if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1141 // For a newly reopened submission - we want to show the previous submission in the table.
1142 $this->get_group_and_submission($row->id, $group, $submission, $submission->attemptnumber-1);
1144 if (isset($field)) {
1145 return $plugin->get_editor_text($field, $submission->id);
1147 return $this->format_plugin_summary_with_link($plugin,
1148 $submission,
1149 'grading',
1150 array());
1152 } else if ($row->submissionid) {
1153 if ($row->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1154 // For a newly reopened submission - we want to show the previous submission in the table.
1155 $submission = $this->assignment->get_user_submission($row->userid, false, $row->attemptnumber - 1);
1156 } else {
1157 $submission = new stdClass();
1158 $submission->id = $row->submissionid;
1159 $submission->timecreated = $row->firstsubmission;
1160 $submission->timemodified = $row->timesubmitted;
1161 $submission->assignment = $this->assignment->get_instance()->id;
1162 $submission->userid = $row->userid;
1163 $submission->attemptnumber = $row->attemptnumber;
1165 // Field is used for only for import/export and refers the the fieldname for the text editor.
1166 if (isset($field)) {
1167 return $plugin->get_editor_text($field, $submission->id);
1169 return $this->format_plugin_summary_with_link($plugin,
1170 $submission,
1171 'grading',
1172 array());
1174 } else {
1175 $grade = null;
1176 if (isset($field)) {
1177 return $plugin->get_editor_text($field, $row->gradeid);
1180 if ($row->gradeid) {
1181 $grade = new stdClass();
1182 $grade->id = $row->gradeid;
1183 $grade->timecreated = $row->firstmarked;
1184 $grade->timemodified = $row->timemarked;
1185 $grade->assignment = $this->assignment->get_instance()->id;
1186 $grade->userid = $row->userid;
1187 $grade->grade = $row->grade;
1188 $grade->mailed = $row->mailed;
1189 $grade->attemptnumber = $row->attemptnumber;
1191 if ($this->quickgrading && $plugin->supports_quickgrading()) {
1192 return $plugin->get_quickgrading_html($row->userid, $grade);
1193 } else if ($grade) {
1194 return $this->format_plugin_summary_with_link($plugin,
1195 $grade,
1196 'grading',
1197 array());
1201 return '';
1205 * Using the current filtering and sorting - load all rows and return a single column from them.
1207 * @param string $columnname The name of the raw column data
1208 * @return array of data
1210 public function get_column_data($columnname) {
1211 $this->setup();
1212 $this->currpage = 0;
1213 $this->query_db($this->tablemaxrows);
1214 $result = array();
1215 foreach ($this->rawdata as $row) {
1216 $result[] = $row->$columnname;
1218 return $result;
1222 * Return things to the renderer.
1224 * @return string the assignment name
1226 public function get_assignment_name() {
1227 return $this->assignment->get_instance()->name;
1231 * Return things to the renderer.
1233 * @return int the course module id
1235 public function get_course_module_id() {
1236 return $this->assignment->get_course_module()->id;
1240 * Return things to the renderer.
1242 * @return int the course id
1244 public function get_course_id() {
1245 return $this->assignment->get_course()->id;
1249 * Return things to the renderer.
1251 * @return stdClass The course context
1253 public function get_course_context() {
1254 return $this->assignment->get_course_context();
1258 * Return things to the renderer.
1260 * @return bool Does this assignment accept submissions
1262 public function submissions_enabled() {
1263 return $this->assignment->is_any_submission_plugin_enabled();
1267 * Return things to the renderer.
1269 * @return bool Can this user view all grades (the gradebook)
1271 public function can_view_all_grades() {
1272 $context = $this->assignment->get_course_context();
1273 return has_capability('gradereport/grader:view', $context) &&
1274 has_capability('moodle/grade:viewall', $context);
1278 * Override the table show_hide_link to not show for select column.
1280 * @param string $column the column name, index into various names.
1281 * @param int $index numerical index of the column.
1282 * @return string HTML fragment.
1284 protected function show_hide_link($column, $index) {
1285 if ($index > 0) {
1286 return parent::show_hide_link($column, $index);
1288 return '';