$topic,
)
);
// Redirect to the boardindex if no valid topic id is provided.
if ($smcFunc['db_num_rows']($request) == 0)
redirectexit();
$row = $smcFunc['db_fetch_assoc']($request);
$smcFunc['db_free_result']($request);
if (!empty($row['id_poll']))
{
loadLanguage('Post');
// Get the question and if it's locked.
$request = $smcFunc['db_query']('', '
SELECT
p.question, p.voting_locked, p.hide_results, p.expire_time, p.max_votes, p.change_vote,
p.guest_vote, p.id_member, IFNULL(mem.real_name, p.poster_name) AS poster_name, p.num_guest_voters, p.reset_poll
FROM {db_prefix}polls AS p
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = p.id_member)
WHERE p.id_poll = {int:id_poll}
LIMIT 1',
array(
'id_poll' => $row['id_poll'],
)
);
$pollinfo = $smcFunc['db_fetch_assoc']($request);
$smcFunc['db_free_result']($request);
$request = $smcFunc['db_query']('', '
SELECT COUNT(DISTINCT id_member) AS total
FROM {db_prefix}log_polls
WHERE id_poll = {int:id_poll}
AND id_member != {int:not_guest}',
array(
'id_poll' => $row['id_poll'],
'not_guest' => 0,
)
);
list ($pollinfo['total']) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
// Total voters needs to include guest voters
$pollinfo['total'] += $pollinfo['num_guest_voters'];
// Get all the options, and calculate the total votes.
$request = $smcFunc['db_query']('', '
SELECT pc.id_choice, pc.label, pc.votes, IFNULL(lp.id_choice, -1) AS voted_this
FROM {db_prefix}poll_choices AS pc
LEFT JOIN {db_prefix}log_polls AS lp ON (lp.id_choice = pc.id_choice AND lp.id_poll = {int:id_poll} AND lp.id_member = {int:current_member} AND lp.id_member != {int:not_guest})
WHERE pc.id_poll = {int:id_poll}',
array(
'current_member' => $user_info['id'],
'id_poll' => $row['id_poll'],
'not_guest' => 0,
)
);
$pollOptions = array();
$realtotal = 0;
$pollinfo['has_voted'] = false;
while ($row = $smcFunc['db_fetch_assoc']($request))
{
censorText($row['label']);
$pollOptions[$row['id_choice']] = $row;
$realtotal += $row['votes'];
$pollinfo['has_voted'] |= $row['voted_this'] != -1;
}
$smcFunc['db_free_result']($request);
// If this is a guest we need to do our best to work out if they have voted, and what they voted for.
if ($user_info['is_guest'] && $pollinfo['guest_vote'] && allowedTo('poll_vote'))
{
if (!empty($_COOKIE['guest_poll_vote']) && preg_match('~^[0-9,;]+$~', $_COOKIE['guest_poll_vote']) && strpos($_COOKIE['guest_poll_vote'], ';' . $row['id_poll'] . ',') !== false)
{
// ;id,timestamp,[vote,vote...]; etc
$guestinfo = explode(';', $_COOKIE['guest_poll_vote']);
// Find the poll we're after.
foreach ($guestinfo as $i => $guestvoted)
{
$guestvoted = explode(',', $guestvoted);
if ($guestvoted[0] == $row['id_poll'])
break;
}
// Has the poll been reset since guest voted?
if ($pollinfo['reset_poll'] > $guestvoted[1])
{
// Remove the poll info from the cookie to allow guest to vote again
unset($guestinfo[$i]);
if (!empty($guestinfo))
$_COOKIE['guest_poll_vote'] = ';' . implode(';', $guestinfo);
else
unset($_COOKIE['guest_poll_vote']);
}
else
{
// What did they vote for?
unset($guestvoted[0], $guestvoted[1]);
foreach ($pollOptions as $choice => $details)
{
$pollOptions[$choice]['voted_this'] = in_array($choice, $guestvoted) ? 1 : -1;
$pollinfo['has_voted'] |= $pollOptions[$choice]['voted_this'] != -1;
}
unset($choice, $details, $guestvoted);
}
unset($guestinfo, $guestvoted, $i);
}
}
$context['user']['started'] = $user_info['id'] == $row['id_member'] && !$user_info['is_guest'];
// Set up the basic poll information.
$context['poll'] = array(
'id' => $row['id_poll'],
'image' => 'normal_' . (empty($pollinfo['voting_locked']) ? 'poll' : 'locked_poll'),
'question' => parse_bbc($pollinfo['question']),
'total_votes' => $pollinfo['total'],
'change_vote' => !empty($pollinfo['change_vote']),
'is_locked' => !empty($pollinfo['voting_locked']),
'options' => array(),
'lock' => allowedTo('poll_lock_any') || ($context['user']['started'] && allowedTo('poll_lock_own')),
'edit' => allowedTo('poll_edit_any') || ($context['user']['started'] && allowedTo('poll_edit_own')),
'allowed_warning' => $pollinfo['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($pollOptions), $pollinfo['max_votes'])) : '',
'is_expired' => !empty($pollinfo['expire_time']) && $pollinfo['expire_time'] < time(),
'expire_time' => !empty($pollinfo['expire_time']) ? timeformat($pollinfo['expire_time']) : 0,
'has_voted' => !empty($pollinfo['has_voted']),
'starter' => array(
'id' => $pollinfo['id_member'],
'name' => $row['poster_name'],
'href' => $pollinfo['id_member'] == 0 ? '' : $scripturl . '?action=profile;u=' . $pollinfo['id_member'],
'link' => $pollinfo['id_member'] == 0 ? $row['poster_name'] : '' . $row['poster_name'] . ''
)
);
// Make the lock and edit permissions defined above more directly accessible.
$context['allow_lock_poll'] = $context['poll']['lock'];
$context['allow_edit_poll'] = $context['poll']['edit'];
// You're allowed to view the results if:
// 1. you're just a super-nice-guy, or
// 2. anyone can see them (hide_results == 0), or
// 3. you can see them after you voted (hide_results == 1), or
// 4. you've waited long enough for the poll to expire. (whether hide_results is 1 or 2.)
$context['allow_poll_view'] = allowedTo('moderate_board') || $pollinfo['hide_results'] == 0 || ($pollinfo['hide_results'] == 1 && $context['poll']['has_voted']) || $context['poll']['is_expired'];
// Calculate the percentages and bar lengths...
$divisor = $realtotal == 0 ? 1 : $realtotal;
// Determine if a decimal point is needed in order for the options to add to 100%.
$precision = $realtotal == 100 ? 0 : 1;
// Now look through each option, and...
foreach ($pollOptions as $i => $option)
{
// First calculate the percentage, and then the width of the bar...
$bar = round(($option['votes'] * 100) / $divisor, $precision);
$barWide = $bar == 0 ? 1 : floor(($bar * 8) / 3);
// Now add it to the poll's contextual theme data.
$context['poll']['options'][$i] = array(
'id' => 'options-' . $i,
'percent' => $bar,
'votes' => $option['votes'],
'voted_this' => $option['voted_this'] != -1,
'bar' => '',
// Note: IE < 8 requires us to set a width on the container, too.
'bar_ndt' => $bar > 0 ? '