http://qs321.pair.com?node_id=122160
Category: web stuff
Author/Contact Info dan@ltc.org
Description: There are probably better quizes out there... merlyn has one TStanley has QuizTaker.pl This one differs in that it uses HTML::Template and puts a nice wrong.gif next to questions that are answered wrong.
#!/usr/local/bin/perl -wT
use strict;

my $template_file="../go/class/quiz/quiz.tmpl";
my $quiz_data_file="./quizes/70-215-1.txt";

#**********
# quiz.pl 
# dan@ltc.org
#**********

use CGI;
my $q=new CGI;
print $q->header();

open(FH,$quiz_data_file)
            or die"$! couldn't open quiz.txt\n";
my @lines=<FH>;
close(FH) 
        or die"$! couldn't close quiz.txt";

my @quiz=();
my @answers=();
my $question_count=0;
my $numElements=0;

for ($question_count=0;$question_count<@lines;$question_count++){
    my @fields=split('\t',$lines[$question_count]);
    
    my $a={}; #answer
    $numElements+=(@fields-2);
    $a->{'answer'}=shift(@fields);
    $a->{'answer_id'}='"q'.$question_count.'"';
    push @answers,$a;

    my $q={}; #question
    $q->{'question'}=shift(@fields);
    $q->{'image_name'}="q$question_count".'_image';
                
    my $choice_count=0;
    my $c=[];
    my $letter='a';
    while(@fields){
        my $choice_items={};
        $choice_items->{'choice_name'}="q$question_count";
        $choice_items->{'choice_value'}=$letter++;
        $choice_items->{'choice_text'}=shift(@fields);
        push (@$c,$choice_items); 
    }
    $q->{choices}=$c;
    push @quiz,$q;
                
}        

use HTML::Template; 
my $t=HTML::Template->new(filename=>$template_file);
$t->param(QUIZ=>\@quiz);
$t->param(ANSWERS=>\@answers);
$t->param(numElements=>$numElements, 
      numQues=>$question_count);
print $t->output();

####################################################
####################################################
<html>
<head>
<!--     
    quiz.tmpl
    huge parts of this html form lifted 
    straight from Charity Kahn's builder.com 
    quizbuilder script.
-->

<title>Quiz</title>

<script language="JavaScript"
SRC=/go/class/quiz/quiz.js></script>

<script language="JavaScript">
<!--

  var numElements = <TMPL_VAR NAME="numElements">;
  var numQues = <TMPL_VAR NAME="numQues">;
  var answers = new Array(1);

  <TMPL_LOOP NAME="ANSWERS">
answers[<TMPL_VAR NAME="answer_id">] ="<TMPL_VAR NAME="answer">";
</TMPL_LOOP>

// -->
</script>

</head>

<body >


        
<form name="quiz">


<ol compact type=1>
<TMPL_LOOP NAME="QUIZ">
<li><TMPL_VAR NAME="question">
<img src="/go/class/quiz/blank.gif" width=78
height=26 name="<TMPL_VAR NAME="image_name">" align="top">
</li> 
<ol type='a' compact>
<TMPL_LOOP NAME="choices">

<li><input type="radio" name="<TMPL_VAR NAME="choice_name">" value="<T
+MPL_VAR NAME="choice_value">">
<TMPL_VAR NAME="choice_text">
</li>
</TMPL_LOOP>
</ol>
<br>

</TMPL_LOOP>
</ol>

<input type="button" value="Get score"     onClick="getScore(this.form
+)">
<input type="reset" value="Clear"><p>
Score = <input type=text size=15 name="percentage"><br>
</form>

</body></html>
################################################
################################################
//  dan@ltc.org
// quiz.js
//
// Except for the bits that change the image
// and the bits that allow variable number of 
// choices, this script is lifted straight from 
// from Charity Kahn's builder.com 
// quizbuilder script.

var wrongImage = new Image(78,26);
wrongImage.src = "/go/class/quiz/wrong.gif"
var blankImage = new Image(78,26);
blankImage.src = "/go/class/quiz/blank.gif"

function getScore(form) {
  var score = 0;
  var c;
  var imageName;
  var prev=0;
  var cur=0;
  var isBlank;

while(cur<numElements){
    isBlank=true;    
    while(form.elements[cur].name==form.elements[prev].name){
        c=form.elements[cur];
        if(c.checked){
            isBlank=false;
            if(c.value==answers[c.name]){
                score++;
                imageName=c.name+"_image";
                document.images[imageName].src=blankImage.src;
            }else{
                imageName=c.name+"_image";
                document.images[imageName].src=wrongImage.src;
            }
        }
        cur++;
    }
    if (isBlank){         
        c=form.elements[prev];
        imageName=c.name+"_image";
        document.images[imageName].src=wrongImage.src;
    }
    prev=cur;
}

  score = Math.round(score/numQues*100);
  form.percentage.value = score + "%";
}