I have a script that generates test questions and grades them . I was
asked to make them generate in a random order , can someone point me in the
right direction
They come in two files make_test.cgi and grade.cgi
#! /usr/bin/perl
#
# make test for Cram Program
#
# version 0.02 mar 18 2002
#
############################################
require "subparseform.lib";
&Parse_Form;
$name = $formdata{'name'};
$test = $formdata{'test1'};
##########################################
# connect to DB
############################
use DBI;
my $database = "cram";
my $data_source = "DBI:mysql:$database";
my $username = "root";
my $password = "elaine";
####################################
## make sure they havent taken the test before
####################################
# $dbh = DBI -> connect($data_source, $username, $password)
# my $sth = $dbh1 -> prepare("select * from scores where name = '$name' and
test = '$test'");
# $sth -> execute
# $result = "$result
# if($result ne "") {
# my Arrary
######################################################
### Make Random #
######################################################
$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 subject = '$test' ");
$sth1 -> execute or die " unable to execute query ";
#$sth1 -> finish;
##########################################
# make count
$count = 0;
###########################################
# make Page
########################################
print "content-type: text/html\n\n";
print "<html>\n";
print "<head><title> Welcome ";
print "$name";
print " </title></head>\n";
print "</head>\n";
print "<body bgcolor=\"#FFFFFF\" text=\"000000\" \n";
print "background=\"notebook.jpg\">\n\n";
print "<h3 align=\"right\">$name </h3> \n";
print "<h3 align=\"right\">$test Test</h3> \n";
print "<hr>\n";
print "<div align=\"center\">\n";
print "<center>\n";
print "<table border=\"1\" width=\"80%\">\n";
print "<form action=\"grade.cgi\"";
print " method=\"post\" ";
print " name=\"cramtest\">\n";
print "<input type=\"hidden\" name=\"name\" value=\"$name\">\n";
print "<input type=\"hidden\" name=\"test12\" value=\"$test\">\n";
my $array_ref = $sth1 -> fetchall_arrayref();
foreach $row(@$array_ref){
##############################################################
# Count Routine
##############################################################
#
$count ++;
#
my($num, $subject, $ques,$quesA, $quesB, $quesC, $quesD,$quesE,$ans) =
@$row;
$tot_ans = 0;
print "<tr>\n";
print "<td width=\"3%\">$num</td>\n";
print "<td width=\"3%\"> </td>\n";
print "<td width=\"84%\" bgcolor=\"#FFFF00\">$ques\n";
$c_ans = $num."c_ans";
print " <input type=\"hidden\" name=\"$c_ans\"
value=\"$ans\">\n";
print " </td>\n";
print "</tr>\n";
$ques1a = $num."a";
if ($quesA ne ""){
$tot_ans++;
print "<tr>\n\n";
print "<td width=\"3%\"> <input type=\"checkbox\" name=\"$ques1a\"\n";
print "value=\"ON\"></td>\n\n";
print "<td width=\"3%\"> A.</td>\n";
print "<td width=\"84%\">$quesA </td>\n";
print "</tr>\n";
}
$ques1b = $num."b";
if ($quesB ne ""){
$tot_ans++;
print "<tr>\n";
$num_a = $num;
print "<td width=\"3%\"> <input type=\"checkbox\" name=\"$ques1b\" ";
print "value=\"ON\"></td>\n";
print "<td width=\"3%\"> B.</td>\n";
print "<td width=\"84%\">$quesB</td>\n";
print "</tr>\n";
}
$ques1c = $num."c";
if ($quesC ne ""){
$tot_ans++;
print "<tr>\n";
print "<td width=\"3%\"> <input type=\"checkbox\" name=\"$ques1c\" ";
print "value=\"ON\"></td>\n";
print "<td width=\"3%\"> C.</td>\n";
print "<td width=\"84%\">$quesC</td>\n";
print "</tr>\n";
}
$ques1d = $num."d";
if ($quesD ne ""){
$tot_ans++;
print "<tr>\n";
print "<td width=\"3%\"> <input type=\"checkbox\" name=\"$ques1d\" ";
print "value=\"ON\"></td>\n";
print "<td width=\"3%\"> D.</td>\n";
print "<td width=\"84%\">$quesD </td>\n";
print "</tr>\n";
}
if ($quesE ne ""){
$tot_ans++;
$ques1e = $num."e";
print "<tr>\n";
print "<td width=\"3%\"> <input type=\"checkbox\" name=\"$ques1e\" ";
print "value=\"ON\"></td>\n";
print "<td width=\"3%\"> E.</td>\n";
print "<td width=\"84%\">$quesE </td>\n";
print "</tr>\n";
}
$ques1f = $num."f";
if ($quesf ne ""){
$tot_ans++;
print "<tr>\n";
print "<td width=\"3%\"> <input type=\"checkbox\" name=\"$ques1f\" ";
print "value=\"ON\"></td>\n";
print "<td width=\"3%\"> f.</td>\n";
print "<td width=\"84%\">$quesf </td>\n";
print "</tr>\n";
}
$tot_ans1 = $num."tot_ans1";
print "<input type=\"hidden\" name=\"$tot_ans1\" value=\"$tot_ans\">\n";
}
print "<input type=\"hidden\" name=\"count\" value=\"$count\">\n";
print "</table>\n";
print "<input type=\"Submit\" value=\"Submit to Be Graded\">\n";
print "</form>\n";
print "</center>\n";
print "</div>\n";
print "</body>\n";
print "</html>\n";
#! /usr/bin/perl
# user grading script
#
# Mar 18, 2002 ver 0.02
############################################
require "subparseform.lib";
&Parse_Form;
$test = $formdata{'test12'};
$sname = $formdata{'name'};
##########################################
# connect to DB
############################
use DBI;
my $database = "cram";
my $data_source = "DBI:mysql:$database";
my $username = "root";
my $password = "elaine";
$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 subject = '$test' ");
$sth1 -> execute or die " unable to execute query ";
#$sth1 -> finish;
###########################################
# make Page
########################################
print "content-type: text/html\n\n";
print "<html>\n";
print "<head><title> Welcome ";
print "$sname";
print " </title></head>\n";
print "</head>\n";
print "<body bgcolor=\"#FFFFFF\" text=\"000000\" \n";
print "background=\" \n'>https://kins2158/cram/notebook.jpg\"
<https://kins2158/cram/notebook.jpg\> >\n";
print "<h1 align=\"center\">$sname </h1> \n";
print "<h3 align=\"center\"> Test results for Test : $test </h3>\n";
print "<hr>\n";
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','0000-00-00','$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 : $status";
#####################################################
## 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)values('$sname','$test','$aver','$status')");
$sth1 -> execute;
$sth1 -> finish;
$dbh->disconnect;
print "</body>\n";
print "</html>\n";