MDL-11543 Printing nice list of categories for parent category drop-down
[moodle.git] / grade / edit / tree / grade.php
CommitLineData
20544755 1<?php //$Id$
739196ba 2
78ad5f3f 3require_once '../../../config.php';
3af29899 4require_once $CFG->dirroot.'/grade/lib.php';
76317c73 5require_once $CFG->dirroot.'/grade/report/lib.php';
6fb39a98 6require_once 'grade_form.php';
20544755 7
8$courseid = required_param('courseid', PARAM_INT);
23207a1a 9$id = optional_param('id', 0, PARAM_INT);
10$itemid = optional_param('itemid', 0, PARAM_INT);
11$userid = optional_param('userid', 0, PARAM_INT);
20544755 12
13if (!$course = get_record('course', 'id', $courseid)) {
14 print_error('nocourseid');
15}
16
20544755 17require_login($course);
18$context = get_context_instance(CONTEXT_COURSE, $course->id);
27b1735b 19if (!has_capability('moodle/grade:manage', $context)) {
a5b8be62 20 require_capability('moodle/grade:edit', $context);
27b1735b 21}
20544755 22
23// default return url
3af29899 24$gpr = new grade_plugin_return();
25$returnurl = $gpr->get_return_url($CFG->wwwroot.'/grade/report.php?id='.$course->id);
20544755 26
23207a1a 27// security checks!
28if (!empty($id)) {
29 if (!$grade = get_record('grade_grades', 'id', $id)) {
30 error('Incorrect grade id');
31 }
32
33 if (!empty($itemid) and $itemid != $grade->itemid) {
34 error('Incorrect itemid');
35 }
36 $itemid = $grade->itemid;
37
38 if (!empty($userid) and $userid != $grade->userid) {
39 error('Incorrect userid');
40 }
41 $userid = $grade->userid;
79eabc2a 42
23207a1a 43 unset($grade);
9c25957a 44
23207a1a 45} else if (empty($userid) or empty($itemid)) {
46 error('Missing userid and itemid');
47}
48
49if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) {
50 error('Can not find grade_item');
51}
52
dc3ca94c 53// now verify grading user has access to all groups or is member of the same group when separate groups used in course
b2bc96d1 54if (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
2c386f82 55 if ($groups = groups_get_all_groups($COURSE->id, $userid)) {
dc3ca94c 56 $ok = false;
57 foreach ($groups as $group) {
58 if (groups_is_member($group->id, $USER->id)) {
59 $ok = true;
60 }
61 }
62 if (!$ok) {
63 error('Can not grade this user');
64 }
65 } else {
66 error('Can not grade this user');
67 }
68}
23207a1a 69
70$mform = new edit_grade_form(null, array('grade_item'=>$grade_item, 'gpr'=>$gpr));
71
15a51159 72if ($grade = get_record('grade_grades', 'itemid', $grade_item->id, 'userid', $userid)) {
3f2b0c8a 73
74 // always clean existing feedback - grading should not have XSS risk
75 if (can_use_html_editor()) {
76 $options = new object();
77 $options->smiley = false;
78 $options->filter = false;
79 $options->noclean = false;
80 $grade->feedback = format_text($grade->feedback, $grade->feedbackformat, $options);
81 $grade->feedbackformat = FORMAT_HTML;
82 } else {
83 $grade->feedback = clean_text($grade->feedback, $grade->feedbackformat);
920a0fb2 84 }
85
15a51159 86 $grade->locked = $grade->locked > 0 ? 1:0;
87 $grade->overridden = $grade->overridden > 0 ? 1:0;
88 $grade->excluded = $grade->excluded > 0 ? 1:0;
89
90 if ($grade->hidden > 1) {
91 $grade->hiddenuntil = $grade->hidden;
f60c61b1 92 $grade->hidden = 1;
15a51159 93 } else {
94 $grade->hiddenuntil = 0;
95 }
23207a1a 96
f60c61b1 97 if ($grade_item->is_hidden()) {
98 $grade->hidden = 1;
99 }
100
fb0e3570 101 if ($grade_item->is_locked()) {
102 $grade->locked = 1;
103 }
104
76317c73 105 // normalize the final grade value
106 if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
107 if (empty($grade->finalgrade)) {
108 $grade->finalgrade = -1;
109 } else {
110 $grade->finalgrade = (int)$grade->finalgrade;
111 }
112 } else if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
31a6c06c 113 $grade->finalgrade = format_float($grade->finalgrade, $grade_item->get_decimals());
76317c73 114 }
115
116 $grade->oldgrade = $grade->finalgrade;
117
23207a1a 118 $mform->set_data($grade);
9c25957a 119
20544755 120} else {
27b1735b 121 $mform->set_data(array('itemid'=>$itemid, 'userid'=>$userid, 'locked'=>$grade_item->locked, 'locktime'=>$grade_item->locktime));
20544755 122}
123
124if ($mform->is_cancelled()) {
125 redirect($returnurl);
23207a1a 126
20544755 127// form processing
41486d1c 128} else if ($data = $mform->get_data(false)) {
23207a1a 129 $old_grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true); //might not exist yet
920a0fb2 130
0658afc9 131 // fix no grade for scales
76317c73 132 if (!isset($data->finalgrade) or $data->finalgrade == $data->oldgrade) {
fb0e3570 133 $data->finalgrade = $old_grade_grade->finalgrade;
134
135 } else if ($grade_item->gradetype == GRADE_TYPE_SCALE and $data->finalgrade < 1) {
0658afc9 136 $data->finalgrade = NULL;
76317c73 137
138 } else if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
139 $data->finalgrade = unformat_float($data->finalgrade);
0658afc9 140 }
141
fb0e3570 142 if (!isset($data->feedback)) {
3f2b0c8a 143 $data->feedback = $old_grade_grade->feedback;
144 $data->feedbackformat = $old_grade_grade->feedbackformat;
fb0e3570 145 }
23207a1a 146 // update final grade or feedback
147 $grade_item->update_final_grade($data->userid, $data->finalgrade, NULL, 'editgrade', $data->feedback, $data->feedbackformat);
8c4d80f1 148
83d387c2 149 $grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true);
fb0e3570 150 $grade_grade->grade_item =& $grade_item; // no db fetching
8c4d80f1 151
27b1735b 152 if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:hide', $context)) {
f60c61b1 153 $hidden = empty($data->hidden) ? 0: $data->hidden;
154 $hiddenuntil = empty($data->hiddenuntil) ? 0: $data->hiddenuntil;
155
156 if ($grade_item->is_hidden()) {
157 if ($old_grade_grade->hidden == 1 and $hiddenuntil == 0) {
158 //nothing to do - grade was originally hidden, we want to keep it that way
27b1735b 159 } else {
f60c61b1 160 $grade_grade->set_hidden($hiddenuntil);
27b1735b 161 }
15a51159 162 } else {
f60c61b1 163 if ($hiddenuntil) {
164 $grade_grade->set_hidden($hiddenuntil);
165 } else {
166 $grade_grade->set_hidden($hidden); // checkbox data might be undefined
167 }
15a51159 168 }
15a51159 169 }
23207a1a 170
fb0e3570 171 if (isset($data->locked) and !$grade_item->is_locked()) {
172 if (($old_grade_grade->locked or $old_grade_grade->locktime)
173 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:unlock', $context))) {
174 //ignore data
175
176 } else if ((!$old_grade_grade->locked and !$old_grade_grade->locktime)
177 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:lock', $context))) {
178 //ignore data
179
180 } else {
181 $grade_grade->set_locktime($data->locktime); //set_lock may reset locktime
182 $grade_grade->set_locked($data->locked, false, true);
23207a1a 183 }
184 }
185
fb0e3570 186 if (isset($data->excluded) and has_capability('moodle/grade:manage', $context)) {
187 $grade_grade->set_excluded($data->excluded);
188 }
189
a5b8be62 190 if (isset($data->overridden) and has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:edit', $context)) {
fb0e3570 191 // ignore overridden flag when changing final grade
192 if ($old_grade_grade->finalgrade == $grade_grade->finalgrade) {
193 $grade_grade->set_overridden($data->overridden);
27b1735b 194 }
23207a1a 195 }
920a0fb2 196
fb0e3570 197 // detect cases when we need to do full regrading
198 if ($old_grade_grade->excluded != $grade_grade->excluded) {
199 $parent = $grade_item->get_parent_category();
200 $parent->force_regrading();
20544755 201
fb0e3570 202 } else if ($old_grade_grade->overridden != $grade_grade->overridden and empty($grade_grade->overridden)) { // only when unoverriding
203 $grade_item->force_regrading();
27b1735b 204
fb0e3570 205 } else if ($old_grade_grade->locktime != $grade_grade->locktime) {
206 $grade_item->force_regrading();
207 }
27b1735b 208
ffe6eb27 209 redirect($returnurl);
20544755 210}
211
20544755 212$strgrades = get_string('grades');
20544755 213$strgraderreport = get_string('graderreport', 'grades');
23207a1a 214$strgradeedit = get_string('editgrade', 'grades');
215$struser = get_string('user');
20544755 216
826c5f86 217$navigation = grade_build_nav(__FILE__, $strgradeedit, array('courseid' => $courseid));
20544755 218
219/*********** BEGIN OUTPUT *************/
220
23207a1a 221print_header_simple($strgrades . ': ' . $strgraderreport . ': ' . $strgradeedit,
222 ': ' . $strgradeedit , $navigation, '', '', true, '', navmenu($course));
20544755 223
23207a1a 224print_heading($strgradeedit);
20544755 225
226print_simple_box_start("center");
227
20544755 228// Form if in edit or add modes
229$mform->display();
20544755 230
231print_simple_box_end();
232
233print_footer($course);
234die;