Description:
This box chooses which is the correct poll to display, and displays it. It will show the current poll if on the front page, the poll attached to a story if on a story page, or the poll that you're working on if on a story preview page. This code is rather complex and hard to maintain, I wouldn't suggest making changes to it unless you're either a masochist or you have a ton of spare time.
Box Code:
my $pollqid = shift @ARGS;
my $preview = 0;
my ($pollqid, $action) = $S->get_qid_to_show();
$preview = 1 if ($action eq 'preview');
return '' if ($pollqid == 0);
my $poll_hash = $S->get_poll_hash( $pollqid, $action );
# first get the poll form all set up except for the answers
my $poll_form = qq|
<!-- begin poll form -->
<FORM ACTION="%%rootdir%%/" METHOD="POST">
<INPUT TYPE="hidden" NAME="op" VALUE="view_poll">
<INPUT TYPE="hidden" NAME="qid" VALUE="$poll_hash->{'qid'}">
<INPUT type="hidden" name="ispoll" value="1">|;
$poll_form .= "<b>$poll_hash->{'question'}</b><br>";
# here is where all the answer fields get filled in
my $answer_array = $S->get_poll_answers($poll_hash->{'qid'}, $action);
# now check if they have already voted or haven't logged in
my $row;
if ( $S->_can_vote($poll_hash->{'qid'}) ) {
foreach $row ( @{$answer_array} ) {
$poll_form .= qq|
<INPUT TYPE="radio" NAME="aid" VALUE="$row->{'aid'}"> $row->{'answer'}<BR>|;
}
} else {
my $total_votes = $poll_hash->{'voters'};
if($total_votes == 0) {
$total_votes = 1; # so we don't get a divide by 0 error
}
$poll_form .= qq|
<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0>|;
foreach $row ( @{$answer_array} ) {
my $percent = int($row->{'votes'} / $total_votes * 100);
$poll_form .= qq|
<TR>
<TD valign="top">%%norm_font%%%%dot%%%%norm_font_end%%</TD>
<TD valign="top">%%norm_font%%$row->{'answer'}%%norm_font_end%%</TD>
<TD valign="top">%%norm_font%% $percent% %%norm_font_end%%</TD>
</TR>|;
}
$poll_form .= qq|
</TABLE>|;
}
# get the # of comments
my $comment_num = $S->poll_comment_num($poll_hash->{'qid'});
# only show the vote button if they havn't voted
if ( $S->_can_vote($poll_hash->{'qid'}) && ! $preview ) {
$poll_form .= qq|<BR><INPUT TYPE="submit" name="vote" VALUE="Vote">|;
}
# now finish up the form
my $op = $S->{CGI}->param('op');
my $comm_disp = ($op ne 'displaystory') ?
qq{
<TD>%%norm_font%%Votes: <b>$poll_hash->{'voters'}</b>%%norm_font_end%%</TD>
<TD ALIGN="center" WIDTH=15>%%norm_font%%|%%norm_font_end%%</TD>
<TD ALIGN="right">%%norm_font%% Comments: <b>$comment_num</b>%%norm_font_end%%</TD></TR>
} :
qq{
<TD COLSPAN="3" ALIGN="center">%%norm_font%%Votes: <b>$poll_hash->{'voters'}</b>%%norm_font_end%%</TD>
};
$poll_form .= qq{
</FORM>
<!-- end poll form -->
<P>
%%norm_font%%
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 ALIGN="center">
<TR>
$comm_disp
<TR> };
if( $preview ) {
$poll_form .= qq{
<TD>%%norm_font%%Results%%norm_font_end%%</TD>
<TD ALIGN="center" WIDTH=15>%%norm_font%%|%%norm_font_end%%</TD>
<TD ALIGN="right">%%norm_font%% Other Polls%%norm_font_end%%</TD></TR>
};
} else {
$poll_form .= qq{
<TD>%%norm_font%%<a href="%%rootdir%%/poll/$poll_hash->{'qid'}">Results</a>%%norm_font_end%%</TD>
<TD ALIGN="center" WIDTH=15>%%norm_font%%|%%norm_font_end%%</TD>
<TD ALIGN="right">%%norm_font%% <a href="%%rootdir%%/?op=search;type=polls;search=Search">Other Polls</a>%%norm_font_end%%</TD></TR>
};
}
$poll_form .= qq{
</TABLE>
%%norm_font_end%%
<!-- end poll content -->};
## don't forget to tell them its a poll preview if it is
if( $preview ) {
$title = "Poll Preview";
}
if ($poll_form) {
return {content => qq{%%norm_font%%$poll_form%%norm_font_end%%}, title => $title};
} else {
return '';
}
|