MDL-49324 grades: Create helper function for regrading on report view
[moodle.git] / grade / report / grader / index.php
CommitLineData
e060e33d 1<?php
e060e33d 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/>.
65dd61bd 16
a153c9f2
AD
17/**
18 * The gradebook grader report
19 *
20 * @package gradereport_grader
21 * @copyright 2007 Moodle Pty Ltd (http://moodle.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
02d1a0a5
MA
25require_once('../../../config.php');
26require_once($CFG->libdir.'/gradelib.php');
27require_once($CFG->dirroot.'/user/renderer.php');
28require_once($CFG->dirroot.'/grade/lib.php');
29require_once($CFG->dirroot.'/grade/report/grader/lib.php');
65dd61bd 30
fd01125f 31$courseid = required_param('id', PARAM_INT); // course id
65dd61bd 32$page = optional_param('page', 0, PARAM_INT); // active page
65dd61bd 33$edit = optional_param('edit', -1, PARAM_BOOL); // sticky editting mode
4b98acca 34
8178f34c 35$sortitemid = optional_param('sortitemid', 0, PARAM_ALPHANUM); // sort by which grade item
48b5d8f3 36$action = optional_param('action', 0, PARAM_ALPHAEXT);
8178f34c 37$move = optional_param('move', 0, PARAM_INT);
38$type = optional_param('type', 0, PARAM_ALPHA);
a9d5bde7 39$target = optional_param('target', 0, PARAM_ALPHANUM);
02d1a0a5 40$toggle = optional_param('toggle', null, PARAM_INT);
73a5d828 41$toggle_type = optional_param('toggle_type', 0, PARAM_ALPHANUM);
e5161d0c 42
573558af
AL
43$graderreportsifirst = optional_param('sifirst', null, PARAM_NOTAGS);
44$graderreportsilast = optional_param('silast', null, PARAM_NOTAGS);
8e4bf6cc 45
d1a4346f 46// The report object is recreated each time, save search information to SESSION object for future use.
41877849 47if (isset($graderreportsifirst)) {
02d1a0a5
MA
48 $SESSION->gradereport['filterfirstname'] = $graderreportsifirst;
49}
41877849 50if (isset($graderreportsilast)) {
02d1a0a5
MA
51 $SESSION->gradereport['filtersurname'] = $graderreportsilast;
52}
8e4bf6cc 53
4d5059d4 54$PAGE->set_url(new moodle_url('/grade/report/grader/index.php', array('id'=>$courseid)));
f79776cb 55$PAGE->requires->yui_module('moodle-gradereport_grader-gradereporttable', 'Y.M.gradereport_grader.init', null, null, true);
c018f973 56
02d1a0a5 57// basic access checks
d24832f9 58if (!$course = $DB->get_record('course', array('id' => $courseid))) {
65dd61bd 59 print_error('nocourseid');
60}
61require_login($course);
d4060472 62$context = context_course::instance($course->id);
2cc773f5 63
65dd61bd 64require_capability('gradereport/grader:view', $context);
2cc773f5 65require_capability('moodle/grade:viewall', $context);
65dd61bd 66
02d1a0a5 67// return tracking object
65dd61bd 68$gpr = new grade_plugin_return(array('type'=>'report', 'plugin'=>'grader', 'courseid'=>$courseid, 'page'=>$page));
69
02d1a0a5 70// last selected report session tracking
65dd61bd 71if (!isset($USER->grade_last_report)) {
72 $USER->grade_last_report = array();
73}
74$USER->grade_last_report[$course->id] = 'grader';
75
02d1a0a5 76// Build editing on/off buttons
65dd61bd 77
78if (!isset($USER->gradeediting)) {
2cc773f5 79 $USER->gradeediting = array();
65dd61bd 80}
81
a5b8be62 82if (has_capability('moodle/grade:edit', $context)) {
2cc773f5 83 if (!isset($USER->gradeediting[$course->id])) {
84 $USER->gradeediting[$course->id] = 0;
85 }
86
87 if (($edit == 1) and confirm_sesskey()) {
88 $USER->gradeediting[$course->id] = 1;
89 } else if (($edit == 0) and confirm_sesskey()) {
90 $USER->gradeediting[$course->id] = 0;
91 }
65dd61bd 92
2cc773f5 93 // page params for the turn editting on
94 $options = $gpr->get_options();
95 $options['sesskey'] = sesskey();
96
97 if ($USER->gradeediting[$course->id]) {
98 $options['edit'] = 0;
99 $string = get_string('turneditingoff');
100 } else {
101 $options['edit'] = 1;
102 $string = get_string('turneditingon');
103 }
104
4d5059d4 105 $buttons = new single_button(new moodle_url('index.php', $options), $string, 'get');
65dd61bd 106} else {
2cc773f5 107 $USER->gradeediting[$course->id] = 0;
108 $buttons = '';
65dd61bd 109}
110
65dd61bd 111$gradeserror = array();
112
d490d87c 113// Handle toggle change request
d490d87c 114if (!is_null($toggle) && !empty($toggle_type)) {
65dd61bd 115 set_user_preferences(array('grade_report_show'.$toggle_type => $toggle));
d490d87c 116}
117
dc482cfa 118// Perform actions
438a5aa9 119if (!empty($target) && !empty($action) && confirm_sesskey()) {
9fe5a1b8 120 grade_report_grader::do_process_action($target, $action, $courseid);
438a5aa9 121}
122
b5e7b2bf 123$reportname = get_string('pluginname', 'gradereport_grader');
bcfc87f5 124
30be6c84 125// Do this check just before printing the grade header (and only do it once).
0a802c9c
AN
126grade_regrade_final_grades_if_required($course);
127
02d1a0a5 128// Print header
367a75fa
SH
129print_grade_page_head($COURSE->id, 'report', 'grader', $reportname, false, $buttons);
130
92ff6236
AD
131//Initialise the grader report object that produces the table
132//the class grade_report_grader_ajax was removed as part of MDL-21562
0be986de 133$report = new grade_report_grader($courseid, $gpr, $context, $page, $sortitemid);
8e4bf6cc 134$numusers = $report->get_numusers(true, true);
75674470 135
7a9ba4b4 136// make sure separate group does not prevent view
137if ($report->currentgroup == -2) {
c018f973 138 echo $OUTPUT->heading(get_string("notingroup"));
5a931394 139 echo $OUTPUT->footer();
7a9ba4b4 140 exit;
141}
142
02d1a0a5 143// processing posted grades & feedback here
294ce987 144if ($data = data_submitted() and confirm_sesskey() and has_capability('moodle/grade:edit', $context)) {
75674470 145 $warnings = $report->process_data($data);
146} else {
36ffa395 147 $warnings = array();
75674470 148}
149
75674470 150// final grades MUST be loaded after the processing
4ba9941c 151$report->load_users();
4ba9941c 152$report->load_final_grades();
4ba9941c 153echo $report->group_selector;
8e4bf6cc 154
cbe8e5b3
MA
155// User search
156$url = new moodle_url('/grade/report/grader/index.php', array('id' => $course->id));
02d1a0a5
MA
157$firstinitial = isset($SESSION->gradereport['filterfirstname']) ? $SESSION->gradereport['filterfirstname'] : '';
158$lastinitial = isset($SESSION->gradereport['filtersurname']) ? $SESSION->gradereport['filtersurname'] : '';
cbe8e5b3
MA
159$totalusers = $report->get_numusers(true, false);
160$renderer = $PAGE->get_renderer('core_user');
41877849 161echo $renderer->user_search($url, $firstinitial, $lastinitial, $numusers, $totalusers, $report->currentgroupname);
0cfabab6 162
75674470 163//show warnings if any
02d1a0a5 164foreach ($warnings as $warning) {
c3b834b4 165 echo $OUTPUT->notification($warning);
0cfabab6 166}
167
a740a0ad 168$studentsperpage = $report->get_students_per_page();
09cef06a 169// Don't use paging if studentsperpage is empty or 0 at course AND site levels
170if (!empty($studentsperpage)) {
929d7a83 171 echo $OUTPUT->paging_bar($numusers, $report->page, $studentsperpage, $report->pbarurl);
09cef06a 172}
4ba9941c 173
cbe8e5b3
MA
174$displayaverages = true;
175if ($numusers == 0) {
176 $displayaverages = false;
177}
178
179$reporthtml = $report->get_grade_table($displayaverages);
5b508a08 180
8178f34c 181// print submit button
fe213365 182if ($USER->gradeediting[$course->id] && ($report->get_pref('showquickfeedback') || $report->get_pref('quickgrading'))) {
0f204015 183 echo '<form action="index.php" enctype="application/x-www-form-urlencoded" method="post" id="gradereport_grader">'; // Enforce compatibility with our max_input_vars hack.
916276fc
PS
184 echo '<div>';
185 echo '<input type="hidden" value="'.s($courseid).'" name="id" />';
186 echo '<input type="hidden" value="'.sesskey().'" name="sesskey" />';
09171b95 187 echo '<input type="hidden" value="'.time().'" name="timepageload" />';
916276fc 188 echo '<input type="hidden" value="grader" name="report"/>';
4a29d291 189 echo '<input type="hidden" value="'.$page.'" name="page"/>';
916276fc 190 echo $reporthtml;
e54e5193 191 echo '<div class="submit"><input type="submit" id="gradersubmit" value="'.s(get_string('savechanges')).'" /></div>';
916276fc 192 echo '</div></form>';
319770d7 193} else {
194 echo $reporthtml;
16501647 195}
20544755 196
197// prints paging bar at bottom for large pages
09cef06a 198if (!empty($studentsperpage) && $studentsperpage >= 20) {
929d7a83 199 echo $OUTPUT->paging_bar($numusers, $report->page, $studentsperpage, $report->pbarurl);
20544755 200}
67ab0ff0
MN
201
202$event = \gradereport_grader\event\grade_report_viewed::create(
203 array(
204 'context' => $context,
205 'courseid' => $courseid,
206 )
207);
208$event->trigger();
209
6c3ef410 210echo $OUTPUT->footer();