--- /dev/null
+<?PHP // $Id$ \r
+// Alton College, Hampshire, UK - Tom Flannaghan\r
+// Imports learnwise multiple choice quizzes\r
+// Based on format.php, included by ../../import.php\r
+\r
+function string_between($text, $start, $end)\r
+{\r
+ $startpos = strpos($text, $start) + strlen($start);\r
+ $endpos = strpos($text, $end);\r
+ \r
+ if($startpos <= $endpos) return substr($text, $startpos, $endpos - $startpos);\r
+}\r
+ \r
+ \r
+\r
+class quiz_file_format extends quiz_default_format {\r
+\r
+ function readquestions($lines) {\r
+ $questions = array();\r
+ $currentquestion = array();\r
+ \r
+ foreach($lines as $line) {\r
+ $line = trim($line);\r
+ $currentquestion[] = $line;\r
+ \r
+ if($question = $this->readquestion($currentquestion)) {\r
+ $questions[] = $question;\r
+ $currentquestion = array();\r
+ }\r
+ }\r
+ \r
+ return $questions;\r
+ }\r
+\r
+ function readquestion($lines) {\r
+ $text = '';\r
+ foreach ($lines as $line) $text .= $line;\r
+ $text = str_replace(array('\t','\n','\r','\''), array('','','','\\\''), $text);\r
+ $len = strlen($text);\r
+\r
+ $question = NULL;\r
+\r
+ if($questiontype = sscanf($text, '<question type="%s">'))\r
+ {\r
+ if($pos = stripos($text, '</question>'))\r
+ {\r
+ $text = substr($text, 0, $pos);\r
+ $text = stristr($text, '<question');\r
+ \r
+ $questiontext = '';\r
+ $questionaward = '';\r
+ $questionhint = '';\r
+ $optionlist = '';\r
+ $questiontext = string_between($text, '<text>', '</text>');\r
+ $questionaward = string_between($text, '<award>', '</award>');\r
+ $questionhint = string_between($text, '<hint>', '</hint>');\r
+ $optionlist = string_between($text, '<answer>', '</answer>');\r
+ \r
+ $optionlist = explode('<option', $optionlist);\r
+ $n=0;\r
+ \r
+ foreach($optionlist as $option)\r
+ {\r
+ $a = string_between($option, ' correct="', '">');\r
+ $b = string_between($option, '">', '</option>');\r
+ \r
+ $options_correct[$n] = $a; $options_text[$n] = $b;\r
+ //echo "$a, $b<br>";\r
+ $n++;\r
+ \r
+ }\r
+ \r
+ $question->qtype = MULTICHOICE;\r
+ $question->name = substr($questiontext,0,30);\r
+ if(strlen($questionlen)<30) $question->name .= '...';\r
+ $question->questiontext = $questiontext;\r
+ $question->single = 1;\r
+ $question->feedback[] = '';\r
+ $question->usecase = 0;\r
+ $question->defaultgrade = 1;\r
+ $question->image = '';\r
+ \r
+ for($n=0; $n<sizeof($options_text); $n++)\r
+ {\r
+ if($options_text[$n])\r
+ {\r
+ if($options_correct[$n]=='yes') $fraction = (int) $questionaward; else $fraction = 0;\r
+ $question->fraction[] = $fraction;\r
+ $question->answer[] = $options_text[$n];\r
+ //echo "hello: $options_text[$n], $fraction<br>";\r
+ }\r
+ }\r
+ }\r
+ }\r
+ \r
+ return $question;\r
+ }\r
+}\r
+\r
+?>\r