Merge branch 'wip-MDL-27145-master' of git://github.com/samhemelryk/moodle
[moodle.git] / grade / report / grader / index.php
blob3a35fd86575f5a8db6ef4a9ee0f6f5c750e12981
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 require_once '../../../config.php';
19 require_once $CFG->libdir.'/gradelib.php';
20 require_once $CFG->dirroot.'/grade/lib.php';
21 require_once $CFG->dirroot.'/grade/report/grader/lib.php';
23 $courseid = required_param('id', PARAM_INT); // course id
24 $page = optional_param('page', 0, PARAM_INT); // active page
25 $perpageurl = optional_param('perpage', 0, PARAM_INT);
26 $edit = optional_param('edit', -1, PARAM_BOOL); // sticky editting mode
28 $sortitemid = optional_param('sortitemid', 0, PARAM_ALPHANUM); // sort by which grade item
29 $action = optional_param('action', 0, PARAM_ALPHAEXT);
30 $move = optional_param('move', 0, PARAM_INT);
31 $type = optional_param('type', 0, PARAM_ALPHA);
32 $target = optional_param('target', 0, PARAM_ALPHANUM);
33 $toggle = optional_param('toggle', NULL, PARAM_INT);
34 $toggle_type = optional_param('toggle_type', 0, PARAM_ALPHANUM);
36 $PAGE->set_url(new moodle_url('/grade/report/grader/index.php', array('id'=>$courseid)));
38 /// basic access checks
39 if (!$course = $DB->get_record('course', array('id' => $courseid))) {
40 print_error('nocourseid');
42 require_login($course);
43 $context = get_context_instance(CONTEXT_COURSE, $course->id);
45 require_capability('gradereport/grader:view', $context);
46 require_capability('moodle/grade:viewall', $context);
48 /// return tracking object
49 $gpr = new grade_plugin_return(array('type'=>'report', 'plugin'=>'grader', 'courseid'=>$courseid, 'page'=>$page));
51 /// last selected report session tracking
52 if (!isset($USER->grade_last_report)) {
53 $USER->grade_last_report = array();
55 $USER->grade_last_report[$course->id] = 'grader';
57 /// Build editing on/off buttons
59 if (!isset($USER->gradeediting)) {
60 $USER->gradeediting = array();
63 if (has_capability('moodle/grade:edit', $context)) {
64 if (!isset($USER->gradeediting[$course->id])) {
65 $USER->gradeediting[$course->id] = 0;
68 if (($edit == 1) and confirm_sesskey()) {
69 $USER->gradeediting[$course->id] = 1;
70 } else if (($edit == 0) and confirm_sesskey()) {
71 $USER->gradeediting[$course->id] = 0;
74 // page params for the turn editting on
75 $options = $gpr->get_options();
76 $options['sesskey'] = sesskey();
78 if ($USER->gradeediting[$course->id]) {
79 $options['edit'] = 0;
80 $string = get_string('turneditingoff');
81 } else {
82 $options['edit'] = 1;
83 $string = get_string('turneditingon');
86 $buttons = new single_button(new moodle_url('index.php', $options), $string, 'get');
87 } else {
88 $USER->gradeediting[$course->id] = 0;
89 $buttons = '';
92 $gradeserror = array();
94 // Handle toggle change request
95 if (!is_null($toggle) && !empty($toggle_type)) {
96 set_user_preferences(array('grade_report_show'.$toggle_type => $toggle));
99 //first make sure we have proper final grades - this must be done before constructing of the grade tree
100 grade_regrade_final_grades($courseid);
102 // Perform actions
103 if (!empty($target) && !empty($action) && confirm_sesskey()) {
104 grade_report_grader::process_action($target, $action);
107 $reportname = get_string('pluginname', 'gradereport_grader');
109 /// Print header
110 print_grade_page_head($COURSE->id, 'report', 'grader', $reportname, false, $buttons);
112 //Initialise the grader report object that produces the table
113 //the class grade_report_grader_ajax was removed as part of MDL-21562
114 $report = new grade_report_grader($courseid, $gpr, $context, $page, $sortitemid);
116 // make sure separate group does not prevent view
117 if ($report->currentgroup == -2) {
118 echo $OUTPUT->heading(get_string("notingroup"));
119 echo $OUTPUT->footer();
120 exit;
123 /// processing posted grades & feedback here
124 if ($data = data_submitted() and confirm_sesskey() and has_capability('moodle/grade:edit', $context)) {
125 $warnings = $report->process_data($data);
126 } else {
127 $warnings = array();
131 // Override perpage if set in URL
132 if ($perpageurl) {
133 $report->user_prefs['studentsperpage'] = $perpageurl;
136 // final grades MUST be loaded after the processing
137 $report->load_users();
138 $numusers = $report->get_numusers();
139 $report->load_final_grades();
141 echo $report->group_selector;
142 echo '<div class="clearer"></div>';
143 // echo $report->get_toggles_html();
145 //show warnings if any
146 foreach($warnings as $warning) {
147 echo $OUTPUT->notification($warning);
150 $studentsperpage = $report->get_pref('studentsperpage');
151 // Don't use paging if studentsperpage is empty or 0 at course AND site levels
152 if (!empty($studentsperpage)) {
153 echo $OUTPUT->paging_bar($numusers, $report->page, $studentsperpage, $report->pbarurl);
156 $reporthtml = $report->get_grade_table();
158 // print submit button
159 if ($USER->gradeediting[$course->id] && ($report->get_pref('showquickfeedback') || $report->get_pref('quickgrading'))) {
160 echo '<form action="index.php" method="post">';
161 echo '<div>';
162 echo '<input type="hidden" value="'.s($courseid).'" name="id" />';
163 echo '<input type="hidden" value="'.sesskey().'" name="sesskey" />';
164 echo '<input type="hidden" value="grader" name="report"/>';
165 echo $reporthtml;
166 echo '<div class="submit"><input type="submit" value="'.s(get_string('update')).'" /></div>';
167 echo '</div></form>';
168 } else {
169 echo $reporthtml;
172 // prints paging bar at bottom for large pages
173 if (!empty($studentsperpage) && $studentsperpage >= 20) {
174 echo $OUTPUT->paging_bar($numusers, $report->page, $studentsperpage, $report->pbarurl);
176 echo $OUTPUT->footer();