Flare
04-30-2001, 10:00 PM
As promised a few weeks ago, I finally found some time to make the much needed addition to allow users to change their votes in a poll. After examining the code a bit, I ended up reusing some of the vB code for this ... I hope this isn't a violation of the hacks rules. I read through them, and I think the code re-use is so small and specialized that it will be ok for redistribution via this forum. If it's not, please feel free to edit/lock this thread, I just ask that a moderator let me know what parts are unacceptable.
Here is the hack, it's fairly simple:
Open up poll.php:
At the bottom of the file, find ?>
Just above this, insert this code:
// #################################### Change Vote ##########################################
if ($action=="changevote")
{
// other permissions?
$permissions=getpermissions($forumid);
if (!$permissions[canview] or !$permissions[canvote]) {
show_nopermission();
}
$pollid=verifyid("poll",$pollid);
$pollinfo=$DB_site->query_first("SELECT * FROM poll WHERE pollid='$pollid'");
$voteinfo=$DB_site->query_first("SELECT * FROM pollvote WHERE userid='$bbuserinfo[userid]' AND pollid='$pollid'");
$threadinfo=$DB_site->query_first("SELECT * FROM thread WHERE pollid='$pollid' AND open<>10");
$threadcache[$threadinfo[threadid]]=$threadinfo;
//check if poll is closed
if (!$pollinfo[active] or !$threadinfo[open] or ($pollinfo[dateline]+($pollinfo[timeout]*86400)<time() and $pollinfo[timeout]!=0)){ //poll closed
eval("standarderror(\"".gettemplate("error_pollclosed")."\");");
exit;
}
//check if a user has already voted - abuse management
if (!$uservoteinfo=$DB_site->query_first("SELECT userid FROM pollvote WHERE userid=$bbuserinfo[userid] AND pollid='$pollid'"))
{
//the user has not voted yet
eval("standarderror(\"".gettemplate("error_userhasnotvoted")."\");");
exit;
}
//Error checking complete, lets get the options
$timenow = time();
$DB_site->query("DELETE FROM pollvote WHERE pollid='$pollid' AND userid='$bbuserinfo[userid]' LIMIT 1");
$splitvotes=explode("|||",$pollinfo[votes]);
$splitvotes[$voteinfo[voteoption]-1]--;
$counter=0;
while ($counter<$pollinfo[numberoptions]) {
$votesstring.="|||".intval($splitvotes[$counter]);
$counter++;
}
if (substr($votesstring,0,3)=="|||") {
$votesstring=substr($votesstring,3);
}
$DB_site->query("UPDATE poll SET votes='".addslashes($votesstring)."' WHERE pollid='$pollid'");
//make last reply date == last vote date
if ($updatelastpost){ //option selected in CP
$DB_site->query("UPDATE thread SET lastpost=$timenow WHERE threadid=$threadinfo[threadid]");
}
// redirect
eval("standardredirect(\"".gettemplate("redirect_pollchange")."\",\"showthread.php?s=$session[sessionhash]&threadid=$threadinfo[threadid]\");");
}
Save poll.php and upload it.
In your templates, you will need to add two templates and modify 1 existing one.
First:
Add template " error_userhasnotvoted "
The text of the template should be:
You have not voted on this thread, so you can not change your vote.
Second:
Add template " redirect_pollchange "
The text of the template should be:
You have deleted your vote. You will be taken to the poll, where you will be allowed to revote.
Lastly:
Edit your "showthread_pollresults_voted" template.
Change the text to:
You have already voted on this poll!
<BR>If you wish to change your vote, click <a href="poll.php?action=changevote&pollid=$pollinfo[pollid]">here</a>.
That's it...
Now your users will have the option to erase thier vote and revote. Not <i>quite</i> as elegant as allowing them to actually revote, but it will suffice for now ... at least for me. Someone feel free to expand on this if they want something a bit more robust.
Thanks, and good luck!
-Flare
Here is the hack, it's fairly simple:
Open up poll.php:
At the bottom of the file, find ?>
Just above this, insert this code:
// #################################### Change Vote ##########################################
if ($action=="changevote")
{
// other permissions?
$permissions=getpermissions($forumid);
if (!$permissions[canview] or !$permissions[canvote]) {
show_nopermission();
}
$pollid=verifyid("poll",$pollid);
$pollinfo=$DB_site->query_first("SELECT * FROM poll WHERE pollid='$pollid'");
$voteinfo=$DB_site->query_first("SELECT * FROM pollvote WHERE userid='$bbuserinfo[userid]' AND pollid='$pollid'");
$threadinfo=$DB_site->query_first("SELECT * FROM thread WHERE pollid='$pollid' AND open<>10");
$threadcache[$threadinfo[threadid]]=$threadinfo;
//check if poll is closed
if (!$pollinfo[active] or !$threadinfo[open] or ($pollinfo[dateline]+($pollinfo[timeout]*86400)<time() and $pollinfo[timeout]!=0)){ //poll closed
eval("standarderror(\"".gettemplate("error_pollclosed")."\");");
exit;
}
//check if a user has already voted - abuse management
if (!$uservoteinfo=$DB_site->query_first("SELECT userid FROM pollvote WHERE userid=$bbuserinfo[userid] AND pollid='$pollid'"))
{
//the user has not voted yet
eval("standarderror(\"".gettemplate("error_userhasnotvoted")."\");");
exit;
}
//Error checking complete, lets get the options
$timenow = time();
$DB_site->query("DELETE FROM pollvote WHERE pollid='$pollid' AND userid='$bbuserinfo[userid]' LIMIT 1");
$splitvotes=explode("|||",$pollinfo[votes]);
$splitvotes[$voteinfo[voteoption]-1]--;
$counter=0;
while ($counter<$pollinfo[numberoptions]) {
$votesstring.="|||".intval($splitvotes[$counter]);
$counter++;
}
if (substr($votesstring,0,3)=="|||") {
$votesstring=substr($votesstring,3);
}
$DB_site->query("UPDATE poll SET votes='".addslashes($votesstring)."' WHERE pollid='$pollid'");
//make last reply date == last vote date
if ($updatelastpost){ //option selected in CP
$DB_site->query("UPDATE thread SET lastpost=$timenow WHERE threadid=$threadinfo[threadid]");
}
// redirect
eval("standardredirect(\"".gettemplate("redirect_pollchange")."\",\"showthread.php?s=$session[sessionhash]&threadid=$threadinfo[threadid]\");");
}
Save poll.php and upload it.
In your templates, you will need to add two templates and modify 1 existing one.
First:
Add template " error_userhasnotvoted "
The text of the template should be:
You have not voted on this thread, so you can not change your vote.
Second:
Add template " redirect_pollchange "
The text of the template should be:
You have deleted your vote. You will be taken to the poll, where you will be allowed to revote.
Lastly:
Edit your "showthread_pollresults_voted" template.
Change the text to:
You have already voted on this poll!
<BR>If you wish to change your vote, click <a href="poll.php?action=changevote&pollid=$pollinfo[pollid]">here</a>.
That's it...
Now your users will have the option to erase thier vote and revote. Not <i>quite</i> as elegant as allowing them to actually revote, but it will suffice for now ... at least for me. Someone feel free to expand on this if they want something a bit more robust.
Thanks, and good luck!
-Flare