| 
#!/usr/local/bin/perl
# This is how to call quiz http://www.thaiall.com/quiz/quizsam.pl?thai&5
$basedir = join '',$ENV{DOCUMENT_ROOT},"/quiz/";
&checkgetsys;
&header;	
if ($mainquiz eq "question") { &getquestion; } else  { &checkanswer; }
print'<hr><center>bye bye bye bye</body></html>';
# ===== end of main =====
sub checkgetsys {
$getsys = $ENV{"QUERY_STRING"};
$getsys =~ tr/+/ /;
$getsys =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$getsys =~ s/~!/~!/g;
$_ = $getsys;
$x = tr/#-@A-Za-zก-ฮฯ-๙/ /cs;
$getsysurl = $_;
$mainquiz = "question";
@gs = split /&/, $getsysurl;
$subj = $gs[0];  # สำรองไว้ให้เขียนโปรแกรมเดียว แต่ใช้กับข้อสอบได้หลายชุด
$amt = $gs[1]; # จำนวนข้อสอบที่ต้องการทำ
$idquiz = $^T;
$title = "ทดสอบความรู้รอบตัว ชุดประเทศไทย";
if ($subj ne "thai") { $subj = "thai"; }
if ($amt ne "3" and $amt ne "5") { $amt = "5"; }
$fname = "quizsam.qiz";
# check answer input
$getsys = <STDIN>;
$getsys =~ tr/+/ /;
$getsys =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$getsys =~ s/~!/~!/g;
$_ = $getsys;
$x = tr/#-@A-Za-zก-ฮฯ-๙/ /cs;
$getsysform = $_;
if (length($getsysform) > 5) {
	$mainquiz = "answer";
	@gs = split /&/, $getsysform;
	foreach $pair (@gs) {
		  ($name, $value) = split(/=/, $pair);
		  $FORM{$name} = $value;
		  if (length($name)>4) {  push @idnum, $name; }
	}
	$subj = $FORM{'subj'};	
	$amt = $FORM{'amt'};	
}
}
sub header	{
print"Content-type:text/html\n\n";
print'<html><head><title>ThaiAll >> ',$title,'</title></head>',"\n";
print'<body bgcolor=#fffffd><font face="microsoft sans serif" size=0><center>';
print'<a href=http://www.thaiall.com/quiz/>Thaiall Quiz Online</a><br>';
print'[<a href=/quiz/quizsam.pl?thai&3>ชุด 3 ข้อ</a> | ';
print'<a href=/quiz/quizsam.pl?thai&5>ชุด 5 ข้อ</a>]</center><hr>';
}
sub getquestion {
print'<center>You open ',$title,' in type ',$amt,' </center><hr>';
open(fn, "<$basedir$fname" );
@bfn = <fn>;
close(fn);
$cntr = @bfn;
$dif = ($idquiz % 5) + 2 ;
for $r (1 .. $amt) {
	$onr = ($idquiz / $r * $dif) % $cntr;	
	push @keepar , $bfn[$onr];			
}
print '<font size=2>';
print '<ul><form method=post action=/quiz/quizsam.pl>';
print '<input name=subj type=hidden value=',$subj,'>';
print '<input name=amt type=hidden value=',$amt,'>';
for $j (0 .. $amt-1) {	
	@recq = split ':',$keepar[$j];
	$num = $recq[0];
	$ques = $recq[1];
	$c1 = $recq[2];
	$c2 = $recq[3];
	$c3 = $recq[4];
	$c4 = $recq[5];
	$ans = substr($recq[6],0,1);
	print $j+1,". <font size=3>",$ques," <font size=0>[",$num,"]</font></font><br>","\n";
	print "<ul>";
	print "<input type=radio name=",$num," value=1>",$c1,"<br>";
	print "<input type=radio name=",$num," value=2>",$c2,"<br>";
	print "<input type=radio name=",$num," value=3>",$c3,"<br>";
	print "<input type=radio name=",$num," value=4>",$c4,"<br>";
	print "<input type=radio name=",$num," value=0 checked>all error<br>";
	print "</ul>";	
}
print '<hr><center><input type=submit name=submit value=Check>';
print '<input type=reset name=reset value=Reset></center>';
print '</form></ul></font>';
}
sub checkanswer {
print'<center>Request to check the answer : ',$title,' in type ',$amt,'</center><hr>';
open(fn, "<$basedir$fname" );
@bfn = <fn>;
close(fn);
$cntr = @bfn;
$isright = 0;
$iswrong = 0;
$isnone = 0;
print "<ul>";
for $j (0 .. @idnum-1) {
	for $k (0 .. $cntr) {
	  ($xnum,$xques,$xc1,$xc2,$xc3,$xc4,$xans) = split(/:/,$bfn[$k]);
	  $xans = substr($xans,0,1);
	  if ($idnum[$j] eq $xnum) {
		print $j+1,". <font size=3>",$xques," <font size=0>[",$xnum,"]</font></font><br>","\n";	
	  	print "<font size=2><ol>";		
		if ($xans eq "1") { print "<li><b>",$xc1,"</b>";} else {print "<li>",$xc1;}
		if ($xans eq "2") { print "<li><b>",$xc2,"</b>";} else {print "<li>",$xc2;}
		if ($xans eq "3") { print "<li><b>",$xc3,"</b>";} else {print "<li>",$xc3;}
		if ($xans eq "4") { print "<li><b>",$xc4,"</b>";} else {print "<li>",$xc4;}				
		print "<br>";
		if ($FORM{$xnum} ne "0") {
			if ($FORM{$xnum} eq $xans) {
				print "<font color=#ff0000 size=0>Right";
				$isright++;				
			} else {
				print "<font color=#0000ff size=0>";
				print "<b>",$FORM{$xnum}," wrong but ",$xans," right</b>";
				$iswrong++;
			}			
		} else {
			print "<font color=#aaaaaa size=0><b>not decision</b>";
			$isnone++;
		}
		print "</font><hr></ol></font>";
	  }
	}
}
print '<hr></ul>';
$workit = $isright + $iswrong;
print'<font size=3>All ',$workit,'<br>Right ',$isright,'<br>Wrong ',$iswrong;
print'<br>None ',$isnone,'<br>';
}
 |