forked from kordan/moodle-mod_surveypro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_form.php
executable file
·286 lines (240 loc) · 11.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
<?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/>.
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/course/moodleform_mod.php');
class mod_surveypro_mod_form extends moodleform_mod {
/*
* definition
*
* @param none
* @return none
*/
public function definition() {
global $COURSE, $DB, $CFG, $cm;
$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
if ($CFG->branch < '29') {
$config = get_config('surveypro');
$this->add_intro_editor($config->requiremodintro, get_string('moduleintro'));
} else {
$this->standard_intro_elements(get_string('moduleintro'));
}
// Open date
$fieldname = 'timeopen';
$mform->addElement('date_time_selector', $fieldname, get_string($fieldname, 'surveypro'), array('optional' => true));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// Close date
$fieldname = 'timeclose';
$mform->addElement('date_time_selector', $fieldname, get_string($fieldname, 'surveypro'), array('optional' => true));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// modulesettinghdr fieldset (header)
$fieldname = 'modulesettinghdr';
$mform->addElement('header', $fieldname, get_string($fieldname, 'surveypro'));
// newpageforchild
$fieldname = 'newpageforchild';
$mform->addElement('checkbox', $fieldname, get_string($fieldname, 'surveypro'));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// allow/deny saveresume
$fieldname = 'saveresume';
$mform->addElement('checkbox', $fieldname, get_string($fieldname, 'surveypro'));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// history
$fieldname = 'history';
$mform->addElement('checkbox', $fieldname, get_string($fieldname, 'surveypro'));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// allow/deny anonymous
$fieldname = 'anonymous';
$mform->addElement('checkbox', $fieldname, get_string($fieldname, 'surveypro'));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// recaptcha
if (surveypro_site_recaptcha_enabled()) {
$fieldname = 'captcha';
$mform->addElement('checkbox', $fieldname, get_string($fieldname, 'surveypro'));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
}
// startyear
$boundaryyear = array_combine(range(1902, 2038), range(1902, 2038));
$fieldname = 'startyear';
$mform->addElement('select', $fieldname, get_string($fieldname, 'surveypro'), $boundaryyear);
$mform->setDefault($fieldname, 1970);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// stopyear
$fieldname = 'stopyear';
$mform->addElement('select', $fieldname, get_string($fieldname, 'surveypro'), $boundaryyear);
$mform->setDefault($fieldname, 2020);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// userstyle
$fieldname = 'userstyle';
$filemanageroptions = surveypro_get_user_style_options();
$mform->addElement('filemanager', $fieldname.'_filemanager', get_string($fieldname, 'surveypro'), null, $filemanageroptions);
$mform->addHelpButton($fieldname.'_filemanager', $fieldname, 'surveypro');
// maxentries
$fieldname = 'maxentries';
$countoptions = array(0 => get_string('unlimited', 'surveypro')) +
(array_combine(range(1, SURVEYPRO_MAX_ENTRIES), // keys
range(1, SURVEYPRO_MAX_ENTRIES))); // values
$mform->addElement('select', $fieldname, get_string($fieldname, '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, 'surveypro'), $options);
$select->setMultiple(true);
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// notifymore
$fieldname = 'notifymore';
$mform->addElement('textarea', $fieldname, get_string($fieldname, 'surveypro'), array('wrap' => 'virtual', 'rows' => '10', 'cols' => '65'));
$mform->addHelpButton($fieldname, $fieldname, 'surveypro');
// define thanks page
$fieldname = 'thankshtml';
// $context = context_course::instance($COURSE->id); <-- just defined 20 rows above
$editoroptions = surveypro_get_editor_options();
$mform->addElement('editor', $fieldname.'_editor', get_string($fieldname, '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, '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();
}
// this function is executed once mod_form has been displayed
// and it is an helper to prepare data before saving them
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;
}
// this function is executed once mod_form has been displayed
// and is needed to define some presets
public function data_preprocessing(&$defaults) {
global $DB;
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');
file_prepare_draft_area($draftitemid, $this->context->id, 'mod_surveypro', SURVEYPRO_STYLEFILEAREA, 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;
}
}
/*
* validation
*
* @param $data
* @param $files
* @return $errors
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
return $errors;
}
/*
* add_completion_rules
*
* @return none
*/
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();
$elementgroup[] = $mform->createElement('checkbox', $fieldname.'_check', '', get_string($fieldname.'_check', 'surveypro'));
$elementgroup[] = $mform->createElement('text', $fieldname, '', array('size' => 3));
$mform->setType($fieldname, PARAM_INT);
$mform->addGroup($elementgroup, $fieldname.'_group', get_string($fieldname.'_group', 'surveypro'), ' ', false);
$mform->addHelpButton($fieldname.'_group', $fieldname.'_group', 'surveypro');
$mform->disabledIf($fieldname, $fieldname.'_check', 'notchecked');
return array($fieldname.'_group');
}
/*
* completion_rule_enabled
*
* @param $data
* @return none
*/
public function completion_rule_enabled($data) {
return (!empty($data['completionsubmit_check']) && ($data['completionsubmit'] != 0));
}
}