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