Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
9
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Open sidebar
Defend the Web
Website
Commits
056d8a19
Commit
056d8a19
authored
Nov 21, 2019
by
Luke Ward
Browse files
Allow karma to be undone without voting the other way
parent
552ebc66
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
59 additions
and
36 deletions
+59
-36
src/lib/discussion/Post.php
src/lib/discussion/Post.php
+30
-13
src/routes/discussion.php
src/routes/discussion.php
+1
-1
src/www/css/_discussion.scss
src/www/css/_discussion.scss
+0
-2
src/www/js/_discussions.js
src/www/js/_discussions.js
+28
-20
No files found.
src/lib/discussion/Post.php
View file @
056d8a19
...
...
@@ -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'
);
}
}
}
...
...
src/routes/discussion.php
View file @
056d8a19
...
...
@@ -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
)
{
...
...
src/www/css/_discussion.scss
View file @
056d8a19
...
...
@@ -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
{
...
...
src/www/js/_discussions.js
View file @
056d8a19
...
...
@@ -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
);
}
}
}
});
...
...
Luke Ward
@0x6C77
mentioned in issue
#1 (closed)
·
Nov 21, 2019
mentioned in issue
#1 (closed)
mentioned in issue #1
Toggle commit list
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment