Merged from MOODLE_17_STABLE.
[moodle.git] / question / format / examview / format.php
CommitLineData
84769fd8 1<?php // $Id$
2/*
3**
4** Examview 4.0 XML format into Moodle 1.4.3
5** Author: Dan McCuaig ( dmccuaig@wvc.edu )
6**
7** @TODO:
8** Take care of odd unicode character mapping (ex: curly quotes)
2befe778 9** Image and table support
84769fd8 10** Formatting support
11** Support of rejoinders
12**
13** $Log$
06968068 14** Revision 1.5 2006/10/30 16:22:58 thepurpleblob
15** fixed a notice
16**
2befe778 17** Revision 1.4 2006/08/10 18:23:39 tjhunt
18** Convert tabs to spaces.
19**
c0724bdc 20** Revision 1.3 2006/05/04 11:17:50 thepurpleblob
21** Merging from STABLE
22**
23** Revision 1.2.2.1 2006/05/04 11:15:11 thepurpleblob
24** htmlentities() replaced by s()
25**
f5565b69 26** Revision 1.2 2006/03/01 07:36:08 gustav_delius
27** Removing some more references to quiz from import/export code
28**
84769fd8 29** Revision 1.1 2006/02/24 15:14:04 thepurpleblob
30** Moving quiz import/export files to new question area.
31**
32** Revision 1.3 2006/02/13 16:17:22 thepurpleblob
33** Now used defaultquestion() method. Bug #4752
34**
35** Revision 1.2 2005/05/31 15:19:00 thepurpleblob
36** merged from STABLE
37**
38** Revision 1.1.2.1 2005/05/31 15:17:20 thepurpleblob
39** Took out dos line endings
40**
41** Revision 1.1 2005/05/16 08:12:40 thepurpleblob
42** Added support for Examview import.
43**
44**
45*/
46
47// Based on default.php, included by ../import.php
48
49require_once("$CFG->libdir/xmlize.php");
50//require_once("xmlize.php");
51
52/*
53define("SHORTANSWER", "1");
54define("TRUEFALSE", "2");
55define("MULTICHOICE", "3");
56define("MATCH", "5");
57define("DESCRIPTION", "7");
58define("NUMERICAL", "8");
59define("MULTIANSWER", "9");
60define("CALCULATED", "10");
61*/
62
f5565b69 63class qformat_examview extends qformat_default {
84769fd8 64
65 var $qtypes = array('tf' => TRUEFALSE,
66 'mc' => MULTICHOICE,
67 'yn' => TRUEFALSE,
68 'co' => SHORTANSWER,
69 'ma' => MATCH,
70 'mtf' => 99,
71 'nr' => NUMERICAL,
72 'pr' => 99,
73 'es' => 99,
74 'ca' => 99,
75 'ot' => 99
76 );
77
78 var $matching_questions = array();
79
80 function provide_import() {
81 return true;
82 }
83
84 function print_matching_questions()
85 {
86 foreach($this->matching_questions as $key => $value) {
87 print("$key => $value->questiontext<BR>");
88 print("Questions:<UL>");
89 foreach($value->subquestions as $qkey => $qvalue) {
90 print("<LI>$qkey => $qvalue</LI>");
91 }
92 print("</UL>");
93 print("Choices:<UL>");
94 foreach($value->subchoices as $ckey => $cvalue) {
95 print("<LI>$ckey => $cvalue</LI>");
96 }
97 print("</UL>");
98 print("Answers:<UL>");
99 foreach($value->subanswers as $akey => $avalue) {
100 print("<LI>$akey => $avalue</LI>");
101 }
102 print("</UL>");
103 }
104 }
105
106 function parse_matching_groups($matching_groups)
107 {
108 if (empty($matching_groups)) {
109 return;
110 }
111 foreach($matching_groups as $match_group) {
112 $newgroup = NULL;
113 $groupname = trim($match_group['@']['name']);
114 $questiontext = $this->ArrayTagToString($match_group['#']['text']['0']['#']);
115 $newgroup->questiontext = trim($questiontext);
116 $newgroup->subchoices = array();
117 $newgroup->subquestions = array();
118 $newgroup->subanswers = array();
119 $choices = $match_group['#']['choices']['0']['#'];
120 foreach($choices as $key => $value) {
121 if (strpos(trim($key),'choice-') !== FALSE) {
122 $key = strtoupper(trim(str_replace('choice-', '', $key)));
123 $newgroup->subchoices[$key] = trim($value['0']['#']);
124 }
125 }
126 $this->matching_questions[$groupname] = $newgroup;
127 }
128 }
129
130 function parse_ma($qrec, $groupname)
131 {
132 $match_group = $this->matching_questions[$groupname];
133 $phrase = trim($qrec['text']['0']['#']);
134 $answer = trim($qrec['answer']['0']['#']);
135 $match_group->subquestions[] = $phrase;
136 $match_group->subanswers[] = $match_group->subchoices[$answer];
137 $this->matching_questions[$groupname] = $match_group;
138 return NULL;
139 }
140
141 function process_matches(&$questions)
142 {
143 if (empty($this->matching_questions)) {
144 return;
145 }
146 foreach($this->matching_questions as $match_group) {
147 $question = $this->defaultquestion();
2befe778 148 $htmltext = $this->htmlPrepare($match_group->questiontext);
149 $htmltext = addslashes($htmltext);
84769fd8 150 $question->questiontext = $htmltext;
151 $question->name = $question->questiontext;
152 $question->qtype = MATCH;
84769fd8 153 $question->subquestions = array();
154 $question->subanswers = array();
155 foreach($match_group->subquestions as $key => $value) {
2befe778 156 $htmltext = $this->htmlPrepare($value);
157 $htmltext = addslashes($htmltext);
84769fd8 158 $question->subquestions[] = $htmltext;
159
160 $htmltext = $this->htmlPrepare($match_group->subanswers[$key]);
2befe778 161 $htmltext = addslashes($htmltext);
84769fd8 162 $question->subanswers[] = $htmltext;
163 }
164 $questions[] = $question;
165 }
166 }
167
2befe778 168 function cleanUnicode($text) {
2befe778 169 return str_replace('&#x2019;', "'", $text);
170 }
06968068 171
172 function readquestions($lines) {
84769fd8 173 /// Parses an array of lines into an array of questions,
174 /// where each item is a question object as defined by
175 /// readquestion().
06968068 176
84769fd8 177 $questions = array();
178 $currentquestion = array();
179
180 $text = implode($lines, ' ');
2befe778 181 $text = $this->cleanUnicode($text);
84769fd8 182
183 $xml = xmlize($text, 0);
06968068 184 if (!empty($xml['examview']['#']['matching-group'])) {
185 $this->parse_matching_groups($xml['examview']['#']['matching-group']);
186 }
84769fd8 187
188 $questionNode = $xml['examview']['#']['question'];
189 foreach($questionNode as $currentquestion) {
190 if ($question = $this->readquestion($currentquestion)) {
191 $questions[] = $question;
192 }
193 }
194
195 // print('<hr>');
196 // $this->print_matching_questions();
197 $this->process_matches($questions);
198 // print('<hr>');
199
200 return $questions;
201 }
202 // end readquestions
203
204 function htmlPrepare($htmltext)
06968068 205 {
206 // $text = trim($text);
c0724bdc 207 $text = s($htmltext);
84769fd8 208 //$htmltext = nl2br($text);
209 return $text;
210 }
211
212 function ArrayTagToString($aTag)
213 {
214 if (!is_array($aTag)) {
215 return $aTag;
216 }
217 $out = '';
218 foreach($aTag as $key => $value) {
219 if (is_array($value)) {
220 $out = $out.$this->ArrayTagToString($value);
221 } else {
222 $out = $value;
223 }
224 }
225 return $out;
226 }
227
228
229 function readquestion($qrec)
230 {
231
232 $type = trim($qrec['@']['type']);
233 $question = $this->defaultquestion();
234 $question->qtype = $this->qtypes[$type];
235 $question->single = 1;
236 // Only one answer is allowed
237 $htmltext = $this->ArrayTagToString($qrec['#']['text'][0]['#']);
238 $htmltext = $this->htmlPrepare($htmltext);
239 $htmltext = addslashes($htmltext);
240 $question->questiontext = $htmltext;
241 $question->name = $question->questiontext;
242
243 switch ($question->qtype) {
244 case MULTICHOICE:
245 $question = $this->parse_mc($qrec['#'], $question);
246 break;
247 case MATCH:
248 $groupname = trim($qrec['@']['group']);
249 $question = $this->parse_ma($qrec['#'], $groupname);
250 break;
251 case TRUEFALSE:
252 $question = $this->parse_tf_yn($qrec['#'], $question);
253 break;
254 case SHORTANSWER:
255 $question = $this->parse_co($qrec['#'], $question);
256 break;
257 case NUMERICAL:
258 $question = $this->parse_nr($qrec['#'], $question);
259 break;
260 break;
261 default:
262 print("<p>Question type ".$type." import not supported for ".$question->questiontext."<p>");
263 $question = NULL;
264 }
265 // end switch ($question->qtype)
266
267 return $question;
268 }
269 // end readquestion
270
271 function parse_tf_yn($qrec, $question)
272 {
273 $choices = array('T' => 1, 'Y' => 1, 'F' => 0, 'N' => 0 );
274 $answer = trim($qrec['answer'][0]['#']);
275 $question->answer = $choices[$answer];
276 if ($question->answer == 1) {
277 $question->feedbacktrue = 'Correct';
278 $question->feedbackfalse = 'Incorrect';
279 } else {
280 $question->feedbacktrue = 'Incorrect';
281 $question->feedbackfalse = 'Correct';
282 }
283 return $question;
284 }
285
286 function parse_mc($qrec, $question)
287 {
288 $answer = 'choice-'.strtolower(trim($qrec['answer'][0]['#']));
289
290 $choices = $qrec['choices'][0]['#'];
291 foreach($choices as $key => $value) {
292 if (strpos(trim($key),'choice-') !== FALSE) {
293
294 $question->answer[$key] = $this->htmlPrepare($value[0]['#']);
295 if (strcmp($key, $answer) == 0) {
296 $question->fraction[$key] = 1;
297 $question->feedback[$key] = 'Correct';
298 } else {
299 $question->fraction[$key] = 0;
300 $question->feedback[$key] = 'Incorrect';
301 }
302 }
303 }
304 return $question;
305 }
306
307 function parse_co($qrec, $question)
308 {
309 $question->usecase = 0;
310 $answer = trim($qrec['answer'][0]['#']);
311 $answers = explode("\n",$answer);
312
313 foreach($answers as $key => $value) {
314 $value = trim($value);
315 if (strlen($value) > 0) {
316 $question->answer[$key] = addslashes($value);
317 $question->fraction[$key] = 1;
318 $question->feedback[$key] = "Correct";
319 }
320 }
321 return $question;
322 }
323
324 function parse_nr($qrec, $question)
325 {
326 $answer = trim($qrec['answer'][0]['#']);
327 $answers = explode("\n",$answer);
328
329 foreach($answers as $key => $value) {
330 $value = trim($value);
331 if (is_numeric($value)) {
332 $errormargin = 0;
333 $question->answer[$key] = $value;
334 $question->fraction[$key] = 1;
335 $question->feedback[$key] = "Correct";
336 $question->min[$key] = $question->answer[$key] - $errormargin;
337 $question->max[$key] = $question->answer[$key] + $errormargin;
338 }
339 }
340 return $question;
341 }
342
343}
344// end class
345
346?>