forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
editsection_form.php
330 lines (291 loc) · 16.1 KB
/
editsection_form.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
<?php
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/filelib.php');
require_once($CFG->libdir.'/completionlib.php');
require_once($CFG->libdir.'/gradelib.php');
/**
* Default form for editing course section
*
* Course format plugins may specify different editing form to use
*/
class editsection_form extends moodleform {
function definition() {
$mform = $this->_form;
$course = $this->_customdata['course'];
$mform->addElement('checkbox', 'usedefaultname', get_string('sectionusedefaultname'));
$mform->setDefault('usedefaultname', true);
$mform->addElement('text', 'name', get_string('sectionname'), array('size'=>'30'));
$mform->setType('name', PARAM_TEXT);
$mform->disabledIf('name','usedefaultname','checked');
/// Prepare course and the editor
$mform->addElement('editor', 'summary_editor', get_string('summary'), null, $this->_customdata['editoroptions']);
$mform->addHelpButton('summary_editor', 'summary');
$mform->setType('summary_editor', PARAM_RAW);
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
// additional fields that course format has defined
$courseformat = course_get_format($course);
$formatoptions = $courseformat->section_format_options(true);
if (!empty($formatoptions)) {
$elements = $courseformat->create_edit_form_elements($mform, true);
}
$mform->_registerCancelButton('cancel');
}
public function definition_after_data() {
global $CFG, $DB;
$mform = $this->_form;
$course = $this->_customdata['course'];
if (!empty($CFG->enableavailability)) {
$mform->addElement('header', '', get_string('availabilityconditions', 'condition'));
// String used by conditions more than once
$strcondnone = get_string('none', 'condition');
// Grouping conditions - only if grouping is enabled at site level
if (!empty($CFG->enablegroupmembersonly)) {
$options = array();
$options[0] = get_string('none');
if ($groupings = $DB->get_records('groupings', array('courseid' => $course->id))) {
foreach ($groupings as $grouping) {
$context = context_course::instance($course->id);
$options[$grouping->id] = format_string(
$grouping->name, true, array('context' => $context));
}
}
$mform->addElement('select', 'groupingid', get_string('groupingsection', 'group'), $options);
$mform->addHelpButton('groupingid', 'groupingsection', 'group');
}
// Available from/to defaults to midnight because then the display
// will be nicer where it tells users when they can access it (it
// shows only the date and not time).
$date = usergetdate(time());
$midnight = make_timestamp($date['year'], $date['mon'], $date['mday']);
// Date and time conditions.
$mform->addElement('date_time_selector', 'availablefrom',
get_string('availablefrom', 'condition'),
array('optional' => true, 'defaulttime' => $midnight));
$mform->addElement('date_time_selector', 'availableuntil',
get_string('availableuntil', 'condition'),
array('optional' => true, 'defaulttime' => $midnight));
// Conditions based on grades
$gradeoptions = array();
$items = grade_item::fetch_all(array('courseid' => $course->id));
$items = $items ? $items : array();
foreach ($items as $id => $item) {
$gradeoptions[$id] = $item->get_name();
}
asort($gradeoptions);
$gradeoptions = array(0 => $strcondnone) + $gradeoptions;
$grouparray = array();
$grouparray[] = $mform->createElement('select', 'conditiongradeitemid', '', $gradeoptions);
$grouparray[] = $mform->createElement('static', '', '',
' ' . get_string('grade_atleast', 'condition').' ');
$grouparray[] = $mform->createElement('text', 'conditiongrademin', '', array('size' => 3));
$grouparray[] = $mform->createElement('static', '', '',
'% ' . get_string('grade_upto', 'condition') . ' ');
$grouparray[] = $mform->createElement('text', 'conditiongrademax', '', array('size' => 3));
$grouparray[] = $mform->createElement('static', '', '', '%');
$group = $mform->createElement('group', 'conditiongradegroup',
get_string('gradecondition', 'condition'), $grouparray);
// Get full version (including condition info) of section object
$ci = new condition_info_section($this->_customdata['cs']);
$fullcs = $ci->get_full_section();
$count = count($fullcs->conditionsgrade) + 1;
// Grade conditions
$this->repeat_elements(array($group), $count, array(), 'conditiongraderepeats',
'conditiongradeadds', 2, get_string('addgrades', 'condition'), true);
$mform->addHelpButton('conditiongradegroup[0]', 'gradecondition', 'condition');
// Conditions based on user fields
$operators = condition_info::get_condition_user_field_operators();
$useroptions = condition_info::get_condition_user_fields();
asort($useroptions);
$useroptions = array(0 => $strcondnone) + $useroptions;
$grouparray = array();
$grouparray[] =& $mform->createElement('select', 'conditionfield', '', $useroptions);
$grouparray[] =& $mform->createElement('select', 'conditionfieldoperator', '', $operators);
$grouparray[] =& $mform->createElement('text', 'conditionfieldvalue');
$mform->setType('conditionfieldvalue', PARAM_RAW);
$group = $mform->createElement('group', 'conditionfieldgroup', get_string('userfield', 'condition'), $grouparray);
$fieldcount = count($fullcs->conditionsfield) + 1;
$this->repeat_elements(array($group), $fieldcount, array(), 'conditionfieldrepeats', 'conditionfieldadds', 2,
get_string('adduserfields', 'condition'), true);
$mform->addHelpButton('conditionfieldgroup[0]', 'userfield', 'condition');
// Conditions based on completion
$completion = new completion_info($course);
if ($completion->is_enabled()) {
$completionoptions = array();
$modinfo = get_fast_modinfo($course);
foreach ($modinfo->cms as $id => $cm) {
// Add each course-module if it:
// (a) has completion turned on
// (b) does not belong to current course-section
if ($cm->completion && ($fullcs->id != $cm->section)) {
$completionoptions[$id] = $cm->name;
}
}
asort($completionoptions);
$completionoptions = array(0 => $strcondnone) +
$completionoptions;
$completionvalues = array(
COMPLETION_COMPLETE => get_string('completion_complete', 'condition'),
COMPLETION_INCOMPLETE => get_string('completion_incomplete', 'condition'),
COMPLETION_COMPLETE_PASS => get_string('completion_pass', 'condition'),
COMPLETION_COMPLETE_FAIL => get_string('completion_fail', 'condition'));
$grouparray = array();
$grouparray[] = $mform->createElement('select', 'conditionsourcecmid', '',
$completionoptions);
$grouparray[] = $mform->createElement('select', 'conditionrequiredcompletion', '',
$completionvalues);
$group = $mform->createElement('group', 'conditioncompletiongroup',
get_string('completioncondition', 'condition'), $grouparray);
$count = count($fullcs->conditionscompletion) + 1;
$this->repeat_elements(array($group), $count, array(),
'conditioncompletionrepeats', 'conditioncompletionadds', 2,
get_string('addcompletions', 'condition'), true);
$mform->addHelpButton('conditioncompletiongroup[0]',
'completionconditionsection', 'condition');
}
// Availability conditions - set up form values
if (!empty($CFG->enableavailability)) {
$num = 0;
foreach ($fullcs->conditionsgrade as $gradeitemid => $minmax) {
$groupelements = $mform->getElement(
'conditiongradegroup[' . $num . ']')->getElements();
$groupelements[0]->setValue($gradeitemid);
$groupelements[2]->setValue(is_null($minmax->min) ? '' :
format_float($minmax->min, 5, true, true));
$groupelements[4]->setValue(is_null($minmax->max) ? '' :
format_float($minmax->max, 5, true, true));
$num++;
}
$num = 0;
foreach ($fullcs->conditionsfield as $fieldid => $data) {
$groupelements = $mform->getElement(
'conditionfieldgroup[' . $num . ']')->getElements();
$groupelements[0]->setValue($fieldid);
$groupelements[1]->setValue(is_null($data->operator) ? '' :
$data->operator);
$groupelements[2]->setValue(is_null($data->value) ? '' :
$data->value);
$num++;
}
if ($completion->is_enabled()) {
$num = 0;
foreach ($fullcs->conditionscompletion as $othercmid => $state) {
$groupelements = $mform->getElement('conditioncompletiongroup[' . $num . ']')->getElements();
$groupelements[0]->setValue($othercmid);
$groupelements[1]->setValue($state);
$num++;
}
}
}
// Do we display availability info to students?
$showhide = array(
CONDITION_STUDENTVIEW_SHOW => get_string('showavailabilitysection_show', 'condition'),
CONDITION_STUDENTVIEW_HIDE => get_string('showavailabilitysection_hide', 'condition'));
$mform->addElement('select', 'showavailability',
get_string('showavailabilitysection', 'condition'), $showhide);
}
$this->add_action_buttons();
}
public function validation($data, $files) {
$errors = parent::validation($data, $files);
// Conditions: Don't let them set dates which make no sense
if (array_key_exists('availablefrom', $data) &&
$data['availablefrom'] && $data['availableuntil'] &&
$data['availablefrom'] >= $data['availableuntil']) {
$errors['availablefrom'] = get_string('badavailabledates', 'condition');
}
// Conditions: Verify that the grade conditions are numbers, and make sense.
if (array_key_exists('conditiongradegroup', $data)) {
foreach ($data['conditiongradegroup'] as $i => $gradedata) {
if ($gradedata['conditiongrademin'] !== '' &&
!is_numeric(unformat_float($gradedata['conditiongrademin']))) {
$errors["conditiongradegroup[{$i}]"] = get_string('gradesmustbenumeric', 'condition');
continue;
}
if ($gradedata['conditiongrademax'] !== '' &&
!is_numeric(unformat_float($gradedata['conditiongrademax']))) {
$errors["conditiongradegroup[{$i}]"] = get_string('gradesmustbenumeric', 'condition');
continue;
}
if ($gradedata['conditiongrademin'] !== '' && $gradedata['conditiongrademax'] !== '' &&
unformat_float($gradedata['conditiongrademax']) <= unformat_float($gradedata['conditiongrademin'])) {
$errors["conditiongradegroup[{$i}]"] = get_string('badgradelimits', 'condition');
continue;
}
if ($gradedata['conditiongrademin'] === '' && $gradedata['conditiongrademax'] === '' &&
$gradedata['conditiongradeitemid']) {
$errors["conditiongradegroup[{$i}]"] = get_string('gradeitembutnolimits', 'condition');
continue;
}
if (($gradedata['conditiongrademin'] !== '' || $gradedata['conditiongrademax'] !== '') &&
!$gradedata['conditiongradeitemid']) {
$errors["conditiongradegroup[{$i}]"] = get_string('gradelimitsbutnoitem', 'condition');
continue;
}
}
}
// Conditions: Verify that the user profile field has not been declared more than once
if (array_key_exists('conditionfieldgroup', $data)) {
// Array to store the existing fields
$arrcurrentfields = array();
// Error message displayed if any condition is declared more than once. We use lang string because
// this way we don't actually generate the string unless there is an error.
$stralreadydeclaredwarning = new lang_string('fielddeclaredmultipletimes', 'condition');
foreach ($data['conditionfieldgroup'] as $i => $fielddata) {
if ($fielddata['conditionfield'] == 0) { // Don't need to bother if none is selected
continue;
}
if (in_array($fielddata['conditionfield'], $arrcurrentfields)) {
$errors["conditionfieldgroup[{$i}]"] = $stralreadydeclaredwarning->out();
}
// Add the field to the array
$arrcurrentfields[] = $fielddata['conditionfield'];
}
}
return $errors;
}
/**
* Load in existing data as form defaults
*
* @param stdClass|array $default_values object or array of default values
*/
function set_data($default_values) {
if (!is_object($default_values)) {
// we need object for file_prepare_standard_editor
$default_values = (object)$default_values;
}
$editoroptions = $this->_customdata['editoroptions'];
$default_values = file_prepare_standard_editor($default_values, 'summary', $editoroptions,
$editoroptions['context'], 'course', 'section', $default_values->id);
$default_values->usedefaultname = (is_null($default_values->name));
parent::set_data($default_values);
}
/**
* Return submitted data if properly submitted or returns NULL if validation fails or
* if there is no submitted data.
*
* @return object submitted data; NULL if not valid or not submitted or cancelled
*/
function get_data() {
$data = parent::get_data();
if ($data !== null) {
$editoroptions = $this->_customdata['editoroptions'];
if (!empty($data->usedefaultname)) {
$data->name = null;
}
$data = file_postupdate_standard_editor($data, 'summary', $editoroptions,
$editoroptions['context'], 'course', 'section', $data->id);
$course = $this->_customdata['course'];
foreach (course_get_format($course)->section_format_options() as $option => $unused) {
// fix issue with unset checkboxes not being returned at all
if (!isset($data->$option)) {
$data->$option = null;
}
}
}
return $data;
}
}