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 |
use \html_writer;
|
|
|
19 |
use mod_questionnaire\question\choice;
|
|
|
20 |
use mod_questionnaire\responsetype\response\response;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* This file contains the parent class for drop question types.
|
|
|
24 |
*
|
|
|
25 |
* @author Mike Churchward
|
|
|
26 |
* @copyright 2016 onward Mike Churchward (mike.churchward@poetopensource.org)
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
28 |
* @package mod_questionnaire
|
|
|
29 |
*/
|
|
|
30 |
class drop extends question {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Each question type must define its response class.
|
|
|
34 |
* @return string The response object based off of questionnaire_response_base.
|
|
|
35 |
*/
|
|
|
36 |
protected function responseclass() {
|
|
|
37 |
return '\\mod_questionnaire\\responsetype\\single';
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Short name for this question type - no spaces, etc..
|
|
|
42 |
* @return string
|
|
|
43 |
*/
|
|
|
44 |
public function helpname() {
|
|
|
45 |
return 'dropdown';
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Return true if the question has choices.
|
|
|
50 |
*/
|
|
|
51 |
public function has_choices() {
|
|
|
52 |
return true;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Override and return a form template if provided. Output of question_survey_display is iterpreted based on this.
|
|
|
57 |
* @return string
|
|
|
58 |
*/
|
|
|
59 |
public function question_template() {
|
|
|
60 |
return 'mod_questionnaire/question_drop';
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Override and return a form template if provided. Output of response_survey_display is iterpreted based on this.
|
|
|
65 |
* @return string
|
|
|
66 |
*/
|
|
|
67 |
public function response_template() {
|
|
|
68 |
return 'mod_questionnaire/response_drop';
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Override this and return true if the question type allows dependent questions.
|
|
|
73 |
* @return bool
|
|
|
74 |
*/
|
|
|
75 |
public function allows_dependents() {
|
|
|
76 |
return true;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* True if question type supports feedback options. False by default.
|
|
|
81 |
* @return bool
|
|
|
82 |
*/
|
|
|
83 |
public function supports_feedback() {
|
|
|
84 |
return true;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Return the context tags for the check question template.
|
|
|
89 |
* @param \mod_questionnaire\responsetype\response\response $response
|
|
|
90 |
* @param array $dependants Array of all questions/choices depending on this question.
|
|
|
91 |
* @param boolean $blankquestionnaire
|
|
|
92 |
* @return object The check question context tags.
|
|
|
93 |
*
|
|
|
94 |
*/
|
|
|
95 |
protected function question_survey_display($response, $dependants, $blankquestionnaire=false) {
|
|
|
96 |
// Drop.
|
|
|
97 |
$options = [];
|
|
|
98 |
|
|
|
99 |
$choicetags = new \stdClass();
|
|
|
100 |
$choicetags->qelements = new \stdClass();
|
|
|
101 |
$options[] = (object)['value' => '', 'label' => get_string('choosedots')];
|
|
|
102 |
foreach ($this->choices as $key => $choice) {
|
|
|
103 |
if ($pos = strpos($choice->content, '=')) {
|
|
|
104 |
$choice->content = substr($choice->content, $pos + 1);
|
|
|
105 |
}
|
|
|
106 |
$option = new \stdClass();
|
|
|
107 |
$option->value = $key;
|
|
|
108 |
$option->label = format_string($choice->content);
|
|
|
109 |
if (isset($response->answers[$this->id][$key])) {
|
|
|
110 |
$option->selected = true;
|
|
|
111 |
}
|
|
|
112 |
$options[] = $option;
|
|
|
113 |
}
|
|
|
114 |
$chobj = new \stdClass();
|
|
|
115 |
$chobj->name = 'q'.$this->id;
|
|
|
116 |
$chobj->id = self::qtypename($this->type_id) . $this->name;
|
|
|
117 |
$chobj->class = 'select custom-select menu q'.$this->id;
|
|
|
118 |
$chobj->options = $options;
|
|
|
119 |
$choicetags->qelements->choice = $chobj;
|
|
|
120 |
|
|
|
121 |
return $choicetags;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Return the context tags for the drop response template.
|
|
|
126 |
* @param \mod_questionnaire\responsetype\response\response $response
|
|
|
127 |
* @return \stdClass The check question response context tags.
|
|
|
128 |
*/
|
|
|
129 |
protected function response_survey_display($response) {
|
|
|
130 |
static $uniquetag = 0; // To make sure all radios have unique names.
|
|
|
131 |
|
|
|
132 |
$resptags = new \stdClass();
|
|
|
133 |
$resptags->name = 'q' . $this->id.$uniquetag++;
|
|
|
134 |
$resptags->id = 'menu' . $resptags->name;
|
|
|
135 |
$resptags->class = 'select custom-select ' . $resptags->id;
|
|
|
136 |
$resptags->options = [];
|
|
|
137 |
$resptags->options[] = (object)['value' => '', 'label' => get_string('choosedots')];
|
|
|
138 |
|
|
|
139 |
if (!isset($response->answers[$this->id])) {
|
|
|
140 |
$response->answers[$this->id][] = new \mod_questionnaire\responsetype\answer\answer();
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
foreach ($this->choices as $id => $choice) {
|
|
|
144 |
$contents = questionnaire_choice_values($choice->content);
|
|
|
145 |
$chobj = new \stdClass();
|
|
|
146 |
$chobj->value = $id;
|
|
|
147 |
$chobj->label = format_text($contents->text, FORMAT_HTML, ['noclean' => true]);
|
|
|
148 |
if (isset($response->answers[$this->id][$id])) {
|
|
|
149 |
$chobj->selected = 1;
|
|
|
150 |
$resptags->selectedlabel = $chobj->label;
|
|
|
151 |
}
|
|
|
152 |
$resptags->options[] = $chobj;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
return $resptags;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* Return the length form element.
|
|
|
160 |
* @param \MoodleQuickForm $mform
|
|
|
161 |
* @param string $helpname
|
|
|
162 |
*/
|
|
|
163 |
protected function form_length(\MoodleQuickForm $mform, $helpname = '') {
|
|
|
164 |
return question::form_length_hidden($mform);
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* Return the precision form element.
|
|
|
169 |
* @param \MoodleQuickForm $mform
|
|
|
170 |
* @param string $helpname
|
|
|
171 |
*/
|
|
|
172 |
protected function form_precise(\MoodleQuickForm $mform, $helpname = '') {
|
|
|
173 |
return question::form_precise_hidden($mform);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* True if question provides mobile support.
|
|
|
178 |
* @return bool
|
|
|
179 |
*/
|
|
|
180 |
public function supports_mobile() {
|
|
|
181 |
return true;
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
/**
|
|
|
185 |
* Return the mobile question display.
|
|
|
186 |
* @param int $qnum
|
|
|
187 |
* @param bool $autonum
|
|
|
188 |
* @return \stdClass
|
|
|
189 |
*/
|
|
|
190 |
public function mobile_question_display($qnum, $autonum = false) {
|
|
|
191 |
$mobiledata = parent::mobile_question_display($qnum, $autonum);
|
|
|
192 |
$mobiledata->isselect = true;
|
|
|
193 |
return $mobiledata;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* Return the mobile response data.
|
|
|
198 |
* @param response $response
|
|
|
199 |
* @return array
|
|
|
200 |
*/
|
|
|
201 |
public function get_mobile_response_data($response) {
|
|
|
202 |
$resultdata = [];
|
|
|
203 |
if (isset($response->answers[$this->id])) {
|
|
|
204 |
foreach ($response->answers[$this->id] as $answer) {
|
|
|
205 |
// Add a fieldkey for each choice.
|
|
|
206 |
$resultdata[$this->mobile_fieldkey()] = $answer->choiceid;
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
return $resultdata;
|
|
|
210 |
}
|
|
|
211 |
}
|