#!/usr/local/bin/perl ############################################################################ # Generic 1-x-2 question engine # # by Anders Hultman, July 1996 # # Distributed under GNU General Public License # # # # The referring form should have the following hidden name-value pairs: # # name="answerfile" # # value="{full system path to a file with the correct answers}" # # name="scorefile" # # value="{full system path to a file where the score is written}" # # # # The referring form should have the following visible name-value pairs: # # name="contestant" value="{the contestant fills in his/hers name here}" # # name="address" value="{the contestant fills in his/hers email/physical # # address here}" # # Hint for advanced users: The input to "address" is typically done with # # a plain text submission field but can also be done in some other way, # # e.g. a popup menu where the contestant chooses from a list of locations, # # countries, offices etc. # # # # The referring form should have a number of questions with selectable # # answers. None of these should be marked as default. The layout could be # # radio buttons or popup menus. Each question should give a name/value- # # pair like this: name="qn" value="a" where n is the number of the # # question and a is the answer. The answer should be one character long # # and be exact the same as the answer in the answerfile (see below). # # Any characters (except for the period character) are allowed as # # answers, but the most common would be 1x2, 1234, abcde or something # # like that. Note that any number of alternative answers is allowed. # # # # The answerfile should be formatted like 1xx212xx. followed by a newline # # character. Each character is the answer to a question (in that specified # # order) and the last character on the line should be a period # # (the "." character). # # # # The scorefile could, but doesn't have to be, in a directory accessible # # from the web. # # # ############################################################################ # Configure the script by defining these variables: # Fill in the full URL to this script $scripturl = "http://www.server.se/cgi-bin/contest.cgi"; # No more configuring below this line. ############################################################################ # Get the input read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # Split the name-value pairs @pairs = split(/&/, $buffer); $i = 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; $value =~ s/<([^>]|\n)*>//g; $value =~ s///g; if (substr($name, 0, 1) eq "q") { @given[substr($name, 1, 5)-1] = $value; $i++; } else { $FORM{$name} = $value; } } # Read the correct answers open (AFIL, "$FORM{'answerfile'}"); $answers = ; close (AFIL); # How many questions are there? # The number of questions is considered to be equal to the number of # answers the answerfile, not the number of questions answered by the # contestant, since the contestant could choose not to answer all # of the questions. $questno = index ($answers, "."); # Compare correct and given answers $correct = 0; for ($i = 0; $i <= ($questno-1); $i++) { if (substr ($answers, $i, 1) eq (@given[$i])) {$correct++ } } # Write the score open (SFIL, ">>$FORM{'scorefile'}"); $tid = time; print SFIL "$FORM{'contestant'}\t$correct correct answers of $questno\t"; print SFIL "$FORM{'address'}\t$ENV{'REMOTE_ADDR'}\t$tid\n"; close (SFIL); # Write a web page print "Content-type: text/html\n\n"; print "\n\nResults\n\n\n"; print "

Results

\n"; print "Congratulations $FORM{'contestant'}! You had $correct correct ", "answers out of $questno.

\n"; print "Back to where I came from\n"; print "\n\n";