|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\review\widgets\rating; |
|
4
|
|
|
|
|
5
|
|
|
use app\modules\review\models\RatingItem; |
|
6
|
|
|
use app\modules\review\models\RatingValues; |
|
7
|
|
|
use app\modules\review\models\Review; |
|
8
|
|
|
use Yii; |
|
9
|
|
|
use yii\base\Widget; |
|
10
|
|
|
use yii\helpers\ArrayHelper; |
|
11
|
|
|
|
|
12
|
|
|
class RatingShowWidget extends Widget |
|
13
|
|
|
{ |
|
14
|
|
|
public $objectModel; |
|
15
|
|
|
public $calcFunction; |
|
16
|
|
|
public $viewFile = 'rating-show'; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Review|null |
|
19
|
|
|
*/ |
|
20
|
|
|
public $reviewModel = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @inheritdoc |
|
24
|
|
|
*/ |
|
25
|
|
|
public function init() |
|
26
|
|
|
{ |
|
27
|
|
|
parent::init(); |
|
28
|
|
|
RatingAsset::register($this->view); |
|
29
|
|
|
if (is_array($this->calcFunction) && count($this->calcFunction) >= 2) { |
|
30
|
|
|
$class = array_shift($this->calcFunction); |
|
31
|
|
|
$method = array_shift($this->calcFunction); |
|
32
|
|
|
if (class_exists($class) && method_exists($class, $method)) { |
|
33
|
|
|
$this->calcFunction = [$class, $method]; |
|
34
|
|
|
} else { |
|
35
|
|
|
$this->calcFunction = [$this, 'calculateRating']; |
|
36
|
|
|
} |
|
37
|
|
|
} elseif (!$this->calcFunction instanceof \Closure) { |
|
38
|
|
|
$this->calcFunction = [$this, 'calculateRating']; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function run() |
|
46
|
|
|
{ |
|
47
|
|
|
parent::run(); |
|
48
|
|
|
if (!is_null($this->objectModel)) { |
|
49
|
|
|
$groupedRatingValues = RatingValues::getValuesByObjectModel($this->objectModel); |
|
50
|
|
|
if (!empty($groupedRatingValues)) { |
|
51
|
|
|
$groups = []; |
|
52
|
|
|
foreach ($groupedRatingValues as $groupId => $group) { |
|
53
|
|
|
$ratingItem = RatingItem::findById($groupId); |
|
54
|
|
|
$groups[] = [ |
|
55
|
|
|
'name' => !is_null($ratingItem) ? $ratingItem->name : Yii::t('app', 'Unknown rating'), |
|
56
|
|
|
'rating' => call_user_func($this->calcFunction, $group), |
|
57
|
|
|
'votes' => count($group), |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
} else { |
|
61
|
|
|
$groups = null; |
|
62
|
|
|
} |
|
63
|
|
|
return $this->render( |
|
64
|
|
|
$this->viewFile, |
|
65
|
|
|
[ |
|
66
|
|
|
'groups' => $groups, |
|
67
|
|
|
] |
|
68
|
|
|
); |
|
69
|
|
|
} elseif (!is_null($this->reviewModel)) { |
|
70
|
|
|
$value = RatingValues::findOne(['rating_id' => $this->reviewModel->rating_id]); |
|
71
|
|
|
$groups = []; |
|
72
|
|
|
|
|
73
|
|
|
$ratingItem = RatingItem::findById(ArrayHelper::getValue($value, 'rating_item_id', 0)); |
|
|
|
|
|
|
74
|
|
|
$group = [ArrayHelper::getValue($value, 'value')]; |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
$groups[] = [ |
|
77
|
|
|
'name' => !is_null($ratingItem) ? $ratingItem->name : Yii::t('app', 'Unknown rating'), |
|
78
|
|
|
'rating' => call_user_func($this->calcFunction, $group), |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
return $this->render( |
|
82
|
|
|
$this->viewFile, |
|
83
|
|
|
[ |
|
84
|
|
|
'groups' => $groups, |
|
85
|
|
|
] |
|
86
|
|
|
); |
|
87
|
|
|
} else { |
|
88
|
|
|
return ''; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param $values |
|
95
|
|
|
* @return float|int |
|
96
|
|
|
*/ |
|
97
|
|
|
private function calculateRating($values) |
|
98
|
|
|
{ |
|
99
|
|
|
$count = count($values); |
|
100
|
|
|
if (0 === $sum = array_sum($values)) { |
|
101
|
|
|
return 0; |
|
102
|
|
|
} |
|
103
|
|
|
return $sum / $count; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.