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;
55 * overridden constructor keeps a reference to the assignment class that is displaying this table
57 * @param assign $assignment The assignment class
58 * @param int $perpage how many per page
59 * @param string $filter The current filter
60 * @param int $rowoffset For showing a subsequent page of results
61 * @param bool $quickgrading Is this table wrapped in a quickgrading form?
63 function __construct(assign $assignment, $perpage, $filter, $rowoffset, $quickgrading) {
64 global $CFG, $PAGE, $DB;
65 parent::__construct('mod_assign_grading');
66 $this->assignment = $assignment;
67 $this->perpage = $perpage;
68 $this->quickgrading = $quickgrading;
69 $this->output = $PAGE->get_renderer('mod_assign');
71 $this->define_baseurl(new moodle_url($CFG->wwwroot . '/mod/assign/view.php', array('action'=>'grading', 'id'=>$assignment->get_course_module()->id)));
73 // do some business - then set the sql
75 $currentgroup = groups_get_activity_group($assignment->get_course_module(), true);
78 $this->rownum = $rowoffset - 1;
81 $users = array_keys( $assignment->list_participants($currentgroup, true));
82 if (count($users) == 0) {
83 // insert a record that will never match to the sql is still valid.
88 $params['assignmentid1'] = (int)$this->assignment->get_instance()->id;
89 $params['assignmentid2'] = (int)$this->assignment->get_instance()->id;
91 $fields = user_picture::fields('u') . ', u.id as userid, ';
92 $fields .= 's.status as status, s.id as submissionid, s.timecreated as firstsubmission, s.timemodified as timesubmitted, ';
93 $fields .= 'g.id as gradeid, g.grade as grade, g.timemodified as timemarked, g.timecreated as firstmarked, g.mailed as mailed, g.locked as locked';
94 $from = '{user} u LEFT JOIN {assign_submission} s ON u.id = s.userid AND s.assignment = :assignmentid1' .
95 ' LEFT JOIN {assign_grades} g ON u.id = g.userid AND g.assignment = :assignmentid2';
97 $userparams = array();
100 list($userwhere, $userparams) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, 'user');
101 $where = 'u.id ' . $userwhere;
102 $params = array_merge($params, $userparams);
104 if ($filter == ASSIGN_FILTER_SUBMITTED) {
105 $where .= ' AND s.timecreated > 0 ';
107 if ($filter == ASSIGN_FILTER_REQUIRE_GRADING) {
108 $where .= ' AND (s.timemodified > g.timemodified OR (s.timemodified IS NOT NULL AND g.timemodified IS NULL))';
110 if (strpos($filter, ASSIGN_FILTER_SINGLE_USER) === 0) {
111 $userfilter = (int) array_pop(explode('=', $filter));
112 $where .= ' AND (u.id = :userid)';
113 $params['userid'] = $userfilter;
115 $this->set_sql($fields, $from, $where, $params);
121 if (!$this->is_downloading()) {
122 $columns[] = 'select';
123 $headers[] = get_string('select') . '<div class="selectall"><input type="checkbox" name="selectall" title="' . get_string('selectall') . '"/></div>';
127 $columns[] = 'picture';
128 $headers[] = get_string('pictureofuser');
131 $columns[] = 'fullname';
132 $headers[] = get_string('fullname');
135 if ($assignment->is_any_submission_plugin_enabled()) {
136 $columns[] = 'status';
137 $headers[] = get_string('status');
142 $columns[] = 'grade';
143 $headers[] = get_string('grade');
144 if (!$this->is_downloading()) {
145 // We have to call this column userid so we can use userid as a default sortable column.
146 $columns[] = 'userid';
147 $headers[] = get_string('edit');
150 // Submission plugins
151 if ($assignment->is_any_submission_plugin_enabled()) {
152 $columns[] = 'timesubmitted';
153 $headers[] = get_string('lastmodifiedsubmission', 'assign');
155 foreach ($this->assignment->get_submission_plugins() as $plugin) {
156 if ($plugin->is_visible() && $plugin->is_enabled()) {
157 $columns[] = 'assignsubmission_' . $plugin->get_type();
158 $headers[] = $plugin->get_name();
164 $columns[] = 'timemarked';
165 $headers[] = get_string('lastmodifiedgrade', 'assign');
168 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
169 if ($plugin->is_visible() && $plugin->is_enabled()) {
170 $columns[] = 'assignfeedback_' . $plugin->get_type();
171 $headers[] = $plugin->get_name();
176 $columns[] = 'finalgrade';
177 $headers[] = get_string('finalgrade', 'grades');
179 // load the grading info for all users
180 $this->gradinginfo = grade_get_grades($this->assignment->get_course()->id, 'mod', 'assign', $this->assignment->get_instance()->id, $users);
182 if (!empty($CFG->enableoutcomes) && !empty($this->gradinginfo->outcomes)) {
183 $columns[] = 'outcomes';
184 $headers[] = get_string('outcomes', 'grades');
189 $this->define_columns($columns);
190 $this->define_headers($headers);
191 // We require at least one unique column for the sort.
192 $this->sortable(true, 'userid');
193 $this->no_sorting('finalgrade');
194 $this->no_sorting('userid');
195 $this->no_sorting('select');
196 $this->no_sorting('outcomes');
198 foreach ($this->assignment->get_submission_plugins() as $plugin) {
199 if ($plugin->is_visible() && $plugin->is_enabled()) {
200 $this->no_sorting('assignsubmission_' . $plugin->get_type());
203 foreach ($this->assignment->get_feedback_plugins() as $plugin) {
204 if ($plugin->is_visible() && $plugin->is_enabled()) {
205 $this->no_sorting('assignfeedback_' . $plugin->get_type());
212 * Add the userid to the row class so it can be updated via ajax
214 * @param stdClass $row The row of data
215 * @return string The row class
217 function get_row_class($row) {
218 return 'user' . $row->userid;
222 * Return the number of rows to display on a single page
224 * @return int The number of rows per page
226 function get_rows_per_page() {
227 return $this->perpage;
231 * Display a grade with scales etc.
233 * @param string $grade
234 * @param boolean $editable
235 * @param int $userid The user id of the user this grade belongs to
236 * @param int $modified Timestamp showing when the grade was last modified
237 * @return string The formatted grade
239 function display_grade($grade, $editable, $userid, $modified) {
240 if ($this->is_downloading()) {
243 $o = $this->assignment->display_grade($grade, $editable, $userid, $modified);
248 * Format a list of outcomes
250 * @param stdClass $row
253 function col_outcomes(stdClass $row) {
255 foreach($this->gradinginfo->outcomes as $index=>$outcome) {
256 $options = make_grades_menu(-$outcome->scaleid);
258 $options[0] = get_string('nooutcome', 'grades');
259 if ($this->quickgrading && !($outcome->grades[$row->userid]->locked)) {
260 $select = '<select name="outcome_' . $index . '_' . $row->userid . '" class="quickgrade">';
261 foreach ($options as $optionindex => $optionvalue) {
263 if ($outcome->grades[$row->userid]->grade == $optionindex) {
264 $selected = 'selected="selected"';
266 $select .= '<option value="' . $optionindex . '"' . $selected . '>' . $optionvalue . '</option>';
268 $select .= '</select>';
269 $outcomes .= $this->output->container($outcome->name . ': ' . $select, 'outcome');
271 $outcomes .= $this->output->container($outcome->name . ': ' . $options[$outcome->grades[$row->userid]->grade], 'outcome');
280 * Format a user picture for display (and update rownum as a sideeffect)
282 * @param stdClass $row
285 function col_picture(stdClass $row) {
287 return $this->output->user_picture($row);
293 * Format a user record for display (link to profile)
295 * @param stdClass $row
298 function col_fullname($row) {
299 $courseid = $this->assignment->get_course()->id;
300 $link= new moodle_url('/user/view.php', array('id' =>$row->id, 'course'=>$courseid));
301 return $this->output->action_link($link, fullname($row));
305 * Insert a checkbox for selecting the current row for batch operations
307 * @param stdClass $row
310 function col_select(stdClass $row) {
311 return '<input type="checkbox" name="selectedusers" value="' . $row->userid . '"/>';
315 * Return a users grades from the listing of all grade data for this assignment
318 * @return mixed stdClass or false
320 private function get_gradebook_data_for_user($userid) {
321 if (isset($this->gradinginfo->items[0]) && $this->gradinginfo->items[0]->grades[$userid]) {
322 return $this->gradinginfo->items[0]->grades[$userid];
328 * Format a column of data for display
330 * @param stdClass $row
333 function col_grade(stdClass $row) {
340 if (!$this->is_downloading()) {
341 $icon = $this->output->pix_icon('gradefeedback', get_string('grade'), 'mod_assign');
342 $url = new moodle_url('/mod/assign/view.php',
343 array('id' => $this->assignment->get_course_module()->id,
344 'rownum'=>$this->rownum,'action'=>'grade'));
345 $link = $this->output->action_link($url, $icon);
346 $separator = $this->output->spacer(array(), true);
348 $gradingdisabled = $this->assignment->grading_disabled($row->id);
349 $grade = $this->display_grade($row->grade, $this->quickgrading && !$gradingdisabled, $row->userid, $row->timemarked);
351 //return $grade . $separator . $link;
352 return $link . $separator . $grade;
356 * Format a column of data for display
358 * @param stdClass $row
361 function col_finalgrade(stdClass $row) {
364 $grade = $this->get_gradebook_data_for_user($row->userid);
366 $o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
373 * Format a column of data for display
375 * @param stdClass $row
378 function col_timemarked(stdClass $row) {
381 if ($row->timemarked && $row->grade !== NULL && $row->grade >= 0) {
382 $o = userdate($row->timemarked);
389 * Format a column of data for display
391 * @param stdClass $row
394 function col_timesubmitted(stdClass $row) {
397 if ($row->timesubmitted) {
398 $o = userdate($row->timesubmitted);
405 * Format a column of data for display
407 * @param stdClass $row
410 function col_status(stdClass $row) {
413 if ($this->assignment->is_any_submission_plugin_enabled()) {
415 $o .= $this->output->container(get_string('submissionstatus_' . $row->status, 'assign'), array('class'=>'submissionstatus' .$row->status));
416 if ($this->assignment->get_instance()->duedate && $row->timesubmitted > $this->assignment->get_instance()->duedate) {
417 $o .= $this->output->container(get_string('submittedlateshort', 'assign', format_time($row->timesubmitted - $this->assignment->get_instance()->duedate)), 'latesubmission');
420 $o .= $this->output->container(get_string('submissionslockedshort', 'assign'), 'lockedsubmission');
422 if ($row->grade !== NULL && $row->grade >= 0) {
423 $o .= $this->output->container(get_string('graded', 'assign'), 'submissiongraded');
431 * Format a column of data for display
433 * @param stdClass $row
436 function col_userid(stdClass $row) {
438 if ($this->rownum < 0) {
439 $this->rownum = $this->currpage * $this->pagesize;
446 $url = new moodle_url('/mod/assign/view.php',
447 array('id' => $this->assignment->get_course_module()->id,
448 'rownum'=>$this->rownum,'action'=>'grade'));
450 $description = get_string('grade');
452 $description = get_string('updategrade','assign');
454 $actions[$url->out(false)] = $description;
456 if (!$row->status || $row->status == ASSIGN_SUBMISSION_STATUS_DRAFT || !$this->assignment->get_instance()->submissiondrafts) {
458 $url = new moodle_url('/mod/assign/view.php', array('id' => $this->assignment->get_course_module()->id,
461 'sesskey'=>sesskey(),
462 'page'=>$this->currpage));
463 $description = get_string('preventsubmissionsshort', 'assign');
464 $actions[$url->out(false)] = $description;
466 $url = new moodle_url('/mod/assign/view.php', array('id' => $this->assignment->get_course_module()->id,
469 'sesskey'=>sesskey(),
470 'page'=>$this->currpage));
471 $description = get_string('allowsubmissionsshort', 'assign');
472 $actions[$url->out(false)] = $description;
475 if ($row->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED && $this->assignment->get_instance()->submissiondrafts) {
476 $url = new moodle_url('/mod/assign/view.php', array('id' => $this->assignment->get_course_module()->id,
478 'action'=>'reverttodraft',
479 'sesskey'=>sesskey(),
480 'page'=>$this->currpage));
481 $description = get_string('reverttodraftshort', 'assign');
482 $actions[$url->out(false)] = $description;
485 $edit .= $this->output->container_start(array('yui3-menu', 'actionmenu'), 'actionselect' . $row->id);
486 $edit .= $this->output->container_start(array('yui3-menu-content'));
487 $edit .= html_writer::start_tag('ul');
488 $edit .= html_writer::start_tag('li', array('class'=>'menuicon'));
490 $menuicon = $this->output->pix_icon('i/menu', get_string('actions'));
491 $edit .= $this->output->action_link('#menu' . $row->id, $menuicon, null, array('class'=>'yui3-menu-label'));
492 $edit .= $this->output->container_start(array('yui3-menu', 'yui3-loading'), 'menu' . $row->id);
493 $edit .= $this->output->container_start(array('yui3-menu-content'));
494 $edit .= html_writer::start_tag('ul');
496 foreach ($actions as $url => $description) {
497 $edit .= html_writer::start_tag('li', array('class'=>'yui3-menuitem'));
499 $edit .= $this->output->action_link($url, $description, null, array('class'=>'yui3-menuitem-content'));
501 $edit .= html_writer::end_tag('li');
503 $edit .= html_writer::end_tag('ul');
504 $edit .= $this->output->container_end();
505 $edit .= $this->output->container_end();
506 $edit .= html_writer::end_tag('li');
507 $edit .= html_writer::end_tag('ul');
509 $edit .= $this->output->container_end();
510 $edit .= $this->output->container_end();
516 * Write the plugin summary with an optional link to view the full feedback/submission.
518 * @param assign_plugin $plugin Submission plugin or feedback plugin
519 * @param stdClass $item Submission or grade
520 * @param string $returnaction The return action to pass to the view_submission page (the current page)
521 * @param string $returnparams The return params to pass to the view_submission page (the current page)
522 * @return string The summary with an optional link
524 private function format_plugin_summary_with_link(assign_plugin $plugin, stdClass $item, $returnaction, $returnparams) {
526 $showviewlink = false;
528 $summary = $plugin->view_summary($item, $showviewlink);
531 $icon = $this->output->pix_icon('t/preview', get_string('view' . substr($plugin->get_subtype(), strlen('assign')), 'mod_assign'));
532 $link = $this->output->action_link(
533 new moodle_url('/mod/assign/view.php',
534 array('id' => $this->assignment->get_course_module()->id,
537 'plugin'=>$plugin->get_type(),
538 'action'=>'viewplugin' . $plugin->get_subtype(),
539 'returnaction'=>$returnaction,
540 'returnparams'=>http_build_query($returnparams))),
542 $separator = $this->output->spacer(array(), true);
545 return $link . $separator . $summary;
550 * Format the submission and feedback columns
552 * @param string $colname The column name
553 * @param stdClass $row The submission row
554 * @return mixed string or NULL
556 function other_cols($colname, $row){
557 if (($pos = strpos($colname, 'assignsubmission_')) !== false) {
558 $plugin = $this->assignment->get_submission_plugin_by_type(substr($colname, strlen('assignsubmission_')));
560 if ($plugin->is_visible() && $plugin->is_enabled()) {
561 if ($row->submissionid) {
562 $submission = new stdClass();
563 $submission->id = $row->submissionid;
564 $submission->timecreated = $row->firstsubmission;
565 $submission->timemodified = $row->timesubmitted;
566 $submission->assignment = $this->assignment->get_instance()->id;
567 $submission->userid = $row->userid;
568 return $this->format_plugin_summary_with_link($plugin, $submission, 'grading', array());
573 if (($pos = strpos($colname, 'feedback_')) !== false) {
574 $plugin = $this->assignment->get_feedback_plugin_by_type(substr($colname, strlen('assignfeedback_')));
575 if ($plugin->is_visible() && $plugin->is_enabled()) {
578 $grade = new stdClass();
579 $grade->id = $row->gradeid;
580 $grade->timecreated = $row->firstmarked;
581 $grade->timemodified = $row->timemarked;
582 $grade->assignment = $this->assignment->get_instance()->id;
583 $grade->userid = $row->userid;
584 $grade->grade = $row->grade;
585 $grade->mailed = $row->mailed;
587 if ($this->quickgrading && $plugin->supports_quickgrading()) {
588 return $plugin->get_quickgrading_html($row->userid, $grade);
590 return $this->format_plugin_summary_with_link($plugin, $grade, 'grading', array());
599 * Using the current filtering and sorting - load all rows and return a single column from them
601 * @param string $columnname The name of the raw column data
602 * @return array of data
604 function get_column_data($columnname) {
607 $this->query_db($this->tablemaxrows);
609 foreach ($this->rawdata as $row) {
610 $result[] = $row->$columnname;
615 * Using the current filtering and sorting - load a single row and return a single column from it
617 * @param int $rownumber The rownumber to load
618 * @param string $columnname The name of the raw column data
619 * @param bool $lastrow Set to true if this is the last row in the table
620 * @return mixed string or false
622 function get_cell_data($rownumber, $columnname, $lastrow) {
624 $this->currpage = $rownumber;
626 if ($rownumber == $this->totalrows-1) {
629 foreach ($this->rawdata as $row) {
630 return $row->$columnname;
636 * Return things to the renderer
638 * @return string the assignment name
640 function get_assignment_name() {
641 return $this->assignment->get_instance()->name;
645 * Return things to the renderer
647 * @return int the course module id
649 function get_course_module_id() {
650 return $this->assignment->get_course_module()->id;
654 * Return things to the renderer
656 * @return int the course id
658 function get_course_id() {
659 return $this->assignment->get_course()->id;
663 * Return things to the renderer
665 * @return stdClass The course context
667 function get_course_context() {
668 return $this->assignment->get_course_context();
672 * Return things to the renderer
674 * @return bool Does this assignment accept submissions
676 function submissions_enabled() {
677 return $this->assignment->is_any_submission_plugin_enabled();
681 * Return things to the renderer
683 * @return bool Can this user view all grades (the gradebook)
685 function can_view_all_grades() {
686 return has_capability('gradereport/grader:view', $this->assignment->get_course_context()) && has_capability('moodle/grade:viewall', $this->assignment->get_course_context());
690 * Override the table show_hide_link to not show for select column
692 * @param string $column the column name, index into various names.
693 * @param int $index numerical index of the column.
694 * @return string HTML fragment.
696 protected function show_hide_link($column, $index) {
698 return parent::show_hide_link($column, $index);