Permalink
Browse files

Addition code to handle 'multianswerchoice' type Learnwise questions.

  • Loading branch information...
1 parent d175f50 commit 2d4aa134d5d8e9c25f7a10797915013d0e0a8ac5 thepurpleblob committed Jan 27, 2005
Showing with 129 additions and 94 deletions.
  1. +129 −94 mod/quiz/format/learnwise/format.php
@@ -1,109 +1,144 @@
-<?PHP // $Id$
-// Alton College, Hampshire, UK - Tom Flannaghan
-// Imports learnwise multiple choice quizzes
+<?php // $Id$
+// Alton College, Hampshire, UK - Tom Flannaghan, Andrew Walker
+// Imports learnwise multiple choice quizzes (single and multiple answers)
+// currently ignores the deduct attribute for multiple answer questions
+// deductions are currently simply found by dividing the award for the incorrect
+// answer by the total number of options
// Based on format.php, included by ../../import.php
-function string_between($text, $start, $end)
-{
- $startpos = strpos($text, $start) + strlen($start);
- $endpos = strpos($text, $end);
-
- if($startpos <= $endpos) return substr($text, $startpos, $endpos - $startpos);
-}
-
-
-
class quiz_file_format extends quiz_default_format {
- function readquestions($lines) {
+ function readquestions($lines) {
$questions = array();
$currentquestion = array();
- foreach($lines as $line) {
- $line = trim($line);
- $currentquestion[] = $line;
+ foreach($lines as $line) {
+ $line = trim($line);
+ $currentquestion[] = $line;
- if($question = $this->readquestion($currentquestion)) {
- $questions[] = $question;
- $currentquestion = array();
- }
- }
-
- return $questions;
- }
-
- function unhtmlentities($string)
- // puts all the &gt; etc stuff back to 'normal'
- // good for PHP 4.1.0 on
- {
- $trans_tbl = get_html_translation_table(HTML_ENTITIES);
- $trans_tbl = array_flip($trans_tbl);
- return strtr($string, $trans_tbl);
- }
+ if ($question = $this->readquestion($currentquestion)) {
+ $questions[] = $question;
+ $currentquestion = array();
+ }
+ }
+ return $questions;
+ }
function readquestion($lines) {
- $text = '';
- foreach ($lines as $line) $text .= $line;
- $text = str_replace(array('\t','\n','\r','\''), array('','','','\\\''), $text);
- $len = strlen($text);
-
- $question = NULL;
-
- if($questiontype = sscanf($text, '<question type="%s">'))
- {
- if($pos = stripos($text, '</question>'))
- {
- $text = substr($text, 0, $pos);
- $text = stristr($text, '<question');
-
- $questiontext = '';
- $questionaward = '';
- $questionhint = '';
- $optionlist = '';
- $questiontext = string_between($text, '<text>', '</text>');
- $questionaward = string_between($text, '<award>', '</award>');
- $questionhint = string_between($text, '<hint>', '</hint>');
- $optionlist = string_between($text, '<answer>', '</answer>');
-
- $optionlist = explode('<option', $optionlist);
- $n=0;
-
- foreach($optionlist as $option)
- {
- $a = string_between($option, ' correct="', '">');
- $b = string_between($option, '">', '</option>');
-
- $options_correct[$n] = $a; $options_text[$n] = $b;
- //echo "$a, $b<br>";
- $n++;
-
- }
-
- $question->qtype = MULTICHOICE;
- $question->name = substr($this->unhtmlentities($questiontext),0,30);
- if(strlen($questionlen)<30) $question->name .= '...';
- $question->questiontext = $this->unhtmlentities($questiontext);
- $question->single = 1;
- $question->feedback[] = '';
- $question->usecase = 0;
- $question->defaultgrade = 1;
- $question->image = '';
-
- for($n=0; $n<sizeof($options_text); $n++)
- {
- if($options_text[$n])
- {
- if($options_correct[$n]=='yes') $fraction = (int) $questionaward; else $fraction = 0;
- $question->fraction[] = $fraction;
- $question->answer[] = $this->unhtmlentities($options_text[$n]);
- //echo "hello: $options_text[$n], $fraction<br>";
- }
- }
- }
- }
-
+ $text = implode(' ', $lines);
+ $text = str_replace(array('\t','\n','\r','\''), array('','','','\\\''), $text);
+
+ $startpos = strpos($text, '<question type');
+ $endpos = strpos($text, '</question>');
+ if ($startpos === false || $endpos === false) {
+ return false;
+ }
+
+ preg_match("/<question type=[\"\']([^\"\']+)[\"\']>/i", $text, $matches);
+ $type = strtolower($matches[1]); // multichoice or multianswerchoice
+
+ $questiontext = $this->unhtmlentities($this->stringbetween($text, '<text>', '</text>'));
+ $questionhint = $this->unhtmlentities($this->stringbetween($text, '<hint>', '</hint>'));
+ $questionaward = $this->stringbetween($text, '<award>', '</award>');
+ $optionlist = $this->stringbetween($text, '<answer>', '</answer>');
+
+ $optionlist = explode('<option', $optionlist);
+
+ $n = 0;
+
+ $optionscorrect = array();
+ $optionstext = array();
+
+ if ($type == 'multichoice') {
+ foreach ($optionlist as $option) {
+ $correct = $this->stringbetween($option, ' correct="', '">');
+ $answer = $this->stringbetween($option, '">', '</option>');
+ $optionscorrect[$n] = $correct;
+ $optionstext[$n] = $this->unhtmlentities($answer);
+ ++$n;
+ }
+ } else if ($type == 'multianswerchoice') {
+ $numcorrect = 0;
+ $totalaward = 0;
+
+ $optionsaward = array();
+
+ foreach ($optionlist as $option) {
+ preg_match("/correct=\"([^\"]*)\"/i", $option, $correctmatch);
+ preg_match("/award=\"([^\"]*)\"/i", $option, $awardmatch);
+
+ $correct = $correctmatch[1];
+ $award = $awardmatch[1];
+ if ($correct == 'yes') {
+ $totalaward += $award;
+ ++$numcorrect;
+ }
+
+ $answer = $this->stringbetween($option, '">', '</option>');
+
+ $optionscorrect[$n] = $correct;
+ $optionstext[$n] = $this->unhtmlentities($answer);
+ $optionsaward[$n] = $award;
+ ++$n;
+ }
+
+ } else {
+ echo "<p>I don't understand this question type (type = <strong>$type</strong>).</p>\n";
+ }
+
+ $question->qtype = MULTICHOICE;
+ $question->name = substr($questiontext, 0, 30);
+ if (strlen($questiontext) > 30) {
+ $question->name .= '...';
+ }
+
+ $question->questiontext = $questiontext;
+ $question->single = ($type == 'multichoice') ? 1 : 0;
+ $question->feedback[] = '';
+ $question->usecase = 0;
+ $question->defaultgrade = 1;
+ $question->image = '';
+
+ $question->fraction = array();
+ $question->answer = array();
+ for ($n = 0; $n < count($optionstext); ++$n) {
+ if ($optionstext[$n]) {
+ if (!isset($numcorrect)) { // single answer
+ if ($optionscorrect[$n] == 'yes') {
+ $fraction = (int) $questionaward;
+ } else {
+ $fraction = 0;
+ }
+ } else { // mulitple answers
+ if ($optionscorrect[$n] == 'yes') {
+ $fraction = $optionsaward[$n] / $totalaward;
+ } else {
+ $fraction = -$optionsaward[$n] / count($optionstext);
+ }
+ }
+ $question->fraction[] = $fraction;
+ $question->answer[] = $optionstext[$n];
+ }
+ }
+
return $question;
}
+
+ function stringbetween($text, $start, $end) {
+ $startpos = strpos($text, $start) + strlen($start);
+ $endpos = strpos($text, $end);
+
+ if ($startpos <= $endpos) {
+ return substr($text, $startpos, $endpos - $startpos);
+ }
+ }
+
+ function unhtmlentities($string) {
+ $transtable = get_html_translation_table(HTML_ENTITIES);
+ $transtable = array_flip($transtable);
+ return strtr($string, $transtable);
+ }
+
}
?>

0 comments on commit 2d4aa13

Please sign in to comment.