-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuestionnaireAnswerDatetimeBehavior.php
More file actions
134 lines (127 loc) · 3.94 KB
/
QuestionnaireAnswerDatetimeBehavior.php
File metadata and controls
134 lines (127 loc) · 3.94 KB
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
<?php
/**
* QuestionnaireValidate Behavior
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Allcreator <info@allcreator.net>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('QuestionnaireAnswerBehavior', 'Questionnaires.Model/Behavior');
/**
* Datetime Behavior
*
* @package Questionnaires\Questionnaires\Model\Befavior\Answer
* @author Allcreator <info@allcreator.net>
*/
class QuestionnaireAnswerDatetimeBehavior extends QuestionnaireAnswerBehavior {
/**
* this answer type
*
* @var int
*/
protected $_myType = QuestionnairesComponent::TYPE_DATE_AND_TIME;
/**
* datetime validate check type
*
* @var array
*/
protected $_datetmValidateType = array(
QuestionnairesComponent::TYPE_DATE_AND_TIME
);
/**
* Answer datetime format
*
* @var array
*/
protected $_datetimeFormat = array(
QuestionnairesComponent::TYPE_OPTION_DATE => 'Y-m-d',
QuestionnairesComponent::TYPE_OPTION_TIME => 'H:i',
QuestionnairesComponent::TYPE_OPTION_DATE_TIME => 'Y-m-d H:i',
);
/**
* answerValidation 回答内容の正当性
*
* @param object &$model use model
* @param array $data Validation対象データ
* @param array $question 回答データに対応する質問
* @param array $allAnswers 入力された回答すべて
* @return bool
*/
public function answerDatetimeValidation(&$model, $data, $question, $allAnswers) {
if (! in_array($question['question_type'], $this->_datetmValidateType)) {
return true;
}
$answer = $data['answer_value'];
$ret = true;
if ($question['is_require'] === true || $data['answer_value']) {
if (!$this->_validateDatetime($model, $question['question_type_option'], $answer)) {
$ret = false;
}
if (!$this->_validateTimeRange($model, $question, $answer)) {
$ret = false;
}
}
return $ret;
}
/**
* _validateDatetime 日付・時間の正当性
*
* @param object &$model use model
* @param int $questionTypeOption 時間・日付オプション
* @param string $answer 回答データ
* @return bool
*/
protected function _validateDatetime(&$model, $questionTypeOption, $answer) {
if ($questionTypeOption == QuestionnairesComponent::TYPE_OPTION_DATE) {
if (! Validation::date($answer, 'ymd')) {
$model->validationErrors['answer_value'][] =
__d('questionnaires', 'Please enter a valid date in YY-MM-DD format.');
return false;
}
} elseif ($questionTypeOption == QuestionnairesComponent::TYPE_OPTION_TIME) {
if (! Validation::time($answer)) {
$model->validationErrors['answer_value'][] =
__d('questionnaires', 'Please enter the time.');
return false;
}
} elseif ($questionTypeOption == QuestionnairesComponent::TYPE_OPTION_DATE_TIME) {
if (! Validation::datetime($answer, 'ymd')) {
$model->validationErrors['answer_value'][] =
__d('questionnaires', 'Please enter a valid date and time.');
return false;
}
} else {
$model->validationErrors['answer_value'][] =
__d('net_commons', 'Invalid request.');
return false;
}
return true;
}
/**
* _validateDatetime 日付・時間の正当性
*
* @param object &$model use model
* @param array $question 回答データに対応する質問
* @param string $answer 回答データ
* @return bool
*/
protected function _validateTimeRange(&$model, $question, $answer) {
if ($question['is_range'] != QuestionnairesComponent::USES_USE) {
return true;
}
$rangeResult = Validation::range(
strtotime($answer),
strtotime($question['min']) - 1,
strtotime($question['max']) + 1);
if ($rangeResult) {
return true;
}
$model->validationErrors['answer_value'][] = __d('questionnaires',
'Please enter the answer between %s and %s.',
date($this->_datetimeFormat[$question['question_type_option']], strtotime($question['min'])),
date($this->_datetimeFormat[$question['question_type_option']], strtotime($question['max'])));
return false;
}
}