Skip to content

Commit

Permalink
feat: Allow display of other users ratings by user id. (#66)
Browse files Browse the repository at this point in the history
* Updated Rateable.php with user parameter

Once the userId is provided, the ratings for that user is retrieved instead of the auth user.

* Updated ratingPercent and user_id in Rateable.php

Second parameter, rounded, was added to ratingPercent, with false as default value, giving the User the option to round off the result or get the float.
Corrected the variable userId to user_id.

* Update Rateable.php w/ modified byUser

Added extra checks on user_id for not null value

* Update Rateable.php w/ optimized byUser

The function byUser was optimized to avoid overloading the app with unnecessary null check, to improve performance.
Thank you @willvincent for pointing this out.

* Update Rateable.php without User model import

Due to test fails, the import for User model was removed, and the user class fetched from auth.providers instead.

* Update Rateable.php w/ Config facade

Added Config facade import
  • Loading branch information
metaversedataman authored Jun 29, 2023
1 parent f5445b9 commit 9ea34cf
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/Rateable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace willvincent\Rateable;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;

trait Rateable
{
Expand All @@ -15,22 +16,38 @@ trait Rateable
*
* @return Rating
*/
public function rate($value, $comment = null)

private function byUser($user_id = null) {
if(!! $user_id) {
return Auth::id();
}

$userClass = Config::get('auth.model');
if (is_null($userClass)) {
$userClass = Config::get('auth.providers.users.model');
}

return (!! $userClass::whereId($user_id)->count())? $user_id: Auth::id();
}

public function rate($value, $comment = null, $user_id = null)
{
$user_id = $this->byUser($user_id);
$rating = new Rating();
$rating->rating = $value;
$rating->comment = $comment;
$rating->user_id = Auth::id();
$rating->user_id = $user_id;

$this->ratings()->save($rating);
}

public function rateOnce($value, $comment = null)
public function rateOnce($value, $comment = null, $user_id = null)
{
$user_id = $this->byUser($user_id);
$rating = Rating::query()
->where('rateable_type', '=', $this->getMorphClass())
->where('rateable_id', '=', $this->id)
->where('user_id', '=', Auth::id())
->where('user_id', '=', $user_id)
->first()
;

Expand Down Expand Up @@ -68,22 +85,30 @@ public function usersRated()
return $this->ratings()->groupBy('user_id')->pluck('user_id')->count();
}

public function userAverageRating()
public function userAverageRating($user_id = null)
{
return $this->ratings()->where('user_id', Auth::id())->avg('rating');
$user_id = $this->byUser($user_id);
return $this->ratings()->where('user_id', $user_id)->avg('rating');
}

public function userSumRating()
public function userSumRating($user_id = null)
{
return $this->ratings()->where('user_id', Auth::id())->sum('rating');
$user_id = $this->byUser($user_id);
return $this->ratings()->where('user_id', $user_id)->sum('rating');
}

public function ratingPercent($max = 5)
public function ratingPercent($max = 5, bool $rounded = false)
{
$quantity = $this->ratings()->count();
$total = $this->sumRating();
// return "$total || $quantity";

return ($quantity * $max) > 0 ? $total / (($quantity * $max) / 100) : 0;
$is_rounded = is_bool($rounded)? $rounded: false;
if($rounded) {
return ($quantity * $max) > 0 ? ceil(($total / ($quantity * $max)) * 100) : 0;
} else {
return ($quantity * $max) > 0 ? $total / (($quantity * $max) / 100) : 0;
}
}

// Getters
Expand Down

0 comments on commit 9ea34cf

Please sign in to comment.