forked from PoetOS/moodle-mod_questionnaire
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdate.php
234 lines (219 loc) · 7.94 KB
/
date.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_questionnaire\question;
/**
* This file contains the parent class for date question types.
*
* @author Mike Churchward
* @copyright 2016 onward Mike Churchward ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package mod_questionnaire
*/
class date extends question {
/**
* Return the responseclass used.
* @return string
*/
protected function responseclass() {
return '\\mod_questionnaire\\responsetype\\date';
}
/**
* Return the help name.
* @return string
*/
public function helpname() {
return 'date';
}
/**
* Override and return a form template if provided. Output of question_survey_display is iterpreted based on this.
* @return boolean | string
*/
public function question_template() {
return 'mod_questionnaire/question_date';
}
/**
* Override and return a form template if provided. Output of response_survey_display is iterpreted based on this.
* @return boolean | string
*/
public function response_template() {
return 'mod_questionnaire/response_date';
}
/**
* Return the context tags for the check question template.
* @param \mod_questionnaire\responsetype\response\response $response
* @param array $descendantsdata
* @param boolean $blankquestionnaire
* @param boolean $isprint if true, this content is in print blank page, vice versa.
* @return object The check question context tags.
*/
protected function question_survey_display($response, $descendantsdata, $blankquestionnaire=false, $isprint=false) {
// Date.
$questiontags = new \stdClass();
if (!empty($response->answers[$this->id])) {
$dateentered = $response->answers[$this->id][0]->value;
$setdate = $this->check_date_format($dateentered);
if (!$setdate) {
$msg = get_string('wrongdateformat', 'questionnaire', $dateentered);
$this->add_notification($msg);
} else {
$response->answers[$this->id][0]->value = $dateentered;
}
}
$choice = new \stdClass();
$choice->type = 'date'; // Using HTML5 date input.
$choice->onkeypress = 'return event.keyCode != 13;';
$choice->name = 'q'.$this->id;
$choice->value = (isset($response->answers[$this->id][0]->value) ? $response->answers[$this->id][0]->value : '');
$choice->isprint = $isprint;
$questiontags->qelements = new \stdClass();
$questiontags->qelements->choice = $choice;
return $questiontags;
}
/**
* Return the context tags for the check response template.
* @param \mod_questionnaire\responsetype\response\response $response
* @return object The check question response context tags.
*/
protected function response_survey_display($response) {
$resptags = new \stdClass();
if (isset($response->answers[$this->id])) {
$answer = reset($response->answers[$this->id]);
$resptags->content = $answer->value;
}
return $resptags;
}
/**
* Check question's form data for valid response. Override this is type has specific format requirements.
*
* @param \stdClass $responsedata The data entered into the response.
* @return boolean
*/
public function response_valid($responsedata) {
$responseval = false;
if (is_a($responsedata, 'mod_questionnaire\responsetype\response\response')) {
// If $responsedata is a response object, look through the answers.
if (isset($responsedata->answers[$this->id]) && !empty($responsedata->answers[$this->id])) {
$answer = $responsedata->answers[$this->id][0];
$responseval = $answer->value;
}
} else if (isset($responsedata->{'q'.$this->id})) {
$responseval = $responsedata->{'q' . $this->id};
}
if ($responseval !== false) {
$checkdateresult = true;
if ($responseval != '') {
$checkdateresult = $this->check_date_format($responseval);
}
return $checkdateresult;
} else {
return parent::response_valid($responsedata);
}
}
/**
* Return the form length.
* @param \MoodleQuickForm $mform
* @param string $helpname
* @return \MoodleQuickForm|void
*/
protected function form_length(\MoodleQuickForm $mform, $helpname = '') {
return question::form_length_hidden($mform);
}
/**
* Return the form precision.
* @param \MoodleQuickForm $mform
* @param string $helpname
* @return \MoodleQuickForm|void
*/
protected function form_precise(\MoodleQuickForm $mform, $helpname = '') {
return question::form_precise_hidden($mform);
}
/**
* Verify that the date provided is in the proper YYYY-MM-DD format.
* @param string $date
* @return bool
*/
public function check_date_format($date) {
$datepieces = explode('-', $date);
$return = true;
if (count($datepieces) != 3) {
$return = false;
} else {
foreach ($datepieces as $piece => $datepiece) {
if (!is_numeric($datepiece)) {
$return = false;
break;
}
switch ($piece) {
// Year check.
case 0:
if ((strlen($datepiece) != 4) || ($datepiece <= 0)) {
$return = false;
break 2;
}
break;
// Month check.
case 1:
if ((strlen($datepiece) != 2) || ((int)$datepiece < 1) || ((int)$datepiece > 12)) {
$return = false;
break 2;
}
break;
// Day check.
case 2:
if ((strlen($datepiece) != 2) || ((int)$datepiece < 1) || ((int)$datepiece > 31)) {
$return = false;
break 2;
}
break;
}
}
}
return $return;
}
/**
* True if question provides mobile support.
*
* @return bool
*/
public function supports_mobile() {
return true;
}
/**
* Does the question support mobile display.
* @param int $qnum
* @param bool $autonum
* @return \stdClass
*/
public function mobile_question_display($qnum, $autonum = false) {
$mobiledata = parent::mobile_question_display($qnum, $autonum);
$mobiledata->isdate = true;
return $mobiledata;
}
/**
* Does the question have mobile choices.
* @return mixed
*/
public function mobile_question_choices_display() {
$choices = [];
$choices[0] = new \stdClass();
$choices[0]->id = 0;
$choices[0]->choice_id = 0;
$choices[0]->question_id = $this->id;
$choices[0]->content = '';
$choices[0]->value = null;
return $choices;
}
}