MDL-40697 core_grades: removed existing user_graded event triggers
[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);
d4060472 53$context = context_course::instance($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();
0d02aa53 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
3d27180e
DW
110 if (empty($grade->feedback)) {
111 $grade->feedback = '';
3f2b0c8a 112 } else {
3d27180e
DW
113 $options = new stdClass();
114 $options->smiley = false;
115 $options->filter = false;
116 $options->noclean = false;
117 $options->para = false;
118 $grade->feedback = format_text($grade->feedback, $grade->feedbackformat, $options);
920a0fb2 119 }
3d27180e 120 $grade->feedbackformat = FORMAT_HTML;
920a0fb2 121
15a51159 122 $grade->locked = $grade->locked > 0 ? 1:0;
123 $grade->overridden = $grade->overridden > 0 ? 1:0;
124 $grade->excluded = $grade->excluded > 0 ? 1:0;
125
126 if ($grade->hidden > 1) {
127 $grade->hiddenuntil = $grade->hidden;
f60c61b1 128 $grade->hidden = 1;
15a51159 129 } else {
130 $grade->hiddenuntil = 0;
131 }
23207a1a 132
f60c61b1 133 if ($grade_item->is_hidden()) {
134 $grade->hidden = 1;
135 }
136
fb0e3570 137 if ($grade_item->is_locked()) {
138 $grade->locked = 1;
139 }
140
76317c73 141 // normalize the final grade value
142 if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
143 if (empty($grade->finalgrade)) {
144 $grade->finalgrade = -1;
145 } else {
146 $grade->finalgrade = (int)$grade->finalgrade;
147 }
148 } else if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
31a6c06c 149 $grade->finalgrade = format_float($grade->finalgrade, $grade_item->get_decimals());
76317c73 150 }
151
0f392ff4 152 $grade->oldgrade = $grade->finalgrade;
153 $grade->oldfeedback = $grade->feedback;
76317c73 154
8bdc9cac 155 $grade->feedback = array('text'=>$grade->feedback, 'format'=>$grade->feedbackformat);
9c25957a 156
8bdc9cac 157 $mform->set_data($grade);
20544755 158} else {
54352ac9 159 $grade = new stdClass();
8bdc9cac 160 $grade->feedback = array('text'=>'', 'format'=>FORMAT_HTML);
27b1735b 161 $mform->set_data(array('itemid'=>$itemid, 'userid'=>$userid, 'locked'=>$grade_item->locked, 'locktime'=>$grade_item->locktime));
20544755 162}
163
164if ($mform->is_cancelled()) {
165 redirect($returnurl);
23207a1a 166
20544755 167// form processing
653a8648 168} else if ($data = $mform->get_data(false)) {
8bdc9cac 169
c84d86e0 170 if (isset($data->feedback) && is_array($data->feedback)) {
8bdc9cac
SH
171 $data->feedbackformat = $data->feedback['format'];
172 $data->feedback = $data->feedback['text'];
173 }
174
23207a1a 175 $old_grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true); //might not exist yet
920a0fb2 176
0658afc9 177 // fix no grade for scales
5048575d 178 if (!isset($data->finalgrade) or $data->finalgrade == $data->oldgrade) {
fb0e3570 179 $data->finalgrade = $old_grade_grade->finalgrade;
180
5048575d 181 } else if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
182 if ($data->finalgrade < 1) {
183 $data->finalgrade = NULL;
184 }
76317c73 185
186 } else if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
187 $data->finalgrade = unformat_float($data->finalgrade);
5048575d 188
189 } else {
6ef4878b 190 //this should not happen
5048575d 191 $data->finalgrade = $old_grade_grade->finalgrade;
0658afc9 192 }
193
0f392ff4 194 // the overriding of feedback is tricky - we have to care about external items only
4cc977a6 195 if (!property_exists($data, 'feedback') or $data->feedback == $data->oldfeedback) {
3f2b0c8a 196 $data->feedback = $old_grade_grade->feedback;
197 $data->feedbackformat = $old_grade_grade->feedbackformat;
fb0e3570 198 }
afd2efc0 199
23207a1a 200 // update final grade or feedback
c5883e79 201 // when we set override grade the first time, it happens here
0f392ff4 202 $grade_item->update_final_grade($data->userid, $data->finalgrade, 'editgrade', $data->feedback, $data->feedbackformat);
8c4d80f1 203
83d387c2 204 $grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true);
fb0e3570 205 $grade_grade->grade_item =& $grade_item; // no db fetching
8c4d80f1 206
0f392ff4 207 if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:edit', $context)) {
c5883e79
RK
208 // change overridden flag
209 if (!isset($data->overridden)) {
210 $data->overridden = 0; // checkbox unticked
5048575d 211 }
c5883e79 212 $grade_grade->set_overridden($data->overridden);
0f392ff4 213 }
214
27b1735b 215 if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:hide', $context)) {
f60c61b1 216 $hidden = empty($data->hidden) ? 0: $data->hidden;
217 $hiddenuntil = empty($data->hiddenuntil) ? 0: $data->hiddenuntil;
218
219 if ($grade_item->is_hidden()) {
220 if ($old_grade_grade->hidden == 1 and $hiddenuntil == 0) {
221 //nothing to do - grade was originally hidden, we want to keep it that way
27b1735b 222 } else {
f60c61b1 223 $grade_grade->set_hidden($hiddenuntil);
27b1735b 224 }
15a51159 225 } else {
f60c61b1 226 if ($hiddenuntil) {
227 $grade_grade->set_hidden($hiddenuntil);
228 } else {
229 $grade_grade->set_hidden($hidden); // checkbox data might be undefined
230 }
15a51159 231 }
15a51159 232 }
23207a1a 233
fb0e3570 234 if (isset($data->locked) and !$grade_item->is_locked()) {
235 if (($old_grade_grade->locked or $old_grade_grade->locktime)
236 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:unlock', $context))) {
237 //ignore data
238
239 } else if ((!$old_grade_grade->locked and !$old_grade_grade->locktime)
240 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:lock', $context))) {
241 //ignore data
242
243 } else {
244 $grade_grade->set_locktime($data->locktime); //set_lock may reset locktime
245 $grade_grade->set_locked($data->locked, false, true);
25bcd908 246 // reload grade in case it was regraded from activity
247 $grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true);
248 $grade_grade->grade_item =& $grade_item; // no db fetching
23207a1a 249 }
250 }
251
fb0e3570 252 if (isset($data->excluded) and has_capability('moodle/grade:manage', $context)) {
253 $grade_grade->set_excluded($data->excluded);
254 }
255
fb0e3570 256 // detect cases when we need to do full regrading
257 if ($old_grade_grade->excluded != $grade_grade->excluded) {
258 $parent = $grade_item->get_parent_category();
259 $parent->force_regrading();
20544755 260
6ef4878b 261 } else if ($old_grade_grade->overridden != $grade_grade->overridden and empty($grade_grade->overridden)) { // only when unoverridding
fb0e3570 262 $grade_item->force_regrading();
27b1735b 263
fb0e3570 264 } else if ($old_grade_grade->locktime != $grade_grade->locktime) {
265 $grade_item->force_regrading();
266 }
27b1735b 267
ffe6eb27 268 redirect($returnurl);
20544755 269}
270
20544755 271$strgrades = get_string('grades');
20544755 272$strgraderreport = get_string('graderreport', 'grades');
23207a1a 273$strgradeedit = get_string('editgrade', 'grades');
274$struser = get_string('user');
20544755 275
7cc378fd 276grade_build_nav(__FILE__, $strgradeedit, array('courseid' => $courseid));
20544755 277
278/*********** BEGIN OUTPUT *************/
7cc378fd 279$PAGE->set_title($strgrades . ': ' . $strgraderreport . ': ' . $strgradeedit);
02114f13 280$PAGE->set_heading($course->fullname);
20544755 281
7cc378fd 282echo $OUTPUT->header();
c018f973 283echo $OUTPUT->heading($strgradeedit);
20544755 284
c3b834b4 285echo $OUTPUT->box_start();
20544755 286
20544755 287// Form if in edit or add modes
288$mform->display();
20544755 289
c3b834b4 290echo $OUTPUT->box_end();
20544755 291
5a931394 292echo $OUTPUT->footer();
20544755 293die;