Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions productcomments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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(
Expand Down
30 changes: 30 additions & 0 deletions src/Repository/ProductCommentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down