MDL-68284 gradebook: Prevent exposing hidden quiz grade item
[moodle.git] / grade / edit / tree / item_form.php
CommitLineData
e060e33d 1<?php
e060e33d 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
8ad36f4c 16
a153c9f2
AD
17/**
18 * A moodleform allowing the editing of the grade options for an individual grade item
19 *
20 * @package core_grades
21 * @copyright 2007 Petr Skoda
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
bfebaf64
MD
25if (!defined('MOODLE_INTERNAL')) {
26 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
27}
28
8d272708 29require_once $CFG->libdir.'/formslib.php';
30
db313fec 31class edit_item_form extends moodleform {
5c75a0a3 32 private $displayoptions;
fa234b67 33
db313fec 34 function definition() {
5c75a0a3 35 global $COURSE, $CFG, $DB;
8d272708 36
db313fec 37 $mform =& $this->_form;
8d272708 38
d9be236f 39 $item = $this->_customdata['current'];
40
8d272708 41/// visible elements
42 $mform->addElement('header', 'general', get_string('gradeitem', 'grades'));
43
db313fec 44 $mform->addElement('text', 'itemname', get_string('itemname', 'grades'));
45a9f90a 45 $mform->setType('itemname', PARAM_TEXT);
db313fec 46 $mform->addElement('text', 'iteminfo', get_string('iteminfo', 'grades'));
d75817e1 47 $mform->addHelpButton('iteminfo', 'iteminfo', 'grades');
45a9f90a 48 $mform->setType('iteminfo', PARAM_TEXT);
0605ad50 49
d343bd21 50 $mform->addElement('text', 'idnumber', get_string('idnumbermod'));
da6fe848 51 $mform->addHelpButton('idnumber', 'idnumbermod');
45a9f90a 52 $mform->setType('idnumber', PARAM_RAW);
8d272708 53
664d8be7
MN
54 if (!empty($item->id)) {
55 $gradeitem = new grade_item(array('id' => $item->id, 'courseid' => $item->courseid));
56 // If grades exist set a message so the user knows why they can not alter the grade type or scale.
57 // We could never change the grade type for external items, so only need to show this for manual grade items.
58 if ($gradeitem->has_grades() && !$gradeitem->is_external_item()) {
59 // Set a message so the user knows why they can not alter the grade type or scale.
60 if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
61 $gradesexistmsg = get_string('modgradecantchangegradetyporscalemsg', 'grades');
62 } else {
63 $gradesexistmsg = get_string('modgradecantchangegradetypemsg', 'grades');
64 }
65
66 $gradesexisthtml = '<div class=\'alert\'>' . $gradesexistmsg . '</div>';
67 $mform->addElement('static', 'gradesexistmsg', '', $gradesexisthtml);
68 }
69 }
70
93dbb218
AD
71 // Manual grade items cannot have grade type GRADE_TYPE_NONE.
72 $options = array(GRADE_TYPE_VALUE => get_string('typevalue', 'grades'),
73 GRADE_TYPE_SCALE => get_string('typescale', 'grades'),
74 GRADE_TYPE_TEXT => get_string('typetext', 'grades'));
acdc8e8a 75
8d272708 76 $mform->addElement('select', 'gradetype', get_string('gradetype', 'grades'), $options);
6cb9d6e2 77 $mform->addHelpButton('gradetype', 'gradetype', 'grades');
8d272708 78 $mform->setDefault('gradetype', GRADE_TYPE_VALUE);
79
e754b3e0 80 //$mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
81 //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_TEXT);
82 //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_NONE);
8d272708 83
84 $options = array(0=>get_string('usenoscale', 'grades'));
d9be236f 85 if ($scales = grade_scale::fetch_all_local($COURSE->id)) {
86 foreach ($scales as $scale) {
87 $options[$scale->id] = $scale->get_name();
88 }
89 }
90 if ($scales = grade_scale::fetch_all_global()) {
8d272708 91 foreach ($scales as $scale) {
d9be236f 92 $options[$scale->id] = $scale->get_name();
93 }
94 }
6ef4878b 95 // ugly BC hack - it was possible to use custom scale from other courses :-(
d9be236f 96 if (!empty($item->scaleid) and !isset($options[$item->scaleid])) {
97 if ($scale = grade_scale::fetch(array('id'=>$item->scaleid))) {
98 $options[$scale->id] = $scale->get_name().get_string('incorrectcustomscale', 'grades');
8d272708 99 }
100 }
101 $mform->addElement('select', 'scaleid', get_string('scale'), $options);
b2bf67b4 102 $mform->addHelpButton('scaleid', 'typescale', 'grades');
3f7ebaf5 103 $mform->disabledIf('scaleid', 'gradetype', 'noteq', GRADE_TYPE_SCALE);
8d272708 104
664d8be7
MN
105 $choices = array();
106 $choices[''] = get_string('choose');
107 $choices['no'] = get_string('no');
108 $choices['yes'] = get_string('yes');
109 $mform->addElement('select', 'rescalegrades', get_string('modgraderescalegrades', 'grades'), $choices);
110 $mform->addHelpButton('rescalegrades', 'modgraderescalegrades', 'grades');
111 $mform->disabledIf('rescalegrades', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
112
db313fec 113 $mform->addElement('text', 'grademax', get_string('grademax', 'grades'));
f5f4bbab 114 $mform->addHelpButton('grademax', 'grademax', 'grades');
3f7ebaf5 115 $mform->disabledIf('grademax', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
45a9f90a 116 $mform->setType('grademax', PARAM_RAW);
8d272708 117
0e999796
AD
118 if ((bool) get_config('moodle', 'grade_report_showmin')) {
119 $mform->addElement('text', 'grademin', get_string('grademin', 'grades'));
120 $mform->addHelpButton('grademin', 'grademin', 'grades');
121 $mform->disabledIf('grademin', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
122 $mform->setType('grademin', PARAM_RAW);
123 }
8d272708 124
db313fec 125 $mform->addElement('text', 'gradepass', get_string('gradepass', 'grades'));
5cca21a6 126 $mform->addHelpButton('gradepass', 'gradepass', 'grades');
8d272708 127 $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_NONE);
3f7ebaf5 128 $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_TEXT);
45a9f90a 129 $mform->setType('gradepass', PARAM_RAW);
8d272708 130
db313fec 131 $mform->addElement('text', 'multfactor', get_string('multfactor', 'grades'));
d4a11ee5 132 $mform->addHelpButton('multfactor', 'multfactor', 'grades');
9c8d38fa 133 $mform->setAdvanced('multfactor');
8d272708 134 $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
3f7ebaf5 135 $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT);
45a9f90a 136 $mform->setType('multfactor', PARAM_RAW);
8d272708 137
db313fec 138 $mform->addElement('text', 'plusfactor', get_string('plusfactor', 'grades'));
12b0938c 139 $mform->addHelpButton('plusfactor', 'plusfactor', 'grades');
9c8d38fa 140 $mform->setAdvanced('plusfactor');
8d272708 141 $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
3f7ebaf5 142 $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT);
45a9f90a 143 $mform->setType('plusfactor', PARAM_RAW);
9580a21f 144
fa234b67 145 /// grade display prefs
e0724506 146 $default_gradedisplaytype = grade_get_setting($COURSE->id, 'displaytype', $CFG->grade_displaytype);
7d10995c 147 $options = array(GRADE_DISPLAY_TYPE_DEFAULT => get_string('default', 'grades'),
148 GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'),
149 GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
150 GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades'),
151 GRADE_DISPLAY_TYPE_REAL_PERCENTAGE => get_string('realpercentage', 'grades'),
152 GRADE_DISPLAY_TYPE_REAL_LETTER => get_string('realletter', 'grades'),
153 GRADE_DISPLAY_TYPE_LETTER_REAL => get_string('letterreal', 'grades'),
154 GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE => get_string('letterpercentage', 'grades'),
155 GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER => get_string('percentageletter', 'grades'),
156 GRADE_DISPLAY_TYPE_PERCENTAGE_REAL => get_string('percentagereal', 'grades')
157 );
158
159 asort($options);
160
e0724506 161 foreach ($options as $key=>$option) {
162 if ($key == $default_gradedisplaytype) {
163 $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option);
164 break;
165 }
166 }
167 $mform->addElement('select', 'display', get_string('gradedisplaytype', 'grades'), $options);
56f9c6ce 168 $mform->addHelpButton('display', 'gradedisplaytype', 'grades');
65553428 169 $mform->disabledIf('display', 'gradetype', 'eq', GRADE_TYPE_TEXT);
ac5b0a49 170
e0724506 171 $default_gradedecimals = grade_get_setting($COURSE->id, 'decimalpoints', $CFG->grade_decimalpoints);
172 $options = array(-1=>get_string('defaultprev', 'grades', $default_gradedecimals), 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
173 $mform->addElement('select', 'decimals', get_string('decimalpoints', 'grades'), $options);
1155b8db 174 $mform->addHelpButton('decimals', 'decimalpoints', 'grades');
e0724506 175 $mform->setDefault('decimals', -1);
fa234b67 176 $mform->disabledIf('decimals', 'display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
e0724506 177 if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
178 $mform->disabledIf('decimals', 'display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
179 }
65553428 180 $mform->disabledIf('decimals', 'gradetype', 'eq', GRADE_TYPE_TEXT);
31a6c06c 181
15a51159 182 /// hiding
455dc0de 183 if ($item->cancontrolvisibility) {
1ebbd18b 184 $mform->addElement('advcheckbox', 'hidden', get_string('hidden', 'grades'), '', [], [0, 1]);
455dc0de 185 $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
1ebbd18b 186 $mform->disabledIf('hidden', 'hiddenuntil[enabled]', 'checked');
455dc0de
FM
187 } else {
188 $mform->addElement('static', 'hidden', get_string('hidden', 'grades'),
189 get_string('componentcontrolsvisibility', 'grades'));
190 // Unset hidden to avoid data override.
191 unset($item->hidden);
192 }
d7a04343 193 $mform->addHelpButton('hidden', 'hidden', 'grades');
15a51159 194
195 /// locking
c86caae7 196 $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
731bc4cc 197 $mform->addHelpButton('locked', 'locked', 'grades');
db313fec 198
199 $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
8d272708 200 $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE);
db313fec 201
e2eb2edb 202/// parent category related settings
203 $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
204
044a298e
JO
205 $mform->addElement('advcheckbox', 'weightoverride', get_string('adjustedweight', 'grades'));
206 $mform->addHelpButton('weightoverride', 'weightoverride', 'grades');
f8c8f980
FM
207 $mform->disabledIf('weightoverride', 'gradetype', 'eq', GRADE_TYPE_NONE);
208 $mform->disabledIf('weightoverride', 'gradetype', 'eq', GRADE_TYPE_TEXT);
044a298e
JO
209
210 $mform->addElement('text', 'aggregationcoef2', get_string('weight', 'grades'));
211 $mform->addHelpButton('aggregationcoef2', 'weight', 'grades');
212 $mform->setType('aggregationcoef2', PARAM_RAW);
213 $mform->disabledIf('aggregationcoef2', 'weightoverride');
f8c8f980
FM
214 $mform->disabledIf('aggregationcoef2', 'gradetype', 'eq', GRADE_TYPE_NONE);
215 $mform->disabledIf('aggregationcoef2', 'gradetype', 'eq', GRADE_TYPE_TEXT);
044a298e 216
e2eb2edb 217 $options = array();
e2eb2edb 218 $coefstring = '';
219 $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
4dc81cc7 220
e2eb2edb 221 foreach ($categories as $cat) {
222 $cat->apply_forced_settings();
223 $options[$cat->id] = $cat->get_name();
e2eb2edb 224 }
225
226 if (count($categories) > 1) {
61ac129b 227 $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options);
e2eb2edb 228 }
229
8d272708 230/// hidden params
db313fec 231 $mform->addElement('hidden', 'id', 0);
232 $mform->setType('id', PARAM_INT);
233
9580a21f 234 $mform->addElement('hidden', 'courseid', $COURSE->id);
db313fec 235 $mform->setType('courseid', PARAM_INT);
236
9580a21f 237 $mform->addElement('hidden', 'itemtype', 'manual'); // all new items are manual only
db313fec 238 $mform->setType('itemtype', PARAM_ALPHA);
239
3af29899 240/// add return tracking info
241 $gpr = $this->_customdata['gpr'];
242 $gpr->add_mform_elements($mform);
243
3d5c00b3 244/// mark advanced according to site settings
245 if (isset($CFG->grade_item_advanced)) {
246 $advanced = explode(',', $CFG->grade_item_advanced);
247 foreach ($advanced as $el) {
248 if ($mform->elementExists($el)) {
249 $mform->setAdvanced($el);
250 }
251 }
252 }
db313fec 253//-------------------------------------------------------------------------------
254 // buttons
255 $this->add_action_buttons();
d9be236f 256//-------------------------------------------------------------------------------
257 $this->set_data($item);
db313fec 258 }
8d272708 259
260
261/// tweak the form - depending on existing data
262 function definition_after_data() {
9580a21f 263 global $CFG, $COURSE;
8d272708 264
265 $mform =& $this->_form;
266
267 if ($id = $mform->getElementValue('id')) {
d55c1ef2
MM
268 $gradeitem = grade_item::fetch(array('id' => $id));
269 $parentcategory = $gradeitem->get_parent_category();
270 } else {
271 // If we do not have an id, we are creating a new grade item.
272 $gradeitem = new grade_item(array('courseid' => $COURSE->id, 'itemtype' => 'manual'), false);
273
274 // Assign the course category to this grade item.
275 $parentcategory = grade_category::fetch_course_category($COURSE->id);
276 $gradeitem->parent_category = $parentcategory;
277 }
278
279 if (!$gradeitem->is_raw_used()) {
280 $mform->removeElement('plusfactor');
281 $mform->removeElement('multfactor');
282 }
9580a21f 283
d55c1ef2
MM
284 if ($gradeitem->is_outcome_item()) {
285 // We have to prevent incompatible modifications of outcomes if outcomes disabled.
286 $mform->removeElement('grademax');
287 if ($mform->elementExists('grademin')) {
288 $mform->removeElement('grademin');
9c8d38fa 289 }
d55c1ef2
MM
290 $mform->removeElement('gradetype');
291 $mform->removeElement('display');
292 $mform->removeElement('decimals');
293 $mform->hardFreeze('scaleid');
9c8d38fa 294
d55c1ef2
MM
295 } else {
296 if ($gradeitem->is_external_item()) {
297 // Following items are set up from modules and should not be overrided by user.
0e999796 298 if ($mform->elementExists('grademin')) {
d55c1ef2
MM
299 // The site setting grade_report_showmin may have prevented grademin being added to the form.
300 $mform->hardFreeze('grademin');
301 }
302 $mform->hardFreeze('itemname,gradetype,grademax,scaleid');
303 if ($gradeitem->itemnumber == 0) {
304 // The idnumber of grade itemnumber 0 is synced with course_modules.
305 $mform->hardFreeze('idnumber');
0e999796 306 }
9127bc5d 307
d55c1ef2
MM
308 // For external items we can not change the grade type, even if no grades exist, so if it is set to
309 // scale, then remove the grademax and grademin fields from the form - no point displaying them.
310 if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
311 $mform->removeElement('grademax');
0e999796 312 if ($mform->elementExists('grademin')) {
d55c1ef2 313 $mform->removeElement('grademin');
3d83539c 314 }
d55c1ef2
MM
315 } else { // Not using scale, so remove it.
316 $mform->removeElement('scaleid');
317 }
664d8be7 318
d55c1ef2
MM
319 // Always remove the rescale grades element if it's an external item.
320 $mform->removeElement('rescalegrades');
321 } else if ($gradeitem->has_grades()) {
322 // Can't change the grade type or the scale if there are grades.
323 $mform->hardFreeze('gradetype, scaleid');
664d8be7 324
d55c1ef2
MM
325 // If we are using scales then remove the unnecessary rescale and grade fields.
326 if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
664d8be7 327 $mform->removeElement('rescalegrades');
d55c1ef2
MM
328 $mform->removeElement('grademax');
329 if ($mform->elementExists('grademin')) {
330 $mform->removeElement('grademin');
664d8be7 331 }
d55c1ef2
MM
332 } else { // Remove the scale field.
333 $mform->removeElement('scaleid');
334 // Set the maximum grade to disabled unless a grade is chosen.
335 $mform->disabledIf('grademax', 'rescalegrades', 'eq', '');
9127bc5d 336 }
d55c1ef2
MM
337 } else {
338 // Remove the rescale element if there are no grades.
339 $mform->removeElement('rescalegrades');
9580a21f 340 }
d55c1ef2
MM
341 }
342
343 // If we wanted to change parent of existing item - we would have to verify there are no circular references in parents!!!
344 if ($id && $mform->elementExists('parentcategory')) {
345 $mform->hardFreeze('parentcategory');
346 }
347
348 $parentcategory->apply_forced_settings();
0758a08e 349
d55c1ef2
MM
350 if (!$parentcategory->is_aggregationcoef_used()) {
351 if ($mform->elementExists('aggregationcoef')) {
352 $mform->removeElement('aggregationcoef');
147cd91c 353 }
354
d55c1ef2
MM
355 } else {
356 $coefstring = $gradeitem->get_coefstring();
147cd91c 357
d55c1ef2
MM
358 if ($coefstring !== '') {
359 if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') {
360 // The advcheckbox is not compatible with disabledIf!
361 $coefstring = 'aggregationcoefextrasum';
362 $element =& $mform->createElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades'));
363 } else {
364 $element =& $mform->createElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
e2eb2edb 365 }
d55c1ef2
MM
366 if ($mform->elementExists('parentcategory')) {
367 $mform->insertElementBefore($element, 'parentcategory');
368 } else {
369 $mform->insertElementBefore($element, 'id');
9580a21f 370 }
d55c1ef2 371 $mform->addHelpButton('aggregationcoef', $coefstring, 'grades');
9580a21f 372 }
d55c1ef2
MM
373 $mform->disabledIf('aggregationcoef', 'gradetype', 'eq', GRADE_TYPE_NONE);
374 $mform->disabledIf('aggregationcoef', 'gradetype', 'eq', GRADE_TYPE_TEXT);
375 $mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $parentcategory->id);
376 }
9580a21f 377
d55c1ef2
MM
378 // Remove fields used by natural weighting if the parent category is not using natural weighting.
379 // Or if the item is a scale and scales are not used in aggregation.
380 if ($parentcategory->aggregation != GRADE_AGGREGATE_SUM
381 || (empty($CFG->grade_includescalesinaggregation) && $gradeitem->gradetype == GRADE_TYPE_SCALE)) {
382 if ($mform->elementExists('weightoverride')) {
383 $mform->removeElement('weightoverride');
64b03e5b 384 }
d55c1ef2
MM
385 if ($mform->elementExists('aggregationcoef2')) {
386 $mform->removeElement('aggregationcoef2');
387 }
388 }
64b03e5b 389
d55c1ef2
MM
390 if ($category = $gradeitem->get_item_category()) {
391 if ($category->aggregation == GRADE_AGGREGATE_SUM) {
392 if ($mform->elementExists('gradetype')) {
393 $mform->hardFreeze('gradetype');
394 }
395 if ($mform->elementExists('grademin')) {
396 $mform->hardFreeze('grademin');
397 }
398 if ($mform->elementExists('grademax')) {
399 $mform->hardFreeze('grademax');
400 }
401 if ($mform->elementExists('scaleid')) {
402 $mform->removeElement('scaleid');
0758a08e 403 }
404 }
e2eb2edb 405 }
9580a21f 406
e2eb2edb 407 // no parent header for course category
408 if (!$mform->elementExists('aggregationcoef') and !$mform->elementExists('parentcategory')) {
409 $mform->removeElement('headerparent');
8d272708 410 }
411 }
412
8d272708 413/// perform extra validation before submission
a78890d5 414 function validation($data, $files) {
204175c5 415 global $COURSE;
d629c601
DW
416 $grade_item = false;
417 if ($data['id']) {
418 $grade_item = new grade_item(array('id' => $data['id'], 'courseid' => $data['courseid']));
419 }
204175c5 420
a78890d5 421 $errors = parent::validation($data, $files);
60243313 422
423 if (array_key_exists('idnumber', $data)) {
d629c601 424 if ($grade_item) {
60243313 425 if ($grade_item->itemtype == 'mod') {
426 $cm = get_coursemodule_from_instance($grade_item->itemmodule, $grade_item->iteminstance, $grade_item->courseid);
427 } else {
428 $cm = null;
429 }
430 } else {
431 $grade_item = null;
432 $cm = null;
433 }
204175c5 434 if (!grade_verify_idnumber($data['idnumber'], $COURSE->id, $grade_item, $cm)) {
60243313 435 $errors['idnumber'] = get_string('idnumbertaken');
436 }
437 }
8d272708 438
6e75c7f8 439 if (array_key_exists('gradetype', $data) and $data['gradetype'] == GRADE_TYPE_SCALE) {
440 if (empty($data['scaleid'])) {
d4175332 441 $errors['scaleid'] = get_string('missingscale', 'grades');
6e75c7f8 442 }
443 }
444
c86caae7 445 if (array_key_exists('grademin', $data) and array_key_exists('grademax', $data)) {
446 if ($data['grademax'] == $data['grademin'] or $data['grademax'] < $data['grademin']) {
d4175332 447 $errors['grademin'] = get_string('incorrectminmax', 'grades');
448 $errors['grademax'] = get_string('incorrectminmax', 'grades');
c86caae7 449 }
450 }
664d8be7
MN
451
452 // We do not want the user to be able to change the grade type or scale for this item if grades exist.
453 if ($grade_item && $grade_item->has_grades()) {
454 // Check that grade type is set - should never not be set unless form has been modified.
455 if (!isset($data['gradetype'])) {
456 $errors['gradetype'] = get_string('modgradecantchangegradetype', 'grades');
457 } else if ($data['gradetype'] !== $grade_item->gradetype) { // Check if we are changing the grade type.
458 $errors['gradetype'] = get_string('modgradecantchangegradetype', 'grades');
459 } else if ($data['gradetype'] == GRADE_TYPE_SCALE) {
460 // Check if we are changing the scale - can't do this when grades exist.
461 if (isset($data['scaleid']) && ($data['scaleid'] !== $grade_item->scaleid)) {
462 $errors['scaleid'] = get_string('modgradecantchangescale', 'grades');
463 }
464 }
465 }
e7c71c18 466 if ($grade_item) {
664d8be7 467 if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
d27e1c75
MS
468 if ((((bool) get_config('moodle', 'grade_report_showmin')) &&
469 grade_floats_different($data['grademin'], $grade_item->grademin)) ||
e7c71c18 470 grade_floats_different($data['grademax'], $grade_item->grademax)) {
664d8be7
MN
471 if ($grade_item->has_grades() && empty($data['rescalegrades'])) {
472 $errors['rescalegrades'] = get_string('mustchooserescaleyesorno', 'grades');
473 }
e7c71c18
DW
474 }
475 }
476 }
c86caae7 477
a78890d5 478 return $errors;
8d272708 479 }
480
db313fec 481}
6c3ef410 482