How to enable anonymous voting in my module
mercmobily - June 5, 2009 - 08:29
| Project: | Voting API |
| Version: | 6.x-2.x-dev |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
Hi,
I am the author of Extra Voting Forms and would like to implement anonymous voting "the right way" using VotingAPI.
At the moment, when a user votes, I just do:
// Give the actual vote using the Voting API
$votes = array();
$votes['content_type'] = extra_voting_forms_c($o->sk_type);
$votes['content_id'] = $o->sk_id;
$votes['uid'] = $account->uid;
$votes['value_type'] = 'points';
$votes['value'] = $vote;
$result = votingapi_set_votes($votes);And then I load the total for that node:
// Get the current number of votes
$criteria = array();
$criteria['content_type'] = extra_voting_forms_c($o->sk_type);
$criteria['content_id'] = $o->sk_id;
$criteria['tag'] = 'vote';
$criteria['value_type'] = 'points';
$criteria['function'] = 'sum';
$r = votingapi_select_results($criteria);
$r = (int)$r[0]['value'];When I show the form, I go:
// ****************************************************************
// ** SET THE BASIC VARIABLES
// ****************************************************************
// This will get the record. If != NULL, then a vote (even 0) WAS cast
$criteria = array();
$criteria['content_type'] = extra_voting_forms_c($o->sk_type);
$criteria['content_id'] = $o->sk_id;
$criteria['value_type'] = 'points';
$criteria['tag'] = 'vote';
$criteria['uid'] = $user->uid;
$existing_vote_cast = votingapi_select_votes($criteria);Now... I am trying to work out what the "right" way to add anonymous voting so that it's the "VotingAPI" way.
I am a little confused by the code in fivestar, which I tried to emulate... unsuccessfully.
So, what are the "basic requirement" to get anonymous vote happening?
I expected to just allow votes from "0", and hoped that VotingAPI would do all the hard stuff to figure out if a vote from from anonymous... but that didn't quite work :D
Please help!
Merc.

#1
Looking at Votingapi code only, not testing
<?php
// Give the actual vote using the Voting API
$votes = array();
$votes['content_type'] = extra_voting_forms_c($o->sk_type);
$votes['content_id'] = $o->sk_id;
$votes['uid'] = $account->uid;
$votes['value_type'] = 'points';
$votes['value'] = $vote;
if ($account->uid == 0) {
// delete votes where ip_address() is the same and the user is 0
$result = votingapi_set_votes($votes, array('vote_source' => ip_address(), 'uid' => 0));
}
else {
$result = votingapi_set_votes($votes);
}
?>
edit: on your local machine, where vote_source is always the same, it won't appear to work.
To test I would temporary modify ip_address() function to return random stuff or deploy to a testing server. There is a anon testing window that will save ya there and its default value is an hour. On the flip side, if you don't want to delete the anon vote cast by the same ip, pass in an empty array instead (also note, empty array() is decidedly different then NULL and VotingAPI does different behavior).#2
Hi,
Thank you for your help!
OK, I am following you up to this point:
> There is a anon testing window that will save ya there and its default value is an hour.
OK... so votes from the same IP address within 1 hour will be considered as coming from the same anon user?
> On the flip side, if you don't want to delete the anon vote cast by the same ip, pass in an empty array instead
> (also note, empty array() is decidedly different then NULL and VotingAPI does different behavior).
This is where I lost you totally :D Sorry...! What do you mean?
Merc.
#3
Hi,
And... what about the "loading" side of the story? When I load the existing vote (which will influence what the voting form displays), do I have to take special precautions? (See: load filtering by ip_address() for example...?)
Merc
#4
subscribing!!
#5
subs