#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);



# (c) NueDream Inc. 2000-2002 (www.nuedream.com)



##############    GLOBAL CONSTANTS    ############



$file = "questions.db";                                                               # This is the name of your questions file.

$quizname = "Crime Scene Quiz";                                                                # This is what appears in the TITLE.

$spacer = '|';                                                                        # The delimeter used in the database.

$result1 = "The suspect may get away if you don't study the evidence more.";		  
# Message Displayed when user gets below 50%.

$result2 = "You got most of the evidence. Some of those questions are harder than they look."; # Message Displayed when user gets between 51%-80%.

$result3 = "You're a top detective, looks like you know your stuff.";                 # Message Displayed when user get above 80%.

$cheater = "What? Cheating won't get you anywhere in life.";                                # Message Displayed when user cheated. 

$fontface = "verdana, tahoma, arial";                                                 # Font Type.

$anticheat = 0;                                                                       # Enables or disables the anti-cheating feature

                                                                                      # (1-Enabled, 0-Disabled).

##############   PROGRAM START    ############



@pairs = split(/&/, $ENV{'QUERY_STRING'} );

	foreach $pair (@pairs) {

		($name,$value) = split (/=/, $pair);

		$value =~ tr/+/ /;

		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("c", hex($1))/eg;

		$value =~ s/\n/ /g;

		$value =~ s/\r//g;

		$value =~ s/\cM//g;

		$GET{$name} = $value;

	}



$cookie    = $ENV{'HTTP_COOKIE'};

(@cookies) = split(/;/, $cookie);



$content = $ENV{'QUERY_STRING'};

($type, $question) = split(/=/, $content);



if($question eq "") {

	$question = 1;

}



$current = $question-1;

$correct = 0;



