Commit 9a71ec1c authored by Tino Goratsch's avatar Tino Goratsch

hardened the rating functionality against possible errors

parent db079173
Pipeline #18858145 passed with stages
in 3 minutes and 44 seconds
<?php
/**
* Copyright (c) by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
* See the LICENSE file at the top-level module directory for licensing details.
*/
namespace ACP3\Core\Helpers\DataGrid\ColumnRenderer;
class RoundNumberColumnRenderer extends AbstractColumnRenderer
{
/**
......
......@@ -27,6 +27,10 @@ class Rate extends AbstractFrontendAction
* @var \ACP3\Modules\ACP3\Share\Model\ShareRatingModel
*/
private $shareRatingModel;
/**
* @var bool|null
*/
private $alredyRated;
/**
* Rate constructor.
......@@ -72,7 +76,10 @@ class Rate extends AbstractFrontendAction
return [
'rating' => \array_merge(
$this->shareRatingsRepository->getRatingStatistics($id),
['already_rated' => $this->hasAlreadyRated($ipAddress, $id)]
[
'share_id' => $id,
'already_rated' => $this->hasAlreadyRated($ipAddress, $id),
]
),
];
}
......@@ -111,6 +118,10 @@ class Rate extends AbstractFrontendAction
*/
private function hasAlreadyRated(string $ipAddress, int $shareId): bool
{
return $this->shareRatingsRepository->hasAlreadyRated($ipAddress, $shareId) === true;
if ($this->alredyRated === null) {
$this->alredyRated = $this->shareRatingsRepository->hasAlreadyRated($ipAddress, $shareId);
}
return $this->alredyRated;
}
}
......@@ -72,6 +72,7 @@ class AddSocialSharingListener
if (!empty($sharingInfo)) {
$sharing['ratings_active'] = ((int) $sharingInfo['ratings_active']) === 1;
$sharing['rating'] = $this->shareRatingsRepository->getRatingStatistics($sharingInfo['id']);
$sharing['rating']['share_id'] = $sharingInfo['id'];
if (((int) $sharingInfo['active']) === 1) {
$sharing['path'] = $this->request->getUriWithoutPages();
......
......@@ -19,7 +19,7 @@ class DataGridRepository extends \ACP3\Core\Model\Repository\DataGridRepository
return [
'main.*',
'AVG(sr.stars) AS average_rating',
'COUNT(sr.id) AS ratings_count'
'COUNT(sr.id) AS ratings_count',
];
}
......
{if $sharing.ratings_active === true && !empty($sharing.rating)}
{if $sharing.ratings_active === true}
<hr>
<div id="rating-wrapper">
{include file="asset:Share/Partials/rating.tpl" rating=$sharing.rating}
......
......@@ -8,7 +8,7 @@
{if isset($rating.already_rated) && $rating.already_rated}
<small class="rating__already-rated">{lang t="share|already_rated"}</small>
{/if}
{$rating_rounded=$rating.average_rating|round:0}
{$rating_rounded=$rating.average_rating|round:0|default:0}
{for $i=5 to 1 step -1}
<label for="rating-star-{$i}"
title="{lang t="share|rate_with_x_stars" args=['%stars%' => $i]}"
......@@ -18,7 +18,7 @@
{/for}
</div>
</form>
{if $rating.total_ratings > 0}
{if isset($rating.total_ratings) && $rating.total_ratings > 0}
<div class="rating-summary"
itemprop="aggregateRating"
itemscope itemtype="http://schema.org/AggregateRating">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment