1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace common\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use \common\interfaces\TimeInterface; |
7
|
|
|
use \common\interfaces\BehaviorInterface; |
8
|
|
|
use \common\interfaces\UserBehaviorInterface; |
9
|
|
|
use \common\components\ActiveRecord; |
10
|
|
|
use yii\db\Query; |
11
|
|
|
use yii\helpers\ArrayHelper as AH; |
12
|
|
|
use \DateTime; |
13
|
|
|
use \DateTimeZone; |
14
|
|
|
use yii\db\Expression; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This is the model class for table "user_behavior_link". |
18
|
|
|
* |
19
|
|
|
* @property integer $id |
20
|
|
|
* @property integer $user_id |
21
|
|
|
* @property integer $behavior_id |
22
|
|
|
* @property integer $category_id |
23
|
|
|
* @property string $date |
24
|
|
|
* |
25
|
|
|
* @property Behavior $user |
26
|
|
|
*/ |
27
|
|
|
class UserBehavior extends ActiveRecord implements UserBehaviorInterface |
28
|
|
|
{ |
29
|
|
|
private $time; |
30
|
|
|
private $behavior; |
31
|
|
|
|
32
|
|
|
public function __construct(BehaviorInterface $behavior, TimeInterface $time, $config = []) { |
33
|
|
|
$this->behavior = $behavior; |
34
|
|
|
$this->time = $time; |
35
|
|
|
parent::__construct($config); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
41
|
|
|
public static function tableName() |
42
|
|
|
{ |
43
|
|
|
return 'user_behavior_link'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
public function rules() |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
|
|
[['user_id', 'behavior_id', 'category_id', 'date'], 'required'], |
53
|
|
|
[['user_id', 'behavior_id', 'category_id'], 'integer'], |
54
|
|
|
//[['date'], 'string'] |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
|
|
public function attributeLabels() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
'id' => 'ID', |
65
|
|
|
'date' => 'Date', |
66
|
|
|
'user_id' => 'User ID', |
67
|
|
|
'behavior_id' => 'Behavior ID', |
68
|
|
|
'category_id' => 'Category ID', |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return \yii\db\ActiveQuery |
74
|
|
|
*/ |
75
|
|
|
public function getUser() |
76
|
|
|
{ |
77
|
|
|
return $this->hasOne(\common\models\User::class, ['id' => 'user_id']); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getPastCheckinDates() { |
81
|
|
|
$past_checkin_dates = []; |
82
|
|
|
$query = new Query; |
83
|
|
|
$query->params = [":user_id" => Yii::$app->user->id]; |
84
|
|
|
$query->select("date") |
85
|
|
|
->from('user_behavior_link l') |
86
|
|
|
->groupBy('date, user_id') |
87
|
|
|
->having('user_id = :user_id'); |
88
|
|
|
$temp_dates = $query->all(); |
89
|
|
|
foreach($temp_dates as $temp_date) { |
90
|
|
|
$past_checkin_dates[] = $this->time->convertUTCToLocal($temp_date['date']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $past_checkin_dates; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getUserBehaviorsWithCategory($checkin_date) { |
97
|
|
|
list($start, $end) = $this->time->getUTCBookends($checkin_date); |
98
|
|
|
|
99
|
|
|
$query = new Query; |
100
|
|
|
$query->select('*') |
101
|
|
|
->from('user_behavior_link') |
102
|
|
|
->orderBy('behavior_id') |
103
|
|
|
->where("user_id=:user_id |
104
|
|
|
AND date > :start_date |
105
|
|
|
AND date < :end_date", |
106
|
|
|
[ |
107
|
|
|
":user_id" => Yii::$app->user->id, |
108
|
|
|
":start_date" => $start, |
109
|
|
|
":end_date" => $end |
110
|
|
|
]); |
111
|
|
|
|
112
|
|
|
$user_behaviors = self::decorate($query->all()); |
113
|
|
|
return AH::map($user_behaviors, 'id', function($a) { return $a['behavior']['name']; }, function($b) {return $b['behavior']['category_id']; }); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getCheckinBreakdown(int $period = 30) { |
117
|
|
|
$datetimes = $this->time->getDateTimesInPeriod($period); |
|
|
|
|
118
|
|
|
$key = "checkins_".Yii::$app->user->id."_{$period}_".$this->time->getLocalDate(); |
119
|
|
|
$checkins = Yii::$app->cache->get($key); |
120
|
|
|
|
121
|
|
|
if($checkins === false) { |
122
|
|
|
$checkins = []; |
123
|
|
|
foreach($datetimes as $datetime) { |
124
|
|
|
$behaviors = self::decorate($this->getBehaviorsWithCounts($datetime)); |
125
|
|
|
$checkins[$datetime->format('Y-m-d')] = $this->getBehaviorsByCategory($behaviors); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
Yii::$app->cache->set($key, $checkins, 60*60*24); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $checkins; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param integer $limit the desired number of behaviors |
136
|
|
|
* @return array a list of the most-selected behaviors |
137
|
|
|
*/ |
138
|
|
|
public function getTopBehaviors(int $limit = 5) { |
139
|
|
|
return self::decorate($this->getBehaviorsWithCounts($limit)); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param array $decorated_behaviors an array of behaviors ran through self::decorate() |
144
|
|
|
* @return array a list of categories and the number of selected behaviors in each category |
145
|
|
|
*/ |
146
|
|
|
public function getBehaviorsByCategory(array $decorated_behaviors) { |
147
|
|
|
$arr = array_reduce($decorated_behaviors, function($acc, $row) { |
148
|
|
|
$cat_id = $row['category_id']; |
149
|
|
|
if(array_key_exists($cat_id, $acc)) { |
150
|
|
|
$acc[$cat_id]['count'] += $row['count']; |
151
|
|
|
} else { |
152
|
|
|
$acc[$cat_id] = [ |
153
|
|
|
'name' => $row['category']['name'], |
154
|
|
|
'count' => $row['count'], |
155
|
|
|
'color' => \common\models\Category::$colors[$cat_id]['color'], |
156
|
|
|
'highlight' => \common\models\Category::$colors[$cat_id]['highlight'], |
157
|
|
|
]; |
158
|
|
|
} |
159
|
|
|
return $acc; |
160
|
|
|
}, []); |
161
|
|
|
ksort($arr); |
162
|
|
|
return $arr; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/* |
166
|
|
|
* @param integer|\DateTime $limit a limit for the number of behaviors to |
167
|
|
|
* return if an integer is supplied. If a \DateTime object is given instead, |
168
|
|
|
* then it adds a start and end date range in the WHERE clause of the query. |
169
|
|
|
* @return array a list of the user's behaviors with a count of each behavior's |
170
|
|
|
* occurence, sorted in descending order of occurrence. |
171
|
|
|
*/ |
172
|
|
|
public function getBehaviorsWithCounts($limit = null) { |
173
|
|
|
$query = new Query; |
174
|
|
|
$query->params = [":user_id" => Yii::$app->user->id]; |
175
|
|
|
$query->select("user_id, behavior_id, category_id, COUNT(id) as count") |
176
|
|
|
->from('user_behavior_link') |
177
|
|
|
->groupBy('behavior_id, category_id, user_id') |
178
|
|
|
->having('user_id = :user_id') |
179
|
|
|
->orderBy('count DESC'); |
180
|
|
|
|
181
|
|
|
if($limit instanceof \DateTime) { |
182
|
|
|
list($start, $end) = $this->time->getUTCBookends($limit->format('Y-m-d')); |
183
|
|
|
$query->params += [':start_date' => $start, ':end_date' => $end]; |
184
|
|
|
$query->where('user_id=:user_id AND date > :start_date AND date <= :end_date'); |
185
|
|
|
} else if(is_int($limit)) { |
186
|
|
|
$query->limit($limit); |
187
|
|
|
} |
188
|
|
|
return $query->all(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* getByDate() |
193
|
|
|
* Returns an array of behaviors selected by the given user on the given date. |
194
|
|
|
* NOTE: this function is designed to return data in the same format that is returned by using yii\helpers\ArrayHelper::index(\common\models\Behavior::$behaviors, 'name', "category_id"). This facilitates generation of the CheckinForm and allows us to easily merge these two datasets together. |
195
|
|
|
* @param integer $user_id |
196
|
|
|
* @param string|null $local_date |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
|
|
public function getByDate(int $user_id, $local_date = null) { |
200
|
|
|
if(is_null($local_date)) $local_date = $this->time->getLocalDate(); |
201
|
|
|
|
202
|
|
|
$behaviors = $this->getBehaviorData($user_id, $local_date); |
203
|
|
|
$behaviors = self::decorate($behaviors); |
204
|
|
|
return $this->parseBehaviorData($behaviors); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/* |
208
|
|
|
* Adds the respective Category and Behavior to each UserBehavior in the given array. |
209
|
|
|
* @param array of UserBehaviors |
|
|
|
|
210
|
|
|
* @return an array of decorated UserBehaviors, each with an added Category and Behavior |
|
|
|
|
211
|
|
|
*/ |
212
|
|
|
public static function decorate(array $uo) { |
213
|
|
|
foreach($uo as &$o) { |
214
|
|
|
$behavior = \common\models\Behavior::getBehavior('id', $o['behavior_id']); |
215
|
|
|
$category = \common\models\Category::getCategory('id', $o['category_id']); |
216
|
|
|
// if this behavior does not have a valid behavior or category, something |
217
|
|
|
// is weird and we don't want to do this halfway. |
218
|
|
|
if($behavior && $category) { |
219
|
|
|
$o['behavior'] = $behavior; |
220
|
|
|
$o['category'] = $category; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
return $uo; |
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// this should probably be a private method...but unit testing is hard |
227
|
|
|
public function getBehaviorData(int $user_id, $local_date) { |
|
|
|
|
228
|
|
|
list($start, $end) = $this->time->getUTCBookends($local_date); |
229
|
|
|
|
230
|
|
|
return $this->find() |
231
|
|
|
->where("user_id=:user_id |
232
|
|
|
AND date > :start_date |
233
|
|
|
AND date < :end_date", |
234
|
|
|
[ |
235
|
|
|
"user_id" => Yii::$app->user->id, |
236
|
|
|
':start_date' => $start, |
237
|
|
|
":end_date" => $end |
238
|
|
|
]) |
239
|
|
|
->asArray() |
240
|
|
|
->all(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// this should probably be a private method...but unit testing is hard |
244
|
|
|
public function parseBehaviorData($behaviors) { |
245
|
|
|
if(!$behaviors) return []; |
246
|
|
|
|
247
|
|
|
$bhvrs_by_cat = []; |
248
|
|
|
foreach($behaviors as $behavior) { |
249
|
|
|
$indx = $behavior['category_id']; |
250
|
|
|
|
251
|
|
|
$bhvrs_by_cat[$indx][$behavior['behavior']['name']] = [ |
252
|
|
|
"id" => $behavior['behavior_id'], |
253
|
|
|
"name"=>$behavior['behavior']['name']]; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return $bhvrs_by_cat; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|