MDL-37883 Assignment Grade import/export. Additional fix to prevent different grade...
[moodle.git] / mod / assign / gradingtable.php
CommitLineData
bbd0e548
DW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This file contains the definition for the grading table which subclassses easy_table
19 *
20 * @package mod_assign
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27require_once($CFG->libdir.'/tablelib.php');
28require_once($CFG->libdir.'/gradelib.php');
29require_once($CFG->dirroot.'/mod/assign/locallib.php');
30
31/**
32 * Extends table_sql to provide a table of assignment submissions
33 *
34 * @package mod_assign
35 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class assign_grading_table extends table_sql implements renderable {
39 /** @var assign $assignment */
40 private $assignment = null;
41 /** @var int $perpage */
42 private $perpage = 10;
43 /** @var int $rownum (global index of current row in table) */
44 private $rownum = -1;
45 /** @var renderer_base for getting output */
46 private $output = null;
47 /** @var stdClass gradinginfo */
48 private $gradinginfo = null;
49 /** @var int $tablemaxrows */
50 private $tablemaxrows = 10000;
bf78ebd6
DW
51 /** @var boolean $quickgrading */
52 private $quickgrading = false;
9e795179
DW
53 /** @var boolean $hasgrantextension - Only do the capability check once for the entire table */
54 private $hasgrantextension = false;
cd01491c
DW
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();
df47b77f
DW
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;
bbd0e548
DW
65
66 /**
67 * overridden constructor keeps a reference to the assignment class that is displaying this table
68 *
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
bf78ebd6 73 * @param bool $quickgrading Is this table wrapped in a quickgrading form?
bbd0e548 74 */
e5403f8c
DW
75 public function __construct(assign $assignment,
76 $perpage,
77 $filter,
78 $rowoffset,
79 $quickgrading,
80 $downloadfilename = null) {
a3dbdc2c 81 global $CFG, $PAGE, $DB;
bbd0e548
DW
82 parent::__construct('mod_assign_grading');
83 $this->assignment = $assignment;
df47b77f
DW
84
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();
90 }
91 $this->plugingradingbatchoperations[$plugin->get_type()][$action] = $description;
92 }
93 }
94 }
bbd0e548 95 $this->perpage = $perpage;
bf78ebd6 96 $this->quickgrading = $quickgrading;
bbd0e548
DW
97 $this->output = $PAGE->get_renderer('mod_assign');
98
e5403f8c
DW
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);
bbd0e548 102
e5403f8c 103 // Do some business - then set the sql.
bbd0e548
DW
104 $currentgroup = groups_get_activity_group($assignment->get_course_module(), true);
105
106 if ($rowoffset) {
107 $this->rownum = $rowoffset - 1;
108 }
109
110 $users = array_keys( $assignment->list_participants($currentgroup, true));
111 if (count($users) == 0) {
e5403f8c 112 // Insert a record that will never match to the sql is still valid.
bbd0e548
DW
113 $users[] = -1;
114 }
115
116 $params = array();
a3dbdc2c
DW
117 $params['assignmentid1'] = (int)$this->assignment->get_instance()->id;
118 $params['assignmentid2'] = (int)$this->assignment->get_instance()->id;
bbd0e548 119
9e795179
DW
120 $fields = user_picture::fields('u') . ', ';
121 $fields .= 'u.id as userid, ';
122 $fields .= 's.status as status, ';
123 $fields .= 's.id as submissionid, ';
124 $fields .= 's.timecreated as firstsubmission, ';
125 $fields .= 's.timemodified as timesubmitted, ';
126 $fields .= 'g.id as gradeid, ';
127 $fields .= 'g.grade as grade, ';
128 $fields .= 'g.timemodified as timemarked, ';
129 $fields .= 'g.timecreated as firstmarked, ';
130 $fields .= 'g.mailed as mailed, ';
131 $fields .= 'g.locked as locked, ';
132 $fields .= 'g.extensionduedate as extensionduedate';
a3dbdc2c
DW
133 $from = '{user} u LEFT JOIN {assign_submission} s ON u.id = s.userid AND s.assignment = :assignmentid1' .
134 ' LEFT JOIN {assign_grades} g ON u.id = g.userid AND g.assignment = :assignmentid2';
bbd0e548
DW
135
136 $userparams = array();
a3dbdc2c
DW
137 $userindex = 0;
138
139 list($userwhere, $userparams) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
140 $where = 'u.id ' . $userwhere;
141 $params = array_merge($params, $userparams);
9682626e 142
93d1de6d
DW
143 // The filters do not make sense when there are no submissions, so do not apply them.
144 if ($this->assignment->is_any_submission_plugin_enabled()) {
145 if ($filter == ASSIGN_FILTER_SUBMITTED) {
146 $where .= ' AND s.timecreated > 0 ';
147 }
148 if ($filter == ASSIGN_FILTER_REQUIRE_GRADING) {
149 $where .= ' AND (s.timemodified IS NOT NULL AND
150 s.status = :submitted AND
151 (s.timemodified > g.timemodified OR g.timemodified IS NULL))';
152 $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
153 }
154 if (strpos($filter, ASSIGN_FILTER_SINGLE_USER) === 0) {
155 $userfilter = (int) array_pop(explode('=', $filter));
156 $where .= ' AND (u.id = :userid)';
157 $params['userid'] = $userfilter;
158 }
bbd0e548
DW
159 }
160 $this->set_sql($fields, $from, $where, $params);
161
df47b77f
DW
162 if ($downloadfilename) {
163 $this->is_downloading('csv', $downloadfilename);
164 }
165
bbd0e548
DW
166 $columns = array();
167 $headers = array();
168
df47b77f 169 // Select.
bbd0e548 170 if (!$this->is_downloading()) {
df47b77f
DW
171 $columns[] = 'select';
172 $headers[] = get_string('select') .
7400be1b
RT
173 '<div class="selectall"><label class="accesshide" for="selectall">' . get_string('selectall') . '</label>
174 <input type="checkbox" id="selectall" name="selectall" title="' . get_string('selectall') . '"/></div>';
bbd0e548
DW
175 }
176
b473171a
DW
177 // User picture.
178 if (!$this->assignment->is_blind_marking()) {
df47b77f
DW
179 if (!$this->is_downloading()) {
180 $columns[] = 'picture';
181 $headers[] = get_string('pictureofuser');
182 } else {
183 $columns[] = 'recordid';
184 $headers[] = get_string('recordid', 'assign');
185 }
b473171a
DW
186
187 // Fullname.
188 $columns[] = 'fullname';
189 $headers[] = get_string('fullname');
190 } else {
191 // Record ID.
192 $columns[] = 'recordid';
193 $headers[] = get_string('recordid', 'assign');
194 }
bbd0e548 195
e5403f8c 196 // Submission status.
bbd0e548
DW
197 if ($assignment->is_any_submission_plugin_enabled()) {
198 $columns[] = 'status';
199 $headers[] = get_string('status');
200 }
201
e5403f8c 202 // Team submission columns.
12a1a0da
DW
203 if ($assignment->get_instance()->teamsubmission) {
204 $columns[] = 'team';
205 $headers[] = get_string('submissionteam', 'assign');
206
207 $columns[] = 'teamstatus';
208 $headers[] = get_string('teamsubmissionstatus', 'assign');
209 }
bbd0e548 210
e5403f8c 211 // Grade.
bbd0e548
DW
212 $columns[] = 'grade';
213 $headers[] = get_string('grade');
df47b77f
DW
214 if ($this->is_downloading()) {
215 if ($this->assignment->get_instance()->grade >= 0) {
216 $columns[] = 'grademax';
217 $headers[] = get_string('maxgrade', 'assign');
218 } else {
219 // This is a custom scale.
220 $columns[] = 'scale';
221 $headers[] = get_string('scale', 'assign');
222 }
223 }
2bda0460
NK
224 if (!$this->is_downloading()) {
225 // We have to call this column userid so we can use userid as a default sortable column.
226 $columns[] = 'userid';
227 $headers[] = get_string('edit');
228 }
bbd0e548 229
e5403f8c 230 // Submission plugins.
bbd0e548
DW
231 if ($assignment->is_any_submission_plugin_enabled()) {
232 $columns[] = 'timesubmitted';
233 $headers[] = get_string('lastmodifiedsubmission', 'assign');
234
235 foreach ($this->assignment->get_submission_plugins() as $plugin) {
df47b77f
DW
236 if ($this->is_downloading()) {
237 if ($plugin->is_visible() && $plugin->is_enabled()) {
238 foreach ($plugin->get_editor_fields() as $field => $description) {
239 $index = 'plugin' . count($this->plugincache);
240 $this->plugincache[$index] = array($plugin, $field);
241 $columns[] = $index;
242 $headers[] = $plugin->get_name();
243 }
244 }
245 } else {
246 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
247 $index = 'plugin' . count($this->plugincache);
248 $this->plugincache[$index] = array($plugin);
249 $columns[] = $index;
250 $headers[] = $plugin->get_name();
251 }
bbd0e548
DW
252 }
253 }
254 }
255
e5403f8c 256 // Time marked.
bbd0e548
DW
257 $columns[] = 'timemarked';
258 $headers[] = get_string('lastmodifiedgrade', 'assign');
259
e5403f8c 260 // Feedback plugins.
bbd0e548 261 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
df47b77f
DW
262 if ($this->is_downloading()) {
263 if ($plugin->is_visible() && $plugin->is_enabled()) {
264 foreach ($plugin->get_editor_fields() as $field => $description) {
265 $index = 'plugin' . count($this->plugincache);
266 $this->plugincache[$index] = array($plugin, $field);
267 $columns[] = $index;
268 $headers[] = $description;
269 }
270 }
271 } else if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
272 $index = 'plugin' . count($this->plugincache);
273 $this->plugincache[$index] = array($plugin);
274 $columns[] = $index;
bbd0e548
DW
275 $headers[] = $plugin->get_name();
276 }
277 }
278
9f484b98
RW
279 // Exclude 'Final grade' column in downloaded grading worksheets.
280 if (!$this->is_downloading()) {
281 // Final grade.
282 $columns[] = 'finalgrade';
283 $headers[] = get_string('finalgrade', 'grades');
284 }
bbd0e548 285
e5403f8c
DW
286 // Load the grading info for all users.
287 $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id,
288 'mod',
289 'assign',
290 $this->assignment->get_instance()->id,
291 $users);
292 $this->hasgrantextension = has_capability('mod/assign:grantextension',
293 $this->assignment->get_context());
a769a001
DW
294
295 if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
296 $columns[] = 'outcomes';
297 $headers[] = get_string('outcomes', 'grades');
298 }
bbd0e548 299
e5403f8c 300 // Set the columns.
bbd0e548
DW
301 $this->define_columns($columns);
302 $this->define_headers($headers);
f9e62127
DW
303 // We require at least one unique column for the sort.
304 $this->sortable(true, 'userid');
305 $this->no_sorting('recordid');
bbd0e548 306 $this->no_sorting('finalgrade');
f9e62127 307 $this->no_sorting('userid');
bbd0e548 308 $this->no_sorting('select');
a769a001
DW
309 $this->no_sorting('outcomes');
310
12a1a0da
DW
311 if ($assignment->get_instance()->teamsubmission) {
312 $this->no_sorting('team');
313 $this->no_sorting('teamstatus');
314 }
315
f9e62127 316 $plugincolumnindex = 0;
bbd0e548 317 foreach ($this->assignment->get_submission_plugins() as $plugin) {
df47b77f 318 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
f9e62127
DW
319 $submissionpluginindex = 'plugin' . $plugincolumnindex++;
320 $this->no_sorting($submissionpluginindex);
bbd0e548
DW
321 }
322 }
323 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
df47b77f 324 if ($plugin->is_visible() && $plugin->is_enabled() && $plugin->has_user_summary()) {
f9e62127
DW
325 $feedbackpluginindex = 'plugin' . $plugincolumnindex++;
326 $this->no_sorting($feedbackpluginindex);
bbd0e548
DW
327 }
328 }
329
5fc59630
RW
330 // When there is no data we still want the column headers printed in the csv file.
331 if ($this->is_downloading()) {
332 $this->start_output();
333 }
bbd0e548
DW
334 }
335
61cf3253 336 /**
e5403f8c 337 * Before adding each row to the table make sure rownum is incremented.
61cf3253
DW
338 *
339 * @param array $row row of data from db used to make one row of the table.
340 * @return array one row for the table
341 */
e5403f8c 342 public function format_row($row) {
61cf3253
DW
343 if ($this->rownum < 0) {
344 $this->rownum = $this->currpage * $this->pagesize;
345 } else {
346 $this->rownum += 1;
347 }
348
349 return parent::format_row($row);
350 }
351
b473171a 352 /**
e5403f8c 353 * Add a column with an ID that uniquely identifies this user in this assignment.
b473171a
DW
354 *
355 * @return string
356 */
e5403f8c
DW
357 public function col_recordid(stdClass $row) {
358 return get_string('hiddenuser', 'assign') .
359 $this->assignment->get_uniqueid_for_user($row->userid);
360 }
b473171a
DW
361
362
bbd0e548 363 /**
e5403f8c 364 * Add the userid to the row class so it can be updated via ajax.
bbd0e548
DW
365 *
366 * @param stdClass $row The row of data
367 * @return string The row class
368 */
e5403f8c 369 public function get_row_class($row) {
bbd0e548
DW
370 return 'user' . $row->userid;
371 }
372
373 /**
e5403f8c 374 * Return the number of rows to display on a single page.
bbd0e548
DW
375 *
376 * @return int The number of rows per page
377 */
e5403f8c 378 public function get_rows_per_page() {
bbd0e548
DW
379 return $this->perpage;
380 }
381
df47b77f
DW
382 /**
383 * For download only - list all the valid options for this custom scale.
384 *
385 * @param stdClass $row - The row of data
386 * @return string A list of valid options for the current scale
387 */
7a2b911c 388 public function col_scale($row) {
df47b77f
DW
389 global $DB;
390
391 if (empty($this->scale)) {
e5403f8c
DW
392 $dbparams = array('id'=>-($this->assignment->get_instance()->grade));
393 $this->scale = $DB->get_record('scale', $dbparams);
df47b77f
DW
394 }
395
396 if (!empty($this->scale->scale)) {
397 return implode("\n", explode(',', $this->scale->scale));
398 }
399 return '';
400 }
401
bbd0e548
DW
402 /**
403 * Display a grade with scales etc.
404 *
405 * @param string $grade
bf78ebd6 406 * @param boolean $editable
2a4fbc32
SH
407 * @param int $userid The user id of the user this grade belongs to
408 * @param int $modified Timestamp showing when the grade was last modified
bbd0e548
DW
409 * @return string The formatted grade
410 */
e5403f8c 411 public function display_grade($grade, $editable, $userid, $modified) {
bbd0e548 412 if ($this->is_downloading()) {
df47b77f
DW
413 if ($this->assignment->get_instance()->grade >= 0) {
414 if ($grade == -1 || $grade === null) {
415 return '';
416 }
7688a319 417 return format_float($grade, 2);
df47b77f
DW
418 } else {
419 // This is a custom scale.
420 $scale = $this->assignment->display_grade($grade, false);
421 if ($scale == '-') {
422 $scale = '';
423 }
424 return $scale;
425 }
bbd0e548 426 }
df47b77f 427 return $this->assignment->display_grade($grade, $editable, $userid, $modified);
bbd0e548
DW
428 }
429
12a1a0da 430 /**
e5403f8c 431 * Get the team info for this user.
12a1a0da
DW
432 *
433 * @param stdClass $row
434 * @return string The team name
435 */
e5403f8c 436 public function col_team(stdClass $row) {
cd01491c
DW
437 $submission = false;
438 $group = false;
439 $this->get_group_and_submission($row->id, $group, $submission);
12a1a0da
DW
440 if ($group) {
441 return $group->name;
442 }
443 return get_string('defaultteam', 'assign');
444 }
445
446 /**
cd01491c
DW
447 * Use a static cache to try and reduce DB calls.
448 *
449 * @param int $userid The user id for this submission
450 * @param int $groupid The groupid (returned)
451 * @param mixed $submission The stdClass submission or false (returned)
452 */
e5403f8c 453 public function get_group_and_submission($userid, &$group, &$submission) {
cd01491c
DW
454 $group = false;
455 if (isset($this->submissiongroups[$userid])) {
456 $group = $this->submissiongroups[$userid];
457 } else {
458 $group = $this->assignment->get_submission_group($userid, false);
459 $this->submissiongroups[$userid] = $group;
460 }
461
462 $groupid = 0;
463 if ($group) {
464 $groupid = $group->id;
465 }
466
467 if (isset($this->groupsubmissions[$groupid])) {
468 $submission = $this->groupsubmissions[$groupid];
469 } else {
470 $submission = $this->assignment->get_group_submission($userid, $groupid, false);
471 $this->groupsubmissions[$groupid] = $submission;
472 }
473 }
474
475
476 /**
e5403f8c 477 * Get the team status for this user.
12a1a0da
DW
478 *
479 * @param stdClass $row
480 * @return string The team name
481 */
e5403f8c 482 public function col_teamstatus(stdClass $row) {
cd01491c
DW
483 $submission = false;
484 $group = false;
485 $this->get_group_and_submission($row->id, $group, $submission);
486
12a1a0da
DW
487 $status = '';
488 if ($submission) {
489 $status = $submission->status;
490 }
491 return get_string('submissionstatus_' . $status, 'assign');
492 }
493
494
a769a001 495 /**
e5403f8c 496 * Format a list of outcomes.
a769a001
DW
497 *
498 * @param stdClass $row
499 * @return string
500 */
e5403f8c 501 public function col_outcomes(stdClass $row) {
a769a001 502 $outcomes = '';
e5403f8c 503 foreach ($this->gradinginfo->outcomes as $index => $outcome) {
a769a001
DW
504 $options = make_grades_menu(-$outcome->scaleid);
505
506 $options[0] = get_string('nooutcome', 'grades');
2c6a3dbf 507 if ($this->quickgrading && !($outcome->grades[$row->userid]->locked)) {
bf78ebd6
DW
508 $select = '<select name="outcome_' . $index . '_' . $row->userid . '" class="quickgrade">';
509 foreach ($options as $optionindex => $optionvalue) {
510 $selected = '';
511 if ($outcome->grades[$row->userid]->grade == $optionindex) {
512 $selected = 'selected="selected"';
513 }
514 $select .= '<option value="' . $optionindex . '"' . $selected . '>' . $optionvalue . '</option>';
515 }
516 $select .= '</select>';
517 $outcomes .= $this->output->container($outcome->name . ': ' . $select, 'outcome');
518 } else {
e5403f8c
DW
519 $name = $outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade];
520 $outcomes .= $this->output->container($name, 'outcome');
bf78ebd6 521 }
a769a001
DW
522 }
523
524 return $outcomes;
525 }
526
527
bbd0e548 528 /**
e5403f8c 529 * Format a user picture for display.
bbd0e548
DW
530 *
531 * @param stdClass $row
532 * @return string
533 */
e5403f8c 534 public function col_picture(stdClass $row) {
bbd0e548
DW
535 if ($row->picture) {
536 return $this->output->user_picture($row);
537 }
538 return '';
539 }
540
541 /**
e5403f8c 542 * Format a user record for display (link to profile).
bbd0e548
DW
543 *
544 * @param stdClass $row
545 * @return string
546 */
e5403f8c 547 public function col_fullname($row) {
df47b77f
DW
548 if (!$this->is_downloading()) {
549 $courseid = $this->assignment->get_course()->id;
550 $link= new moodle_url('/user/view.php', array('id' =>$row->id, 'course'=>$courseid));
551 return $this->output->action_link($link, fullname($row));
552 } else {
553 return fullname($row);
554 }
bbd0e548
DW
555 }
556
557 /**
e5403f8c 558 * Insert a checkbox for selecting the current row for batch operations.
bbd0e548
DW
559 *
560 * @param stdClass $row
561 * @return string
562 */
e5403f8c
DW
563 public function col_select(stdClass $row) {
564 $selectcol = '<label class="accesshide" for="selectuser_' . $row->userid . '">';
565 $selectcol .= get_string('selectuser', 'assign', fullname($row));
566 $selectcol .= '</label>';
567 $selectcol .= '<input type="checkbox"
568 id="selectuser_' . $row->userid . '"
569 name="selectedusers"
570 value="' . $row->userid . '"/>';
571 return $selectcol;
bbd0e548
DW
572 }
573
574 /**
e5403f8c 575 * Return a users grades from the listing of all grade data for this assignment.
bbd0e548
DW
576 *
577 * @param int $userid
578 * @return mixed stdClass or false
579 */
580 private function get_gradebook_data_for_user($userid) {
581 if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
582 return $this->gradinginfo->items[0]->grades[$userid];
583 }
584 return false;
585 }
586
df47b77f 587 /**
e5403f8c 588 * Format a column of data for display.
df47b77f
DW
589 *
590 * @param stdClass $row
591 * @return string
592 */
7a2b911c
DW
593 public function col_grademax(stdClass $row) {
594 return format_float($this->assignment->get_instance()->grade, 2);
df47b77f
DW
595 }
596
bbd0e548 597 /**
e5403f8c 598 * Format a column of data for display.
bbd0e548
DW
599 *
600 * @param stdClass $row
601 * @return string
602 */
e5403f8c 603 public function col_grade(stdClass $row) {
bbd0e548
DW
604 $o = '';
605
606 $link = '';
607 $separator = '';
608 $grade = '';
df47b77f 609 $gradingdisabled = $this->assignment->grading_disabled($row->id);
bbd0e548
DW
610
611 if (!$this->is_downloading()) {
37eb2caa
DW
612 $name = fullname($row);
613 if ($this->assignment->is_blind_marking()) {
e5403f8c
DW
614 $name = get_string('hiddenuser', 'assign') .
615 $this->assignment->get_uniqueid_for_user($row->userid);
37eb2caa 616 }
e5403f8c
DW
617 $icon = $this->output->pix_icon('gradefeedback',
618 get_string('gradeuser', 'assign', $name),
619 'mod_assign');
620 $urlparams = array('id' => $this->assignment->get_course_module()->id,
621 'rownum'=>$this->rownum,
622 'action'=>'grade');
623 $url = new moodle_url('/mod/assign/view.php', $urlparams);
bbd0e548
DW
624 $link = $this->output->action_link($url, $icon);
625 $separator = $this->output->spacer(array(), true);
626 }
e5403f8c
DW
627 $grade = $this->display_grade($row->grade,
628 $this->quickgrading && !$gradingdisabled,
629 $row->userid,
630 $row->timemarked);
bbd0e548 631
bbd0e548
DW
632 return $link . $separator . $grade;
633 }
634
635 /**
e5403f8c 636 * Format a column of data for display.
bbd0e548
DW
637 *
638 * @param stdClass $row
639 * @return string
640 */
e5403f8c 641 public function col_finalgrade(stdClass $row) {
bbd0e548
DW
642 $o = '';
643
644 $grade = $this->get_gradebook_data_for_user($row->userid);
645 if ($grade) {
bf78ebd6 646 $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
bbd0e548
DW
647 }
648
649 return $o;
650 }
651
652 /**
e5403f8c 653 * Format a column of data for display.
bbd0e548
DW
654 *
655 * @param stdClass $row
656 * @return string
657 */
e5403f8c 658 public function col_timemarked(stdClass $row) {
bbd0e548
DW
659 $o = '-';
660
e5403f8c 661 if ($row->timemarked && $row->grade !== null && $row->grade >= 0) {
bbd0e548
DW
662 $o = userdate($row->timemarked);
663 }
7688a319
DW
664 if ($row->timemarked && $this->is_downloading()) {
665 // Force it for downloads as it affects import.
666 $o = userdate($row->timemarked);
667 }
bbd0e548
DW
668
669 return $o;
670 }
671
672 /**
e5403f8c 673 * Format a column of data for display.
bbd0e548
DW
674 *
675 * @param stdClass $row
676 * @return string
677 */
e5403f8c 678 public function col_timesubmitted(stdClass $row) {
bbd0e548
DW
679 $o = '-';
680
681 if ($row->timesubmitted) {
682 $o = userdate($row->timesubmitted);
683 }
684
685 return $o;
686 }
687
688 /**
689 * Format a column of data for display
690 *
691 * @param stdClass $row
692 * @return string
693 */
e5403f8c 694 public function col_status(stdClass $row) {
bbd0e548
DW
695 $o = '';
696
e5403f8c
DW
697 $instance = $this->assignment->get_instance();
698
bbd0e548
DW
699 if ($this->assignment->is_any_submission_plugin_enabled()) {
700
9e795179
DW
701 $o .= $this->output->container(get_string('submissionstatus_' . $row->status, 'assign'),
702 array('class'=>'submissionstatus' .$row->status));
e5403f8c
DW
703 if ($instance->duedate &&
704 $row->timesubmitted > $instance->duedate) {
705 if (!$row->extensionduedate ||
706 $row->timesubmitted > $row->extensionduedate) {
707 $usertime = format_time($row->timesubmitted - $instance->duedate);
708 $latemessage = get_string('submittedlateshort',
709 'assign',
710 $usertime);
9e795179
DW
711 $o .= $this->output->container($latemessage, 'latesubmission');
712 }
bbd0e548
DW
713 }
714 if ($row->locked) {
e5403f8c
DW
715 $lockedstr = get_string('submissionslockedshort', 'assign');
716 $o .= $this->output->container($lockedstr, 'lockedsubmission');
bbd0e548 717 }
e5403f8c 718 if ($row->grade !== null && $row->grade >= 0) {
bbd0e548
DW
719 $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
720 }
9e795179
DW
721 if (!$row->timesubmitted) {
722 $now = time();
e5403f8c 723 $due = $instance->duedate;
9e795179
DW
724 if ($row->extensionduedate) {
725 $due = $row->extensionduedate;
726 }
727 if ($due && ($now > $due)) {
e5403f8c
DW
728 $overduestr = get_string('overdue', 'assign', format_time($now - $due));
729 $o .= $this->output->container($overduestr, 'overduesubmission');
9e795179
DW
730 }
731 }
732 if ($row->extensionduedate) {
e5403f8c
DW
733 $userdate = userdate($row->extensionduedate);
734 $extensionstr = get_string('userextensiondate', 'assign', $userdate);
735 $o .= $this->output->container($extensionstr, 'extensiondate');
9e795179 736 }
bbd0e548
DW
737 }
738
df47b77f
DW
739 if ($this->is_downloading()) {
740 $o = strip_tags(str_replace('</div>', "\n", $o));
741 }
742
bbd0e548
DW
743 return $o;
744 }
745
746 /**
e5403f8c 747 * Format a column of data for display.
bbd0e548
DW
748 *
749 * @param stdClass $row
750 * @return string
751 */
e5403f8c 752 public function col_userid(stdClass $row) {
bbd0e548 753 $edit = '';
bbd0e548
DW
754
755 $actions = array();
756
e5403f8c
DW
757 $urlparams = array('id'=>$this->assignment->get_course_module()->id,
758 'rownum'=>$this->rownum,
759 'action'=>'grade');
760 $url = new moodle_url('/mod/assign/view.php', $urlparams);
761
bbd0e548 762 if (!$row->grade) {
e5403f8c
DW
763 $description = get_string('grade');
764 } else {
765 $description = get_string('updategrade', 'assign');
bbd0e548
DW
766 }
767 $actions[$url->out(false)] = $description;
768
9e795179
DW
769 // Hide for offline assignments.
770 if ($this->assignment->is_any_submission_plugin_enabled()) {
771 if (!$row->status ||
772 $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT ||
773 !$this->assignment->get_instance()->submissiondrafts) {
774
775 if (!$row->locked) {
e5403f8c
DW
776 $urlparams = array('id' => $this->assignment->get_course_module()->id,
777 'userid'=>$row->id,
778 'action'=>'lock',
779 'sesskey'=>sesskey(),
780 'page'=>$this->currpage);
781 $url = new moodle_url('/mod/assign/view.php', $urlparams);
782
9e795179
DW
783 $description = get_string('preventsubmissionsshort', 'assign');
784 $actions[$url->out(false)] = $description;
785 } else {
e5403f8c
DW
786 $urlparams = array('id' => $this->assignment->get_course_module()->id,
787 'userid'=>$row->id,
788 'action'=>'unlock',
789 'sesskey'=>sesskey(),
790 'page'=>$this->currpage);
791 $url = new moodle_url('/mod/assign/view.php', $urlparams);
9e795179 792 $description = get_string('allowsubmissionsshort', 'assign');
e5403f8c 793 $actions[$url->out(false)] = $description;
9e795179
DW
794 }
795 }
796
797 if (($this->assignment->get_instance()->duedate ||
798 $this->assignment->get_instance()->cutoffdate) &&
799 $this->hasgrantextension) {
e5403f8c
DW
800 $urlparams = array('id' => $this->assignment->get_course_module()->id,
801 'userid'=>$row->id,
802 'action'=>'grantextension',
803 'sesskey'=>sesskey(),
804 'page'=>$this->currpage);
805 $url = new moodle_url('/mod/assign/view.php', $urlparams);
9e795179 806 $description = get_string('grantextension', 'assign');
bbd0e548
DW
807 $actions[$url->out(false)] = $description;
808 }
809 }
e5403f8c
DW
810 if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED &&
811 $this->assignment->get_instance()->submissiondrafts) {
812 $urlparams = array('id' => $this->assignment->get_course_module()->id,
813 'userid'=>$row->id,
814 'action'=>'reverttodraft',
815 'sesskey'=>sesskey(),
816 'page'=>$this->currpage);
817 $url = new moodle_url('/mod/assign/view.php', $urlparams);
bbd0e548
DW
818 $description = get_string('reverttodraftshort', 'assign');
819 $actions[$url->out(false)] = $description;
820 }
821
822 $edit .= $this->output->container_start(array('yui3-menu', 'actionmenu'), 'actionselect' . $row->id);
823 $edit .= $this->output->container_start(array('yui3-menu-content'));
824 $edit .= html_writer::start_tag('ul');
825 $edit .= html_writer::start_tag('li', array('class'=>'menuicon'));
826
0348468b 827 $menuicon = $this->output->pix_icon('t/contextmenu', get_string('actions'));
bbd0e548
DW
828 $edit .= $this->output->action_link('#menu' . $row->id, $menuicon, null, array('class'=>'yui3-menu-label'));
829 $edit .= $this->output->container_start(array('yui3-menu', 'yui3-loading'), 'menu' . $row->id);
830 $edit .= $this->output->container_start(array('yui3-menu-content'));
831 $edit .= html_writer::start_tag('ul');
832
833 foreach ($actions as $url => $description) {
834 $edit .= html_writer::start_tag('li', array('class'=>'yui3-menuitem'));
835
836 $edit .= $this->output->action_link($url, $description, null, array('class'=>'yui3-menuitem-content'));
837
838 $edit .= html_writer::end_tag('li');
839 }
840 $edit .= html_writer::end_tag('ul');
841 $edit .= $this->output->container_end();
842 $edit .= $this->output->container_end();
843 $edit .= html_writer::end_tag('li');
844 $edit .= html_writer::end_tag('ul');
845
846 $edit .= $this->output->container_end();
847 $edit .= $this->output->container_end();
848
849 return $edit;
850 }
851
852 /**
853 * Write the plugin summary with an optional link to view the full feedback/submission.
854 *
855 * @param assign_plugin $plugin Submission plugin or feedback plugin
856 * @param stdClass $item Submission or grade
e5403f8c
DW
857 * @param string $returnaction The return action to pass to the
858 * view_submission page (the current page)
859 * @param string $returnparams The return params to pass to the view_submission
860 * page (the current page)
bbd0e548
DW
861 * @return string The summary with an optional link
862 */
e5403f8c
DW
863 private function format_plugin_summary_with_link(assign_plugin $plugin,
864 stdClass $item,
865 $returnaction,
866 $returnparams) {
bbd0e548
DW
867 $link = '';
868 $showviewlink = false;
2c6a3dbf 869
bbd0e548
DW
870 $summary = $plugin->view_summary($item, $showviewlink);
871 $separator = '';
872 if ($showviewlink) {
e5403f8c
DW
873 $viewstr = get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'assign');
874 $icon = $this->output->pix_icon('t/preview', $viewstr);
875 $urlparams = array('id' => $this->assignment->get_course_module()->id,
bbd0e548
DW
876 'sid'=>$item->id,
877 'gid'=>$item->id,
878 'plugin'=>$plugin->get_type(),
879 'action'=>'viewplugin' . $plugin->get_subtype(),
880 'returnaction'=>$returnaction,
e5403f8c
DW
881 'returnparams'=>http_build_query($returnparams));
882 $url = new moodle_url('/mod/assign/view.php', $urlparams);
883 $link = $this->output->action_link($url, $icon);
bbd0e548
DW
884 $separator = $this->output->spacer(array(), true);
885 }
886
887 return $link . $separator . $summary;
888 }
889
890
891 /**
e5403f8c 892 * Format the submission and feedback columns.
bbd0e548
DW
893 *
894 * @param string $colname The column name
895 * @param stdClass $row The submission row
896 * @return mixed string or NULL
897 */
e5403f8c 898 public function other_cols($colname, $row) {
df47b77f 899 $plugincache = $this->plugincache[$colname];
2c6a3dbf 900
df47b77f
DW
901 $plugin = $plugincache[0];
902
903 $field = null;
904 if (isset($plugincache[1])) {
905 $field = $plugincache[1];
906 }
907
908 if ($plugin->is_visible() && $plugin->is_enabled()) {
909 if ($plugin->get_subtype() == 'assignsubmission') {
cd01491c
DW
910 if ($this->assignment->get_instance()->teamsubmission) {
911 $group = false;
912 $submission = false;
913 $this->get_group_and_submission($row->id, $group, $submission);
914 if ($submission) {
df47b77f
DW
915 if (isset($field)) {
916 return $plugin->get_editor_text($field, $submission->id);
917 }
e5403f8c
DW
918 return $this->format_plugin_summary_with_link($plugin,
919 $submission,
920 'grading',
921 array());
cd01491c
DW
922 }
923 } else if ($row->submissionid) {
df47b77f
DW
924 if (isset($field)) {
925 return $plugin->get_editor_text($field, $row->submissionid);
926 }
bbd0e548
DW
927 $submission = new stdClass();
928 $submission->id = $row->submissionid;
929 $submission->timecreated = $row->firstsubmission;
930 $submission->timemodified = $row->timesubmitted;
931 $submission->assignment = $this->assignment->get_instance()->id;
932 $submission->userid = $row->userid;
e5403f8c
DW
933 return $this->format_plugin_summary_with_link($plugin,
934 $submission,
935 'grading',
936 array());
bbd0e548 937 }
df47b77f 938 } else {
2c6a3dbf 939 $grade = null;
df47b77f
DW
940 if (isset($field)) {
941 return $plugin->get_editor_text($field, $row->gradeid);
942 }
943
bbd0e548
DW
944 if ($row->gradeid) {
945 $grade = new stdClass();
946 $grade->id = $row->gradeid;
947 $grade->timecreated = $row->firstmarked;
948 $grade->timemodified = $row->timemarked;
949 $grade->assignment = $this->assignment->get_instance()->id;
950 $grade->userid = $row->userid;
951 $grade->grade = $row->grade;
952 $grade->mailed = $row->mailed;
2c6a3dbf
DW
953 }
954 if ($this->quickgrading && $plugin->supports_quickgrading()) {
955 return $plugin->get_quickgrading_html($row->userid, $grade);
956 } else if ($grade) {
e5403f8c
DW
957 return $this->format_plugin_summary_with_link($plugin,
958 $grade,
959 'grading',
960 array());
bbd0e548
DW
961 }
962 }
bbd0e548 963 }
df47b77f 964 return '';
bbd0e548
DW
965 }
966
967 /**
e5403f8c 968 * Using the current filtering and sorting - load all rows and return a single column from them.
bbd0e548
DW
969 *
970 * @param string $columnname The name of the raw column data
971 * @return array of data
972 */
e5403f8c 973 public function get_column_data($columnname) {
bbd0e548
DW
974 $this->setup();
975 $this->currpage = 0;
976 $this->query_db($this->tablemaxrows);
977 $result = array();
978 foreach ($this->rawdata as $row) {
979 $result[] = $row->$columnname;
980 }
981 return $result;
982 }
bbd0e548
DW
983
984 /**
e5403f8c 985 * Return things to the renderer.
bbd0e548
DW
986 *
987 * @return string the assignment name
988 */
e5403f8c 989 public function get_assignment_name() {
bbd0e548
DW
990 return $this->assignment->get_instance()->name;
991 }
992
993 /**
e5403f8c 994 * Return things to the renderer.
bbd0e548
DW
995 *
996 * @return int the course module id
997 */
e5403f8c 998 public function get_course_module_id() {
bbd0e548
DW
999 return $this->assignment->get_course_module()->id;
1000 }
1001
1002 /**
e5403f8c 1003 * Return things to the renderer.
bbd0e548
DW
1004 *
1005 * @return int the course id
1006 */
e5403f8c 1007 public function get_course_id() {
bbd0e548
DW
1008 return $this->assignment->get_course()->id;
1009 }
1010
1011 /**
e5403f8c 1012 * Return things to the renderer.
bbd0e548
DW
1013 *
1014 * @return stdClass The course context
1015 */
e5403f8c 1016 public function get_course_context() {
bbd0e548
DW
1017 return $this->assignment->get_course_context();
1018 }
1019
1020 /**
e5403f8c 1021 * Return things to the renderer.
bbd0e548
DW
1022 *
1023 * @return bool Does this assignment accept submissions
1024 */
e5403f8c 1025 public function submissions_enabled() {
bbd0e548
DW
1026 return $this->assignment->is_any_submission_plugin_enabled();
1027 }
1028
1029 /**
e5403f8c 1030 * Return things to the renderer.
bbd0e548
DW
1031 *
1032 * @return bool Can this user view all grades (the gradebook)
1033 */
e5403f8c
DW
1034 public function can_view_all_grades() {
1035 $context = $this->assignment->get_course_context();
1036 return has_capability('gradereport/grader:view', $context) &&
1037 has_capability('moodle/grade:viewall', $context);
bbd0e548
DW
1038 }
1039
1040 /**
e5403f8c 1041 * Override the table show_hide_link to not show for select column.
bbd0e548
DW
1042 *
1043 * @param string $column the column name, index into various names.
1044 * @param int $index numerical index of the column.
1045 * @return string HTML fragment.
1046 */
1047 protected function show_hide_link($column, $index) {
1048 if ($index > 0) {
1049 return parent::show_hide_link($column, $index);
1050 }
1051 return '';
1052 }
1053}