$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
";
echo "question: $this->question
";
foreach ($this->choices as $k => $v) {
echo "$k: $v
";
}
echo "correct: $this->correct
"; } } 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 "
";
#print_r($tags);
#print_r($values);
#echo "";
}
//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;
}
}
?>