Skip to content
Permalink
Browse files

Fixes bug on /checkin/report page

Top Behaviors threw an error when only Personal Behaviors had been
selected.

Also adds custom_behavior to the UserBehavior model rules and deletes a
broken and unused relation in that model.
  • Loading branch information
CorWatts committed Nov 25, 2019
1 parent 818991b commit ace19a135ceb455eed5e8c2e0f961f093efabd13
Showing with 13 additions and 11 deletions.
  1. +11 −11 common/models/UserBehavior.php
  2. +2 −0 common/tests/unit/models/UserBehaviorTest.php
@@ -53,6 +53,7 @@ public function rules()
return [
[['user_id', 'behavior_id', 'category_id', 'date'], 'required'],
[['user_id', 'behavior_id', 'category_id'], 'integer'],
['custom_behavior', 'string'],
];
}
@@ -68,6 +69,7 @@ public function attributeLabels()
'user_id' => 'User ID',
'behavior_id' => 'Behavior ID',
'category_id' => 'Category ID',
'custom_behavior' => 'Personal Behavior Name',
];
}
@@ -80,14 +82,6 @@ public function getUser()
return $this->hasOne(\common\models\User::class, ['id' => 'user_id']);
}
/**
* @return \yii\db\ActiveQuery
* @codeCoverageIgnore
*/
public function getCustomBehavior() {
return $this->hasOne(\common\models\CustomBehavior::class, ['custom_behavior_id' => 'id']);
}
public function getPastCheckinDates() {
$past_checkin_dates = [];
$query = new Query;
@@ -189,9 +183,9 @@ public function getBehaviorsByCategory(array $decorated_behaviors) {
public function getBehaviorsWithCounts($limit = null) {
$query = new Query;
$query->params = [":user_id" => Yii::$app->user->id];
$query->select("user_id, behavior_id, category_id, COUNT(id) as count")
$query->select("user_id, behavior_id, category_id, custom_behavior, COUNT(id) as count")
->from('user_behavior_link')
->groupBy('behavior_id, category_id, user_id')
->groupBy('behavior_id, category_id, custom_behavior, user_id')
->having('user_id = :user_id')
->orderBy('count DESC');
@@ -235,7 +229,13 @@ public static function decorate(array $uo) {
$category = \common\models\Category::getCategory('id', $o['category_id']);
if($behavior) {
$o['behavior'] = $behavior;
}
} else if (array_key_exists('custom_behavior', $o)) {
// if a behavior isn't set, this may be a custom_behavior
$o['behavior'] = [
// we don't have 'id' in here...this is a custom_behavior instead of a 'real' one
'name' => $o['custom_behavior'],
];
} // else -- something is weird
if($category) {
$o['category'] = $category;
}
@@ -314,6 +314,7 @@ public function testRules() {
expect('rules', $this->assertEquals($this->user_behavior->rules(), [
[['user_id', 'behavior_id', 'category_id', 'date'], 'required'],
[['user_id', 'behavior_id', 'category_id'], 'integer'],
['custom_behavior', 'string'],
]));
}
@@ -324,6 +325,7 @@ public function testAttributeLabels() {
'user_id' => 'User ID',
'behavior_id' => 'Behavior ID',
'category_id' => 'Category ID',
'custom_behavior' => 'Personal Behavior Name',
]));
}

0 comments on commit ace19a1

Please sign in to comment.
You can’t perform that action at this time.