home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Javascript / InteractiveWebDesignJavascript / Scripting / cgi1 / perl / cgi-bin / quiz.pl < prev    next >
Encoding:
Perl Script  |  2001-07-18  |  6.7 KB  |  221 lines

  1. #!/usr/local/bin/perl
  2. #
  3. #
  4. # Quiz.pl
  5. # by Leif M. Wright
  6. # http://www.conservatives.net/atheist/scripts/
  7. # scripts@conservatives.net
  8. #################################################################
  9. # Quiz.pl is a simple little quiz script that lets you put an
  10. # unlimited number of quizzes on your site for your users to 
  11. # take. Operation is simple.
  12. # You only have to modify two variables in the script itself, and
  13. # they are defined below in the USER CONFIGURATION section.
  14. # Each quiz is a separate html document with several hidden
  15. # inputs, some of which are required. (they're explained below)
  16. # Each quiz gets its answers from a pipe-delimited database that 
  17. # looks like this:
  18. #         
  19. #         1|yes|The answer to this question is obviously yes, you dolt
  20. # The first item is the question number, which should match
  21. # the question number in your html form. The second item is the answer
  22. # to the question. The third item is a description that explains
  23. # the answer to the user once they've either gotten it wrong or right.
  24. # Each answer should take up one line in your answer file, which
  25. # should end with an ".an" extension. For example, if your first 
  26. # quiz was called "FirstQuiz", the answer file would be "FirstQuiz.an"
  27. # Your html form will need some hidden inputs:
  28. #      <input type=hidden name=Quiz value="1">
  29. #      This contains the name of the quiz (which also will correspond
  30. #      to the name of the answer database). In this case, my quiz is named 
  31. #      "1" If you use numerals for your quiz names, the script has a 
  32. #      nifty feature that looks for the next quiz (in this case it would
  33. #      be quiz number 2. If that quiz exists, the script will provide a 
  34. #      link to it. If it doesn't exist, the script will provide a link to home.
  35. #      
  36. #      <input type=hidden name=QuestionCount value="10">
  37. #      This contains the number of questions in the quiz. It allows the 
  38. #      script to count the number of questions that should be answered, and
  39. #      inform the user if they didn't answer all the questions
  40. #      
  41. #      <input type=hidden name=required value="1|4|7">
  42. #      The character separating the numbers is a pipe (|).
  43. #      This tells the script which questions to require answers to.
  44. #      It only works on inputs of the text and select box type.You cannot
  45. #      require answers to radio box-inputs or checkbox inputs.
  46. #      
  47. # You can put an unlimited number of questions in a quiz, just be 
  48. # sure to change the "QuestionCount" hidden input value to reflect
  49. # the number of questions you use.
  50. #
  51. #################################################################
  52. #      USER CONFIGURATION SECTION
  53. #################################################################
  54. # This should be a web url to your home page. It SHOULD NOT contain
  55. # a trailing slash "/".
  56. $baseurl = "http://localhost/quiz.html";
  57.  
  58. # $basedir is the UNIX directory that contains your output file. Again,
  59. # It should point to a directory only, not a file, and it SHOULD NOT
  60. # contain a trailing slash "/"
  61. $basedir = "C:\Certificate\Scripting\cgi1\perl\cgi-bin";
  62.  
  63. #That's it! You're done!
  64.  
  65. #################################################################
  66. #      END OF USER CONFIGURATION SECTION      #
  67. #################################################################
  68. #reads form data
  69. &parse;
  70. $answersfile = "$basedir/" . "$contents_by_name{'Quiz'}" . ".an";
  71. print "Content-type: text/html\n\n";
  72.  
  73.   if ($contents_by_name{'name'} eq '') {
  74.       print "<body bgcolor=red><font color=white><h1>You must tell us your name! Please go back and try again!</h1>\n";
  75.       exit;
  76.   }
  77.   
  78.   
  79.   @required = split(/\|/, $contents_by_name{'required'});
  80. foreach $required (@required) {
  81.     for ($i = 0; $i < $count; $i++)
  82.     {
  83.         
  84.     if (($fieldnames{$i} eq $required) && ($contents{$i} eq '')) {
  85. #    print "Content-type: text/html\n\n";
  86.         print "<body bgcolor=white><h1>Error</h1>\n";
  87.         print "<font size=4>You must answer question number <b>$fieldnames{$i}</b>\n";
  88.         print "<br> Please go back and complete the form\n";
  89.     exit;
  90.     
  91.     }
  92.     }
  93. }
  94.  
  95.   
  96. &grade;
  97.   
  98.  
  99.  
  100. sub grade
  101. {
  102. open(ANSWERS, "$answersfile");
  103. @answers = <ANSWERS>;
  104. close (ANSWERS);
  105.     
  106.     print "<body bgcolor=white><font face=\"Verdana,Arial,Helvetica,Geneva\" size=4><h1>Score for $contents_by_name{'name'}</h1>\n";
  107.  
  108. #print OUTFILE "<HTML><BODY>";
  109. for ($i = 0; $i < $count; $i++)
  110. {
  111.     if (($fieldnames{$i} eq 'Quiz') || ($fieldnames{$i} eq 'QuestionCount') || ($fieldnames{$i} eq 'name') || ($fieldnames{$i} eq 'required'))
  112.     {
  113.         next;
  114.     }
  115.     
  116.     &CompareAnswers;
  117.     
  118.     
  119. }
  120.  
  121. #print "<hr noshade size=1>\n";
  122.     print "<hr noshade size=1>\n";
  123.     if ($rightcount + $wrongcount != $contents_by_name{'QuestionCount'}) {
  124.         $TotalAnswered = $rightcount + $wrongcount;
  125.         
  126.         print "You did not answer all $contents_by_name{'QuestionCount'} questions.<Br>";
  127.         print "Of the $TotalAnswered questions you answered, you got $rightcount correct and $wrongcount incorrect.<p>";
  128.     &checkifnext;
  129.     
  130.     }
  131.     
  132.         
  133.     print "You got <b>$rightcount</b> answers right.<br>You got <b>$wrongcount</b> answers wrong.<p>\n";
  134.     &checkifnext;
  135.     
  136.     
  137. sub checkifnext {
  138.     
  139.         $nextquiz = $contents_by_name{'Quiz'} +1;
  140.     $nextcomplete = "$basedir/" . "$nextquiz" . ".an";
  141.  
  142.     
  143.     open(NEXTER, "$nextcomplete") || &done;
  144.     close (NEXTER);
  145.         
  146.     print "<a href=quiz$nextquiz.html>Take the next quiz</a>";
  147.     exit;
  148.     
  149. }
  150.  
  151.     
  152.     
  153.  
  154. }
  155.  
  156. sub done {
  157.     print "<a href=$baseurl>Home</a>";
  158.     exit;
  159. }
  160.  
  161.  
  162.  
  163. sub CompareAnswers {
  164.   
  165.  
  166.  
  167.  
  168.    foreach $answers (@answers) {
  169.         ($QuestionNumber,$AnswerDetail,$AnswerExplain) = split(/\|/, $answers);
  170.     $AnswerDisplay = $AnswerDetail;
  171.     
  172. #    chop($AnswerDetail);
  173.     
  174.         if ($QuestionNumber eq $fieldnames{$i}) {
  175.             if ($AnswerDetail eq $contents{$i}) {
  176.                 print "Your answer for question $fieldnames{$i}, <b>$contents{$i},</b> is CORRECT! <i>$AnswerExplain</i><p>\n";
  177.         $rightcount++;
  178.         
  179.         
  180.             } else {
  181.         print "<font color=red>Your answer for question $fieldnames{$i},</font><b> $contents{$i},</b><font color=red> is <b>WRONG!</b></font>";
  182.         print " <i><b>$AnswerExplain</b></i><p>\n";
  183.         $wrongcount++;
  184.         
  185.             }
  186.         }
  187.         
  188. }
  189. }
  190.  
  191.  
  192.  
  193. sub parse
  194. {
  195. if ($ENV{'REQUEST_METHOD'} eq 'POST')
  196. {
  197.     read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  198.     @pairs = split(/&/, $buffer);
  199.     $count = 0;
  200.     foreach $pair (@pairs)
  201.     {
  202.         ($name, $value) = split(/=/, $pair);
  203.         $value =~ tr/+/ /;
  204.         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  205.         $contents_by_name{$name} = $value;
  206.         $contents{$count} = $value;
  207.         $fieldnames{$count} = $name;
  208.         $count++;
  209.     }
  210.  
  211. }
  212. }
  213.