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 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;
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?
75 public function __construct(assign $assignment,
80 $downloadfilename = null) {
81 global $CFG, $PAGE, $DB, $USER;
82 parent::__construct('mod_assign_grading');
83 $this->assignment = $assignment;
85 foreach ($assignment->get_feedback_plugins() as $plugin) {
86 if ($plugin->is_visible() && $plugin->is_enabled()) {
87 foreach ($plugin->get_grading_batch_operations() as $action => $description) {
88 if (empty($this->plugingradingbatchoperations)) {
89 $this->plugingradingbatchoperations[$plugin->get_type()] = array();
91 $this->plugingradingbatchoperations[$plugin->get_type()][$action] = $description;
95 $this->perpage = $perpage;
96 $this->quickgrading = $quickgrading;
97 $this->output = $PAGE->get_renderer('mod_assign');
99 $urlparams = array('action'=>'grading', 'id'=>$assignment->get_course_module()->id);
100 $url = new moodle_url($CFG->wwwroot . '/mod/assign/view.php', $urlparams);
101 $this->define_baseurl($url);
103 // Do some business - then set the sql.
104 $currentgroup = groups_get_activity_group($assignment->get_course_module(), true);
107 $this->rownum = $rowoffset - 1;
110 $users = array_keys( $assignment->list_participants($currentgroup, true));
111 if (count($users) == 0) {
112 // Insert a record that will never match to the sql is still valid.
117 $params['assignmentid1'] = (int)$this->assignment->get_instance()->id;
118 $params['assignmentid2'] = (int)$this->assignment->get_instance()->id;
119 $params['assignmentid3'] = (int)$this->assignment->get_instance()->id;
120 $params['assignmentid4'] = (int)$this->assignment->get_instance()->id;
121 $params['assignmentid5'] = (int)$this->assignment->get_instance()->id;
123 $extrauserfields = get_extra_user_fields($this->assignment->get_context());
125 $fields = user_picture::fields('u', $extrauserfields) . ', ';
126 $fields .= 'u.id as userid, ';
127 $fields .= 's.status as status, ';
128 $fields .= 's.id as submissionid, ';
129 $fields .= 's.timecreated as firstsubmission, ';
130 $fields .= 's.timemodified as timesubmitted, ';
131 $fields .= 's.attemptnumber as attemptnumber, ';
132 $fields .= 'g.id as gradeid, ';
133 $fields .= 'g.grade as grade, ';
134 $fields .= 'g.timemodified as timemarked, ';
135 $fields .= 'g.timecreated as firstmarked, ';
136 $fields .= 'uf.mailed as mailed, ';
137 $fields .= 'uf.locked as locked, ';
138 $fields .= 'uf.extensionduedate as extensionduedate, ';
139 $fields .= 'uf.workflowstate as workflowstate, ';
140 $fields .= 'uf.allocatedmarker as allocatedmarker ';
142 $submissionmaxattempt = 'SELECT mxs.userid, MAX(mxs.attemptnumber) AS maxattempt
143 FROM {assign_submission} mxs
144 WHERE mxs.assignment = :assignmentid4 GROUP BY mxs.userid';
145 $grademaxattempt = 'SELECT mxg.userid, MAX(mxg.attemptnumber) AS maxattempt
146 FROM {assign_grades} mxg
147 WHERE mxg.assignment = :assignmentid5 GROUP BY mxg.userid';
149 LEFT JOIN ( ' . $submissionmaxattempt . ' ) smx ON u.id = smx.userid
150 LEFT JOIN ( ' . $grademaxattempt . ' ) gmx ON u.id = gmx.userid
151 LEFT JOIN {assign_submission} s ON
153 s.assignment = :assignmentid1 AND
154 s.attemptnumber = smx.maxattempt
155 LEFT JOIN {assign_grades} g ON
157 g.assignment = :assignmentid2 AND
158 g.attemptnumber = gmx.maxattempt
159 LEFT JOIN {assign_user_flags} uf ON u.id = uf.userid AND uf.assignment = :assignmentid3';
161 $userparams = array();
164 list($userwhere, $userparams) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
165 $where = 'u.id ' . $userwhere;
166 $params = array_merge($params, $userparams);
168 // The filters do not make sense when there are no submissions, so do not apply them.
169 if ($this->assignment->is_any_submission_plugin_enabled()) {
170 if ($filter == ASSIGN_FILTER_SUBMITTED) {
171 $where .= ' AND (s.timemodified IS NOT NULL AND
172 s.status = :submitted) ';
173 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
175 } else if ($filter == ASSIGN_FILTER_REQUIRE_GRADING) {
176 $where .= ' AND (s.timemodified IS NOT NULL AND
177 s.status = :submitted AND
178 (s.timemodified > g.timemodified OR g.timemodified IS NULL))';
179 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
181 } else if (strpos($filter, ASSIGN_FILTER_SINGLE_USER) === 0) {
182 $userfilter = (int) array_pop(explode('=', $filter));
183 $where .= ' AND (u.id = :userid)';
184 $params['userid'] = $userfilter;
188 if ($this->assignment->get_instance()->markingallocation) {
189 if (has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
190 //Check to see if marker filter is set
191 $markerfilter = (int)get_user_preferences('assign_markerfilter', '');
192 if (!empty($markerfilter)) {
193 $where .= ' AND uf.allocatedmarker = :markerid';
194 $params['markerid'] = $markerfilter;
196 } else { // Only show users allocated to this marker.
197 $where .= ' AND uf.allocatedmarker = :markerid';
198 $params['markerid'] = $USER->id;
202 if ($this->assignment->get_instance()->markingworkflow) {
203 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
204 if (!empty($workflowstates)) {
205 $workflowfilter = get_user_preferences('assign_workflowfilter', '');
206 if (array_key_exists($workflowfilter, $workflowstates) || $workflowfilter == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED) {
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;
212 $where .= ' AND uf.workflowstate = :workflowstate';
213 $params['workflowstate'] = $workflowfilter;
219 $this->set_sql($fields, $from, $where, $params);
221 if ($downloadfilename) {
222 $this->is_downloading('csv', $downloadfilename);
229 if (!$this->is_downloading()) {
230 $columns[] = 'select';
231 $headers[] = get_string('select') .
232 '<div class="selectall"><label class="accesshide" for="selectall">' . get_string('selectall') . '</label>
233 <input type="checkbox" id="selectall" name="selectall" title="' . get_string('selectall') . '"/></div>';
237 if (!$this->assignment->is_blind_marking()) {
238 if (!$this->is_downloading()) {
239 $columns[] = 'picture';
240 $headers[] = get_string('pictureofuser');
242 $columns[] = 'recordid';
243 $headers[] = get_string('recordid', 'assign');
247 $columns[] = 'fullname';
248 $headers[] = get_string('fullname');
250 foreach ($extrauserfields as $extrafield) {
251 $columns[] = $extrafield;
252 $headers[] = get_user_field_name($extrafield);
256 $columns[] = 'recordid';
257 $headers[] = get_string('recordid', 'assign');
260 // Submission status.
261 if ($assignment->is_any_submission_plugin_enabled()) {
262 $columns[] = 'status';
263 $headers[] = get_string('status');
264 } else if ($this->assignment->get_instance()->markingworkflow) {
265 $columns[] = 'workflowstatus';
266 $headers[] = get_string('status');
270 // Team submission columns.
271 if ($assignment->get_instance()->teamsubmission) {
273 $headers[] = get_string('submissionteam', 'assign');
275 $columns[] = 'teamstatus';
276 $headers[] = get_string('teamsubmissionstatus', 'assign');
279 if ($this->assignment->get_instance()->markingallocation &&
280 has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
281 // Add a column for the allocated marker.
282 $columns[] = 'allocatedmarker';
283 $headers[] = get_string('marker', 'assign');
286 $columns[] = 'grade';
287 $headers[] = get_string('grade');
288 if ($this->is_downloading()) {
289 if ($this->assignment->get_instance()->grade >= 0) {
290 $columns[] = 'grademax';
291 $headers[] = get_string('maxgrade', 'assign');
293 // This is a custom scale.
294 $columns[] = 'scale';
295 $headers[] = get_string('scale', 'assign');
298 if ($this->assignment->get_instance()->markingworkflow) {
299 // Add a column for the marking workflow state.
300 $columns[] = 'workflowstate';
301 $headers[] = get_string('markingworkflowstate', 'assign');
303 // Add a column for the list of valid marking workflow states.
304 $columns[] = 'gradecanbechanged';
305 $headers[] = get_string('gradecanbechanged', 'assign');
307 if (!$this->is_downloading()) {
308 // We have to call this column userid so we can use userid as a default sortable column.
309 $columns[] = 'userid';
310 $headers[] = get_string('edit');
313 // Submission plugins.
314 if ($assignment->is_any_submission_plugin_enabled()) {
315 $columns[] = 'timesubmitted';
316 $headers[] = get_string('lastmodifiedsubmission', 'assign');
318 foreach ($this->assignment->get_submission_plugins() as $plugin) {
319 if ($this->is_downloading()) {
320 if ($plugin->is_visible() && $plugin->is_enabled()) {
321 foreach ($plugin->get_editor_fields() as $field => $description) {
322 $index = 'plugin' . count($this->plugincache);
323 $this->plugincache[$index] = array($plugin, $field);
325 $headers[] = $plugin->get_name();
329 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
330 $index = 'plugin' . count($this->plugincache);
331 $this->plugincache[$index] = array($plugin);
333 $headers[] = $plugin->get_name();
340 $columns[] = 'timemarked';
341 $headers[] = get_string('lastmodifiedgrade', 'assign');
344 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
345 if ($this->is_downloading()) {
346 if ($plugin->is_visible() && $plugin->is_enabled()) {
347 foreach ($plugin->get_editor_fields() as $field => $description) {
348 $index = 'plugin' . count($this->plugincache);
349 $this->plugincache[$index] = array($plugin, $field);
351 $headers[] = $description;
354 } else if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
355 $index = 'plugin' . count($this->plugincache);
356 $this->plugincache[$index] = array($plugin);
358 $headers[] = $plugin->get_name();
362 // Exclude 'Final grade' column in downloaded grading worksheets.
363 if (!$this->is_downloading()) {
365 $columns[] = 'finalgrade';
366 $headers[] = get_string('finalgrade', 'grades');
369 // Load the grading info for all users.
370 $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id,
373 $this->assignment->get_instance()->id,
375 $this->hasgrantextension = has_capability('mod/assign:grantextension',
376 $this->assignment->get_context());
378 if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
379 $columns[] = 'outcomes';
380 $headers[] = get_string('outcomes', 'grades');
384 $this->define_columns($columns);
385 $this->define_headers($headers);
386 foreach ($extrauserfields as $extrafield) {
387 $this->column_class($extrafield, $extrafield);
389 // We require at least one unique column for the sort.
390 $this->sortable(true, 'userid');
391 $this->no_sorting('recordid');
392 $this->no_sorting('finalgrade');
393 $this->no_sorting('userid');
394 $this->no_sorting('select');
395 $this->no_sorting('outcomes');
397 if ($assignment->get_instance()->teamsubmission) {
398 $this->no_sorting('team');
399 $this->no_sorting('teamstatus');
402 $plugincolumnindex = 0;
403 foreach ($this->assignment->get_submission_plugins() as $plugin) {
404 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
405 $submissionpluginindex = 'plugin' . $plugincolumnindex++;
406 $this->no_sorting($submissionpluginindex);
409 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
410 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
411 $feedbackpluginindex = 'plugin' . $plugincolumnindex++;
412 $this->no_sorting($feedbackpluginindex);
416 // When there is no data we still want the column headers printed in the csv file.
417 if ($this->is_downloading()) {
418 $this->start_output();
423 * Before adding each row to the table make sure rownum is incremented.
425 * @param array $row row of data from db used to make one row of the table.
426 * @return array one row for the table
428 public function format_row($row) {
429 if ($this->rownum < 0) {
430 $this->rownum = $this->currpage * $this->pagesize;
435 return parent::format_row($row);
439 * Add a column with an ID that uniquely identifies this user in this assignment.
443 public function col_recordid(stdClass $row) {
444 return get_string('hiddenuser', 'assign') .
445 $this->assignment->get_uniqueid_for_user($row->userid);
450 * Add the userid to the row class so it can be updated via ajax.
452 * @param stdClass $row The row of data
453 * @return string The row class
455 public function get_row_class($row) {
456 return 'user' . $row->userid;
460 * Return the number of rows to display on a single page.
462 * @return int The number of rows per page
464 public function get_rows_per_page() {
465 return $this->perpage;
469 * list current marking workflow state
471 * @param stdClass $row
474 public function col_workflowstatus(stdClass $row) {
477 $gradingdisabled = $this->assignment->grading_disabled($row->id);
478 if ($this->assignment->get_instance()->markingworkflow) {
479 // The function in the assignment keeps a static cache of this list of states.
480 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
481 $workflowstate = $row->workflowstate;
482 if (empty($workflowstate)) {
483 $workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
485 if ($this->quickgrading && !$gradingdisabled) {
486 $name = 'quickgrade_' . $row->id . '_workflowstate';
487 $o .= html_writer::select($workflowstates, $name, $workflowstate, array('' => get_string('markingworkflowstatenotmarked', 'assign')));
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));
496 $o .= $this->output->container(get_string('markingworkflowstate' . $workflowstate, 'assign'), $workflowstate);
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;
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 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.
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)) {
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];
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) {
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));
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) {
593 return format_float($grade, 2);
595 // This is a custom scale.
596 $scale = $this->assignment->display_grade($grade, false);
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) {
615 $this->get_group_and_submission($row->id, $group, $submission, -1);
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 $groupid The groupid (returned)
627 * @param mixed $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) {
632 if (isset($this->submissiongroups[$userid])) {
633 $group = $this->submissiongroups[$userid];
635 $group = $this->assignment->get_submission_group($userid, false);
636 $this->submissiongroups[$userid] = $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];
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) {
664 $this->get_group_and_submission($row->id, $group, $submission, -1);
668 $status = $submission->status;
670 return get_string('submissionstatus_' . $status, 'assign');
675 * Format a list of outcomes.
677 * @param stdClass $row
680 public function col_outcomes(stdClass $row) {
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) {
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');
698 $name = $outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade];
699 $outcomes .= $this->output->container($name, 'outcome');
708 * Format a user picture for display.
710 * @param stdClass $row
713 public function col_picture(stdClass $row) {
715 return $this->output->user_picture($row);
721 * Format a user record for display (link to profile).
723 * @param stdClass $row
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 return $this->output->action_link($link, fullname($row));
732 return fullname($row);
737 * Insert a checkbox for selecting the current row for batch operations.
739 * @param stdClass $row
742 public function col_select(stdClass $row) {
743 $selectcol = '<label class="accesshide" for="selectuser_' . $row->userid . '">';
744 $selectcol .= get_string('selectuser', 'assign', fullname($row));
745 $selectcol .= '</label>';
746 $selectcol .= '<input type="checkbox"
747 id="selectuser_' . $row->userid . '"
749 value="' . $row->userid . '"/>';
754 * Return a users grades from the listing of all grade data for this assignment.
757 * @return mixed stdClass or false
759 private function get_gradebook_data_for_user($userid) {
760 if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
761 return $this->gradinginfo->items[0]->grades[$userid];
767 * Format a column of data for display.
769 * @param stdClass $row
772 public function col_gradecanbechanged(stdClass $row) {
773 $gradingdisabled = $this->assignment->grading_disabled($row->id);
774 if ($gradingdisabled) {
775 return get_string('no');
777 return get_string('yes');
782 * Format a column of data for display
784 * @param stdClass $row
787 public function col_grademax(stdClass $row) {
788 return format_float($this->assignment->get_instance()->grade, 2);
792 * Format a column of data for display.
794 * @param stdClass $row
797 public function col_grade(stdClass $row) {
801 $separator = $this->output->spacer(array(), true);
803 $gradingdisabled = $this->assignment->grading_disabled($row->id);
805 if (!$this->is_downloading()) {
806 $name = fullname($row);
807 if ($this->assignment->is_blind_marking()) {
808 $name = get_string('hiddenuser', 'assign') .
809 $this->assignment->get_uniqueid_for_user($row->userid);
811 $icon = $this->output->pix_icon('gradefeedback',
812 get_string('gradeuser', 'assign', $name),
814 $urlparams = array('id' => $this->assignment->get_course_module()->id,
815 'rownum'=>$this->rownum,
817 $url = new moodle_url('/mod/assign/view.php', $urlparams);
818 $link = $this->output->action_link($url, $icon);
819 $grade .= $link . $separator;
822 $grade .= $this->display_grade($row->grade,
823 $this->quickgrading && !$gradingdisabled,
831 * Format a column of data for display.
833 * @param stdClass $row
836 public function col_finalgrade(stdClass $row) {
839 $grade = $this->get_gradebook_data_for_user($row->userid);
841 $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
848 * Format a column of data for display.
850 * @param stdClass $row
853 public function col_timemarked(stdClass $row) {
856 if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
857 $o = userdate($row->timemarked);
859 if ($row->timemarked && $this->is_downloading()) {
860 // Force it for downloads as it affects import.
861 $o = userdate($row->timemarked);
868 * Format a column of data for display.
870 * @param stdClass $row
873 public function col_timesubmitted(stdClass $row) {
876 if ($row->timesubmitted) {
877 $o = userdate($row->timesubmitted);
884 * Format a column of data for display
886 * @param stdClass $row
889 public function col_status(stdClass $row) {
892 $instance = $this->assignment->get_instance();
894 if ($this->assignment->is_any_submission_plugin_enabled()) {
896 $o .= $this->output->container(get_string('submissionstatus_' . $row->status, 'assign'),
897 array('class'=>'submissionstatus' .$row->status));
898 if ($instance->duedate &&
899 $row->timesubmitted > $instance->duedate) {
900 if (!$row->extensionduedate ||
901 $row->timesubmitted > $row->extensionduedate) {
902 $usertime = format_time($row->timesubmitted - $instance->duedate);
903 $latemessage = get_string('submittedlateshort',
906 $o .= $this->output->container($latemessage, 'latesubmission');
910 $lockedstr = get_string('submissionslockedshort', 'assign');
911 $o .= $this->output->container($lockedstr, 'lockedsubmission');
913 $o .= $this->col_workflowstatus($row);
914 if (!$row->timesubmitted) {
916 $due = $instance->duedate;
917 if ($row->extensionduedate) {
918 $due = $row->extensionduedate;
920 if ($due && ($now > $due)) {
921 $overduestr = get_string('overdue', 'assign', format_time($now - $due));
922 $o .= $this->output->container($overduestr, 'overduesubmission');
925 if ($row->extensionduedate) {
926 $userdate = userdate($row->extensionduedate);
927 $extensionstr = get_string('userextensiondate', 'assign', $userdate);
928 $o .= $this->output->container($extensionstr, 'extensiondate');
932 if ($this->is_downloading()) {
933 $o = strip_tags(str_replace('</div>', "\n", $o));
940 * Format a column of data for display.
942 * @param stdClass $row
945 public function col_userid(stdClass $row) {
950 $urlparams = array('id'=>$this->assignment->get_course_module()->id,
951 'rownum'=>$this->rownum,
953 $url = new moodle_url('/mod/assign/view.php', $urlparams);
956 $description = get_string('grade');
958 $description = get_string('updategrade', 'assign');
960 $actions[$url->out(false)] = $description;
962 // Hide for offline assignments.
963 if ($this->assignment->is_any_submission_plugin_enabled()) {
965 $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
966 !$this->assignment->get_instance()->submissiondrafts) {
969 $urlparams = array('id' => $this->assignment->get_course_module()->id,
972 'sesskey'=>sesskey(),
973 'page'=>$this->currpage);
974 $url = new moodle_url('/mod/assign/view.php', $urlparams);
976 $description = get_string('preventsubmissionsshort', 'assign');
977 $actions[$url->out(false)] = $description;
979 $urlparams = array('id' => $this->assignment->get_course_module()->id,
982 'sesskey'=>sesskey(),
983 'page'=>$this->currpage);
984 $url = new moodle_url('/mod/assign/view.php', $urlparams);
985 $description = get_string('allowsubmissionsshort', 'assign');
986 $actions[$url->out(false)] = $description;
990 if (($this->assignment->get_instance()->duedate ||
991 $this->assignment->get_instance()->cutoffdate) &&
992 $this->hasgrantextension) {
993 $urlparams = array('id' => $this->assignment->get_course_module()->id,
995 'action'=>'grantextension',
996 'sesskey'=>sesskey(),
997 'page'=>$this->currpage);
998 $url = new moodle_url('/mod/assign/view.php', $urlparams);
999 $description = get_string('grantextension', 'assign');
1000 $actions[$url->out(false)] = $description;
1003 if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
1004 $this->assignment->get_instance()->submissiondrafts) {
1005 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1007 'action'=>'reverttodraft',
1008 'sesskey'=>sesskey(),
1009 'page'=>$this->currpage);
1010 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1011 $description = get_string('reverttodraftshort', 'assign');
1012 $actions[$url->out(false)] = $description;
1015 $ismanual = $this->assignment->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
1016 $hassubmission = !empty($row->status);
1017 $notreopened = $hassubmission && $row->status != ASSIGN_SUBMISSION_STATUS_REOPENED;
1018 $isunlimited = $this->assignment->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS;
1019 $hasattempts = $isunlimited || $row->attemptnumber < $this->assignment->get_instance()->maxattempts - 1;
1021 if ($ismanual && $hassubmission && $notreopened && $hasattempts) {
1022 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1024 'action'=>'addattempt',
1025 'sesskey'=>sesskey(),
1026 'page'=>$this->currpage);
1027 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1028 $description = get_string('addattempt', 'assign');
1029 $actions[$url->out(false)] = $description;
1032 $edit .= $this->output->container_start(array('yui3-menu', 'actionmenu'), 'actionselect' . $row->id);
1033 $edit .= $this->output->container_start(array('yui3-menu-content'));
1034 $edit .= html_writer::start_tag('ul');
1035 $edit .= html_writer::start_tag('li', array('class'=>'menuicon'));
1037 $menuicon = $this->output->pix_icon('t/contextmenu', get_string('actions'));
1038 $edit .= $this->output->action_link('#menu' . $row->id, $menuicon, null, array('class'=>'yui3-menu-label'));
1039 $edit .= $this->output->container_start(array('yui3-menu', 'yui3-loading'), 'menu' . $row->id);
1040 $edit .= $this->output->container_start(array('yui3-menu-content'));
1041 $edit .= html_writer::start_tag('ul');
1043 foreach ($actions as $url => $description) {
1044 $edit .= html_writer::start_tag('li', array('class'=>'yui3-menuitem'));
1046 $edit .= $this->output->action_link($url, $description, null, array('class'=>'yui3-menuitem-content'));
1048 $edit .= html_writer::end_tag('li');
1050 $edit .= html_writer::end_tag('ul');
1051 $edit .= $this->output->container_end();
1052 $edit .= $this->output->container_end();
1053 $edit .= html_writer::end_tag('li');
1054 $edit .= html_writer::end_tag('ul');
1056 $edit .= $this->output->container_end();
1057 $edit .= $this->output->container_end();
1063 * Write the plugin summary with an optional link to view the full feedback/submission.
1065 * @param assign_plugin $plugin Submission plugin or feedback plugin
1066 * @param stdClass $item Submission or grade
1067 * @param string $returnaction The return action to pass to the
1068 * view_submission page (the current page)
1069 * @param string $returnparams The return params to pass to the view_submission
1070 * page (the current page)
1071 * @return string The summary with an optional link
1073 private function format_plugin_summary_with_link(assign_plugin $plugin,
1078 $showviewlink = false;
1080 $summary = $plugin->view_summary($item, $showviewlink);
1082 if ($showviewlink) {
1083 $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
1084 $icon = $this->output->pix_icon('t/preview', $viewstr);
1085 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1088 'plugin'=>$plugin->get_type(),
1089 'action'=>'viewplugin' . $plugin->get_subtype(),
1090 'returnaction'=>$returnaction,
1091 'returnparams'=>http_build_query($returnparams));
1092 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1093 $link = $this->output->action_link($url, $icon);
1094 $separator = $this->output->spacer(array(), true);
1097 return $link . $separator . $summary;
1102 * Format the submission and feedback columns.
1104 * @param string $colname The column name
1105 * @param stdClass $row The submission row
1106 * @return mixed string or NULL
1108 public function other_cols($colname, $row) {
1109 // For extra user fields the result is already in $row.
1110 if (empty($this->plugincache[$colname])) {
1111 return $row->$colname;
1114 // This must be a plugin field.
1115 $plugincache = $this->plugincache[$colname];
1117 $plugin = $plugincache[0];
1120 if (isset($plugincache[1])) {
1121 $field = $plugincache[1];
1124 if ($plugin->is_visible() && $plugin->is_enabled()) {
1125 if ($plugin->get_subtype() == 'assignsubmission') {
1126 if ($this->assignment->get_instance()->teamsubmission) {
1128 $submission = false;
1130 $this->get_group_and_submission($row->id, $group, $submission, -1);
1132 if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1133 // For a newly reopened submission - we want to show the previous submission in the table.
1134 $this->get_group_and_submission($row->id, $group, $submission, $submission->attemptnumber-1);
1136 if (isset($field)) {
1137 return $plugin->get_editor_text($field, $submission->id);
1139 return $this->format_plugin_summary_with_link($plugin,
1144 } else if ($row->submissionid) {
1145 if ($row->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1146 // For a newly reopened submission - we want to show the previous submission in the table.
1147 $submission = $this->assignment->get_user_submission($row->userid, false, $row->attemptnumber - 1);
1149 $submission = new stdClass();
1150 $submission->id = $row->submissionid;
1151 $submission->timecreated = $row->firstsubmission;
1152 $submission->timemodified = $row->timesubmitted;
1153 $submission->assignment = $this->assignment->get_instance()->id;
1154 $submission->userid = $row->userid;
1156 // Field is used for only for import/export and refers the the fieldname for the text editor.
1157 if (isset($field)) {
1158 return $plugin->get_editor_text($field, $submission->id);
1160 return $this->format_plugin_summary_with_link($plugin,
1167 if (isset($field)) {
1168 return $plugin->get_editor_text($field, $row->gradeid);
1171 if ($row->gradeid) {
1172 $grade = new stdClass();
1173 $grade->id = $row->gradeid;
1174 $grade->timecreated = $row->firstmarked;
1175 $grade->timemodified = $row->timemarked;
1176 $grade->assignment = $this->assignment->get_instance()->id;
1177 $grade->userid = $row->userid;
1178 $grade->grade = $row->grade;
1179 $grade->mailed = $row->mailed;
1181 if ($this->quickgrading && $plugin->supports_quickgrading()) {
1182 return $plugin->get_quickgrading_html($row->userid, $grade);
1183 } else if ($grade) {
1184 return $this->format_plugin_summary_with_link($plugin,
1195 * Using the current filtering and sorting - load all rows and return a single column from them.
1197 * @param string $columnname The name of the raw column data
1198 * @return array of data
1200 public function get_column_data($columnname) {
1202 $this->currpage = 0;
1203 $this->query_db($this->tablemaxrows);
1205 foreach ($this->rawdata as $row) {
1206 $result[] = $row->$columnname;
1212 * Return things to the renderer.
1214 * @return string the assignment name
1216 public function get_assignment_name() {
1217 return $this->assignment->get_instance()->name;
1221 * Return things to the renderer.
1223 * @return int the course module id
1225 public function get_course_module_id() {
1226 return $this->assignment->get_course_module()->id;
1230 * Return things to the renderer.
1232 * @return int the course id
1234 public function get_course_id() {
1235 return $this->assignment->get_course()->id;
1239 * Return things to the renderer.
1241 * @return stdClass The course context
1243 public function get_course_context() {
1244 return $this->assignment->get_course_context();
1248 * Return things to the renderer.
1250 * @return bool Does this assignment accept submissions
1252 public function submissions_enabled() {
1253 return $this->assignment->is_any_submission_plugin_enabled();
1257 * Return things to the renderer.
1259 * @return bool Can this user view all grades (the gradebook)
1261 public function can_view_all_grades() {
1262 $context = $this->assignment->get_course_context();
1263 return has_capability('gradereport/grader:view', $context) &&
1264 has_capability('moodle/grade:viewall', $context);
1268 * Override the table show_hide_link to not show for select column.
1270 * @param string $column the column name, index into various names.
1271 * @param int $index numerical index of the column.
1272 * @return string HTML fragment.
1274 protected function show_hide_link($column, $index) {
1276 return parent::show_hide_link($column, $index);