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