TinyPortal

Development => Block Codes => Topic started by: Greybrow on January 28, 2007, 11:17:34 PM

Title: [Block] Poll specified or most recent specified board
Post by: Greybrow on January 28, 2007, 11:17:34 PM
I've been looking for a block, that would show most recent polls, but only from one board, in which only moderators could post polls.
And I had to write it myself :)
I tried to test it as much as I could, but I can't be sure if it would work for everyone.
I tested it on SMF 1.1.2 and TP 0.9.8

So here it is:
/////////////////////////////////////////////////////////
// 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, $ID_MEMBER, $settings, $boardurl, $sc, $user_info;
global $context;

$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 = db_query("
SELECT
p.ID_POLL, p.question, p.votingLocked, p.hideResults, p.expireTime, p.maxVotes, 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 = $topic
AND b.ID_BOARD = t.ID_BOARD
AND $user_info[query_see_board]" . (!in_array(0, $boardsAllowed) ? "
AND b.ID_BOARD IN (" . implode(', ', $boardsAllowed) . ")" : '') . "
LIMIT 1", __FILE__, __LINE__);
}
else
{
// board chosen, so lets try to get the most recent poll from it
$board = (int) $board;
$request = db_query("
SELECT
p.ID_POLL, p.question, p.votingLocked, p.hideResults, p.expireTime, p.maxVotes, 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 = $board
AND $user_info[query_see_board]" . (!in_array(0, $boardsAllowed) ? "
AND $board IN (" . implode(', ', $boardsAllowed) . ")" : '') . "
ORDER BY p.ID_POLL DESC
LIMIT 1", __FILE__, __LINE__);
}

// Either this topic has no poll, or the user cannot view it.
if (mysql_num_rows($request) == 0)
return array();

$row = mysql_fetch_assoc($request);
mysql_free_result($request);

if($topic == 0)
$topic = (int)$row['ID_TOPIC'];

// Check if they can vote.
if ((!empty($row['expireTime']) && $row['expireTime'] < time()) || $user_info['is_guest'] || !empty($row['votingLocked']) || !allowedTo('poll_vote', $row['ID_BOARD']))
$allow_vote = false;
else
{
$request = db_query("
SELECT ID_MEMBER
FROM {$db_prefix}log_polls
WHERE ID_POLL = $row[ID_POLL]
AND ID_MEMBER = $ID_MEMBER
LIMIT 1", __FILE__, __LINE__);
$allow_vote = mysql_num_rows($request) == 0;
mysql_free_result($request);
}
$request = db_query("
SELECT COUNT(DISTINCT ID_MEMBER)
FROM {$db_prefix}log_polls
WHERE ID_POLL = $row[ID_POLL]", __FILE__, __LINE__);
list ($total) = mysql_fetch_row($request);
mysql_free_result($request);

$request = db_query("
SELECT ID_CHOICE, label, votes
FROM {$db_prefix}poll_choices
WHERE ID_POLL = $row[ID_POLL]", __FILE__, __LINE__);
$options = array();
$total_votes = 0;
while ($rowChoice = mysql_fetch_assoc($request))
{
censorText($rowChoice['label']);

$options[$rowChoice['ID_CHOICE']] = array($rowChoice['label'], $rowChoice['votes']);
$total_votes += $rowChoice['votes'];
}
mysql_free_result($request);

$return = array(
'id' => $row['ID_POLL'],
'image' => empty($pollinfo['votingLocked']) ? 'poll' : 'locked_poll',
'question' => $row['question'],
'total_votes' => $total,
'is_locked' => !empty($pollinfo['votingLocked']),
'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['maxVotes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" id="options-' . $i . '" value="' . $i . '" class="check" />'
);
}

$return['allowed_warning'] = $row['maxVotes'] > 1 ? sprintf($txt['poll_options6'], $row['maxVotes']) : '';

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="', $txt['smf23'], '" /></td>
</tr>
</table>
<input type="hidden" name="sc" value="', $sc, '" />
</form>';
}
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>', $txt['smf24'], ': ', $return['total_votes'], '</b></td>
</tr>
</table>';
}
}

///////////////////////////////////////
// display most recent poll from board number 1
tp_showPoll(null,'echo',1);
Title: Re: [Block] Poll specified or most recent specified board
Post by: Thurnok on January 29, 2007, 04:55:45 AM
Nice job Greybrow!   :)

Title: Re: [Block] Poll specified or most recent specified board
Post by: deef on January 29, 2007, 08:57:29 PM
This snippet is not working for me :(

Quote
Parse error: syntax error, unexpected T_STRING in /var/www/html/xxx/Sources/Load.php(1776) : eval()'d code(35) : eval()'d code on line 221

I got a Eval'd error (devision by zero I have read on the net)

// edit //

Some people are to stupid  :2funny:

Count me in  :idiot2:

Got some text pasted in de code ...  :(

So solved it my own :-) :-)

it's working now :-)
Title: Re: [Block] Poll specified or most recent specified board
Post by: Greybrow on January 29, 2007, 09:05:17 PM
Check line 221, but in the block code. (That probably would be the last line)
I suppose you made a spelling error in your function call.
Title: Re: [Block] Poll specified or most recent specified board
Post by: deef on January 29, 2007, 09:07:19 PM
Quote from: Greybrow on January 29, 2007, 09:05:17 PM
Check line 221, but in the block code. (That probably would be the last line)
I suppose you made a spelling error in your function call.

sorry allready solved :)

I hade copy some extra text outside the frame

Right under the code screen:

Quote
Report to moderator    Logged

Thanx for your fast reply
Title: Re: [Block] Poll specified or most recent specified board
Post by: oliver on February 01, 2007, 04:05:10 AM
Very nice  :coolsmiley:

