MDL-34612 Grade condition range validation allows impossible conditions
[moodle.git] / course / editsection_form.php
1 <?php
3 if (!defined('MOODLE_INTERNAL')) {
4     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
5 }
7 require_once($CFG->libdir.'/formslib.php');
9 class editsection_form extends moodleform {
11     function definition() {
13         $mform  = $this->_form;
14         $course = $this->_customdata['course'];
15         $mform->addElement('checkbox', 'usedefaultname', get_string('sectionusedefaultname'));
16         $mform->setDefault('usedefaultname', true);
18         $mform->addElement('text', 'name', get_string('sectionname'), array('size'=>'30'));
19         $mform->setType('name', PARAM_TEXT);
20         $mform->disabledIf('name','usedefaultname','checked');
22         /// Prepare course and the editor
24         $mform->addElement('editor', 'summary_editor', get_string('summary'), null, $this->_customdata['editoroptions']);
25         $mform->addHelpButton('summary_editor', 'summary');
26         $mform->setType('summary_editor', PARAM_RAW);
28         $mform->addElement('hidden', 'id');
29         $mform->setType('id', PARAM_INT);
31         $mform->_registerCancelButton('cancel');
32     }
34     public function definition_after_data() {
35         global $CFG, $DB;
37         $mform  = $this->_form;
38         $course = $this->_customdata['course'];
40         if (!empty($CFG->enableavailability)) {
41             $mform->addElement('header', '', get_string('availabilityconditions', 'condition'));
42             // Grouping conditions - only if grouping is enabled at site level
43             if (!empty($CFG->enablegroupmembersonly)) {
44                 $options = array();
45                 $options[0] = get_string('none');
46                 if ($groupings = $DB->get_records('groupings', array('courseid' => $course->id))) {
47                     foreach ($groupings as $grouping) {
48                         $context = context_course::instance($course->id);
49                         $options[$grouping->id] = format_string(
50                                 $grouping->name, true, array('context' => $context));
51                     }
52                 }
53                 $mform->addElement('select', 'groupingid', get_string('groupingsection', 'group'), $options);
54                 $mform->addHelpButton('groupingid', 'groupingsection', 'group');
55             }
57             // Date and time conditions
58             $mform->addElement('date_time_selector', 'availablefrom',
59                     get_string('availablefrom', 'condition'), array('optional' => true));
60             $mform->addElement('date_time_selector', 'availableuntil',
61                     get_string('availableuntil', 'condition'), array('optional' => true));
63             // Conditions based on grades
64             $gradeoptions = array();
65             $items = grade_item::fetch_all(array('courseid' => $course->id));
66             $items = $items ? $items : array();
67             foreach ($items as $id => $item) {
68                 $gradeoptions[$id] = $item->get_name();
69             }
70             asort($gradeoptions);
71             $gradeoptions = array(0 => get_string('none', 'condition')) + $gradeoptions;
73             $grouparray = array();
74             $grouparray[] = $mform->createElement('select', 'conditiongradeitemid', '', $gradeoptions);
75             $grouparray[] = $mform->createElement('static', '', '',
76                     ' ' . get_string('grade_atleast', 'condition').' ');
77             $grouparray[] = $mform->createElement('text', 'conditiongrademin', '', array('size' => 3));
78             $grouparray[] = $mform->createElement('static', '', '',
79                     '% ' . get_string('grade_upto', 'condition') . ' ');
80             $grouparray[] = $mform->createElement('text', 'conditiongrademax', '', array('size' => 3));
81             $grouparray[] = $mform->createElement('static', '', '', '%');
82             $group = $mform->createElement('group', 'conditiongradegroup',
83                     get_string('gradecondition', 'condition'), $grouparray);
85             // Get full version (including condition info) of section object
86             $ci = new condition_info_section($this->_customdata['cs']);
87             $fullcs = $ci->get_full_section();
88             $count = count($fullcs->conditionsgrade) + 1;
90             // Grade conditions
91             $this->repeat_elements(array($group), $count, array(), 'conditiongraderepeats',
92                     'conditiongradeadds', 2, get_string('addgrades', 'condition'), true);
93             $mform->addHelpButton('conditiongradegroup[0]', 'gradecondition', 'condition');
95             // Conditions based on completion
96             $completion = new completion_info($course);
97             if ($completion->is_enabled()) {
98                 $completionoptions = array();
99                 $modinfo = get_fast_modinfo($course);
100                 foreach ($modinfo->cms as $id => $cm) {
101                     // Add each course-module if it:
102                     // (a) has completion turned on
103                     // (b) does not belong to current course-section
104                     if ($cm->completion && ($fullcs->id != $cm->section)) {
105                         $completionoptions[$id] = $cm->name;
106                     }
107                 }
108                 asort($completionoptions);
109                 $completionoptions = array(0 => get_string('none', 'condition')) +
110                         $completionoptions;
112                 $completionvalues = array(
113                     COMPLETION_COMPLETE => get_string('completion_complete', 'condition'),
114                     COMPLETION_INCOMPLETE => get_string('completion_incomplete', 'condition'),
115                     COMPLETION_COMPLETE_PASS => get_string('completion_pass', 'condition'),
116                     COMPLETION_COMPLETE_FAIL => get_string('completion_fail', 'condition'));
118                 $grouparray = array();
119                 $grouparray[] = $mform->createElement('select', 'conditionsourcecmid', '',
120                         $completionoptions);
121                 $grouparray[] = $mform->createElement('select', 'conditionrequiredcompletion', '',
122                         $completionvalues);
123                 $group = $mform->createElement('group', 'conditioncompletiongroup',
124                         get_string('completioncondition', 'condition'), $grouparray);
126                 $count = count($fullcs->conditionscompletion) + 1;
127                 $this->repeat_elements(array($group), $count, array(),
128                         'conditioncompletionrepeats', 'conditioncompletionadds', 2,
129                         get_string('addcompletions', 'condition'), true);
130                 $mform->addHelpButton('conditioncompletiongroup[0]',
131                         'completionconditionsection', 'condition');
132             }
134             // Availability conditions - set up form values
135             if (!empty($CFG->enableavailability)) {
136                 $num = 0;
137                 foreach ($fullcs->conditionsgrade as $gradeitemid => $minmax) {
138                     $groupelements = $mform->getElement(
139                             'conditiongradegroup[' . $num . ']')->getElements();
140                     $groupelements[0]->setValue($gradeitemid);
141                     $groupelements[2]->setValue(is_null($minmax->min) ? '' :
142                             format_float($minmax->min, 5, true, true));
143                     $groupelements[4]->setValue(is_null($minmax->max) ? '' :
144                             format_float($minmax->max, 5, true, true));
145                     $num++;
146                 }
148                 if ($completion->is_enabled()) {
149                     $num = 0;
150                     foreach ($fullcs->conditionscompletion as $othercmid => $state) {
151                         $groupelements = $mform->getElement('conditioncompletiongroup[' . $num . ']')->getElements();
152                         $groupelements[0]->setValue($othercmid);
153                         $groupelements[1]->setValue($state);
154                         $num++;
155                     }
156                 }
157             }
159             // Do we display availability info to students?
160             $showhide = array(
161                 CONDITION_STUDENTVIEW_SHOW => get_string('showavailabilitysection_show', 'condition'),
162                 CONDITION_STUDENTVIEW_HIDE => get_string('showavailabilitysection_hide', 'condition'));
163             $mform->addElement('select', 'showavailability',
164                     get_string('showavailabilitysection', 'condition'), $showhide);
166             $mform->setDefault('showavailability', $this->_customdata['showavailability']);
167         }
169         $this->add_action_buttons();
170     }
172     public function validation($data, $files) {
173         $errors = parent::validation($data, $files);
174         // Conditions: Don't let them set dates which make no sense
175         if (array_key_exists('availablefrom', $data) &&
176                 $data['availablefrom'] && $data['availableuntil'] &&
177                 $data['availablefrom'] >= $data['availableuntil']) {
178             $errors['availablefrom'] = get_string('badavailabledates', 'condition');
179         }
181         // Conditions: Verify that the grade conditions are numbers, and make sense.
182         if (array_key_exists('conditiongradegroup', $data)) {
183             foreach ($data['conditiongradegroup'] as $i => $gradedata) {
184                 if ($gradedata['conditiongrademin'] !== '' &&
185                         !is_numeric(unformat_float($gradedata['conditiongrademin']))) {
186                     $errors["conditiongradegroup[{$i}]"] = get_string('gradesmustbenumeric', 'condition');
187                     continue;
188                 }
189                 if ($gradedata['conditiongrademax'] !== '' &&
190                         !is_numeric(unformat_float($gradedata['conditiongrademax']))) {
191                     $errors["conditiongradegroup[{$i}]"] = get_string('gradesmustbenumeric', 'condition');
192                     continue;
193                 }
194                 if ($gradedata['conditiongrademin'] !== '' && $gradedata['conditiongrademax'] !== '' &&
195                         unformat_float($gradedata['conditiongrademax']) <= unformat_float($gradedata['conditiongrademin'])) {
196                     $errors["conditiongradegroup[{$i}]"] = get_string('badgradelimits', 'condition');
197                     continue;
198                 }
199                 if ($gradedata['conditiongrademin'] === '' && $gradedata['conditiongrademax'] === '' &&
200                         $gradedata['conditiongradeitemid']) {
201                     $errors["conditiongradegroup[{$i}]"] = get_string('gradeitembutnolimits', 'condition');
202                     continue;
203                 }
204                 if (($gradedata['conditiongrademin'] !== '' || $gradedata['conditiongrademax'] !== '') &&
205                         !$gradedata['conditiongradeitemid']) {
206                     $errors["conditiongradegroup[{$i}]"] = get_string('gradelimitsbutnoitem', 'condition');
207                     continue;
208                 }
209             }
210         }
212         return $errors;
213     }