-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathquestions_form.php
More file actions
388 lines (333 loc) · 18 KB
/
questions_form.php
File metadata and controls
388 lines (333 loc) · 18 KB
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
<?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/>.
namespace mod_questionnaire;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');
#[\AllowDynamicProperties]
/**
* The form definition class for questions.
*
* @package mod_questionnaire
* @copyright 2016 Mike Churchward (mike.churchward@poetgroup.org)
* @author Mike Churchward & Joseph Rézeau
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class questions_form extends \moodleform {
/**
* The constructor.
* @param mixed $action
* @param bool $moveq
*/
public function __construct($action, $moveq=false) {
$this->moveq = $moveq;
return parent::__construct($action);
}
/**
* Form definition.
*/
public function definition() {
global $CFG, $questionnaire, $SESSION;
global $DB;
$sid = $questionnaire->survey->id;
$mform =& $this->_form;
$mform->addElement('header', 'questionhdr', get_string('addquestions', 'questionnaire'));
$mform->addHelpButton('questionhdr', 'questiontypes', 'questionnaire');
$strremove = get_string('remove', 'questionnaire');
$strmove = get_string('move');
$strmovehere = get_string('movehere');
$strposition = get_string('position', 'questionnaire');
if (!isset($questionnaire->questions)) {
$questionnaire->questions = array();
}
if ($this->moveq) {
$moveqposition = $questionnaire->questions[$this->moveq]->position;
}
$pos = 0;
$select = '';
if (!($qtypes = $DB->get_records_select_menu('questionnaire_question_type', $select, null, '', 'typeid,type'))) {
$qtypes = array();
}
// Get the names of each question type in the appropriate language.
foreach ($qtypes as $key => $qtype) {
// Do not allow "Page Break" to be selected as first element of a Questionnaire.
if (empty($questionnaire->questions) && ($qtype == 'Page Break')) {
unset($qtypes[$key]);
} else {
$qtypes[$key] = questionnaire_get_type($key);
}
}
natsort($qtypes);
$addqgroup = array();
$addqgroup[] =& $mform->createElement('select', 'type_id', '', $qtypes);
// The 'sticky' type_id value for further new questions.
if (isset($SESSION->questionnaire->type_id)) {
$mform->setDefault('type_id', $SESSION->questionnaire->type_id);
}
$addqgroup[] =& $mform->createElement('submit', 'addqbutton', get_string('addselqtype', 'questionnaire'));
$questionnairehasdependencies = $questionnaire->has_dependencies();
$mform->addGroup($addqgroup, 'addqgroup', '', ' ', false);
if (isset($SESSION->questionnaire->validateresults) && $SESSION->questionnaire->validateresults != '') {
$mform->addElement('static', 'validateresult', '', '<div class="qdepend warning">'.
$SESSION->questionnaire->validateresults.'</div>');
$SESSION->questionnaire->validateresults = '';
}
$qnum = 0;
// JR skip logic :: to prevent moving child higher than parent OR parent lower than child
// we must get now the parent and child positions.
if ($questionnairehasdependencies) {
$parentpositions = questionnaire_get_parent_positions($questionnaire->questions);
$childpositions = questionnaire_get_child_positions($questionnaire->questions);
}
$mform->addElement('header', 'manageq', get_string('managequestions', 'questionnaire'));
$mform->addHelpButton('manageq', 'managequestions', 'questionnaire');
$mform->addElement('html', '<div class="qcontainer">');
foreach ($questionnaire->questions as $question) {
$manageqgroup = array();
$qid = $question->id;
$tid = $question->type_id;
$qtype = $question->type;
$required = $question->required;
// Get displayable list of parents for the questions in questions_form.
if ($questionnairehasdependencies) {
// TODO - Perhaps this should be a function called by the questionnaire after it loads all questions?
$questionnaire->load_parents($question);
$dependencies = $questionnaire->renderer->get_dependency_html($question->id, $question->dependencies);
} else {
$dependencies = '';
}
$pos = $question->position;
// No page break in first position!
if ($tid == QUESPAGEBREAK && $pos == 1) {
$DB->set_field('questionnaire_question', 'deleted', 'y', ['id' => $qid, 'surveyid' => $sid]);
if ($records = $DB->get_records_select('questionnaire_question', 'surveyid = ' . $sid, null, 'position ASC')) {
foreach ($records as $record) {
$DB->set_field('questionnaire_question', 'position', $record->position - 1, array('id' => $record->id));
}
}
redirect($CFG->wwwroot.'/mod/questionnaire/questions.php?id='.$questionnaire->cm->id);
}
if ($question->is_numbered()) {
$qnum++;
}
// Needed for non-English languages JR.
$qtype = '['.questionnaire_get_type($tid).']';
$content = '';
// If question text is "empty", i.e. 2 non-breaking spaces were inserted, do not display any question text.
if ($question->content == '<p> </p>') {
$question->content = '';
}
if ($tid != QUESPAGEBREAK) {
// Needed to print potential media in question text.
$content = format_text(file_rewrite_pluginfile_urls($question->content, 'pluginfile.php',
$question->context->id, 'mod_questionnaire', 'question', $question->id), FORMAT_HTML, ['noclean' => true]);
}
$moveqgroup = array();
$spacer = $questionnaire->renderer->image_url('spacer');
if (!$this->moveq) {
if ($dependencies) {
// Begin div qn-container with indent if questionnaire has child.
$mform->addElement('html', '<div class="qn-container qn-indent">');
} else {
$mform->addElement('html', '<div class="qn-container">'); // Begin div qn-container.
}
$mextra = array('value' => $question->id,
'alt' => $strmove,
'title' => $strmove);
$eextra = array('value' => $question->id,
'alt' => get_string('edit', 'questionnaire'),
'title' => get_string('edit', 'questionnaire'));
$rextra = array('value' => $question->id,
'alt' => $strremove,
'title' => $strremove);
if ($tid == QUESPAGEBREAK) {
$esrc = $spacer;
$eextra = array('disabled' => 'disabled');
} else {
$esrc = $questionnaire->renderer->image_url('t/edit');
}
$rsrc = $questionnaire->renderer->image_url('t/delete');
// Question numbers.
$manageqgroup[] =& $mform->createElement('static', 'qnums', '',
'<div class="qnums">'.$strposition.' '.$pos.'</div>');
// Need to index by 'id' since IE doesn't return assigned 'values' for image inputs.
$manageqgroup[] =& $mform->createElement('static', 'opentag_'.$question->id, '', '');
$msrc = $questionnaire->renderer->image_url('t/move');
if ($questionnairehasdependencies) {
// Do not allow moving parent question at position #1 to be moved down if it has a child at position < 4.
if ($pos == 1) {
if (isset($childpositions[$qid])) {
$maxdown = $childpositions[$qid];
if ($maxdown < 4) {
$strdisabled = get_string('movedisabled', 'questionnaire');
$msrc = $questionnaire->renderer->image_url('t/block');
$mextra = array('value' => $question->id,
'alt' => $strdisabled,
'title' => $strdisabled);
$mextra += array('disabled' => 'disabled');
}
}
}
// Do not allow moving or deleting a page break if immediately followed by a child question
// or immediately preceded by a question with a dependency and followed by a non-dependent question.
if ($tid == QUESPAGEBREAK) {
if ($nextquestion = $DB->get_record('questionnaire_question',
['surveyid' => $sid, 'position' => $pos + 1, 'deleted' => 'n'], 'id, name, content') ) {
$nextquestiondependencies = $DB->get_records('questionnaire_dependency',
['questionid' => $nextquestion->id , 'surveyid' => $sid], 'id ASC');
if ($previousquestion = $DB->get_record('questionnaire_question',
['surveyid' => $sid, 'position' => $pos - 1, 'deleted' => 'n'], 'id, name, content')) {
$previousquestiondependencies = $DB->get_records('questionnaire_dependency',
['questionid' => $previousquestion->id , 'surveyid' => $sid], 'id ASC');
if (!empty($nextquestiondependencies) ||
(!empty($previousquestiondependencies) && empty($nextquestiondependencies))) {
$strdisabled = get_string('movedisabled', 'questionnaire');
$msrc = $questionnaire->renderer->image_url('t/block');
$mextra = array('value' => $question->id,
'alt' => $strdisabled,
'title' => $strdisabled);
$mextra += array('disabled' => 'disabled');
$rsrc = $msrc;
$strdisabled = get_string('deletedisabled', 'questionnaire');
$rextra = array('value' => $question->id,
'alt' => $strdisabled,
'title' => $strdisabled);
$rextra += array('disabled' => 'disabled');
}
}
}
}
}
$manageqgroup[] =& $mform->createElement('image', 'movebutton['.$question->id.']',
$msrc, $mextra);
$manageqgroup[] =& $mform->createElement('image', 'editbutton['.$question->id.']', $esrc, $eextra);
$manageqgroup[] =& $mform->createElement('image', 'removebutton['.$question->id.']', $rsrc, $rextra);
if ($tid != QUESPAGEBREAK && $tid != QUESSECTIONTEXT && $tid != QUESSLIDER) {
if ($required == 'y') {
$reqsrc = $questionnaire->renderer->image_url('t/stop');
$strrequired = get_string('required', 'questionnaire');
} else {
$reqsrc = $questionnaire->renderer->image_url('t/go');
$strrequired = get_string('notrequired', 'questionnaire');
}
$strrequired .= ' '.get_string('clicktoswitch', 'questionnaire');
$reqextra = array('value' => $question->id,
'alt' => $strrequired,
'title' => $strrequired);
$manageqgroup[] =& $mform->createElement('image', 'requiredbutton['.$question->id.']', $reqsrc, $reqextra);
}
$manageqgroup[] =& $mform->createElement('static', 'closetag_'.$question->id, '', '');
} else {
$manageqgroup[] =& $mform->createElement('static', 'qnum', '',
'<div class="qnums">'.$strposition.' '.$pos.'</div>');
$moveqgroup[] =& $mform->createElement('static', 'qnum', '', '');
$display = true;
if ($questionnairehasdependencies) {
// Prevent moving child to higher position than its parent.
if (isset($parentpositions[$this->moveq])) {
$maxup = $parentpositions[$this->moveq];
if ($pos <= $maxup) {
$display = false;
}
}
// Prevent moving parent to lower position than its (first) child.
if (isset($childpositions[$this->moveq])) {
$maxdown = $childpositions[$this->moveq];
if ($pos >= $maxdown) {
$display = false;
}
}
}
$typeid = $DB->get_field('questionnaire_question', 'type_id', array('id' => $this->moveq));
if ($display) {
// Do not move a page break to first position.
if ($typeid == QUESPAGEBREAK && $pos == 1) {
$manageqgroup[] =& $mform->createElement('static', 'qnums', '', '');
} else {
if ($this->moveq == $question->id) {
$moveqgroup[] =& $mform->createElement('cancel', 'cancelbutton', get_string('cancel'));
} else {
$mextra = array('value' => $question->id,
'alt' => $strmove,
'title' => $strmovehere.' (position '.$pos.')');
$msrc = $questionnaire->renderer->image_url('movehere');
$moveqgroup[] =& $mform->createElement('static', 'opentag_'.$question->id, '', '');
$moveqgroup[] =& $mform->createElement('image', 'moveherebutton['.$pos.']', $msrc, $mextra);
$moveqgroup[] =& $mform->createElement('static', 'closetag_'.$question->id, '', '');
}
}
} else {
$manageqgroup[] =& $mform->createElement('static', 'qnums', '', '');
$moveqgroup[] =& $mform->createElement('static', 'qnums', '', '');
}
}
if ($question->name) {
$qname = '('.$question->name.')';
} else {
$qname = '';
}
$manageqgroup[] =& $mform->createElement('static', 'qinfo_'.$question->id, '', $qtype.' '.$qname);
if (!empty($dependencies)) {
$mform->addElement('static', 'qdepend_' . $question->id, '', $dependencies);
}
if ($question->is_numbered()) {
$qnumber = '<div class="qn-info"><h2 class="qn-number">'.$qnum.'</h2></div>';
} else {
$qnumber = '';
}
if ($this->moveq && $pos < $moveqposition) {
$mform->addGroup($moveqgroup, 'moveqgroup', '', '', false);
}
if ($this->moveq) {
if ($this->moveq == $question->id && $display) {
$mform->addElement('html', '<div class="moving" title="'.$strmove.'">'); // Begin div qn-container.
} else {
$mform->addElement('html', '<div class="qn-container">'); // Begin div qn-container.
}
}
$mform->addGroup($manageqgroup, 'manageqgroup', '', ' ', false);
if ($tid != QUESPAGEBREAK) {
$mform->addElement('static', 'qcontent_'.$question->id, '',
$qnumber.'<div class="qn-question">'.$content.'</div>');
}
$mform->addElement('html', '</div>'); // End div qn-container.
if ($this->moveq && $pos >= $moveqposition) {
$mform->addGroup($moveqgroup, 'moveqgroup', '', '', false);
}
}
if ($this->moveq) {
$mform->addElement('hidden', 'moveq', $this->moveq);
}
// Hidden fields.
$mform->addElement('hidden', 'id', 0);
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'sid', 0);
$mform->setType('sid', PARAM_INT);
$mform->addElement('hidden', 'action', 'main');
$mform->setType('action', PARAM_RAW);
$mform->setType('moveq', PARAM_RAW);
$mform->addElement('html', '</div>');
}
/**
* Form validation.
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
return $errors;
}
}