amccord
02-08-2007, 03:56 PM
This is a quiz script that I had downloaded, but there is no support for it. I was hoping you guys might be able to help.
I've read enough tutorials on cgi to figure out how to customize everything except this part.
The questions are displayed randomly (not always in the same order), but the answer choices are not. Is there any way that I can change the following script to where it will list the answer choices randomly?
Any help is GREATLY appreciated!
sub ShowQuiz {
my $quiz = shift;
my $num_ques = getNumLine("$quiz.db");
$limit = param('number') > 0 ? param('number') : $num_ques;
$limit = $num_ques if $limit > $num_ques;
for ($i=0;$i<$num_ques;$i++) {$array[$i] = $i;}
for ($i=0;$i<$num_ques;$i++) {
$x = $array[int(rand($num_ques))];
$y = $array[int(rand($num_ques))];
$temp = $array[$x];
$array[$x] = $array[$y];
$array[$y] = $temp;
}
splice(@array, $limit);
flock(2, DB);
open(DB, "$db_dir/$quiz.db") or error("Cannot open $quiz.db: $!");
@lines = <DB>;
close(DB);
flock(8, DB);
print "<form action=\"$script\" method=\"POST\">\n";
print "<table border=0 cellspacing=0 cellpadding=0 bgcolor=#FFFFFF width=95%><tr><td>\n";
print "<table border=0 cellspacing=1 cellpadding=4 width=100%>\n";
print "<tr><td bgcolor=#FFFFFF><font$font_para size=4><b><i>".getQuizName($quiz)."</i></b></font><br></td></tr>\n";
$i = 1;
foreach $linenum (@array) {
my $j = 'color' . (($i % 2) + 1);
($qtype, $question, $qimage, $answer, $aimage, $explanation, $choice) = split(/\|\|/, $lines[$linenum]);
print "<tr bgcolor=#DFE9F9><td width=100%><font$font_para size=2><b>$i. $question</b>";
print "<br><img src=\"http://$qimage\">" if $qimage ne "";
if ($qtype eq "mc") {
chomp($choice);
@choices = split(/\`\`/, $choice);
foreach $choice (@choices) {
print "<br><input type=\"radio\" name=\"$linenum\" value=\"$choice\">$choice";
}
}
if ($qtype eq "sa") {
print "<br>Answer: <input type=\"text\" name=\"$linenum\" size=30>";
}
if ($qtype eq "nu") {
print "<br>Answer: <input type=\"text\" name=\"$linenum\" size=30> (Enter only numeric value)";
}
if ($qtype eq "tf") {
print "<br><input type=\"radio\" name=\"$linenum\" value=\"t\">True <input type=\"radio\" name=\"$linenum\" value=\"f\">False";
}
print "</font></td></tr>\n";
$i++;
}
I've read enough tutorials on cgi to figure out how to customize everything except this part.
The questions are displayed randomly (not always in the same order), but the answer choices are not. Is there any way that I can change the following script to where it will list the answer choices randomly?
Any help is GREATLY appreciated!
sub ShowQuiz {
my $quiz = shift;
my $num_ques = getNumLine("$quiz.db");
$limit = param('number') > 0 ? param('number') : $num_ques;
$limit = $num_ques if $limit > $num_ques;
for ($i=0;$i<$num_ques;$i++) {$array[$i] = $i;}
for ($i=0;$i<$num_ques;$i++) {
$x = $array[int(rand($num_ques))];
$y = $array[int(rand($num_ques))];
$temp = $array[$x];
$array[$x] = $array[$y];
$array[$y] = $temp;
}
splice(@array, $limit);
flock(2, DB);
open(DB, "$db_dir/$quiz.db") or error("Cannot open $quiz.db: $!");
@lines = <DB>;
close(DB);
flock(8, DB);
print "<form action=\"$script\" method=\"POST\">\n";
print "<table border=0 cellspacing=0 cellpadding=0 bgcolor=#FFFFFF width=95%><tr><td>\n";
print "<table border=0 cellspacing=1 cellpadding=4 width=100%>\n";
print "<tr><td bgcolor=#FFFFFF><font$font_para size=4><b><i>".getQuizName($quiz)."</i></b></font><br></td></tr>\n";
$i = 1;
foreach $linenum (@array) {
my $j = 'color' . (($i % 2) + 1);
($qtype, $question, $qimage, $answer, $aimage, $explanation, $choice) = split(/\|\|/, $lines[$linenum]);
print "<tr bgcolor=#DFE9F9><td width=100%><font$font_para size=2><b>$i. $question</b>";
print "<br><img src=\"http://$qimage\">" if $qimage ne "";
if ($qtype eq "mc") {
chomp($choice);
@choices = split(/\`\`/, $choice);
foreach $choice (@choices) {
print "<br><input type=\"radio\" name=\"$linenum\" value=\"$choice\">$choice";
}
}
if ($qtype eq "sa") {
print "<br>Answer: <input type=\"text\" name=\"$linenum\" size=30>";
}
if ($qtype eq "nu") {
print "<br>Answer: <input type=\"text\" name=\"$linenum\" size=30> (Enter only numeric value)";
}
if ($qtype eq "tf") {
print "<br><input type=\"radio\" name=\"$linenum\" value=\"t\">True <input type=\"radio\" name=\"$linenum\" value=\"f\">False";
}
print "</font></td></tr>\n";
$i++;
}