-
Notifications
You must be signed in to change notification settings - Fork 15
/
mod_form.php
369 lines (311 loc) · 15.4 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_surveypro
* @copyright 2013 onwards kordan <[email protected]>
* @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');
/**
* Surveypro settings form.
*
* @package mod_surveypro
* @copyright 2013 onwards kordan <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_surveypro_mod_form extends \moodleform_mod {
/**
* Definition.
*
* @return void
*/
public function definition() {
global $CFG, $COURSE;
$mform = $this->_form;
// Adding the "general" fieldset, where all the common settings are showed.
$fieldname = 'general';
$mform->addElement('header', $fieldname, get_string($fieldname, 'form'));
// Adding the standard "name" field.
$fieldname = 'name';
$mform->addElement('text', $fieldname, get_string('name'), ['size' => '64']);
if (!empty($CFG->formatstringstriptags)) {
$mform->setType($fieldname, PARAM_TEXT);
} else {
$mform->setType($fieldname, PARAM_CLEANHTML);
}
$mform->addRule($fieldname, get_string('required'), 'required', null, 'client');
$mform->addRule($fieldname, get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addHelpButton($fieldname, 'surveyproname', 'surveypro');
// Adding the standard "intro" and "introformat" fields.
$this->standard_intro_elements(get_string('moduleintro'));
// Open date.
$fieldname = 'timeopen';
$mform->addElement('date_time_selector', $fieldname, get_string($fieldname, 'mod_surveypro'), ['optional' => true]);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Close date.
$fieldname = 'timeclose';
$mform->addElement('date_time_selector', $fieldname, get_string($fieldname, 'mod_surveypro'), ['optional' => true]);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Modulesettinghdr fieldset (header).
$fieldname = 'modulesettinghdr';
$mform->addElement('header', $fieldname, get_string($fieldname, 'mod_surveypro'));
// Newpageforchild.
$fieldname = 'newpageforchild';
$mform->addElement('checkbox', $fieldname, get_string($fieldname, 'mod_surveypro'));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Never started reminder.
$fieldname = 'neverstartedemail';
$mform->addElement('checkbox', $fieldname, get_string($fieldname, 'mod_surveypro'));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Allow/deny pauseresume with or without reminder email.
$options = [SURVEYPRO_ONESHOTNOEMAIL => get_string('oneshotnoemail', 'mod_surveypro')];
$options[SURVEYPRO_ONESHOTEMAIL] = get_string('oneshotemail', 'mod_surveypro');
$options[SURVEYPRO_PAUSERESUMENOEMAIL] = get_string('pauseresumenoemail', 'mod_surveypro');
$options[SURVEYPRO_PAUSERESUMEEMAIL] = get_string('pauseresumeemail', 'mod_surveypro');
$fieldname = 'pauseresume';
$mform->addElement('select', $fieldname, get_string($fieldname, 'mod_surveypro'), $options);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Keep in progress.
$fieldname = 'keepinprogress';
$mform->addElement('checkbox', $fieldname, get_string($fieldname, 'mod_surveypro'));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// History.
$fieldname = 'history';
$mform->addElement('checkbox', $fieldname, get_string($fieldname, 'mod_surveypro'));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Allow/deny anonymous.
$fieldname = 'anonymous';
$mform->addElement('checkbox', $fieldname, get_string($fieldname, 'mod_surveypro'));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Recaptcha.
if (surveypro_site_recaptcha_enabled()) {
$fieldname = 'captcha';
$mform->addElement('checkbox', $fieldname, get_string($fieldname, 'mod_surveypro'));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
}
// Startyear.
$yearsrange = range(1902, 2038);
$boundaryyear = array_combine($yearsrange, $yearsrange);
$fieldname = 'startyear';
$mform->addElement('select', $fieldname, get_string($fieldname, 'mod_surveypro'), $boundaryyear);
$mform->setDefault($fieldname, 1970);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Stopyear.
$fieldname = 'stopyear';
$mform->addElement('select', $fieldname, get_string($fieldname, 'mod_surveypro'), $boundaryyear);
$currentyear = userdate(time(), "%Y");
$mform->setDefault($fieldname, $currentyear);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Userstyle.
$fieldname = 'userstyle';
$attributes = surveypro_get_user_style_options();
$mform->addElement('filemanager', $fieldname.'_filemanager', get_string($fieldname, 'mod_surveypro'), null, $attributes);
$mform->addHelpButton($fieldname.'_filemanager', $fieldname, 'surveypro');
// Maxentries.
$fieldname = 'maxentries';
$maxentries = 50;
$entriesrange = range(1, $maxentries);
$countoptions = array_combine($entriesrange, $entriesrange);
array_unshift($countoptions, get_string('unlimited', 'mod_surveypro'));
$mform->addElement('select', $fieldname, get_string($fieldname, 'mod_surveypro'), $countoptions);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Helper variables.
$attributes = ['wrap' => 'virtual', 'rows' => 10, 'cols' => 60];
// Define thanks page.
$fieldname = 'thankspageeditor';
$editoroptions = surveypro_get_editor_options();
$mform->addElement('editor', $fieldname, get_string($fieldname, 'mod_surveypro'), $attributes, $editoroptions);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
$mform->setType($fieldname, PARAM_RAW); // No XSS prevention here, users must be trusted.
// Mailroles.
$fieldname = 'mailroles';
$options = [];
$context = \context_course::instance($COURSE->id);
// I look for roles belonging to governance.
// At the moment the capability 'mod/surveypro:accessreports' seems to be a good indicator of that group.
$roleoptions = get_role_names_with_caps_in_context($context, ['mod/surveypro:accessreports']);
foreach ($roleoptions as $roleid => $rolename) {
$users = get_role_users($roleid, $context, true);
$options[$roleid] = $rolename.' ('.count($users).')';
}
$select = $mform->addElement('select', $fieldname, get_string($fieldname, 'mod_surveypro'), $options);
$select->setMultiple(true);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Mailextraaddresses.
$fieldname = 'mailextraaddresses';
$mform->addElement('textarea', $fieldname, get_string($fieldname, 'mod_surveypro'), $attributes);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Custom mail message for notifications. No embedded pictures to handle.
$fieldname = 'mailcontenteditor';
$mform->addElement('editor', $fieldname, get_string($fieldname, 'mod_surveypro'), $attributes);
$mailcontentdefault = ['text' => get_string('mailcontentdefault', 'surveypro'), 'format' => FORMAT_HTML];
$mform->setDefault($fieldname, $mailcontentdefault);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
$mform->setType($fieldname, PARAM_RAW); // No XSS prevention here, users must be trusted.
// Riskyeditdeadline.
$fieldname = 'riskyeditdeadline';
$mform->addElement('date_time_selector', $fieldname, get_string($fieldname, 'mod_surveypro'));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Add standard elements, common to all modules.
$this->standard_coursemodule_elements();
// Add standard buttons, common to all modules.
$this->add_action_buttons();
}
/**
* Retrieve the data parsed from the address.
*
* @return \stdClass the parsed data.
*/
public function get_data() {
$data = parent::get_data();
if (!$data) {
return false;
}
// Thankspage.
$data->thankspageformat = $data->thankspageeditor['format'];
$data->thankspage = $data->thankspageeditor['text'];
// Mailroles.
if (isset($data->mailroles)) {
$data->mailroles = implode(',', $data->mailroles);
} else {
$data->mailroles = '';
}
// Mailcontent.
$data->mailcontentformat = $data->mailcontenteditor['format'];
$data->mailcontent = $data->mailcontenteditor['text'];
// 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->completionsubmit_check) || !$autocompletion) {
$data->completionsubmit = 0;
}
}
return $data;
}
/**
* Any data processing needed before the form is displayed
* (needed to set up draft areas for editor and filemanager elements)
*
* @param object $defaults the data being passed to the form.
*/
public function data_preprocessing(&$defaults) {
parent::data_preprocessing($defaults);
// Manage userstyle filemanager.
$filename = 'userstyle';
$filemanageroptions = surveypro_get_user_style_options();
$draftitemid = file_get_submitted_draft_itemid($filename.'_filemanager');
if ($this->current->instance) {
file_prepare_draft_area($draftitemid, $this->context->id, 'mod_surveypro',
SURVEYPRO_STYLEFILEAREA, 0, $filemanageroptions);
$defaults[$filename.'_filemanager'] = $draftitemid;
// Manage thankspage editor.
$filename = 'thankspageeditor';
$editoroptions = surveypro_get_editor_options();
// Editing an existing surveypro - let us prepare the added editor elements (intro done automatically).
$draftitemid = file_get_submitted_draft_itemid('thankspage');
$defaults[$filename]['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
'mod_surveypro', SURVEYPRO_THANKSPAGEFILEAREA, 0, $editoroptions, $defaults['thankspage']);
$defaults[$filename]['format'] = $defaults['thankspageformat'];
$defaults[$filename]['itemid'] = $draftitemid;
// Mailroles.
if ($defaults['mailroles']) {
$filename = 'mailroles';
$presetroles = explode(',', $defaults['mailroles']);
foreach ($presetroles as $roleid) {
$values[] = $roleid;
}
$defaults[$filename] = $values;
// Manage mailcontent editor. No embedded pictures to handle.
$filename = 'mailcontenteditor';
$defaults[$filename]['text'] = $defaults['mailcontent'];
$defaults[$filename]['format'] = $defaults['mailcontentformat'];
}
} else {
file_prepare_draft_area($draftitemid, $this->context->id, 'mod_surveypro',
SURVEYPRO_STYLEFILEAREA, 0, $filemanageroptions);
$defaults[$filename.'_filemanager'] = $draftitemid;
// Manage thankspage editor.
$filename = 'thankspageeditor';
$draftitemid = file_get_submitted_draft_itemid('thankspage');
file_prepare_draft_area($draftitemid, null, 'mod_surveypro', SURVEYPRO_THANKSPAGEFILEAREA, 0);
$defaults[$filename]['text'] = '';
$defaults[$filename]['format'] = editors_get_preferred_format();
$defaults[$filename]['itemid'] = $draftitemid;
// Mailroles.
// Nothing to do.
// Manage mailcontent editor. No embedded pictures to handle.
// Nothing to do.
}
$fieldname = 'completionsubmit';
$defaults[$fieldname.'_check'] = !empty($defaults[$fieldname]) ? 1 : 0;
if (empty($defaults[$fieldname])) {
$defaults[$fieldname] = 1;
}
}
/**
* Validate form data.
*
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
$mailextraaddresses = $data['mailextraaddresses'];
$mailextraaddresses = trim($mailextraaddresses);
$mailextraaddresses = preg_replace('~\n\n+~', "\n", $mailextraaddresses);
$extraemail = explode("\n", $mailextraaddresses);
$extraemail = array_map('trim', $extraemail);
$arrayunique = array_unique($extraemail);
if (count($arrayunique) != count($extraemail)) {
$errors['mailextraaddresses'] = get_string('duplicateemail', 'surveypro');
}
return $errors;
}
/**
* Add_completion_rules.
*
* @return void
*/
public function add_completion_rules() {
$mform =& $this->_form;
// Completionsubmit_check is not saved to db because it is completely redundant.
// If completionsubmit is not empty it is checked otherwise it is not checked.
// See data_preprocessing method just few lines above.
$fieldname = 'completionsubmit';
$elementgroup = [];
$checklabel = get_string($fieldname.'_check', 'mod_surveypro');
$elementgroup[] = $mform->createElement('checkbox', $fieldname.'_check', '', $checklabel);
$elementgroup[] = $mform->createElement('text', $fieldname, '', ['size' => 3]);
$mform->setType($fieldname, PARAM_INT);
$mform->addGroup($elementgroup, $fieldname.'_group', get_string($fieldname.'_group', 'mod_surveypro'), ' ', false);
$mform->addHelpButton($fieldname.'_group', $fieldname.'_group', 'surveypro');
$mform->disabledIf($fieldname, $fieldname.'_check', 'notchecked');
return [$fieldname.'_group'];
}
/**
* Completion_rule_enabled.
*
* @param array $data
* @return bool
*/
public function completion_rule_enabled($data) {
return (!empty($data['completionsubmit_check']) && ($data['completionsubmit'] != 0));
}
}