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
|
|
|
*/ |
41
|
|
|
public static function tableName() |
42
|
|
|
{ |
43
|
|
|
return 'question'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
public function rules() |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
|
|
[['user_id', 'category_id', 'user_behavior_id', 'question', 'answer', 'date'], 'required'], |
53
|
|
|
[['user_id', 'behavior_id', 'category_id', 'user_behavior_id', 'question'], 'integer'], |
54
|
|
|
['behavior_id', 'in', 'range' => array_column(\common\models\Behavior::$behaviors, 'id')], |
55
|
|
|
['category_id', 'in', 'range' => array_column(\common\models\Category::$categories, 'id')], |
56
|
|
|
['answer', 'string'], |
57
|
|
|
['date', 'safe'] |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
|
|
public function attributeLabels() |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
'id' => 'ID', |
68
|
|
|
'user_id' => 'User ID', |
69
|
|
|
'behavior_id' => 'Behavior ID', |
70
|
|
|
'category_id' => 'Category ID', |
71
|
|
|
'user_behavior_id' => 'User Behavior ID', |
72
|
|
|
'question' => 'Question', |
73
|
|
|
'answer' => 'Answer', |
74
|
|
|
'date' => 'Date', |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function afterFind() |
79
|
|
|
{ |
80
|
|
|
$this->question_text = self::$QUESTIONS[$this->question]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return \yii\db\ActiveQuery |
85
|
|
|
*/ |
86
|
|
|
public function getUser() |
87
|
|
|
{ |
88
|
|
|
return $this->hasOne(\common\models\User::class, ['id' => 'user_id']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return \yii\db\ActiveQuery |
93
|
|
|
*/ |
94
|
|
|
public function getUserBehavior() |
95
|
|
|
{ |
96
|
|
|
return $this->hasOne(\common\models\UserBehavior::class, ['id' => 'user_behavior_id']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getByUser(int $user_id, $local_date = null) { |
|
|
|
|
100
|
|
|
$time = Yii::$container->get(\common\interfaces\TimeInterface::class); |
101
|
|
|
if(is_null($local_date)) $local_date = $time->getLocalDate(); |
102
|
|
|
list($start, $end) = $time->getUTCBookends($local_date); |
103
|
|
|
|
104
|
|
|
$questions = $this->find() |
105
|
|
|
->where("user_id=:user_id |
106
|
|
|
AND date > :start_date |
107
|
|
|
AND date < :end_date", |
108
|
|
|
[ |
109
|
|
|
"user_id" => Yii::$app->user->id, |
110
|
|
|
':start_date' => $start, |
111
|
|
|
":end_date" => $end |
112
|
|
|
]) |
113
|
|
|
->with('userBehavior') |
114
|
|
|
->all(); |
115
|
|
|
|
116
|
|
|
return $this->parseQuestionData($questions); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function parseQuestionData($questions) { |
120
|
|
|
if(!$questions) return []; |
121
|
|
|
|
122
|
|
|
$question_answers = []; |
123
|
|
|
foreach($questions as $question) { |
124
|
|
|
$user_behavior_id = $question->user_behavior_id; |
125
|
|
|
|
126
|
|
|
$behavior_name = $question->behavior_id |
127
|
|
|
// TODO: I think this is potentially a source of exceptions...do we check |
128
|
|
|
// if the behavior_id is an expected value on form submission? |
129
|
|
|
? Behavior::getBehavior('id', $question->behavior_id)['name'] |
130
|
|
|
: $question->userBehavior->custom_behavior; |
131
|
|
|
|
132
|
|
|
$question_answers[$user_behavior_id]['question'] = [ |
133
|
|
|
"user_behavior_id" => $user_behavior_id, |
134
|
|
|
"behavior_name" => $behavior_name, |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
$question_answers[$user_behavior_id]["answers"][] = [ |
138
|
|
|
"title" => $this::$QUESTIONS[$question['question']], |
139
|
|
|
"answer" => $question['answer'] |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $question_answers; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.