added proxy handling for lang import
[moodle.git] / question / format / learnwise / format.php
CommitLineData
84769fd8 1<?php // $Id$ \r
2// Alton College, Hampshire, UK - Tom Flannaghan, Andrew Walker\r
3// Imports learnwise multiple choice quizzes (single and multiple answers)\r
4// currently ignores the deduct attribute for multiple answer questions\r
5// deductions are currently simply found by dividing the award for the incorrect \r
6// answer by the total number of options\r
7// Based on format.php, included by ../../import.php\r
8\r
9class quiz_format_learnwise extends quiz_default_format {\r
10\r
11 function provide_import() {\r
12 return true;\r
13 }\r
14\r
15 function readquestions($lines) {\r
16 $questions = array();\r
17 $currentquestion = array();\r
18 \r
19 foreach($lines as $line) {\r
20 $line = trim($line);\r
21 $currentquestion[] = $line;\r
22 \r
23 if ($question = $this->readquestion($currentquestion)) {\r
24 $questions[] = $question;\r
25 $currentquestion = array();\r
26 }\r
27 } \r
28 return $questions;\r
29 }\r
30\r
31 function readquestion($lines) {\r
32 $text = implode(' ', $lines);\r
33 $text = str_replace(array('\t','\n','\r','\''), array('','','','\\\''), $text);\r
34\r
35 $startpos = strpos($text, '<question type');\r
36 $endpos = strpos($text, '</question>');\r
37 if ($startpos === false || $endpos === false) {\r
38 return false;\r
39 }\r
40\r
41 preg_match("/<question type=[\"\']([^\"\']+)[\"\']>/i", $text, $matches);\r
42 $type = strtolower($matches[1]); // multichoice or multianswerchoice\r
43\r
44 $questiontext = $this->unhtmlentities($this->stringbetween($text, '<text>', '</text>'));\r
45 $questionhint = $this->unhtmlentities($this->stringbetween($text, '<hint>', '</hint>'));\r
46 $questionaward = $this->stringbetween($text, '<award>', '</award>');\r
47 $optionlist = $this->stringbetween($text, '<answer>', '</answer>');\r
48\r
49 $optionlist = explode('<option', $optionlist);\r
50\r
51 $n = 0;\r
52\r
53 $optionscorrect = array();\r
54 $optionstext = array();\r
55\r
56 if ($type == 'multichoice') {\r
57 foreach ($optionlist as $option) {\r
58 $correct = $this->stringbetween($option, ' correct="', '">');\r
59 $answer = $this->stringbetween($option, '">', '</option>');\r
60 $optionscorrect[$n] = $correct; \r
61 $optionstext[$n] = $this->unhtmlentities($answer);\r
62 ++$n;\r
63 }\r
64 } else if ($type == 'multianswerchoice') {\r
65 $numcorrect = 0;\r
66 $totalaward = 0;\r
67\r
68 $optionsaward = array();\r
69\r
70 foreach ($optionlist as $option) {\r
71 preg_match("/correct=\"([^\"]*)\"/i", $option, $correctmatch);\r
72 preg_match("/award=\"([^\"]*)\"/i", $option, $awardmatch);\r
73\r
74 $correct = $correctmatch[1];\r
75 $award = $awardmatch[1];\r
76 if ($correct == 'yes') {\r
77 $totalaward += $award;\r
78 ++$numcorrect;\r
79 }\r
80\r
81 $answer = $this->stringbetween($option, '">', '</option>');\r
82\r
83 $optionscorrect[$n] = $correct; \r
84 $optionstext[$n] = $this->unhtmlentities($answer);\r
85 $optionsaward[$n] = $award;\r
86 ++$n;\r
87 }\r
88\r
89 } else {\r
90 echo "<p>I don't understand this question type (type = <strong>$type</strong>).</p>\n";\r
91 }\r
92\r
93 $question = $this->defaultquestion();\r
94 $question->qtype = MULTICHOICE;\r
95 $question->name = substr($questiontext, 0, 30);\r
96 if (strlen($questiontext) > 30) {\r
97 $question->name .= '...';\r
98 }\r
99\r
100 $question->questiontext = $questiontext;\r
101 $question->single = ($type == 'multichoice') ? 1 : 0;\r
102 $question->feedback[] = '';\r
103 \r
104 $question->fraction = array();\r
105 $question->answer = array();\r
106 for ($n = 0; $n < count($optionstext); ++$n) {\r
107 if ($optionstext[$n]) {\r
108 if (!isset($numcorrect)) { // single answer\r
109 if ($optionscorrect[$n] == 'yes') {\r
110 $fraction = (int) $questionaward; \r
111 } else {\r
112 $fraction = 0;\r
113 } \r
114 } else { // mulitple answers\r
115 if ($optionscorrect[$n] == 'yes') {\r
116 $fraction = $optionsaward[$n] / $totalaward; \r
117 } else {\r
118 $fraction = -$optionsaward[$n] / count($optionstext);\r
119 }\r
120 }\r
121 $question->fraction[] = $fraction;\r
122 $question->answer[] = $optionstext[$n];\r
123 $question->feedback[] = ''; // no feedback in this type\r
124 }\r
125 }\r
126\r
127 return $question;\r
128 }\r
129\r
130 function stringbetween($text, $start, $end) {\r
131 $startpos = strpos($text, $start) + strlen($start);\r
132 $endpos = strpos($text, $end);\r
133 \r
134 if ($startpos <= $endpos) {\r
135 return substr($text, $startpos, $endpos - $startpos);\r
136 }\r
137 }\r
138\r
139 function unhtmlentities($string) {\r
140 $transtable = get_html_translation_table(HTML_ENTITIES);\r
141 $transtable = array_flip($transtable);\r
142 return strtr($string, $transtable);\r
143 } \r
144\r
145}\r
146\r
147?>\r