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