Skip to content

Commit 3f5120b

Browse files
committed
Wip
1 parent eefb684 commit 3f5120b

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/Contracts/ReviewRateableContract.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ public function averageRatingsByDepartment(string $department, bool $approved =
8585
*/
8686
public function getReviews(bool $approved = true, bool $withRatings = true): Collection;
8787

88+
public function getReviewsByDepartment(string $department, bool $approved = true, bool $withRatings = true): Collection;
89+
8890
/**
8991
* Get the total number of reviews for the attached model.
9092
*
@@ -109,4 +111,14 @@ public function overallAverageRating(bool $approved = true): ?float;
109111
*/
110112
public function deleteReview(int $reviewId): bool;
111113

114+
/**
115+
* Return an array of rating value ⇒ count, for the full model
116+
* or for a given department.
117+
*
118+
* @param string|null $department
119+
* @param bool $approved
120+
* @return array
121+
*/
122+
public function ratingCounts(?string $department = null, bool $approved = true): array;
123+
112124
}

src/Services/ReviewRateableService.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,18 @@ public function deleteReview(int $reviewId): bool
179179
return $this->getModel()->deleteReview($reviewId);
180180
}
181181

182+
/**
183+
* Return an array of rating value ⇒ count, for the full model
184+
* or for a given department.
185+
*
186+
* @param string|null $department
187+
* @param bool $approved
188+
* @return array
189+
* @throws Exception
190+
*/
191+
public function ratingCounts(?string $department = null, bool $approved = true): array
192+
{
193+
return $this->getModel()->ratingCounts($approved);
194+
}
195+
182196
}

src/Traits/ReviewRateable.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Codebyray\ReviewRateable\Traits;
44

5+
use Codebyray\ReviewRateable\Models\Rating;
56
use Codebyray\ReviewRateable\Models\Review;
67
use Illuminate\Database\Eloquent\Collection;
8+
use Illuminate\Support\Facades\DB;
79

810
trait ReviewRateable
911
{
@@ -185,6 +187,28 @@ public function getReviews(bool $approved = true, bool $withRatings = true): Col
185187
return $query->get();
186188
}
187189

190+
/**
191+
* Get all reviews (with attached ratings) for a department,
192+
* filtered by the approved status.
193+
*
194+
* @param string $department
195+
* @param bool $approved
196+
* @param bool $withRatings
197+
* @return Collection
198+
*/
199+
public function getReviewsByDepartment(string $department, bool $approved = true, bool $withRatings = true): Collection
200+
{
201+
$query = $this->reviews()
202+
->where('department', $department)
203+
->where('approved', $approved);
204+
205+
if ($withRatings) {
206+
$query->with('ratings');
207+
}
208+
209+
return $query->get();
210+
}
211+
188212
/**
189213
* Calculate the average rating for a given key, filtering reviews by approval.
190214
*
@@ -344,4 +368,46 @@ public function deleteReview(int $reviewId): bool
344368
return false;
345369
}
346370

371+
/**
372+
* Return an array of rating value ⇒ count, for the full model
373+
* or for a given department.
374+
*
375+
* @param string|null $department If null, counts across all departments.
376+
* @param bool $approved Only count approved reviews?
377+
* @return array [1 => 12, 2 => 5, 3 => 23, 4 => 17, 5 => 42]
378+
*/
379+
public function ratingCounts(?string $department = null, bool $approved = true): array
380+
{
381+
$min = config('review-rateable.min_rating_value', 1);
382+
$max = config('review-rateable.max_rating_value', 5);
383+
384+
$reviewQuery = $this->reviews()
385+
->where('approved', $approved);
386+
387+
if ($department) {
388+
$reviewQuery->where('department', $department);
389+
}
390+
391+
$rawCounts = Rating::select('value', DB::raw('count(*) as count'))
392+
->whereHas('review', function ($q) use ($reviewQuery) {
393+
$sql = $reviewQuery->toBase()->getQuery()->toSql();
394+
$bindings = $reviewQuery->getBindings();
395+
$q->from(DB::raw("({$sql}) as reviews_sub"))
396+
->whereColumn('reviews_sub.id', 'ratings.review_id');
397+
foreach ($bindings as $i => $b) {
398+
$q->addBinding($b, 'where');
399+
}
400+
})
401+
->groupBy('value')
402+
->pluck('count', 'value')
403+
->all();
404+
405+
$counts = [];
406+
for ($i = $min; $i <= $max; $i++) {
407+
$counts[$i] = $rawCounts[$i] ?? 0;
408+
}
409+
410+
return $counts;
411+
}
412+
347413
}

0 commit comments

Comments
 (0)