Skip to content

Instantly share code, notes, and snippets.

@Cameron-D
Created July 7, 2014 11:06
Show Gist options
  • Save Cameron-D/fb72901018ed78d72681 to your computer and use it in GitHub Desktop.
Save Cameron-D/fb72901018ed78d72681 to your computer and use it in GitHub Desktop.
Recount MyBB Polls
<?php
if(!defined("IN_MYBB"))
{
die("This file cannot be accessed directly.");
}
function pollrecount_info()
{
return array(
"name" => "Poll Recount",
"description" => "Provides an option to recount poll votes for an individual poll.",
"website" => "",
"author" => "Cameron:D",
"authorsite" => "http://community.mybb.com/user-24685.html",
"version" => "1.0",
"compatibility" => "16*",
);
}
function pollrecount_activate()
{
require MYBB_ROOT.'/inc/adminfunctions_templates.php';
find_replace_templatesets("showthread_poll_results", '#'.preg_quote('{$edit_poll}').'#', '{$edit_poll}{$recount_poll}');
}
function pollrecount_deactivate()
{
require MYBB_ROOT.'/inc/adminfunctions_templates.php';
find_replace_templatesets("showthread_poll_results", '#'.preg_quote('{$recount_poll}').'#', '');
}
$plugins->add_hook("showthread_start", "pollrecount_showthread_start");
$plugins->add_hook("misc_start", "pollrecount_misc_start");
function pollrecount_showthread_start()
{
global $mybb, $tid, $fid, $recount_poll;
if(is_moderator($fid, 'caneditposts'))
{
$recount_poll = " | <a href=\"misc.php?action=pollrecount&amp;tid={$tid}&amp;my_post_key={$mybb->post_code}\">Recount Votes</a>";
}
else
{
$recount_poll = '';
}
}
function pollrecount_misc_start()
{
global $mybb, $db;
verify_post_check($mybb->input['my_post_key']);
$tid = intval($mybb->input['tid']);
$query = $db->simple_select("threads", "fid", "tid={$tid}");
$thread = $db->fetch_array($query);
if(!is_moderator($thread['fid'], 'caneditposts'))
{
error_no_permission();
}
if($mybb->input['action'] == "pollrecount")
{
$query = $db->simple_select("polls", "pid,options", "tid={$tid}");
if($db->num_rows($query) == 0)
{
error($lang->error_invalidpoll);
}
$poll = $db->fetch_array($query);
$totaloptions = count(explode("||~|~||", $poll['options']));
$votes = array();
// The -1 here (and below) is due to the fact that vote options start at 1;
for ($i=0; $i<$totaloptions-1; $i++)
{
$votes[$i] = 0;
}
$query = $db->simple_select("pollvotes", "voteoption", "pid={$poll['pid']}");
while ($vote = $db->fetch_array($query))
{
$votes[$vote['voteoption']-1] += 1;
}
$votestring = implode($votes, "||~|~||");
$update = array("votes" => $votestring);
$db->update_query("polls", $update, "pid={$poll['pid']}");
redirect(get_thread_link($tid), "Poll votes have been recounted");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment