]> git.mjollnir.org Git - moodle.git/commitdiff
Adding Learnwise import - created by Alton College, Hampshire UK
authorthepurpleblob <thepurpleblob>
Tue, 25 Jan 2005 20:18:05 +0000 (20:18 +0000)
committerthepurpleblob <thepurpleblob>
Tue, 25 Jan 2005 20:18:05 +0000 (20:18 +0000)
Still to do testing etc.

mod/quiz/format/learnwise/format.php [new file with mode: 0755]

diff --git a/mod/quiz/format/learnwise/format.php b/mod/quiz/format/learnwise/format.php
new file mode 100755 (executable)
index 0000000..b8fe644
--- /dev/null
@@ -0,0 +1,100 @@
+<?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