Skip to content

Commit

Permalink
Merge pull request #14 from mauricerenck/develop
Browse files Browse the repository at this point in the history
imrpovement: stars - return rating
  • Loading branch information
mauricerenck authored Feb 19, 2021
2 parents 16effc5 + 9d90fe8 commit 7cbf689
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
12 changes: 9 additions & 3 deletions assets/stars.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const initStarRatings = () => {
const ratingContainer = document.querySelector('.rate-page__stars');
const stars = ratingContainer.querySelectorAll('.star');
const summary = ratingContainer.querySelector('.sum');
const slug = ratingContainer.getAttribute('data-slug')
const baseUrl = ratingContainer.getAttribute('data-base-url')

Expand Down Expand Up @@ -38,8 +39,9 @@ const initStarRatings = () => {
},
body: JSON.stringify(clientRating)
})
.then(() => {
setUserRating()
.then(response => response.json())
.then((result) => {
setUserRating(result.avgRating)
loading = false
})
.catch(error => {
Expand Down Expand Up @@ -90,7 +92,7 @@ const initStarRatings = () => {
}


const setUserRating = () => {
const setUserRating = (avgRating) => {
const starDomElements = document.querySelectorAll('.rate-page__stars .rating .star');
const currentRating = getRating();

Expand All @@ -106,6 +108,10 @@ const initStarRatings = () => {
element.classList.add('user');
}
});

if (avgRating) {
summary.innerHTML = avgRating.toString()
}
}

stars.forEach((thumb) => {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mauricerenck/ratepage",
"version": "1.2.0",
"version": "1.2.1",
"description": "Let your visitor rate pages",
"type": "kirby-plugin",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ratepage",
"version": "1.2.0",
"version": "1.2.1",
"description": "Let your visitor rate pages",
"main": "index.js",
"author": "Maurice Renck",
Expand Down
32 changes: 29 additions & 3 deletions utils/stars.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ public function writeRating($rating, $prevRating, $targetPage)
$fieldData['star' . $prevRating]--;
}

$fieldData = yaml::encode($fieldData);
$fieldDataString = yaml::encode($fieldData);

$kirby = kirby();
$kirby->impersonate('kirby');
$targetPage->update([
'ratingStar' => $fieldData
'ratingStar' => $fieldDataString
]);

return $fieldData;
}

public function setRating($data)
Expand All @@ -63,13 +65,37 @@ public function setRating($data)
return new Response(json_encode($targetPage), 'application/json', 404);
}

$this->writeRating($rating['rating'], $prevRating, $targetPage['targetPage']);
$ratings = $this->writeRating($rating['rating'], $prevRating, $targetPage['targetPage']);

$response = [
'status' => 'ok',
'message' => 'Rating saved',
'avgRating' => $this->getAvgRating($ratings)
];

return new Response(json_encode($response), 'application/json', 201);
}

public function getAvgRating($ratings)
{
$stars = [];

if (count($ratings) > 0) {
$stars = [
$ratings['star1'] * 1,
$ratings['star2'] * 2,
$ratings['star3'] * 3,
$ratings['star4'] * 4,
$ratings['star5'] * 5
];
}

$totalClicks = array_sum($ratings);
$totalStars = array_sum($stars);

$avgStars = ($totalStars > 0 && $totalClicks > 0) ? $totalStars / $totalClicks : 0;
$avgStarsRounded = (round($avgStars * 4)) / 4;

return $avgStarsRounded;
}
}

0 comments on commit 7cbf689

Please sign in to comment.