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 mod_questionnaire\db\bulk_sql_config;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Class for text response types.
|
|
|
23 |
* @author Mike Churchward
|
|
|
24 |
* @copyright 2016 onward Mike Churchward (mike.churchward@poetopensource.org)
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
26 |
* @package mod_questionnaire
|
|
|
27 |
*/
|
|
|
28 |
class text extends responsetype {
|
|
|
29 |
/**
|
|
|
30 |
* Provide the necessary response data table name. Should probably always be used with late static binding 'static::' form
|
|
|
31 |
* rather than 'self::' form to allow for class extending.
|
|
|
32 |
*
|
|
|
33 |
* @return string response table name.
|
|
|
34 |
*/
|
|
|
35 |
public static function response_table() {
|
|
|
36 |
return 'questionnaire_response_text';
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Provide an array of answer objects from web form data for the question.
|
|
|
41 |
*
|
|
|
42 |
* @param \stdClass $responsedata All of the responsedata as an object.
|
|
|
43 |
* @param \mod_questionnaire\question\question $question
|
|
|
44 |
* @return array \mod_questionnaire\responsetype\answer\answer An array of answer objects.
|
|
|
45 |
*/
|
|
|
46 |
public static function answers_from_webform($responsedata, $question) {
|
|
|
47 |
$answers = [];
|
|
|
48 |
if (isset($responsedata->{'q'.$question->id}) && (strlen($responsedata->{'q'.$question->id}) > 0)) {
|
|
|
49 |
$val = $responsedata->{'q' . $question->id};
|
|
|
50 |
$record = new \stdClass();
|
|
|
51 |
$record->responseid = $responsedata->rid;
|
|
|
52 |
$record->questionid = $question->id;
|
|
|
53 |
$record->value = $val;
|
|
|
54 |
$answers[] = answer\answer::create_from_data($record);
|
|
|
55 |
}
|
|
|
56 |
return $answers;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Insert a provided response to the question.
|
|
|
61 |
*
|
|
|
62 |
* @param object $responsedata All of the responsedata as an object.
|
|
|
63 |
* @return int|bool - on error the subtype should call set_error and return false.
|
|
|
64 |
*/
|
|
|
65 |
public function insert_response($responsedata) {
|
|
|
66 |
global $DB;
|
|
|
67 |
|
|
|
68 |
if (!$responsedata instanceof \mod_questionnaire\responsetype\response\response) {
|
|
|
69 |
$response = \mod_questionnaire\responsetype\response\response::response_from_webform($responsedata, [$this->question]);
|
|
|
70 |
} else {
|
|
|
71 |
$response = $responsedata;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
if (!empty($response) && isset($response->answers[$this->question->id][0])) {
|
|
|
75 |
$record = new \stdClass();
|
|
|
76 |
$record->response_id = $response->id;
|
|
|
77 |
$record->question_id = $this->question->id;
|
|
|
78 |
$record->response = clean_text($response->answers[$this->question->id][0]->value);
|
|
|
79 |
return $DB->insert_record(static::response_table(), $record);
|
|
|
80 |
} else {
|
|
|
81 |
return false;
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Provide the result information for the specified result records.
|
|
|
87 |
*
|
|
|
88 |
* @param int|array $rids - A single response id, or array.
|
|
|
89 |
* @param boolean $anonymous - Whether or not responses are anonymous.
|
|
|
90 |
* @return array - Array of data records.
|
|
|
91 |
*/
|
|
|
92 |
public function get_results($rids=false, $anonymous=false) {
|
|
|
93 |
global $DB;
|
|
|
94 |
|
|
|
95 |
$rsql = '';
|
|
|
96 |
if (!empty($rids)) {
|
|
|
97 |
list($rsql, $params) = $DB->get_in_or_equal($rids);
|
|
|
98 |
$rsql = ' AND response_id ' . $rsql;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
if ($anonymous) {
|
|
|
102 |
$sql = 'SELECT t.id, t.response, r.submitted AS submitted, ' .
|
|
|
103 |
'r.questionnaireid, r.id AS rid ' .
|
|
|
104 |
'FROM {'.static::response_table().'} t, ' .
|
|
|
105 |
'{questionnaire_response} r ' .
|
|
|
106 |
'WHERE question_id=' . $this->question->id . $rsql .
|
|
|
107 |
' AND t.response_id = r.id ' .
|
|
|
108 |
'ORDER BY r.submitted DESC';
|
|
|
109 |
} else {
|
|
|
110 |
$sql = 'SELECT t.id, t.response, r.submitted AS submitted, r.userid, u.username AS username, ' .
|
|
|
111 |
'u.id as usrid, ' .
|
|
|
112 |
'r.questionnaireid, r.id AS rid ' .
|
|
|
113 |
'FROM {'.static::response_table().'} t, ' .
|
|
|
114 |
'{questionnaire_response} r, ' .
|
|
|
115 |
'{user} u ' .
|
|
|
116 |
'WHERE question_id=' . $this->question->id . $rsql .
|
|
|
117 |
' AND t.response_id = r.id' .
|
|
|
118 |
' AND u.id = r.userid ' .
|
|
|
119 |
'ORDER BY u.lastname, u.firstname, r.submitted';
|
|
|
120 |
}
|
|
|
121 |
return $DB->get_records_sql($sql, $params);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Provide a template for results screen if defined.
|
|
|
126 |
* @param bool $pdf
|
|
|
127 |
* @return mixed The template string or false/
|
|
|
128 |
*/
|
|
|
129 |
public function results_template($pdf = false) {
|
|
|
130 |
if ($pdf) {
|
|
|
131 |
return 'mod_questionnaire/resultspdf_text';
|
|
|
132 |
} else {
|
|
|
133 |
return 'mod_questionnaire/results_text';
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* Provide the result information for the specified result records.
|
|
|
139 |
*
|
|
|
140 |
* @param int|array $rids - A single response id, or array.
|
|
|
141 |
* @param string $sort - Optional display sort.
|
|
|
142 |
* @param boolean $anonymous - Whether or not responses are anonymous.
|
|
|
143 |
* @return string - Display output.
|
|
|
144 |
*/
|
|
|
145 |
public function display_results($rids=false, $sort='', $anonymous=false) {
|
|
|
146 |
if (is_array($rids)) {
|
|
|
147 |
$prtotal = 1;
|
|
|
148 |
} else if (is_int($rids)) {
|
|
|
149 |
$prtotal = 0;
|
|
|
150 |
}
|
|
|
151 |
if ($rows = $this->get_results($rids, $anonymous)) {
|
|
|
152 |
$numrespondents = count($rids);
|
|
|
153 |
$numresponses = count($rows);
|
|
|
154 |
$pagetags = $this->get_results_tags($rows, $numrespondents, $numresponses, $prtotal);
|
|
|
155 |
} else {
|
|
|
156 |
$pagetags = new \stdClass();
|
|
|
157 |
}
|
|
|
158 |
return $pagetags;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Gets the results tags for templates for questions with defined choices (single, multiple, boolean).
|
|
|
163 |
*
|
|
|
164 |
* @param array $weights
|
|
|
165 |
* @param int $participants Number of questionnaire participants.
|
|
|
166 |
* @param int $respondents Number of question respondents.
|
|
|
167 |
* @param int $showtotals
|
|
|
168 |
* @param string $sort
|
|
|
169 |
* @return \stdClass
|
|
|
170 |
*/
|
|
|
171 |
public function get_results_tags($weights, $participants, $respondents, $showtotals = 1, $sort = '') {
|
|
|
172 |
$pagetags = new \stdClass();
|
|
|
173 |
if ($respondents == 0) {
|
|
|
174 |
return $pagetags;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
// If array element is an object, outputting non-numeric responses.
|
|
|
178 |
if (is_object(reset($weights))) {
|
|
|
179 |
global $CFG, $SESSION, $questionnaire, $DB;
|
|
|
180 |
$viewsingleresponse = $questionnaire->capabilities->viewsingleresponse;
|
|
|
181 |
$nonanonymous = $questionnaire->respondenttype != 'anonymous';
|
|
|
182 |
if ($viewsingleresponse && $nonanonymous) {
|
|
|
183 |
$currentgroupid = '';
|
|
|
184 |
if (isset($SESSION->questionnaire->currentgroupid)) {
|
|
|
185 |
$currentgroupid = $SESSION->questionnaire->currentgroupid;
|
|
|
186 |
}
|
|
|
187 |
$url = $CFG->wwwroot.'/mod/questionnaire/report.php?action=vresp&sid='.$questionnaire->survey->id.
|
|
|
188 |
'¤tgroupid='.$currentgroupid;
|
|
|
189 |
}
|
|
|
190 |
$users = [];
|
|
|
191 |
$evencolor = false;
|
|
|
192 |
foreach ($weights as $row) {
|
|
|
193 |
$response = new \stdClass();
|
|
|
194 |
$response->text = format_text($row->response, FORMAT_HTML);
|
|
|
195 |
if ($viewsingleresponse && $nonanonymous) {
|
|
|
196 |
$rurl = $url.'&rid='.$row->rid.'&individualresponse=1';
|
|
|
197 |
$title = userdate($row->submitted);
|
|
|
198 |
if (!isset($users[$row->userid])) {
|
|
|
199 |
$users[$row->userid] = $DB->get_record('user', ['id' => $row->userid]);
|
|
|
200 |
}
|
|
|
201 |
$response->respondent = '<a href="'.$rurl.'" title="'.$title.'">'.fullname($users[$row->userid]).'</a>';
|
|
|
202 |
} else {
|
|
|
203 |
$response->respondent = '';
|
|
|
204 |
}
|
|
|
205 |
// The 'evencolor' attribute is used by the PDF template.
|
|
|
206 |
$response->evencolor = $evencolor;
|
|
|
207 |
$pagetags->responses[] = (object)['response' => $response];
|
|
|
208 |
$evencolor = !$evencolor;
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
if ($showtotals == 1) {
|
|
|
212 |
$pagetags->total = new \stdClass();
|
|
|
213 |
$pagetags->total->total = "$respondents/$participants";
|
|
|
214 |
}
|
|
|
215 |
} else {
|
|
|
216 |
$nbresponses = 0;
|
|
|
217 |
$sum = 0;
|
|
|
218 |
$strtotal = get_string('totalofnumbers', 'questionnaire');
|
|
|
219 |
$straverage = get_string('average', 'questionnaire');
|
|
|
220 |
|
|
|
221 |
if (!empty($weights) && is_array($weights)) {
|
|
|
222 |
ksort($weights);
|
|
|
223 |
$evencolor = false;
|
|
|
224 |
foreach ($weights as $text => $num) {
|
|
|
225 |
$response = new \stdClass();
|
|
|
226 |
$response->text = $text;
|
|
|
227 |
$response->respondent = $num;
|
|
|
228 |
// The 'evencolor' attribute is used by the PDF template.
|
|
|
229 |
$response->evencolor = $evencolor;
|
|
|
230 |
$nbresponses += $num;
|
|
|
231 |
$sum += $text * $num;
|
|
|
232 |
$evencolor = !$evencolor;
|
|
|
233 |
$pagetags->responses[] = (object)['response' => $response];
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
$response = new \stdClass();
|
|
|
237 |
$response->text = $sum;
|
|
|
238 |
$response->respondent = $strtotal;
|
|
|
239 |
$response->evencolor = $evencolor;
|
|
|
240 |
$pagetags->responses[] = (object)['response' => $response];
|
|
|
241 |
$evencolor = !$evencolor;
|
|
|
242 |
|
|
|
243 |
$response = new \stdClass();
|
|
|
244 |
$response->respondent = $straverage;
|
|
|
245 |
$avg = $sum / $nbresponses;
|
|
|
246 |
$response->text = sprintf('%.' . $this->question->precise . 'f', $avg);
|
|
|
247 |
$response->evencolor = $evencolor;
|
|
|
248 |
$pagetags->responses[] = (object)['response' => $response];
|
|
|
249 |
$evencolor = !$evencolor;
|
|
|
250 |
|
|
|
251 |
if ($showtotals == 1) {
|
|
|
252 |
$pagetags->total = new \stdClass();
|
|
|
253 |
$pagetags->total->total = "$respondents/$participants";
|
|
|
254 |
$pagetags->total->evencolor = $evencolor;
|
|
|
255 |
}
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
return $pagetags;
|
|
|
260 |
}
|
|
|
261 |
|
|
|
262 |
/**
|
|
|
263 |
* Return an array of answers by question/choice for the given response. Must be implemented by the subclass.
|
|
|
264 |
*
|
|
|
265 |
* @param int $rid The response id.
|
|
|
266 |
* @return array
|
|
|
267 |
*/
|
|
|
268 |
public static function response_select($rid) {
|
|
|
269 |
global $DB;
|
|
|
270 |
|
|
|
271 |
$values = [];
|
|
|
272 |
$sql = 'SELECT q.id, q.content, a.response as aresponse '.
|
|
|
273 |
'FROM {'.static::response_table().'} a, {questionnaire_question} q '.
|
|
|
274 |
'WHERE a.response_id=? AND a.question_id=q.id ';
|
|
|
275 |
$records = $DB->get_records_sql($sql, [$rid]);
|
|
|
276 |
foreach ($records as $qid => $row) {
|
|
|
277 |
unset($row->id);
|
|
|
278 |
$row = (array)$row;
|
|
|
279 |
$newrow = [];
|
|
|
280 |
foreach ($row as $key => $val) {
|
|
|
281 |
if (!is_numeric($key)) {
|
|
|
282 |
$newrow[] = $val;
|
|
|
283 |
}
|
|
|
284 |
}
|
|
|
285 |
$values[$qid] = $newrow;
|
|
|
286 |
$val = array_pop($values[$qid]);
|
|
|
287 |
array_push($values[$qid], $val, $val);
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
return $values;
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
/**
|
|
|
294 |
* Return an array of answer objects by question for the given response id.
|
|
|
295 |
* THIS SHOULD REPLACE response_select.
|
|
|
296 |
*
|
|
|
297 |
* @param int $rid The response id.
|
|
|
298 |
* @return array array answer
|
|
|
299 |
* @throws \dml_exception
|
|
|
300 |
*/
|
|
|
301 |
public static function response_answers_by_question($rid) {
|
|
|
302 |
global $DB;
|
|
|
303 |
|
|
|
304 |
$answers = [];
|
|
|
305 |
$sql = 'SELECT id, response_id as responseid, question_id as questionid, 0 as choiceid, response as value ' .
|
|
|
306 |
'FROM {' . static::response_table() .'} ' .
|
|
|
307 |
'WHERE response_id = ? ';
|
|
|
308 |
$records = $DB->get_records_sql($sql, [$rid]);
|
|
|
309 |
foreach ($records as $record) {
|
|
|
310 |
$answers[$record->questionid][] = answer\answer::create_from_data($record);
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
return $answers;
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
/**
|
|
|
317 |
* Configure bulk sql
|
|
|
318 |
* @return bulk_sql_config
|
|
|
319 |
*/
|
|
|
320 |
protected function bulk_sql_config() {
|
|
|
321 |
return new bulk_sql_config(static::response_table(), 'qrt', false, true, false);
|
|
|
322 |
}
|
|
|
323 |
}
|
|
|
324 |
|