Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unrate an item #26

Open
wants to merge 1 commit into
base: v2-dev
Choose a base branch
from
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
18 changes: 12 additions & 6 deletions src/resources/js/starratings.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ var starRatings = {
var errorReturned = (typeof results === 'string' || results instanceof String);
// If no error message was returned
if (!errorReturned) {
if (starRatings.devMode) {
console.log(results.unrated ? 'Item unrated' : 'New rating');
}
var i; // Counter
var currentPosition;
var currentPosition = 0;
// Remove existing rating
for (i = 0; i < elementStars.length; i++) {
starRatings._removeClass(elementStars[i], 'sr-avg-rating');
Expand All @@ -45,12 +48,15 @@ var starRatings = {
starRatings._removeClass(elementStars[i], 'sr-ratable');
}
}
// Adds new rating
for (i = 0; i < value; i++) {
elementStars[i].innerHTML = starRatings.starIcons['4/4'];
starRatings._addClass(elementStars[i], 'sr-user-rating');
currentPosition = (i+1);
if (!results.unrated) {
// Adds new rating
for (i = 0; i < value; i++) {
elementStars[i].innerHTML = starRatings.starIcons['4/4'];
starRatings._addClass(elementStars[i], 'sr-user-rating');
currentPosition = (i + 1);
}
}

// Fills remaining stars
for (i = currentPosition; i < elementStars.length; i++) {
elementStars[i].innerHTML = starRatings.starIcons['0/4'];
Expand Down
30 changes: 23 additions & 7 deletions src/services/Rate.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,23 @@ public function rate(int $elementId, ?string $key, int $rating, int|User|null $u
'rating' => $rating,
'changedFrom' => $oldRating,
'userId' => $userId,
'unrated' => false
];

$message = null;

// Trigger event before a rating is cast
if (Event::hasHandlers(StarRatings::class, StarRatings::EVENT_BEFORE_RATE)) {
Event::trigger(StarRatings::class, StarRatings::EVENT_BEFORE_RATE, new RateEvent($returnData));
}

// Cast element rating, get potential error message.
$message = $this->_rateElement($elementId, $key, $rating, $userId, $changed, $oldRating);
if (!$changed && $oldRating) {
$this->_unrateElement($elementId, $key, $userId, $oldRating);
$returnData['unrated'] = true;
} else {
// Cast element rating. Get potential error message.
$message = $this->_rateElement($elementId, $key, $rating, $userId, $changed, $oldRating);
}

// Trigger event after a rating is cast
if (Event::hasHandlers(StarRatings::class, StarRatings::EVENT_AFTER_RATE)) {
Expand Down Expand Up @@ -249,15 +257,23 @@ private function _rateElement(int $elementId, ?string $key, int $rating, int $us
}

/**
* Update the logged-in user history in the database.
* Internally remove a rating.
*
* @param int $elementId
* @param string|null $key
* @param int $rating
* @param int|null $userId
* @return bool
* @param int $userId
* @param int|null $oldRating
*/
private function _updateUserHistoryDatabase(int $elementId, ?string $key, int $rating, ?int $userId): bool
private function _unrateElement(int $elementId, ?string $key, int $userId, ?int $oldRating)
{
$this->_removeRatingFromDb($elementId, $key, $userId);
$this->_removeRatingFromCookie($elementId, $key);
$this->_updateElementAvgRating($elementId, $key, $oldRating, true);
$this->_updateRatingLog($elementId, $key, 0, $userId, true);
}

//
private function _updateUserHistoryDatabase($elementId, $key, $rating, $userId)
{
// If user is not logged in, return false
if (!$userId) {
Expand Down