<< going on | Home | organizing the jME-Week >>

Resetting WP-Polls

Please ignore this entry as production test showed big issues

Use it as hints how to modify WP-Polls but don’t expect that code to work properly!

Known issue: The vote count is not decreased and there seems to be a problem with IE.It works but ajax is haning to saying there is a problem…So actually with that all of this is not much worth! I will try to fix that as fast a possible!

At the moment I’m setting up the website for the jME-Week. It is build upon wordpress. I found a quite capable plugin for voting WP_POLLS. Problem is that once a user voted he is not able to change his vote anymore. My setup is that only users are allowed to vote and the poll should only be visible for logged in users. So I added something like a hook to reset the vote for the current vote again. It is a really dirty hack. So if anyone is using it, it is your own risk!

Ok,…here we go. I assume you installed the plugin already. Go to the “Poll_Templates”-Page in the administration-area:

add this at the beginning of the result footer “Displayed when the user HAS voted”:


<form method=”post”><input type=”submit” value=”reset vote”/><input type=”hidden” name=”poll_id” value=”%POLL_ID%”/><input type=”hidden” name=”reset_poll” value=”1″/></form>

Soo…now comes the real dirty one. Actually I just needed a place where I can catch with global $user_ID the current userID. For some reason I didn’t realise that in wp-polls-widget.php so before search too long in put it into wp-settings.php (You can find it in your main wordpress-directory. usally that is WP)

Add at the end of the file right behind

$wp->init(); // Sets up current user.

// Everything is loaded and initialized.
do_action('init');

the action for processing the reset:

$reset = $_POST['reset_poll'];
if ($reset==’1′)
{
global $wpdb;
global $user_ID;
$poll_id = $_POST['poll_id'];
if ($user_ID>0)
{


$connection=mysql_connect("localhost", "<FILL IN USERNAME>", "<FILL IN PASSWORD>") or die("DB ERROR");
mysql_select_db("<FILLIN DATABASENAME",$connection) or die("DB ERROR");
mysql_query("update wp_pollsa SET polla_votes=polla_votes-1 where polla_aid in (select s.pollip_aid from wp_pollsip s where pollip_userid=$user_ID and pollip_qid=$poll_id)");
mysql_close($connection);
$wpdb->query("delete from $wpdb->pollsip where pollip_userid=$user_ID and pollip_qid=$poll_id");
}
}

In order to disable poll in the sidebar edit the wp-polls-widget.php in the wp/wp-content/plugins/wp-polls/ directory and replace the code at the beginning:


function widget_polls_init() {
get_currentuserinfo();
global $userdata;
if (!function_exists('register_sidebar_widget')||!isset($userdata)) {
return;
}

I know it is really a mess,…but it works for me and I really don’t want to put too much time in to it. AND every minute using PHP makes me much too agressive! I HATE PHP! I HATE IT SOO MUCH!
If someone found a better place how to handle all of this I would be glad for a note.

And don’t forget: Use this on your own risk. It is not well tested….Actually it is only tested twice AND please no requests for modifications! I won’t do it! Look at “I hate PHP“. (If my mother would know what PHP is she would hate it for sure as well…! Isn’t it violating the Geneva conventions? We have to stop it :-) )

Export this post as PDF document  Export this post to PDF document

Social Bookmarks :  Add this post to Slashdot    Add this post to Digg    Add this post to Reddit    Add this post to Delicious    Add this post to Stumble it    Add this post to Google    Add this post to Technorati    Add this post to Bloglines    Add this post to Facebook    Add this post to Furl    Add this post to Windows Live    Add this post to Yahoo!
Home