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 radio question types.
|
|
|
21 |
*
|
|
|
22 |
* @author Mike Churchward
|
|
|
23 |
* @copyright 2016 onward Mike Churchward (mike.churchward@poetopensource.org)
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
25 |
* @package mod_questionnaire
|
|
|
26 |
*/
|
|
|
27 |
class radio extends question {
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Each question type must define its response class.
|
|
|
31 |
* @return object The response object based off of questionnaire_response_base.
|
|
|
32 |
*/
|
|
|
33 |
protected function responseclass() {
|
|
|
34 |
return '\\mod_questionnaire\\responsetype\\single';
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Short name for this question type - no spaces, etc..
|
|
|
39 |
* @return string
|
|
|
40 |
*/
|
|
|
41 |
public function helpname() {
|
|
|
42 |
return 'radiobuttons';
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Return true if the question has choices.
|
|
|
47 |
*/
|
|
|
48 |
public function has_choices() {
|
|
|
49 |
return true;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Override and return a form template if provided. Output of question_survey_display is iterpreted based on this.
|
|
|
54 |
* @return boolean | string
|
|
|
55 |
*/
|
|
|
56 |
public function question_template() {
|
|
|
57 |
return 'mod_questionnaire/question_radio';
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Override and return a response template if provided. Output of response_survey_display is iterpreted based on this.
|
|
|
62 |
* @return boolean | string
|
|
|
63 |
*/
|
|
|
64 |
public function response_template() {
|
|
|
65 |
return 'mod_questionnaire/response_radio';
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Override this and return true if the question type allows dependent questions.
|
|
|
70 |
* @return boolean
|
|
|
71 |
*/
|
|
|
72 |
public function allows_dependents() {
|
|
|
73 |
return true;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* True if question type supports feedback options. False by default.
|
|
|
78 |
*/
|
|
|
79 |
public function supports_feedback() {
|
|
|
80 |
return true;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Return the context tags for the check question template.
|
|
|
85 |
* @param \mod_questionnaire\responsetype\response\response $response
|
|
|
86 |
* @param array $dependants Array of all questions/choices depending on this question.
|
|
|
87 |
* @param boolean $blankquestionnaire
|
|
|
88 |
* @return object The check question context tags.
|
|
|
89 |
*
|
|
|
90 |
*/
|
|
|
91 |
protected function question_survey_display($response, $dependants=[], $blankquestionnaire=false) {
|
|
|
92 |
// Radio buttons.
|
|
|
93 |
global $idcounter; // To make sure all radio buttons have unique ids. // JR 20 NOV 2007.
|
|
|
94 |
|
|
|
95 |
$otherempty = false;
|
|
|
96 |
$horizontal = $this->length;
|
|
|
97 |
$ischecked = false;
|
|
|
98 |
|
|
|
99 |
$choicetags = new \stdClass();
|
|
|
100 |
$choicetags->qelements = [];
|
|
|
101 |
|
|
|
102 |
foreach ($this->choices as $id => $choice) {
|
|
|
103 |
$radio = new \stdClass();
|
|
|
104 |
if ($horizontal) {
|
|
|
105 |
$radio->horizontal = $horizontal;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
if (!$choice->is_other_choice()) { // This is a normal radio button.
|
|
|
109 |
$htmlid = 'auto-rb'.sprintf('%04d', ++$idcounter);
|
|
|
110 |
|
|
|
111 |
$radio->name = 'q'.$this->id;
|
|
|
112 |
$radio->id = $htmlid;
|
|
|
113 |
$radio->value = $id;
|
|
|
114 |
if (isset($response->answers[$this->id][$id])) {
|
|
|
115 |
$radio->checked = true;
|
|
|
116 |
$ischecked = true;
|
|
|
117 |
}
|
|
|
118 |
$value = '';
|
|
|
119 |
if ($blankquestionnaire) {
|
|
|
120 |
$radio->disabled = true;
|
|
|
121 |
$value = ' ('.$choice->value.') ';
|
|
|
122 |
}
|
|
|
123 |
$contents = questionnaire_choice_values($choice->content);
|
|
|
124 |
$radio->label = $value.format_text($contents->text, FORMAT_HTML, ['noclean' => true]).$contents->image;
|
|
|
125 |
} else { // Radio button with associated !other text field.
|
|
|
126 |
$othertext = $choice->other_choice_display();
|
|
|
127 |
$cname = choice::id_other_choice_name($id);
|
|
|
128 |
$odata = isset($response->answers[$this->id][$id]) ? $response->answers[$this->id][$id]->value : '';
|
|
|
129 |
$htmlid = 'auto-rb'.sprintf('%04d', ++$idcounter);
|
|
|
130 |
|
|
|
131 |
$radio->name = 'q'.$this->id;
|
|
|
132 |
$radio->id = $htmlid;
|
|
|
133 |
$radio->value = $id;
|
|
|
134 |
if (isset($response->answers[$this->id][$id]) || !empty($odata)) {
|
|
|
135 |
$radio->checked = true;
|
|
|
136 |
$ischecked = true;
|
|
|
137 |
}
|
|
|
138 |
$otherempty = !empty($radio->checked) && empty($odata);
|
|
|
139 |
$radio->label = format_text($othertext, FORMAT_HTML, ['noclean' => true]);
|
|
|
140 |
$radio->oname = 'q'.$this->id.choice::id_other_choice_name($id);
|
|
|
141 |
$radio->oid = $htmlid.'-other';
|
|
|
142 |
if (isset($odata)) {
|
|
|
143 |
$radio->ovalue = format_string(stripslashes($odata));
|
|
|
144 |
}
|
|
|
145 |
$radio->olabel = 'Text for '.format_text($othertext, FORMAT_HTML, ['noclean' => true]);
|
|
|
146 |
}
|
|
|
147 |
$choicetags->qelements[] = (object)['choice' => $radio];
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
// CONTRIB-846.
|
|
|
151 |
if (!$this->required()) {
|
|
|
152 |
$radio = new \stdClass();
|
|
|
153 |
$htmlid = 'auto-rb'.sprintf('%04d', ++$idcounter);
|
|
|
154 |
if ($horizontal) {
|
|
|
155 |
$radio->horizontal = $horizontal;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
$radio->name = 'q'.$this->id;
|
|
|
159 |
$radio->id = $htmlid;
|
|
|
160 |
$radio->value = 0;
|
|
|
161 |
|
|
|
162 |
if (!$ischecked && !$blankquestionnaire) {
|
|
|
163 |
$radio->checked = true;
|
|
|
164 |
}
|
|
|
165 |
$content = get_string('noanswer', 'questionnaire');
|
|
|
166 |
$radio->label = format_text($content, FORMAT_HTML, ['noclean' => true]);
|
|
|
167 |
|
|
|
168 |
$choicetags->qelements[] = (object)['choice' => $radio];
|
|
|
169 |
}
|
|
|
170 |
// End CONTRIB-846.
|
|
|
171 |
|
|
|
172 |
if ($otherempty) {
|
|
|
173 |
$this->add_notification(get_string('otherempty', 'questionnaire'));
|
|
|
174 |
}
|
|
|
175 |
return $choicetags;
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Return the context tags for the radio response template.
|
|
|
180 |
* @param \mod_questionnaire\responsetype\response\response $response
|
|
|
181 |
* @return object The radio question response context tags.
|
|
|
182 |
*/
|
|
|
183 |
protected function response_survey_display($response) {
|
|
|
184 |
static $uniquetag = 0; // To make sure all radios have unique names.
|
|
|
185 |
|
|
|
186 |
$resptags = new \stdClass();
|
|
|
187 |
$resptags->choices = [];
|
|
|
188 |
|
|
|
189 |
$qdata = new \stdClass();
|
|
|
190 |
$horizontal = $this->length;
|
|
|
191 |
if (isset($response->answers[$this->id])) {
|
|
|
192 |
$answer = reset($response->answers[$this->id]);
|
|
|
193 |
$checked = $answer->choiceid;
|
|
|
194 |
} else {
|
|
|
195 |
$checked = null;
|
|
|
196 |
}
|
|
|
197 |
foreach ($this->choices as $id => $choice) {
|
|
|
198 |
$chobj = new \stdClass();
|
|
|
199 |
if ($horizontal) {
|
|
|
200 |
$chobj->horizontal = 1;
|
|
|
201 |
}
|
|
|
202 |
$chobj->name = $id.$uniquetag++;
|
|
|
203 |
$contents = questionnaire_choice_values($choice->content);
|
|
|
204 |
$choice->content = $contents->text.$contents->image;
|
|
|
205 |
if ($id == $checked) {
|
|
|
206 |
$chobj->selected = 1;
|
|
|
207 |
if ($choice->is_other_choice()) {
|
|
|
208 |
$chobj->othercontent = $answer->value;
|
|
|
209 |
}
|
|
|
210 |
}
|
|
|
211 |
if ($choice->is_other_choice()) {
|
|
|
212 |
$chobj->content = $choice->other_choice_display();
|
|
|
213 |
} else {
|
|
|
214 |
$chobj->content = ($choice->content === '' ? $id : format_text($choice->content, FORMAT_HTML, ['noclean' => true]));
|
|
|
215 |
}
|
|
|
216 |
$resptags->choices[] = $chobj;
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
return $resptags;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Check question's form data for complete response.
|
|
|
224 |
*
|
|
|
225 |
* @param object $responsedata The data entered into the response.
|
|
|
226 |
* @return boolean
|
|
|
227 |
*/
|
|
|
228 |
public function response_complete($responsedata) {
|
|
|
229 |
if (isset($responsedata->{'q'.$this->id}) && ($this->required()) &&
|
|
|
230 |
(strpos($responsedata->{'q'.$this->id}, 'other_') !== false)) {
|
|
|
231 |
return (trim($responsedata->{'q'.$this->id.''.substr($responsedata->{'q'.$this->id}, 5)}) != false);
|
|
|
232 |
} else {
|
|
|
233 |
return parent::response_complete($responsedata);
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
/**
|
|
|
238 |
* Return the length form element.
|
|
|
239 |
* @param \MoodleQuickForm $mform
|
|
|
240 |
* @param string $helptext
|
|
|
241 |
*/
|
|
|
242 |
protected function form_length(\MoodleQuickForm $mform, $helptext = '') {
|
|
|
243 |
$lengroup = [];
|
|
|
244 |
$lengroup[] =& $mform->createElement('radio', 'length', '', get_string('vertical', 'questionnaire'), '0');
|
|
|
245 |
$lengroup[] =& $mform->createElement('radio', 'length', '', get_string('horizontal', 'questionnaire'), '1');
|
|
|
246 |
$mform->addGroup($lengroup, 'lengroup', get_string('alignment', 'questionnaire'), ' ', false);
|
|
|
247 |
$mform->addHelpButton('lengroup', 'alignment', 'questionnaire');
|
|
|
248 |
$mform->setType('length', PARAM_INT);
|
|
|
249 |
|
|
|
250 |
return $mform;
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
/**
|
|
|
254 |
* Return the precision form element.
|
|
|
255 |
* @param \MoodleQuickForm $mform
|
|
|
256 |
* @param string $helptext
|
|
|
257 |
*/
|
|
|
258 |
protected function form_precise(\MoodleQuickForm $mform, $helptext = '') {
|
|
|
259 |
return question::form_precise_hidden($mform);
|
|
|
260 |
}
|
|
|
261 |
|
|
|
262 |
/**
|
|
|
263 |
* True if question provides mobile support.
|
|
|
264 |
*
|
|
|
265 |
* @return bool
|
|
|
266 |
*/
|
|
|
267 |
public function supports_mobile() {
|
|
|
268 |
return true;
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
/**
|
|
|
272 |
* Override and return false if not supporting mobile app.
|
|
|
273 |
* @param int $qnum
|
|
|
274 |
* @param bool $autonum
|
|
|
275 |
* @return \stdClass
|
|
|
276 |
*/
|
|
|
277 |
public function mobile_question_display($qnum, $autonum = false) {
|
|
|
278 |
$mobiledata = parent::mobile_question_display($qnum, $autonum);
|
|
|
279 |
$mobiledata->isradiobutton = true;
|
|
|
280 |
return $mobiledata;
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
/**
|
|
|
284 |
* Override and return false if not supporting mobile app.
|
|
|
285 |
* @return array
|
|
|
286 |
*/
|
|
|
287 |
public function mobile_question_choices_display() {
|
|
|
288 |
$choices = parent::mobile_question_choices_display();
|
|
|
289 |
foreach ($choices as $choicenum => $choice) {
|
|
|
290 |
if ($choice->is_other_choice()) {
|
|
|
291 |
$choices[$choicenum]->otherchoicekey = $this->mobile_fieldkey($choice->other_choice_name());
|
|
|
292 |
$choices[$choicenum]->content = format_text($choice->other_choice_display(), FORMAT_HTML, ['noclean' => true]);
|
|
|
293 |
}
|
|
|
294 |
}
|
|
|
295 |
return $choices;
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
/**
|
|
|
299 |
* Return the mobile response data.
|
|
|
300 |
* @param response $response
|
|
|
301 |
* @return array
|
|
|
302 |
*/
|
|
|
303 |
public function get_mobile_response_data($response) {
|
|
|
304 |
$resultdata = [];
|
|
|
305 |
if (isset($response->answers[$this->id])) {
|
|
|
306 |
foreach ($response->answers[$this->id] as $answer) {
|
|
|
307 |
// Add a fieldkey for each choice.
|
|
|
308 |
$resultdata[$this->mobile_fieldkey()] = $answer->choiceid;
|
|
|
309 |
if ($this->choices[$answer->choiceid]->is_other_choice()) {
|
|
|
310 |
$resultdata[$this->mobile_fieldkey($this->choices[$answer->choiceid]->other_choice_name())] = $answer->value;
|
|
|
311 |
}
|
|
|
312 |
}
|
|
|
313 |
}
|
|
|
314 |
return $resultdata;
|
|
|
315 |
}
|
|
|
316 |
}
|