-
Notifications
You must be signed in to change notification settings - Fork 37
/
mod_form.php
458 lines (410 loc) · 21.7 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
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
<?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/>.
/**
* The main StudentQuiz configuration form
*
* It uses the standard core Moodle formslib. For more info about them, please
* visit: http://docs.moodle.org/en/Development:lib/formslib.php
*
* @package mod_studentquiz
* @copyright 2017 HSR (http://www.hsr.ch)
* @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(__DIR__ . '/locallib.php');
require_once(__DIR__ . '/reportlib.php');
use mod_studentquiz\utils;
/**
* Module instance settings form
*
* @package mod_studentquiz
* @copyright 2017 HSR (http://www.hsr.ch)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_studentquiz_mod_form extends moodleform_mod {
/**
* Defines forms elements
*/
public function definition() {
global $CFG;
$mform = $this->_form;
$defaultqtypes = [];
$defaultqtypesdefined = false;
if ($qtypesdata = get_config('studentquiz', 'defaultqtypes')) {
// Default question types already defined in Administration setting.
$defaultqtypesdefined = true;
$defaultqtypes = explode(',', $qtypesdata);
}
// Adding the "general" fieldset, where all the common settings are showed.
$mform->addElement('header', 'general', get_string('general', 'form'));
// Adding the standard "name" field.
$mform->addElement('text', 'name', get_string('studentquizname', 'studentquiz'),
array('size' => '64'));
$mform->addHelpButton('name', 'studentquizname', 'studentquiz');
if (!empty($CFG->formatstringstriptags)) {
$mform->setType('name', PARAM_TEXT);
} else {
$mform->setType('name', PARAM_CLEANHTML);
}
$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('name', get_string('maximumchars', '', 255),
'maxlength', 255, 'client');
// Adding the standard "intro" and "introformat" fields.
$this->standard_intro_elements();
// Field question publishing.
$publishingoptions = [
1 => get_string('setting_question_publishing_automatic', 'studentquiz'),
0 => get_string('setting_question_publishing_require_approval', 'studentquiz')
];
$mform->addElement('select', 'publishnewquestion', get_string('setting_question_publishing', 'studentquiz'),
$publishingoptions);
$mform->addHelpButton('publishnewquestion', 'setting_question_publishing', 'studentquiz');
$mform->setType('publishnewquestion', PARAM_INT);
$mform->setDefault('publishnewquestion', 1);
$mform->addElement('header', 'sectionranking', get_string('settings_section_header_ranking', 'studentquiz'));
// Field anonymous Ranking.
$mform->addElement('checkbox', 'anonymrank',
get_string('settings_anonymous_label', 'studentquiz'));
$mform->setType('anonymrank', PARAM_INT);
$mform->addHelpButton('anonymrank', 'settings_anonymous', 'studentquiz');
$mform->setDefault('anonymrank', 1);
// Select question behaviour. Removed in v3.0.0, unless a use-case needs this option.
// Since there's no studentquiz behavior anymore, we could offer a selection.
// But they have to be of type feedback (or support non-feedback behavior).
// Field questionquantifier.
$mform->addElement('text', 'questionquantifier',
get_string('settings_questionquantifier', 'studentquiz'));
$mform->setType('questionquantifier', PARAM_INT);
$mform->addHelpButton('questionquantifier', 'settings_questionquantifier', 'studentquiz');
$mform->setDefault('questionquantifier',
get_config('studentquiz', 'addquestion'));
// Field approvedquantifier.
$mform->addElement('text', 'approvedquantifier',
get_string('settings_approvedquantifier', 'studentquiz'));
$mform->setType('approvedquantifier', PARAM_INT);
$mform->addHelpButton('approvedquantifier', 'settings_approvedquantifier', 'studentquiz');
$mform->setDefault('approvedquantifier',
get_config('studentquiz', 'approved'));
// Field ratequantifier.
$mform->addElement('text', 'ratequantifier',
get_string('settings_ratequantifier', 'studentquiz'));
$mform->setType('ratequantifier', PARAM_INT);
$mform->addHelpButton('ratequantifier', 'settings_ratequantifier', 'studentquiz');
$mform->setDefault('ratequantifier',
get_config('studentquiz', 'rate'));
// Field correctanswerquantifier.
$mform->addElement('text', 'correctanswerquantifier',
get_string('settings_lastcorrectanswerquantifier', 'studentquiz'));
$mform->setType('correctanswerquantifier', PARAM_INT);
$mform->addHelpButton('correctanswerquantifier', 'settings_lastcorrectanswerquantifier', 'studentquiz');
$mform->setDefault('correctanswerquantifier',
get_config('studentquiz', 'correctanswered'));
// Field incorrectanswerquantifier.
$mform->addElement('text', 'incorrectanswerquantifier',
get_string('settings_lastincorrectanswerquantifier', 'studentquiz'));
$mform->setType('incorrectanswerquantifier', PARAM_INT);
$mform->addHelpButton('incorrectanswerquantifier', 'settings_lastincorrectanswerquantifier', 'studentquiz');
$mform->setDefault('incorrectanswerquantifier',
get_config('studentquiz', 'incorrectanswered'));
// Selection for excluded roles.
$rolescanbeexculded = mod_studentquiz_report::get_roles_which_can_be_exculded();
if (!empty($rolescanbeexculded)) {
$excluderolesgroup = [];
foreach ($rolescanbeexculded as $role => $roleinfo) {
$excluderolesgroup[] = $mform->createElement('checkbox', $role, '', $roleinfo['name']);
// Default question types already defined in Administration setting.
// This question type was enable by default in Administration setting.
$mform->setDefault("excluderoles[" . $role . "]", $roleinfo['default']);
}
$mform->addGroup($excluderolesgroup, 'excluderoles', get_string('settings_excluderoles', 'studentquiz'));
$mform->addHelpButton('excluderoles', 'settings_excluderoles', 'studentquiz');
}
$mform->addElement('header', 'sectionquestion', get_string('settings_section_header_question', 'studentquiz'));
// Selection for allowed question types.
$allowedgroup = array();
$allowedgroup[] = $mform->createElement('checkbox', "ALL", '', get_string('settings_allowallqtypes', 'studentquiz'));
foreach (mod_studentquiz_get_question_types() as $qtype => $name) {
$allowedgroup[] = $mform->createElement('checkbox', $qtype, '', $name);
if ($defaultqtypesdefined && in_array($qtype, $defaultqtypes)) {
// Default question types already defined in Administration setting.
// This question type was enable by default in Administration setting.
$mform->setDefault("allowedqtypes[" . $qtype . "]", 1);
}
}
if (!$defaultqtypesdefined) {
// Default question types was not defined in Administration setting.
// Set to ALL question types by default.
$mform->setDefault("allowedqtypes[ALL]", 1);
}
$mform->addGroup($allowedgroup, 'allowedqtypes', get_string('settings_allowedqtypes', 'studentquiz'));
$mform->disabledIf('allowedqtypes', "allowedqtypes[ALL]", 'checked');
$mform->addHelpButton('allowedqtypes', 'settings_allowedqtypes', 'studentquiz');
// Comment and rating sections.
$mform->addElement('header', 'sectioncomment', get_string('settings_section_header_comment_rating', 'studentquiz'));
// Field force rating.
$mform->addElement('checkbox', 'forcerating', get_string('settings_forcerating', 'studentquiz'));
$mform->setType('forcerating', PARAM_INT);
$mform->addHelpButton('forcerating', 'settings_forcerating', 'studentquiz');
$mform->setDefault('forcerating', get_config('studentquiz', 'forcerating'));
// Field force commenting.
$mform->addElement('checkbox', 'forcecommenting', get_string('settings_forcecommenting', 'studentquiz'));
$mform->setType('forcecommenting', PARAM_INT);
$mform->addHelpButton('forcecommenting', 'settings_forcecommenting', 'studentquiz');
$mform->setDefault('forcecommenting', get_config('studentquiz', 'forcecommenting'));
// Field enable private commenting.
$mform->addElement('checkbox', 'privatecommenting', get_string('settings_privatecommenting', 'studentquiz'));
$mform->setType('privatecommenting', PARAM_INT);
$mform->addHelpButton('privatecommenting', 'settings_privatecommenting', 'studentquiz');
$mform->setDefault('privatecommenting', get_config('studentquiz', 'showprivatecomment'));
// Comment deletion period.
$mform->addElement('select', 'commentdeletionperiod',
get_string('settings_commentdeletionperiod', 'studentquiz'),
\mod_studentquiz\commentarea\container::get_deletion_period_options()
);
$mform->setType('commentdeletionperiod', PARAM_INT);
$mform->addHelpButton('commentdeletionperiod', 'settings_commentdeletionperiod', 'studentquiz');
$mform->setDefault('commentdeletionperiod', get_config('studentquiz', 'commentediting_deletionperiod'));
// Email address for reporting unacceptable comment for this studentquiz, default is blank.
$mform->addElement('text', 'reportingemail', get_string('settings_reportingemail', 'studentquiz'), ['size' => 64]);
$mform->setType('reportingemail', PARAM_NOTAGS);
$mform->addRule('reportingemail', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addHelpButton('reportingemail', 'settings_reportingemail', 'studentquiz');
// Availability.
$mform->addElement('header', 'availability', get_string('availability', 'moodle'));
$mform->addElement('date_time_selector', 'opensubmissionfrom',
get_string('settings_availability_open_submission_from', 'studentquiz'), ['optional' => true]);
$mform->addElement('date_time_selector', 'closesubmissionfrom',
get_string('settings_availability_close_submission_from', 'studentquiz'), ['optional' => true]);
$mform->addElement('date_time_selector', 'openansweringfrom',
get_string('settings_availability_open_answering_from', 'studentquiz'), ['optional' => true]);
$mform->addElement('date_time_selector', 'closeansweringfrom',
get_string('settings_availability_close_answering_from', 'studentquiz'), ['optional' => true]);
// Notification.
$mform->addElement('header', 'notification', get_string('settings_notification', 'studentquiz'));
// Field email digest type.
$digesttypes = [
0 => get_string('settings_email_digest_type_no_digest', 'studentquiz'),
1 => get_string('settings_email_digest_type_daily_digest', 'studentquiz'),
2 => get_string('settings_email_digest_type_weekly_digest', 'studentquiz')
];
$mform->addElement('select', 'digesttype', get_string('settings_email_digest_type', 'studentquiz'),
$digesttypes);
$mform->addHelpButton('digesttype', 'settings_email_digest_type', 'studentquiz');
// Field first day of week.
$daysofweek = [
0 => get_string('sunday', 'calendar'),
1 => get_string('monday', 'calendar'),
2 => get_string('tuesday', 'calendar'),
3 => get_string('wednesday', 'calendar'),
4 => get_string('thursday', 'calendar'),
5 => get_string('friday', 'calendar'),
6 => get_string('saturday', 'calendar')
];
$mform->addElement('select', 'digestfirstday', get_string('settings_email_digest_first_day', 'studentquiz'), $daysofweek);
$mform->addHelpButton('digestfirstday', 'settings_email_digest_first_day', 'studentquiz');
$mform->setDefault('digestfirstday', 1);
$mform->disabledIf('digestfirstday', 'digesttype', 'neq', 2);
// Add standard elements, common to all modules.
$this->standard_coursemodule_elements();
// Add standard buttons, common to all modules.
$this->add_action_buttons();
}
/**
* TODO: describe this
* @param array $defaultvalues
*/
public function data_preprocessing(&$defaultvalues) {
// Comma separated should be fine for our case.
if (isset($defaultvalues['allowedqtypes'])) {
$enabled = explode(',', $defaultvalues['allowedqtypes']);
foreach (array_keys(mod_studentquiz_get_question_types()) as $qtype) {
$defaultvalues["allowedqtypes[$qtype]"] = (int)in_array($qtype, $enabled);
}
$defaultvalues["allowedqtypes[ALL]"] = (int)in_array("ALL", $enabled);
}
if (isset($defaultvalues['excluderoles'])) {
$enabled = explode(',', $defaultvalues['excluderoles']);
foreach (array_keys(mod_studentquiz_get_roles()) as $role) {
$defaultvalues["excluderoles[$role]"] = (int)in_array($role, $enabled);
}
}
$defaultvalues['completionpointenabled'] = !empty($defaultvalues['completionpoint']) ? 1 : 0;
if (empty($defaultvalues['completionpoint'])) {
$defaultvalues['completionpoint'] = 1;
}
$defaultvalues['completionquestionpublishedenabled'] = !empty($defaultvalues['completionquestionpublished']) ? 1
: 0;
if (empty($defaultvalues['completionquestionpublished'])) {
$defaultvalues['completionquestionpublished'] = 1;
}
$defaultvalues['completionquestionapprovedenabled'] = !empty($defaultvalues['completionquestionapproved']) ? 1
: 0;
if (empty($defaultvalues['completionquestionapproved'])) {
$defaultvalues['completionquestionapproved'] = 1;
}
}
/**
* List of added element names, or names of wrapping group elements.
*
* @return array List of added element names, or names of wrapping group elements.
*/
public function add_completion_rules(): array {
$mform =& $this->_form;
$completionpointel = $this->get_suffixed_name('completionpointgroup');
$completionquestionpublishedel = $this->get_suffixed_name('completionquestionpublishedgroup');
$completionquestionapprovedgel = $this->get_suffixed_name('completionquestionapprovedgroup');
// Require point.
$group = [];
$group[] =& $mform->createElement('checkbox', 'completionpointenabled', '',
get_string('completionpoint', 'mod_studentquiz'));
$group[] =& $mform->createElement('text', 'completionpoint', '', ['size' => 3]);
$mform->setType('completionpoint', PARAM_INT);
$mform->addGroup($group, $completionpointel, get_string('completionpointgroup',
'mod_studentquiz'), [' '], false);
$mform->addHelpButton($completionpointel, 'completionpointgroup',
'mod_studentquiz');
$mform->disabledIf('completionpoint', 'completionpointenabled', 'notchecked');
// Require published questions.
$group = [];
$group[] =& $mform->createElement('checkbox', 'completionquestionpublishedenabled', '',
get_string('completionquestionpublished', 'mod_studentquiz'));
$group[] =& $mform->createElement('text', 'completionquestionpublished', '',
['size' => 3]);
$mform->setType('completionquestionpublished', PARAM_INT);
$mform->addGroup($group, $completionquestionpublishedel,
get_string('completionquestionpublishedgroup', 'mod_studentquiz'),
[' '], false);
$mform->addHelpButton($completionquestionpublishedel,
'completionquestionpublishedgroup', 'mod_studentquiz');
$mform->disabledIf('completionquestionpublished', 'completionquestionpublishedenabled',
'notchecked');
// Require created approved questions.
$group = [];
$group[] =& $mform->createElement('checkbox',
'completionquestionapprovedenabled', '', get_string('completionquestionapproved',
'mod_studentquiz'));
$group[] =& $mform->createElement('text',
'completionquestionapproved', '', ['size' => 3]);
$mform->setType('completionquestionapproved', PARAM_INT);
$mform->addGroup($group, $completionquestionapprovedgel,
get_string('completionquestionapprovedgroup', 'mod_studentquiz'), [' '],
false);
$mform->addHelpButton($completionquestionapprovedgel,
'completionquestionapprovedgroup', 'mod_studentquiz');
$mform->disabledIf('completionquestionapproved',
'completionquestionapprovedenabled', 'notchecked');
return [$completionpointel, $completionquestionpublishedel, $completionquestionapprovedgel];
}
/**
* Called during validation to see whether some activity-specific completion rules are selected.
*
* @param array $data Input data not yet validated.
* @return bool True if one or more rules is enabled, false if none are.
*/
public function completion_rule_enabled($data): bool {
return (!empty($data['completionpointenabled'])
&& $data['completionpoint'] != 0)
|| (!empty($data['completionquestionpublishedenabled'])
&& $data['completionquestionpublished'] != 0)
|| (!empty($data['completionquestionapprovedenabled'])
&& $data['completionquestionapproved'] != 0);
}
/**
* Validation of studentquiz_mod_form.
*
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if (!isset($data['allowedqtypes'])) {
$errors['allowedqtypes'] = get_string('needtoallowatleastoneqtype', 'studentquiz');
}
if ($data['opensubmissionfrom'] > 0 && $data['closesubmissionfrom'] > 0 &&
$data['opensubmissionfrom'] >= $data['closesubmissionfrom']) {
$errors['closesubmissionfrom'] = get_string('submissionendbeforestart', 'studentquiz');
}
if ($data['openansweringfrom'] > 0 && $data['closeansweringfrom'] > 0 &&
$data['openansweringfrom'] >= $data['closeansweringfrom']) {
$errors['closeansweringfrom'] = get_string('answeringndbeforestart', 'studentquiz');
}
if (!empty($data['reportingemail']) && !$this->validate_emails($data['reportingemail'])) {
$errors['reportingemail'] = get_string('invalidemail', 'studentquiz');
}
if ($data['groupmode'] == VISIBLEGROUPS) {
$errors['groupmode'] = get_string('visiblegroupnotyetsupport', 'studentquiz');
}
return $errors;
}
/**
* Validate string contains validate email or multiple emails.
*
* @param string $emails - Example: test@gmail.com;test1@gmail.com.
* @return bool
*/
private function validate_emails($emails) {
$list = explode(';' , $emails);
foreach ($list as $email) {
if (!validate_email($email)) {
return false;
}
}
return true;
}
/**
* Return submitted data if properly submitted or returns NULL if validation fails or
* if there is no submitted data.
*
* @return bool|object
*/
public function get_data() {
$data = parent::get_data();
// Set the reportingemail to null if empty so that they are consistency.
if ($data) {
if (empty($data->reportingemail)) {
$data->reportingemail = null;
}
}
// Turn off completion settings if the checkboxes aren't ticked.
if (!empty($data->completionunlocked)) {
$autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
if (empty($data->completionpointenabled) || !$autocompletion) {
$data->completionpoint = 0;
}
if (empty($data->completionquestionpublishedenabled) || !$autocompletion) {
$data->completionquestionpublished = 0;
}
if (empty($data->completionquestionapprovedenabled) || !$autocompletion) {
$data->completionquestionapproved = 0;
}
}
return $data;
}
/**
* Get the suffix of name.
*
* @param string $fieldname The field name of the question.
* @return string The suffixed name.
*/
protected function get_suffixed_name(string $fieldname): string {
if (utils::moodle_version_is("<=", "42")) {
return $fieldname;
}
return $fieldname . $this->get_suffix();
}
}