-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod_form.php
369 lines (322 loc) · 15.6 KB
/
mod_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
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
<?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/>.
/**
* This file contains the forms to create and edit an instance of this module
*
* @package mod_reservation
* @copyright 2011 onwards Roberto Pinna
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/course/moodleform_mod.php');
require_once($CFG->dirroot.'/mod/reservation/locallib.php');
/**
* Reservation settings form
*
* @package mod_reservation
* @copyright 2011 onwards Roberto Pinna
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_reservation_mod_form extends moodleform_mod {
/**
* Called to define this moodle form
*
* @return void
*/
public function definition() {
global $CFG, $COURSE, $DB, $PAGE;
$mform =& $this->_form;
$reservationconfig = get_config('reservation');
$mform->addElement('header', 'general', get_string('general', 'form'));
// Name.
$mform->addElement('text', 'name', get_string('name'));
$mform->setType('name', PARAM_TEXT);
$mform->addRule('name', null, 'required', null, 'client');
// Description.
$this->standard_intro_elements(get_string('description', 'reservation'));
// Event Settings.
$mform->addElement('header', 'eventsettings', get_string('eventsettings', 'reservation'));
$mform->setExpanded('eventsettings');
$context = context_course::instance($COURSE->id);
$capability = 'moodle/course:viewhiddenactivities';
if (!empty($this->_cm)) {
$context = context_module::instance($this->_cm->id);
// Fix for tutors.
$capability = 'mod/reservation:viewrequest';
}
if ($teacherusers = get_enrolled_users($context, $capability, 0, 'u.*', 'u.lastname ASC')) {
$availableteachers = [];
foreach ($teacherusers as $teacheruser) {
if (! has_capability('mod/reservation:reserve', $context, $teacheruser)) {
$availableteachers[$teacheruser->id] = fullname($teacheruser);
}
}
$teacherselect = &$mform->addElement('select', 'teachers', get_string('teachers'), $availableteachers);
$teacherselect->setMultiple(true);
} else {
$mform->addElement('static', 'noteachers', get_string('teachers'), get_string('noteachers', 'reservation'));
$mform->addElement('hidden', 'teachers');
$mform->setType('teachers', PARAM_TEXT);
}
$locationgrp = [];
$locationsize = 60;
if ($locations = $DB->get_records_menu('reservation_location')) {
$associativelocations = [];
foreach ($locations as $location) {
$associativelocations[$location] = $location;
}
if (extension_loaded('intl') === true) {
collator_asort(collator_create('root'), $associativelocations);
} else {
natcasesort($associativelocations);
}
$locations = array_merge([0 => get_string('otherlocation', 'reservation')], $associativelocations);
$onchange = 'onchange="getElementById(\'id_locationtext\').value=\'\'"';
$locationgrp[] = &$mform->createElement('select', 'location', null, $locations, $onchange);
$locationsize = 40;
}
$locationgrp[] = &$mform->createElement('text', 'locationtext', 'size="'.$locationsize.'"');
$mform->setType('locationtext', PARAM_TEXT);
$mform->disabledIf('locationtext', 'location', 'ne', '0');
$mform->addGroup($locationgrp, 'locationgrp', get_string('location', 'reservation'), ' ', false);
$mform->addElement('date_time_selector', 'timestart', get_string('timestart', 'reservation'));
$mform->addElement('date_time_selector', 'timeend', get_string('timeend', 'reservation'), ['optional' => true]);
if (!empty($reservationconfig->check_clashes)) {
$mform->addElement('static', 'collision', '',
'<button type="button" id="checkclashes" class="btn btn-primary">' .
get_string('checkclashes', 'reservation') . '</button>');
}
// Reservation Settings.
$mform->addElement('header', 'reservationsettings', get_string('reservationsettings', 'reservation'));
$mform->setExpanded('reservationsettings');
$mform->addElement('date_time_selector', 'timeopen', get_string('timeopen', 'reservation'), ['optional' => true]);
$mform->addElement('date_time_selector', 'timeclose', get_string('timeclose', 'reservation'));
$choices = [];
$choices[0] = get_string('no');
$choices[1] = get_string('optional', 'reservation');
$choices[2] = get_string('required', 'reservation');
$mform->addElement('select', 'note', get_string('enablenote', 'reservation'), $choices);
if (!isset($reservationconfig->max_requests)) {
$reservationconfig->max_requests = '100';
}
$values = [0 => get_string('nolimit', 'reservation')];
for ($i = 1; $i <= $reservationconfig->max_requests; $i++) {
$values[$i] = "$i";
}
$mform->addElement('select', 'maxrequest', get_string('maxrequest', 'reservation'), $values);
$choices = [];
$choices[0] = get_string('numberafterclose', 'reservation');
$choices[1] = get_string('listafterclose', 'reservation');
$choices[2] = get_string('listalways', 'reservation');
$choices[3] = get_string('numberalways', 'reservation');
$choices[4] = get_string('none', 'reservation');
$mform->addElement('select', 'showrequest', get_string('showuserrequest', 'reservation'), $choices);
$reservationid = $this->_instance;
if ($reservations = reservation_get_parentable($reservationid)) {
if (isset($reservationconfig->connect_to) && ($reservationconfig->connect_to == 'site')) {
$displaylist = [];
if (class_exists('core_course_category')) {
$displaylist = core_course_category::make_categories_list();
} else {
require_once($CFG->libdir. '/coursecatlib.php');
$displaylist = coursecat::make_categories_list();
}
}
$values = [0 => get_string('noparent', 'reservation')];
foreach ($reservations as $reservation) {
$value = $reservation->coursename.': '.$reservation->name;
if (isset($reservationconfig->connect_to) && ($reservationconfig->connect_to == 'site')) {
$value = $displaylist[$reservation->category] .'/'. $value;
}
$values[$reservation->id] = $value;
}
$attrs = [];
if (!empty($reservationid)) {
if ($reservation = $DB->get_record('reservation', ['id' => $reservationid])) {
if (reservation_get_requests($reservation)) {
// Set read only if exists requests to avoid multiple request on connected reservations.
$attrs['readonly'] = 'readonly';
}
}
}
$mform->addElement('select', 'parent', get_string('parent', 'reservation'), $values, $attrs);
$mform->setAdvanced('parent');
}
if (!isset($reservationconfig->max_overbook)) {
$reservationconfig->max_overbook = 100;
} else {
$reservationconfig->max_overbook = intval($reservationconfig->max_overbook);
}
if (!isset($reservationconfig->overbook_step)) {
$reservationconfig->overbook_step = 5;
}
$values = [0 => get_string('nooverbook', 'reservation')];
$step = $reservationconfig->overbook_step;
for ($i = $step; $i <= $reservationconfig->max_overbook; $i += $step) {
$values[$i] = "$i%";
}
$mform->addElement('select', 'overbook', get_string('overbook', 'reservation'), $values);
$mform->disabledIf('overbook', 'maxrequest', 'eq', '0');
$mform->setAdvanced('overbook');
if (!empty($reservationconfig->sublimits)) {
$sublimitgrps = [];
for ($i = 1; $i <= $reservationconfig->sublimits; $i++) {
$sublimitgrps[$i] = [];
$values = [];
for ($j = 0; $j <= $reservationconfig->max_requests; $j++) {
$values[$j] = "$j";
}
$sublimitgrps[$i][] = &$mform->createElement('select', 'requestlimit_'.$i, null, $values);
$sublimitgrps[$i][] = &$mform->createElement('static', 'with_'.$i, null, get_string('with', 'reservation'));
unset($fields);
$fields = [];
$fields['-'] = get_string('choose');
$fields['group'] = get_string('group');
$fields['city'] = get_string('city');
$fields['institution'] = get_string('institution');
$fields['department'] = get_string('department');
$fields['address'] = get_string('address');
$customfields = $DB->get_records('user_info_field');
if (!empty($customfields)) {
foreach ($customfields as $customfield) {
$fields[$customfield->shortname] = $customfield->name;
}
}
$attributes = 'class="field"';
$sublimitgrps[$i][] = &$mform->createElement('select', 'field_'.$i, null, $fields, $attributes);
unset($operators);
$operators = [];
$operators[] = get_string('equal', 'reservation');
$operators[] = get_string('notequal', 'reservation');
$sublimitgrps[$i][] = &$mform->createElement('select', 'operator_'.$i, null, $operators);
$attributes = 'class="matchvalue"';
$sublimitgrps[$i][] = &$mform->createElement('text', 'matchvalue_'.$i, null, $attributes);
$mform->setType('matchvalue_'.$i, PARAM_TEXT);
$mform->disabledIf('matchvalue_'.$i, 'field_'.$i, 'eq', '-');
$mform->addGroup($sublimitgrps[$i], 'sublimitgrp_'.$i, get_string('sublimit', 'reservation', $i), ' ', false);
$mform->setAdvanced('sublimitgrp_'.$i);
}
}
$this->standard_grading_coursemodule_elements();
$this->standard_coursemodule_elements();
$this->add_action_buttons();
$options = new stdClass();
$options->courseid = $COURSE->id;
$PAGE->requires->js_call_amd('mod_reservation/reservationedit', 'init', [$options]);
}
/**
* Any data processing needed before the form is displayed
* (needed to set up draft areas for editor and filemanager elements)
* @param array $defaultvalues
*/
public function data_preprocessing(&$defaultvalues) {
global $DB;
if (!empty($defaultvalues['instance'])) {
if (!empty($defaultvalues['teachers'])) {
$teachers = explode(',', $defaultvalues['teachers']);
$defaultvalues['teachers'] = $teachers;
}
if (!$locations = $DB->get_records_menu('reservation_location')) {
$locations = [];
}
if (! in_array($defaultvalues['location'], $locations)) {
$defaultvalues['locationtext'] = $defaultvalues['location'];
$defaultvalues['location'] = null;
} else {
$defaultvalues['locationtext'] = '';
}
$queryparameters = ['reservationid' => $defaultvalues['instance']];
if ($reservationlimits = $DB->get_records('reservation_limit', $queryparameters, 'id')) {
$i = 1;
foreach ($reservationlimits as $reservationlimit) {
$defaultvalues['field_'.$i] = $reservationlimit->field;
$defaultvalues['operator_'.$i] = $reservationlimit->operator;
$defaultvalues['matchvalue_'.$i] = $reservationlimit->matchvalue;
$defaultvalues['requestlimit_'.$i] = $reservationlimit->requestlimit;
$i++;
}
$sublimits = get_config('reservation', 'sublimits');
if ($i > $sublimits) {
set_config('sublimits', $i, 'reservation');
}
}
}
}
/**
* Allows module to modify the data returned by form get_data().
* This method is also called in the bulk activity completion form.
*
* Only available on moodleform_mod.
*
* @param stdClass $data the form data to be modified.
*/
public function data_postprocessing($data) {
parent::data_postprocessing($data);
if (!empty($data->completionunlocked)) {
// Turn off completion settings if the checkboxes aren't ticked.
$autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
if (!$autocompletion || empty($data->completionreserved)) {
$data->completionreserved = 0;
}
}
}
/**
* Perform minimal validation on the settings form
* @param array $data
* @param array $files
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if (isset($data['timeend']) && !empty($data['timeend']) && ($data['timeend'] < $data['timestart'])) {
$errors['timeend'] = get_string('err_timeendlower', 'reservation');
}
if (isset($data['timeopen']) && !empty($data['timeopen']) && ($data['timeclose'] < $data['timeopen'])) {
$errors['timeopen'] = get_string('err_timeopengreater', 'reservation');
}
if (($data['maxrequest'] > 0) && !empty($reservationconfig->sublimits)) {
$sublimitsum = 0;
for ($i = 1; ($data['field_'.$i] != '-') && ($i <= $reservationconfig->sublimits); $i++) {
$sublimitsum += $data['requestlimit_'.$i];
}
if ($sublimitsum > $data['maxrequest']) {
$errors['maxrequest'] = get_string('err_sublimitsgreater', 'reservation');
}
}
return $errors;
}
/**
* Add any custom completion rules to the form.
*
* @return array Contains the names of the added form elements
*/
public function add_completion_rules() {
$mform =& $this->_form;
$mform->addElement('checkbox',
'completionreserved',
'',
get_string('completionreserved', 'reservation'));
return ['completionreserved'];
}
/**
* Determines if completion is enabled for this module.
*
* @param array $data
* @return bool
*/
public function completion_rule_enabled($data) {
return !empty($data['completionreserved']);
}
}