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\response;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* This defines a structured class to hold responses.
|
|
|
21 |
*
|
|
|
22 |
* @author Mike Churchward
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
24 |
* @package mod_questionnaire
|
|
|
25 |
* @copyright 2019, onwards Poet
|
|
|
26 |
*/
|
|
|
27 |
class response {
|
|
|
28 |
|
|
|
29 |
// Class properties.
|
|
|
30 |
|
|
|
31 |
/** @var int $id The id of the response this applies to. */
|
|
|
32 |
public $id;
|
|
|
33 |
|
|
|
34 |
/** @var int $questionnaireid The id of the questionnaire this response applies to. */
|
|
|
35 |
public $questionnaireid;
|
|
|
36 |
|
|
|
37 |
/** @var int $userid The id of the user for this response. */
|
|
|
38 |
public $userid;
|
|
|
39 |
|
|
|
40 |
/** @var int $submitted The most recent submission date of this response. */
|
|
|
41 |
public $submitted;
|
|
|
42 |
|
|
|
43 |
/** @var boolean $complete Flag for final submission of this response. */
|
|
|
44 |
public $complete;
|
|
|
45 |
|
|
|
46 |
/** @var int $grade Numeric grade for this response (if applicable). */
|
|
|
47 |
public $grade;
|
|
|
48 |
|
|
|
49 |
/** @var array $answers Array by question of array of answer objects. */
|
|
|
50 |
public $answers;
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Choice constructor.
|
|
|
54 |
* @param null $id
|
|
|
55 |
* @param null $questionnaireid
|
|
|
56 |
* @param null $userid
|
|
|
57 |
* @param null $submitted
|
|
|
58 |
* @param null $complete
|
|
|
59 |
* @param null $grade
|
|
|
60 |
* @param bool $addanswers
|
|
|
61 |
*/
|
|
|
62 |
public function __construct($id = null, $questionnaireid = null, $userid = null, $submitted = null, $complete = null,
|
|
|
63 |
$grade = null, $addanswers = true) {
|
|
|
64 |
$this->id = $id;
|
|
|
65 |
$this->questionnaireid = $questionnaireid;
|
|
|
66 |
$this->userid = $userid;
|
|
|
67 |
$this->submitted = $submitted;
|
|
|
68 |
$this->complete = $complete;
|
|
|
69 |
$this->grade = $grade;
|
|
|
70 |
|
|
|
71 |
// Add answers by questions that exist.
|
|
|
72 |
if ($addanswers) {
|
|
|
73 |
$this->add_questions_answers();
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Create and return a response object from data.
|
|
|
79 |
*
|
|
|
80 |
* @param \stdClass|array $responsedata The data to load.
|
|
|
81 |
* @return response
|
|
|
82 |
*/
|
|
|
83 |
public static function create_from_data($responsedata) {
|
|
|
84 |
if (!is_array($responsedata)) {
|
|
|
85 |
$responsedata = (array)$responsedata;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
$properties = array_keys(get_class_vars(__CLASS__));
|
|
|
89 |
foreach ($properties as $property) {
|
|
|
90 |
if (!isset($responsedata[$property])) {
|
|
|
91 |
$responsedata[$property] = null;
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
return new response($responsedata['id'], $responsedata['questionnaireid'], $responsedata['userid'],
|
|
|
96 |
$responsedata['submitted'], $responsedata['complete'], $responsedata['grade']);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Provide a response object from web form data to the question.
|
|
|
101 |
*
|
|
|
102 |
* @param \stdClass $responsedata All of the responsedata as an object.
|
|
|
103 |
* @param array $questions
|
|
|
104 |
* @return bool|response A response object.
|
|
|
105 |
*/
|
|
|
106 |
public static function response_from_webform($responsedata, $questions) {
|
|
|
107 |
global $USER;
|
|
|
108 |
|
|
|
109 |
$questionnaireid = isset($responsedata->questionnaire_id) ? $responsedata->questionnaire_id :
|
|
|
110 |
(isset($responsedata->a) ? $responsedata->a : 0);
|
|
|
111 |
$response = new response($responsedata->rid, $questionnaireid, $USER->id, null, null, null, false);
|
|
|
112 |
foreach ($questions as $question) {
|
|
|
113 |
if ($question->supports_responses()) {
|
|
|
114 |
$response->answers[$question->id] = $question->responsetype::answers_from_webform($responsedata, $question);
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
return $response;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Provide a response object from mobile app data to the question.
|
|
|
122 |
*
|
|
|
123 |
* @param id $questionnaireid
|
|
|
124 |
* @param id $responseid
|
|
|
125 |
* @param \stdClass $responsedata All of the responsedata as an object.
|
|
|
126 |
* @param array $questions Array of question objects.
|
|
|
127 |
* @return bool|response A response object.
|
|
|
128 |
*/
|
|
|
129 |
public static function response_from_appdata($questionnaireid, $responseid, $responsedata, $questions) {
|
|
|
130 |
global $USER;
|
|
|
131 |
|
|
|
132 |
$response = new response($responseid, $questionnaireid, $USER->id, null, null, null, false);
|
|
|
133 |
|
|
|
134 |
// Process app data by question and choice and create a webform structure.
|
|
|
135 |
$processedresponses = new \stdClass();
|
|
|
136 |
$processedresponses->rid = $responseid;
|
|
|
137 |
foreach ($responsedata as $answerid => $value) {
|
|
|
138 |
$parts = explode('_', $answerid);
|
|
|
139 |
if ($parts[0] == 'response') {
|
|
|
140 |
$qid = 'q' . $parts[2];
|
|
|
141 |
if (!isset($processedresponses->{$qid})) {
|
|
|
142 |
$processedresponses->{$qid} = [];
|
|
|
143 |
}
|
|
|
144 |
if (isset($parts[3])) {
|
|
|
145 |
$cid = $parts[3];
|
|
|
146 |
} else {
|
|
|
147 |
$cid = 0;
|
|
|
148 |
}
|
|
|
149 |
$processedresponses->{$qid}[$cid] = $value;
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
foreach ($questions as $question) {
|
|
|
154 |
if ($question->supports_responses() && isset($processedresponses->{'q'.$question->id})) {
|
|
|
155 |
$response->answers[$question->id] = $question->responsetype::answers_from_appdata($processedresponses, $question);
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
return $response;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Add the answers to each question in a question array of answers structure.
|
|
|
163 |
*/
|
|
|
164 |
public function add_questions_answers() {
|
|
|
165 |
$this->answers = [];
|
|
|
166 |
$this->answers += \mod_questionnaire\responsetype\multiple::response_answers_by_question($this->id);
|
|
|
167 |
$this->answers += \mod_questionnaire\responsetype\single::response_answers_by_question($this->id);
|
|
|
168 |
$this->answers += \mod_questionnaire\responsetype\rank::response_answers_by_question($this->id);
|
|
|
169 |
$this->answers += \mod_questionnaire\responsetype\boolean::response_answers_by_question($this->id);
|
|
|
170 |
$this->answers += \mod_questionnaire\responsetype\date::response_answers_by_question($this->id);
|
|
|
171 |
$this->answers += \mod_questionnaire\responsetype\text::response_answers_by_question($this->id);
|
|
|
172 |
}
|
|
|
173 |
}
|