Merge branch 'MDL-36412-m23' of git://github.com/sammarshallou/moodle into MOODLE_23_...
[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             // Available from/to defaults to midnight because then the display
58             // will be nicer where it tells users when they can access it (it
59             // shows only the date and not time).
60             $date = usergetdate(time());
61             $midnight = make_timestamp($date['year'], $date['mon'], $date['mday']);
63             // Date and time conditions.
64             $mform->addElement('date_time_selector', 'availablefrom',
65                     get_string('availablefrom', 'condition'),
66                     array('optional' => true, 'defaulttime' => $midnight));
67             $mform->addElement('date_time_selector', 'availableuntil',
68                     get_string('availableuntil', 'condition'),
69                     array('optional' => true, 'defaulttime' => $midnight));
71             // Conditions based on grades
72             $gradeoptions = array();
73             $items = grade_item::fetch_all(array('courseid' => $course->id));
74             $items = $items ? $items : array();
75             foreach ($items as $id => $item) {
76                 $gradeoptions[$id] = $item->get_name();
77             }
78             asort($gradeoptions);
79             $gradeoptions = array(0 => get_string('none', 'condition')) + $gradeoptions;
81             $grouparray = array();
82             $grouparray[] = $mform->createElement('select', 'conditiongradeitemid', '', $gradeoptions);
83             $grouparray[] = $mform->createElement('static', '', '',
84                     ' ' . get_string('grade_atleast', 'condition').' ');
85             $grouparray[] = $mform->createElement('text', 'conditiongrademin', '', array('size' => 3));
86             $grouparray[] = $mform->createElement('static', '', '',
87                     '% ' . get_string('grade_upto', 'condition') . ' ');
88             $grouparray[] = $mform->createElement('text', 'conditiongrademax', '', array('size' => 3));
89             $grouparray[] = $mform->createElement('static', '', '', '%');
90             $group = $mform->createElement('group', 'conditiongradegroup',
91                     get_string('gradecondition', 'condition'), $grouparray);
93             // Get full version (including condition info) of section object
94             $ci = new condition_info_section($this->_customdata['cs']);
95             $fullcs = $ci->get_full_section();
96             $count = count($fullcs->conditionsgrade) + 1;
98             // Grade conditions
99             $this->repeat_elements(array($group), $count, array(), 'conditiongraderepeats',
100                     'conditiongradeadds', 2, get_string('addgrades', 'condition'), true);
101             $mform->addHelpButton('conditiongradegroup[0]', 'gradecondition', 'condition');
103             // Conditions based on completion
104             $completion = new completion_info($course);
105             if ($completion->is_enabled()) {
106                 $completionoptions = array();
107                 $modinfo = get_fast_modinfo($course);
108                 foreach ($modinfo->cms as $id => $cm) {
109                     // Add each course-module if it:
110                     // (a) has completion turned on
111                     // (b) does not belong to current course-section
112                     if ($cm->completion && ($fullcs->id != $cm->section)) {
113                         $completionoptions[$id] = $cm->name;
114                     }
115                 }
116                 asort($completionoptions);
117                 $completionoptions = array(0 => get_string('none', 'condition')) +
118                         $completionoptions;
120                 $completionvalues = array(
121                     COMPLETION_COMPLETE => get_string('completion_complete', 'condition'),
122                     COMPLETION_INCOMPLETE => get_string('completion_incomplete', 'condition'),
123                     COMPLETION_COMPLETE_PASS => get_string('completion_pass', 'condition'),
124                     COMPLETION_COMPLETE_FAIL => get_string('completion_fail', 'condition'));
126                 $grouparray = array();
127                 $grouparray[] = $mform->createElement('select', 'conditionsourcecmid', '',
128                         $completionoptions);
129                 $grouparray[] = $mform->createElement('select', 'conditionrequiredcompletion', '',
130                         $completionvalues);
131                 $group = $mform->createElement('group', 'conditioncompletiongroup',
132                         get_string('completioncondition', 'condition'), $grouparray);
134                 $count = count($fullcs->conditionscompletion) + 1;
135                 $this->repeat_elements(array($group), $count, array(),
136                         'conditioncompletionrepeats', 'conditioncompletionadds', 2,
137                         get_string('addcompletions', 'condition'), true);
138                 $mform->addHelpButton('conditioncompletiongroup[0]',
139                         'completionconditionsection', 'condition');
140             }
142             // Availability conditions - set up form values
143             if (!empty($CFG->enableavailability)) {
144                 $num = 0;
145                 foreach ($fullcs->conditionsgrade as $gradeitemid => $minmax) {
146                     $groupelements = $mform->getElement(
147                             'conditiongradegroup[' . $num . ']')->getElements();
148                     $groupelements[0]->setValue($gradeitemid);
149                     $groupelements[2]->setValue(is_null($minmax->min) ? '' :
150                             format_float($minmax->min, 5, true, true));
151                     $groupelements[4]->setValue(is_null($minmax->max) ? '' :
152                             format_float($minmax->max, 5, true, true));
153                     $num++;
154                 }
156                 if ($completion->is_enabled()) {
157                     $num = 0;
158                     foreach ($fullcs->conditionscompletion as $othercmid => $state) {
159                         $groupelements = $mform->getElement('conditioncompletiongroup[' . $num . ']')->getElements();
160                         $groupelements[0]->setValue($othercmid);
161                         $groupelements[1]->setValue($state);
162                         $num++;
163                     }
164                 }
165             }
167             // Do we display availability info to students?
168             $showhide = array(
169                 CONDITION_STUDENTVIEW_SHOW => get_string('showavailabilitysection_show', 'condition'),
170                 CONDITION_STUDENTVIEW_HIDE => get_string('showavailabilitysection_hide', 'condition'));
171             $mform->addElement('select', 'showavailability',
172                     get_string('showavailabilitysection', 'condition'), $showhide);
173         }
175         $this->add_action_buttons();
176     }
178     public function validation($data, $files) {
179         $errors = parent::validation($data, $files);
180         // Conditions: Don't let them set dates which make no sense
181         if (array_key_exists('availablefrom', $data) &&
182                 $data['availablefrom'] && $data['availableuntil'] &&
183                 $data['availablefrom'] >= $data['availableuntil']) {
184             $errors['availablefrom'] = get_string('badavailabledates', 'condition');
185         }
187         // Conditions: Verify that the grade conditions are numbers, and make sense.
188         if (array_key_exists('conditiongradegroup', $data)) {
189             foreach ($data['conditiongradegroup'] as $i => $gradedata) {
190                 if ($gradedata['conditiongrademin'] !== '' &&
191                         !is_numeric(unformat_float($gradedata['conditiongrademin']))) {
192                     $errors["conditiongradegroup[{$i}]"] = get_string('gradesmustbenumeric', 'condition');
193                     continue;
194                 }
195                 if ($gradedata['conditiongrademax'] !== '' &&
196                         !is_numeric(unformat_float($gradedata['conditiongrademax']))) {
197                     $errors["conditiongradegroup[{$i}]"] = get_string('gradesmustbenumeric', 'condition');
198                     continue;
199                 }
200                 if ($gradedata['conditiongrademin'] !== '' && $gradedata['conditiongrademax'] !== '' &&
201                         unformat_float($gradedata['conditiongrademax']) <= unformat_float($gradedata['conditiongrademin'])) {
202                     $errors["conditiongradegroup[{$i}]"] = get_string('badgradelimits', 'condition');
203                     continue;
204                 }
205                 if ($gradedata['conditiongrademin'] === '' && $gradedata['conditiongrademax'] === '' &&
206                         $gradedata['conditiongradeitemid']) {
207                     $errors["conditiongradegroup[{$i}]"] = get_string('gradeitembutnolimits', 'condition');
208                     continue;
209                 }
210                 if (($gradedata['conditiongrademin'] !== '' || $gradedata['conditiongrademax'] !== '') &&
211                         !$gradedata['conditiongradeitemid']) {
212                     $errors["conditiongradegroup[{$i}]"] = get_string('gradelimitsbutnoitem', 'condition');
213                     continue;
214                 }
215             }
216         }
218         return $errors;
219     }