MDL-64557 modform: switch disabledIf to hideIf, where appropriate
[moodle.git] / lib / form / modgrade.php
CommitLineData
c65795d3 1<?php
6c1fd304
RT
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/>.
16
17
18/**
19 * Drop down form element to select the grade
20 *
21 * Contains HTML class for a drop down element to select the grade for an activity,
22 * used in mod update form
23 *
24 * @package core_form
25 * @copyright 2006 Jamie Pratt <me@jamiep.org>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
c65795d3 29global $CFG;
30require_once "$CFG->libdir/form/select.php";
59766233
JM
31require_once("HTML/QuickForm/element.php");
32require_once($CFG->dirroot.'/lib/form/group.php');
33require_once($CFG->dirroot.'/lib/grade/grade_scale.php');
c65795d3 34
35/**
6c1fd304
RT
36 * Drop down form element to select the grade
37 *
c65795d3 38 * HTML class for a drop down element to select the grade for an activity,
39 * used in mod update form
40 *
6c1fd304
RT
41 * @package core_form
42 * @category form
43 * @copyright 2006 Jamie Pratt <me@jamiep.org>
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
c65795d3 45 */
d629c601
DW
46class MoodleQuickForm_modgrade extends MoodleQuickForm_group {
47
48 /** @var boolean $isupdate Is this an add or an update ? */
49 public $isupdate = false;
50
51 /** @var float $currentgrade The current grademax for the grade_item */
52 public $currentgrade = false;
53
54 /** @var boolean $hasgrades Has this grade_item got any real grades (with values) */
55 public $hasgrades = false;
56
57 /** @var boolean $canrescale Does this activity support rescaling grades? */
58 public $canrescale = false;
4a0e2e63 59
664d8be7
MN
60 /** @var int $currentscaleid The current scale id */
61 public $currentscaleid = null;
62
63 /** @var string $currentgradetype The current gradetype - can either be 'none', 'scale', or 'point' */
64 public $currentgradetype = 'none';
65
66 /** @var boolean $useratings Set to true if the activity is using ratings, false otherwise */
67 public $useratings = false;
68
69 /** @var MoodleQuickForm_select $gradetypeformelement */
70 private $gradetypeformelement;
71
72 /** @var MoodleQuickForm_select $scaleformelement */
73 private $scaleformelement;
74
75 /** @var MoodleQuickForm_text $maxgradeformelement */
76 private $maxgradeformelement;
77
c65795d3 78 /**
59766233 79 * Constructor
c65795d3 80 *
59766233
JM
81 * @param string $elementname Element's name
82 * @param mixed $elementlabel Label(s) for an element
d629c601
DW
83 * @param array $options Options to control the element's display. Required - must contain the following options:
84 * 'isupdate' - is this a new module or are we editing an existing one?
85 * 'currentgrade' - the current grademax in the database for this gradeitem
86 * 'hasgrades' - whether or not the grade_item has existing grade_grades
e7c71c18 87 * 'canrescale' - whether or not the activity supports rescaling grades
59766233 88 * @param mixed $attributes Either a typical HTML attribute string or an associative array
c65795d3 89 */
1a0df553 90 public function __construct($elementname = null, $elementlabel = null, $options = array(), $attributes = null) {
32fada5c 91 // TODO MDL-52313 Replace with the call to parent::__construct().
1a0df553 92 HTML_QuickForm_element::__construct($elementname, $elementlabel, $attributes);
59766233
JM
93 $this->_persistantFreeze = true;
94 $this->_appendName = true;
c65795d3 95 $this->_type = 'modgrade';
d629c601 96 $this->isupdate = !empty($options['isupdate']);
d629c601
DW
97 if (isset($options['currentgrade'])) {
98 $this->currentgrade = $options['currentgrade'];
99 }
664d8be7
MN
100 if (isset($options['currentgradetype'])) {
101 $gradetype = $options['currentgradetype'];
102 switch ($gradetype) {
103 case GRADE_TYPE_NONE :
104 $this->currentgradetype = 'none';
105 break;
106 case GRADE_TYPE_SCALE :
107 $this->currentgradetype = 'scale';
108 break;
109 case GRADE_TYPE_VALUE :
110 $this->currentgradetype = 'point';
111 break;
112 }
113 }
114 if (isset($options['currentscaleid'])) {
115 $this->currentscaleid = $options['currentscaleid'];
116 }
d629c601
DW
117 $this->hasgrades = !empty($options['hasgrades']);
118 $this->canrescale = !empty($options['canrescale']);
664d8be7 119 $this->useratings = !empty($options['useratings']);
59766233
JM
120 }
121
1a0df553
MG
122 /**
123 * Old syntax of class constructor. Deprecated in PHP7.
124 *
125 * @deprecated since Moodle 3.1
126 */
127 public function MoodleQuickForm_modgrade($elementname = null, $elementlabel = null, $options = array(), $attributes = null) {
128 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
129 self::__construct($elementname, $elementlabel, $options, $attributes);
130 }
131
59766233
JM
132 /**
133 * Create elements for this group.
134 */
135 public function _createElements() {
d629c601 136 global $COURSE, $CFG, $OUTPUT;
59766233
JM
137 $attributes = $this->getAttributes();
138 if (is_null($attributes)) {
139 $attributes = array();
140 }
141
142 $this->_elements = array();
143
144 // Create main elements
145 // We have to create the scale and point elements first, as we need their IDs.
146
147 // Grade scale select box.
148 $scales = get_scales_menu($COURSE->id);
149 $langscale = get_string('modgradetypescale', 'grades');
721e2def 150 $this->scaleformelement = $this->createFormElement('select', 'modgrade_scale', $langscale,
664d8be7 151 $scales, $attributes);
91bda4cd 152 $this->scaleformelement->setHiddenLabel(true);
664d8be7
MN
153 $scaleformelementid = $this->generate_modgrade_subelement_id('modgrade_scale');
154 $this->scaleformelement->updateAttributes(array('id' => $scaleformelementid));
59766233
JM
155
156 // Maximum grade textbox.
157 $langmaxgrade = get_string('modgrademaxgrade', 'grades');
721e2def 158 $this->maxgradeformelement = $this->createFormElement('text', 'modgrade_point', $langmaxgrade, array());
91bda4cd 159 $this->maxgradeformelement->setHiddenLabel(true);
664d8be7
MN
160 $maxgradeformelementid = $this->generate_modgrade_subelement_id('modgrade_point');
161 $this->maxgradeformelement->updateAttributes(array('id' => $maxgradeformelementid));
59766233
JM
162
163 // Grade type select box.
164 $gradetype = array(
165 'none' => get_string('modgradetypenone', 'grades'),
166 'scale' => get_string('modgradetypescale', 'grades'),
167 'point' => get_string('modgradetypepoint', 'grades'),
168 );
169 $langtype = get_string('modgradetype', 'grades');
721e2def 170 $this->gradetypeformelement = $this->createFormElement('select', 'modgrade_type', $langtype, $gradetype,
664d8be7 171 $attributes, true);
91bda4cd 172 $this->gradetypeformelement->setHiddenLabel(true);
664d8be7
MN
173 $gradetypeformelementid = $this->generate_modgrade_subelement_id('modgrade_type');
174 $this->gradetypeformelement->updateAttributes(array('id' => $gradetypeformelementid));
175
176 if ($this->isupdate && $this->hasgrades) {
177 $this->gradetypeformelement->updateAttributes(array('disabled' => 'disabled'));
178 $this->scaleformelement->updateAttributes(array('disabled' => 'disabled'));
179
180 // Check box for options for processing existing grades.
181 if ($this->canrescale) {
182 $langrescalegrades = get_string('modgraderescalegrades', 'grades');
183 $choices = array();
184 $choices[''] = get_string('choose');
185 $choices['no'] = get_string('no');
186 $choices['yes'] = get_string('yes');
721e2def 187 $rescalegradesselect = $this->createFormElement('select',
664d8be7
MN
188 'modgrade_rescalegrades',
189 $langrescalegrades,
190 $choices);
91bda4cd 191 $rescalegradesselect->setHiddenLabel(true);
664d8be7
MN
192 $rescalegradesselectid = $this->generate_modgrade_subelement_id('modgrade_rescalegrades');
193 $rescalegradesselect->updateAttributes(array('id' => $rescalegradesselectid));
194 }
d629c601
DW
195 }
196
59766233 197 // Add elements.
664d8be7
MN
198 if ($this->isupdate && $this->hasgrades) {
199 // Set a message so the user knows why they can not alter the grade type or scale.
200 if ($this->currentgradetype == 'scale') {
201 $gradesexistmsg = get_string('modgradecantchangegradetyporscalemsg', 'grades');
749af21b 202 } else if ($this->canrescale) {
664d8be7 203 $gradesexistmsg = get_string('modgradecantchangegradetypemsg', 'grades');
749af21b
AG
204 } else {
205 $gradesexistmsg = get_string('modgradecantchangegradetype', 'grades');
664d8be7
MN
206 }
207
c3f2290d 208 $gradesexisthtml = '<div class=\'alert alert-warning\'>' . $gradesexistmsg . '</div>';
721e2def 209 $this->_elements[] = $this->createFormElement('static', 'gradesexistmsg', '', $gradesexisthtml);
664d8be7 210 }
59766233
JM
211
212 // Grade type select box.
664d8be7
MN
213 $label = html_writer::tag('label', $this->gradetypeformelement->getLabel(),
214 array('for' => $this->gradetypeformelement->getAttribute('id')));
721e2def 215 $this->_elements[] = $this->createFormElement('static', 'gradetypelabel', '', '&nbsp;'.$label);
664d8be7 216 $this->_elements[] = $this->gradetypeformelement;
721e2def 217 $this->_elements[] = $this->createFormElement('static', 'gradetypespacer', '', '<br />');
c65795d3 218
664d8be7
MN
219 // Only show the grade scale select box when applicable.
220 if (!$this->isupdate || !$this->hasgrades || $this->currentgradetype == 'scale') {
221 $label = html_writer::tag('label', $this->scaleformelement->getLabel(),
222 array('for' => $this->scaleformelement->getAttribute('id')));
721e2def 223 $this->_elements[] = $this->createFormElement('static', 'scalelabel', '', $label);
664d8be7 224 $this->_elements[] = $this->scaleformelement;
721e2def 225 $this->_elements[] = $this->createFormElement('static', 'scalespacer', '', '<br />');
664d8be7 226 }
d629c601 227
664d8be7 228 if ($this->isupdate && $this->hasgrades && $this->canrescale && $this->currentgradetype == 'point') {
d629c601 229 // We need to know how to apply any changes to maxgrade - ie to either update, or don't touch exising grades.
664d8be7
MN
230 $label = html_writer::tag('label', $rescalegradesselect->getLabel(),
231 array('for' => $rescalegradesselect->getAttribute('id')));
d629c601 232 $labelhelp = new help_icon('modgraderescalegrades', 'grades');
721e2def 233 $this->_elements[] = $this->createFormElement('static', 'scalelabel', '', $label . $OUTPUT->render($labelhelp));
d629c601 234 $this->_elements[] = $rescalegradesselect;
721e2def 235 $this->_elements[] = $this->createFormElement('static', 'scalespacer', '', '<br />');
d629c601 236 }
664d8be7
MN
237
238 // Only show the max points form element when applicable.
239 if (!$this->isupdate || !$this->hasgrades || $this->currentgradetype == 'point') {
240 $label = html_writer::tag('label', $this->maxgradeformelement->getLabel(),
241 array('for' => $this->maxgradeformelement->getAttribute('id')));
721e2def 242 $this->_elements[] = $this->createFormElement('static', 'pointlabel', '', $label);
664d8be7 243 $this->_elements[] = $this->maxgradeformelement;
721e2def 244 $this->_elements[] = $this->createFormElement('static', 'pointspacer', '', '<br />');
664d8be7 245 }
6c1fd304 246 }
c65795d3 247
248 /**
59766233
JM
249 * Calculate the output value for the element as a whole.
250 *
251 * @param array $submitvalues The incoming values from the form.
252 * @param bool $notused Not used.
253 * @return array Return value for the element, formatted like field name => value.
254 */
255 public function exportValue(&$submitvalues, $notused = false) {
256 global $COURSE;
257
258 // Get the values from all the child elements.
259 $vals = array();
260 foreach ($this->_elements as $element) {
261 $thisexport = $element->exportValue($submitvalues[$this->getName()], true);
262 if (!is_null($thisexport)) {
263 $vals += $thisexport;
264 }
265 }
266
267 $type = (isset($vals['modgrade_type'])) ? $vals['modgrade_type'] : 'none';
268 $point = (isset($vals['modgrade_point'])) ? $vals['modgrade_point'] : null;
269 $scale = (isset($vals['modgrade_scale'])) ? $vals['modgrade_scale'] : null;
d629c601 270 $rescalegrades = (isset($vals['modgrade_rescalegrades'])) ? $vals['modgrade_rescalegrades'] : null;
c3d4d3f7
DW
271
272 $return = $this->process_value($type, $scale, $point, $rescalegrades);
d629c601 273 return array($this->getName() => $return, $this->getName() . '_rescalegrades' => $rescalegrades);
59766233
JM
274 }
275
276 /**
277 * Process the value for the group based on the selected grade type, and the input for the scale and point elements.
278 *
279 * @param string $type The value of the grade type select box. Can be 'none', 'scale', or 'point'
280 * @param string|int $scale The value of the scale select box.
281 * @param string|int $point The value of the point grade textbox.
c3d4d3f7 282 * @param string $rescalegrades The value of the rescalegrades select.
59766233
JM
283 * @return int The resulting value
284 */
c3d4d3f7 285 protected function process_value($type='none', $scale=null, $point=null, $rescalegrades=null) {
59766233
JM
286 global $COURSE;
287 $val = 0;
c3d4d3f7
DW
288 if ($this->isupdate && $this->hasgrades && $this->canrescale && $this->currentgradetype == 'point' && empty($rescalegrades)) {
289 // If the maxgrade field is disabled with javascript, no value is sent with the form and mform assumes the default.
290 // If the user was forced to choose a rescale option - and they haven't - prevent any changes to the max grade.
bb0b39b8 291 return (string)unformat_float($this->currentgrade);
c3d4d3f7 292 }
59766233
JM
293 switch ($type) {
294 case 'point':
295 if ($this->validate_point($point) === true) {
296 $val = (int)$point;
297 }
298 break;
299
300 case 'scale':
301 if ($this->validate_scale($scale)) {
302 $val = (int)(-$scale);
303 }
304 break;
305 }
306 return $val;
307 }
308
309 /**
310 * Determines whether a given value is a valid scale selection.
311 *
312 * @param string|int $val The value to test.
313 * @return bool Valid or invalid
314 */
315 protected function validate_scale($val) {
316 global $COURSE;
317 $scales = get_scales_menu($COURSE->id);
318 return (!empty($val) && isset($scales[(int)$val])) ? true : false;
319 }
320
321 /**
322 * Determines whether a given value is a valid point selection.
323 *
324 * @param string|int $val The value to test.
325 * @return bool Valid or invalid
326 */
327 protected function validate_point($val) {
328 if (empty($val)) {
329 return false;
330 }
331 $maxgrade = (int)get_config('core', 'gradepointmax');
332 $isintlike = ((string)(int)$val === $val) ? true : false;
333 return ($isintlike === true && $val > 0 && $val <= $maxgrade) ? true : false;
334 }
335
336 /**
337 * Called by HTML_QuickForm whenever form event is made on this element.
c65795d3 338 *
6c1fd304
RT
339 * @param string $event Name of event
340 * @param mixed $arg event arguments
ed7605f4 341 * @param moodleform $caller calling object
6c1fd304 342 * @return mixed
c65795d3 343 */
59766233 344 public function onQuickFormEvent($event, $arg, &$caller) {
721e2def 345 $this->setMoodleForm($caller);
c65795d3 346 switch ($event) {
ed7605f4
SH
347 case 'createElement':
348 // The first argument is the name.
349 $name = $arg[0];
350
351 // Set disable actions.
87c7fb6e
DS
352 $caller->hideIf($name.'[modgrade_scale]', $name.'[modgrade_type]', 'neq', 'scale');
353 $caller->hideIf($name.'[modgrade_point]', $name.'[modgrade_type]', 'neq', 'point');
354 $caller->hideIf($name.'[modgrade_rescalegrades]', $name.'[modgrade_type]', 'neq', 'point');
ed7605f4
SH
355
356 // Set validation rules for the sub-elements belonging to this element.
357 // A handy note: the parent scope of a closure is the function in which the closure was declared.
358 // Because of this using $this is safe despite the closures being called statically.
359 // A nasty magic hack!
664d8be7
MN
360 $checkgradetypechange = function($val) {
361 // Nothing is affected by changes to the grade type if there are no grades yet.
362 if (!$this->hasgrades) {
363 return true;
364 }
365 // Check if we are changing the grade type when grades are present.
366 if (isset($val['modgrade_type']) && $val['modgrade_type'] !== $this->currentgradetype) {
367 return false;
368 }
369 return true;
370 };
371 $checkscalechange = function($val) {
372 // Nothing is affected by changes to the scale if there are no grades yet.
373 if (!$this->hasgrades) {
374 return true;
375 }
376 // Check if we are changing the scale type when grades are present.
bdd4bed8
RT
377 // If modgrade_type is empty then use currentgradetype.
378 $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype;
379 if ($gradetype === 'scale') {
664d8be7
MN
380 if (isset($val['modgrade_scale']) && ($val['modgrade_scale'] !== $this->currentscaleid)) {
381 return false;
382 }
383 }
384 return true;
385 };
386 $checkmaxgradechange = function($val) {
387 // Nothing is affected by changes to the max grade if there are no grades yet.
388 if (!$this->hasgrades) {
389 return true;
390 }
391 // If we are not using ratings we can change the max grade.
392 if (!$this->useratings) {
393 return true;
394 }
395 // Check if we are changing the max grade if we are using ratings and there is a grade.
bdd4bed8
RT
396 // If modgrade_type is empty then use currentgradetype.
397 $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype;
398 if ($gradetype === 'point') {
664d8be7
MN
399 if (isset($val['modgrade_point']) &&
400 grade_floats_different($this->currentgrade, $val['modgrade_point'])) {
401 return false;
402 }
403 }
404 return true;
405 };
ed7605f4
SH
406 $checkmaxgrade = function($val) {
407 // Closure to validate a max points value. See the note above about scope if this confuses you.
bdd4bed8
RT
408 // If modgrade_type is empty then use currentgradetype.
409 $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype;
410 if ($gradetype === 'point') {
411 if (isset($val['modgrade_point'])) {
412 return $this->validate_point($val['modgrade_point']);
ed7605f4 413 }
ed7605f4
SH
414 }
415 return true;
416 };
417 $checkvalidscale = function($val) {
418 // Closure to validate a scale value. See the note above about scope if this confuses you.
bdd4bed8
RT
419 // If modgrade_type is empty then use currentgradetype.
420 $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype;
421 if ($gradetype === 'scale') {
422 if (isset($val['modgrade_scale'])) {
423 return $this->validate_scale($val['modgrade_scale']);
ed7605f4 424 }
ed7605f4
SH
425 }
426 return true;
427 };
428
e7c71c18
DW
429 $checkrescale = function($val) {
430 // Nothing is affected by changes to grademax if there are no grades yet.
431 if (!$this->isupdate || !$this->hasgrades || !$this->canrescale) {
432 return true;
433 }
434 // Closure to validate a scale value. See the note above about scope if this confuses you.
bdd4bed8
RT
435 // If modgrade_type is empty then use currentgradetype.
436 $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype;
437 if ($gradetype === 'point' && isset($val['modgrade_point'])) {
e7c71c18
DW
438 // Work out if the value was actually changed in the form.
439 if (grade_floats_different($this->currentgrade, $val['modgrade_point'])) {
440 if (empty($val['modgrade_rescalegrades'])) {
441 // This was an "edit", the grademax was changed and the process existing setting was not set.
442 return false;
443 }
444 }
445 }
446 return true;
447 };
448
664d8be7
MN
449 $cantchangegradetype = get_string('modgradecantchangegradetype', 'grades');
450 $cantchangemaxgrade = get_string('modgradecantchangeratingmaxgrade', 'grades');
ed7605f4
SH
451 $maxgradeexceeded = get_string('modgradeerrorbadpoint', 'grades', get_config('core', 'gradepointmax'));
452 $invalidscale = get_string('modgradeerrorbadscale', 'grades');
664d8be7 453 $cantchangescale = get_string('modgradecantchangescale', 'grades');
e7c71c18 454 $mustchooserescale = get_string('mustchooserescaleyesorno', 'grades');
ed7605f4
SH
455 // When creating the rules the sixth arg is $force, we set it to true because otherwise the form
456 // will attempt to validate the existence of the element, we don't want this because the element
457 // is being created right now and doesn't actually exist as a registered element yet.
664d8be7
MN
458 $caller->addRule($name, $cantchangegradetype, 'callback', $checkgradetypechange, 'server', false, true);
459 $caller->addRule($name, $cantchangemaxgrade, 'callback', $checkmaxgradechange, 'server', false, true);
ed7605f4
SH
460 $caller->addRule($name, $maxgradeexceeded, 'callback', $checkmaxgrade, 'server', false, true);
461 $caller->addRule($name, $invalidscale, 'callback', $checkvalidscale, 'server', false, true);
664d8be7 462 $caller->addRule($name, $cantchangescale, 'callback', $checkscalechange, 'server', false, true);
e7c71c18 463 $caller->addRule($name, $mustchooserescale, 'callback', $checkrescale, 'server', false, true);
ed7605f4
SH
464
465 break;
466
59766233 467 case 'updateValue':
ed7605f4
SH
468 // As this is a group element with no value of its own we are only interested in situations where the
469 // default value or a constant value are being provided to the actual element.
470 // In this case we expect an int that is going to translate to a scale if negative, or to max points
471 // if positive.
472
664d8be7
MN
473 // Set the maximum points field to disabled if the rescale option has not been chosen and there are grades.
474 $caller->disabledIf($this->getName() . '[modgrade_point]', $this->getName() .
475 '[modgrade_rescalegrades]', 'eq', '');
476
ed7605f4 477 // A constant value should be given as an int.
4ca17b63 478 // The default value should be an int and be either $CFG->gradepointdefault or whatever was set in set_data().
59766233
JM
479 $value = $this->_findValue($caller->_constantValues);
480 if (null === $value) {
4ca17b63
MG
481 if ($caller->isSubmitted() && $this->_findValue($caller->_submitValues) !== null) {
482 // Submitted values are array, one value for each individual element in this group.
483 // When there is submitted data let parent::onQuickFormEvent() process it.
ed7605f4 484 break;
59766233 485 }
ed7605f4 486 $value = $this->_findValue($caller->_defaultValues);
c65795d3 487 }
59766233 488
ed7605f4
SH
489 if (!is_null($value) && !is_scalar($value)) {
490 // Something unexpected (likely an array of subelement values) has been given - this will be dealt
491 // with somewhere else - where exactly... likely the subelements.
492 debugging('An invalid value (type '.gettype($value).') has arrived at '.__METHOD__, DEBUG_DEVELOPER);
493 break;
494 }
59766233
JM
495
496 // Set element state for existing data.
ed7605f4
SH
497 // This is really a pretty hacky thing to do, when data is being set the group element is called
498 // with the data first and the subelements called afterwards.
499 // This means that the subelements data (inc const and default values) can be overridden by form code.
500 // So - when we call this code really we can't be sure that will be the end value for the element.
59766233
JM
501 if (!empty($this->_elements)) {
502 if (!empty($value)) {
503 if ($value < 0) {
664d8be7
MN
504 $this->gradetypeformelement->setValue('scale');
505 $this->scaleformelement->setValue(($value * -1));
59766233 506 } else if ($value > 0) {
664d8be7 507 $this->gradetypeformelement->setValue('point');
6965ffe0
AG
508 $maxvalue = !empty($this->currentgrade) ? (string)unformat_float($this->currentgrade) : $value;
509 $this->maxgradeformelement->setValue($maxvalue);
59766233
JM
510 }
511 } else {
664d8be7
MN
512 $this->gradetypeformelement->setValue('none');
513 $this->maxgradeformelement->setValue('');
59766233 514 }
c65795d3 515 }
59766233 516 break;
c65795d3 517 }
59766233 518
ed7605f4 519 // Always let the parent do its thing!
c65795d3 520 return parent::onQuickFormEvent($event, $arg, $caller);
521 }
522
d84c64b7
DM
523 /**
524 * Generates the id attribute for the subelement of the modgrade group.
525 *
526 * Uses algorithm similar to what {@link HTML_QuickForm_element::_generateId()}
527 * does but takes the name of the wrapping modgrade group into account.
528 *
529 * @param string $subname the name of the HTML_QuickForm_element in this modgrade group
530 * @return string
531 */
532 protected function generate_modgrade_subelement_id($subname) {
533 $gid = str_replace(array('[', ']'), array('_', ''), $this->getName());
534 return clean_param('id_'.$gid.'_'.$subname, PARAM_ALPHANUMEXT);
535 }
c65795d3 536}