MDL-15094 fixed stats
[moodle.git] / question / format / examview / format.php
CommitLineData
84769fd8 1<?php // $Id$
84769fd8 2
3// Based on default.php, included by ../import.php
41a89a07 4/**
5 * @package questionbank
6 * @subpackage importexport
7 */
84769fd8 8
9require_once("$CFG->libdir/xmlize.php");
84769fd8 10
f5565b69 11class qformat_examview extends qformat_default {
84769fd8 12
85474b26 13 var $qtypes = array(
14 'tf' => TRUEFALSE,
15 'mc' => MULTICHOICE,
16 'yn' => TRUEFALSE,
17 'co' => SHORTANSWER,
18 'ma' => MATCH,
19 'mtf' => 99,
20 'nr' => NUMERICAL,
21 'pr' => 99,
22 'es' => 99,
23 'ca' => 99,
24 'ot' => 99,
25 'sa' => ESSAY
26 );
84769fd8 27
28 var $matching_questions = array();
29
30 function provide_import() {
31 return true;
32 }
33
0b3940f8 34 /**
35 * unxmlise reconstructs part of the xml data structure in order
36 * to identify the actual data therein
37 * @param array $xml section of the xml data structure
38 * @return string data with evrything else removed
39 */
40 function unxmlise( $xml ) {
41 // if it's not an array then it's probably just data
42 if (!is_array($xml)) {
43 $text = s(addslashes($xml));
44 }
45 else {
46 // otherwise parse the array
47 $text = '';
48 foreach ($xml as $tag=>$data) {
49 // if tag is '@' then it's attributes and we don't care
50 if ($tag!=='@') {
51 $text = $text . $this->unxmlise( $data );
52 }
84769fd8 53 }
84769fd8 54 }
0b3940f8 55
56 // currently we throw the tags we found
57 $text = strip_tags($text);
58 return $text;
84769fd8 59 }
60
61 function parse_matching_groups($matching_groups)
62 {
63 if (empty($matching_groups)) {
64 return;
65 }
66 foreach($matching_groups as $match_group) {
67 $newgroup = NULL;
68 $groupname = trim($match_group['@']['name']);
0b3940f8 69 $questiontext = $this->unxmlise($match_group['#']['text'][0]['#']);
84769fd8 70 $newgroup->questiontext = trim($questiontext);
71 $newgroup->subchoices = array();
72 $newgroup->subquestions = array();
73 $newgroup->subanswers = array();
74 $choices = $match_group['#']['choices']['0']['#'];
75 foreach($choices as $key => $value) {
76 if (strpos(trim($key),'choice-') !== FALSE) {
77 $key = strtoupper(trim(str_replace('choice-', '', $key)));
78 $newgroup->subchoices[$key] = trim($value['0']['#']);
79 }
80 }
81 $this->matching_questions[$groupname] = $newgroup;
82 }
83 }
84
85 function parse_ma($qrec, $groupname)
86 {
87 $match_group = $this->matching_questions[$groupname];
0b3940f8 88 $phrase = trim($this->unxmlise($qrec['text']['0']['#']));
89 $answer = trim($this->unxmlise($qrec['answer']['0']['#']));
90 $answer = strip_tags( $answer );
84769fd8 91 $match_group->subquestions[] = $phrase;
92 $match_group->subanswers[] = $match_group->subchoices[$answer];
93 $this->matching_questions[$groupname] = $match_group;
94 return NULL;
95 }
96
97 function process_matches(&$questions)
98 {
99 if (empty($this->matching_questions)) {
100 return;
101 }
102 foreach($this->matching_questions as $match_group) {
103 $question = $this->defaultquestion();
a60b5c16 104 $htmltext = s(addslashes($match_group->questiontext));
84769fd8 105 $question->questiontext = $htmltext;
106 $question->name = $question->questiontext;
107 $question->qtype = MATCH;
84769fd8 108 $question->subquestions = array();
109 $question->subanswers = array();
110 foreach($match_group->subquestions as $key => $value) {
0b3940f8 111 $htmltext = s(addslashes($value));
84769fd8 112 $question->subquestions[] = $htmltext;
113
a60b5c16 114 $htmltext = s(addslashes($match_group->subanswers[$key]));
84769fd8 115 $question->subanswers[] = $htmltext;
116 }
117 $questions[] = $question;
118 }
119 }
120
2befe778 121 function cleanUnicode($text) {
2befe778 122 return str_replace('&#x2019;', "'", $text);
123 }
06968068 124
125 function readquestions($lines) {
84769fd8 126 /// Parses an array of lines into an array of questions,
127 /// where each item is a question object as defined by
128 /// readquestion().
06968068 129
84769fd8 130 $questions = array();
131 $currentquestion = array();
132
133 $text = implode($lines, ' ');
2befe778 134 $text = $this->cleanUnicode($text);
84769fd8 135
136 $xml = xmlize($text, 0);
06968068 137 if (!empty($xml['examview']['#']['matching-group'])) {
138 $this->parse_matching_groups($xml['examview']['#']['matching-group']);
139 }
84769fd8 140
141 $questionNode = $xml['examview']['#']['question'];
142 foreach($questionNode as $currentquestion) {
143 if ($question = $this->readquestion($currentquestion)) {
144 $questions[] = $question;
145 }
146 }
147
84769fd8 148 $this->process_matches($questions);
84769fd8 149 return $questions;
150 }
151 // end readquestions
152
84769fd8 153 function readquestion($qrec)
154 {
155
156 $type = trim($qrec['@']['type']);
157 $question = $this->defaultquestion();
a60b5c16 158 if (array_key_exists($type, $this->qtypes)) {
159 $question->qtype = $this->qtypes[$type];
160 }
161 else {
162 $question->qtype = null;
163 }
84769fd8 164 $question->single = 1;
165 // Only one answer is allowed
0b3940f8 166 $htmltext = $this->unxmlise($qrec['#']['text'][0]['#']);
84769fd8 167 $question->questiontext = $htmltext;
85474b26 168 $question->name = shorten_text( $question->questiontext, 250 );
84769fd8 169
170 switch ($question->qtype) {
171 case MULTICHOICE:
172 $question = $this->parse_mc($qrec['#'], $question);
173 break;
174 case MATCH:
175 $groupname = trim($qrec['@']['group']);
176 $question = $this->parse_ma($qrec['#'], $groupname);
177 break;
178 case TRUEFALSE:
179 $question = $this->parse_tf_yn($qrec['#'], $question);
180 break;
181 case SHORTANSWER:
182 $question = $this->parse_co($qrec['#'], $question);
183 break;
85474b26 184 case ESSAY:
185 $question = $this->parse_sa($qrec['#'], $question);
186 break;
84769fd8 187 case NUMERICAL:
188 $question = $this->parse_nr($qrec['#'], $question);
189 break;
190 break;
191 default:
192 print("<p>Question type ".$type." import not supported for ".$question->questiontext."<p>");
193 $question = NULL;
194 }
195 // end switch ($question->qtype)
196
197 return $question;
198 }
199 // end readquestion
200
201 function parse_tf_yn($qrec, $question)
202 {
203 $choices = array('T' => 1, 'Y' => 1, 'F' => 0, 'N' => 0 );
204 $answer = trim($qrec['answer'][0]['#']);
205 $question->answer = $choices[$answer];
fa5cb18b 206 $question->correctanswer = $question->answer;
84769fd8 207 if ($question->answer == 1) {
208 $question->feedbacktrue = 'Correct';
209 $question->feedbackfalse = 'Incorrect';
210 } else {
211 $question->feedbacktrue = 'Incorrect';
212 $question->feedbackfalse = 'Correct';
213 }
214 return $question;
215 }
216
217 function parse_mc($qrec, $question)
218 {
219 $answer = 'choice-'.strtolower(trim($qrec['answer'][0]['#']));
220
221 $choices = $qrec['choices'][0]['#'];
222 foreach($choices as $key => $value) {
223 if (strpos(trim($key),'choice-') !== FALSE) {
0b3940f8 224
225 $question->answer[$key] = s($this->unxmlise($value[0]['#']));
84769fd8 226 if (strcmp($key, $answer) == 0) {
227 $question->fraction[$key] = 1;
228 $question->feedback[$key] = 'Correct';
229 } else {
230 $question->fraction[$key] = 0;
231 $question->feedback[$key] = 'Incorrect';
232 }
233 }
234 }
235 return $question;
236 }
237
238 function parse_co($qrec, $question)
239 {
240 $question->usecase = 0;
0b3940f8 241 $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
242 $answer = strip_tags( $answer );
84769fd8 243 $answers = explode("\n",$answer);
244
245 foreach($answers as $key => $value) {
246 $value = trim($value);
247 if (strlen($value) > 0) {
248 $question->answer[$key] = addslashes($value);
249 $question->fraction[$key] = 1;
250 $question->feedback[$key] = "Correct";
251 }
252 }
253 return $question;
254 }
85474b26 255
256 function parse_sa($qrec, $question) {
257 $feedback = trim($this->unxmlise($qrec['answer'][0]['#']));
258 $question->feedback = $feedback;
259 $question->fraction = 0;
260 return $question;
261 }
84769fd8 262
263 function parse_nr($qrec, $question)
264 {
0b3940f8 265 $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
266 $answer = strip_tags( $answer );
84769fd8 267 $answers = explode("\n",$answer);
268
269 foreach($answers as $key => $value) {
270 $value = trim($value);
271 if (is_numeric($value)) {
272 $errormargin = 0;
273 $question->answer[$key] = $value;
274 $question->fraction[$key] = 1;
275 $question->feedback[$key] = "Correct";
276 $question->min[$key] = $question->answer[$key] - $errormargin;
277 $question->max[$key] = $question->answer[$key] + $errormargin;
278 }
279 }
280 return $question;
281 }
282
283}
284// end class
285
286?>