forked from kordan/moodle-mod_surveypro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_form.php
304 lines (257 loc) · 12.2 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
<?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'), array('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'), array('optional' => true));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Close date.
$fieldname = 'timeclose';
$mform->addElement('date_time_selector', $fieldname, get_string($fieldname, 'mod_surveypro'), array('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');
// Allow/deny saveresume.
$fieldname = 'saveresume';
$mform->addElement('checkbox', $fieldname, get_string($fieldname, 'mod_surveypro'));
$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);
$mform->setDefault($fieldname, 2020);
$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');
// Notifyrole.
$fieldname = 'notifyrole';
$options = array();
$context = context_course::instance($COURSE->id);
$roleoptions = get_role_names_with_caps_in_context($context, array('mod/surveypro:submit'));
$roleoptions += get_role_names_with_caps_in_context($context, array('mod/surveypro:accessreports'));
foreach ($roleoptions as $roleid => $rolename) {
$options[$roleid] = $rolename;
}
$select = $mform->addElement('select', $fieldname, get_string($fieldname, 'mod_surveypro'), $options);
$select->setMultiple(true);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Notifymore.
$fieldname = 'notifymore';
$textareaoptions = array('wrap' => 'virtual', 'rows' => '10', 'cols' => '65');
$mform->addElement('textarea', $fieldname, get_string($fieldname, 'mod_surveypro'), $textareaoptions);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Define thanks page.
$fieldname = 'thankshtml';
$editoroptions = surveypro_get_editor_options();
$mform->addElement('editor', $fieldname.'_editor', get_string($fieldname, 'mod_surveypro'), null, $editoroptions);
$mform->addHelpButton($fieldname.'_editor', $fieldname, 'surveypro');
$mform->setType($fieldname.'_editor', 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;
}
$data->thankshtmlformat = $data->thankshtml_editor['format'];
$data->thankshtml = $data->thankshtml_editor['text'];
// Notifyrole.
if (isset($data->notifyrole)) {
$data->notifyrole = implode($data->notifyrole, ',');
} else {
$data->notifyrole = '';
}
// 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);
if ($this->current->instance) {
// Manage userstyle filemanager.
$filename = 'userstyle';
$filemanageroptions = surveypro_get_user_style_options();
$draftitemid = file_get_submitted_draft_itemid($filename.'_filemanager');
$filearea = SURVEYPRO_STYLEFILEAREA;
file_prepare_draft_area($draftitemid, $this->context->id, 'mod_surveypro', $filearea, 0, $filemanageroptions);
$defaults[$filename.'_filemanager'] = $draftitemid;
// Manage thankshtml editor.
$filename = 'thankshtml';
$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($filename);
$defaults[$filename.'_editor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
'mod_surveypro', SURVEYPRO_THANKSHTMLFILEAREA, false, $editoroptions, $defaults[$filename]);
$defaults[$filename.'_editor']['format'] = $defaults['thankshtmlformat'];
$defaults[$filename.'_editor']['itemid'] = $draftitemid;
// Notifyrole.
$presetroles = explode(',', $defaults['notifyrole']);
foreach ($presetroles as $roleid) {
$values[] = $roleid;
}
$defaults['notifyrole'] = $values;
}
$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);
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 = array();
$checklabel = get_string($fieldname.'_check', 'mod_surveypro');
$elementgroup[] = $mform->createElement('checkbox', $fieldname.'_check', '', $checklabel);
$elementgroup[] = $mform->createElement('text', $fieldname, '', array('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 array($fieldname.'_group');
}
/**
* Completion_rule_enabled.
*
* @param array $data
* @return bool
*/
public function completion_rule_enabled($data) {
return (!empty($data['completionsubmit_check']) && ($data['completionsubmit'] != 0));
}
}