I have a test maker script , that generates random questions from a DB
My grader script needs to grade those same random questions
it has a variable that called $questions_asked
its output is like this !146!612!145!385!655!34!122!583!119!109
the code
#! /usr/bin/perl
# user grading script
#
# May 23, 2002 ver 0.03
############################################
require "subparseform.lib";
&Parse_Form;
$test = $formdata{'test12'};
$sname = $formdata{'name'};
$date1 = $formdata{'date1'};
$numofques = $formdata{'numofques'};
$questions_asked = $formdata{'admin_test'};
###########################################
# make Page
########################################
print "content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<Script Language=\"JavaScript\">\n";
print "//------------------------------------------+\n";
print "// SmartMenus v.1 |\n";
print "// \�2001 VASIL DINKOV - PLOVDIV, BULGARIA |\n";
print "// <http://mp3.dinkov.com> http://mp3.dinkov.com | vasko\@dinkov.com
|\n";
print "//------------------------------------------+\n";
print "// For Internet Explorer 4 and Netscape 6 |\n";
print "//------------------------------------------+\n\n";
print "//==1==Defining Colors\n";
print "var menuFrameColor=\'#4d4c76\'\;\n";
print "var menuBgColor=\'#bebddf\'\;\n";
print "var menuOverBgColor=\'#fff5c6\'\;\n";
print "var menuFontColor=\'#4d4c76\'\;\n";
print "var menuOverFontColor=\'#4d4c76\'//(can be the same as
menuFontColor)\n\n
";
print "//==2==Defining TARGET for the links in the menu\n";
print "var target=\'self\'//(newWindow or self)(cannot navigate to
frame)\n\n";
print " //==3==Defining font, menu cell padding & frame border width\n";
print "var font=\'verdana,arial,helvetica\'\;";
print "var fontSize=\'10px\'\;\n";
print "var fontWeight=\'bold\'\;//(bold or normal)\n";
print " var cellPadding=\'2\'\;\n";
print " var frameBorderWidth=\'1\'\;\n";
print " //==4==Defining The Menus Width\n";
print " var menuWidth=\'140\'\;\n";
print " //==5==Defining The Menus Placement (top left corner of the
menus)\n";
print "var menuTopPixels=15\;//from document top & left corner(without
quotes)\n
";
print " var menu1LeftPixels=0\;\n";
print " var menu2LeftPixels=74\;\n";
print " var menu3LeftPixels=144\;\n";
print "var menu4LeftPixels=226\;\n";
print " var menu5LeftPixels=296\;\n";
print " //var menu6LeftPixels=400\;//(if you want more add like this)\n";
print "//==6==Defining The Menus Count (how many do you want)\n";
print " var menuCount=2\;//(without quotes)\n";
print " //=============MENU DEFINITIONS=============\n";
print " //(be sure to have the same number of array items for both href &
text)\n";
print " //===Menu 1\n";
print "href1=new Array(\'nam.cgi\',\'addmaterial.cgi\',\'adduser.php\',";
print "\'\')\;\n";
print " text1=new Array(\'Add a Patron\',\'Add Material\',\'Add a User\',";
print "\'Edit a User\')\;\n";
print "function MV1(){var x=\'\'\;for(i=0\;i<href1.length\;i++)";
print "{x=x+\"<tr><td class=mxz onclick=doGo(\'\"+href1[i]+\"\')";
print " onmouseover=ov(this) onmouseout=ou(this)>\"+text1[i]+\"</td>";
print "</tr>\"}\;return x}\;\n";
print " //===Menu 1 END \n\n\n";
print "//===Menu 2\n";
print " href2=new Array(\'checkout_report.cgi\')\;\n";
print " text2=new Array(\'Checkout Report\')\;\n\n";
print " function MV2(){var x=\'\'\;for(i=0\;i<href2.length\;i++)";
print "{x=x+\"<tr><td class=mxz //onclick=doGo(\'\"+href2[i]+\"\')";
print " onmouseover=ov(this) onmouseout=ou(this)>\"+text2[i]+\"</td>";
print "</tr>\"}\;return x}\;\n";
print " //===Menu 2 END \n\n\n";
print "//=============MENU DEFINITIONS END============= \n";
print "// THE SERIOUS SCRIPT PLEASE DO NOT TOUCH\n";
print "if(document.all){topY=menuTopPixels}else{topY=menuTopPixels-2}\;";
print "/*NS bud*/function rV(){q=\'\'\;for(z=1\;z<=menuCount\;z++)";
print "{if(document.all){q=q+\"m\"+z+\".style.display=\'none\'\;\"}";
print
"else{q=q+\"document.getElementById(\'m\"+z+\"\').style.display=\'none\'\;\"
}}\;return q}\;";
print "eval(\"function hide(a){if(a.clientY<=topY){\"+rV()+\"}}\;";
print "function hide2(){\"+rV()+\"}\")\;eval(\"function ov(ok)";
print "{ok.style.background=\'\"+menuOverBgColor+\"\'\;ok.style.";
print "color=\'\"+menuOverFontColor+\"\'}\;function ou(ok)";
print "{ok.style.background=\'\"+menuBgColor+\"\'\;ok.style.";
print "color=\'\"+menuFontColor+\"\'}\;function doGo(ok)";
print "{var tgt=\'\"+target+\"\'\;if(tgt==\'newWindow\'){window.open(ok)";
print "}else{document.location.href=ok}}\;function
show(a){if(document.all)";
print "{eval(a).style.display=\'\'}else{document.getElementById(a).style.";
print "display=\'\'}}\")\;document.write(\"<style>.mxz{font-family:\"";
print "+font+\"\;font-size:\"+fontSize+\"\;color:\"+menuFontColor+\"\;";
print
"font-weight:\"+fontWeight+\"\;background:\"+menuBgColor+\"\;padding:\"";
print "+cellPadding+\"\;cursor:default}</style>\")\;function buildTheAsses";
print "(){for(j=1\;j<=menuCount\;j++){document.write(\"<div
style=position:";
print "absolute\;top:\"+(menuTopPixels-1)+\"\;left:\"+(eval(\'menu\'+j+\'";
print "LeftPixels\')-100)+\"\;z-index:3\;display:none id=\"+(\'m\'+j)";
print "+\"><table cellpadding=0 cellspacing=0 border=0><tr><td colspan=3";
print " height=1></td></tr><tr><td width=100 onmouseover=hide2()></td>";
print "<td bgcolor=\"+menuFrameColor+\"><table width=\"+menuWidth+\"";
print " cellspacing=\"+frameBorderWidth+\"
border=0>\"+eval(\'MV\'+j+\'()\')";
print "+\"</table></td><td width=100 onmouseover=hide2()></td></tr><tr>";
print "<td colspan=3 height=200 onmouseover=hide2()></td></tr>";
print "</table></div>\")}}\;buildTheAsses()\n\n";
print "</script>\n";
print "<BR>\n";
print "<BR><title> Welcome ";
print "$sname";
print " </title></head>\n";
print "</head>\n";
print "<body bgcolor=\"#FFFFFF\" text=\"000000\" \n";
print ">\n";
print "<h1 align=\"center\">$sname </h1> \n";
print "<h3 align=\"center\"> Test results for Test : $test </h3>\n";
print "<hr>\n";
##########################################
# connect to DB
############################
use DBI;
my $database = "tests";
my $data_source = "DBI:mysql:$database";
my $username = "user";
my $password = "password";
$dbh =DBI ->connect($data_source, $username, $password) or die "cant connect
to
$data_source : my $dbh-> errstr\n";
my $sth1 = $dbh -> prepare("select * from tests where num = ? ");
while( $questions_asked =~ /!([^!]*)/g) {
my $num1 = $1;
print " This is a test: $num1<BR>";
my $sth1 = $dbh -> prepare("select * from tests where num = ? ");
### }
$sth1 -> execute($num1) or die " unable to execute query ";
#$sth1 -> finish;
my $array_ref = $sth1 -> fetchall_arrayref();
########################################################
### Get Date ###
########################################################
@days = ("Sunday","Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday");
@months = ("January","February","March","April","May","June",
"July","August","September","October","November",
"December");
($sec,$min,$hr,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
$year = $year + 1900; #prints correct date
$today_n = $days[$wday];
print "<div align=\"center\">\n";
print "<center>\n";
print "<table border=\"1\" width=\"80%\">\n";
$tot_cor = 0;
$tot_ques = 0;
foreach $row(@$array_ref){
$they_ans="";
my($num,$subject,$ques,$quesA,$quesB,$quesC,$quesD,$quesE,$ans,$corr_a,$corr
_b,$corr_c,$corr_d,$corr_e,$exp) = @$row;
####################################
#correct answer for this question
###################################
$corr = $num."c_ans";
$corr1 = $formdata{$corr};
###################################
# Total answers for the question
###################################
$totans = $num."tot_ans1";
$totans1 = $formdata{$totans};
$tot_ques++;
#####################################
### This process goes through all the questions
### And adds them into on variable
#############################################
#####################################
## answer 1 ######
#####################################
$ans1 = $num."a";
$ans1a = $formdata{$ans1};
if($ans1a eq "ON") {
$they_ans = "A";
}
#####################################
## answer 2 ######
#####################################
$ans1 = $num."b";
$ans1b = $formdata{$ans1};
if($ans1b eq "ON"){
$they_ans = $they_ans."B";
}
#####################################
## answer 3 ######
#####################################
$ans1 = $num."c";
$ans1c = $formdata{$ans1};
if($ans1c eq "ON") {
$they_ans = $they_ans."C";
}
#####################################
## answer 4 ######
#####################################
$ans1 = $num."d";
$ans1d = $formdata{$ans1};
if($ans1d eq "ON") {
$they_ans = $they_ans."D";
}
#####################################
## answer 5 ######
#####################################
$ans1 = $num."e";
$ans1e = $formdata{$ans1};
if($ans1e eq "ON") {
$they_ans = $they_ans."E";
}
#####################################
## answer 6 ######
#####################################
$ans1 = $num."f";
$ans1f = $formdata{$ans1};
if($ans1f eq "ON") {
$they_ans = $they_ans."F";
}
if($corr1 eq $they_ans) {
$outcome = "Correct";
$tot_cor++;
}
if($corr1 ne $they_ans) {
$outcome = "Incorrect";
}
### add to test analysis
#
# connect to DB
############################
my $database = "cram";
my $data_source = "DBI:mysql:$database";
my $username = "root";
my $password = "elaine";
$dbh5 = DBI ->connect($data_source, $username, $password);
my $sth4 = $dbh5 -> prepare("insert into
testhistory(sname,testname,date,question,theyans,outcome)values('$sname','$s
ubject','$date1','$num','$they_ans','$outcome')");
$sth4 -> execute ;
#$sth1 -> finish;
$dbh5->disconnect;
if($outcome ne "Correct"){
print "<tr>\n";
print "<td width=\"3%\">$num</td>\n";
print "<td width=\"73%\"> $ques </td>\n";
print "</tr><tr>\n";
print "<td width=\"33%\">Correct answer is : $corr1</td>\n";
print "<td width=\"64%\">You answered $they_ans </td>\n";
print "</tr><tr>\n";
print "<td>The Reason</td>\n";
print "<td>$exp </td>\n";
print "</tr><tr><td bgcolor=\"#FFFF00\"> </td></tr>\n";
}
}
}
print "</table>\n";
print "</center>\n";
print "</div>\n";
print "<center>\n";
#########################
## get total questions and corrects
$aver = $tot_cor/$tot_ques;
$aver = sprintf "%.2f", $aver;
print " Your Score is $aver \n";
$status = "Passed";
if($aver < 0.70)
{
$status = "Failed";
}
print "<BR>Yours status is : <h2> $status </h2>";
print " $questions_asked";
#####################################################
## insert Grades into DB
#####################################################
#
# connect to DB
############################
# use DBI;
# my $database = "sobtmedia";
# my $data_source = "DBI:mysql:$database";
# my $username = "root";
# my $password = "elaine";
# $dbh = DBI ->connect($data_source, $username, $password);
my $sth1 = $dbh -> prepare("insert into
scores(name,test,score,result,date)values('$sname','$test','$aver','$status'
,'$date1')");
$sth1 -> execute;
$sth1 -> finish;
my $sth1 = $dbh -> prepare("update user1 set lasttest = '$test' where
username = '$sname'");
$sth1 -> execute;
$sth1 -> finish;
$dbh->disconnect;
$sth1 -> execute;
$sth1 -> finish;
$dbh->disconnect;
print "test is : $checkout1";
print "</body>\n";
print "</html>\n";
thanks
Jim