#!/usr/bin/perl
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#open(FILE,"input.dat");
#read(FILE, $buffer, 300);
#close(FILE);
chomp($buffer);
# Split the name-value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
# Un-Webify plus signs and %-encoding
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s///g;
if ($allow_html != 1) {
$value =~ s/<([^>]|\n)*>//g;
}
$FORM{$name} = $value;
}
$StudentName = $FORM{'StudentName'};
$StudentNo = $FORM{'StudentNo'};
if (($StudentName eq "") || ($StudentNo eq ""))
{
print "Content-type: text/html
沒有輸入姓名/座號
沒有輸入姓名/座號,請重新作答!
";
exit;
}
$RecordFile = $FORM{'RecordFile'};
$AnswerFile = $FORM{'AnswerFile'};
$NumberOfTests = $FORM{'NumberOfTests'};
# read the answer file
open (FILE,"$AnswerFile") || die "Can't Open $AnswerFile: $!\n";
@LINES=;
close(FILE);
$SIZE=@LINES;
$scores = 0;
$numErrors = 0;
# match the answers
for ($i=0;$i<$SIZE;$i++)
{
$_=$LINES[$i];
chomp;
s/,\s*/,/g;
@items = split(/,/);
$numItems = @items;
# print "@items\n";
if ($numItems == 3) # single choice
{
if ($FORM{$items[0]} == $items[2])
{
$scores += $items[1];
}
else
{
$errors[$numErrors] = $i+1;
$numErrors += 1;
}
# print "Single: $items[0] $scores\n";
}
else # multiple choice
{
$correct = 1;
for ($j=2;$j<=$numItems;$j++)
{
$name="$items[0].$items[$j]";
if ($FORM{$name} != $items[$j])
{
$correct = 0;
break;
}
}
if ($correct == 1)
{
$scores += $items[1];
}
else
{
$errors[$numErrors] = $i+1;
$numErrors += 1;
}
# print "Multiple: $items[0] $scores\n";
}
}
#print "Errors: @errors\n";
$numCorrects = $NumberOfTests - $numErrors;
print "Content-type: text/html
答題結果
答題結果:
你答對了 $numCorrects 題,
總分是 $scores 分,
下列題目答錯了: {@errors},
請再仔細想一下。
";
#append student scores to log files
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$month = ($mon + 1);
@months = ("January","February","March","April","May","June","July","August","September","October","November","December");
$year += 1900;
$date1 = sprintf("%s %02d, %4d %02d:%02d:%02d",$months[$mon],$mday,$year,$hour,$min,$sec);
$year %= 100;
$date2 = sprintf("%02d:%02d:%02d %02d/%02d/%02d",$hour,$min,$sec,$month,$mday,$year);
$date3 = sprintf("%02d/%02d/%02d",$month,$mday,$year);
if (! open (FILE, "$RecordFile"))
{
open (FILE, ">$RecordFile") || die "Can't Create $RecordFile: $!\n";
print FILE "$NumberOfTests\n";
}
close(FILE);
open (FILE,">>$RecordFile") || die "Can't Open $RecordFile: $!\n";
print FILE "$date2, $StudentName, $StudentNo, $scores";
foreach $error (@errors)
{
print FILE ", $error";
}
print FILE "\n";
close(FILE);