Skip to content

Commit 74bcd11

Browse files
committed
Working on adding averageRatingForAll.
1 parent 5c878fc commit 74bcd11

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/Contracts/ReviewRateable.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,11 @@ public function deleteRating($id);
180180
* @return mixed
181181
*/
182182
public function getUserRatings($id, $author, $sort = 'desc');
183+
184+
/**
185+
*
186+
* @param $round
187+
* @return double
188+
*/
189+
public function averageRatingForAll($round = null);
183190
}

src/Models/Rating.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,28 @@ public function getUserRatings($id, $author, $sort = 'desc')
213213

214214
return $rating;
215215
}
216+
217+
/**
218+
* @param $id
219+
*
220+
* @return mixed
221+
*/
222+
public function getAverageRatingForAll($round = null, $onlyApproved = false)
223+
{
224+
$where = $onlyApproved ? [['approved', '1']] : [];
225+
$avgExpression = null;
226+
227+
if ($round) {
228+
$avgExpression = 'ROUND(AVG(rating), ' . $round . ') as averageReviewRateable';
229+
} else {
230+
$avgExpression = 'AVG(rating) as averageReviewRateable';
231+
}
232+
233+
return $this->ratings()
234+
->selectRaw($avgExpression)
235+
->where($where)
236+
->get()
237+
->first()
238+
->averageReviewRateable;
239+
}
216240
}

src/Traits/ReviewRateable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,4 +382,14 @@ public function getUserRatings($id, $author, $sort = 'desc')
382382
{
383383
return (new Rating())->getUserRatings($id, $author, $sort = 'desc');
384384
}
385+
386+
/**
387+
* @param $id
388+
*
389+
* @return mixed
390+
*/
391+
public function averageRatingForAll($round = null, $onlyApproved = false)
392+
{
393+
return (new Rating())->getAverageRatingForAll($round, $onlyApproved);
394+
}
385395
}

0 commit comments

Comments
 (0)