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 |
|
|
|
19 |
/**
|
|
|
20 |
* This file contains the parent class for date question 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 date extends question {
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Return the responseclass used.
|
|
|
31 |
* @return string
|
|
|
32 |
*/
|
|
|
33 |
protected function responseclass() {
|
|
|
34 |
return '\\mod_questionnaire\\responsetype\\date';
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Return the help name.
|
|
|
39 |
* @return string
|
|
|
40 |
*/
|
|
|
41 |
public function helpname() {
|
|
|
42 |
return 'date';
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Override and return a form template if provided. Output of question_survey_display is iterpreted based on this.
|
|
|
47 |
* @return boolean | string
|
|
|
48 |
*/
|
|
|
49 |
public function question_template() {
|
|
|
50 |
return 'mod_questionnaire/question_date';
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Override and return a form template if provided. Output of response_survey_display is iterpreted based on this.
|
|
|
55 |
* @return boolean | string
|
|
|
56 |
*/
|
|
|
57 |
public function response_template() {
|
|
|
58 |
return 'mod_questionnaire/response_date';
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Return the context tags for the check question template.
|
|
|
63 |
* @param \mod_questionnaire\responsetype\response\response $response
|
|
|
64 |
* @param array $descendantsdata
|
|
|
65 |
* @param boolean $blankquestionnaire
|
|
|
66 |
* @return object The check question context tags.
|
|
|
67 |
*/
|
|
|
68 |
protected function question_survey_display($response, $descendantsdata, $blankquestionnaire=false) {
|
|
|
69 |
// Date.
|
|
|
70 |
$questiontags = new \stdClass();
|
|
|
71 |
if (!empty($response->answers[$this->id])) {
|
|
|
72 |
$dateentered = $response->answers[$this->id][0]->value;
|
|
|
73 |
$setdate = $this->check_date_format($dateentered);
|
|
|
74 |
if (!$setdate) {
|
|
|
75 |
$msg = get_string('wrongdateformat', 'questionnaire', $dateentered);
|
|
|
76 |
$this->add_notification($msg);
|
|
|
77 |
} else {
|
|
|
78 |
$response->answers[$this->id][0]->value = $dateentered;
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
$choice = new \stdClass();
|
|
|
82 |
$choice->type = 'date'; // Using HTML5 date input.
|
|
|
83 |
$choice->onkeypress = 'return event.keyCode != 13;';
|
|
|
84 |
$choice->name = 'q'.$this->id;
|
|
|
85 |
$choice->value = (isset($response->answers[$this->id][0]->value) ? $response->answers[$this->id][0]->value : '');
|
|
|
86 |
$questiontags->qelements = new \stdClass();
|
|
|
87 |
$questiontags->qelements->choice = $choice;
|
|
|
88 |
return $questiontags;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Return the context tags for the check response template.
|
|
|
93 |
* @param \mod_questionnaire\responsetype\response\response $response
|
|
|
94 |
* @return object The check question response context tags.
|
|
|
95 |
*/
|
|
|
96 |
protected function response_survey_display($response) {
|
|
|
97 |
$resptags = new \stdClass();
|
|
|
98 |
if (isset($response->answers[$this->id])) {
|
|
|
99 |
$answer = reset($response->answers[$this->id]);
|
|
|
100 |
$resptags->content = $answer->value;
|
|
|
101 |
}
|
|
|
102 |
return $resptags;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Check question's form data for valid response. Override this is type has specific format requirements.
|
|
|
107 |
*
|
|
|
108 |
* @param \stdClass $responsedata The data entered into the response.
|
|
|
109 |
* @return boolean
|
|
|
110 |
*/
|
|
|
111 |
public function response_valid($responsedata) {
|
|
|
112 |
$responseval = false;
|
|
|
113 |
if (is_a($responsedata, 'mod_questionnaire\responsetype\response\response')) {
|
|
|
114 |
// If $responsedata is a response object, look through the answers.
|
|
|
115 |
if (isset($responsedata->answers[$this->id]) && !empty($responsedata->answers[$this->id])) {
|
|
|
116 |
$answer = $responsedata->answers[$this->id][0];
|
|
|
117 |
$responseval = $answer->value;
|
|
|
118 |
}
|
|
|
119 |
} else if (isset($responsedata->{'q'.$this->id})) {
|
|
|
120 |
$responseval = $responsedata->{'q' . $this->id};
|
|
|
121 |
}
|
|
|
122 |
if ($responseval !== false) {
|
|
|
123 |
$checkdateresult = true;
|
|
|
124 |
if ($responseval != '') {
|
|
|
125 |
$checkdateresult = $this->check_date_format($responseval);
|
|
|
126 |
}
|
|
|
127 |
return $checkdateresult;
|
|
|
128 |
} else {
|
|
|
129 |
return parent::response_valid($responsedata);
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Return the form length.
|
|
|
135 |
* @param \MoodleQuickForm $mform
|
|
|
136 |
* @param string $helpname
|
|
|
137 |
* @return \MoodleQuickForm|void
|
|
|
138 |
*/
|
|
|
139 |
protected function form_length(\MoodleQuickForm $mform, $helpname = '') {
|
|
|
140 |
return question::form_length_hidden($mform);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Return the form precision.
|
|
|
145 |
* @param \MoodleQuickForm $mform
|
|
|
146 |
* @param string $helpname
|
|
|
147 |
* @return \MoodleQuickForm|void
|
|
|
148 |
*/
|
|
|
149 |
protected function form_precise(\MoodleQuickForm $mform, $helpname = '') {
|
|
|
150 |
return question::form_precise_hidden($mform);
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* Verify that the date provided is in the proper YYYY-MM-DD format.
|
|
|
155 |
* @param string $date
|
|
|
156 |
* @return bool
|
|
|
157 |
*/
|
|
|
158 |
public function check_date_format($date) {
|
|
|
159 |
$datepieces = explode('-', $date);
|
|
|
160 |
$return = true;
|
|
|
161 |
if (count($datepieces) != 3) {
|
|
|
162 |
$return = false;
|
|
|
163 |
} else {
|
|
|
164 |
foreach ($datepieces as $piece => $datepiece) {
|
|
|
165 |
if (!is_numeric($datepiece)) {
|
|
|
166 |
$return = false;
|
|
|
167 |
break;
|
|
|
168 |
}
|
|
|
169 |
switch ($piece) {
|
|
|
170 |
// Year check.
|
|
|
171 |
case 0:
|
|
|
172 |
if ((strlen($datepiece) != 4) || ($datepiece <= 0)) {
|
|
|
173 |
$return = false;
|
|
|
174 |
break 2;
|
|
|
175 |
}
|
|
|
176 |
break;
|
|
|
177 |
// Month check.
|
|
|
178 |
case 1:
|
|
|
179 |
if ((strlen($datepiece) != 2) || ((int)$datepiece < 1) || ((int)$datepiece > 12)) {
|
|
|
180 |
$return = false;
|
|
|
181 |
break 2;
|
|
|
182 |
}
|
|
|
183 |
break;
|
|
|
184 |
// Day check.
|
|
|
185 |
case 2:
|
|
|
186 |
if ((strlen($datepiece) != 2) || ((int)$datepiece < 1) || ((int)$datepiece > 31)) {
|
|
|
187 |
$return = false;
|
|
|
188 |
break 2;
|
|
|
189 |
}
|
|
|
190 |
break;
|
|
|
191 |
}
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
return $return;
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
/**
|
|
|
198 |
* True if question provides mobile support.
|
|
|
199 |
*
|
|
|
200 |
* @return bool
|
|
|
201 |
*/
|
|
|
202 |
public function supports_mobile() {
|
|
|
203 |
return true;
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* Does the question support mobile display.
|
|
|
208 |
* @param int $qnum
|
|
|
209 |
* @param bool $autonum
|
|
|
210 |
* @return \stdClass
|
|
|
211 |
*/
|
|
|
212 |
public function mobile_question_display($qnum, $autonum = false) {
|
|
|
213 |
$mobiledata = parent::mobile_question_display($qnum, $autonum);
|
|
|
214 |
$mobiledata->isdate = true;
|
|
|
215 |
return $mobiledata;
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
/**
|
|
|
219 |
* Does the question have mobile choices.
|
|
|
220 |
* @return mixed
|
|
|
221 |
*/
|
|
|
222 |
public function mobile_question_choices_display() {
|
|
|
223 |
$choices = [];
|
|
|
224 |
$choices[0] = new \stdClass();
|
|
|
225 |
$choices[0]->id = 0;
|
|
|
226 |
$choices[0]->choice_id = 0;
|
|
|
227 |
$choices[0]->question_id = $this->id;
|
|
|
228 |
$choices[0]->content = '';
|
|
|
229 |
$choices[0]->value = null;
|
|
|
230 |
return $choices;
|
|
|
231 |
}
|
|
|
232 |
}
|