PDA

View Full Version : Selecting random answer choices??


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++;

}

amccord
02-08-2007, 09:30 PM
Solved by the help of a lovely person, KevinADC. To have the answers displayed randomly, simply replace this code:
if ($qtype eq "mc") {
chomp($choice);
@choices = split(/\`\`/, $choice);
foreach $choice (@choices) {
print "<br><input type=\"radio\" name=\"$linenum\" value=\"$choice\">$choice";
}
}


With this code:
use List::Util qw/shuffle/;
if ($qtype eq "mc") {
chomp($choice);
my @choices = split(/\`\`/, $choice);
@choices = shuffle(@choices);
foreach $choice (@choices) {
print "<br><input type=\"radio\" name=\"$linenum\" value=\"$choice\">$choice";
}
}


And viola! Thanks again Kevin

KevinADC
02-09-2007, 12:16 AM
no problemo ;)

3000today
02-11-2007, 08:51 PM
>> but there is no support for it.

I don't think you'll find an easier quiz script generator than this software tool:

http://www.pcshareware.com/qzscript.htm

It saves you a ton of coding, albeit it is in Javascript.

HTH!

3000T

KevinADC
02-12-2007, 12:49 AM
Some people prefer to write their own scripts, it's as much about the journey as the destination.