From a3c875b907fa5865f7f3d35ff8183a191bb4ae5b Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 6 Oct 2023 15:59:32 +0800 Subject: [PATCH] Fix setting of rider's current rating. Fix issues found during testing. Remove debug messages. #764 --- src/Command/LoadAggregateRiderRatingsComand.php | 2 +- src/Controller/APIController.php | 11 +++++++---- src/Controller/CustomerAppAPI/RiderController.php | 9 ++++++--- src/Entity/AggregatedRiderRating.php | 2 +- src/Entity/Rider.php | 10 ++-------- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Command/LoadAggregateRiderRatingsComand.php b/src/Command/LoadAggregateRiderRatingsComand.php index 7e10230f..b6c864c0 100644 --- a/src/Command/LoadAggregateRiderRatingsComand.php +++ b/src/Command/LoadAggregateRiderRatingsComand.php @@ -84,7 +84,7 @@ class LoadAggregateRiderRatingsComand extends Command protected function createAggregatedRiderRating($agg_rider_ratings) { - error_log(print_r($agg_rider_ratings, true)); + // error_log(print_r($agg_rider_ratings, true)); foreach ($agg_rider_ratings as $key => $data) { diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index c1fb28ad..8614537e 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -1707,10 +1707,10 @@ class APIController extends Controller implements LoggedController $em->flush(); // need to update or add aggregated rider rating - $this->updateAggregatedRiderRating($em, $rider, $rating_num); + $average_rating = $this->updateAggregatedRiderRating($em, $rider, $rating_num); // TODO: preliminary rating computation on the entity for now - $rider->updateRatingAverage(); + $rider->updateRatingAverage($average_rating); $em->persist($rider); $em->flush(); @@ -4837,6 +4837,7 @@ class APIController extends Controller implements LoggedController protected function updateAggregatedRiderRating($em, $rider, $rating_num) { $rider_id = $rider->getID(); + $agg_rating = 0; // check if rider is in the the aggregated rider rating table $agg_rider_rating = $em->getRepository(AggregatedRiderRating::class)->findOneBy(['rider_id' => $rider_id]); @@ -4861,7 +4862,7 @@ class APIController extends Controller implements LoggedController $r_rating = $agg_rider_rating->getAggregateRating(); $r_count = $agg_rider_rating->getAggregateCount(); - $new_count = ++$r_count; + $new_count = $r_count + 1; $agg_rating = $this->computeAggregatedRiderRating($r_rating, $rating_num, $r_count, $new_count); @@ -4871,6 +4872,8 @@ class APIController extends Controller implements LoggedController } $em->flush(); + + return $agg_rating; } protected function computeAggregatedRiderRating($old_rating, $new_rating, $r_count, $new_count) @@ -4879,7 +4882,7 @@ class APIController extends Controller implements LoggedController $agg_comp = bcmul($old_rating, $r_count, 2); $rating = bcadd($agg_comp, $new_rating, 2); - + $agg_rating = bcdiv($rating, $new_count, 2); return $agg_rating; diff --git a/src/Controller/CustomerAppAPI/RiderController.php b/src/Controller/CustomerAppAPI/RiderController.php index bc563e09..3d143fbf 100644 --- a/src/Controller/CustomerAppAPI/RiderController.php +++ b/src/Controller/CustomerAppAPI/RiderController.php @@ -232,10 +232,10 @@ class RiderController extends ApiController $this->em->flush(); // need to update or add aggregated rider rating - $this->updateAggregatedRiderRating($rider, $rating_num); + $average_rating = $this->updateAggregatedRiderRating($rider, $rating_num); // TODO: preliminary rating computation on the entity for now - $rider->updateRatingAverage(); + $rider->updateRatingAverage($average_rating); $this->em->persist($rider); $this->em->flush(); @@ -246,6 +246,7 @@ class RiderController extends ApiController protected function updateAggregatedRiderRating($rider, $rating_num) { $rider_id = $rider->getID(); + $agg_rating = 0; // check if rider is in the the aggregated rider rating table $agg_rider_rating = $this->em->getRepository(AggregatedRiderRating::class)->findOneBy(['rider_id' => $rider_id]); @@ -270,7 +271,7 @@ class RiderController extends ApiController $r_rating = $agg_rider_rating->getAggregateRating(); $r_count = $agg_rider_rating->getAggregateCount(); - $new_count = ++$r_count; + $new_count = $r_count + 1; $agg_rating = $this->computeAggregatedRiderRating($r_rating, $rating_num, $r_count, $new_count); @@ -280,6 +281,8 @@ class RiderController extends ApiController } $this->em->flush(); + + return $agg_rating; } protected function computeAggregatedRiderRating($old_rating, $new_rating, $r_count, $new_count) diff --git a/src/Entity/AggregatedRiderRating.php b/src/Entity/AggregatedRiderRating.php index 4b1eb829..6d042359 100644 --- a/src/Entity/AggregatedRiderRating.php +++ b/src/Entity/AggregatedRiderRating.php @@ -28,7 +28,7 @@ class AggregatedRiderRating // average rating of rider /** - * @ORM\Column(type="integer") + * @ORM\Column(type="float") */ protected $aggregate_rating; diff --git a/src/Entity/Rider.php b/src/Entity/Rider.php index fcdb3ed2..2a078983 100644 --- a/src/Entity/Rider.php +++ b/src/Entity/Rider.php @@ -241,15 +241,9 @@ class Rider return $this->image_file; } - public function updateRatingAverage() + public function updateRatingAverage($rating) { - $total = 0; - - foreach ($this->ratings as $rating) { - $total += $rating->getRating(); - } - - $this->setCurrentRating(round($total / $this->ratings->count(), 2)); + $this->setCurrentRating($rating); } public function setCurrentRating($rating)