Thanks for sharing  :up:
Title: Re: [Block] Poll specified or most recent specified board
Post by: adodger on February 08, 2007, 10:26:35 AM
THANK YOU!
It works perfectly! :up:
Title: Re: [Block] Poll specified or most recent specified board
Post by: almora on February 08, 2007, 12:16:23 PM
Thank You!
Title: Re: [Block] Poll specified or most recent specified board
Post by: martino on March 04, 2007, 01:07:08 PM
Very nice code! I have one question though. What I want to do is set up a board where I will put all the polls, but I want to hide it from everyone, but still allow members to vote from the portal, even guest. However now when I hide it guests can't see it in the portal...

Also, how could I disable the redirect to the poll after someone votes?
Title: Re: [Block] Poll specified or most recent specified board
Post by: Greybrow on March 05, 2007, 01:59:59 AM
I think, that it's impossible to use polls from board that is forbidden to see.
Even if you'd change in block the sql query, in way that would show voting for the poll, voting would be impossible, because of built in protection in smf. Same would go with the redirection.

Only thing I can think of, is creating your own poll system, and include it as a block.
Title: Re: [Block] Poll specified or most recent specified board
Post by: martino on March 05, 2007, 07:33:51 PM
Hmmm...I guess that makes sense. Thank you for the reply.

Also, one more thing which I found was that when I have two polls in one block (http://evaforum.com/forums/index.php) and when you click on one option from either one of the polls the corresponding number of the chosen option will be selected in the other one, but the vote will be submitted just for the one that was selected by the user. Is this some kind of a bug?

P.S. Sorry, but if you want to test it out on that site I posted you'll have to register... :(
Title: Re: [Block] Poll specified or most recent specified board
Post by: MinasC on March 06, 2007, 02:06:51 AM
great great great block there !!! thnx a lot !!!
Title: Re: [Block] Poll specified or most recent specified board
Post by: keith021773 on March 06, 2007, 02:27:16 AM
Great code!!     

I wonder if it would be possible to have a random poll block from all of the polls that are still active (voting still allowed).  That way it even new members could find old polls..   
Title: Re: [Block] Poll specified or most recent specified board
Post by: Greybrow on March 06, 2007, 08:05:08 AM
Quote from: martino on March 05, 2007, 07:33:51 PMAlso, one more thing which I found was that when I have two polls in one block (http://evaforum.com/forums/index.php) and when you click on one option from either one of the polls the corresponding number of the chosen option will be selected in the other one, but the vote will be submitted just for the one that was selected by the user.

Hm... I suppose it's not a bug it's a feature ;)
I didn't actually check two polls voting, but I understand what is happening.
Unfortunately it can't be fixed. Not in easy way at least.
Title: Re: [Block] Poll specified or most recent specified board
Post by: jfinke on March 06, 2007, 03:47:46 PM
Thank you very much!!
Title: Re: [Block] Poll specified or most recent specified board
Post by: Gourgi on March 06, 2007, 07:31:11 PM
this snippet isn't working well with me :(
i'm using SMF 1.1.2 and TP 0.9.7.1 .

when i specify a certain poll
// show the poll from topic 6
tp_showPoll(6);   

it displays the poll (id=6)
(this is the working part of the snippet for me)

but when i specify board 2.0 to display the newest poll from board 2.0
tp_showPoll(null,'echo',2);
it keeps displaying the overall newest poll that i created in board 3.0 !!

same thing happens even if i specify the poll's id (id=5) from board 2.0
tp_showPoll(5,'echo',2);

what i'm doing wrong ?  :idiot2:
Title: Re: [Block] Poll specified or most recent specified board
Post by: Greybrow on March 06, 2007, 09:04:44 PM
hm...
About your board number 2.
No idea.
It works for me just fine... Did you check it, if it's really the 3 board, not the 2?

to the second one:
// show the most recent poll from the board number 5
// tp_showPoll(null,'echo',5);           
//
// keep in mind, that if board given, topic is ignored
// so it will display the same as above
// tp_showPoll(34,'echo',5) 
Title: Re: [Block] Poll specified or most recent specified board
Post by: insanemustang on April 24, 2007, 07:28:39 PM
Worked great on my website, http://www.BamaNation.net

I am hoping I can figure out how to add a specific poll instead of the most recent because I want the one on there to stay for a little while.
Title: Re: [Block] Poll specified or most recent specified board
Post by: Greybrow on April 24, 2007, 08:59:54 PM
My friend found an error (and solution :) ), that was causing strange behaviour when not only one board had polls.
I'll update the code in few minutes.
Title: Re: [Block] Poll specified or most recent specified board
Post by: G6Cad on April 24, 2007, 09:18:14 PM
Please use the code tags when you post the code this time.  ;) 
Title: Re: [Block] Poll specified or most recent specified board
Post by: Greybrow on April 24, 2007, 09:21:06 PM
Didn't I? :)
Title: Re: [Block] Poll specified or most recent specified board
Post by: Omniverse on July 18, 2008, 07:44:38 AM
How could I show more than 1 poll from that board?

Also, is there a way to show the title of the thread as well as the poll title?
Title: Re: [Block] Poll specified or most recent specified board
Post by: Ken. on July 18, 2008, 01:01:51 PM
This topic is over a year old so the posted code may (or may not) work with current versions of SMF/TP.

I use a much simpler code and it works well, but it only shows the topic title.


ssi_showPoll(0.0);
echo '<br /><hr />';
ssi_showPoll(0.0);
echo '<br />';
ssi_showPoll(0.0);


Replace the "0.0" with your topic number.
Title: Re: [Block] Poll specified or most recent specified board
Post by: vortodox on September 09, 2008, 07:48:05 AM
^ I have tested old version of code, and it still works perfectly!