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\responsetype;
|
|
|
18 |
|
|
|
19 |
use \html_writer;
|
|
|
20 |
use \html_table;
|
|
|
21 |
|
|
|
22 |
use mod_questionnaire\db\bulk_sql_config;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* This file contains the parent class for questionnaire response types.
|
|
|
26 |
*
|
|
|
27 |
* @author Mike Churchward
|
|
|
28 |
* @copyright 2016 onward Mike Churchward (mike.churchward@poetopensource.org)
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
30 |
* @package mod_questionnaire
|
|
|
31 |
*/
|
|
|
32 |
abstract class responsetype {
|
|
|
33 |
|
|
|
34 |
// Class properties.
|
|
|
35 |
/** @var \mod_questionnaire\question\question $question The question for this response. */
|
|
|
36 |
public $question;
|
|
|
37 |
|
|
|
38 |
/** @var int $responseid The id of the response this is for. */
|
|
|
39 |
public $responseid;
|
|
|
40 |
|
|
|
41 |
/** @var array $choices An array of \mod_questionnaire\responsetype\choice objects. */
|
|
|
42 |
public $choices;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* responsetype constructor.
|
|
|
46 |
* @param \mod_questionnaire\question\question $question
|
|
|
47 |
* @param int|null $responseid
|
|
|
48 |
* @param array $choices
|
|
|
49 |
*/
|
|
|
50 |
public function __construct(\mod_questionnaire\question\question $question, int $responseid = null, array $choices = []) {
|
|
|
51 |
$this->question = $question;
|
|
|
52 |
$this->responseid = $responseid;
|
|
|
53 |
$this->choices = $choices;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Provide the necessary response data table name. Should probably always be used with late static binding 'static::' form
|
|
|
58 |
* rather than 'self::' form to allow for class extending.
|
|
|
59 |
*
|
|
|
60 |
* @return string response table name.
|
|
|
61 |
*/
|
|
|
62 |
public static function response_table() {
|
|
|
63 |
return 'Must be implemented!';
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Return the known response tables. Should be replaced by a better management system eventually.
|
|
|
68 |
* @return array
|
|
|
69 |
*/
|
|
|
70 |
public static function all_response_tables() {
|
|
|
71 |
return ['questionnaire_response_bool', 'questionnaire_response_date', 'questionnaire_response_other',
|
|
|
72 |
'questionnaire_response_rank', 'questionnaire_response_text', 'questionnaire_resp_multiple',
|
|
|
73 |
'questionnaire_resp_single'];
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Provide an array of answer objects from web form data for the question.
|
|
|
78 |
*
|
|
|
79 |
* @param \stdClass $responsedata All of the responsedata as an object.
|
|
|
80 |
* @param \mod_questionnaire\question\question $question
|
|
|
81 |
* @return array \mod_questionnaire\responsetype\answer\answer An array of answer objects.
|
|
|
82 |
*/
|
|
|
83 |
abstract public static function answers_from_webform($responsedata, $question);
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Insert a provided response to the question.
|
|
|
87 |
*
|
|
|
88 |
* @param object $responsedata All of the responsedata as an object.
|
|
|
89 |
* @return int|bool - on error the subtype should call set_error and return false.
|
|
|
90 |
*/
|
|
|
91 |
abstract public function insert_response($responsedata);
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Provide the result information for the specified result records.
|
|
|
95 |
*
|
|
|
96 |
* @param int|array $rids - A single response id, or array.
|
|
|
97 |
* @param boolean $anonymous - Whether or not responses are anonymous.
|
|
|
98 |
* @return array - Array of data records.
|
|
|
99 |
*/
|
|
|
100 |
abstract public function get_results($rids=false, $anonymous=false);
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Provide the result information for the specified result records.
|
|
|
104 |
*
|
|
|
105 |
* @param int|array $rids - A single response id, or array.
|
|
|
106 |
* @param string $sort - Optional display sort.
|
|
|
107 |
* @param boolean $anonymous - Whether or not responses are anonymous.
|
|
|
108 |
* @return string - Display output.
|
|
|
109 |
*/
|
|
|
110 |
abstract public function display_results($rids=false, $sort='', $anonymous=false);
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* If the choice id needs to be transformed into a different value, override this in the child class.
|
|
|
114 |
* @param mixed $choiceid
|
|
|
115 |
* @return mixed
|
|
|
116 |
*/
|
|
|
117 |
public function transform_choiceid($choiceid) {
|
|
|
118 |
return $choiceid;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Provide a template for results screen if defined.
|
|
|
123 |
* @param bool $pdf
|
|
|
124 |
* @return mixed The template string or false.
|
|
|
125 |
*/
|
|
|
126 |
public function results_template($pdf = false) {
|
|
|
127 |
return false;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Gets the results tags for templates for questions with defined choices (single, multiple, boolean).
|
|
|
132 |
*
|
|
|
133 |
* @param array $weights
|
|
|
134 |
* @param int $participants Number of questionnaire participants.
|
|
|
135 |
* @param int $respondents Number of question respondents.
|
|
|
136 |
* @param int $showtotals
|
|
|
137 |
* @param string $sort
|
|
|
138 |
* @return \stdClass
|
|
|
139 |
*/
|
|
|
140 |
public function get_results_tags($weights, $participants, $respondents, $showtotals = 1, $sort = '') {
|
|
|
141 |
global $CFG;
|
|
|
142 |
|
|
|
143 |
$pagetags = new \stdClass();
|
|
|
144 |
$precision = 0;
|
|
|
145 |
$alt = '';
|
|
|
146 |
$imageurl = $CFG->wwwroot.'/mod/questionnaire/images/';
|
|
|
147 |
|
|
|
148 |
if (!empty($weights) && is_array($weights)) {
|
|
|
149 |
$pos = 0;
|
|
|
150 |
switch ($sort) {
|
|
|
151 |
case 'ascending':
|
|
|
152 |
asort($weights);
|
|
|
153 |
break;
|
|
|
154 |
case 'descending':
|
|
|
155 |
arsort($weights);
|
|
|
156 |
break;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
reset ($weights);
|
|
|
160 |
$pagetags->responses = [];
|
|
|
161 |
$evencolor = false;
|
|
|
162 |
foreach ($weights as $content => $num) {
|
|
|
163 |
$response = new \stdClass();
|
|
|
164 |
$response->text = format_text($content, FORMAT_HTML, ['noclean' => true]);
|
|
|
165 |
if ($num > 0) {
|
|
|
166 |
$percent = round((float)$num / (float)$respondents * 100.0);
|
|
|
167 |
} else {
|
|
|
168 |
$percent = 0;
|
|
|
169 |
}
|
|
|
170 |
if ($percent > 100) {
|
|
|
171 |
$percent = 100;
|
|
|
172 |
}
|
|
|
173 |
if ($num) {
|
|
|
174 |
if (!right_to_left()) {
|
|
|
175 |
$response->alt1 = $alt;
|
|
|
176 |
$response->image1 = $imageurl . 'hbar_l.gif';
|
|
|
177 |
$response->alt3 = $alt;
|
|
|
178 |
$response->image3 = $imageurl . 'hbar_r.gif';
|
|
|
179 |
} else {
|
|
|
180 |
$response->alt1 = $alt;
|
|
|
181 |
$response->image1 = $imageurl . 'hbar_r.gif';
|
|
|
182 |
$response->alt3 = $alt;
|
|
|
183 |
$response->image3 = $imageurl . 'hbar_l.gif';
|
|
|
184 |
}
|
|
|
185 |
$response->alt2 = $alt;
|
|
|
186 |
$response->width2 = $percent * 1.4;
|
|
|
187 |
$response->image2 = $imageurl . 'hbar.gif';
|
|
|
188 |
$response->percent = sprintf(' %.'.$precision.'f%%', $percent);
|
|
|
189 |
}
|
|
|
190 |
$response->total = $num;
|
|
|
191 |
// The 'evencolor' attribute is used by the PDF template.
|
|
|
192 |
$response->evencolor = $evencolor;
|
|
|
193 |
$pagetags->responses[] = (object)['response' => $response];
|
|
|
194 |
$pos++;
|
|
|
195 |
$evencolor = !$evencolor;
|
|
|
196 |
} // End while.
|
|
|
197 |
|
|
|
198 |
if ($showtotals) {
|
|
|
199 |
$pagetags->total = new \stdClass();
|
|
|
200 |
if ($respondents > 0) {
|
|
|
201 |
$percent = round((float)$respondents / (float)$participants * 100.0);
|
|
|
202 |
} else {
|
|
|
203 |
$percent = 0;
|
|
|
204 |
}
|
|
|
205 |
if ($percent > 100) {
|
|
|
206 |
$percent = 100;
|
|
|
207 |
}
|
|
|
208 |
if (!right_to_left()) {
|
|
|
209 |
$pagetags->total->alt1 = $alt;
|
|
|
210 |
$pagetags->total->image1 = $imageurl . 'thbar_l.gif';
|
|
|
211 |
$pagetags->total->alt3 = $alt;
|
|
|
212 |
$pagetags->total->image3 = $imageurl . 'thbar_r.gif';
|
|
|
213 |
} else {
|
|
|
214 |
$pagetags->total->alt1 = $alt;
|
|
|
215 |
$pagetags->total->image1 = $imageurl . 'thbar_r.gif';
|
|
|
216 |
$pagetags->total->alt3 = $alt;
|
|
|
217 |
$pagetags->total->image3 = $imageurl . 'thbar_l.gif';
|
|
|
218 |
}
|
|
|
219 |
$pagetags->total->alt2 = $alt;
|
|
|
220 |
$pagetags->total->width2 = $percent * 1.4;
|
|
|
221 |
$pagetags->total->image2 = $imageurl . 'thbar.gif';
|
|
|
222 |
$pagetags->total->percent = sprintf(' %.'.$precision.'f%%', $percent);
|
|
|
223 |
$pagetags->total->total = "$respondents/$participants";
|
|
|
224 |
$pagetags->total->evencolor = $evencolor;
|
|
|
225 |
}
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
return $pagetags;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
/**
|
|
|
232 |
* Provide the feedback scores for all requested response id's. This should be provided only by questions that provide feedback.
|
|
|
233 |
* @param array $rids
|
|
|
234 |
* @return array | boolean
|
|
|
235 |
*/
|
|
|
236 |
public function get_feedback_scores(array $rids) {
|
|
|
237 |
return false;
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
/**
|
|
|
241 |
* Return an array of answers by question/choice for the given response. Must be implemented by the subclass.
|
|
|
242 |
*
|
|
|
243 |
* @param int $rid The response id.
|
|
|
244 |
* @return array
|
|
|
245 |
*/
|
|
|
246 |
public static function response_select($rid) {
|
|
|
247 |
return [];
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
/**
|
|
|
251 |
* Return an array of answer objects by question for the given response id.
|
|
|
252 |
* THIS SHOULD REPLACE response_select.
|
|
|
253 |
*
|
|
|
254 |
* @param int $rid The response id.
|
|
|
255 |
* @return array array answer
|
|
|
256 |
*/
|
|
|
257 |
public static function response_answers_by_question($rid) {
|
|
|
258 |
return [];
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
/**
|
|
|
262 |
* Provide an array of answer objects from mobile data for the question.
|
|
|
263 |
*
|
|
|
264 |
* @param \stdClass $responsedata All of the responsedata as an object.
|
|
|
265 |
* @param \mod_questionnaire\question\question $question
|
|
|
266 |
* @return array \mod_questionnaire\responsetype\answer\answer An array of answer objects.
|
|
|
267 |
*/
|
|
|
268 |
public static function answers_from_appdata($responsedata, $question) {
|
|
|
269 |
// In most cases this can be a direct call to answers_from_webform with the one modification below. Override when this will
|
|
|
270 |
// not work.
|
|
|
271 |
if (isset($responsedata->{'q'.$question->id}) && !empty($responsedata->{'q'.$question->id})) {
|
|
|
272 |
$responsedata->{'q'.$question->id} = $responsedata->{'q'.$question->id}[0];
|
|
|
273 |
}
|
|
|
274 |
return static::answers_from_webform($responsedata, $question);
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
/**
|
|
|
278 |
* Return all the fields to be used for users in bulk questionnaire sql.
|
|
|
279 |
*
|
|
|
280 |
* @return string
|
|
|
281 |
* author: Guy Thomas
|
|
|
282 |
*/
|
|
|
283 |
protected function user_fields_sql() {
|
|
|
284 |
if (class_exists('\core_user\fields')) {
|
|
|
285 |
$userfieldsarr = \core_user\fields::get_name_fields();
|
|
|
286 |
} else {
|
|
|
287 |
$userfieldsarr = get_all_user_name_fields();
|
|
|
288 |
}
|
|
|
289 |
$userfieldsarr = array_merge($userfieldsarr, ['username', 'department', 'institution']);
|
|
|
290 |
$userfields = '';
|
|
|
291 |
foreach ($userfieldsarr as $field) {
|
|
|
292 |
$userfields .= $userfields === '' ? '' : ', ';
|
|
|
293 |
$userfields .= 'u.'.$field;
|
|
|
294 |
}
|
|
|
295 |
$userfields .= ', u.id as usrid';
|
|
|
296 |
return $userfields;
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
/**
|
|
|
300 |
* Return sql and params for getting responses in bulk.
|
|
|
301 |
* @param int|array $questionnaireids One id, or an array of ids.
|
|
|
302 |
* @param bool|int $responseid
|
|
|
303 |
* @param bool|int $userid
|
|
|
304 |
* @param bool|int $groupid
|
|
|
305 |
* @param int $showincompletes
|
|
|
306 |
* @return array
|
|
|
307 |
* author Guy Thomas
|
|
|
308 |
*/
|
|
|
309 |
public function get_bulk_sql($questionnaireids, $responseid = false, $userid = false, $groupid = false, $showincompletes = 0) {
|
|
|
310 |
global $DB;
|
|
|
311 |
|
|
|
312 |
$sql = $this->bulk_sql();
|
|
|
313 |
if (($groupid !== false) && ($groupid > 0)) {
|
|
|
314 |
$groupsql = ' INNER JOIN {groups_members} gm ON gm.groupid = ? AND gm.userid = qr.userid ';
|
|
|
315 |
$gparams = [$groupid];
|
|
|
316 |
} else {
|
|
|
317 |
$groupsql = '';
|
|
|
318 |
$gparams = [];
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
if (is_array($questionnaireids)) {
|
|
|
322 |
list($qsql, $params) = $DB->get_in_or_equal($questionnaireids);
|
|
|
323 |
} else {
|
|
|
324 |
$qsql = ' = ? ';
|
|
|
325 |
$params = [$questionnaireids];
|
|
|
326 |
}
|
|
|
327 |
if ($showincompletes == 1) {
|
|
|
328 |
$showcompleteonly = '';
|
|
|
329 |
} else {
|
|
|
330 |
$showcompleteonly = 'AND qr.complete = ? ';
|
|
|
331 |
$params[] = 'y';
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
$sql .= "
|
|
|
335 |
AND qr.questionnaireid $qsql $showcompleteonly
|
|
|
336 |
LEFT JOIN {user} u ON u.id = qr.userid
|
|
|
337 |
$groupsql
|
|
|
338 |
";
|
|
|
339 |
$params = array_merge($params, $gparams);
|
|
|
340 |
|
|
|
341 |
if ($responseid) {
|
|
|
342 |
$sql .= " WHERE qr.id = ?";
|
|
|
343 |
$params[] = $responseid;
|
|
|
344 |
} else if ($userid) {
|
|
|
345 |
$sql .= " WHERE qr.userid = ?";
|
|
|
346 |
$params[] = $userid;
|
|
|
347 |
}
|
|
|
348 |
|
|
|
349 |
return [$sql, $params];
|
|
|
350 |
}
|
|
|
351 |
|
|
|
352 |
/**
|
|
|
353 |
* Configure bulk sql
|
|
|
354 |
* @return bulk_sql_config
|
|
|
355 |
*/
|
|
|
356 |
protected function bulk_sql_config() {
|
|
|
357 |
return new bulk_sql_config('questionnaire_response_other', 'qro', true, true, false);
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
/**
|
|
|
361 |
* Return sql for getting responses in bulk.
|
|
|
362 |
* @author Guy Thomas
|
|
|
363 |
* @return string
|
|
|
364 |
*/
|
|
|
365 |
protected function bulk_sql() {
|
|
|
366 |
global $DB;
|
|
|
367 |
$userfields = $this->user_fields_sql();
|
|
|
368 |
|
|
|
369 |
$config = $this->bulk_sql_config();
|
|
|
370 |
$alias = $config->tablealias;
|
|
|
371 |
|
|
|
372 |
$extraselectfields = $config->get_extra_select();
|
|
|
373 |
$extraselect = '';
|
|
|
374 |
foreach ($extraselectfields as $field => $include) {
|
|
|
375 |
$extraselect .= $extraselect === '' ? '' : ', ';
|
|
|
376 |
if ($include) {
|
|
|
377 |
// The 'response' field can be varchar or text, which doesn't work for all DB's (Oracle).
|
|
|
378 |
// So convert the text if needed.
|
|
|
379 |
if ($field === 'response') {
|
|
|
380 |
$extraselect .= $DB->sql_order_by_text($alias . '.' . $field, 1000).' AS '.$field;
|
|
|
381 |
} else {
|
|
|
382 |
$extraselect .= $alias . '.' . $field;
|
|
|
383 |
}
|
|
|
384 |
} else {
|
|
|
385 |
$default = $field === 'response' ? 'null' : 0;
|
|
|
386 |
$extraselect .= $default.' AS ' . $field;
|
|
|
387 |
}
|
|
|
388 |
}
|
|
|
389 |
|
|
|
390 |
return "
|
|
|
391 |
SELECT " . $DB->sql_concat_join("'_'", ['qr.id', "'".$this->question->helpname()."'", $alias.'.id']) . " AS id,
|
|
|
392 |
qr.submitted, qr.complete, qr.grade, qr.userid, $userfields, qr.id AS rid, $alias.question_id,
|
|
|
393 |
$extraselect
|
|
|
394 |
FROM {questionnaire_response} qr
|
|
|
395 |
JOIN {".$config->table."} $alias
|
|
|
396 |
ON $alias.response_id = qr.id
|
|
|
397 |
";
|
|
|
398 |
}
|
|
|
399 |
|
|
|
400 |
}
|