Description:
Basically the same idea as poll_box, except the poll displayed is selected randomly. Section permissions and list_polls perm are respected. Form creation code was largely stolen from the stock poll_box.
Box Code:
# users with list_polls priv get to see all polls
# everybody else only gets to see polls from sections that they have
# read access to, not including diaries
my $where = "s.displaystatus <= 1 AND s.displaystatus >= ";
if($S->have_perm('list_polls')) {
$where .= '-3';
} else {
$where .= '0';
# build a list of sections that this user can see and add a clause to
# $where to limit selection to those polls
my ($rv, $sth) = $S->db_select({
WHAT => 'section',
FROM => 'sections',
});
$rv or return 0;
# this is just a slight optimization to keep the number of conditions
# in $where to a minimum (is this a useful thing to do?)
my (@acc, @noacc, $secwhere);
while(my $sec = $sth->fetchrow_hashref) {
if($S->have_section_perm('norm_read_stories', $sec->{'section'}) and $sec->{'section'} ne "Diary") {
push @acc, $sec->{'section'};
} else {
push @noacc, $sec->{'section'};
}
}
$secwhere = ($#acc/2) >= $#noacc
? join(" AND ", map { 's.section != ' . $S->{DBH}->quote($_) } @noacc)
: join(" OR ", map { 's.section = ' . $S->{DBH}->quote($_) } @acc);
$where = "$where AND ( $secwhere )" if $secwhere;
$sth->finish();
}
$where = "s.attached_poll != '' AND $where";
$where .= ' AND s.attached_poll = p.qid';
my ($rv, $sth) = $S->db_select({
WHAT => 'p.qid, p.question, p.voters, p.post_date',
FROM => 'stories s, pollquestions p',
WHERE => "$where",
ORDER_BY => 'rand()',
LIMIT => 1,
});
$rv or return 0;
my $poll = $sth->fetchrow_hashref or return 0;
$sth->finish();
# first get the poll form all set up except for the answers
my $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->{'qid'}">
<INPUT type="hidden" name="ispoll" value="1">\|;
$form .= "<b>$poll->{'question'}</b><br>";
# here is where all the answer fields get filled in
my $qid = $S->{DBH}->quote($poll->{'qid'});
($rv, $sth) = $S->db_select({
WHAT => "qid,aid,answer,votes",
FROM => 'pollanswers',
WHERE => "qid = $qid",
ORDER_BY => "aid ASC",
});
$rv or return 0;
my $answer = [];
while(my $ans = $sth->fetchrow_hashref) { push @{$answer}, $ans }
$sth->finish();
# now check if they have already voted or haven't logged in
my $row;
if ( $S->_can_vote($poll->{'qid'}) ) {
foreach $row ( @{$answer} ) {
$form .= qq\|
<INPUT TYPE="radio" NAME="aid" VALUE="$row->{'aid'}"> $row->{'answer'}<BR>\|;
}
} else {
my $total_votes = $poll->{'voters'} ? $poll->{'voters'} : 1;
$form .= qq\|
<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0>\|;
foreach $row ( @{$answer} ) {
my $percent = int($row->{'votes'} / $total_votes * 100);
$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>\|;
}
$form .= qq\|
</TABLE>\|;
}
# get the # of comments
my $comment_num = $S->poll_comment_num($poll->{'qid'});
# only show the vote button if they havn't voted
if ( $S->_can_vote($poll->{'qid'}) ) {
$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->{'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>}
: qq{
<TD COLSPAN="3" ALIGN="center">|norm_font|Votes: <b>$poll->{'voters'}</b>|norm_font_end|</TD>};
$form .= qq{
</FORM>
<!-- end poll form -->
<P>
|norm_font|
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 ALIGN="center">
<TR>
$comm_disp
</TR>
<TR>
<TD>|norm_font|<a href="|rootdir|/poll/$poll->{'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>
</TABLE>
|norm_font_end|
<!-- end poll content -->};
return '' unless $form;
return {content => qq{|norm_font|$form|norm_font_end|}, title => $title};
|