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 |
/**
|
|
|
20 |
* Class for numerical text response 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 numericaltext extends text {
|
|
|
28 |
/**
|
|
|
29 |
* Provide an array of answer objects from web form data for the question.
|
|
|
30 |
*
|
|
|
31 |
* @param \stdClass $responsedata All of the responsedata as an object.
|
|
|
32 |
* @param \mod_questionnaire\question\question $question
|
|
|
33 |
* @return array \mod_questionnaire\responsetype\answer\answer An array of answer objects.
|
|
|
34 |
*/
|
|
|
35 |
public static function answers_from_webform($responsedata, $question) {
|
|
|
36 |
$answers = [];
|
|
|
37 |
if (isset($responsedata->{'q'.$question->id}) && is_numeric($responsedata->{'q'.$question->id})) {
|
|
|
38 |
$val = $responsedata->{'q' . $question->id};
|
|
|
39 |
// Allow commas as well as points in decimal numbers.
|
|
|
40 |
$val = str_replace(",", ".", $responsedata->{'q' . $question->id});
|
|
|
41 |
$val = preg_replace("/[^0-9.\-]*(-?[0-9]*\.?[0-9]*).*/", '\1', $val);
|
|
|
42 |
$record = new \stdClass();
|
|
|
43 |
$record->responseid = $responsedata->rid;
|
|
|
44 |
$record->questionid = $question->id;
|
|
|
45 |
$record->value = $val;
|
|
|
46 |
$answers[] = answer\answer::create_from_data($record);
|
|
|
47 |
}
|
|
|
48 |
return $answers;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Provide the result information for the specified result records.
|
|
|
53 |
*
|
|
|
54 |
* @param int|array $rids - A single response id, or array.
|
|
|
55 |
* @param string $sort - Optional display sort.
|
|
|
56 |
* @param boolean $anonymous - Whether or not responses are anonymous.
|
|
|
57 |
* @return string - Display output.
|
|
|
58 |
*/
|
|
|
59 |
public function display_results($rids=false, $sort='', $anonymous=false) {
|
|
|
60 |
if (is_array($rids)) {
|
|
|
61 |
$prtotal = 1;
|
|
|
62 |
} else if (is_int($rids)) {
|
|
|
63 |
$prtotal = 0;
|
|
|
64 |
}
|
|
|
65 |
if ($rows = $this->get_results($rids, $anonymous)) {
|
|
|
66 |
$numrespondents = count($rids);
|
|
|
67 |
$numresponses = count($rows);
|
|
|
68 |
// Count identical answers (numeric questions only).
|
|
|
69 |
$counts = [];
|
|
|
70 |
foreach ($rows as $row) {
|
|
|
71 |
if (!empty($row->response) || is_numeric($row->response)) {
|
|
|
72 |
$textidx = clean_text($row->response);
|
|
|
73 |
$counts[$textidx] = !empty($counts[$textidx]) ? ($counts[$textidx] + 1) : 1;
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
$pagetags = $this->get_results_tags($counts, $numrespondents, $numresponses, $prtotal);
|
|
|
77 |
} else {
|
|
|
78 |
$pagetags = new \stdClass();
|
|
|
79 |
}
|
|
|
80 |
return $pagetags;
|
|
|
81 |
}
|
|
|
82 |
}
|