MDL-19404 Getting default values for grade_item object when creating a new grade_cate...
[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
5c75a0a3 36if (!$course = $DB->get_record('course', array('id' => $courseid))) {
20544755 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)) {
5c75a0a3 52 if (!$grade = $DB->get_record('grade_grades', array('id' => $id))) {
1bb0aa0f 53 print_error('invalidgroupid');
23207a1a 54 }
55
56 if (!empty($itemid) and $itemid != $grade->itemid) {
1bb0aa0f 57 print_error('invaliditemid');
23207a1a 58 }
59 $itemid = $grade->itemid;
60
61 if (!empty($userid) and $userid != $grade->userid) {
1bb0aa0f 62 print_error('invaliduser');
23207a1a 63 }
64 $userid = $grade->userid;
79eabc2a 65
23207a1a 66 unset($grade);
9c25957a 67
23207a1a 68} else if (empty($userid) or empty($itemid)) {
1bb0aa0f 69 print_error('missinguseranditemid');
23207a1a 70}
71
72if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$courseid))) {
5db2027e 73 print_error('cannotfindgradeitem');
23207a1a 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) {
1bb0aa0f 86 print_error('cannotgradeuser');
dc3ca94c 87 }
88 } else {
1bb0aa0f 89 print_error('cannotgradeuser');
dc3ca94c 90 }
91}
23207a1a 92
93$mform = new edit_grade_form(null, array('grade_item'=>$grade_item, 'gpr'=>$gpr));
94
5c75a0a3 95if ($grade = $DB->get_record('grade_grades', array('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
653a8648 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
0658afc9 159 // fix no grade for scales
5048575d 160 if (!isset($data->finalgrade) or $data->finalgrade == $data->oldgrade) {
fb0e3570 161 $data->finalgrade = $old_grade_grade->finalgrade;
162
5048575d 163 } else if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
164 if ($data->finalgrade < 1) {
165 $data->finalgrade = NULL;
166 }
76317c73 167
168 } else if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
169 $data->finalgrade = unformat_float($data->finalgrade);
5048575d 170
171 } else {
172 //this shoul not happen
173 $data->finalgrade = $old_grade_grade->finalgrade;
0658afc9 174 }
175
0f392ff4 176 // the overriding of feedback is tricky - we have to care about external items only
177 if (!array_key_exists('feedback', $data) or $data->feedback == $data->oldfeedback) {
3f2b0c8a 178 $data->feedback = $old_grade_grade->feedback;
179 $data->feedbackformat = $old_grade_grade->feedbackformat;
fb0e3570 180 }
23207a1a 181 // update final grade or feedback
0f392ff4 182 $grade_item->update_final_grade($data->userid, $data->finalgrade, 'editgrade', $data->feedback, $data->feedbackformat);
8c4d80f1 183
83d387c2 184 $grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true);
fb0e3570 185 $grade_grade->grade_item =& $grade_item; // no db fetching
8c4d80f1 186
0f392ff4 187 if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:edit', $context)) {
5048575d 188 if (!grade_floats_different($data->finalgrade, $old_grade_grade->finalgrade)
189 and $data->feedback === $old_grade_grade->feedback) {
190 // change overridden flag only if grade or feedback not changed
191 if (!isset($data->overridden)) {
192 $data->overridden = 0; // checkbox
193 }
194 $grade_grade->set_overridden($data->overridden);
195 }
0f392ff4 196 }
197
27b1735b 198 if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:hide', $context)) {
f60c61b1 199 $hidden = empty($data->hidden) ? 0: $data->hidden;
200 $hiddenuntil = empty($data->hiddenuntil) ? 0: $data->hiddenuntil;
201
202 if ($grade_item->is_hidden()) {
203 if ($old_grade_grade->hidden == 1 and $hiddenuntil == 0) {
204 //nothing to do - grade was originally hidden, we want to keep it that way
27b1735b 205 } else {
f60c61b1 206 $grade_grade->set_hidden($hiddenuntil);
27b1735b 207 }
15a51159 208 } else {
f60c61b1 209 if ($hiddenuntil) {
210 $grade_grade->set_hidden($hiddenuntil);
211 } else {
212 $grade_grade->set_hidden($hidden); // checkbox data might be undefined
213 }
15a51159 214 }
15a51159 215 }
23207a1a 216
fb0e3570 217 if (isset($data->locked) and !$grade_item->is_locked()) {
218 if (($old_grade_grade->locked or $old_grade_grade->locktime)
219 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:unlock', $context))) {
220 //ignore data
221
222 } else if ((!$old_grade_grade->locked and !$old_grade_grade->locktime)
223 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:lock', $context))) {
224 //ignore data
225
226 } else {
227 $grade_grade->set_locktime($data->locktime); //set_lock may reset locktime
228 $grade_grade->set_locked($data->locked, false, true);
25bcd908 229 // reload grade in case it was regraded from activity
230 $grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true);
231 $grade_grade->grade_item =& $grade_item; // no db fetching
23207a1a 232 }
233 }
234
fb0e3570 235 if (isset($data->excluded) and has_capability('moodle/grade:manage', $context)) {
236 $grade_grade->set_excluded($data->excluded);
237 }
238
fb0e3570 239 // detect cases when we need to do full regrading
240 if ($old_grade_grade->excluded != $grade_grade->excluded) {
241 $parent = $grade_item->get_parent_category();
242 $parent->force_regrading();
20544755 243
fb0e3570 244 } else if ($old_grade_grade->overridden != $grade_grade->overridden and empty($grade_grade->overridden)) { // only when unoverriding
245 $grade_item->force_regrading();
27b1735b 246
fb0e3570 247 } else if ($old_grade_grade->locktime != $grade_grade->locktime) {
248 $grade_item->force_regrading();
249 }
27b1735b 250
ffe6eb27 251 redirect($returnurl);
20544755 252}
253
20544755 254$strgrades = get_string('grades');
20544755 255$strgraderreport = get_string('graderreport', 'grades');
23207a1a 256$strgradeedit = get_string('editgrade', 'grades');
257$struser = get_string('user');
20544755 258
826c5f86 259$navigation = grade_build_nav(__FILE__, $strgradeedit, array('courseid' => $courseid));
20544755 260
261/*********** BEGIN OUTPUT *************/
262
23207a1a 263print_header_simple($strgrades . ': ' . $strgraderreport . ': ' . $strgradeedit,
264 ': ' . $strgradeedit , $navigation, '', '', true, '', navmenu($course));
20544755 265
23207a1a 266print_heading($strgradeedit);
20544755 267
268print_simple_box_start("center");
269
20544755 270// Form if in edit or add modes
271$mform->display();
20544755 272
273print_simple_box_end();
274
275print_footer($course);
276die;