/
form_view.php
492 lines (467 loc) · 21.1 KB
/
form_view.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
// 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/>.
/**
* OBU Forms - Form view
*
* @package local_obu_forms
* @author Peter Welham
* @copyright 2021, Oxford Brookes University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
require_once("{$CFG->libdir}/formslib.php");
class form_view extends moodleform {
// Variables used in our own validation
private $required_field = array(); // an array of field IDs, all of which must have values
private $required_group = array(); // a group of field IDs, at least one of which must have a value
private $check_id = 0; // ID of check box controlling groups of potentially mandatory fields
private $set_group = array(); // a group of field IDs, all of which must have values if the controlling check box is set
private $unset_group = array(); // a group of field IDs, all of which must have values if the controlling check box is unset
function definition() {
global $USER;
$mform =& $this->_form;
$data = new stdClass();
$data->source = $this->_customdata['source'];
$data->data_id = $this->_customdata['data_id'];
$data->template = $this->_customdata['template'];
$data->username = $this->_customdata['username'];
$data->surname = $this->_customdata['surname'];
$data->forenames = $this->_customdata['forenames'];
$data->current_course = $this->_customdata['current_course'];
$data->start_dates = $this->_customdata['start_dates'];
$data->start_selected = $this->_customdata['start_selected'];
$data->adviser = $this->_customdata['adviser'];
$data->supervisor = $this->_customdata['supervisor'];
$data->course = $this->_customdata['course'];
$data->course_joint = $this->_customdata['course_joint'];
$data->not_enroled = $this->_customdata['not_enroled'];
$data->enroled = $this->_customdata['enroled'];
$data->free_language = $this->_customdata['free_language'];
$data->campus = $this->_customdata['campus'];
$data->study_mode = $this->_customdata['study_mode'];
$data->reason = $this->_customdata['reason'];
$data->addition_reason = $this->_customdata['addition_reason'];
$data->deletion_reason = $this->_customdata['deletion_reason'];
$data->assessment_type = $this->_customdata['assessment_type'];
$data->component_comment = $this->_customdata['component_comment'];
$data->auth_state = $this->_customdata['auth_state'];
$data->auth_level = $this->_customdata['auth_level'];
$data->notes = $this->_customdata['notes'];
$data->fields = $this->_customdata['fields'];
$data->status_text = $this->_customdata['status_text'];
$data->notes = $this->_customdata['notes'];
$data->button_text = $this->_customdata['button_text'];
if ($data->notes != '') {
$this->set_data(array('notes' => $data->notes));
}
// Start with the required hidden fields
$mform->addElement('hidden', 'source', $data->source);
$mform->setType('source', PARAM_RAW);
if ($data->data_id > 0) { // Using form to amend or view
$mform->addElement('hidden', 'id', $data->data_id);
$mform->setType('id', PARAM_RAW);
} else { // Using form for initial input
$mform->addElement('hidden', 'template', $data->template->id);
$mform->setType('template', PARAM_RAW);
}
$mform->addElement('hidden', 'auth_state', $data->auth_state);
$mform->setType('auth_state', PARAM_RAW);
$mform->addElement('hidden', 'auth_level', $data->auth_level);
$mform->setType('auth_level', PARAM_RAW);
// Process the template
$fld_start = '<input ';
$fld_start_len = strlen($fld_start);
$fld_end = '>';
$fld_end_len = strlen($fld_end);
$offset = 0;
$date_format = 'd-m-y';
// This 'dummy' element has two purposes:
// - To force open the Moodle Forms invisible fieldset outside of any table on the form (corrupts display otherwise)
// - To let us inform the user that there are validation errors without them having to scroll down further
$mform->addElement('static', 'form_error');
do {
$pos = strpos($data->template->data, $fld_start, $offset);
if ($pos === false) {
break;
}
if ($pos > $offset) {
$mform->addElement('html', substr($data->template->data, $offset, ($pos - $offset))); // output any HTML
}
$offset = $pos + $fld_start_len;
$pos = strpos($data->template->data, $fld_end, $offset);
if ($pos === false) {
break;
}
$element = split_input_field(substr($data->template->data, $offset, ($pos - $offset)));
$offset = $pos + $fld_end_len;
if (!empty($data->fields)) { // simply display the field
$text = $data->fields[$element['id']];
if ($element['type'] == 'checkbox') { // map a checkbox value to a nice character
if ($text == '1') {
$text = '✔';
} else {
$text = '';
}
}
if (($element['type'] == 'date') && ($text)) { // map a UNIX date to a nice text string
$date = date_create();
date_timestamp_set($date, $text);
$text = date_format($date, $date_format);
}
$mform->addElement('static', $element['id'], $element['value'], $text);
} else {
switch ($element['type']) {
case 'area':
$mform->addElement('textarea', $element['id'], $element['value'], $element['options']);
break;
case 'checkbox':
if (array_key_exists('rule', $element) && ($element['rule'] == 'required')) { // mustn't return a zero value
$mform->addElement('checkbox', $element['id'], $element['value']);
} else if (array_key_exists('name', $element)) {
$mform->addElement('advcheckbox', $element['id'], $element['value'], $element['name'], null, array(0, 1));
} else {
$mform->addElement('advcheckbox', $element['id'], $element['value'], null, null, array(0, 1));
}
break;
case 'date':
if (array_key_exists('options', $element)) {
$mform->addElement('date_selector', $element['id'], $element['value'], $element['options']);
} else {
$mform->addElement('date_selector', $element['id'], $element['value']);
}
break;
case 'radio':
$options = explode('|', $element['name']);
if (array_key_exists('value', $element) && in_array($element['value'], $options)) {
$default = $element['value'];
} else {
$default = '';
}
$radioarray = array();
foreach ($options as $option) {
$radioarray[] = $mform->createElement('radio', $element['id'], '', $option, $option);
if ($default == '') {
$default = $option;
}
}
$mform->addGroup($radioarray, 'radioar', '', array('; '), false);
if ($default != '') {
$mform->setDefault($element['id'], $default);
}
break;
case 'select':
switch ($element['name']) {
case 'start_dates':
$options = $data->start_dates;
break;
case 'adviser':
$options = $data->adviser;
break;
case 'supervisor':
$options = $data->supervisor;
break;
case 'course':
$options = $data->course;
break;
case 'course_joint':
$options = $data->course_joint;
break;
case 'not_enroled':
$options = $data->not_enroled;
break;
case 'enroled':
$options = $data->enroled;
break;
case 'free_language':
$options = $data->free_language;
break;
case 'campus':
$options = $data->campus;
break;
case 'study_mode':
$options = $data->study_mode;
break;
case 'reason':
$options = $data->reason;
break;
case 'addition_reason':
$options = $data->addition_reason;
break;
case 'deletion_reason':
$options = $data->deletion_reason;
break;
case 'assessment_type':
$options = $data->assessment_type;
break;
case 'component_comment':
$options = $data->component_comment;
break;
default:
}
$select = $mform->addElement('select', $element['id'], $element['value'], $options, null);
if (array_key_exists('selected', $element)) {
if ($element['selected'] == 'start_selected') {
$select->setSelected($data->start_selected);
} else {
$select->setSelected($element['selected']); // Use the form field's default
}
} else if ($element['name'] == 'start_dates') {
$select->setSelected($data->start_selected); // Use the default given
}
break;
case 'static':
switch ($element['name']) {
case 'username':
$text = $data->username;
break;
case 'surname':
$text = $data->surname;
break;
case 'forenames':
$text = $data->forenames;
break;
case 'current_course':
$text = $data->current_course;
break;
default:
$text = '';
}
$mform->addElement('static', '', $element['value'], $text); // Display the field...
$mform->addElement('hidden', $element['id'], $text); // ...and also return it
$mform->setType($element['id'], PARAM_RAW);
break;
case 'text':
$mform->addElement('text', $element['id'], $element['value'], $element['options']);
$mform->setType($element['id'], PARAM_RAW);
break;
case 'alphabetic':
$mform->addElement('text', $element['id'], $element['value'], $element['options']);
$mform->setType($element['id'], PARAM_RAW);
$mform->addRule($element['id'], null, 'lettersonly', null, 'server'); // Let Moodle handle the rule
break;
case 'numeric':
$mform->addElement('text', $element['id'], $element['value'], $element['options']);
$mform->setType($element['id'], PARAM_RAW);
$mform->addRule($element['id'], null, 'numeric', null, 'server'); // Let Moodle handle the rule
break;
default:
}
if (array_key_exists('rule', $element)) { // An extra validation rule applies to this field
if ($element['rule'] == 'group') { // At least one of this group of fields is required
$this->required_group[] = $element['id']; // For our own validation
} else if ($element['rule'] == 'check') { // A (single) check box that controls whether groups of fields are mandatory
$this->check_id = $element['id']; // For our own validation
} else if ($element['rule'] == 'check_set') { // A field that is mandatory if the controlling check box is set
$this->set_group[] = $element['id']; // For our own validation
} else if ($element['rule'] == 'check_unset') { // A field that is mandatory if the controlling check box is unset
$this->unset_group[] = $element['id']; // For our own validation
} else {
$mform->addRule($element['id'], null, $element['rule'], null, 'server'); // Let Moodle handle the rule
if ($element['rule'] == 'required') {
$this->required_field[] = $element['id']; // For our own extra validation
}
}
}
}
} while(true);
$mform->addElement('html', substr($data->template->data, $offset)); // Output any remaining HTML
if (!empty($data->status_text)) {
$mform->addElement('html', '<p /><strong>' . $data->status_text . '</strong>'); // Output any status text
}
if (is_manager()) {
$mform->addElement('textarea', 'notes', get_string('notes', 'local_obu_forms'), 'cols="100" rows="10"');
$mform->setType('notes', PARAM_RAW);
}
$buttonarray = array();
if (!has_capability('local/obu_forms:update', context_system::instance())) {
$mform->addElement('html', '<p /><strong>' . get_string('maintenance_mode', 'local_obu_forms') . '</strong>'); // Output 'maintenance' message
$buttonarray[] = &$mform->createElement('cancel');
} else {
if ($data->button_text != 'cancel') {
$buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string($data->button_text, 'local_obu_forms'));
}
if ($data->button_text != 'continue') {
if ($data->button_text == 'authorise') {
$mform->addElement('text', 'comment', get_string('comment', 'local_obu_forms'));
$mform->setType('comment', PARAM_RAW);
$buttonarray[] = &$mform->createElement('submit', 'rejectbutton', get_string('reject', 'local_obu_forms'));
if (is_manager() && ($data->auth_state == 0)) { // This user can redirect the form
$buttonarray[] = &$mform->createElement('submit', 'redirectbutton', get_string('redirect', 'local_obu_forms'));
}
}
if (is_manager()) { // A forms manager can opt to just save the notes
$buttonarray[] = &$mform->createElement('submit', 'savebutton', get_string('save', 'local_obu_forms'));
}
$buttonarray[] = &$mform->createElement('cancel');
} else if (is_manager()) { // A forms manager
if ($data->auth_state == 0) { // Can redirect the form
$buttonarray[] = &$mform->createElement('submit', 'redirectbutton', get_string('redirect', 'local_obu_forms'));
}
$buttonarray[] = &$mform->createElement('submit', 'savebutton', get_string('save', 'local_obu_forms')); // Can opt to just save the notes
}
}
$mform->addGroup($buttonarray, 'buttonarray', '', array(' '), false);
$mform->closeHeaderBefore('buttonarray');
}
function validation($data, $files) {
$errors = parent::validation($data, $files); // Ensure we don't miss errors from any higher-level validation
// Get valid dates for +- 5 years (MMMYY format)
$start_dates = get_dates(date('m'), date('y'), 60, 60);
// Check if at least one field in a required group has an entry
if (empty($this->required_group)) {
$group_entry = true;
} else {
$group_entry = false;
foreach($this->required_group as $key) {
if ($data[$key] != '') {
$group_entry = true;
}
}
}
// Do our own validation and add errors to array
$required_value = false;
foreach ($data as $key => $value) {
$is_null = (($value == '') || (($value == '0') && ((strpos($key, 'start_date') !== false) || ($key == 'adviser') || ($key == 'supervisor') || ($key == 'supervisor_2')))); // Include special cases
if (in_array($key, $this->required_field, true) && ($value == '')) { // Leave the field error display to Moodle
$required_value = true;
} else if (in_array($key, $this->required_field, true) && $is_null) { // Moodle wouldn't identify special cases as blank
$errors[$key] = get_string('value_required', 'local_obu_forms');
} else if (!$group_entry && in_array($key, $this->required_group, true)) { // One of a required group with no entries
$errors[$key] = get_string('group_required', 'local_obu_forms');
} else if (in_array($key, $this->set_group, true) && (array_key_exists($this->check_id, $data) && ($data[$this->check_id] == '1')) && $is_null) { // Controlled by a check box
$errors[$key] = get_string('value_required', 'local_obu_forms');
} else if (in_array($key, $this->unset_group, true) && (array_key_exists($this->check_id, $data) && ($data[$this->check_id] == '0')) && $is_null) { // Controlled by a check box
$errors[$key] = get_string('value_required', 'local_obu_forms');
} else if ($key == 'course') { // Exact match - should be a current course (programme) code
if ($value != '') { // Might not be mandatory
$course_code = strtoupper($value);
if (strpos($course_code, '[') === false) {
$course_code = $course_code . '[OBO]'; // Default campus
}
$current_courses = get_current_courses($this->_customdata['modular']);
if (!in_array($course_code, $current_courses, true)) {
$errors[$key] = get_string('course_not_found', 'local_obu_forms');
}
}
} else if (strpos($key, 'module') !== false) { // Validate module code format etcetera
if ($value != '') { // Only validate if the field was completed
if (strlen($value) == 6) { // Should be in eCSIS format
$prefix = strtoupper(substr($value, 0, 1));
$suffix = substr($value, 1);
if ((($prefix != 'C') && ($prefix != 'F') && ($prefix != 'P') && ($prefix != 'U')) || !is_numeric($suffix)) {
$errors[$key] = get_string('invalid_module_code', 'local_obu_forms');
} else if ($this->_customdata['modular'] && ($prefix != 'P') && ($prefix != 'U')) {
$errors[$key] = get_string('invalid_module_code', 'local_obu_forms');
} else if (($key == 'module') || ($key == 'module_2')) { // Exact match - should be a current module
$current_modules = get_current_modules();
if (!in_array($prefix . $suffix, $current_modules, true)) {
$errors[$key] = get_string('module_not_found', 'local_obu_forms');
}
}
} else if (strlen($value) == 7) { // Shorter Banner format
$subject = strtoupper(substr($value, 0, 3));
$code = substr($value, 3);
if (is_numeric($subject) || !is_numeric($code)) {
$errors[$key] = get_string('invalid_module_code', 'local_obu_forms');
} else if ($key == 'module') { // Exact match - should be a current module
if (!array_key_exists('campus', $data)) {
$campus = 'OBO'; // The default
} else {
$campus = $data['campus'];
}
$current_modules = get_current_modules();
if (!in_array($subject . $code . ' [' . $campus . ']', $current_modules, true)) {
$errors[$key] = get_string('module_not_found', 'local_obu_forms');
}
} else if ($key == 'module_2') { // Exact match - should be a current module
if (!array_key_exists('campus_2', $data)) {
$campus = 'OBO'; // The default
} else {
$campus = $data['campus_2'];
}
$current_modules = get_current_modules();
if (!in_array($subject . $code . ' [' . $campus . ']', $current_modules, true)) {
$errors[$key] = get_string('module_not_found', 'local_obu_forms');
}
}
} else if (strlen($value) == 8) { // Longer Banner format
$subject = strtoupper(substr($value, 0, 4));
$code = substr($value, 4);
if (is_numeric($subject) || !is_numeric($code)) {
$errors[$key] = get_string('invalid_module_code', 'local_obu_forms');
} else if ($key == 'module') { // Exact match - should be a current module
if (!array_key_exists('campus', $data)) {
$campus = 'OBO'; // The default
} else {
$campus = $data['campus'];
}
$current_modules = get_current_modules();
if (!in_array($subject . $code . ' [' . $campus . ']', $current_modules, true)) {
$errors[$key] = get_string('module_not_found', 'local_obu_forms');
}
} else if ($key == 'module_2') { // Exact match - should be a current module
if (!array_key_exists('campus_2', $data)) {
$campus = 'OBO'; // The default
} else {
$campus = $data['campus_2'];
}
$current_modules = get_current_modules();
if (!in_array($subject . $code . ' [' . $campus . ']', $current_modules, true)) {
$errors[$key] = get_string('module_not_found', 'local_obu_forms');
}
}
} else { // Not a known format
$errors[$key] = get_string('invalid_module_code', 'local_obu_forms');
}
// Check that any associated module fields have also been completed
$pos = strpos($key, 'module');
$prefix = substr($key, 0, $pos);
$suffix = substr($key, ($pos + 6));
$key = $prefix . 'title' . $suffix;
if (array_key_exists($key, $data) && ($data[$key] == '')) {
$errors[$key] = get_string('value_required', 'local_obu_forms');
}
$key = $prefix . 'start' . $suffix;
if (array_key_exists($key, $data) && ($data[$key] == '')) {
$errors[$key] = get_string('value_required', 'local_obu_forms');
}
$key = $prefix . 'mark' . $suffix;
if (array_key_exists($key, $data) && ($data[$key] == '')) {
$errors[$key] = get_string('value_required', 'local_obu_forms');
}
$key = $prefix . 'credit' . $suffix;
if (array_key_exists($key, $data) && ($data[$key] == '')) {
$errors[$key] = get_string('value_required', 'local_obu_forms');
}
}
} else if (strpos($key, 'start') !== false) { // Validate start date format
if ($value != '') { // Only validate if the field was completed
if ((strlen($value) > 2) || !is_numeric($value)) { // Ignore any drop-down selection
$month = strtoupper(substr($value, 0, 3));
$year = substr($value, 3);
if ((strlen($value) != 5) || is_numeric($month) || !is_numeric($year)) {
$errors[$key] = get_string('invalid_date_format', 'local_obu_forms');
} else if (!in_array($month . $year, $start_dates, true)) {
$errors[$key] = get_string('invalid_start_date', 'local_obu_forms');
}
}
}
}
}
if ($required_value || !empty($errors)) {
$errors['form_error'] = get_string('form_error', 'local_obu_forms');
}
return $errors;
}
}