1
|
|
|
<?php |
2
|
|
|
namespace site\models; |
3
|
|
|
|
4
|
|
|
use Yii; |
5
|
|
|
use yii\base\Model; |
6
|
|
|
use yii\db\Expression; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Checkin form |
10
|
|
|
*/ |
11
|
|
|
class CheckinForm extends Model |
12
|
|
|
{ |
13
|
|
|
public $behaviors1; |
14
|
|
|
public $behaviors2; |
15
|
|
|
public $behaviors3; |
16
|
|
|
public $behaviors4; |
17
|
|
|
public $behaviors5; |
18
|
|
|
public $behaviors6; |
19
|
|
|
public $behaviors7; |
20
|
|
|
|
21
|
|
|
public $compiled_behaviors; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @inheritdoc |
25
|
|
|
*/ |
26
|
|
|
public function rules() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
[ |
30
|
|
|
[ |
31
|
|
|
'behaviors1', |
32
|
|
|
'behaviors2', |
33
|
|
|
'behaviors3', |
34
|
|
|
'behaviors4', |
35
|
|
|
'behaviors5', |
36
|
|
|
'behaviors6', |
37
|
|
|
'behaviors7' |
38
|
|
|
], |
39
|
|
|
'validateBehaviors'], |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function attributeLabels() { |
44
|
|
|
return [ |
45
|
|
|
'behaviors1' => 'Restoration', |
46
|
|
|
'behaviors2' => 'Forgetting Priorities', |
47
|
|
|
'behaviors3' => 'Anxiety', |
48
|
|
|
'behaviors4' => 'Speeding Up', |
49
|
|
|
'behaviors5' => 'Ticked Off', |
50
|
|
|
'behaviors6' => 'Exhausted', |
51
|
|
|
'behaviors7' => 'Relapsed/Moral Failure' |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setBehaviors($behaviors) { |
56
|
|
|
foreach($behaviors as $category_id => $category_data) { |
57
|
|
|
$attribute = "behaviors$category_id"; |
58
|
|
|
$this->$attribute = []; |
59
|
|
|
foreach($category_data['behaviors'] as $behavior) { |
60
|
|
|
$this->{$attribute}[] = $behavior['id']; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function validateBehaviors($attribute, $params) { |
|
|
|
|
66
|
|
|
if (!$this->hasErrors()) { |
67
|
|
|
foreach($this->$attribute as $behavior) { |
68
|
|
|
if(!is_numeric($behavior)) { |
69
|
|
|
$this->addError($attribute, 'One of your behaviors is not an integer!'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function compileBehaviors() { |
76
|
|
|
$behaviors = array_merge((array)$this->behaviors1, |
77
|
|
|
(array)$this->behaviors2, |
78
|
|
|
(array)$this->behaviors3, |
79
|
|
|
(array)$this->behaviors4, |
80
|
|
|
(array)$this->behaviors5, |
81
|
|
|
(array)$this->behaviors6, |
82
|
|
|
(array)$this->behaviors7); |
83
|
|
|
|
84
|
|
|
return array_filter($behaviors); // strip out false values |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function deleteToday() { |
88
|
|
|
$time = Yii::$container->get(\common\interfaces\TimeInterface::class); |
89
|
|
|
$user_behavior = Yii::$container->get(\common\interfaces\UserBehaviorInterface::class); |
90
|
|
|
|
91
|
|
|
$date = $time->getLocalDate(); |
92
|
|
|
list($start, $end) = $time->getUTCBookends($date); |
93
|
|
|
|
94
|
|
|
$user_behavior->deleteAll("user_id=:user_id |
95
|
|
|
AND date > :start_date |
96
|
|
|
AND date < :end_date", |
97
|
|
|
[ |
98
|
|
|
"user_id" => Yii::$app->user->id, |
99
|
|
|
':start_date' => $start, |
100
|
|
|
":end_date" => $end |
101
|
|
|
] |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
// delete cached behaviors |
105
|
|
|
array_map(function($period) use ($time) { |
106
|
|
|
$key = "checkins_".Yii::$app->user->id."_{$period}_".$time->getLocalDate(); |
107
|
|
|
Yii::$app->cache->delete($key); |
108
|
|
|
}, [30, 90, 180]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function save() { |
112
|
|
|
if(empty($this->compiled_behaviors)) { |
113
|
|
|
$this->commpiled_behaviors = $this->compileBehaviors(); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$user_behavior = Yii::$container->get(\common\interfaces\UserBehaviorInterface::class); |
117
|
|
|
|
118
|
|
|
$rows = []; |
119
|
|
|
foreach($this->compiled_behaviors as $behavior_id) { |
120
|
|
|
$temp = [ |
121
|
|
|
Yii::$app->user->id, |
122
|
|
|
(int)$behavior_id, |
123
|
|
|
new Expression("now()::timestamp") |
124
|
|
|
]; |
125
|
|
|
$rows[] = $temp; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
Yii::$app |
129
|
|
|
->db |
130
|
|
|
->createCommand() |
131
|
|
|
->batchInsert( |
132
|
|
|
$user_behavior->tableName(), |
133
|
|
|
['user_id', 'behavior_id', 'date'], |
134
|
|
|
$rows |
135
|
|
|
)->execute(); |
136
|
|
|
|
137
|
|
|
// if the user has publicised their check-in graph, create the image |
138
|
|
|
if(Yii::$app->user->identity->expose_graph) { |
|
|
|
|
139
|
|
|
$checkins_last_month = $user_behavior->getCheckInBreakdown(); |
140
|
|
|
|
141
|
|
|
if($checkins_last_month) { |
142
|
|
|
Yii::$container |
143
|
|
|
->get(\common\components\Graph::class, [Yii::$app->user->identity]) |
144
|
|
|
->create($checkins_last_month, true); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.