2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * This file contains the definition for the grading table which subclassses easy_table
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');
32 * Extends table_sql to provide a table of assignment submissions
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) */
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;
69 * overridden constructor keeps a reference to the assignment class that is displaying this table
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
78 public function __construct(assign $assignment,
83 $downloadfilename = null) {
84 global $CFG, $PAGE, $DB, $USER;
85 parent::__construct('mod_assign_grading');
86 $this->assignment = $assignment;
88 // Check permissions up front.
89 $this->hasgrantextension = has_capability('mod/assign:grantextension',
90 $this->assignment->get_context());
91 $this->hasgrade = $this->assignment->can_grade();
93 // Check if we have the elevated view capablities to see the blind details.
94 $this->hasviewblind = has_capability('mod/assign:viewblinddetails',
95 $this->assignment->get_context());
97 foreach ($assignment->get_feedback_plugins() as $plugin) {
98 if ($plugin->is_visible() && $plugin->is_enabled()) {
99 foreach ($plugin->get_grading_batch_operations() as $action => $description) {
100 if (empty($this->plugingradingbatchoperations)) {
101 $this->plugingradingbatchoperations[$plugin->get_type()] = array();
103 $this->plugingradingbatchoperations[$plugin->get_type()][$action] = $description;
107 $this->perpage = $perpage;
108 $this->quickgrading = $quickgrading && $this->hasgrade;
109 $this->output = $PAGE->get_renderer('mod_assign');
111 $urlparams = array('action'=>'grading', 'id'=>$assignment->get_course_module()->id);
112 $url = new moodle_url($CFG->wwwroot . '/mod/assign/view.php', $urlparams);
113 $this->define_baseurl($url);
115 // Do some business - then set the sql.
116 $currentgroup = groups_get_activity_group($assignment->get_course_module(), true);
119 $this->rownum = $rowoffset - 1;
122 $users = array_keys( $assignment->list_participants($currentgroup, true));
123 if (count($users) == 0) {
124 // Insert a record that will never match to the sql is still valid.
129 $params['assignmentid1'] = (int)$this->assignment->get_instance()->id;
130 $params['assignmentid2'] = (int)$this->assignment->get_instance()->id;
131 $params['assignmentid3'] = (int)$this->assignment->get_instance()->id;
133 $extrauserfields = get_extra_user_fields($this->assignment->get_context());
135 $fields = user_picture::fields('u', $extrauserfields) . ', ';
136 $fields .= 'u.id as userid, ';
137 $fields .= 's.status as status, ';
138 $fields .= 's.id as submissionid, ';
139 $fields .= 's.timecreated as firstsubmission, ';
140 $fields .= 's.timemodified as timesubmitted, ';
141 $fields .= 's.attemptnumber as attemptnumber, ';
142 $fields .= 'g.id as gradeid, ';
143 $fields .= 'g.grade as grade, ';
144 $fields .= 'g.timemodified as timemarked, ';
145 $fields .= 'g.timecreated as firstmarked, ';
146 $fields .= 'uf.mailed as mailed, ';
147 $fields .= 'uf.locked as locked, ';
148 $fields .= 'uf.extensionduedate as extensionduedate, ';
149 $fields .= 'uf.workflowstate as workflowstate, ';
150 $fields .= 'uf.allocatedmarker as allocatedmarker ';
153 LEFT JOIN {assign_submission} s ON
155 s.assignment = :assignmentid1 AND
157 LEFT JOIN {assign_grades} g ON
159 g.assignment = :assignmentid2 AND
160 g.attemptnumber = s.attemptnumber
161 LEFT JOIN {assign_user_flags} uf ON u.id = uf.userid AND uf.assignment = :assignmentid3';
163 $userparams = array();
166 list($userwhere, $userparams) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
167 $where = 'u.id ' . $userwhere;
168 $params = array_merge($params, $userparams);
170 // The filters do not make sense when there are no submissions, so do not apply them.
171 if ($this->assignment->is_any_submission_plugin_enabled()) {
172 if ($filter == ASSIGN_FILTER_SUBMITTED) {
173 $where .= ' AND (s.timemodified IS NOT NULL AND
174 s.status = :submitted) ';
175 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
177 } else if ($filter == ASSIGN_FILTER_NOT_SUBMITTED) {
178 $where .= ' AND (s.timemodified IS NULL OR s.status != :submitted) ';
179 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
180 } else if ($filter == ASSIGN_FILTER_REQUIRE_GRADING) {
181 $where .= ' AND (s.timemodified IS NOT NULL AND
182 s.status = :submitted AND
183 (s.timemodified > g.timemodified OR g.timemodified IS NULL))';
184 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
186 } else if (strpos($filter, ASSIGN_FILTER_SINGLE_USER) === 0) {
187 $userfilter = (int) array_pop(explode('=', $filter));
188 $where .= ' AND (u.id = :userid)';
189 $params['userid'] = $userfilter;
193 if ($this->assignment->get_instance()->markingworkflow &&
194 $this->assignment->get_instance()->markingallocation) {
195 if (has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
196 // Check to see if marker filter is set.
197 $markerfilter = (int)get_user_preferences('assign_markerfilter', '');
198 if (!empty($markerfilter)) {
199 if ($markerfilter == ASSIGN_MARKER_FILTER_NO_MARKER) {
200 $where .= ' AND (uf.allocatedmarker IS NULL OR uf.allocatedmarker = 0)';
202 $where .= ' AND uf.allocatedmarker = :markerid';
203 $params['markerid'] = $markerfilter;
206 } else { // Only show users allocated to this marker.
207 $where .= ' AND uf.allocatedmarker = :markerid';
208 $params['markerid'] = $USER->id;
212 if ($this->assignment->get_instance()->markingworkflow) {
213 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
214 if (!empty($workflowstates)) {
215 $workflowfilter = get_user_preferences('assign_workflowfilter', '');
216 if ($workflowfilter == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED) {
217 $where .= ' AND (uf.workflowstate = :workflowstate OR uf.workflowstate IS NULL OR '.
218 $DB->sql_isempty('assign_user_flags', 'workflowstate', true, true).')';
219 $params['workflowstate'] = $workflowfilter;
220 } else if (array_key_exists($workflowfilter, $workflowstates)) {
221 $where .= ' AND uf.workflowstate = :workflowstate';
222 $params['workflowstate'] = $workflowfilter;
227 $this->set_sql($fields, $from, $where, $params);
229 if ($downloadfilename) {
230 $this->is_downloading('csv', $downloadfilename);
237 if (!$this->is_downloading() && $this->hasgrade) {
238 $columns[] = 'select';
239 $headers[] = get_string('select') .
240 '<div class="selectall"><label class="accesshide" for="selectall">' . get_string('selectall') . '</label>
241 <input type="checkbox" id="selectall" name="selectall" title="' . get_string('selectall') . '"/></div>';
245 if ($this->hasviewblind || !$this->assignment->is_blind_marking()) {
246 if (!$this->is_downloading()) {
247 $columns[] = 'picture';
248 $headers[] = get_string('pictureofuser');
250 $columns[] = 'recordid';
251 $headers[] = get_string('recordid', 'assign');
255 $columns[] = 'fullname';
256 $headers[] = get_string('fullname');
258 foreach ($extrauserfields as $extrafield) {
259 $columns[] = $extrafield;
260 $headers[] = get_user_field_name($extrafield);
264 $columns[] = 'recordid';
265 $headers[] = get_string('recordid', 'assign');
268 if ($this->hasviewblind) {
269 $columns[] = 'recordid';
270 $headers[] = get_string('recordid', 'assign');
273 // Submission status.
274 if ($assignment->is_any_submission_plugin_enabled()) {
275 $columns[] = 'status';
276 $headers[] = get_string('status', 'assign');
277 } else if ($this->assignment->get_instance()->markingworkflow) {
278 $columns[] = 'workflowstatus';
279 $headers[] = get_string('status', 'assign');
282 // Team submission columns.
283 if ($assignment->get_instance()->teamsubmission) {
285 $headers[] = get_string('submissionteam', 'assign');
288 if ($this->assignment->get_instance()->markingworkflow &&
289 $this->assignment->get_instance()->markingallocation &&
290 has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
291 // Add a column for the allocated marker.
292 $columns[] = 'allocatedmarker';
293 $headers[] = get_string('marker', 'assign');
296 $columns[] = 'grade';
297 $headers[] = get_string('grade');
298 if ($this->is_downloading()) {
299 if ($this->assignment->get_instance()->grade >= 0) {
300 $columns[] = 'grademax';
301 $headers[] = get_string('maxgrade', 'assign');
303 // This is a custom scale.
304 $columns[] = 'scale';
305 $headers[] = get_string('scale', 'assign');
308 if ($this->assignment->get_instance()->markingworkflow) {
309 // Add a column for the marking workflow state.
310 $columns[] = 'workflowstate';
311 $headers[] = get_string('markingworkflowstate', 'assign');
313 // Add a column for the list of valid marking workflow states.
314 $columns[] = 'gradecanbechanged';
315 $headers[] = get_string('gradecanbechanged', 'assign');
317 if (!$this->is_downloading() && $this->hasgrade) {
318 // We have to call this column userid so we can use userid as a default sortable column.
319 $columns[] = 'userid';
320 $headers[] = get_string('edit');
323 // Submission plugins.
324 if ($assignment->is_any_submission_plugin_enabled()) {
325 $columns[] = 'timesubmitted';
326 $headers[] = get_string('lastmodifiedsubmission', 'assign');
328 foreach ($this->assignment->get_submission_plugins() as $plugin) {
329 if ($this->is_downloading()) {
330 if ($plugin->is_visible() && $plugin->is_enabled()) {
331 foreach ($plugin->get_editor_fields() as $field => $description) {
332 $index = 'plugin' . count($this->plugincache);
333 $this->plugincache[$index] = array($plugin, $field);
335 $headers[] = $plugin->get_name();
339 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
340 $index = 'plugin' . count($this->plugincache);
341 $this->plugincache[$index] = array($plugin);
343 $headers[] = $plugin->get_name();
350 $columns[] = 'timemarked';
351 $headers[] = get_string('lastmodifiedgrade', 'assign');
354 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
355 if ($this->is_downloading()) {
356 if ($plugin->is_visible() && $plugin->is_enabled()) {
357 foreach ($plugin->get_editor_fields() as $field => $description) {
358 $index = 'plugin' . count($this->plugincache);
359 $this->plugincache[$index] = array($plugin, $field);
361 $headers[] = $description;
364 } else if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
365 $index = 'plugin' . count($this->plugincache);
366 $this->plugincache[$index] = array($plugin);
368 $headers[] = $plugin->get_name();
372 // Exclude 'Final grade' column in downloaded grading worksheets.
373 if (!$this->is_downloading()) {
375 $columns[] = 'finalgrade';
376 $headers[] = get_string('finalgrade', 'grades');
379 // Load the grading info for all users.
380 $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id,
383 $this->assignment->get_instance()->id,
386 if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
387 $columns[] = 'outcomes';
388 $headers[] = get_string('outcomes', 'grades');
392 $this->define_columns($columns);
393 $this->define_headers($headers);
394 foreach ($extrauserfields as $extrafield) {
395 $this->column_class($extrafield, $extrafield);
397 // We require at least one unique column for the sort.
398 $this->sortable(true, 'userid');
399 $this->no_sorting('recordid');
400 $this->no_sorting('finalgrade');
401 $this->no_sorting('userid');
402 $this->no_sorting('select');
403 $this->no_sorting('outcomes');
405 if ($assignment->get_instance()->teamsubmission) {
406 $this->no_sorting('team');
409 $plugincolumnindex = 0;
410 foreach ($this->assignment->get_submission_plugins() as $plugin) {
411 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
412 $submissionpluginindex = 'plugin' . $plugincolumnindex++;
413 $this->no_sorting($submissionpluginindex);
416 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
417 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
418 $feedbackpluginindex = 'plugin' . $plugincolumnindex++;
419 $this->no_sorting($feedbackpluginindex);
423 // When there is no data we still want the column headers printed in the csv file.
424 if ($this->is_downloading()) {
425 $this->start_output();
430 * Before adding each row to the table make sure rownum is incremented.
432 * @param array $row row of data from db used to make one row of the table.
433 * @return array one row for the table
435 public function format_row($row) {
436 if ($this->rownum < 0) {
437 $this->rownum = $this->currpage * $this->pagesize;
442 return parent::format_row($row);
446 * Add a column with an ID that uniquely identifies this user in this assignment.
448 * @param stdClass $row
451 public function col_recordid(stdClass $row) {
452 return get_string('hiddenuser', 'assign') .
453 $this->assignment->get_uniqueid_for_user($row->userid);
458 * Add the userid to the row class so it can be updated via ajax.
460 * @param stdClass $row The row of data
461 * @return string The row class
463 public function get_row_class($row) {
464 return 'user' . $row->userid;
468 * Return the number of rows to display on a single page.
470 * @return int The number of rows per page
472 public function get_rows_per_page() {
473 return $this->perpage;
477 * list current marking workflow state
479 * @param stdClass $row
482 public function col_workflowstatus(stdClass $row) {
485 $gradingdisabled = $this->assignment->grading_disabled($row->id);
486 // The function in the assignment keeps a static cache of this list of states.
487 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
488 $workflowstate = $row->workflowstate;
489 if (empty($workflowstate)) {
490 $workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
492 if ($this->quickgrading && !$gradingdisabled) {
493 $notmarked = get_string('markingworkflowstatenotmarked', 'assign');
494 $name = 'quickgrade_' . $row->id . '_workflowstate';
495 $o .= html_writer::select($workflowstates, $name, $workflowstate, array('' => $notmarked));
496 // Check if this user is a marker that can't manage allocations and doesn't have the marker column added.
497 if ($this->assignment->get_instance()->markingworkflow &&
498 $this->assignment->get_instance()->markingallocation &&
499 !has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
501 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
502 $o .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $name,
503 'value' => $row->allocatedmarker));
506 $o .= $this->output->container(get_string('markingworkflowstate' . $workflowstate, 'assign'), $workflowstate);
512 * For download only - list current marking workflow state
514 * @param stdClass $row - The row of data
515 * @return string The current marking workflow state
517 public function col_workflowstate($row) {
518 $state = $row->workflowstate;
520 $state = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
523 return get_string('markingworkflowstate' . $state, 'assign');
527 * list current marker
529 * @param stdClass $row - The row of data
530 * @return id the user->id of the marker.
532 public function col_allocatedmarker(stdClass $row) {
533 static $markers = null;
534 static $markerlist = array();
535 if ($markers === null) {
536 $markers = get_users_by_capability($this->assignment->get_context(), 'mod/assign:grade');
537 $markerlist[0] = get_string('choosemarker', 'assign');
538 foreach ($markers as $marker) {
539 $markerlist[$marker->id] = fullname($marker);
542 if (empty($markerlist)) {
543 // TODO: add some form of notification here that no markers are available.
546 if ($this->is_downloading()) {
547 if (isset($markers[$row->allocatedmarker])) {
548 return fullname($markers[$row->allocatedmarker]);
554 if ($this->quickgrading && has_capability('mod/assign:manageallocations', $this->assignment->get_context()) &&
555 (empty($row->workflowstate) ||
556 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_INMARKING ||
557 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED)) {
559 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
560 return html_writer::select($markerlist, $name, $row->allocatedmarker, false);
561 } else if (!empty($row->allocatedmarker)) {
563 if ($this->quickgrading) { // Add hidden field for quickgrading page.
564 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
565 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$row->allocatedmarker));
567 $output .= $markerlist[$row->allocatedmarker];
572 * For download only - list all the valid options for this custom scale.
574 * @param stdClass $row - The row of data
575 * @return string A list of valid options for the current scale
577 public function col_scale($row) {
580 if (empty($this->scale)) {
581 $dbparams = array('id'=>-($this->assignment->get_instance()->grade));
582 $this->scale = $DB->get_record('scale', $dbparams);
585 if (!empty($this->scale->scale)) {
586 return implode("\n", explode(',', $this->scale->scale));
592 * Display a grade with scales etc.
594 * @param string $grade
595 * @param boolean $editable
596 * @param int $userid The user id of the user this grade belongs to
597 * @param int $modified Timestamp showing when the grade was last modified
598 * @return string The formatted grade
600 public function display_grade($grade, $editable, $userid, $modified) {
601 if ($this->is_downloading()) {
602 if ($this->assignment->get_instance()->grade >= 0) {
603 if ($grade == -1 || $grade === null) {
606 return format_float($grade, 2);
608 // This is a custom scale.
609 $scale = $this->assignment->display_grade($grade, false);
616 return $this->assignment->display_grade($grade, $editable, $userid, $modified);
620 * Get the team info for this user.
622 * @param stdClass $row
623 * @return string The team name
625 public function col_team(stdClass $row) {
628 $this->get_group_and_submission($row->id, $group, $submission, -1);
632 return get_string('defaultteam', 'assign');
636 * Use a static cache to try and reduce DB calls.
638 * @param int $userid The user id for this submission
639 * @param int $group The groupid (returned)
640 * @param stdClass|false $submission The stdClass submission or false (returned)
641 * @param int $attemptnumber Return a specific attempt number (-1 for latest)
643 protected function get_group_and_submission($userid, &$group, &$submission, $attemptnumber) {
645 if (isset($this->submissiongroups[$userid])) {
646 $group = $this->submissiongroups[$userid];
648 $group = $this->assignment->get_submission_group($userid, false);
649 $this->submissiongroups[$userid] = $group;
654 $groupid = $group->id;
657 // Static cache is keyed by groupid and attemptnumber.
658 // We may need both the latest and previous attempt in the same page.
659 if (isset($this->groupsubmissions[$groupid . ':' . $attemptnumber])) {
660 $submission = $this->groupsubmissions[$groupid . ':' . $attemptnumber];
662 $submission = $this->assignment->get_group_submission($userid, $groupid, false, $attemptnumber);
663 $this->groupsubmissions[$groupid . ':' . $attemptnumber] = $submission;
668 * Format a list of outcomes.
670 * @param stdClass $row
673 public function col_outcomes(stdClass $row) {
675 foreach ($this->gradinginfo->outcomes as $index => $outcome) {
676 $options = make_grades_menu(-$outcome->scaleid);
678 $options[0] = get_string('nooutcome', 'grades');
679 if ($this->quickgrading && !($outcome->grades[$row->userid]->locked)) {
680 $select = '<select name="outcome_' . $index . '_' . $row->userid . '" class="quickgrade">';
681 foreach ($options as $optionindex => $optionvalue) {
683 if ($outcome->grades[$row->userid]->grade == $optionindex) {
684 $selected = 'selected="selected"';
686 $select .= '<option value="' . $optionindex . '"' . $selected . '>' . $optionvalue . '</option>';
688 $select .= '</select>';
689 $outcomes .= $this->output->container($outcome->name . ': ' . $select, 'outcome');
691 $name = $outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade];
692 if ($this->is_downloading()) {
695 $outcomes .= $this->output->container($name, 'outcome');
705 * Format a user picture for display.
707 * @param stdClass $row
710 public function col_picture(stdClass $row) {
712 return $this->output->user_picture($row);
718 * Format a user record for display (link to profile).
720 * @param stdClass $row
723 public function col_fullname($row) {
724 if (!$this->is_downloading()) {
725 $courseid = $this->assignment->get_course()->id;
726 $link= new moodle_url('/user/view.php', array('id' =>$row->id, 'course'=>$courseid));
727 $fullname = $this->output->action_link($link, $this->assignment->fullname($row));
729 $fullname = $this->assignment->fullname($row);
732 if (!$this->assignment->is_active_user($row->id)) {
733 $suspendedstring = get_string('userenrolmentsuspended', 'grades');
734 $fullname .= ' ' . html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/enrolmentsuspended'),
735 'title' => $suspendedstring, 'alt' => $suspendedstring, 'class' => 'usersuspendedicon'));
736 $fullname = html_writer::tag('span', $fullname, array('class' => 'usersuspended'));
742 * Insert a checkbox for selecting the current row for batch operations.
744 * @param stdClass $row
747 public function col_select(stdClass $row) {
748 $selectcol = '<label class="accesshide" for="selectuser_' . $row->userid . '">';
749 $selectcol .= get_string('selectuser', 'assign', $this->assignment->fullname($row));
750 $selectcol .= '</label>';
751 $selectcol .= '<input type="checkbox"
752 id="selectuser_' . $row->userid . '"
754 value="' . $row->userid . '"/>';
755 $selectcol .= '<input type="hidden"
756 name="grademodified_' . $row->userid . '"
757 value="' . $row->timemarked . '"/>';
762 * Return a users grades from the listing of all grade data for this assignment.
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];
775 * Format a column of data for display.
777 * @param stdClass $row
780 public function col_gradecanbechanged(stdClass $row) {
781 $gradingdisabled = $this->assignment->grading_disabled($row->id);
782 if ($gradingdisabled) {
783 return get_string('no');
785 return get_string('yes');
790 * Format a column of data for display
792 * @param stdClass $row
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
805 public function col_grade(stdClass $row) {
809 $separator = $this->output->spacer(array(), true);
811 $gradingdisabled = $this->assignment->grading_disabled($row->id);
813 if (!$this->is_downloading() && $this->hasgrade) {
814 $name = $this->assignment->fullname($row);
815 $icon = $this->output->pix_icon('gradefeedback',
816 get_string('gradeuser', 'assign', $name),
818 $urlparams = array('id' => $this->assignment->get_course_module()->id,
819 'rownum'=>$this->rownum,
821 $url = new moodle_url('/mod/assign/view.php', $urlparams);
822 $link = $this->output->action_link($url, $icon);
823 $grade .= $link . $separator;
826 $grade .= $this->display_grade($row->grade,
827 $this->quickgrading && !$gradingdisabled,
835 * Format a column of data for display.
837 * @param stdClass $row
840 public function col_finalgrade(stdClass $row) {
843 $grade = $this->get_gradebook_data_for_user($row->userid);
845 $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
852 * Format a column of data for display.
854 * @param stdClass $row
857 public function col_timemarked(stdClass $row) {
860 if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
861 $o = userdate($row->timemarked);
863 if ($row->timemarked && $this->is_downloading()) {
864 // Force it for downloads as it affects import.
865 $o = userdate($row->timemarked);
872 * Format a column of data for display.
874 * @param stdClass $row
877 public function col_timesubmitted(stdClass $row) {
882 $this->get_group_and_submission($row->id, $group, $submission, -1);
883 if ($group && $submission && $submission->timemodified) {
884 $o = userdate($submission->timemodified);
885 } else if ($row->timesubmitted) {
886 $o = userdate($row->timesubmitted);
893 * Format a column of data for display
895 * @param stdClass $row
898 public function col_status(stdClass $row) {
901 $instance = $this->assignment->get_instance();
903 $due = $instance->duedate;
904 if ($row->extensionduedate) {
905 $due = $row->extensionduedate;
910 $this->get_group_and_submission($row->id, $group, $submission, -1);
911 if ($group && $submission) {
912 $timesubmitted = $submission->timemodified;
913 $status = $submission->status;
915 $timesubmitted = $row->timesubmitted;
916 $status = $row->status;
919 if ($this->assignment->is_any_submission_plugin_enabled()) {
921 $o .= $this->output->container(get_string('submissionstatus_' . $status, 'assign'),
922 array('class'=>'submissionstatus' .$status));
923 if ($due && $timesubmitted > $due) {
924 $usertime = format_time($timesubmitted - $due);
925 $latemessage = get_string('submittedlateshort',
928 $o .= $this->output->container($latemessage, 'latesubmission');
931 $lockedstr = get_string('submissionslockedshort', 'assign');
932 $o .= $this->output->container($lockedstr, 'lockedsubmission');
935 // Add status of "grading", use markflow if enabled.
936 if ($instance->markingworkflow) {
937 $o .= $this->col_workflowstatus($row);
938 } else if ($row->grade !== null && $row->grade >= 0) {
939 $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
940 } else if (!$timesubmitted) {
942 if ($due && ($now > $due)) {
943 $overduestr = get_string('overdue', 'assign', format_time($now - $due));
944 $o .= $this->output->container($overduestr, 'overduesubmission');
948 if ($row->extensionduedate) {
949 $userdate = userdate($row->extensionduedate);
950 $extensionstr = get_string('userextensiondate', 'assign', $userdate);
951 $o .= $this->output->container($extensionstr, 'extensiondate');
955 if ($this->is_downloading()) {
956 $o = strip_tags(str_replace('</div>', "\n", $o));
963 * Format a column of data for display.
965 * @param stdClass $row
968 public function col_userid(stdClass $row) {
975 $urlparams = array('id'=>$this->assignment->get_course_module()->id,
976 'rownum'=>$this->rownum,
978 $url = new moodle_url('/mod/assign/view.php', $urlparams);
982 $description = get_string('grade');
984 $description = get_string('updategrade', 'assign');
986 $actions['grade'] = new action_menu_link_secondary(
992 // Everything we need is in the row.
995 if ($this->assignment->get_instance()->teamsubmission) {
996 // Use the cache for this.
999 $this->get_group_and_submission($row->id, $group, $submission, -1);
1002 $submissionsopen = $this->assignment->submissions_open($row->id,
1006 $this->gradinginfo);
1007 $caneditsubmission = $this->assignment->can_edit_submission($row->id, $USER->id);
1009 // Hide for offline assignments.
1010 if ($this->assignment->is_any_submission_plugin_enabled()) {
1011 if (!$row->status ||
1012 $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
1013 !$this->assignment->get_instance()->submissiondrafts) {
1015 if (!$row->locked) {
1016 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1019 'sesskey'=>sesskey(),
1020 'page'=>$this->currpage);
1021 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1023 $description = get_string('preventsubmissionsshort', 'assign');
1024 $actions['lock'] = new action_menu_link_secondary(
1030 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1033 'sesskey'=>sesskey(),
1034 'page'=>$this->currpage);
1035 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1036 $description = get_string('allowsubmissionsshort', 'assign');
1037 $actions['unlock'] = new action_menu_link_secondary(
1045 if (($this->assignment->get_instance()->duedate ||
1046 $this->assignment->get_instance()->cutoffdate) &&
1047 $this->hasgrantextension) {
1048 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1050 'action'=>'grantextension',
1051 'sesskey'=>sesskey(),
1052 'page'=>$this->currpage);
1053 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1054 $description = get_string('grantextension', 'assign');
1055 $actions['grantextension'] = new action_menu_link_secondary(
1061 if ($submissionsopen &&
1062 $USER->id != $row->id &&
1063 $caneditsubmission) {
1064 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1066 'action'=>'editsubmission',
1067 'sesskey'=>sesskey(),
1068 'page'=>$this->currpage);
1069 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1070 $description = get_string('editsubmission', 'assign');
1071 $actions['editsubmission'] = new action_menu_link_secondary(
1078 if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
1079 $this->assignment->get_instance()->submissiondrafts) {
1080 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1082 'action'=>'reverttodraft',
1083 'sesskey'=>sesskey(),
1084 'page'=>$this->currpage);
1085 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1086 $description = get_string('reverttodraftshort', 'assign');
1087 $actions['reverttodraft'] = new action_menu_link_secondary(
1093 if ($row->status == ASSIGN_SUBMISSION_STATUS_DRAFT &&
1094 $this->assignment->get_instance()->submissiondrafts &&
1095 $caneditsubmission &&
1097 $row->id != $USER->id) {
1098 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1100 'action'=>'submitotherforgrading',
1101 'sesskey'=>sesskey(),
1102 'page'=>$this->currpage);
1103 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1104 $description = get_string('submitforgrading', 'assign');
1105 $actions['submitforgrading'] = new action_menu_link_secondary(
1112 $ismanual = $this->assignment->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
1113 $hassubmission = !empty($row->status);
1114 $notreopened = $hassubmission && $row->status != ASSIGN_SUBMISSION_STATUS_REOPENED;
1115 $isunlimited = $this->assignment->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS;
1116 $hasattempts = $isunlimited || $row->attemptnumber < $this->assignment->get_instance()->maxattempts - 1;
1118 if ($ismanual && $hassubmission && $notreopened && $hasattempts) {
1119 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1121 'action'=>'addattempt',
1122 'sesskey'=>sesskey(),
1123 'page'=>$this->currpage);
1124 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1125 $description = get_string('addattempt', 'assign');
1126 $actions['addattempt'] = new action_menu_link_secondary(
1133 $menu = new action_menu();
1134 $menu->set_owner_selector('.gradingtable-actionmenu');
1135 $menu->set_alignment(action_menu::TL, action_menu::BL);
1136 $menu->set_constraint('.gradingtable > .no-overflow');
1137 $menu->set_menu_trigger(get_string('edit'));
1138 foreach ($actions as $action) {
1139 $menu->add($action);
1142 // Prioritise the menu ahead of all other actions.
1143 $menu->prioritise = true;
1145 $edit .= $this->output->render($menu);
1151 * Write the plugin summary with an optional link to view the full feedback/submission.
1153 * @param assign_plugin $plugin Submission plugin or feedback plugin
1154 * @param stdClass $item Submission or grade
1155 * @param string $returnaction The return action to pass to the
1156 * view_submission page (the current page)
1157 * @param string $returnparams The return params to pass to the view_submission
1158 * page (the current page)
1159 * @return string The summary with an optional link
1161 private function format_plugin_summary_with_link(assign_plugin $plugin,
1166 $showviewlink = false;
1168 $summary = $plugin->view_summary($item, $showviewlink);
1170 if ($showviewlink) {
1171 $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
1172 $icon = $this->output->pix_icon('t/preview', $viewstr);
1173 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1176 'plugin'=>$plugin->get_type(),
1177 'action'=>'viewplugin' . $plugin->get_subtype(),
1178 'returnaction'=>$returnaction,
1179 'returnparams'=>http_build_query($returnparams));
1180 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1181 $link = $this->output->action_link($url, $icon);
1182 $separator = $this->output->spacer(array(), true);
1185 return $link . $separator . $summary;
1190 * Format the submission and feedback columns.
1192 * @param string $colname The column name
1193 * @param stdClass $row The submission row
1194 * @return mixed string or NULL
1196 public function other_cols($colname, $row) {
1197 // For extra user fields the result is already in $row.
1198 if (empty($this->plugincache[$colname])) {
1199 return $row->$colname;
1202 // This must be a plugin field.
1203 $plugincache = $this->plugincache[$colname];
1205 $plugin = $plugincache[0];
1208 if (isset($plugincache[1])) {
1209 $field = $plugincache[1];
1212 if ($plugin->is_visible() && $plugin->is_enabled()) {
1213 if ($plugin->get_subtype() == 'assignsubmission') {
1214 if ($this->assignment->get_instance()->teamsubmission) {
1216 $submission = false;
1218 $this->get_group_and_submission($row->id, $group, $submission, -1);
1220 if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1221 // For a newly reopened submission - we want to show the previous submission in the table.
1222 $this->get_group_and_submission($row->id, $group, $submission, $submission->attemptnumber-1);
1224 if (isset($field)) {
1225 return $plugin->get_editor_text($field, $submission->id);
1227 return $this->format_plugin_summary_with_link($plugin,
1232 } else if ($row->submissionid) {
1233 if ($row->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1234 // For a newly reopened submission - we want to show the previous submission in the table.
1235 $submission = $this->assignment->get_user_submission($row->userid, false, $row->attemptnumber - 1);
1237 $submission = new stdClass();
1238 $submission->id = $row->submissionid;
1239 $submission->timecreated = $row->firstsubmission;
1240 $submission->timemodified = $row->timesubmitted;
1241 $submission->assignment = $this->assignment->get_instance()->id;
1242 $submission->userid = $row->userid;
1243 $submission->attemptnumber = $row->attemptnumber;
1245 // Field is used for only for import/export and refers the the fieldname for the text editor.
1246 if (isset($field)) {
1247 return $plugin->get_editor_text($field, $submission->id);
1249 return $this->format_plugin_summary_with_link($plugin,
1256 if (isset($field)) {
1257 return $plugin->get_editor_text($field, $row->gradeid);
1260 if ($row->gradeid) {
1261 $grade = new stdClass();
1262 $grade->id = $row->gradeid;
1263 $grade->timecreated = $row->firstmarked;
1264 $grade->timemodified = $row->timemarked;
1265 $grade->assignment = $this->assignment->get_instance()->id;
1266 $grade->userid = $row->userid;
1267 $grade->grade = $row->grade;
1268 $grade->mailed = $row->mailed;
1269 $grade->attemptnumber = $row->attemptnumber;
1271 if ($this->quickgrading && $plugin->supports_quickgrading()) {
1272 return $plugin->get_quickgrading_html($row->userid, $grade);
1273 } else if ($grade) {
1274 return $this->format_plugin_summary_with_link($plugin,
1285 * Using the current filtering and sorting - load all rows and return a single column from them.
1287 * @param string $columnname The name of the raw column data
1288 * @return array of data
1290 public function get_column_data($columnname) {
1292 $this->currpage = 0;
1293 $this->query_db($this->tablemaxrows);
1295 foreach ($this->rawdata as $row) {
1296 $result[] = $row->$columnname;
1302 * Return things to the renderer.
1304 * @return string the assignment name
1306 public function get_assignment_name() {
1307 return $this->assignment->get_instance()->name;
1311 * Return things to the renderer.
1313 * @return int the course module id
1315 public function get_course_module_id() {
1316 return $this->assignment->get_course_module()->id;
1320 * Return things to the renderer.
1322 * @return int the course id
1324 public function get_course_id() {
1325 return $this->assignment->get_course()->id;
1329 * Return things to the renderer.
1331 * @return stdClass The course context
1333 public function get_course_context() {
1334 return $this->assignment->get_course_context();
1338 * Return things to the renderer.
1340 * @return bool Does this assignment accept submissions
1342 public function submissions_enabled() {
1343 return $this->assignment->is_any_submission_plugin_enabled();
1347 * Return things to the renderer.
1349 * @return bool Can this user view all grades (the gradebook)
1351 public function can_view_all_grades() {
1352 $context = $this->assignment->get_course_context();
1353 return has_capability('gradereport/grader:view', $context) &&
1354 has_capability('moodle/grade:viewall', $context);
1358 * Override the table show_hide_link to not show for select column.
1360 * @param string $column the column name, index into various names.
1361 * @param int $index numerical index of the column.
1362 * @return string HTML fragment.
1364 protected function show_hide_link($column, $index) {
1365 if ($index > 0 || !$this->hasgrade) {
1366 return parent::show_hide_link($column, $index);