#!/usr/bin/perl -w use CGI qw(:standard); use DBI; use HTML::Template; use strict; use vars qw($dbh $qref $correct_session $attempted_session $correct_overall $attempted_overall %stored_vals %cookie_vals $url); sub main_page() { print header(), start_html(-title =>'Elder Linda\'s RLDS Trivia Game', -bgcolor => "e6ffe6", -vlink => "6633cc", -link => "0033cc", text => "008000"), "
", br(), "
Elder Linda's RLDS Trivia Game
", br(), br(), start_form(-action => url()), "Please choose a category:", br(); my $qry = $dbh->prepare('SELECT DISTINCT category FROM questions'); $qry->execute(); my $catref = $qry->fetchall_arrayref(); my @categories; my $count = 0; for (my $x=0; $x <= $#{$catref}; $x++) { $categories[$count] = $catref->[$x][0]; ++$count; } $categories[$count] = "All Categories"; print popup_menu (-name => "category", -values => \@categories); print hidden(-name => "init", -value => "yes"); print submit(-name => "choice", -value => "Go"), end_form(), end_html(); } sub get_questions() { my $category = param("category"); my $qry; if ($category eq "All Categories") { $qry = $dbh->prepare('SELECT number, question, ans_a, ans_b, ans_c, reference FROM questions ORDER BY RAND() LIMIT 1'); $qry->execute(); } else { $qry = $dbh->prepare('SELECT number, question, ans_a, ans_b, ans_c, reference FROM questions WHERE category = ? ORDER BY RAND() LIMIT 1'); $qry->execute($category); } $qref = $qry->fetchrow_hashref(); } sub display_questions() { if (param("init") eq "yes") { %cookie_vals = cookie ("RLDS Trivia Score"); if (!(%cookie_vals)) { $correct_overall = 0; $attempted_overall = 0; } else { $correct_overall = 0; $attempted_overall = 0; #$correct_overall = $cookie_vals{Correct Overall}; #$attempted_overall = $cookie_vals{Attempted Overall}; } $correct_session = 0; $attempted_session = 0; } else { init_counters(); } my $template = HTML::Template->new(filename => '/home/www/elderlinda/quiz-template.tmpl', die_on_bad_params => 0); $template->param(question => $qref->{question}, ans_a => $qref->{ans_a}, ans_b => $qref->{ans_b}, ans_c => $qref->{ans_c}, category => param("category"), number => $qref->{number}, url => $url, correct_session => $correct_session, attempted_session => $attempted_session, correct_overall => $correct_overall, attempted_overall => $attempted_overall ); print header(), $template->output; } sub check_answer() { init_counters(); my $cat = param("category"); my $subans = param("ANS"); my $qnum = param("number"); my $aqry = $dbh->prepare('SELECT ans FROM answers WHERE question = ?'); $aqry->execute($qnum); my @answer = $aqry->fetchrow_array(); my $template = HTML::Template->new(filename => '/home/www/elderlinda/quiz-template-answer.tmpl', die_on_bad_params => 0); my $response; if ($subans eq $answer[0]) { $response = "Correct!"; $correct_session++; $correct_overall++; } else { my $base = "ans_"; my $correct = $base.$answer[0]; my $qry = $dbh->prepare('SELECT ans_a, ans_b, ans_c FROM questions WHERE number = ?'); $qry->execute($qnum); my $answers = $qry->fetchrow_hashref(); $response = "Incorrect. The answer was \"$answers->{$correct}\"."; } my $rqry = $dbh->prepare('SELECT reference FROM questions WHERE number = ?'); $rqry->execute($qnum); my $dbref = $rqry->fetchrow_hashref(); my $ref = $dbref->{reference}; $attempted_session++; $attempted_overall++; my $current_percent = ($correct_session)/($attempted_session); my $overall_percent = ($correct_overall)/($attempted_overall); my $curr_score = "Your score for this category is $correct_session for $attempted_session, or $current_percent."; my $overall_score = "Your score for this category is $correct_overall for $attempted_overall, or $overall_percent."; $template->param(response => $response, category => $cat, url => $url, correct_session => $correct_session, attempted_session => $attempted_session, #correct_overall => $correct_overall, #attempted_overall => $attempted_overall, current_score => $curr_score, overall_score => $overall_score, reference => $ref ); print header(), $template->output; } #print "You're coming from IP Address $ENV{REMOTE_ADDR}", end_html(); sub ask_save() { my $template = HTML::Template->new(filename => '/home/www/elderlinda/save.tmpl', -die_on_bad_params => 0); #my $category = param("category"); my $redirect; if (param("choice") eq "New Category") { $redirect = "restart"; } else { $redirect = "quit"; } $template->param(url => url(), category => param("category"), correct_session => $correct_session, attempted_session => $attempted_session, correct_overall => $correct_overall, attempted_overall => $attempted_overall, redirect => $redirect ); print header(), start_html('Save Session?'), $template->output, end_html(); } sub send_cookie() { my $cookie; my $catbase = param("category"); my $right = " Correct"; my $total = " Attempted"; my $category_right = $catbase.$right; my $category_total = $catbase.$total; my %cookieinfo; my $location; if (!(%cookie_vals)) { $stored_vals{"Correct Overall"} = param("CORRECT_OVERALL"); $stored_vals{"Attempted Overall"} = param("ATTEMPTED_OVERALL"); $stored_vals{$category_right} = param("CORRECT_SESSION"); $stored_vals{$category_total} = param("ATTEMPTED_SESSION"); } else { foreach my $x (keys (%stored_vals)) { $stored_vals{$x} = $cookie_vals{$x}; } $stored_vals{$category_right} = param("CORRECT_SESSION"); $stored_vals{$category_total} = param("ATTEMPTED_SESSION"); $stored_vals{"Overall Correct"} = param("CORRECT_OVERALL"); $stored_vals{"Overall Attempted"} = param("ATTEMPTED_OVERALL"); } $cookie = cookie(-name => "RLDS Trivia Score", -value => \%stored_vals, -expires =>"+1yr"); if (param ("redirect") eq "restart") { print header(-cookie => $cookie, -location => 'http://www.elderlinda.com/cgi-bin/rlds_trivia.mpl'); } elsif (param("redirect") eq "quit") { print header(-cookie => $cookie, -location => 'http://www.elderlinda.com'); } else { print header(), start_html('Error'), "There was an error processing the script. You may ", a({-href => "http://www.elderlinda.com"}, "go to the home page"), " or ", a({-href => url()}, "restart the quiz."); } } sub redirect_user() { if (lc(param("choice")) eq "quit") { print redirect(-uri => 'http://www.elderlinda.com/Trivia.html'); } elsif (lc(param("choice")) eq "new category") { print redirect(-uri => 'http://www.elderlinda.com/cgi-bin/rlds_trivia.mpl'); } else { print header(), start_html('Error'), "An error has occured."; } } sub init_counters() { $correct_session = param("correct_session"); $attempted_session = param("attempted_session"); $correct_overall = param("correct_overall"); $attempted_overall = param("attempted_overall"); } sub hiscore() { my $score = (param("correct_session"))/(param("attempted_session")); my $sth = $dbh->do('SELECT score10 FROM totalhiscore'); my @savedscore = $dbh->fetchrow_array(); if ($score > $savedscore[0]) { print "New High Score!"; } } my $choice = lc(param("choice")); #Connect to the database $dbh = DBI->connect('DBI:mysql:rlds_trivia', 'alex', 'corn!jolio69'); #Tell the script where it is, so it can self-reference $url = url(); #Set up cookie info %stored_vals = ("Old Testament Correct",0,"Old Testament Attempted",0, "New Testament Correct",0,"New Testament Attempted",0, "Christian and Jewish History Correct",0,"Christian and Jewish History Attempted",0, "RLDS History and The Book of D Correct",0,"RLDS History and The Book of D Attempted",0, "The Book of Mormon Correct",0,"The Book of Mormon Attempted",0, "Quotations Correct",0,"Quotations Attempted",0, "Potpourri Correct",0,"Potpourri Attempted",0, "Overall Correct",0,"Overall Attempted",0); #Check submission parameter & display appropriate page if ($choice eq "") { main_page(); } elsif (($choice eq "go") || ($choice eq "next question")) { get_questions(); display_questions(); } elsif (($choice eq "new category") || ($choice eq "quit")) { redirect_user(); #ask_save(); } elsif ($choice eq "check") { check_answer(); } elsif ($choice eq "yes") { send_cookie(); } elsif ($choice eq "no") { redirect_user(); } else { print header(), start_html('Error'), "Invalid submission. Please ", a({-href => url()}, "try again."); }