diff --git a/productcomments.php b/productcomments.php index 851d063..33c1a65 100644 --- a/productcomments.php +++ b/productcomments.php @@ -974,8 +974,24 @@ private function renderProductCommentsList($product) $commentRepository = $this->get('product_comment_repository'); $averageGrade = $commentRepository->getAverageGrade($product->id, (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE')); $commentsNb = $commentRepository->getCommentsNumber($product->id, (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE')); + $summaryGrades = $commentRepository->getSummaryGrades($product->id, (bool) Configuration::get('PRODUCT_COMMENTS_MODERATE')); $isPostAllowed = $commentRepository->isPostAllowed($product->id, (int) $this->context->cookie->id_customer, (int) $this->context->cookie->id_guest); + $summary = [ + 5 => ['count' => 0, 'percent' => 0], + 4 => ['count' => 0, 'percent' => 0], + 3 => ['count' => 0, 'percent' => 0], + 2 => ['count' => 0, 'percent' => 0], + 1 => ['count' => 0, 'percent' => 0], + ]; + + foreach ($summaryGrades as $grade) { + $summary[(int) $grade['grade']] = [ + 'count' => (int) $grade['count'], + 'percent' => (100 / $commentsNb) * (int) $grade['count'], + ]; + } + /* configure pagination */ $commentsTotalPages = 0; $commentsPerPage = (int) Configuration::get('PRODUCT_COMMENTS_COMMENTS_PER_PAGE'); @@ -986,6 +1002,7 @@ private function renderProductCommentsList($product) $this->context->smarty->assign([ 'post_allowed' => $isPostAllowed, 'usefulness_enabled' => Configuration::get('PRODUCT_COMMENTS_USEFULNESS'), + 'summary' => $summary, 'average_grade' => $averageGrade, 'nb_comments' => $commentsNb, 'list_comments_url' => $this->context->link->getModuleLink( diff --git a/src/Repository/ProductCommentRepository.php b/src/Repository/ProductCommentRepository.php index 88db968..8904c2b 100644 --- a/src/Repository/ProductCommentRepository.php +++ b/src/Repository/ProductCommentRepository.php @@ -219,6 +219,36 @@ public function getAverageGrade($productId, $validatedOnly) return (float) $qb->execute()->fetch(\PDO::FETCH_COLUMN); } + /** + * @param int $productId + * @param bool $validatedOnly + * + * @return array + */ + public function getSummaryGrades($productId, $validatedOnly) + { + /** @var QueryBuilder $qb */ + $qb = $this->connection->createQueryBuilder(); + $qb + ->select('pc.grade, COUNT(*) as count') + ->from($this->databasePrefix . 'product_comment', 'pc') + ->andWhere('pc.id_product = :id_product') + ->andWhere('pc.deleted = :deleted') + ->setParameter('deleted', 0) + ->setParameter('id_product', $productId) + ->groupBy('pc.grade') + ; + + if ($validatedOnly) { + $qb + ->andWhere('pc.validate = :validate') + ->setParameter('validate', 1) + ; + } + + return $qb->execute()->fetchAll(); + } + /** * @param int $langId * @param int $shopId