MDL-12572 Adding storage of database search in $SESSION
[moodle.git] / grade / edit / tree / grade.php
CommitLineData
20544755 1<?php //$Id$
739196ba 2
8ad36f4c 3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
78ad5f3f 26require_once '../../../config.php';
3af29899 27require_once $CFG->dirroot.'/grade/lib.php';
76317c73 28require_once $CFG->dirroot.'/grade/report/lib.php';
6fb39a98 29require_once 'grade_form.php';
20544755 30
31$courseid = required_param('courseid', PARAM_INT);
23207a1a 32$id = optional_param('id', 0, PARAM_INT);
33$itemid = optional_param('itemid', 0, PARAM_INT);
34$userid = optional_param('userid', 0, PARAM_INT);
20544755 35
36if (!$course = get_record('course', 'id', $courseid)) {
37 print_error('nocourseid');
38}
39
20544755 40require_login($course);
41$context = get_context_instance(CONTEXT_COURSE, $course->id);
27b1735b 42if (!has_capability('moodle/grade:manage', $context)) {
a5b8be62 43 require_capability('moodle/grade:edit', $context);
27b1735b 44}
20544755 45
46// default return url
3af29899 47$gpr = new grade_plugin_return();
48$returnurl = $gpr->get_return_url($CFG->wwwroot.'/grade/report.php?id='.$course->id);
20544755 49
23207a1a 50// security checks!
51if (!empty($id)) {
52 if (!$grade = get_record('grade_grades', 'id', $id)) {
53 error('Incorrect grade id');
54 }
55
56 if (!empty($itemid) and $itemid != $grade->itemid) {
57 error('Incorrect itemid');
58 }
59 $itemid = $grade->itemid;
60
61 if (!empty($userid) and $userid != $grade->userid) {
62 error('Incorrect userid');
63 }
64 $userid = $grade->userid;
79eabc2a 65
23207a1a 66 unset($grade);
9c25957a 67
23207a1a 68} else if (empty($userid) or empty($itemid)) {
69 error('Missing userid and itemid');
70}
71
72if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) {
73 error('Can not find grade_item');
74}
75
dc3ca94c 76// now verify grading user has access to all groups or is member of the same group when separate groups used in course
b2bc96d1 77if (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
2c386f82 78 if ($groups = groups_get_all_groups($COURSE->id, $userid)) {
dc3ca94c 79 $ok = false;
80 foreach ($groups as $group) {
81 if (groups_is_member($group->id, $USER->id)) {
82 $ok = true;
83 }
84 }
85 if (!$ok) {
86 error('Can not grade this user');
87 }
88 } else {
89 error('Can not grade this user');
90 }
91}
23207a1a 92
93$mform = new edit_grade_form(null, array('grade_item'=>$grade_item, 'gpr'=>$gpr));
94
15a51159 95if ($grade = get_record('grade_grades', 'itemid', $grade_item->id, 'userid', $userid)) {
3f2b0c8a 96
97 // always clean existing feedback - grading should not have XSS risk
98 if (can_use_html_editor()) {
0f392ff4 99 if (empty($grade->feedback)) {
100 $grade->feedback = '';
101 } else {
102 $options = new object();
103 $options->smiley = false;
104 $options->filter = false;
105 $options->noclean = false;
106 $grade->feedback = format_text($grade->feedback, $grade->feedbackformat, $options);
107 }
3f2b0c8a 108 $grade->feedbackformat = FORMAT_HTML;
109 } else {
110 $grade->feedback = clean_text($grade->feedback, $grade->feedbackformat);
920a0fb2 111 }
112
15a51159 113 $grade->locked = $grade->locked > 0 ? 1:0;
114 $grade->overridden = $grade->overridden > 0 ? 1:0;
115 $grade->excluded = $grade->excluded > 0 ? 1:0;
116
117 if ($grade->hidden > 1) {
118 $grade->hiddenuntil = $grade->hidden;
f60c61b1 119 $grade->hidden = 1;
15a51159 120 } else {
121 $grade->hiddenuntil = 0;
122 }
23207a1a 123
f60c61b1 124 if ($grade_item->is_hidden()) {
125 $grade->hidden = 1;
126 }
127
fb0e3570 128 if ($grade_item->is_locked()) {
129 $grade->locked = 1;
130 }
131
76317c73 132 // normalize the final grade value
133 if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
134 if (empty($grade->finalgrade)) {
135 $grade->finalgrade = -1;
136 } else {
137 $grade->finalgrade = (int)$grade->finalgrade;
138 }
139 } else if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
31a6c06c 140 $grade->finalgrade = format_float($grade->finalgrade, $grade_item->get_decimals());
76317c73 141 }
142
0f392ff4 143 $grade->oldgrade = $grade->finalgrade;
144 $grade->oldfeedback = $grade->feedback;
76317c73 145
23207a1a 146 $mform->set_data($grade);
9c25957a 147
20544755 148} else {
27b1735b 149 $mform->set_data(array('itemid'=>$itemid, 'userid'=>$userid, 'locked'=>$grade_item->locked, 'locktime'=>$grade_item->locktime));
20544755 150}
151
152if ($mform->is_cancelled()) {
153 redirect($returnurl);
23207a1a 154
20544755 155// form processing
41486d1c 156} else if ($data = $mform->get_data(false)) {
23207a1a 157 $old_grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true); //might not exist yet
920a0fb2 158
0f392ff4 159 if (!isset($data->overridden)) {
160 $data->overridden = 0; // checkbox
161 }
162
0658afc9 163 // fix no grade for scales
0f392ff4 164 if (($grade_item->is_overridable_item() and !$data->overridden) or !isset($data->finalgrade) or $data->finalgrade == $data->oldgrade) {
fb0e3570 165 $data->finalgrade = $old_grade_grade->finalgrade;
166
167 } else if ($grade_item->gradetype == GRADE_TYPE_SCALE and $data->finalgrade < 1) {
0658afc9 168 $data->finalgrade = NULL;
76317c73 169
170 } else if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
171 $data->finalgrade = unformat_float($data->finalgrade);
0658afc9 172 }
173
0f392ff4 174 // the overriding of feedback is tricky - we have to care about external items only
175 if (!array_key_exists('feedback', $data) or $data->feedback == $data->oldfeedback) {
3f2b0c8a 176 $data->feedback = $old_grade_grade->feedback;
177 $data->feedbackformat = $old_grade_grade->feedbackformat;
fb0e3570 178 }
23207a1a 179 // update final grade or feedback
0f392ff4 180 $grade_item->update_final_grade($data->userid, $data->finalgrade, 'editgrade', $data->feedback, $data->feedbackformat);
8c4d80f1 181
83d387c2 182 $grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true);
fb0e3570 183 $grade_grade->grade_item =& $grade_item; // no db fetching
8c4d80f1 184
0f392ff4 185 if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:edit', $context)) {
186 $grade_grade->set_overridden($data->overridden);
187 }
188
27b1735b 189 if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:hide', $context)) {
f60c61b1 190 $hidden = empty($data->hidden) ? 0: $data->hidden;
191 $hiddenuntil = empty($data->hiddenuntil) ? 0: $data->hiddenuntil;
192
193 if ($grade_item->is_hidden()) {
194 if ($old_grade_grade->hidden == 1 and $hiddenuntil == 0) {
195 //nothing to do - grade was originally hidden, we want to keep it that way
27b1735b 196 } else {
f60c61b1 197 $grade_grade->set_hidden($hiddenuntil);
27b1735b 198 }
15a51159 199 } else {
f60c61b1 200 if ($hiddenuntil) {
201 $grade_grade->set_hidden($hiddenuntil);
202 } else {
203 $grade_grade->set_hidden($hidden); // checkbox data might be undefined
204 }
15a51159 205 }
15a51159 206 }
23207a1a 207
fb0e3570 208 if (isset($data->locked) and !$grade_item->is_locked()) {
209 if (($old_grade_grade->locked or $old_grade_grade->locktime)
210 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:unlock', $context))) {
211 //ignore data
212
213 } else if ((!$old_grade_grade->locked and !$old_grade_grade->locktime)
214 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:lock', $context))) {
215 //ignore data
216
217 } else {
218 $grade_grade->set_locktime($data->locktime); //set_lock may reset locktime
219 $grade_grade->set_locked($data->locked, false, true);
25bcd908 220 // reload grade in case it was regraded from activity
221 $grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true);
222 $grade_grade->grade_item =& $grade_item; // no db fetching
23207a1a 223 }
224 }
225
fb0e3570 226 if (isset($data->excluded) and has_capability('moodle/grade:manage', $context)) {
227 $grade_grade->set_excluded($data->excluded);
228 }
229
fb0e3570 230 // detect cases when we need to do full regrading
231 if ($old_grade_grade->excluded != $grade_grade->excluded) {
232 $parent = $grade_item->get_parent_category();
233 $parent->force_regrading();
20544755 234
fb0e3570 235 } else if ($old_grade_grade->overridden != $grade_grade->overridden and empty($grade_grade->overridden)) { // only when unoverriding
236 $grade_item->force_regrading();
27b1735b 237
fb0e3570 238 } else if ($old_grade_grade->locktime != $grade_grade->locktime) {
239 $grade_item->force_regrading();
240 }
27b1735b 241
ffe6eb27 242 redirect($returnurl);
20544755 243}
244
20544755 245$strgrades = get_string('grades');
20544755 246$strgraderreport = get_string('graderreport', 'grades');
23207a1a 247$strgradeedit = get_string('editgrade', 'grades');
248$struser = get_string('user');
20544755 249
826c5f86 250$navigation = grade_build_nav(__FILE__, $strgradeedit, array('courseid' => $courseid));
20544755 251
252/*********** BEGIN OUTPUT *************/
253
23207a1a 254print_header_simple($strgrades . ': ' . $strgraderreport . ': ' . $strgradeedit,
255 ': ' . $strgradeedit , $navigation, '', '', true, '', navmenu($course));
20544755 256
23207a1a 257print_heading($strgradeedit);
20544755 258
259print_simple_box_start("center");
260
20544755 261// Form if in edit or add modes
262$mform->display();
20544755 263
264print_simple_box_end();
265
266print_footer($course);
267die;