Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Markus Hermann
wp-polls
Commits
986f47aa
Commit
986f47aa
authored
Oct 30, 2020
by
Markus Hermann
🏈
Browse files
wip: Voting works
parent
db5c6e6d
Changes
2
Hide whitespace changes
Inline
Side-by-side
js/hpolls.js
View file @
986f47aa
...
...
@@ -5,7 +5,7 @@ class Poll {
* @param {object} pollContainer
*/
constructor
(
pollContainer
)
{
this
.
endpoint
_url
=
"
https://testwww.digll-hessen.local/wp-admin/admin-ajax.php
"
;
this
.
endpoint
URL
=
"
https://testwww.digll-hessen.local/wp-admin/admin-ajax.php
"
;
this
.
container
=
pollContainer
;
this
.
id
=
jQuery
(
this
.
container
).
attr
(
"
data-poll-id
"
);
this
.
nonce
=
jQuery
(
this
.
container
).
attr
(
"
data-poll-nonce
"
);
...
...
@@ -67,7 +67,7 @@ class Poll {
*/
showResult
()
{
jQuery
.
get
({
url
:
this
.
endpoint
_url
,
url
:
this
.
endpoint
URL
,
data
:
`action=hpolls&poll_id=
${
this
.
id
}
&data=results&poll_
${
this
.
id
}
-nonce=
${
this
.
nonce
}
`
,
dataType
:
"
json
"
}).
success
(
function
(
data
)
{
...
...
@@ -184,7 +184,7 @@ class Poll {
*/
showVote
()
{
jQuery
.
get
({
url
:
this
.
endpoint
_url
,
url
:
this
.
endpoint
URL
,
data
:
`action=hpolls&poll_id=
${
this
.
id
}
&data=vote&poll_
${
this
.
id
}
-nonce=
${
this
.
nonce
}
`
,
dataType
:
"
json
"
}).
success
(
function
(
data
)
{
...
...
@@ -201,8 +201,12 @@ class Poll {
*/
showVoteCallback
(
data
)
{
var
options
=
""
;
this
.
hasMultipleVotesOptions
=
false
;
if
(
data
.
maxVotes
>
1
)
{
options
=
this
.
multipleVotesOptions
(
data
.
id
,
data
.
options
);
this
.
hasMultipleVotesOptions
=
true
;
}
else
{
options
=
this
.
singleVoteOptions
(
data
.
id
,
data
.
options
);
}
...
...
@@ -220,6 +224,7 @@ class Poll {
jQuery
(
this
.
resultView
).
hide
();
jQuery
(
this
.
voteView
).
html
(
template
);
this
.
addCastVoteButton
();
this
.
addResultButton
();
jQuery
(
this
.
voteView
).
show
();
}
...
...
@@ -295,14 +300,124 @@ class Poll {
<button class="btn btn-primary" data-poll-id="
${
this
.
id
}
">Ergebniss anzeigen!</button>
`
);
let
button
=
jQuery
(
this
.
voteView
).
children
(
"
:
button
"
);
let
button
=
jQuery
(
this
.
voteView
).
find
(
"
button
"
);
jQuery
(
button
).
click
(
function
(
sender
)
{
let
pollId
=
jQuery
(
sender
.
target
).
attr
(
"
data-poll-id
"
);
document
.
pollController
.
polls
[
pollId
].
viewMode
=
"
result
"
;
})
});
}
/**
* Append the button to cast your vote.
*/
addCastVoteButton
()
{
jQuery
(
this
.
voteView
).
children
(
"
.panel-body
"
).
append
(
`
<input type="button" class="btn btn-primary" data-poll-id="
${
this
.
id
}
" value="Abstimmen!">
`
);
let
button
=
jQuery
(
this
.
voteView
).
find
(
"
input[type='button']
"
);
jQuery
(
button
).
click
(
function
(
event
)
{
let
pollId
=
jQuery
(
event
.
target
).
attr
(
"
data-poll-id
"
);
document
.
pollController
.
polls
[
pollId
].
castVoteCallback
();
});
}
/**
* Return the button to cast a vote.
*/
get
castVoteButton
()
{
return
jQuery
(
this
.
voteView
).
find
(
"
input[type='button']
"
);
}
/**
* Cast the vote
*/
castVoteCallback
()
{
// Disable the inputs to prevent multiple vote calls
jQuery
(
this
.
castVoteButton
).
prop
(
"
disabled
"
,
"
disabled
"
);
jQuery
(
`input[name='poll-
${
this
.
id
}
']`
).
prop
(
"
disabled
"
,
"
disabled
"
);
this
.
removeAlert
();
// Get the chosen values
if
(
this
.
hasMultipleVotesOptions
)
{
var
chosenOptions
=
[];
let
votes
=
jQuery
(
this
.
voteView
).
find
(
'
input:checkbox
'
).
filter
(
'
:checked
'
);
for
(
var
i
=
0
;
i
<
votes
.
length
;
i
++
)
{
chosenOptions
.
push
(
jQuery
(
votes
[
i
]).
val
());
}
let
votesString
=
chosenOptions
.
join
();
console
.
log
(
votesString
);
this
.
submitVote
(
votesString
);
}
else
{
let
chosenValue
=
jQuery
(
`[name='poll-
${
this
.
id
}
']:checked`
).
val
();
if
(
chosenValue
===
undefined
)
{
this
.
handleEmptyVote
();
}
this
.
submitVote
(
chosenValue
);
}
}
submitVote
(
vote
)
{
jQuery
.
post
({
url
:
this
.
endpointURL
,
data
:
`action=hpolls&data=process-votes&poll_id=
${
this
.
id
}
&poll_
${
this
.
id
}
-nonce=
${
this
.
nonce
}
&answer_ids=
${
vote
}
`
,
cache
:
false
,
dataType
:
"
json
"
}).
success
(
function
(
data
)
{
let
pollId
=
data
.
id
;
document
.
pollController
.
polls
[
pollId
].
submitVoteCallback
(
data
);
}).
fail
(
function
()
{
console
.
log
(
"
Fetching the results failed
"
);
});
}
/**
* FOO
* @param {object} data - The response from the server
*/
submitVoteCallback
(
data
)
{
}
/**
* When the cast vote was empty, enable the input again an show a warning to the user.
*/
handleEmptyVote
()
{
jQuery
(
this
.
castVoteButton
).
prop
(
"
disabled
"
,
""
);
jQuery
(
`input[name='poll-
${
this
.
id
}
']`
).
prop
(
"
disabled
"
,
""
);
this
.
addAlert
(
"
warning
"
,
"
Sie müssen eine Option auswählen um abzustimmen.
"
);
}
/**
* Add an alert to the vote view.
* @param {string} type - The type of the alert
* @param {string} message - The message of the alert
*/
addAlert
(
type
,
message
)
{
if
(
jQuery
(
this
.
voteView
).
find
(
"
.alert
"
).
length
===
0
)
{
jQuery
(
this
.
voteView
).
find
(
"
.panel-body
"
).
prepend
(
`
<div class="alert alert-
${
type
}
" role="alert">
${
message
}
</div>
`
);
}
}
/**
* Remove the alert from the vote view.
*/
removeAlert
()
{
if
(
jQuery
(
this
.
voteView
).
find
(
"
.alert
"
).
length
>
0
)
{
jQuery
(
this
.
voteView
).
find
(
"
.alert
"
).
remove
();
}
}
}
class
PollController
{
...
...
wp-polls.php
View file @
986f47aa
...
...
@@ -2010,7 +2010,7 @@ function highchartsResultShortcode($atts) {
add_action
(
'wp_ajax_hpolls'
,
'pollAPIEndpoint'
);
add_action
(
'wp_ajax_nopriv_hpolls'
,
'pollAPIEndpoint'
);
function
pollAPIEndpoint
()
{
if
(
isset
(
$_REQUEST
[
'action'
])
&&
sanitize_key
(
$_REQUEST
[
'action'
])
===
'hpolls'
)
{
if
(
isset
(
$_REQUEST
[
'action'
])
&&
sanitize_key
(
$_REQUEST
[
'action'
])
===
'hpolls'
)
{
header
(
sprintf
(
'Content-Type: text/html; charset=%s'
,
get_option
(
'blog_charset'
))
);
...
...
@@ -2036,6 +2036,11 @@ function pollAPIEndpoint() {
case
"vote"
:
echo
poll_vote
(
$poll_id
);
break
;
case
"process-votes"
:
$answers
=
sanitize_text_field
(
$_REQUEST
[
'answer_ids'
]);
$answers
=
explode
(
","
,
$answers
);
echo
process_vote
(
$poll_id
,
$answers
);
break
;
}
}
exit
();
...
...
@@ -2100,7 +2105,7 @@ function poll_vote($poll_id) {
$question
=
$question_result
->
question
;
$poll_open
=
((
int
)
$question_result
->
is_active
===
1
)
?
true
:
false
;
$max_votes
=
(
int
)
$question
->
max_votes
;
$max_votes
=
(
int
)
$question
_result
->
max_votes
;
$options
=
[];
...
...
@@ -2118,6 +2123,8 @@ function poll_vote($poll_id) {
];
}
$nonce
=
wp_create_nonce
(
sprintf
(
'poll_%s-nonce'
,
$poll_id
));
return
json_encode
([
"id"
=>
$poll_id
,
"pollOpen"
=>
$poll_open
,
...
...
@@ -2128,6 +2135,149 @@ function poll_vote($poll_id) {
]);
}
// function vote_poll_process($poll_id, $poll_aid_array = [])
function
process_vote
(
$poll_id
,
$answers
=
[])
{
global
$wpdb
,
$user_identity
,
$user_ID
;
if
(
$poll_id
===
0
)
{
throw
new
InvalidArgumentException
(
sprintf
(
__
(
'Invalid Poll ID. Poll ID #%s'
,
'wp-polls'
),
$poll_id
));
}
$polla_aids
=
$wpdb
->
get_col
(
$wpdb
->
prepare
(
"SELECT polla_aid FROM
$wpdb->pollsa
WHERE polla_qid = %d"
,
$poll_id
)
);
error_log
(
print_r
(
$answers
,
true
));
error_log
(
print_r
(
$polla_aids
,
true
));
error_log
(
print_r
(
count
(
array_intersect
(
$answers
,
$polla_aids
)),
true
));
error_log
(
print_r
(
count
(
$answers
),
true
));
$submittedAnswersInvalid
=
count
(
array_intersect
(
$answers
,
$polla_aids
))
!==
count
(
$answers
);
if
(
$submittedAnswersInvalid
)
{
throw
new
InvalidArgumentException
(
sprintf
(
__
(
'Invalid Answer to Poll ID #%s'
,
'wp-polls'
),
$poll_id
));
}
if
(
!
check_allowtovote
())
{
throw
new
InvalidArgumentException
(
sprintf
(
__
(
'User is not allowed to vote for Poll ID #%s'
,
'wp-polls'
),
$poll_id
));
}
if
(
empty
(
$answers
))
{
throw
new
InvalidArgumentException
(
sprintf
(
__
(
'No anwsers given for Poll ID #%s'
,
'wp-polls'
),
$poll_id
));
}
$is_poll_open
=
(
int
)
$wpdb
->
get_var
(
$wpdb
->
prepare
(
"SELECT COUNT(*) FROM
$wpdb->pollsq
WHERE pollq_id = %d AND pollq_active = 1"
,
$poll_id
)
);
if
(
$is_poll_open
===
0
)
{
throw
new
InvalidArgumentException
(
sprintf
(
__
(
'Poll ID #%s is closed'
,
'wp-polls'
),
$poll_id
));
}
$check_voted
=
check_voted
(
$poll_id
);
if
(
!
empty
(
$check_voted
)
)
{
throw
new
InvalidArgumentException
(
sprintf
(
__
(
'You Had Already Voted For This Poll. Poll ID #%s'
,
'wp-polls'
),
$poll_id
));
}
if
(
!
empty
(
$user_identity
))
{
$pollip_user
=
$user_identity
;
}
elseif
(
!
empty
(
$_COOKIE
[
'comment_author_'
.
COOKIEHASH
]))
{
$pollip_user
=
$_COOKIE
[
'comment_author_'
.
COOKIEHASH
];
}
else
{
$pollip_user
=
__
(
'Guest'
,
'wp-polls'
);
}
$pollip_user
=
sanitize_text_field
(
$pollip_user
);
$pollip_userid
=
$user_ID
;
$pollip_ip
=
poll_get_ipaddress
();
$pollip_host
=
poll_get_hostname
();
$pollip_timestamp
=
current_time
(
'timestamp'
);
$poll_logging_method
=
(
int
)
get_option
(
'poll_logging_method'
);
// Only Create Cookie If User Choose Logging Method 1 Or 3
if
(
$poll_logging_method
===
1
||
$poll_logging_method
===
3
)
{
$cookie_expiry
=
(
int
)
get_option
(
'poll_cookielog_expiry'
);
if
(
$cookie_expiry
===
0
)
{
$cookie_expiry
=
YEAR_IN_SECONDS
;
}
setcookie
(
'voted_'
.
$poll_id
,
implode
(
','
,
$poll_aid_array
),
$pollip_timestamp
+
$cookie_expiry
,
apply_filters
(
'wp_polls_cookiepath'
,
SITECOOKIEPATH
)
);
}
// Update the vote count for the answers
$updated_answers
=
[];
foreach
(
$answers
as
$answer
)
{
$update_votes
=
$wpdb
->
query
(
"UPDATE
$wpdb->pollsa
SET polla_votes = (polla_votes + 1) WHERE polla_qid =
$poll_id
AND polla_aid =
$answer
"
);
if
(
!
$update_votes
)
{
$updated_answers
[]
=
$answer
;
}
}
// Update the total vote count
$vote_q
=
$wpdb
->
query
(
sprintf
(
"UPDATE
$wpdb->pollsq
SET pollq_totalvotes = (pollq_totalvotes+%s), pollq_totalvoters = (pollq_totalvoters + 1) WHERE pollq_id =
$poll_id
AND pollq_active = 1"
,
count
(
$updated_answers
)
)
);
if
(
!
$vote_q
)
{
throw
new
InvalidArgumentException
(
sprintf
(
__
(
'Unable To Update Poll Total Votes And Poll Total Voters. Poll ID #%s'
,
'wp-polls'
),
$poll_id
));
}
foreach
(
$updated_answers
as
$answer
)
{
// Log Ratings In DB If User Choose Logging Method 2, 3 or 4
if
(
$poll_logging_method
>
1
){
$wpdb
->
insert
(
$wpdb
->
pollsip
,
[
'pollip_qid'
=>
$poll_id
,
'pollip_aid'
=>
$answer
,
'pollip_ip'
=>
$pollip_ip
,
'pollip_host'
=>
$pollip_host
,
'pollip_timestamp'
=>
$pollip_timestamp
,
'pollip_user'
=>
$pollip_user
,
'pollip_userid'
=>
$pollip_userid
],
[
'%s'
,
'%s'
,
'%s'
,
'%s'
,
'%s'
,
'%s'
,
'%d'
]
);
}
}
return
json_encode
([
"id"
=>
$poll_id
,
"status"
=>
"success"
]);
}
function
displayHighchartsPollVote
(
$poll_id
,
$user_voted
=
[],
$display_loading
=
true
)
{
$nonce
=
wp_create_nonce
(
sprintf
(
'poll_%s-nonce'
,
$poll_id
));
...
...
Write
Preview
Supports
Markdown
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