diff --git a/src/models/Product.php b/src/models/Product.php index 1c687bc..d33ed78 100644 --- a/src/models/Product.php +++ b/src/models/Product.php @@ -85,7 +85,7 @@ public static function getCategories(): array return []; } - $callback = fn($obj): string => $obj->category; + $callback = fn ($obj): string => $obj->category; return array_map($callback, $result); } @@ -393,11 +393,18 @@ public function getRatingDistribution(): array { // Query the database to get the percentage distribution of ratings $query = <<< EOL - SELECT rating, - COUNT(*) * 100.0 / (SELECT COUNT(*) FROM review WHERE product_id = :product_id) AS percentage - FROM review - WHERE product_id = :product_id - GROUP BY rating + SELECT rating, + COUNT(*) * 10.0 / ( + SELECT COUNT(*) + FROM order_product op + JOIN `order` o ON op.order_id = o.order_id + WHERE op.product_id = :product_id + ) AS percentage + FROM review r + JOIN `order` o ON r.client_id = o.client_id + JOIN order_product op ON op.order_id = o.order_id + WHERE op.product_id = :product_id + GROUP BY rating; EOL; $params = ['product_id' => $this->product_id]; @@ -433,4 +440,4 @@ public function updateProduct(array $newProductData): bool return $this->update($newProductData, ['product_id' => $this->product_id], $this->table); } -} \ No newline at end of file +}