home *** CD-ROM | disk | FTP | other *** search
- #!/usr/local/bin/perl
- #
- #
- # Quiz.pl
- # by Leif M. Wright
- # http://www.conservatives.net/atheist/scripts/
- # scripts@conservatives.net
- #
- #################################################################
- # Quiz.pl is a simple little quiz script that lets you put an
- # unlimited number of quizzes on your site for your users to
- # take. Operation is simple.
- #
- # You only have to modify two variables in the script itself, and
- # they are defined below in the USER CONFIGURATION section.
- #
- # Each quiz is a separate html document with several hidden
- # inputs, some of which are required. (they're explained below)
- #
- # Each quiz gets its answers from a pipe-delimited database that
- # looks like this:
- #
- # 1|yes|The answer to this question is obviously yes, you dolt
- #
- # The first item is the question number, which should match
- # the question number in your html form. The second item is the answer
- # to the question. The third item is a description that explains
- # the answer to the user once they've either gotten it wrong or right.
- #
- # Each answer should take up one line in your answer file, which
- # should end with an ".an" extension. For example, if your first
- # quiz was called "FirstQuiz", the answer file would be "FirstQuiz.an"
- #
- # Your html form will need some hidden inputs:
- #
- # <input type=hidden name=Quiz value="1">
- # This contains the name of the quiz (which also will correspond
- # to the name of the answer database). In this case, my quiz is named
- # "1" If you use numerals for your quiz names, the script has a
- # nifty feature that looks for the next quiz (in this case it would
- # be quiz number 2. If that quiz exists, the script will provide a
- # link to it. If it doesn't exist, the script will provide a link to home.
- #
- # <input type=hidden name=QuestionCount value="10">
- # This contains the number of questions in the quiz. It allows the
- # script to count the number of questions that should be answered, and
- # inform the user if they didn't answer all the questions
- #
- # <input type=hidden name=required value="1|4|7">
- # The character separating the numbers is a pipe (|).
- # This tells the script which questions to require answers to.
- # It only works on inputs of the text and select box type.You cannot
- # require answers to radio box-inputs or checkbox inputs.
- #
- # You can put an unlimited number of questions in a quiz, just be
- # sure to change the "QuestionCount" hidden input value to reflect
- # the number of questions you use.
- #
- #################################################################
- # USER CONFIGURATION SECTION
- #################################################################
- # This should be a web url to your home page. It SHOULD NOT contain
- # a trailing slash "/".
- $baseurl = "http://localhost/quiz.html";
-
- # $basedir is the UNIX directory that contains your output file. Again,
- # It should point to a directory only, not a file, and it SHOULD NOT
- # contain a trailing slash "/"
- $basedir = "C:\Certificate\Scripting\cgi1\perl\cgi-bin";
-
- #That's it! You're done!
-
- #################################################################
- # END OF USER CONFIGURATION SECTION #
- #################################################################
- #reads form data
- &parse;
- $answersfile = "$basedir/" . "$contents_by_name{'Quiz'}" . ".an";
- print "Content-type: text/html\n\n";
-
- if ($contents_by_name{'name'} eq '') {
- print "<body bgcolor=red><font color=white><h1>You must tell us your name! Please go back and try again!</h1>\n";
- exit;
- }
-
-
- @required = split(/\|/, $contents_by_name{'required'});
- foreach $required (@required) {
- for ($i = 0; $i < $count; $i++)
- {
-
- if (($fieldnames{$i} eq $required) && ($contents{$i} eq '')) {
- # print "Content-type: text/html\n\n";
- print "<body bgcolor=white><h1>Error</h1>\n";
- print "<font size=4>You must answer question number <b>$fieldnames{$i}</b>\n";
- print "<br> Please go back and complete the form\n";
- exit;
-
- }
- }
- }
-
-
- &grade;
-
-
-
- sub grade
- {
- open(ANSWERS, "$answersfile");
- @answers = <ANSWERS>;
- close (ANSWERS);
-
- print "<body bgcolor=white><font face=\"Verdana,Arial,Helvetica,Geneva\" size=4><h1>Score for $contents_by_name{'name'}</h1>\n";
-
- #print OUTFILE "<HTML><BODY>";
- for ($i = 0; $i < $count; $i++)
- {
- if (($fieldnames{$i} eq 'Quiz') || ($fieldnames{$i} eq 'QuestionCount') || ($fieldnames{$i} eq 'name') || ($fieldnames{$i} eq 'required'))
- {
- next;
- }
-
- &CompareAnswers;
-
-
- }
-
- #print "<hr noshade size=1>\n";
- print "<hr noshade size=1>\n";
- if ($rightcount + $wrongcount != $contents_by_name{'QuestionCount'}) {
- $TotalAnswered = $rightcount + $wrongcount;
-
- print "You did not answer all $contents_by_name{'QuestionCount'} questions.<Br>";
- print "Of the $TotalAnswered questions you answered, you got $rightcount correct and $wrongcount incorrect.<p>";
- &checkifnext;
-
- }
-
-
- print "You got <b>$rightcount</b> answers right.<br>You got <b>$wrongcount</b> answers wrong.<p>\n";
- &checkifnext;
-
-
- sub checkifnext {
-
- $nextquiz = $contents_by_name{'Quiz'} +1;
- $nextcomplete = "$basedir/" . "$nextquiz" . ".an";
-
-
- open(NEXTER, "$nextcomplete") || &done;
- close (NEXTER);
-
- print "<a href=quiz$nextquiz.html>Take the next quiz</a>";
- exit;
-
- }
-
-
-
-
- }
-
- sub done {
- print "<a href=$baseurl>Home</a>";
- exit;
- }
-
-
-
- sub CompareAnswers {
-
-
-
-
- foreach $answers (@answers) {
- ($QuestionNumber,$AnswerDetail,$AnswerExplain) = split(/\|/, $answers);
- $AnswerDisplay = $AnswerDetail;
-
- # chop($AnswerDetail);
-
- if ($QuestionNumber eq $fieldnames{$i}) {
- if ($AnswerDetail eq $contents{$i}) {
- print "Your answer for question $fieldnames{$i}, <b>$contents{$i},</b> is CORRECT! <i>$AnswerExplain</i><p>\n";
- $rightcount++;
-
-
- } else {
- print "<font color=red>Your answer for question $fieldnames{$i},</font><b> $contents{$i},</b><font color=red> is <b>WRONG!</b></font>";
- print " <i><b>$AnswerExplain</b></i><p>\n";
- $wrongcount++;
-
- }
- }
-
- }
- }
-
-
-
- sub parse
- {
- if ($ENV{'REQUEST_METHOD'} eq 'POST')
- {
- read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
- @pairs = split(/&/, $buffer);
- $count = 0;
- foreach $pair (@pairs)
- {
- ($name, $value) = split(/=/, $pair);
- $value =~ tr/+/ /;
- $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
- $contents_by_name{$name} = $value;
- $contents{$count} = $value;
- $fieldnames{$count} = $name;
- $count++;
- }
-
- }
- }
-