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 ($workflowfilter == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED) {
207 $where .= ' AND (uf.workflowstate = :workflowstate OR uf.workflowstate IS NULL OR '.
208 $DB->sql_isempty('assign_user_flags', 'workflowstate', true, true).')';
209 $params['workflowstate'] = $workflowfilter;
210 } else if (array_key_exists($workflowfilter, $workflowstates)) {
211 $where .= ' AND uf.workflowstate = :workflowstate';
212 $params['workflowstate'] = $workflowfilter;
217 $this->set_sql($fields, $from, $where, $params);
219 if ($downloadfilename) {
220 $this->is_downloading('csv', $downloadfilename);
227 if (!$this->is_downloading()) {
228 $columns[] = 'select';
229 $headers[] = get_string('select') .
230 '<div class="selectall"><label class="accesshide" for="selectall">' . get_string('selectall') . '</label>
231 <input type="checkbox" id="selectall" name="selectall" title="' . get_string('selectall') . '"/></div>';
235 if (!$this->assignment->is_blind_marking()) {
236 if (!$this->is_downloading()) {
237 $columns[] = 'picture';
238 $headers[] = get_string('pictureofuser');
240 $columns[] = 'recordid';
241 $headers[] = get_string('recordid', 'assign');
245 $columns[] = 'fullname';
246 $headers[] = get_string('fullname');
248 foreach ($extrauserfields as $extrafield) {
249 $columns[] = $extrafield;
250 $headers[] = get_user_field_name($extrafield);
254 $columns[] = 'recordid';
255 $headers[] = get_string('recordid', 'assign');
258 // Submission status.
259 if ($assignment->is_any_submission_plugin_enabled()) {
260 $columns[] = 'status';
261 $headers[] = get_string('status', 'assign');
262 } else if ($this->assignment->get_instance()->markingworkflow) {
263 $columns[] = 'workflowstatus';
264 $headers[] = get_string('status', 'assign');
267 // Team submission columns.
268 if ($assignment->get_instance()->teamsubmission) {
270 $headers[] = get_string('submissionteam', 'assign');
272 $columns[] = 'teamstatus';
273 $headers[] = get_string('teamsubmissionstatus', 'assign');
276 if ($this->assignment->get_instance()->markingallocation &&
277 has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
278 // Add a column for the allocated marker.
279 $columns[] = 'allocatedmarker';
280 $headers[] = get_string('marker', 'assign');
283 $columns[] = 'grade';
284 $headers[] = get_string('grade');
285 if ($this->is_downloading()) {
286 if ($this->assignment->get_instance()->grade >= 0) {
287 $columns[] = 'grademax';
288 $headers[] = get_string('maxgrade', 'assign');
290 // This is a custom scale.
291 $columns[] = 'scale';
292 $headers[] = get_string('scale', 'assign');
295 if ($this->assignment->get_instance()->markingworkflow) {
296 // Add a column for the marking workflow state.
297 $columns[] = 'workflowstate';
298 $headers[] = get_string('markingworkflowstate', 'assign');
300 // Add a column for the list of valid marking workflow states.
301 $columns[] = 'gradecanbechanged';
302 $headers[] = get_string('gradecanbechanged', 'assign');
304 if (!$this->is_downloading()) {
305 // We have to call this column userid so we can use userid as a default sortable column.
306 $columns[] = 'userid';
307 $headers[] = get_string('edit');
310 // Submission plugins.
311 if ($assignment->is_any_submission_plugin_enabled()) {
312 $columns[] = 'timesubmitted';
313 $headers[] = get_string('lastmodifiedsubmission', 'assign');
315 foreach ($this->assignment->get_submission_plugins() as $plugin) {
316 if ($this->is_downloading()) {
317 if ($plugin->is_visible() && $plugin->is_enabled()) {
318 foreach ($plugin->get_editor_fields() as $field => $description) {
319 $index = 'plugin' . count($this->plugincache);
320 $this->plugincache[$index] = array($plugin, $field);
322 $headers[] = $plugin->get_name();
326 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
327 $index = 'plugin' . count($this->plugincache);
328 $this->plugincache[$index] = array($plugin);
330 $headers[] = $plugin->get_name();
337 $columns[] = 'timemarked';
338 $headers[] = get_string('lastmodifiedgrade', 'assign');
341 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
342 if ($this->is_downloading()) {
343 if ($plugin->is_visible() && $plugin->is_enabled()) {
344 foreach ($plugin->get_editor_fields() as $field => $description) {
345 $index = 'plugin' . count($this->plugincache);
346 $this->plugincache[$index] = array($plugin, $field);
348 $headers[] = $description;
351 } else if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
352 $index = 'plugin' . count($this->plugincache);
353 $this->plugincache[$index] = array($plugin);
355 $headers[] = $plugin->get_name();
359 // Exclude 'Final grade' column in downloaded grading worksheets.
360 if (!$this->is_downloading()) {
362 $columns[] = 'finalgrade';
363 $headers[] = get_string('finalgrade', 'grades');
366 // Load the grading info for all users.
367 $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id,
370 $this->assignment->get_instance()->id,
372 $this->hasgrantextension = has_capability('mod/assign:grantextension',
373 $this->assignment->get_context());
375 if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
376 $columns[] = 'outcomes';
377 $headers[] = get_string('outcomes', 'grades');
381 $this->define_columns($columns);
382 $this->define_headers($headers);
383 foreach ($extrauserfields as $extrafield) {
384 $this->column_class($extrafield, $extrafield);
386 // We require at least one unique column for the sort.
387 $this->sortable(true, 'userid');
388 $this->no_sorting('recordid');
389 $this->no_sorting('finalgrade');
390 $this->no_sorting('userid');
391 $this->no_sorting('select');
392 $this->no_sorting('outcomes');
394 if ($assignment->get_instance()->teamsubmission) {
395 $this->no_sorting('team');
396 $this->no_sorting('teamstatus');
399 $plugincolumnindex = 0;
400 foreach ($this->assignment->get_submission_plugins() as $plugin) {
401 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
402 $submissionpluginindex = 'plugin' . $plugincolumnindex++;
403 $this->no_sorting($submissionpluginindex);
406 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
407 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
408 $feedbackpluginindex = 'plugin' . $plugincolumnindex++;
409 $this->no_sorting($feedbackpluginindex);
413 // When there is no data we still want the column headers printed in the csv file.
414 if ($this->is_downloading()) {
415 $this->start_output();
420 * Before adding each row to the table make sure rownum is incremented.
422 * @param array $row row of data from db used to make one row of the table.
423 * @return array one row for the table
425 public function format_row($row) {
426 if ($this->rownum < 0) {
427 $this->rownum = $this->currpage * $this->pagesize;
432 return parent::format_row($row);
436 * Add a column with an ID that uniquely identifies this user in this assignment.
440 public function col_recordid(stdClass $row) {
441 return get_string('hiddenuser', 'assign') .
442 $this->assignment->get_uniqueid_for_user($row->userid);
447 * Add the userid to the row class so it can be updated via ajax.
449 * @param stdClass $row The row of data
450 * @return string The row class
452 public function get_row_class($row) {
453 return 'user' . $row->userid;
457 * Return the number of rows to display on a single page.
459 * @return int The number of rows per page
461 public function get_rows_per_page() {
462 return $this->perpage;
466 * list current marking workflow state
468 * @param stdClass $row
471 public function col_workflowstatus(stdClass $row) {
474 $gradingdisabled = $this->assignment->grading_disabled($row->id);
475 if ($this->assignment->get_instance()->markingworkflow) {
476 // The function in the assignment keeps a static cache of this list of states.
477 $workflowstates = $this->assignment->get_marking_workflow_states_for_current_user();
478 $workflowstate = $row->workflowstate;
479 if (empty($workflowstate)) {
480 $workflowstate = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
482 if ($this->quickgrading && !$gradingdisabled) {
483 $notmarked = get_string('markingworkflowstatenotmarked', 'assign');
484 $name = 'quickgrade_' . $row->id . '_workflowstate';
485 $o .= html_writer::select($workflowstates, $name, $workflowstate, array('' => $notmarked));
486 // Check if this user is a marker that can't manage allocations and doesn't have the marker column added.
487 if ($this->assignment->get_instance()->markingallocation &&
488 !has_capability('mod/assign:manageallocations', $this->assignment->get_context())) {
490 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
491 $o .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$row->allocatedmarker));
494 $o .= $this->output->container(get_string('markingworkflowstate' . $workflowstate, 'assign'), $workflowstate);
501 * For download only - list current marking workflow state
503 * @param stdClass $row - The row of data
504 * @return string The current marking workflow state
506 public function col_workflowstate($row) {
507 $state = $row->workflowstate;
509 $state = ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
512 return get_string('markingworkflowstate' . $state, 'assign');
516 * list current marker
518 * @param stdClass $row - The row of data
519 * @return id the user->id of the marker.
521 public function col_allocatedmarker(stdClass $row) {
522 static $markers = null;
523 static $markerlist = array();
524 if ($markers === null) {
525 $markers = get_users_by_capability($this->assignment->get_context(), 'mod/assign:grade');
526 $markerlist[0] = get_string('choosemarker', 'assign');
527 foreach ($markers as $marker) {
528 $markerlist[$marker->id] = fullname($marker);
531 if (empty($markerlist)) {
532 // TODO: add some form of notification here that no markers are available.
535 if ($this->is_downloading()) {
536 return $markers[$row->allocatedmarker];
539 if ($this->quickgrading && has_capability('mod/assign:manageallocations', $this->assignment->get_context()) &&
540 (empty($row->workflowstate) ||
541 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_INMARKING ||
542 $row->workflowstate == ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED)) {
544 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
545 return html_writer::select($markerlist, $name, $row->allocatedmarker, false);
546 } else if (!empty($row->allocatedmarker)) {
548 if ($this->quickgrading) { // Add hidden field for quickgrading page.
549 $name = 'quickgrade_' . $row->id . '_allocatedmarker';
550 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$row->allocatedmarker));
552 $output .= $markerlist[$row->allocatedmarker];
557 * For download only - list all the valid options for this custom scale.
559 * @param stdClass $row - The row of data
560 * @return string A list of valid options for the current scale
562 public function col_scale($row) {
565 if (empty($this->scale)) {
566 $dbparams = array('id'=>-($this->assignment->get_instance()->grade));
567 $this->scale = $DB->get_record('scale', $dbparams);
570 if (!empty($this->scale->scale)) {
571 return implode("\n", explode(',', $this->scale->scale));
577 * Display a grade with scales etc.
579 * @param string $grade
580 * @param boolean $editable
581 * @param int $userid The user id of the user this grade belongs to
582 * @param int $modified Timestamp showing when the grade was last modified
583 * @return string The formatted grade
585 public function display_grade($grade, $editable, $userid, $modified) {
586 if ($this->is_downloading()) {
587 if ($this->assignment->get_instance()->grade >= 0) {
588 if ($grade == -1 || $grade === null) {
591 return format_float($grade, 2);
593 // This is a custom scale.
594 $scale = $this->assignment->display_grade($grade, false);
601 return $this->assignment->display_grade($grade, $editable, $userid, $modified);
605 * Get the team info for this user.
607 * @param stdClass $row
608 * @return string The team name
610 public function col_team(stdClass $row) {
613 $this->get_group_and_submission($row->id, $group, $submission, -1);
617 return get_string('defaultteam', 'assign');
621 * Use a static cache to try and reduce DB calls.
623 * @param int $userid The user id for this submission
624 * @param int $groupid The groupid (returned)
625 * @param mixed $submission The stdClass submission or false (returned)
626 * @param int $attemptnumber Return a specific attempt number (-1 for latest)
628 protected function get_group_and_submission($userid, &$group, &$submission, $attemptnumber) {
630 if (isset($this->submissiongroups[$userid])) {
631 $group = $this->submissiongroups[$userid];
633 $group = $this->assignment->get_submission_group($userid, false);
634 $this->submissiongroups[$userid] = $group;
639 $groupid = $group->id;
642 // Static cache is keyed by groupid and attemptnumber.
643 // We may need both the latest and previous attempt in the same page.
644 if (isset($this->groupsubmissions[$groupid . ':' . $attemptnumber])) {
645 $submission = $this->groupsubmissions[$groupid . ':' . $attemptnumber];
647 $submission = $this->assignment->get_group_submission($userid, $groupid, false, $attemptnumber);
648 $this->groupsubmissions[$groupid . ':' . $attemptnumber] = $submission;
654 * Get the team status for this user.
656 * @param stdClass $row
657 * @return string The team name
659 public function col_teamstatus(stdClass $row) {
662 $this->get_group_and_submission($row->id, $group, $submission, -1);
666 $status = $submission->status;
668 return get_string('submissionstatus_' . $status, 'assign');
673 * Format a list of outcomes.
675 * @param stdClass $row
678 public function col_outcomes(stdClass $row) {
680 foreach ($this->gradinginfo->outcomes as $index => $outcome) {
681 $options = make_grades_menu(-$outcome->scaleid);
683 $options[0] = get_string('nooutcome', 'grades');
684 if ($this->quickgrading && !($outcome->grades[$row->userid]->locked)) {
685 $select = '<select name="outcome_' . $index . '_' . $row->userid . '" class="quickgrade">';
686 foreach ($options as $optionindex => $optionvalue) {
688 if ($outcome->grades[$row->userid]->grade == $optionindex) {
689 $selected = 'selected="selected"';
691 $select .= '<option value="' . $optionindex . '"' . $selected . '>' . $optionvalue . '</option>';
693 $select .= '</select>';
694 $outcomes .= $this->output->container($outcome->name . ': ' . $select, 'outcome');
696 $name = $outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade];
697 $outcomes .= $this->output->container($name, 'outcome');
706 * Format a user picture for display.
708 * @param stdClass $row
711 public function col_picture(stdClass $row) {
713 return $this->output->user_picture($row);
719 * Format a user record for display (link to profile).
721 * @param stdClass $row
724 public function col_fullname($row) {
725 if (!$this->is_downloading()) {
726 $courseid = $this->assignment->get_course()->id;
727 $link= new moodle_url('/user/view.php', array('id' =>$row->id, 'course'=>$courseid));
728 return $this->output->action_link($link, fullname($row));
730 return fullname($row);
735 * Insert a checkbox for selecting the current row for batch operations.
737 * @param stdClass $row
740 public function col_select(stdClass $row) {
741 $selectcol = '<label class="accesshide" for="selectuser_' . $row->userid . '">';
742 $selectcol .= get_string('selectuser', 'assign', fullname($row));
743 $selectcol .= '</label>';
744 $selectcol .= '<input type="checkbox"
745 id="selectuser_' . $row->userid . '"
747 value="' . $row->userid . '"/>';
752 * Return a users grades from the listing of all grade data for this assignment.
755 * @return mixed stdClass or false
757 private function get_gradebook_data_for_user($userid) {
758 if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
759 return $this->gradinginfo->items[0]->grades[$userid];
765 * Format a column of data for display.
767 * @param stdClass $row
770 public function col_gradecanbechanged(stdClass $row) {
771 $gradingdisabled = $this->assignment->grading_disabled($row->id);
772 if ($gradingdisabled) {
773 return get_string('no');
775 return get_string('yes');
780 * Format a column of data for display
782 * @param stdClass $row
785 public function col_grademax(stdClass $row) {
786 return format_float($this->assignment->get_instance()->grade, 2);
790 * Format a column of data for display.
792 * @param stdClass $row
795 public function col_grade(stdClass $row) {
799 $separator = $this->output->spacer(array(), true);
801 $gradingdisabled = $this->assignment->grading_disabled($row->id);
803 if (!$this->is_downloading()) {
804 $name = fullname($row);
805 if ($this->assignment->is_blind_marking()) {
806 $name = get_string('hiddenuser', 'assign') .
807 $this->assignment->get_uniqueid_for_user($row->userid);
809 $icon = $this->output->pix_icon('gradefeedback',
810 get_string('gradeuser', 'assign', $name),
812 $urlparams = array('id' => $this->assignment->get_course_module()->id,
813 'rownum'=>$this->rownum,
815 $url = new moodle_url('/mod/assign/view.php', $urlparams);
816 $link = $this->output->action_link($url, $icon);
817 $grade .= $link . $separator;
820 $grade .= $this->display_grade($row->grade,
821 $this->quickgrading && !$gradingdisabled,
829 * Format a column of data for display.
831 * @param stdClass $row
834 public function col_finalgrade(stdClass $row) {
837 $grade = $this->get_gradebook_data_for_user($row->userid);
839 $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
846 * Format a column of data for display.
848 * @param stdClass $row
851 public function col_timemarked(stdClass $row) {
854 if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
855 $o = userdate($row->timemarked);
857 if ($row->timemarked && $this->is_downloading()) {
858 // Force it for downloads as it affects import.
859 $o = userdate($row->timemarked);
866 * Format a column of data for display.
868 * @param stdClass $row
871 public function col_timesubmitted(stdClass $row) {
874 if ($row->timesubmitted) {
875 $o = userdate($row->timesubmitted);
882 * Format a column of data for display
884 * @param stdClass $row
887 public function col_status(stdClass $row) {
890 $instance = $this->assignment->get_instance();
892 if ($this->assignment->is_any_submission_plugin_enabled()) {
894 $o .= $this->output->container(get_string('submissionstatus_' . $row->status, 'assign'),
895 array('class'=>'submissionstatus' .$row->status));
896 if ($instance->duedate &&
897 $row->timesubmitted > $instance->duedate) {
898 if (!$row->extensionduedate ||
899 $row->timesubmitted > $row->extensionduedate) {
900 $usertime = format_time($row->timesubmitted - $instance->duedate);
901 $latemessage = get_string('submittedlateshort',
904 $o .= $this->output->container($latemessage, 'latesubmission');
908 $lockedstr = get_string('submissionslockedshort', 'assign');
909 $o .= $this->output->container($lockedstr, 'lockedsubmission');
911 $o .= $this->col_workflowstatus($row);
912 if (!$row->timesubmitted) {
914 $due = $instance->duedate;
915 if ($row->extensionduedate) {
916 $due = $row->extensionduedate;
918 if ($due && ($now > $due)) {
919 $overduestr = get_string('overdue', 'assign', format_time($now - $due));
920 $o .= $this->output->container($overduestr, 'overduesubmission');
923 if ($row->extensionduedate) {
924 $userdate = userdate($row->extensionduedate);
925 $extensionstr = get_string('userextensiondate', 'assign', $userdate);
926 $o .= $this->output->container($extensionstr, 'extensiondate');
930 if ($this->is_downloading()) {
931 $o = strip_tags(str_replace('</div>', "\n", $o));
938 * Format a column of data for display.
940 * @param stdClass $row
943 public function col_userid(stdClass $row) {
948 $urlparams = array('id'=>$this->assignment->get_course_module()->id,
949 'rownum'=>$this->rownum,
951 $url = new moodle_url('/mod/assign/view.php', $urlparams);
954 $description = get_string('grade');
956 $description = get_string('updategrade', 'assign');
958 $actions[$url->out(false)] = $description;
960 // Hide for offline assignments.
961 if ($this->assignment->is_any_submission_plugin_enabled()) {
963 $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
964 !$this->assignment->get_instance()->submissiondrafts) {
967 $urlparams = array('id' => $this->assignment->get_course_module()->id,
970 'sesskey'=>sesskey(),
971 'page'=>$this->currpage);
972 $url = new moodle_url('/mod/assign/view.php', $urlparams);
974 $description = get_string('preventsubmissionsshort', 'assign');
975 $actions[$url->out(false)] = $description;
977 $urlparams = array('id' => $this->assignment->get_course_module()->id,
980 'sesskey'=>sesskey(),
981 'page'=>$this->currpage);
982 $url = new moodle_url('/mod/assign/view.php', $urlparams);
983 $description = get_string('allowsubmissionsshort', 'assign');
984 $actions[$url->out(false)] = $description;
988 if (($this->assignment->get_instance()->duedate ||
989 $this->assignment->get_instance()->cutoffdate) &&
990 $this->hasgrantextension) {
991 $urlparams = array('id' => $this->assignment->get_course_module()->id,
993 'action'=>'grantextension',
994 'sesskey'=>sesskey(),
995 'page'=>$this->currpage);
996 $url = new moodle_url('/mod/assign/view.php', $urlparams);
997 $description = get_string('grantextension', 'assign');
998 $actions[$url->out(false)] = $description;
1001 if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
1002 $this->assignment->get_instance()->submissiondrafts) {
1003 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1005 'action'=>'reverttodraft',
1006 'sesskey'=>sesskey(),
1007 'page'=>$this->currpage);
1008 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1009 $description = get_string('reverttodraftshort', 'assign');
1010 $actions[$url->out(false)] = $description;
1013 $ismanual = $this->assignment->get_instance()->attemptreopenmethod == ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
1014 $hassubmission = !empty($row->status);
1015 $notreopened = $hassubmission && $row->status != ASSIGN_SUBMISSION_STATUS_REOPENED;
1016 $isunlimited = $this->assignment->get_instance()->maxattempts == ASSIGN_UNLIMITED_ATTEMPTS;
1017 $hasattempts = $isunlimited || $row->attemptnumber < $this->assignment->get_instance()->maxattempts - 1;
1019 if ($ismanual && $hassubmission && $notreopened && $hasattempts) {
1020 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1022 'action'=>'addattempt',
1023 'sesskey'=>sesskey(),
1024 'page'=>$this->currpage);
1025 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1026 $description = get_string('addattempt', 'assign');
1027 $actions[$url->out(false)] = $description;
1030 $edit .= $this->output->container_start(array('yui3-menu', 'actionmenu'), 'actionselect' . $row->id);
1031 $edit .= $this->output->container_start(array('yui3-menu-content'));
1032 $edit .= html_writer::start_tag('ul');
1033 $edit .= html_writer::start_tag('li', array('class'=>'menuicon'));
1035 $menuicon = $this->output->pix_icon('t/contextmenu', get_string('actions'));
1036 $edit .= $this->output->action_link('#menu' . $row->id, $menuicon, null, array('class'=>'yui3-menu-label'));
1037 $edit .= $this->output->container_start(array('yui3-menu', 'yui3-loading'), 'menu' . $row->id);
1038 $edit .= $this->output->container_start(array('yui3-menu-content'));
1039 $edit .= html_writer::start_tag('ul');
1041 foreach ($actions as $url => $description) {
1042 $edit .= html_writer::start_tag('li', array('class'=>'yui3-menuitem'));
1044 $edit .= $this->output->action_link($url, $description, null, array('class'=>'yui3-menuitem-content'));
1046 $edit .= html_writer::end_tag('li');
1048 $edit .= html_writer::end_tag('ul');
1049 $edit .= $this->output->container_end();
1050 $edit .= $this->output->container_end();
1051 $edit .= html_writer::end_tag('li');
1052 $edit .= html_writer::end_tag('ul');
1054 $edit .= $this->output->container_end();
1055 $edit .= $this->output->container_end();
1061 * Write the plugin summary with an optional link to view the full feedback/submission.
1063 * @param assign_plugin $plugin Submission plugin or feedback plugin
1064 * @param stdClass $item Submission or grade
1065 * @param string $returnaction The return action to pass to the
1066 * view_submission page (the current page)
1067 * @param string $returnparams The return params to pass to the view_submission
1068 * page (the current page)
1069 * @return string The summary with an optional link
1071 private function format_plugin_summary_with_link(assign_plugin $plugin,
1076 $showviewlink = false;
1078 $summary = $plugin->view_summary($item, $showviewlink);
1080 if ($showviewlink) {
1081 $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
1082 $icon = $this->output->pix_icon('t/preview', $viewstr);
1083 $urlparams = array('id' => $this->assignment->get_course_module()->id,
1086 'plugin'=>$plugin->get_type(),
1087 'action'=>'viewplugin' . $plugin->get_subtype(),
1088 'returnaction'=>$returnaction,
1089 'returnparams'=>http_build_query($returnparams));
1090 $url = new moodle_url('/mod/assign/view.php', $urlparams);
1091 $link = $this->output->action_link($url, $icon);
1092 $separator = $this->output->spacer(array(), true);
1095 return $link . $separator . $summary;
1100 * Format the submission and feedback columns.
1102 * @param string $colname The column name
1103 * @param stdClass $row The submission row
1104 * @return mixed string or NULL
1106 public function other_cols($colname, $row) {
1107 // For extra user fields the result is already in $row.
1108 if (empty($this->plugincache[$colname])) {
1109 return $row->$colname;
1112 // This must be a plugin field.
1113 $plugincache = $this->plugincache[$colname];
1115 $plugin = $plugincache[0];
1118 if (isset($plugincache[1])) {
1119 $field = $plugincache[1];
1122 if ($plugin->is_visible() && $plugin->is_enabled()) {
1123 if ($plugin->get_subtype() == 'assignsubmission') {
1124 if ($this->assignment->get_instance()->teamsubmission) {
1126 $submission = false;
1128 $this->get_group_and_submission($row->id, $group, $submission, -1);
1130 if ($submission->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1131 // For a newly reopened submission - we want to show the previous submission in the table.
1132 $this->get_group_and_submission($row->id, $group, $submission, $submission->attemptnumber-1);
1134 if (isset($field)) {
1135 return $plugin->get_editor_text($field, $submission->id);
1137 return $this->format_plugin_summary_with_link($plugin,
1142 } else if ($row->submissionid) {
1143 if ($row->status == ASSIGN_SUBMISSION_STATUS_REOPENED) {
1144 // For a newly reopened submission - we want to show the previous submission in the table.
1145 $submission = $this->assignment->get_user_submission($row->userid, false, $row->attemptnumber - 1);
1147 $submission = new stdClass();
1148 $submission->id = $row->submissionid;
1149 $submission->timecreated = $row->firstsubmission;
1150 $submission->timemodified = $row->timesubmitted;
1151 $submission->assignment = $this->assignment->get_instance()->id;
1152 $submission->userid = $row->userid;
1154 // Field is used for only for import/export and refers the the fieldname for the text editor.
1155 if (isset($field)) {
1156 return $plugin->get_editor_text($field, $submission->id);
1158 return $this->format_plugin_summary_with_link($plugin,
1165 if (isset($field)) {
1166 return $plugin->get_editor_text($field, $row->gradeid);
1169 if ($row->gradeid) {
1170 $grade = new stdClass();
1171 $grade->id = $row->gradeid;
1172 $grade->timecreated = $row->firstmarked;
1173 $grade->timemodified = $row->timemarked;
1174 $grade->assignment = $this->assignment->get_instance()->id;
1175 $grade->userid = $row->userid;
1176 $grade->grade = $row->grade;
1177 $grade->mailed = $row->mailed;
1179 if ($this->quickgrading && $plugin->supports_quickgrading()) {
1180 return $plugin->get_quickgrading_html($row->userid, $grade);
1181 } else if ($grade) {
1182 return $this->format_plugin_summary_with_link($plugin,
1193 * Using the current filtering and sorting - load all rows and return a single column from them.
1195 * @param string $columnname The name of the raw column data
1196 * @return array of data
1198 public function get_column_data($columnname) {
1200 $this->currpage = 0;
1201 $this->query_db($this->tablemaxrows);
1203 foreach ($this->rawdata as $row) {
1204 $result[] = $row->$columnname;
1210 * Return things to the renderer.
1212 * @return string the assignment name
1214 public function get_assignment_name() {
1215 return $this->assignment->get_instance()->name;
1219 * Return things to the renderer.
1221 * @return int the course module id
1223 public function get_course_module_id() {
1224 return $this->assignment->get_course_module()->id;
1228 * Return things to the renderer.
1230 * @return int the course id
1232 public function get_course_id() {
1233 return $this->assignment->get_course()->id;
1237 * Return things to the renderer.
1239 * @return stdClass The course context
1241 public function get_course_context() {
1242 return $this->assignment->get_course_context();
1246 * Return things to the renderer.
1248 * @return bool Does this assignment accept submissions
1250 public function submissions_enabled() {
1251 return $this->assignment->is_any_submission_plugin_enabled();
1255 * Return things to the renderer.
1257 * @return bool Can this user view all grades (the gradebook)
1259 public function can_view_all_grades() {
1260 $context = $this->assignment->get_course_context();
1261 return has_capability('gradereport/grader:view', $context) &&
1262 has_capability('moodle/grade:viewall', $context);
1266 * Override the table show_hide_link to not show for select column.
1268 * @param string $column the column name, index into various names.
1269 * @param int $index numerical index of the column.
1270 * @return string HTML fragment.
1272 protected function show_hide_link($column, $index) {
1274 return parent::show_hide_link($column, $index);