1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace mod_questionnaire\question;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* This file contains the parent class for slider question types.
|
|
|
21 |
*
|
|
|
22 |
* @author Hieu Vu Van
|
|
|
23 |
* @copyright 2022 The Open University.
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
25 |
* @package mod_questionnaire
|
|
|
26 |
*/
|
|
|
27 |
class slider extends question {
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Return the responseclass used.
|
|
|
31 |
* @return string
|
|
|
32 |
*/
|
|
|
33 |
protected function responseclass() {
|
|
|
34 |
return '\\mod_questionnaire\\responsetype\\slider';
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Return the help name.
|
|
|
39 |
* @return string
|
|
|
40 |
*/
|
|
|
41 |
public function helpname() {
|
|
|
42 |
return 'slider';
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Return true if the question has choices.
|
|
|
47 |
*/
|
|
|
48 |
public function has_choices() {
|
|
|
49 |
return false;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Override and return a form template if provided. Output of question_survey_display is iterpreted based on this.
|
|
|
54 |
*
|
|
|
55 |
* @return boolean | string
|
|
|
56 |
*/
|
|
|
57 |
public function question_template() {
|
|
|
58 |
return 'mod_questionnaire/question_slider';
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Override and return a response template if provided. Output of response_survey_display is iterpreted based on this.
|
|
|
63 |
*
|
|
|
64 |
* @return boolean | string
|
|
|
65 |
*/
|
|
|
66 |
public function response_template() {
|
|
|
67 |
return 'mod_questionnaire/response_slider';
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Return the context tags for the check question template.
|
|
|
72 |
*
|
|
|
73 |
* @param \mod_questionnaire\responsetype\response\response $response
|
|
|
74 |
* @param array $dependants Array of all questions/choices depending on this question.
|
|
|
75 |
* @param boolean $blankquestionnaire
|
|
|
76 |
* @return object The check question context tags.
|
|
|
77 |
*
|
|
|
78 |
*/
|
|
|
79 |
protected function question_survey_display($response, $dependants = [], $blankquestionnaire = false) {
|
|
|
80 |
global $PAGE;
|
|
|
81 |
$PAGE->requires->js_init_call('M.mod_questionnaire.init_slider', null, false, questionnaire_get_js_module());
|
|
|
82 |
$extradata = json_decode($this->extradata);
|
|
|
83 |
$questiontags = new \stdClass();
|
|
|
84 |
if (isset($response->answers[$this->id][0])) {
|
|
|
85 |
$extradata->startingvalue = $response->answers[$this->id][0]->value;
|
|
|
86 |
}
|
|
|
87 |
$extradata->name = 'q' . $this->id;
|
|
|
88 |
$extradata->id = self::qtypename($this->type_id) . $this->id;
|
|
|
89 |
$questiontags->qelements = new \stdClass();
|
|
|
90 |
$questiontags->qelements->extradata = $extradata;
|
|
|
91 |
return $questiontags;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Return the context tags for the slider response template.
|
|
|
96 |
* @param \mod_questionnaire\responsetype\response\response $response
|
|
|
97 |
* @return \stdClass The check question response context tags.
|
|
|
98 |
*/
|
|
|
99 |
protected function response_survey_display($response) {
|
|
|
100 |
global $PAGE;
|
|
|
101 |
$PAGE->requires->js_init_call('M.mod_questionnaire.init_slider', null, false, questionnaire_get_js_module());
|
|
|
102 |
|
|
|
103 |
$resptags = new \stdClass();
|
|
|
104 |
if (isset($response->answers[$this->id])) {
|
|
|
105 |
$answer = reset($response->answers[$this->id]);
|
|
|
106 |
$resptags->content = format_text($answer->value, FORMAT_HTML);
|
|
|
107 |
if (!empty($response->answers[$this->id]['extradata'])) {
|
|
|
108 |
$resptags->extradata = $response->answers[$this->id]['extradata'];
|
|
|
109 |
} else {
|
|
|
110 |
$extradata = json_decode($this->extradata);
|
|
|
111 |
$resptags->extradata = $extradata;
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
return $resptags;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Add the form required field.
|
|
|
119 |
* @param \MoodleQuickForm $mform
|
|
|
120 |
* @return \MoodleQuickForm
|
|
|
121 |
*/
|
|
|
122 |
protected function form_required(\MoodleQuickForm $mform) {
|
|
|
123 |
return $mform;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Return the form precision.
|
|
|
128 |
* @param \MoodleQuickForm $mform
|
|
|
129 |
* @param string $helptext
|
|
|
130 |
* @return \MoodleQuickForm|void
|
|
|
131 |
*/
|
|
|
132 |
protected function form_precise(\MoodleQuickForm $mform, $helptext = '') {
|
|
|
133 |
return question::form_precise_hidden($mform);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Return the form length.
|
|
|
138 |
* @param \MoodleQuickForm $mform
|
|
|
139 |
* @param string $helptext
|
|
|
140 |
* @return \MoodleQuickForm|void
|
|
|
141 |
*/
|
|
|
142 |
protected function form_length(\MoodleQuickForm $mform, $helptext = '') {
|
|
|
143 |
return question::form_length_hidden($mform);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Override if the question uses the extradata field.
|
|
|
148 |
* @param \MoodleQuickForm $mform
|
|
|
149 |
* @param string $helpname
|
|
|
150 |
* @return \MoodleQuickForm
|
|
|
151 |
*/
|
|
|
152 |
protected function form_extradata(\MoodleQuickForm $mform, $helpname = '') {
|
|
|
153 |
$minelementname = 'minrange';
|
|
|
154 |
$maxelementname = 'maxrange';
|
|
|
155 |
$startingvalue = 'startingvalue';
|
|
|
156 |
$stepvalue = 'stepvalue';
|
|
|
157 |
|
|
|
158 |
$ranges = [];
|
|
|
159 |
if (!empty($this->extradata)) {
|
|
|
160 |
$ranges = json_decode($this->extradata);
|
|
|
161 |
}
|
|
|
162 |
$mform->addElement('text', 'leftlabel', get_string('leftlabel', 'questionnaire'));
|
|
|
163 |
$mform->setType('leftlabel', PARAM_RAW);
|
|
|
164 |
if (isset($ranges->leftlabel)) {
|
|
|
165 |
$mform->setDefault('leftlabel', $ranges->leftlabel);
|
|
|
166 |
}
|
|
|
167 |
$mform->addElement('text', 'centerlabel', get_string('centerlabel', 'questionnaire'));
|
|
|
168 |
$mform->setType('centerlabel', PARAM_RAW);
|
|
|
169 |
if (isset($ranges->centerlabel)) {
|
|
|
170 |
$mform->setDefault('centerlabel', $ranges->centerlabel);
|
|
|
171 |
}
|
|
|
172 |
$mform->addElement('text', 'rightlabel', get_string('rightlabel', 'questionnaire'));
|
|
|
173 |
$mform->setType('rightlabel', PARAM_RAW);
|
|
|
174 |
if (isset($ranges->rightlabel)) {
|
|
|
175 |
$mform->setDefault('rightlabel', $ranges->rightlabel);
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
$patterint = '/^-?\d+$/';
|
|
|
179 |
$mform->addElement('text', $minelementname, get_string($minelementname, 'questionnaire'), ['size' => '3']);
|
|
|
180 |
$mform->setType($minelementname, PARAM_RAW);
|
|
|
181 |
$mform->addRule($minelementname, get_string('err_required', 'form'), 'required', null, 'client');
|
|
|
182 |
$mform->addRule($minelementname, get_string('err_numeric', 'form'), 'numeric', '', 'client');
|
|
|
183 |
$mform->addRule($minelementname, get_string('err_numeric', 'form'), 'regex', $patterint, 'client');
|
|
|
184 |
$mform->addHelpButton($minelementname, $minelementname, 'questionnaire');
|
|
|
185 |
if (isset($ranges->minrange)) {
|
|
|
186 |
$mform->setDefault($minelementname, $ranges->minrange);
|
|
|
187 |
} else {
|
|
|
188 |
$mform->setDefault($minelementname, 1);
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
$mform->addElement('text', $maxelementname, get_string($maxelementname, 'questionnaire'), ['size' => '3']);
|
|
|
192 |
$mform->setType($maxelementname, PARAM_RAW);
|
|
|
193 |
$mform->addHelpButton($maxelementname, $maxelementname, 'questionnaire');
|
|
|
194 |
$mform->addRule($maxelementname, get_string('err_required', 'form'), 'required', null, 'client');
|
|
|
195 |
$mform->addRule($maxelementname, get_string('err_numeric', 'form'), 'numeric', '', 'client');
|
|
|
196 |
$mform->addRule($maxelementname, get_string('err_numeric', 'form'), 'regex', $patterint, 'client');
|
|
|
197 |
if (isset($ranges->maxrange)) {
|
|
|
198 |
$mform->setDefault($maxelementname, $ranges->maxrange);
|
|
|
199 |
} else {
|
|
|
200 |
$mform->setDefault($maxelementname, 10);
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
$mform->addElement('text', $startingvalue, get_string($startingvalue, 'questionnaire'), ['size' => '3']);
|
|
|
204 |
$mform->setType($startingvalue, PARAM_RAW);
|
|
|
205 |
$mform->addHelpButton($startingvalue, $startingvalue, 'questionnaire');
|
|
|
206 |
$mform->addRule($startingvalue, get_string('err_required', 'form'), 'required', null, 'client');
|
|
|
207 |
$mform->addRule($startingvalue, get_string('err_numeric', 'form'), 'numeric', '', 'client');
|
|
|
208 |
$mform->addRule($startingvalue, get_string('err_numeric', 'form'), 'regex', $patterint, 'client');
|
|
|
209 |
if (isset($ranges->startingvalue)) {
|
|
|
210 |
$mform->setDefault($startingvalue, $ranges->startingvalue);
|
|
|
211 |
} else {
|
|
|
212 |
$mform->setDefault($startingvalue, 5);
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
$mform->addElement('text', $stepvalue, get_string($stepvalue, 'questionnaire'), ['size' => '3']);
|
|
|
216 |
$mform->setType($stepvalue, PARAM_RAW);
|
|
|
217 |
$mform->addHelpButton($stepvalue, $stepvalue, 'questionnaire');
|
|
|
218 |
$mform->addRule($stepvalue, get_string('err_required', 'form'), 'required', null, 'client');
|
|
|
219 |
$mform->addRule($stepvalue, get_string('err_numeric', 'form'), 'numeric', '', 'client');
|
|
|
220 |
$mform->addRule($stepvalue, get_string('err_numeric', 'form'), 'regex', '/^-?\d+$/', 'client');
|
|
|
221 |
|
|
|
222 |
if (isset($ranges->stepvalue)) {
|
|
|
223 |
$mform->setDefault($stepvalue, $ranges->stepvalue);
|
|
|
224 |
} else {
|
|
|
225 |
$mform->setDefault($stepvalue, 1);
|
|
|
226 |
}
|
|
|
227 |
return $mform;
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
/**
|
|
|
231 |
* Any preprocessing of general data.
|
|
|
232 |
* @param \stdClass $formdata
|
|
|
233 |
* @return bool
|
|
|
234 |
*/
|
|
|
235 |
protected function form_preprocess_data($formdata) {
|
|
|
236 |
$ranges = [];
|
|
|
237 |
if (isset($formdata->minrange)) {
|
|
|
238 |
$ranges['minrange'] = $formdata->minrange;
|
|
|
239 |
}
|
|
|
240 |
if (isset($formdata->maxrange)) {
|
|
|
241 |
$ranges['maxrange'] = $formdata->maxrange;
|
|
|
242 |
}
|
|
|
243 |
if (isset($formdata->startingvalue)) {
|
|
|
244 |
$ranges['startingvalue'] = $formdata->startingvalue;
|
|
|
245 |
}
|
|
|
246 |
if (isset($formdata->stepvalue)) {
|
|
|
247 |
$ranges['stepvalue'] = $formdata->stepvalue;
|
|
|
248 |
}
|
|
|
249 |
if (isset($formdata->leftlabel)) {
|
|
|
250 |
$ranges['leftlabel'] = $formdata->leftlabel;
|
|
|
251 |
}
|
|
|
252 |
if (isset($formdata->rightlabel)) {
|
|
|
253 |
$ranges['rightlabel'] = $formdata->rightlabel;
|
|
|
254 |
}
|
|
|
255 |
if (isset($formdata->centerlabel)) {
|
|
|
256 |
$ranges['centerlabel'] = $formdata->centerlabel;
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
// Now store the new named degrees in extradata.
|
|
|
260 |
$formdata->extradata = json_encode($ranges);
|
|
|
261 |
return parent::form_preprocess_data($formdata);
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
/**
|
|
|
265 |
* True if question provides mobile support.
|
|
|
266 |
*
|
|
|
267 |
* @return bool
|
|
|
268 |
*/
|
|
|
269 |
public function supports_mobile() {
|
|
|
270 |
return true;
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
/**
|
|
|
274 |
* True if question need extradata for mobile app.
|
|
|
275 |
*
|
|
|
276 |
* @return bool
|
|
|
277 |
*/
|
|
|
278 |
public function mobile_question_extradata_display() {
|
|
|
279 |
return true;
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
/**
|
|
|
283 |
* Return the mobile question display.
|
|
|
284 |
*
|
|
|
285 |
* @param int $qnum
|
|
|
286 |
* @param bool $autonum
|
|
|
287 |
* @return \stdClass
|
|
|
288 |
*/
|
|
|
289 |
public function mobile_question_display($qnum, $autonum = false) {
|
|
|
290 |
$mobiledata = parent::mobile_question_display($qnum, $autonum);
|
|
|
291 |
$mobiledata->isslider = true;
|
|
|
292 |
return $mobiledata;
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
/**
|
|
|
296 |
* Return the otherdata to be used by the mobile app.
|
|
|
297 |
*
|
|
|
298 |
* @return array
|
|
|
299 |
*/
|
|
|
300 |
public function mobile_otherdata() {
|
|
|
301 |
$extradata = json_decode($this->extradata);
|
|
|
302 |
return [$this->mobile_fieldkey() => $extradata->startingvalue];
|
|
|
303 |
}
|
|
|
304 |
}
|