1<?php
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 * A moodleform to edit the grade options for an individual grade category
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
25if (!defined('MOODLE_INTERNAL')) {
26    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
27}
28
29require_once $CFG->libdir.'/formslib.php';
30
31class edit_category_form extends moodleform {
32    private $aggregation_options = array();
33
34    function definition() {
35        global $CFG, $COURSE, $DB, $OUTPUT;
36        $mform =& $this->_form;
37
38        $category = $this->_customdata['current'];
39
40        $this->aggregation_options = grade_helper::get_aggregation_strings();
41
42        // visible elements
43        $mform->addElement('header', 'headercategory', get_string('gradecategory', 'grades'));
44        $mform->addElement('text', 'fullname', get_string('categoryname', 'grades'));
45        $mform->setType('fullname', PARAM_TEXT);
46        $mform->addRule('fullname', null, 'required', null, 'client');
47
48        $mform->addElement('select', 'aggregation', get_string('aggregation', 'grades'), $this->aggregation_options);
49        $mform->addHelpButton('aggregation', 'aggregation', 'grades');
50
51        if ((int)$CFG->grade_aggregation_flag & 2) {
52            $mform->setAdvanced('aggregation');
53        }
54
55        $mform->addElement('checkbox', 'aggregateonlygraded', get_string('aggregateonlygraded', 'grades'));
56        $mform->addHelpButton('aggregateonlygraded', 'aggregateonlygraded', 'grades');
57
58        if ((int)$CFG->grade_aggregateonlygraded_flag & 2) {
59            $mform->setAdvanced('aggregateonlygraded');
60        }
61
62        if (empty($CFG->enableoutcomes)) {
63            $mform->addElement('hidden', 'aggregateoutcomes');
64            $mform->setType('aggregateoutcomes', PARAM_INT);
65        } else {
66            $mform->addElement('checkbox', 'aggregateoutcomes', get_string('aggregateoutcomes', 'grades'));
67            $mform->addHelpButton('aggregateoutcomes', 'aggregateoutcomes', 'grades');
68            if ((int)$CFG->grade_aggregateoutcomes_flag & 2) {
69                $mform->setAdvanced('aggregateoutcomes');
70            }
71        }
72
73        $mform->addElement('text', 'keephigh', get_string('keephigh', 'grades'), 'size="3"');
74        $mform->setType('keephigh', PARAM_INT);
75        $mform->addHelpButton('keephigh', 'keephigh', 'grades');
76        if ((int)$CFG->grade_keephigh_flag & 2) {
77            $mform->setAdvanced('keephigh');
78        }
79
80        $mform->addElement('text', 'droplow', get_string('droplow', 'grades'), 'size="3"');
81        $mform->setType('droplow', PARAM_INT);
82        $mform->addHelpButton('droplow', 'droplow', 'grades');
83        $mform->disabledIf('droplow', 'keephigh', 'noteq', 0);
84        if ((int)$CFG->grade_droplow_flag & 2) {
85            $mform->setAdvanced('droplow');
86        }
87
88        $mform->disabledIf('keephigh', 'droplow', 'noteq', 0);
89        $mform->disabledIf('droplow', 'keephigh', 'noteq', 0);
90
91        // Grade item settings
92        // Displayed as Category total to avoid confusion between grade items requiring marking and category totals
93        $mform->addElement('header', 'general', get_string('categorytotal', 'grades'));
94
95        $mform->addElement('text', 'grade_item_itemname', get_string('categorytotalname', 'grades'));
96        $mform->setType('grade_item_itemname', PARAM_TEXT);
97        $mform->setAdvanced('grade_item_itemname');
98
99        $mform->addElement('text', 'grade_item_iteminfo', get_string('iteminfo', 'grades'));
100        $mform->addHelpButton('grade_item_iteminfo', 'iteminfo', 'grades');
101        $mform->setType('grade_item_iteminfo', PARAM_TEXT);
102
103        $mform->addElement('text', 'grade_item_idnumber', get_string('idnumbermod'));
104        $mform->addHelpButton('grade_item_idnumber', 'idnumbermod');
105        $mform->setType('grade_item_idnumber', PARAM_RAW);
106
107        if (!empty($category->id)) {
108            $gradecategory = grade_category::fetch(array('id' => $category->id));
109            $gradeitem = $gradecategory->load_grade_item();
110
111            // If grades exist set a message so the user knows why they can not alter the grade type or scale.
112            // We could never change the grade type for external items, so only need to show this for manual grade items.
113            if ($gradeitem->has_overridden_grades()) {
114                // Set a message so the user knows why the can not alter the grade type or scale.
115                if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
116                    $gradesexistmsg = get_string('modgradecategorycantchangegradetyporscalemsg', 'grades');
117                } else {
118                    $gradesexistmsg = get_string('modgradecategorycantchangegradetypemsg', 'grades');
119                }
120                $notification = new \core\output\notification($gradesexistmsg, \core\output\notification::NOTIFY_INFO);
121                $notification->set_show_closebutton(false);
122                $mform->addElement('static', 'gradesexistmsg', '', $OUTPUT->render($notification));
123            }
124        }
125
126        $options = array(GRADE_TYPE_NONE=>get_string('typenone', 'grades'),
127                         GRADE_TYPE_VALUE=>get_string('typevalue', 'grades'),
128                         GRADE_TYPE_SCALE=>get_string('typescale', 'grades'),
129                         GRADE_TYPE_TEXT=>get_string('typetext', 'grades'));
130
131        $mform->addElement('select', 'grade_item_gradetype', get_string('gradetype', 'grades'), $options);
132        $mform->addHelpButton('grade_item_gradetype', 'gradetype', 'grades');
133        $mform->setDefault('grade_item_gradetype', GRADE_TYPE_VALUE);
134        $mform->disabledIf('grade_item_gradetype', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
135
136        //$mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
137        //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_TEXT);
138        //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_NONE);
139
140        $options = array(0=>get_string('usenoscale', 'grades'));
141        if ($scales = grade_scale::fetch_all_local($COURSE->id)) {
142            foreach ($scales as $scale) {
143                $options[$scale->id] = $scale->get_name();
144            }
145        }
146        if ($scales = grade_scale::fetch_all_global()) {
147            foreach ($scales as $scale) {
148                $options[$scale->id] = $scale->get_name();
149            }
150        }
151        // ugly BC hack - it was possible to use custom scale from other courses :-(
152        if (!empty($category->grade_item_scaleid) and !isset($options[$category->grade_item_scaleid])) {
153            if ($scale = grade_scale::fetch(array('id'=>$category->grade_item_scaleid))) {
154                $options[$scale->id] = $scale->get_name().' '.get_string('incorrectcustomscale', 'grades');
155            }
156        }
157        $mform->addElement('select', 'grade_item_scaleid', get_string('scale'), $options);
158        $mform->addHelpButton('grade_item_scaleid', 'typescale', 'grades');
159        $mform->disabledIf('grade_item_scaleid', 'grade_item_gradetype', 'noteq', GRADE_TYPE_SCALE);
160        $mform->disabledIf('grade_item_scaleid', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
161
162        $choices = array();
163        $choices[''] = get_string('choose');
164        $choices['no'] = get_string('no');
165        $choices['yes'] = get_string('yes');
166        $mform->addElement('select', 'grade_item_rescalegrades', get_string('modgradecategoryrescalegrades', 'grades'), $choices);
167        $mform->addHelpButton('grade_item_rescalegrades', 'modgradecategoryrescalegrades', 'grades');
168        $mform->disabledIf('grade_item_rescalegrades', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
169
170        $mform->addElement('text', 'grade_item_grademax', get_string('grademax', 'grades'));
171        $mform->setType('grade_item_grademax', PARAM_RAW);
172        $mform->addHelpButton('grade_item_grademax', 'grademax', 'grades');
173        $mform->disabledIf('grade_item_grademax', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
174        $mform->disabledIf('grade_item_grademax', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
175
176        if ((bool) get_config('moodle', 'grade_report_showmin')) {
177            $mform->addElement('text', 'grade_item_grademin', get_string('grademin', 'grades'));
178            $mform->setType('grade_item_grademin', PARAM_RAW);
179            $mform->addHelpButton('grade_item_grademin', 'grademin', 'grades');
180            $mform->disabledIf('grade_item_grademin', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
181            $mform->disabledIf('grade_item_grademin', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
182        }
183
184        $mform->addElement('text', 'grade_item_gradepass', get_string('gradepass', 'grades'));
185        $mform->setType('grade_item_gradepass', PARAM_RAW);
186        $mform->addHelpButton('grade_item_gradepass', 'gradepass', 'grades');
187        $mform->disabledIf('grade_item_gradepass', 'grade_item_gradetype', 'eq', GRADE_TYPE_NONE);
188        $mform->disabledIf('grade_item_gradepass', 'grade_item_gradetype', 'eq', GRADE_TYPE_TEXT);
189
190        /// grade display prefs
191        $default_gradedisplaytype = grade_get_setting($COURSE->id, 'displaytype', $CFG->grade_displaytype);
192        $options = array(GRADE_DISPLAY_TYPE_DEFAULT            => get_string('default', 'grades'),
193                         GRADE_DISPLAY_TYPE_REAL               => get_string('real', 'grades'),
194                         GRADE_DISPLAY_TYPE_PERCENTAGE         => get_string('percentage', 'grades'),
195                         GRADE_DISPLAY_TYPE_LETTER             => get_string('letter', 'grades'),
196                         GRADE_DISPLAY_TYPE_REAL_PERCENTAGE    => get_string('realpercentage', 'grades'),
197                         GRADE_DISPLAY_TYPE_REAL_LETTER        => get_string('realletter', 'grades'),
198                         GRADE_DISPLAY_TYPE_LETTER_REAL        => get_string('letterreal', 'grades'),
199                         GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE  => get_string('letterpercentage', 'grades'),
200                         GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER  => get_string('percentageletter', 'grades'),
201                         GRADE_DISPLAY_TYPE_PERCENTAGE_REAL    => get_string('percentagereal', 'grades')
202                         );
203
204        asort($options);
205
206        foreach ($options as $key=>$option) {
207            if ($key == $default_gradedisplaytype) {
208                $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option);
209                break;
210            }
211        }
212        $mform->addElement('select', 'grade_item_display', get_string('gradedisplaytype', 'grades'), $options);
213        $mform->addHelpButton('grade_item_display', 'gradedisplaytype', 'grades');
214        $mform->disabledIf('grade_item_display', 'grade_item_gradetype', 'in',
215            array(GRADE_TYPE_TEXT, GRADE_TYPE_NONE));
216
217        $default_gradedecimals = grade_get_setting($COURSE->id, 'decimalpoints', $CFG->grade_decimalpoints);
218        $options = array(-1=>get_string('defaultprev', 'grades', $default_gradedecimals), 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
219        $mform->addElement('select', 'grade_item_decimals', get_string('decimalpoints', 'grades'), $options);
220        $mform->addHelpButton('grade_item_decimals', 'decimalpoints', 'grades');
221        $mform->setDefault('grade_item_decimals', -1);
222        $mform->disabledIf('grade_item_decimals', 'grade_item_display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
223        $mform->disabledIf('grade_item_decimals', 'grade_item_gradetype', 'in',
224            array(GRADE_TYPE_TEXT, GRADE_TYPE_NONE));
225
226        if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
227            $mform->disabledIf('grade_item_decimals', 'grade_item_display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
228        }
229
230        /// hiding
231        // advcheckbox is not compatible with disabledIf!
232        $mform->addElement('checkbox', 'grade_item_hidden', get_string('hidden', 'grades'));
233        $mform->addHelpButton('grade_item_hidden', 'hidden', 'grades');
234        $mform->addElement('date_time_selector', 'grade_item_hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
235        $mform->disabledIf('grade_item_hidden', 'grade_item_hiddenuntil[off]', 'notchecked');
236
237        /// locking
238        $mform->addElement('checkbox', 'grade_item_locked', get_string('locked', 'grades'));
239        $mform->addHelpButton('grade_item_locked', 'locked', 'grades');
240
241        $mform->addElement('date_time_selector', 'grade_item_locktime', get_string('locktime', 'grades'), array('optional'=>true));
242        $mform->disabledIf('grade_item_locktime', 'grade_item_gradetype', 'eq', GRADE_TYPE_NONE);
243
244/// parent category related settings
245        $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
246
247        $mform->addElement('advcheckbox', 'grade_item_weightoverride', get_string('adjustedweight', 'grades'));
248        $mform->addHelpButton('grade_item_weightoverride', 'weightoverride', 'grades');
249
250        $mform->addElement('text', 'grade_item_aggregationcoef2', get_string('weight', 'grades'));
251        $mform->addHelpButton('grade_item_aggregationcoef2', 'weight', 'grades');
252        $mform->setType('grade_item_aggregationcoef2', PARAM_RAW);
253        $mform->disabledIf('grade_item_aggregationcoef2', 'grade_item_weightoverride');
254
255        $options = array();
256        $default = -1;
257        $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
258
259        foreach ($categories as $cat) {
260            $cat->apply_forced_settings();
261            $options[$cat->id] = $cat->get_name();
262            if ($cat->is_course_category()) {
263                $default = $cat->id;
264            }
265        }
266
267        if (count($categories) > 1) {
268            $mform->addElement('select', 'parentcategory', get_string('parentcategory', 'grades'), $options);
269            $mform->setDefault('parentcategory', $default);
270            $mform->addElement('static', 'currentparentaggregation', get_string('currentparentaggregation', 'grades'));
271        }
272
273        // hidden params
274        $mform->addElement('hidden', 'id', 0);
275        $mform->setType('id', PARAM_INT);
276
277        $mform->addElement('hidden', 'courseid', 0);
278        $mform->setType('courseid', PARAM_INT);
279
280/// add return tracking info
281        $gpr = $this->_customdata['gpr'];
282        $gpr->add_mform_elements($mform);
283
284/// mark advanced according to site settings
285        if (isset($CFG->grade_item_advanced)) {
286            $advanced = explode(',', $CFG->grade_item_advanced);
287            foreach ($advanced as $el) {
288                $el = 'grade_item_'.$el;
289                if ($mform->elementExists($el)) {
290                    $mform->setAdvanced($el);
291                }
292            }
293        }
294
295//-------------------------------------------------------------------------------
296        // buttons
297        $this->add_action_buttons();
298//-------------------------------------------------------------------------------
299        $this->set_data($category);
300    }
301
302
303/// tweak the form - depending on existing data
304    function definition_after_data() {
305        global $CFG, $COURSE;
306
307        $mform =& $this->_form;
308
309        $somecat = new grade_category();
310
311        foreach ($somecat->forceable as $property) {
312            if ((int)$CFG->{"grade_{$property}_flag"} & 1) {
313                if ($mform->elementExists($property)) {
314                    if (empty($CFG->grade_hideforcedsettings)) {
315                        $mform->hardFreeze($property);
316                    } else {
317                        if ($mform->elementExists($property)) {
318                            $mform->removeElement($property);
319                        }
320                    }
321                }
322            }
323        }
324
325        if ($CFG->grade_droplow > 0) {
326            if ($mform->elementExists('keephigh')) {
327                $mform->removeElement('keephigh');
328            }
329        } else if ($CFG->grade_keephigh > 0) {
330            if ($mform->elementExists('droplow')) {
331                $mform->removeElement('droplow');
332            }
333        }
334
335        if ($id = $mform->getElementValue('id')) {
336            $grade_category = grade_category::fetch(array('id'=>$id));
337            $grade_item = $grade_category->load_grade_item();
338
339            // remove agg coef if not used
340            if ($grade_category->is_course_category()) {
341                if ($mform->elementExists('parentcategory')) {
342                    $mform->removeElement('parentcategory');
343                }
344                if ($mform->elementExists('currentparentaggregation')) {
345                    $mform->removeElement('currentparentaggregation');
346                }
347
348            } else {
349                // if we wanted to change parent of existing category - we would have to verify there are no circular references in parents!!!
350                if ($mform->elementExists('parentcategory')) {
351                    $mform->hardFreeze('parentcategory');
352                }
353                $parent_cat = $grade_category->get_parent_category();
354                $mform->setDefault('currentparentaggregation', $this->aggregation_options[$parent_cat->aggregation]);
355
356            }
357
358            // Prevent the user from using drop lowest/keep highest when the aggregation method cannot handle it.
359            if (!$grade_category->can_apply_limit_rules()) {
360                if ($mform->elementExists('keephigh')) {
361                    $mform->setConstant('keephigh', 0);
362                    $mform->hardFreeze('keephigh');
363                }
364                if ($mform->elementExists('droplow')) {
365                    $mform->setConstant('droplow', 0);
366                    $mform->hardFreeze('droplow');
367                }
368            }
369
370            if ($grade_item->is_calculated()) {
371                // following elements are ignored when calculation formula used
372                if ($mform->elementExists('aggregation')) {
373                    $mform->removeElement('aggregation');
374                }
375                if ($mform->elementExists('keephigh')) {
376                    $mform->removeElement('keephigh');
377                }
378                if ($mform->elementExists('droplow')) {
379                    $mform->removeElement('droplow');
380                }
381                if ($mform->elementExists('aggregateonlygraded')) {
382                    $mform->removeElement('aggregateonlygraded');
383                }
384                if ($mform->elementExists('aggregateoutcomes')) {
385                    $mform->removeElement('aggregateoutcomes');
386                }
387            }
388
389            // If it is a course category, remove the "required" rule from the "fullname" element
390            if ($grade_category->is_course_category()) {
391                unset($mform->_rules['fullname']);
392                $key = array_search('fullname', $mform->_required);
393                unset($mform->_required[$key]);
394            }
395
396            // If it is a course category and its fullname is ?, show an empty field
397            if ($grade_category->is_course_category() && $mform->getElementValue('fullname') == '?') {
398                $mform->setDefault('fullname', '');
399            }
400            // remove unwanted aggregation options
401            if ($mform->elementExists('aggregation')) {
402                $allaggoptions = array_keys($this->aggregation_options);
403                $agg_el =& $mform->getElement('aggregation');
404                $visible = explode(',', $CFG->grade_aggregations_visible);
405                if (!is_null($grade_category->aggregation)) {
406                    // current type is always visible
407                    $visible[] = $grade_category->aggregation;
408                }
409                foreach ($allaggoptions as $type) {
410                    if (!in_array($type, $visible)) {
411                        $agg_el->removeOption($type);
412                    }
413                }
414            }
415
416        } else {
417            // adding new category
418            if ($mform->elementExists('currentparentaggregation')) {
419                $mform->removeElement('currentparentaggregation');
420            }
421            // remove unwanted aggregation options
422            if ($mform->elementExists('aggregation')) {
423                $allaggoptions = array_keys($this->aggregation_options);
424                $agg_el =& $mform->getElement('aggregation');
425                $visible = explode(',', $CFG->grade_aggregations_visible);
426                foreach ($allaggoptions as $type) {
427                    if (!in_array($type, $visible)) {
428                        $agg_el->removeOption($type);
429                    }
430                }
431            }
432
433            $mform->removeElement('grade_item_rescalegrades');
434        }
435
436
437        // no parent header for course category
438        if (!$mform->elementExists('parentcategory')) {
439            $mform->removeElement('headerparent');
440        }
441
442/// GRADE ITEM
443        if ($id = $mform->getElementValue('id')) {
444            $grade_category = grade_category::fetch(array('id'=>$id));
445            $grade_item = $grade_category->load_grade_item();
446
447            $mform->setDefault('grade_item_hidden', (int) $grade_item->hidden);
448
449            if ($grade_item->is_outcome_item()) {
450                // we have to prevent incompatible modifications of outcomes if outcomes disabled
451                $mform->removeElement('grade_item_grademax');
452                if ($mform->elementExists('grade_item_grademin')) {
453                    $mform->removeElement('grade_item_grademin');
454                }
455                $mform->removeElement('grade_item_gradetype');
456                $mform->removeElement('grade_item_display');
457                $mform->removeElement('grade_item_decimals');
458                $mform->hardFreeze('grade_item_scaleid');
459            // Only show the option to rescale grades on a category if its corresponding grade_item has overridden grade_grades.
460            } else if ($grade_item->has_overridden_grades()) {
461                // Can't change the grade type or the scale if there are grades.
462                $mform->hardFreeze('grade_item_gradetype, grade_item_scaleid');
463
464                // If we are using scles then remove the unnecessary rescale and grade fields.
465                if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
466                    $mform->removeElement('grade_item_rescalegrades');
467                    $mform->removeElement('grade_item_grademax');
468                    if ($mform->elementExists('grade_item_grademin')) {
469                        $mform->removeElement('grade_item_grademin');
470                    }
471                } else { // Not using scale, so remove it.
472                    $mform->removeElement('grade_item_scaleid');
473                    $mform->disabledIf('grade_item_grademax', 'grade_item_rescalegrades', 'eq', '');
474                    $mform->disabledIf('grade_item_grademin', 'grade_item_rescalegrades', 'eq', '');
475                }
476            } else { // Remove the rescale element if there are no grades.
477                $mform->removeElement('grade_item_rescalegrades');
478            }
479
480            //remove the aggregation coef element if not needed
481            if ($grade_item->is_course_item()) {
482                if ($mform->elementExists('grade_item_aggregationcoef')) {
483                    $mform->removeElement('grade_item_aggregationcoef');
484                }
485
486                if ($mform->elementExists('grade_item_weightoverride')) {
487                    $mform->removeElement('grade_item_weightoverride');
488                }
489                if ($mform->elementExists('grade_item_aggregationcoef2')) {
490                    $mform->removeElement('grade_item_aggregationcoef2');
491                }
492            } else {
493                if ($grade_item->is_category_item()) {
494                    $category = $grade_item->get_item_category();
495                    $parent_category = $category->get_parent_category();
496                } else {
497                    $parent_category = $grade_item->get_parent_category();
498                }
499
500                $parent_category->apply_forced_settings();
501
502                if (!$parent_category->is_aggregationcoef_used()) {
503                    if ($mform->elementExists('grade_item_aggregationcoef')) {
504                        $mform->removeElement('grade_item_aggregationcoef');
505                    }
506                } else {
507
508                    $coefstring = $grade_item->get_coefstring();
509
510                    if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') {
511                        // advcheckbox is not compatible with disabledIf!
512                        $coefstring = 'aggregationcoefextrasum';
513                        $element =& $mform->createElement('checkbox', 'grade_item_aggregationcoef', get_string($coefstring, 'grades'));
514                    } else {
515                        $element =& $mform->createElement('text', 'grade_item_aggregationcoef', get_string($coefstring, 'grades'));
516                    }
517                    $mform->insertElementBefore($element, 'parentcategory');
518                    $mform->addHelpButton('grade_item_aggregationcoef', $coefstring, 'grades');
519                }
520
521                // Remove fields used by natural weighting if the parent category is not using natural weighting.
522                // Or if the item is a scale and scales are not used in aggregation.
523                if ($parent_category->aggregation != GRADE_AGGREGATE_SUM
524                        || (empty($CFG->grade_includescalesinaggregation) && $grade_item->gradetype == GRADE_TYPE_SCALE)) {
525                    if ($mform->elementExists('grade_item_weightoverride')) {
526                        $mform->removeElement('grade_item_weightoverride');
527                    }
528                    if ($mform->elementExists('grade_item_aggregationcoef2')) {
529                        $mform->removeElement('grade_item_aggregationcoef2');
530                    }
531                }
532            }
533        }
534    }
535
536/// perform extra validation before submission
537    function validation($data, $files) {
538        global $COURSE;
539        $gradeitem = false;
540        if ($data['id']) {
541            $gradecategory = grade_category::fetch(array('id' => $data['id']));
542            $gradeitem = $gradecategory->load_grade_item();
543        }
544
545        $errors = parent::validation($data, $files);
546
547        if (array_key_exists('grade_item_gradetype', $data) and $data['grade_item_gradetype'] == GRADE_TYPE_SCALE) {
548            if (empty($data['grade_item_scaleid'])) {
549                $errors['grade_item_scaleid'] = get_string('missingscale', 'grades');
550            }
551        }
552        if (array_key_exists('grade_item_grademin', $data) and array_key_exists('grade_item_grademax', $data)) {
553            if (($data['grade_item_grademax'] != 0 OR $data['grade_item_grademin'] != 0) AND
554                ($data['grade_item_grademax'] == $data['grade_item_grademin'] OR
555                 $data['grade_item_grademax'] < $data['grade_item_grademin'])) {
556                 $errors['grade_item_grademin'] = get_string('incorrectminmax', 'grades');
557                 $errors['grade_item_grademax'] = get_string('incorrectminmax', 'grades');
558             }
559        }
560
561        if ($data['id'] && $gradeitem->has_overridden_grades()) {
562            if ($gradeitem->gradetype == GRADE_TYPE_VALUE) {
563                if (grade_floats_different($data['grade_item_grademin'], $gradeitem->grademin) ||
564                    grade_floats_different($data['grade_item_grademax'], $gradeitem->grademax)) {
565                    if (empty($data['grade_item_rescalegrades'])) {
566                        $errors['grade_item_rescalegrades'] = get_string('mustchooserescaleyesorno', 'grades');
567                    }
568                }
569            }
570        }
571        return $errors;
572    }
573}
574
575
576