#!/usr/bin/perl use strict; use lib '/home/slowtwitch/forum.slowtwitch.com/cgi-bin/admin'; use GForum qw/$DB $CFG $IN/; use vars qw/$PRINT_HEADER $FONT $MAX $poll_vars/; local $SIG{__DIE__} = \&GForum::fatal; GForum::init('/home/slowtwitch/forum.slowtwitch.com/cgi-bin/admin'); $poll_vars = do "poll.cfg"; $MAX = 20; $FONT = qq~font face="tacoma, arial, san-serif" size="2"~; main(); sub main { header(); nav(); my $do = $IN->param('do'); if ($do eq 'add_form') { add_form(); } elsif ($do eq 'add') { add(); } elsif ($do eq 'modify_form') { modify_form(); } elsif ($do eq 'modify') { modify(); } elsif ($do eq 'disable') { disable(); } elsif ($do eq 'enable') { enable(); } elsif ($do eq 'home_show') { home_show(); } elsif ($do eq 'home_notshow') { home_notshow(); } else { &list(); } footer(); } sub header { if (!$PRINT_HEADER) { print $IN->header(); } print "Poll Admin<$FONT>"; } sub nav { print qq! Poll: Add Poll | List Poll !; #
Quiz: Add Quiz | List Quiz
} sub footer { print ""; } sub include_form { # ------------------------------------------------------------------- # my $val = shift || {}; my $option = shift || $MAX; my $output = qq~
\n~; $output .= qq~

<$FONT color="red">$val->{error}

~ if $val->{error}; $output .= qq~ Question:

\n Description:

\n Answers Options:
~; my $i = 1; my $select = qq~  ~; if (!$val->{poll_type}) { $output .= qq~Votes:
\n~; } $output .= qq~\n~; $output .= "
"; $select .= qq~\n~; $i++; } if ($IN->param('num_answers') > $i) { foreach ($i .. $IN->param('num_answers')) { $output .= qq~Option $_:  ~; if (!$val->{poll_type}) { $output .= qq~Votes:
\n~; } $output .= "
"; $select .= qq~\n~; } } $i += 2; } else { foreach (1 .. $option) { my $ans = $val->{'poll_answer_answer_' . $_}; $output .= qq~Option $_:  ~; if (!$val->{poll_type}) { $output .= qq~Votes: \n~; } $output .= "
"; $select .= qq~\n~; } } $select .= ""; if ($val->{poll_type} == 1) { $output .= qq~Right Answer: $select
\n~; } $output .= qq~Total Votes:

