1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yoeunes\Rateable\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\DB; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Illuminate\Database\Query\JoinClause; |
8
|
|
|
use Yoeunes\Rateable\Builders\RatingBuilder; |
9
|
|
|
use Yoeunes\Rateable\Builders\RatingQueryBuilder; |
10
|
|
|
use Yoeunes\Rateable\Exceptions\InvalidRatingValue; |
11
|
|
|
|
12
|
|
|
trait Rateable |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* This model has many ratings. |
16
|
|
|
* |
17
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
18
|
|
|
*/ |
19
|
|
|
public function ratings() |
20
|
|
|
{ |
21
|
|
|
return $this->morphMany(config('rateable.rating'), 'rateable'); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function averageRating() |
25
|
|
|
{ |
26
|
|
|
return $this->ratings()->avg('value'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function countRating() |
30
|
|
|
{ |
31
|
|
|
return $this->ratings()->count(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function totalRating() |
35
|
|
|
{ |
36
|
|
|
return $this->ratings()->sum('value'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function averageRatingForUser(int $user_id) |
40
|
|
|
{ |
41
|
|
|
return $this->ratings()->where('user_id', $user_id)->avg('value'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function totalRatingForUser(int $user_id) |
45
|
|
|
{ |
46
|
|
|
return $this->ratings()->where('user_id', $user_id)->sum('value'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function countRatingForUser(int $user_id) |
50
|
|
|
{ |
51
|
|
|
return $this->ratings()->where('user_id', $user_id)->count(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function ratingPercentage() |
55
|
|
|
{ |
56
|
|
|
$max = config('rateable.max_rating'); |
57
|
|
|
|
58
|
|
|
$quantity = $this->ratings()->count(); |
59
|
|
|
|
60
|
|
|
$total = $this->totalRating(); |
61
|
|
|
|
62
|
|
|
return ($quantity * $max) > 0 ? $total / (($quantity * $max) / 100) : 0; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function positiveRatingCount() |
66
|
|
|
{ |
67
|
|
|
return $this->ratings()->where('value', '>=', '0')->count(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function positiveRatingTotal() |
71
|
|
|
{ |
72
|
|
|
return $this->ratings()->where('value', '>=', '0')->sum('value'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function negativeRatingCount() |
76
|
|
|
{ |
77
|
|
|
return $this->ratings()->where('value', '<', '0')->count(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function negativeRatingTotal() |
81
|
|
|
{ |
82
|
|
|
return $this->ratings()->where('value', '<', '0')->sum('value'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function isRated() |
86
|
|
|
{ |
87
|
|
|
return $this->ratings()->exists(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function isRatedBy(int $user_id) |
91
|
|
|
{ |
92
|
|
|
return $this->ratings()->where('user_id', $user_id)->exists(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* to order by average_rating. |
97
|
|
|
* |
98
|
|
|
* add protected $appends = [ 'average_rating' ]; to your model |
99
|
|
|
* |
100
|
|
|
* Lesson::all()->sortBy('average_rating') |
101
|
|
|
* Lesson::with('relatedModel')->get()->sortBy('average_rating') |
102
|
|
|
* Lesson::where('status', 'published')->get()->sortBy('average_rating') |
103
|
|
|
* |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
|
public function getAverageRatingAttribute() |
107
|
|
|
{ |
108
|
|
|
return $this->averageRating(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function scopeOrderByAverageRating(Builder $query, string $direction = 'asc') |
112
|
|
|
{ |
113
|
|
|
return $query |
114
|
|
|
->leftJoin('ratings', function (JoinClause $join) { |
115
|
|
|
$join |
116
|
|
|
->on('ratings.rateable_id', $this->getTable() . '.id') |
|
|
|
|
117
|
|
|
->where('ratings.rateable_type', morph_type(__CLASS__)); |
118
|
|
|
}) |
119
|
|
|
->addSelect(DB::raw('AVG(ratings.value) as average_rating')) |
120
|
|
|
->groupBy($this->getTable(). '.id') |
|
|
|
|
121
|
|
|
->orderBy('average_rating', $direction); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function deleteRating(int $rating_id) |
125
|
|
|
{ |
126
|
|
|
return $this->ratings()->where('id', $rating_id)->delete(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function resetRating() |
130
|
|
|
{ |
131
|
|
|
return $this->ratings()->delete(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function deleteRatingsForUser(int $user_id) |
135
|
|
|
{ |
136
|
|
|
return $this->ratings()->where('user_id', $user_id)->delete(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param int $user_id |
141
|
|
|
* @param int $value |
142
|
|
|
* |
143
|
|
|
* @return int |
144
|
|
|
* |
145
|
|
|
* @throws \Throwable |
146
|
|
|
*/ |
147
|
|
|
public function updateRatingForUser(int $user_id, int $value) |
148
|
|
|
{ |
149
|
|
|
throw_if($value < config('rateable.min_rating') || $value > config('rateable.max_rating'), InvalidRatingValue::class, 'Invalid rating value'); |
150
|
|
|
|
151
|
|
|
return $this->ratings()->where('user_id', $user_id)->update(['value' => $value]); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param int $rating_id |
156
|
|
|
* @param int $value |
157
|
|
|
* |
158
|
|
|
* @return int |
159
|
|
|
* |
160
|
|
|
* @throws \Throwable |
161
|
|
|
*/ |
162
|
|
|
public function updateRating(int $rating_id, int $value) |
163
|
|
|
{ |
164
|
|
|
throw_if($value < config('rateable.min_rating') || $value > config('rateable.max_rating'), InvalidRatingValue::class, 'Invalid rating value'); |
165
|
|
|
|
166
|
|
|
return $this->ratings()->where('id', $rating_id)->update(['value' => $value]); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return RatingBuilder |
171
|
|
|
* |
172
|
|
|
* @throws \Throwable |
173
|
|
|
*/ |
174
|
|
|
public function getRatingBuilder() |
175
|
|
|
{ |
176
|
|
|
return (new RatingBuilder()) |
177
|
|
|
->rateable($this); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function raters() |
181
|
|
|
{ |
182
|
|
|
return $this->morphToMany(config('rateable.user'), 'rateable', 'ratings'); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function countRatingsByDate($from = null, $to = null) |
186
|
|
|
{ |
187
|
|
|
$query = $this->ratings(); |
188
|
|
|
|
189
|
|
|
if (! empty($from) && empty($to)) { |
190
|
|
|
$query->where('created_at', '>=', date_transformer($from)); |
191
|
|
|
} elseif (empty($from) && ! empty($to)) { |
192
|
|
|
$query->where('created_at', '<=', date_transformer($to)); |
193
|
|
|
} elseif (! empty($from) && ! empty($to)) { |
194
|
|
|
$query->whereBetween('created_at', [date_transformer($from), date_transformer($to)]); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $query->sum('value'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function getRatingQueryBuilder() |
201
|
|
|
{ |
202
|
|
|
return new RatingQueryBuilder($this->ratings()); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.