MDL-36331 core_grade: fixed up 2 incorrect default return URLs
[moodle.git] / grade / edit / tree / grade.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/>.
8ad36f4c 16
a153c9f2
AD
17/**
18 * Edit a user's grade for a particular activity
19 *
20 * @package core_grades
21 * @copyright 2007 Petr Skoda
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
78ad5f3f 25require_once '../../../config.php';
3af29899 26require_once $CFG->dirroot.'/grade/lib.php';
76317c73 27require_once $CFG->dirroot.'/grade/report/lib.php';
6fb39a98 28require_once 'grade_form.php';
20544755 29
30$courseid = required_param('courseid', PARAM_INT);
23207a1a 31$id = optional_param('id', 0, PARAM_INT);
32$itemid = optional_param('itemid', 0, PARAM_INT);
33$userid = optional_param('userid', 0, PARAM_INT);
20544755 34
a6855934 35$url = new moodle_url('/grade/edit/tree/grade.php', array('courseid'=>$courseid));
beebcf26 36if ($id !== 0) {
37 $url->param('id', $id);
38}
39if ($itemid !== 0) {
40 $url->param('itemid', $itemid);
41}
42if ($userid !== 0) {
43 $url->param('userid', $userid);
44}
45$PAGE->set_url($url);
46
5c75a0a3 47if (!$course = $DB->get_record('course', array('id' => $courseid))) {
20544755 48 print_error('nocourseid');
49}
50
367a75fa 51$PAGE->set_pagelayout('incourse');
20544755 52require_login($course);
53$context = get_context_instance(CONTEXT_COURSE, $course->id);
27b1735b 54if (!has_capability('moodle/grade:manage', $context)) {
a5b8be62 55 require_capability('moodle/grade:edit', $context);
27b1735b 56}
20544755 57
58// default return url
3af29899 59$gpr = new grade_plugin_return();
11694f39 60$returnurl = $gpr->get_return_url($CFG->wwwroot.'/grade/report/index.php?id='.$course->id);
20544755 61
23207a1a 62// security checks!
63if (!empty($id)) {
5c75a0a3 64 if (!$grade = $DB->get_record('grade_grades', array('id' => $id))) {
1bb0aa0f 65 print_error('invalidgroupid');
23207a1a 66 }
67
68 if (!empty($itemid) and $itemid != $grade->itemid) {
1bb0aa0f 69 print_error('invaliditemid');
23207a1a 70 }
71 $itemid = $grade->itemid;
72
73 if (!empty($userid) and $userid != $grade->userid) {
1bb0aa0f 74 print_error('invaliduser');
23207a1a 75 }
76 $userid = $grade->userid;
79eabc2a 77
23207a1a 78 unset($grade);
9c25957a 79
23207a1a 80} else if (empty($userid) or empty($itemid)) {
1bb0aa0f 81 print_error('missinguseranditemid');
23207a1a 82}
83
84if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) {
5db2027e 85 print_error('cannotfindgradeitem');
23207a1a 86}
87
dc3ca94c 88// now verify grading user has access to all groups or is member of the same group when separate groups used in course
b2bc96d1 89if (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
2c386f82 90 if ($groups = groups_get_all_groups($COURSE->id, $userid)) {
dc3ca94c 91 $ok = false;
92 foreach ($groups as $group) {
93 if (groups_is_member($group->id, $USER->id)) {
94 $ok = true;
95 }
96 }
97 if (!$ok) {
1bb0aa0f 98 print_error('cannotgradeuser');
dc3ca94c 99 }
100 } else {
1bb0aa0f 101 print_error('cannotgradeuser');
dc3ca94c 102 }
103}
23207a1a 104
105$mform = new edit_grade_form(null, array('grade_item'=>$grade_item, 'gpr'=>$gpr));
106
5c75a0a3 107if ($grade = $DB->get_record('grade_grades', array('itemid' => $grade_item->id, 'userid' => $userid))) {
3f2b0c8a 108
109 // always clean existing feedback - grading should not have XSS risk
110 if (can_use_html_editor()) {
0f392ff4 111 if (empty($grade->feedback)) {
112 $grade->feedback = '';
113 } else {
ace9051c 114 $options = new stdClass();
0f392ff4 115 $options->smiley = false;
116 $options->filter = false;
117 $options->noclean = false;
185db1c9 118 $options->para = false;
0f392ff4 119 $grade->feedback = format_text($grade->feedback, $grade->feedbackformat, $options);
120 }
3f2b0c8a 121 $grade->feedbackformat = FORMAT_HTML;
122 } else {
123 $grade->feedback = clean_text($grade->feedback, $grade->feedbackformat);
920a0fb2 124 }
125
15a51159 126 $grade->locked = $grade->locked > 0 ? 1:0;
127 $grade->overridden = $grade->overridden > 0 ? 1:0;
128 $grade->excluded = $grade->excluded > 0 ? 1:0;
129
130 if ($grade->hidden > 1) {
131 $grade->hiddenuntil = $grade->hidden;
f60c61b1 132 $grade->hidden = 1;
15a51159 133 } else {
134 $grade->hiddenuntil = 0;
135 }
23207a1a 136
f60c61b1 137 if ($grade_item->is_hidden()) {
138 $grade->hidden = 1;
139 }
140
fb0e3570 141 if ($grade_item->is_locked()) {
142 $grade->locked = 1;
143 }
144
76317c73 145 // normalize the final grade value
146 if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
147 if (empty($grade->finalgrade)) {
148 $grade->finalgrade = -1;
149 } else {
150 $grade->finalgrade = (int)$grade->finalgrade;
151 }
152 } else if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
31a6c06c 153 $grade->finalgrade = format_float($grade->finalgrade, $grade_item->get_decimals());
76317c73 154 }
155
0f392ff4 156 $grade->oldgrade = $grade->finalgrade;
157 $grade->oldfeedback = $grade->feedback;
76317c73 158
8bdc9cac 159 $grade->feedback = array('text'=>$grade->feedback, 'format'=>$grade->feedbackformat);
9c25957a 160
8bdc9cac 161 $mform->set_data($grade);
20544755 162} else {
54352ac9 163 $grade = new stdClass();
8bdc9cac 164 $grade->feedback = array('text'=>'', 'format'=>FORMAT_HTML);
27b1735b 165 $mform->set_data(array('itemid'=>$itemid, 'userid'=>$userid, 'locked'=>$grade_item->locked, 'locktime'=>$grade_item->locktime));
20544755 166}
167
168if ($mform->is_cancelled()) {
169 redirect($returnurl);
23207a1a 170
20544755 171// form processing
653a8648 172} else if ($data = $mform->get_data(false)) {
8bdc9cac 173
c84d86e0 174 if (isset($data->feedback) && is_array($data->feedback)) {
8bdc9cac
SH
175 $data->feedbackformat = $data->feedback['format'];
176 $data->feedback = $data->feedback['text'];
177 }
178
23207a1a 179 $old_grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true); //might not exist yet
920a0fb2 180
0658afc9 181 // fix no grade for scales
5048575d 182 if (!isset($data->finalgrade) or $data->finalgrade == $data->oldgrade) {
fb0e3570 183 $data->finalgrade = $old_grade_grade->finalgrade;
184
5048575d 185 } else if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
186 if ($data->finalgrade < 1) {
187 $data->finalgrade = NULL;
188 }
76317c73 189
190 } else if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
191 $data->finalgrade = unformat_float($data->finalgrade);
5048575d 192
193 } else {
6ef4878b 194 //this should not happen
5048575d 195 $data->finalgrade = $old_grade_grade->finalgrade;
0658afc9 196 }
197
0f392ff4 198 // the overriding of feedback is tricky - we have to care about external items only
4cc977a6 199 if (!property_exists($data, 'feedback') or $data->feedback == $data->oldfeedback) {
3f2b0c8a 200 $data->feedback = $old_grade_grade->feedback;
201 $data->feedbackformat = $old_grade_grade->feedbackformat;
fb0e3570 202 }
23207a1a 203 // update final grade or feedback
c5883e79 204 // when we set override grade the first time, it happens here
0f392ff4 205 $grade_item->update_final_grade($data->userid, $data->finalgrade, 'editgrade', $data->feedback, $data->feedbackformat);
8c4d80f1 206
83d387c2 207 $grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true);
fb0e3570 208 $grade_grade->grade_item =& $grade_item; // no db fetching
8c4d80f1 209
0f392ff4 210 if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:edit', $context)) {
c5883e79
RK
211 // change overridden flag
212 if (!isset($data->overridden)) {
213 $data->overridden = 0; // checkbox unticked
5048575d 214 }
c5883e79 215 $grade_grade->set_overridden($data->overridden);
0f392ff4 216 }
217
27b1735b 218 if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:hide', $context)) {
f60c61b1 219 $hidden = empty($data->hidden) ? 0: $data->hidden;
220 $hiddenuntil = empty($data->hiddenuntil) ? 0: $data->hiddenuntil;
221
222 if ($grade_item->is_hidden()) {
223 if ($old_grade_grade->hidden == 1 and $hiddenuntil == 0) {
224 //nothing to do - grade was originally hidden, we want to keep it that way
27b1735b 225 } else {
f60c61b1 226 $grade_grade->set_hidden($hiddenuntil);
27b1735b 227 }
15a51159 228 } else {
f60c61b1 229 if ($hiddenuntil) {
230 $grade_grade->set_hidden($hiddenuntil);
231 } else {
232 $grade_grade->set_hidden($hidden); // checkbox data might be undefined
233 }
15a51159 234 }
15a51159 235 }
23207a1a 236
fb0e3570 237 if (isset($data->locked) and !$grade_item->is_locked()) {
238 if (($old_grade_grade->locked or $old_grade_grade->locktime)
239 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:unlock', $context))) {
240 //ignore data
241
242 } else if ((!$old_grade_grade->locked and !$old_grade_grade->locktime)
243 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:lock', $context))) {
244 //ignore data
245
246 } else {
247 $grade_grade->set_locktime($data->locktime); //set_lock may reset locktime
248 $grade_grade->set_locked($data->locked, false, true);
25bcd908 249 // reload grade in case it was regraded from activity
250 $grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true);
251 $grade_grade->grade_item =& $grade_item; // no db fetching
23207a1a 252 }
253 }
254
fb0e3570 255 if (isset($data->excluded) and has_capability('moodle/grade:manage', $context)) {
256 $grade_grade->set_excluded($data->excluded);
257 }
258
fb0e3570 259 // detect cases when we need to do full regrading
260 if ($old_grade_grade->excluded != $grade_grade->excluded) {
261 $parent = $grade_item->get_parent_category();
262 $parent->force_regrading();
20544755 263
6ef4878b 264 } else if ($old_grade_grade->overridden != $grade_grade->overridden and empty($grade_grade->overridden)) { // only when unoverridding
fb0e3570 265 $grade_item->force_regrading();
27b1735b 266
fb0e3570 267 } else if ($old_grade_grade->locktime != $grade_grade->locktime) {
268 $grade_item->force_regrading();
269 }
27b1735b 270
ffe6eb27 271 redirect($returnurl);
20544755 272}
273
20544755 274$strgrades = get_string('grades');
20544755 275$strgraderreport = get_string('graderreport', 'grades');
23207a1a 276$strgradeedit = get_string('editgrade', 'grades');
277$struser = get_string('user');
20544755 278
7cc378fd 279grade_build_nav(__FILE__, $strgradeedit, array('courseid' => $courseid));
20544755 280
281/*********** BEGIN OUTPUT *************/
7cc378fd 282$PAGE->set_title($strgrades . ': ' . $strgraderreport . ': ' . $strgradeedit);
02114f13 283$PAGE->set_heading($course->fullname);
20544755 284
7cc378fd 285echo $OUTPUT->header();
c018f973 286echo $OUTPUT->heading($strgradeedit);
20544755 287
c3b834b4 288echo $OUTPUT->box_start();
20544755 289
20544755 290// Form if in edit or add modes
291$mform->display();
20544755 292
c3b834b4 293echo $OUTPUT->box_end();
20544755 294
5a931394 295echo $OUTPUT->footer();
20544755 296die;