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