if($GET{'action'} eq "") {



print "Content-type: text/html\n\n";


print "<html>\n<head>\n<title>$quizname</title>\n</head>\n\n<body BACKGROUND=\"/art/yc_seal_bkg.gif\">\n";
print "\n<!-- NueQuiz. (c) NueDream Inc. 2000-2001. (www.nuedream.com) -->\n\n";

print "<CENTER><NOBR>";
print "<IMG SRC=\"http://www.crimescene.com/art/yc_seal_small.gif\" WIDTH=80 HEIGHT=80 ALIGN=bottom>";
print "<IMG SRC=\"http://www.crimescene.com/art/yc_header.gif\" WIDTH=313 HEIGHT=63 ALIGN=bottom>";
print "<IMG SRC=\"http://www.crimescene.com/art/ys_led_badge.gif\" WIDTH=83 HEIGHT=80 ALIGN=bottom>";
print "<HR>";
print "<A HREF=\"http://www.crimescene.com/drama.member/answer/index.html\">";
print "View the Artist Murdered Case Solution</A>";
print "<HR>";
print "</CENTER></NOBR>";


print "<center>\n<table width=\"450\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n<tr>\n<td>\n";

print "<font face=\"$fontface\" size=\"2\">\n";



open(DATA,$file) or dienice("Couldn't open $file :: $!\n");

#flock(DATA,0); # uncomment this line if using unix based server

seek(DATA,0,0);

@data = <DATA>;

close(DATA);



# Calculating total number of questions.

$total = 0;

foreach $line (@data) {

	($quiz,$num,$q,$ans,$a,$b,$c,$d,$e,$f,$reason) = split(/\|/,$line);

	if ($quiz eq $type) {

		$total++;

	}

}



$total++;



# Printing Quiz results.

if ($question == $total) {				



	print "<br><b>QUIZ Results:</b><br><hr width=\"450\" align=\"left\" color=\"black\" NOSHADE><br>";

	print "<font size=\"1\">";

	foreach $part (@cookies) {

		if ($part =~ /$type/) {

			($name, $value) = split(/=/,$part);

			(@answers) = split(/-/,$value);

					

			foreach $a (@answers) {

				($questionc, $answerc) = split(/_/,$a);

		                

				foreach $line (@data) {

		       		chomp($line);

					($quiz,$num,$q,$ans,$a,$b,$c,$d,$e,$f,$reason) = split(/\|/,$line);

#####################################################################

					if ($quiz eq $type && $questionc eq $num) {

		   				if(substr($answerc,0,1) eq $ans) {

							$tempa = substr($answerc,0,1);

							$tempb = substr($answerc,1,1);

							print "<b>$questionc.</b> $q<br>You Answered: <b>$tempa</b>. <font color=\"#33cc00\"><b>CORRECT!</b></font><ol>$reason</ol>";

							$correct++;

						} else {

							$tempa = substr($answerc,0,1);

							$tempb = substr($answerc,1,1);

							print "<b>$questionc.</b> $q<br>You Answered: <b>$tempa</b>. <font color=\"red\"><b>INCORRECT!</b></font><br>Correct Answer: <b>$tempb</b>.<ol>$reason</ol>";

						}

					}

				}

			}

		} 

	}

			

	print "</font>";

	print "<br><hr width=\"450\" align=\"left\" color=\"black\" NOSHADE>";

			

	$percent = ($correct/$current)*100;

	printf ("<b>$correct</b> / <b>$current</b> = <b> %4.1f", $percent, "%</b><br><br>");

	print "%</b><br><br>";

	

	if ( ($correct/$total) < 0.5) {

		print "$result1";

	} elsif (($correct/$total) < 0.8) {

		print "$result2";

	} elsif (($correct/$total) <= 1.0) {

		print "$result3";

	} else {

		print "$cheater";

	}



} else {

	

		foreach $line (@data) {

			chomp($line);

			($quiz,$num,$q,$ans,$a,$b,$c,$d,$e,$f,$reason) = split(/\|/,$line);

			

			if ($quiz eq $type && $question eq $num) {

			

				print "<form action=\"quiz.cgi?$type=$question&action=check\" method=\"GET\">\n";

			    print "<br><b>$question. $q</b><br>";

				

   				if ($a ne "") { print "<input type=\"radio\" name=\"$num\" value=\"a\" checked>A. $a<br>\n"; }

   				if ($b ne "") { print "<input type=\"radio\" name=\"$num\" value=\"b\">B. $b<br>\n"; }

   				if ($c ne "") { print "<input type=\"radio\" name=\"$num\" value=\"c\">C. $c<br>\n"; }

   				if ($d ne "") { print "<input type=\"radio\" name=\"$num\" value=\"d\">D. $d<br>\n"; }

   				if ($e ne "") { print "<input type=\"radio\" name=\"$num\" value=\"e\">E. $e<br>\n"; }

   				if ($f ne "") { print "<input type=\"radio\" name=\"$num\" value=\"f\">F. $f<br>\n"; }

				

   				print "<br><input type=\"hidden\" name=\"$type\" value=\"$num\">\n";

				print "<br><input type=\"hidden\" name=\"action\" value=\"check\">\n";

				print "<input type=\"submit\" value=\"Continue ª\">\n</form>\n";

			}

		}

	}



print "</font>\n</td>\n</tr>\n</table>\n</center>\n</body>\n</html>";



} elsif ($GET{'action'} eq "check") {



$content = $ENV{'QUERY_STRING'};

($temp, $temp2, $temp3) = split(/&/, $content);

($question, $answer) = split(/=/, $temp);

($type, $question) = split(/=/, $temp2);

($temp4, $act) = split(/=/,$temp3);



$start = $question+1;

$url   = "quiz.cgi?$type=$start";

$tempv = "";



(@cookies) = split(/;/, $ENV{'HTTP_COOKIE'});



foreach $crum (@cookies) {

	($name, $value) = split(/=/, $crum);

	if($name =~ /$type/) {

		$tempv = $value;

		last;

	}

}



open(DATA,$file) or dienice("Couldn't open $file :: $!\n");

#flock(DATA,0); # uncomment this line if using unix based server

seek(DATA,0,0);

@data = <DATA>;

close(DATA);



foreach $line (@data) {

 	chomp($line);

 	($quiz,$num,$q,$ans,$a,$b,$c,$d,$e,$f,$reason) = split(/\|/,$line);

	if ($quiz eq $type && $question eq $num) {

 		if($tempv ne "") {

			if ($tempv =~ /$question/) {

		    	if($anticheat == 1) {

					print "Content-type: text/html\n\n";

            		print "<br><br><center><font face=\"verdana\" size=\"2\"><b>$cheater</b><br><br>If you did not try to cheat, try again. This time do not double click the next button.<br> Click once and wait until the next question is fully displayed before clicking again.</font></center>";

				} else {

					print "Location: $url\n\n";

				}

			} else {

                $tempcookie = "$tempv" . "-" . $question . "_" . $answer . $ans;

		        print "Set-cookie: $type=$tempcookie\n";

				print "Location: $url\n\n";

			}

		} else {

			$tempcookie = $question . "_" . $answer . $ans;

			print "Set-cookie: $type=$tempcookie\n";

			print "Location: $url\n\n";

		}

	}

}



}



#############    DYING SUBROUTINE    ############



sub dienice {

	my($msg) = @_;

	print ("<br><hr><br><h1>FATAL ERROR</h1><hr><br>\n");

	print ($msg);

	exit;

}