//////////////////////////////////////////////////////////
// Enhanced showPoll block
// Shows poll in block from specified topic,
// or the most recent from specified board
//
// Author: Greybrow
// Version: 2007-04-24 22:00
// Features:
// shows poll (or polls) that fits in a block
// voting or scores (when voted or can't vote)
// shows question as a topic link
// shows poll from specified topic
// shows most recent poll from specified board
//
// based on ssi_showPoll();
// added block hacks by Raysr and Thurnok from:
// http://tpblocks.ccs-net.com/index.php?topic=25
// http://tpblocks.ccs-net.com/index.php?topic=40
//
// usage:
// - copy whole code to phpblock.
// - at the end of the code use function
// tp_showPoll(topic number or null, 'echo' or null, board number or null)
// or
// copy the function to SSI.php
// put only the function call in phpblock
//
// examples:
// show the poll from topic 34
// tp_showPoll(34);
//
// show the most recent poll from the board number 5
// tp_showPoll(null,'echo',5);
//
// keep in mind, that if board is specified, topic is ignored
// so it will display the same as above
// tp_showPoll(34,'echo',5)
//
// put the array with poll from board 5 into $thepoll variable
// $thepoll = tp_showPoll(null,null,5);
//
// if you call the function more than once,
// with different options, block will show more polls
// but I'm not sure if voting would work correctly :(
///////////////////////////////////////////////////////
function tp_showPoll($topic = null, $output_method = 'echo', $board = null)
{
global $db_prefix, $txt, $settings, $boardurl, $sc, $user_info;
global $context, $scripturl, $smcFunc;
///added: , $scripturl
$boardsAllowed = boardsAllowedTo('poll_view');
if (empty($boardsAllowed))
return array();
if ($topic === null && isset($_REQUEST['ssi_topic']))
$topic = (int) $_REQUEST['ssi_topic'];
else
$topic = (int) $topic;
if ($board === null)
{
// board not chosen, so get the one from specified topic
$request = $smcFunc['db_query']("", "
SELECT
p.id_poll, p.question, p.voting_locked, p.hide_results, p.expire_time, p.max_votes, b.id_board
FROM ({db_prefix}topics AS t, {db_prefix}polls AS p, {db_prefix}boards AS b)
WHERE p.id_poll = t.id_poll
AND t.id_topic = {int:topic}
AND b.id_board = t.id_board
AND $user_info[query_see_board]
" . (!in_array(0, $boardsAllowed) ? "
AND b.id_board IN ({array_int:boardsallowed})" : '') . "
LIMIT 1",
array(
'topic' => $topic,
'boardsallowed' => $boardsAllowed,
)
);
}
else
{
// board chosen, so lets try to get the most recent poll from it
$board = (int) $board;
$request = $smcFunc['db_query']("", "
SELECT
p.id_poll, p.question, p.voting_locked, p.hide_results, p.expire_time, p.max_votes, b.id_board, t.id_topic
FROM ({db_prefix}topics AS t, {db_prefix}polls AS p, {db_prefix}boards AS b)
WHERE p.id_poll = t.id_poll
AND b.id_board = t.id_board
AND b.id_board = {int:board}
AND $user_info[query_see_board]
" . (!in_array(0, $boardsAllowed) ? "
AND {int:board} IN ({array_int:boardsallowed})" : '') . "
ORDER BY p.id_poll DESC
LIMIT 1",
array(
'board' => $board,
'boardsallowed' => $boardsAllowed,
)
);
}
// Either this topic has no poll, or the user cannot view it.
if ($smcFunc['db_num_rows']($request) == 0)
return array();
$row = $smcFunc['db_fetch_assoc']($request);
$smcFunc['db_free_result']($request);
if($topic == 0)
$topic = (int)$row['id_topic'];
// Check if they can vote.
if ((!empty($row['expire_time']) && $row['expire_time'] < time()) || $user_info['is_guest'] || !empty($row['voting_locked']) || !allowedTo('poll_vote', $row['id_board']))
$allow_vote = false;
else
{
$request = $smcFunc['db_query']("", "
SELECT id_member
FROM {db_prefix}log_polls
WHERE id_poll = {int:idpoll}
AND id_member = {int:userinfo}
LIMIT 1",
array(
'idpoll' => $row['id_poll'],
'userinfo' => $user_info['id'],
)
);
$allow_vote = $smcFunc['db_num_rows']($request) == 0;
$smcFunc['db_free_result']($request);
}
$request = $smcFunc['db_query']("", "
SELECT COUNT(DISTINCT id_member)
FROM {db_prefix}log_polls
WHERE id_poll = {int:idpoll}",
array(
'idpoll' => $row['id_poll']
)
);
list ($total) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$request = $smcFunc['db_query']("", "
SELECT id_choice, label, votes
FROM {db_prefix}poll_choices
WHERE id_poll = {int:idpoll}",
array(
'idpoll' => $row['id_poll']
)
);
$options = array();
$total_votes = 0;
while ($rowChoice = $smcFunc['db_fetch_assoc']($request))
{
censorText($rowChoice['label']);
$options[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']);
$total_votes += $rowChoice['votes'];
}
$smcFunc['db_free_result']($request);
$return = array(
'id' => $row['id_poll'],
'image' => empty($pollinfo['voting_locked']) ? 'poll' : 'locked_poll',
'question' => $row['question'],
'total_votes' => $total,
'is_locked' => !empty($pollinfo['voting_locked']),
'allow_vote' => $allow_vote,
'topic' => $topic
);
// Calculate the percentages and bar lengths...
$divisor = $total_votes == 0 ? 1 : $total_votes;
foreach ($options as $i => $option)
{
$bar = floor(($option[1] * 100) / $divisor);
$barWide = $bar == 0 ? 1 : floor(($bar * 5) / 6);
$return['options'][$i] = array(
'id' => 'options-' . $i,
'percent' => $bar,
'votes' => $option[1],
'bar' => '<span style="white-space: nowrap;"><img src="' . $settings['images_url'] . '/poll_left.gif" alt="" /><img src="' . $settings['images_url'] . '/poll_middle.gif" width="' . $barWide . '" height="12" alt="-" /><img src="' . $settings['images_url'] . '/poll_right.gif" alt="" /></span>',
'option' => parse_bbc($option[0]),
'vote_button' => '<input type="' . ($row['max_votes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" id="options-' . $i . '" value="' . $i . '" class="check" />'
);
}
$return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options6'], $row['max_votes']) : '';
if ($output_method != 'echo')
return $return;
if ($return['allow_vote'])
{
echo '
<form action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '">
<input type="hidden" name="poll" value="', $return['id'], '" />
<table border="0" cellspacing="1" cellpadding="0" class="ssi_table">
<tr>
<td colspan="2" class="smalltext"><a href="', $boardurl, '/index.php?topic=', $return['topic'], '"><b>', $return['question'], '</b></a></td>
</tr>
<tr>
<td class="smalltext">', $return['allowed_warning'], '</td>
</tr>';
foreach ($return['options'] as $option)
echo '
<tr>
<td class="smalltext"><label for="', $option['id'], '">', $option['vote_button'], '</td><td class="smalltext">', $option['option'], '</label></td>
</tr>';
echo '
<tr>
<td colspan="2" class="smalltext"><input type="submit" value="Submit Vote" /></td>
</tr>
</table>
<input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
</form>';
}
///////////// /// added code by willyP, per jpdeni:
elseif ($user_info['is_guest'])
{
echo '
<a href="', $boardurl, '/index.php?topic=', $return['topic'], '"><b>', $return['question'], '</b></a><br /><ul>';
foreach ($return['options'] as $option)
echo '
<li>', $option['option'], '</li>';
echo '
</ul>
If you would like to vote in this poll, please <a href="' . $scripturl . '?action=login">login</a> or <a href="' . $scripturl . '?action=register">register</a>.';
}
/////////////end added code by willyP
else
{
echo '
<table border="0" cellspacing="1" cellpadding="0" class="ssi_table">
<tr>
<td colspan="2" class="smalltext"><a href="', $boardurl, '/index.php?topic=', $return['topic'], '"><b>', $return['question'], '</b></a></td>
</tr>';
foreach ($return['options'] as $option)
echo '
<tr>
<td colspan="2" align="left" valign="top" style="font-style: italic" class="smalltext">', $option['option'], '</td>
</tr>
<tr>
<td align="left" class="smalltext">', $option['bar'], '</td>
<td align="left" class="smalltext">', $option['votes'], ' (', $option['percent'], '%)</td>
</tr>';
echo '
<tr>
<td colspan="2" class="smalltext"><b>Total Votes: ', $return['total_votes'], '</b></td>
</tr>
</table>';
}
}
///////////////////////////////////////
// display most recent poll from board number 15
tp_showPoll(null,'echo',15);