\n~; $output .= $val->{poll_id} ? qq~ Want to add more options?
~ : qq~~; return $output . "\n"; } sub list { # ------------------------------------------------------------------- # my $msg = shift; my $start_at = $IN->param("start_at") || 1; my $poll_count = $IN->param("poll_count") || 100; my $cgi = $IN->get_hash(); $cgi->{nh} ||= $start_at; $cgi->{mh} ||= $poll_count; $cgi->{sb} ||= "poll_date"; $cgi->{so} ||= "DESC"; $cgi->{poll_type} ||= 0; #use Data::Dumper; print "
" . Dumper($cgi) . "
"; my $db = $DB->table('Poll'); my $ans_db = $DB->table('PollAnswer'); my $sth = $db->query_sth($cgi); my $output; if ($msg) { $output .= "

$msg

"; } my $i=1; my $correct = ""; while (my $row = $sth->fetchrow_hashref()) { my $date = $row->{poll_date}; $output .= qq~

~; my $answers = $ans_db->select( { poll_id_fk => $row->{poll_id} })->fetchall_hashref(); my $max = 0; my $votes = $row->{poll_votes}; foreach my $poll_answer (@$answers) { if ($votes) { $poll_answer->{poll_answer_percentage} = sprintf("%" . $poll_vars->{percentage_pre} . "." . $poll_vars->{percentage_post} . "f",$poll_answer->{poll_answer_votes} / $votes * 100); } if ($poll_answer->{poll_answer_votes} > $max) { $max = $poll_answer->{poll_answer_votes}; $poll_answer->{poll_answer_is_leading} = 1; } else { $poll_answer->{poll_answer_is_leading} = 0; } if ($poll_answer->{poll_answer_id} == $row->{poll_answer}) { $correct = $poll_answer->{poll_answer_answer}; } $output .= qq~ ~; } if ($row->{poll_type}) { $output .= qq~~; $output .= qq~~; } else { $output .= qq~~; } $output .= qq~
<$FONT>$i.) $row->{poll_question} (posted on $date)
<$FONT>Description: $row->{poll_text}
<$FONT> $poll_answer->{poll_answer_answer} <$FONT> ~; if ($poll_answer->{poll_answer_percentage}) { my $width = $poll_answer->{poll_answer_percentage} * 4; $output .= qq~~; } else { $output .= qq~~; } $output .= qq~$poll_answer->{poll_answer_votes} / $poll_answer->{poll_answer_percentage} %
<$FONT>The Correct Answer is: $correct
<$FONT>Total Answered: $row->{poll_votes}
<$FONT>Total Votes: $row->{poll_votes}
<$FONT> Edit | ~; $output .= $row->{poll_enabled} ? qq~ Disable ~ : qq~ Enable~ ; $output .= $row->{poll_home} ? qq~| Don't Show this on Home Page~ : qq~| Show this on Home Page~ ; $output .= qq~
~; $i++; } print $output; } sub add_form { # ------------------------------------------------------------------- # my $cgi = $IN->get_hash(); my $error = shift; if ($error) { $cgi->{error} = $error; } $cgi->{do} = 'add'; my $form = include_form( { %$cgi }, 4 ); print $form; } sub add { # ------------------------------------------------------------------- # # This subroutine will get called whenever the hook 'do_post_post' # is run. my $total_options = 0; my %seen; my $dup = 0; for (1 .. $MAX) { my $ans = $IN->param("poll_answer_answer_$_"); if ($ans) { if ($seen{$ans}) { $dup = 1; last; } $seen{$ans} = 1; $total_options++; } } if ($dup) { return add_form("Answers are the same."); } if (!$IN->param('poll_question')) { add_form("No question entered!"); return; } if ($total_options < 2) { add_form("POLL_NOT_ENOUGH_OPTIONS"); return; } # Insert question into Poll table my $a = $DB->table('Poll'); my $cgi = $IN->get_hash; $cgi->{'poll_enabled'} = '1'; $cgi->{'poll_type'} ||= 0; $cgi->{'poll_votes'} = 0; if (!$cgi->{'poll_type'}){ $cgi->{'poll_answer'} = 0; } require GT::Date; $cgi->{'poll_date'} = GT::Date::date_get(); my $rec = $a->insert($cgi) or die "$GT::SQL::error"; my $poll_id = $rec->insert_id; if (!$poll_id) { print "error: can't add poll, reason: $GT::SQL::error"; return; } # Insert answers into PollAnswer table my $votes_sum; my $b = $DB->table('PollAnswer'); for (my $i = 1; $i <= $MAX; $i++) { my %answer; if ($IN->param("poll_answer_answer_$i")) { $answer{'poll_answer_answer'} = $IN->param("poll_answer_answer_$i"); $answer{'poll_answer_votes'} = 0; $answer{'poll_id_fk'} = $poll_id; my $res = $b->insert(\%answer); my $ans_id = $res->insert_id; if ($i == $cgi->{poll_answer}) { $a->update( { poll_answer => $ans_id }, { poll_id => $poll_id }); } }; }; my $msg = "Poll added successfully!"; &list($msg); } sub modify_form { # ------------------------------------------------------------------- # my $id = $IN->param('poll_id') || shift; if (!$id) { print "no poll id passed"; return; } my $poll = $DB->table('Poll')->get($id); my $ans_db = $DB->table('PollAnswer'); my $poll_answer = $ans_db->select( { poll_id_fk => $id } )->fetchall_hashref(); my $hits = $ans_db->hits; my $form = include_form( { do => 'modify', %$poll, poll_answers_loop => $poll_answer}, 4 ); print $form; } sub modify { # ------------------------------------------------------------------- # my $poll_id = $IN->param('poll_id'); my $cgi = $IN->get_hash(); # Update existing record or insert new record (PollAnswer table) my $votes_sum = 0; my $a = $DB->table('PollAnswer'); for (my $i = 1; $i <= $MAX; $i++) { my %answer; if ($IN->param("poll_answer_answer_$i")) { $answer{'poll_answer_answer'} = $IN->param("poll_answer_answer_$i"); if ($IN->param("poll_answer_votes_$i")) { $answer{'poll_answer_votes'} = $IN->param("poll_answer_votes_$i"); } $answer{'poll_id_fk'} = $poll_id; $votes_sum += $IN->param("poll_answer_votes_$i"); if ($IN->param("current_poll_answer_id_$i")) { $a->update(\%answer, { poll_answer_id => $IN->param("current_poll_answer_id_$i") }) or die $GT::SQL::error; } else { $a->insert(\%answer); } } elsif ($IN->param("current_poll_answer_id_$i") and not $IN->param("poll_answer_answer_$i")) { $a->delete( { poll_answer_id => $IN->param("current_poll_answer_id_$i") }); }; }; # Update vote count my $p = $DB->table('Poll'); my $poll_type = $IN->param('poll_type') || '0'; my $cols = $p->cols; my $mod = {}; foreach (keys %$cols) { next if ($_ eq 'poll_id'); next if ($_ eq 'poll_date'); next if ($_ eq 'poll_home'); next if ($_ eq 'poll_enabled'); $mod->{$_} = $cgi->{$_}; } if (!$poll_type) { $mod->{poll_answer} = 0; $mod->{poll_type} = 0; } $p->update( $mod , { poll_id => $poll_id }) or die "$GT::SQL::error"; &list("The poll has been updated successfully!"); } sub disable { my $poll_id = $IN->param('poll_id'); my $p = $DB->table('Poll'); my $poll = $p->get($poll_id); if (!$poll) { return &list("No such quiz or poll: $poll_id"); } $p->update({ poll_enabled => 0 }, { poll_id => $poll_id }); $IN->param('poll_id',""); $IN->param('poll_type',$poll->{poll_type}); &list("The poll/quiz $poll->{poll_question} has been disabled."); } sub enable { my $poll_id = $IN->param('poll_id'); my $p = $DB->table('Poll'); my $poll = $p->get($poll_id); if (!$poll) { return &list("No such quiz or poll: $poll_id"); } $p->update({ poll_enabled => 1 }, { poll_id => $poll_id }); $IN->param('poll_id',""); $IN->param('poll_type',$poll->{poll_type}); &list("The poll/quiz $poll->{poll_question} has been enabled."); } sub home_show { my $poll_id = $IN->param('poll_id'); my $p = $DB->table('Poll'); my $poll = $p->get($poll_id); if (!$poll) { return &list("No such quiz or poll: $poll_id"); } $p->update({ poll_home => 0 }); $p->update({ poll_home => 1 }, { poll_id => $poll_id }); $IN->param('poll_id',""); $IN->param('poll_type',$poll->{poll_type}); &list("The poll/quiz $poll->{poll_question} has been set to show on home page."); } sub home_notshow { my $poll_id = $IN->param('poll_id'); my $p = $DB->table('Poll'); my $poll = $p->get($poll_id); if (!$poll) { return &list("No such quiz or poll: $poll_id"); } $p->update({ poll_home => 1 }, { poll_id => $poll_id }); $IN->param('poll_id',""); $IN->param('poll_type',$poll->{poll_type}); &list("The poll/quiz $poll->{poll_question} has been removed from showing on the home page."); }