From 742878acb354d591d6036e8fe2fe53fc752b498f Mon Sep 17 00:00:00 2001 From: Korotkov Pavel Date: Mon, 28 Oct 2019 19:02:32 +0300 Subject: [PATCH] Add only approved average and readme update --- README.md | 3 +- src/Traits/ReviewRateable.php | 68 +++++++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index ba9fd2e..3539b10 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ $post->deleteRating(1); ### Fetch approved or not approved reviews/ratings for a particular resource ```php -// Get not approved ratings +// Get approved ratings $ratings = $post->getApprovedRatings($post->id, 'desc'); // Get not approved ratings @@ -140,6 +140,7 @@ or ````php $post->averageRating(2) //round to 2 decimal place +$post->averageRating(null, true) //get only approved average rating ```` ### Get all ratings: diff --git a/src/Traits/ReviewRateable.php b/src/Traits/ReviewRateable.php index fb5f8eb..22612fa 100644 --- a/src/Traits/ReviewRateable.php +++ b/src/Traits/ReviewRateable.php @@ -17,155 +17,187 @@ public function ratings() /** * - * @var $round + * + * @param $round + * @param $onlyApproved * @return mixed */ - public function averageRating($round= null) + public function averageRating($round= null, $onlyApproved= false) { + $where = $onlyApproved ? [['approved', '1']] : []; + if ($round) { return $this->ratings() ->selectRaw('ROUND(AVG(rating), '.$round.') as averageReviewRateable') + ->where($where) ->pluck('averageReviewRateable'); } return $this->ratings() ->selectRaw('AVG(rating) as averageReviewRateable') + ->where($where) ->pluck('averageReviewRateable'); } /** * * @var $round + * @var $onlyApproved * @return mixed */ - public function averageCustomerServiceRating($round= null) + public function averageCustomerServiceRating($round= null, $onlyApproved= false) { + $where = $onlyApproved ? [['approved', '1']] : []; + if ($round) { return $this->ratings() ->selectRaw('ROUND(AVG(customer_service_rating), '.$round.') as averageCustomerServiceReviewRateable') + ->where($where) ->pluck('averageCustomerServiceReviewRateable'); } return $this->ratings() ->selectRaw('AVG(customer_service_rating) as averageCustomerServiceReviewRateable') + ->where($where) ->pluck('averageCustomerServiceReviewRateable'); } /** * * @param $round + * @param $onlyApproved * @return mixed */ - public function averageQualityRating($round = null) + public function averageQualityRating($round = null, $onlyApproved= false) { + $where = $onlyApproved ? [['approved', '1']] : []; + if ($round) { return $this->ratings() ->selectRaw('ROUND(AVG(quality_rating), '.$round.') as averageQualityReviewRateable') + ->where($where) ->pluck('averageQualityReviewRateable'); } return $this->ratings() ->selectRaw('AVG(quality_rating) as averageQualityReviewRateable') + ->where($where) ->pluck('averageQualityReviewRateable'); } /** * * @var $round + * @var $onlyApproved * @return mixed */ - public function averageFriendlyRating($round = null) + public function averageFriendlyRating($round = null, $onlyApproved= false) { + $where = $onlyApproved ? [['approved', '1']] : []; + if ($round) { return $this->ratings() ->selectRaw('ROUND(AVG(friendly_rating), '.$round.') as averageFriendlyReviewRateable') + ->where($where) ->pluck('averageFriendlyReviewRateable'); } return $this->ratings() ->selectRaw('AVG(friendly_rating) as averageFriendlyReviewRateable') + ->where($where) ->pluck('averageFriendlyReviewRateable'); } /** * * @var $round + * @var $onlyApproved * @return mixed */ - public function averagePricingRating($round = null) + public function averagePricingRating($round = null, $onlyApproved= false) { + $where = $onlyApproved ? [['approved', '1']] : []; + if ($round) { return $this->ratings() ->selectRaw('ROUND(AVG(pricing_rating), '.$round.') as averagePricingReviewRateable') + ->where($where) ->pluck('averagePricingReviewRateable'); } return $this->ratings() ->selectRaw('AVG(pricing_rating) as averagePricingReviewRateable') + ->where($where) ->pluck('averagePricingReviewRateable'); } /** - * + * @var $onlyApproved * @return mixed */ - public function countRating() + public function countRating($onlyApproved= false) { return $this->ratings() ->selectRaw('count(rating) as countReviewRateable') + ->where($onlyApproved ? [['approved', '1']] : []) ->pluck('countReviewRateable'); } /** - * + * @var $onlyApproved * @return mixed */ - public function countCustomerServiceRating() + public function countCustomerServiceRating($onlyApproved= false) { return $this->ratings() ->selectRaw('count(customer_service_rating) as countCustomerServiceReviewRateable') + ->where($onlyApproved ? [['approved', '1']] : []) ->pluck('countCustomerServiceReviewRateable'); } /** - * + * @var $onlyApproved * @return mixed */ - public function countQualityRating() + public function countQualityRating($onlyApproved= false) { return $this->ratings() ->selectRaw('count(quality_rating) as countQualityReviewRateable') + ->where($onlyApproved ? [['approved', '1']] : []) ->pluck('countQualityReviewRateable'); } /** - * + * @var $onlyApproved * @return mixed */ - public function countFriendlyRating() { + public function countFriendlyRating($onlyApproved= false) { return $this->ratings() ->selectRaw('count(friendly_rating) as countFriendlyReviewRateable') + ->where($onlyApproved ? [['approved', '1']] : []) ->pluck('countFriendlyReviewRateable'); } /** - * + * @var $onlyApproved * @return mixed */ - public function countPriceRating() { + public function countPriceRating($onlyApproved= false) { return $this->ratings() ->selectRaw('count(price_rating) as countPriceReviewRateable') + ->where($onlyApproved ? [['approved', '1']] : []) ->pluck('countPriceReviewRateable'); } /** - * + * @var $onlyApproved * @return mixed */ - public function sumRating() + public function sumRating($onlyApproved= false) { return $this->ratings() ->selectRaw('SUM(rating) as sumReviewRateable') + ->where($onlyApproved ? [['approved', '1']] : []) ->pluck('sumReviewRateable'); }