Commit 056d8a19 authored by Luke Ward's avatar Luke Ward
Browse files

Allow karma to be undone without voting the other way

parent 552ebc66
......@@ -173,7 +173,7 @@
}
}
public function karma($up = true) {
public function karma($up = true, $cancel = false) {
$DtW = \dtw\DtW::getInstance();
if (!$DtW->user->isAuth()) {
......@@ -189,17 +189,30 @@
throw new \Exception('You don\'t have enough reputation to down vote');
}
// Insert or update
$stmt = \dtw\DtW::$db->prepare('
INSERT INTO forum_karma (`post_id`, `user_id`, `vote`)
VALUES (:post_id, :user_id, :vote)
ON DUPLICATE KEY UPDATE `vote` = :vote
');
$stmt->execute(array(
':post_id' => $this->id,
':user_id' => $DtW->user->id,
':vote' => $up ? 'up' : 'down',
));
if (!$cancel) {
// Insert or update
$stmt = \dtw\DtW::$db->prepare('
INSERT INTO forum_karma (`post_id`, `user_id`, `vote`)
VALUES (:post_id, :user_id, :vote)
ON DUPLICATE KEY UPDATE `vote` = :vote
');
$stmt->execute(array(
':post_id' => $this->id,
':user_id' => $DtW->user->id,
':vote' => $up ? 'up' : 'down',
));
} else {
// Remove vote
$stmt = \dtw\DtW::$db->prepare('
DELETE FROM forum_karma
WHERE `post_id` = :post_id AND `user_id` = :user_id AND `vote` = :vote
');
$stmt->execute(array(
':post_id' => $this->id,
':user_id' => $DtW->user->id,
':vote' => $up ? 'up' : 'down',
));
}
// Get new total
$this->updateKarma();
......@@ -213,7 +226,11 @@
}
if ($stmt->rowCount() === 0) {
throw new \Exception('Karma had already been awarded for this post');
if (!$cancel) {
throw new \Exception('Karma had already been awarded for this post');
} else {
throw new \Exception('Karma has not been awarded for this post');
}
}
}
......
......@@ -620,7 +620,7 @@
$thread = $app->DtW->discussions->getThread($request->thread);
$post = $app->DtW->discussions->getPost($thread->id, $request->post);
$post->karma(isset($_GET['up']));
$post->karma(isset($_GET['up']), isset($_GET['cancel']));
\dtw\utils\Flash::add('Karma awarded', 'success');
} catch (Exception $e) {
......
......@@ -288,7 +288,6 @@
&.discussion-thread-message-karma--up {
[data-karma=up] {
opacity: 0.3;
cursor: default;
}
h3 {
......@@ -299,7 +298,6 @@
&.discussion-thread-message-karma--down {
[data-karma=down] {
opacity: 0.3;
cursor: default;
}
h3 {
......
......@@ -256,40 +256,46 @@ $(function() {
if (action === 'up') {
if (previousAction === 'up') {
return;
}
newValue++;
if (previousAction === 'down') {
$parent.removeClass('discussion-thread-message-karma--up');
action = 'cancel';
newValue--;
} else {
newValue++;
if (previousAction === 'down') {
newValue++;
}
$parent.removeClass('discussion-thread-message-karma--down');
$parent.addClass('discussion-thread-message-karma--up');
}
$this.siblings('h3').text(newValue);
$parent.removeClass('discussion-thread-message-karma--down');
$parent.addClass('discussion-thread-message-karma--up');
} else if (action === 'down') {
if (previousAction === 'down') {
return;
}
newValue--;
if (previousAction === 'up') {
$parent.removeClass('discussion-thread-message-karma--down');
action = 'cancel';
newValue++;
} else {
newValue--;
if (previousAction === 'up') {
newValue--;
}
$parent.removeClass('discussion-thread-message-karma--up');
$parent.addClass('discussion-thread-message-karma--down');
}
$this.siblings('h3').text(newValue);
$parent.removeClass('discussion-thread-message-karma--up');
$parent.addClass('discussion-thread-message-karma--down');
} else {
return;
}
$this.addClass('active');
var url = $(this).attr('href') + (action === 'cancel' ? '&cancel' : '');
$.ajax({
url: $(this).attr('href'),
url: url,
dataType: 'json',
type: 'POST',
data: {
......@@ -302,9 +308,11 @@ $(function() {
// reset
$this.siblings('h3').text(currentValue);
$parent.removeClass('discussion-thread-message-karma--' + action);
if (previousAction) {
$parent.addClass('discussion-thread-message-karma--' + previousAction);
if (action !== 'cancel') {
$parent.removeClass('discussion-thread-message-karma--' + action);
if (previousAction) {
$parent.addClass('discussion-thread-message-karma--' + previousAction);
}
}
}
});
......
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