1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace common\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use \common\interfaces\QuestionInterface; |
7
|
|
|
use \common\components\ActiveRecord; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @property integer $id |
11
|
|
|
* @property integer $user_id |
12
|
|
|
* @property integer $behavior_id |
13
|
|
|
* @property integer $category_id |
14
|
|
|
* @property integer $user_behavior_id |
15
|
|
|
* @property integer $question |
16
|
|
|
* @property string $answer |
17
|
|
|
* @property string $date |
18
|
|
|
* |
19
|
|
|
* @property User $user |
20
|
|
|
* @property UserBehaviorLink $userBehavior |
21
|
|
|
*/ |
22
|
|
|
class Question extends ActiveRecord implements QuestionInterface |
23
|
|
|
{ |
24
|
|
|
public static $TYPES = [ |
25
|
|
|
'a' => 1, |
26
|
|
|
'b' => 2, |
27
|
|
|
'c' => 3 |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
public static $QUESTIONS = [ |
31
|
|
|
1 => "How does it affect me? How do I act and feel?", |
32
|
|
|
2 => "How does it affect the important people in my life?", |
33
|
|
|
3 => "Why do I do this? What is the benefit for me?" |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
public $question_text; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
* @codeCoverageIgnore |
41
|
|
|
*/ |
42
|
|
|
public static function tableName() |
43
|
|
|
{ |
44
|
|
|
return 'question'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
* @codeCoverageIgnore |
50
|
|
|
*/ |
51
|
|
|
public function rules() |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
|
|
[['user_id', 'category_id', 'user_behavior_id', 'question', 'answer', 'date'], 'required'], |
55
|
|
|
[['user_id', 'behavior_id', 'category_id', 'user_behavior_id', 'question'], 'integer'], |
56
|
|
|
['behavior_id', 'in', 'range' => array_column(\common\models\Behavior::$behaviors, 'id')], |
57
|
|
|
['category_id', 'in', 'range' => array_column(\common\models\Category::$categories, 'id')], |
58
|
|
|
['answer', 'string'], |
59
|
|
|
['date', 'safe'] |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
* @codeCoverageIgnore |
66
|
|
|
*/ |
67
|
|
|
public function attributeLabels() |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
'id' => 'ID', |
71
|
|
|
'user_id' => 'User ID', |
72
|
|
|
'behavior_id' => 'Behavior ID', |
73
|
|
|
'category_id' => 'Category ID', |
74
|
|
|
'user_behavior_id' => 'User Behavior ID', |
75
|
|
|
'question' => 'Question', |
76
|
|
|
'answer' => 'Answer', |
77
|
|
|
'date' => 'Date', |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function afterFind() |
82
|
|
|
{ |
83
|
|
|
$this->question_text = self::$QUESTIONS[$this->question]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return \yii\db\ActiveQuery |
88
|
|
|
* @codeCoverageIgnore |
89
|
|
|
*/ |
90
|
|
|
public function getUser() |
91
|
|
|
{ |
92
|
|
|
return $this->hasOne(\common\models\User::class, ['id' => 'user_id']); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return \yii\db\ActiveQuery |
97
|
|
|
* @codeCoverageIgnore |
98
|
|
|
*/ |
99
|
|
|
public function getUserBehavior() |
100
|
|
|
{ |
101
|
|
|
return $this->hasOne(\common\models\UserBehavior::class, ['id' => 'user_behavior_id']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getByUser(int $user_id, $local_date = null) { |
|
|
|
|
105
|
|
|
$time = Yii::$container->get(\common\interfaces\TimeInterface::class); |
106
|
|
|
if(is_null($local_date)) $local_date = $time->getLocalDate(); |
107
|
|
|
list($start, $end) = $time->getUTCBookends($local_date); |
108
|
|
|
|
109
|
|
|
$questions = $this->find() |
110
|
|
|
->where("user_id=:user_id |
111
|
|
|
AND date > :start_date |
112
|
|
|
AND date < :end_date", |
113
|
|
|
[ |
114
|
|
|
"user_id" => Yii::$app->user->id, |
115
|
|
|
':start_date' => $start, |
116
|
|
|
":end_date" => $end |
117
|
|
|
]) |
118
|
|
|
->with('userBehavior') |
119
|
|
|
->all(); |
120
|
|
|
|
121
|
|
|
return $this->parseQuestionData($questions); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function parseQuestionData($questions) { |
125
|
|
|
if(!$questions) return []; |
126
|
|
|
|
127
|
|
|
$question_answers = []; |
128
|
|
|
foreach($questions as $question) { |
129
|
|
|
$user_behavior_id = $question->user_behavior_id; |
130
|
|
|
|
131
|
|
|
$behavior_name = $question->behavior_id |
132
|
|
|
// TODO: I think this is potentially a source of exceptions |
133
|
|
|
// do we check if the behavior_id is an expected value on |
134
|
|
|
// form submission? |
135
|
|
|
? \common\models\Behavior::getBehavior('id', $question->behavior_id)['name'] |
136
|
|
|
: $question->userBehavior->custom_behavior; |
137
|
|
|
|
138
|
|
|
$question_answers[$user_behavior_id]['question'] = [ |
139
|
|
|
"user_behavior_id" => $user_behavior_id, |
140
|
|
|
"behavior_name" => $behavior_name, |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
$question_answers[$user_behavior_id]["answers"][] = [ |
144
|
|
|
"title" => $this::$QUESTIONS[$question['question']], |
145
|
|
|
"answer" => $question['answer'] |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $question_answers; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.