#native_company# #native_desc#
#native_cta#

LizQuiz

By Elizabeth Cortell
on October 24, 2000

Version: 1.1

Type: Class

Category: Games

License: GNU General Public License

Description: PHP class for creating multiple-choice quizzes on your web site. Configurable with an XML file–no need for a database. Fast, easy to use. Create one quiz object and store as a session variable for each testtaker. Flexible: display questions as you wish. Allows multiple correct answers for each question. XML must be compiled into your instance of PHP to use.

Find an example of it at http://www.adamclymerfanclub.org/clymertrivia.php

For more documentation, including an XML configuration example, go to http://www.zrusilla.com/lizquiz.html

Comments and bug reports to [email protected].

<?php

class question {

  var $question;
  var $order;
  var $choices = array();
  var $correct;
  var $choice;
  
  function question ($ary) {
    foreach ($ary as $k => $v) {
      switch ($v["tag"]) {
	case "question":
          $this->question = $v["value"];
          break;
        case "answer":
          $attrs = $v["attributes"];
          if ($attrs["correct"] == "yes") {
            $this->correct= $attrs["order"];
          }
          $this->choices[$attrs["order"]] = $v["value"];
          break;
        default:
          break;
      }
    }
  }
  
  function dump () {
    echo "Dump object question<br>";
    echo "question: $this->question<br>";
    foreach ($this->choices as $k => $v) {
      echo "$k: $v<br>";
    }
    echo "correct: $this->correct<P>";
  }
}


class quiz {

  // set of question objects
  var $questions = array();
  var $points;
  var $name;

  function quiz ($file) {
    
    if (!file_exists($file)) {return $ary;}
    $values = array();
    $tags   = array();
    $data = join("",file($file));
    $parser = xml_parser_create();
    xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
    xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
    xml_parse_into_struct($parser,$data,&$values,&$tags);
    xml_parser_free($parser);
    
    $this->name = $values[0]["attributes"]["name"];
    
    # parse out each question
     
    $items = $tags["item"];
    for ($i = 0; $i < count($items); $i+=2) {
      $lower = $items[$i];
      $upper = $items[$i+1];
       
      // get question number
      $item = $values[$lower];
      $number = $item["attributes"]["number"];
          
      // get question data and pass to question object
      $lower++;
      $length = $upper-$lower;
      $upper--;
      $this->questions[$number] = new question(array_slice($values, $lower, $length));

      # debug 
      #$this->questions[$number]->dump();
    }
	
    ksort($this->questions);
    reset($this->questions);
    
    # debugging 
    #echo "<pre>";
    #print_r($tags);
    #print_r($values);
    #echo "</pre>";
  }
  
  //get the question array length
  function length () {
    return count($this->questions);
  }
  
  
  //get the question
  function question($id) {
    return $this->questions[$id]->question;
  }
  
  //get the choices for the question
  function choices($id) {
    return $this->questions[$id]->choices;
  }
  
  //add testtaker's answer
  function tally($id, $choice) {
    $this->questions[$id]->choice = $choice;
  }
  
  //return array of testtaker's answers
  function get_answers() {
    
    $choices = array();
    
    foreach ($this->questions as $id => $q) {
      $answers["$id"] = $q->choice;
    }
    return $answers;
  }
  
  //return array of correct answers
  function get_correct() {
      
      $correct = array();
      
      foreach ($this->questions as $id => $q) {
        $correct["$id"] = $q->correct;
      }
      return $correct;
  }
  
  function score() {
    $points = 0;
    foreach ($this->questions as $id => $q) {
      if ($q->choice == $q->correct)  {
        $points++;
      }
    }
    return $points;
  }

  
}
?>