1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hauntd\vote\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\behaviors\TimestampBehavior; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This is the model class for table "vote". |
10
|
|
|
* |
11
|
|
|
* @author Alexander Kononenko <[email protected]> |
12
|
|
|
* @package hauntd\vote\models |
13
|
|
|
* @property integer $id |
14
|
|
|
* @property integer $entity |
15
|
|
|
* @property integer $target_id |
16
|
|
|
* @property integer $user_id |
17
|
|
|
* @property string $user_ip |
18
|
|
|
* @property integer $value |
19
|
|
|
* @property integer $created_at |
20
|
|
|
* |
21
|
|
|
* @property \hauntd\vote\models\VoteAggregate $aggregate |
22
|
|
|
*/ |
23
|
|
|
class Vote extends \yii\db\ActiveRecord |
24
|
|
|
{ |
25
|
|
|
const VOTE_POSITIVE = 1; |
26
|
|
|
const VOTE_NEGATIVE = 0; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public static function tableName() |
32
|
|
|
{ |
33
|
|
|
return '{{%vote}}'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function behaviors() |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
[ |
43
|
|
|
'class' => TimestampBehavior::class, |
44
|
|
|
'createdAtAttribute' => 'created_at', |
45
|
|
|
'updatedAtAttribute' => false, |
46
|
|
|
], |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function rules() |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
[['entity', 'target_id', 'value'], 'required'], |
57
|
|
|
[['entity', 'target_id', 'user_id', 'value', 'created_at'], 'integer'], |
58
|
|
|
[['user_ip'], 'default', 'value' => function () { |
59
|
|
|
if (Yii::$app instanceof \yii\web\Application) { |
60
|
|
|
return Yii::$app->request->userIP; |
61
|
|
|
} |
62
|
|
|
return null; |
63
|
|
|
}], |
64
|
|
|
[['user_id'], 'default', 'value' => function () { |
65
|
|
|
if (isset(Yii::$app->user) && !Yii::$app->user->isGuest) { |
66
|
|
|
return Yii::$app->user->id; |
67
|
|
|
} |
68
|
|
|
return null; |
69
|
|
|
}], |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function attributeLabels() |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
'id' => 'ID', |
80
|
|
|
'entity' => 'Entity', |
81
|
|
|
'target_id' => 'Target Model ID', |
82
|
|
|
'user_id' => 'User ID', |
83
|
|
|
'user_ip' => 'User Ip', |
84
|
|
|
'value' => 'Value', |
85
|
|
|
'created_at' => 'Created At', |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return \yii\db\ActiveQuery |
91
|
|
|
*/ |
92
|
|
|
public function getAggregate() |
93
|
|
|
{ |
94
|
|
|
return $this->hasOne(VoteAggregate::class, [ |
95
|
|
|
'vote.entity' => 'vote_aggregate.entity', |
96
|
|
|
'vote.target_id' => 'vote_aggregate.target_id' |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param bool $insert |
102
|
|
|
* @param array $changedAttributes |
103
|
|
|
*/ |
104
|
|
|
public function afterSave($insert, $changedAttributes) |
105
|
|
|
{ |
106
|
|
|
static::updateRating($this->attributes['entity'], $this->attributes['target_id']); |
107
|
|
|
parent::afterSave($insert, $changedAttributes); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritdoc |
112
|
|
|
*/ |
113
|
|
|
public function afterDelete() |
114
|
|
|
{ |
115
|
|
|
static::updateRating($this->attributes['entity'], $this->attributes['target_id']); |
116
|
|
|
parent::afterDelete(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $entity |
121
|
|
|
* @param $targetId |
122
|
|
|
*/ |
123
|
|
|
public static function updateRating($entity, $targetId) |
124
|
|
|
{ |
125
|
|
|
$positive = static::find()->where(['entity' => $entity, 'target_id' => $targetId, 'value' => self::VOTE_POSITIVE])->count(); |
126
|
|
|
$negative = static::find()->where(['entity' => $entity, 'target_id' => $targetId, 'value' => self::VOTE_NEGATIVE])->count(); |
127
|
|
|
if ($positive + $negative !== 0) { |
128
|
|
|
$rating = (($positive + 1.9208) / ($positive + $negative) - 1.96 * SQRT(($positive * $negative) |
129
|
|
|
/ ($positive + $negative) + 0.9604) / ($positive + $negative)) / (1 + 3.8416 / ($positive + $negative)); |
130
|
|
|
} else { |
131
|
|
|
$rating = 0; |
132
|
|
|
} |
133
|
|
|
$rating = round($rating * 10, 2); |
134
|
|
|
$aggregateModel = VoteAggregate::findOne([ |
135
|
|
|
'entity' => $entity, |
136
|
|
|
'target_id' => $targetId, |
137
|
|
|
]); |
138
|
|
|
if ($aggregateModel == null) { |
139
|
|
|
$aggregateModel = new VoteAggregate(); |
140
|
|
|
$aggregateModel->entity = $entity; |
141
|
|
|
$aggregateModel->target_id = $targetId; |
142
|
|
|
} |
143
|
|
|
$aggregateModel->positive = $positive; |
|
|
|
|
144
|
|
|
$aggregateModel->negative = $negative; |
|
|
|
|
145
|
|
|
$aggregateModel->rating = $rating; |
146
|
|
|
$aggregateModel->save(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.