MDL-34483 qformat_examview: changes required to make it basically work.
[moodle.git] / question / format / examview / format.php
CommitLineData
aeb15530 1<?php
d3603157
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
41a89a07 17/**
d3603157
TH
18 * Examview question importer.
19 *
20 * @package qformat
21 * @subpackage examview
22 * @copyright 2005 Howard Miller
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41a89a07 24 */
84769fd8 25
84769fd8 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
d3603157
TH
29require_once ($CFG->libdir . '/xmlize.php');
30
31
32/**
33 * Examview question importer.
34 *
35 * @copyright 2005 Howard Miller
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
f5565b69 38class qformat_examview extends qformat_default {
f34488b2 39
40 public $qtypes = array(
85474b26 41 'tf' => TRUEFALSE,
42 'mc' => MULTICHOICE,
43 'yn' => TRUEFALSE,
44 'co' => SHORTANSWER,
45 'ma' => MATCH,
46 'mtf' => 99,
47 'nr' => NUMERICAL,
48 'pr' => 99,
49 'es' => 99,
50 'ca' => 99,
51 'ot' => 99,
52 'sa' => ESSAY
53 );
f34488b2 54
55 public $matching_questions = array();
84769fd8 56
57 function provide_import() {
58 return true;
59 }
f34488b2 60
53f2f306
JMV
61 public function mime_type() {
62 return 'application/xml';
63 }
64
0b3940f8 65 /**
66 * unxmlise reconstructs part of the xml data structure in order
67 * to identify the actual data therein
68 * @param array $xml section of the xml data structure
69 * @return string data with evrything else removed
70 */
71 function unxmlise( $xml ) {
72 // if it's not an array then it's probably just data
73 if (!is_array($xml)) {
294ce987 74 $text = s($xml);
0b3940f8 75 }
76 else {
77 // otherwise parse the array
78 $text = '';
79 foreach ($xml as $tag=>$data) {
80 // if tag is '@' then it's attributes and we don't care
f34488b2 81 if ($tag!=='@') {
0b3940f8 82 $text = $text . $this->unxmlise( $data );
83 }
84769fd8 84 }
84769fd8 85 }
0b3940f8 86
87 // currently we throw the tags we found
88 $text = strip_tags($text);
89 return $text;
84769fd8 90 }
53f2f306
JMV
91 protected function text_field($text) {
92 return array(
93 'text' => htmlspecialchars(trim($text), ENT_NOQUOTES),
94 'format' => FORMAT_HTML,
95 'files' => array(),
96 );
97 }
98
99 protected function add_blank_combined_feedback($question) {
100 $question->correctfeedback['text'] = '';
101 $question->correctfeedback['format'] = $question->questiontextformat;
102 $question->correctfeedback['files'] = array();
103 $question->partiallycorrectfeedback['text'] = '';
104 $question->partiallycorrectfeedback['format'] = $question->questiontextformat;
105 $question->partiallycorrectfeedback['files'] = array();
106 $question->incorrectfeedback['text'] = '';
107 $question->incorrectfeedback['format'] = $question->questiontextformat;
108 $question->incorrectfeedback['files'] = array();
109 return $question;
110 }
f34488b2 111
84769fd8 112 function parse_matching_groups($matching_groups)
113 {
114 if (empty($matching_groups)) {
115 return;
116 }
117 foreach($matching_groups as $match_group) {
118 $newgroup = NULL;
119 $groupname = trim($match_group['@']['name']);
0b3940f8 120 $questiontext = $this->unxmlise($match_group['#']['text'][0]['#']);
84769fd8 121 $newgroup->questiontext = trim($questiontext);
122 $newgroup->subchoices = array();
123 $newgroup->subquestions = array();
124 $newgroup->subanswers = array();
125 $choices = $match_group['#']['choices']['0']['#'];
126 foreach($choices as $key => $value) {
127 if (strpos(trim($key),'choice-') !== FALSE) {
128 $key = strtoupper(trim(str_replace('choice-', '', $key)));
129 $newgroup->subchoices[$key] = trim($value['0']['#']);
130 }
131 }
132 $this->matching_questions[$groupname] = $newgroup;
133 }
134 }
f34488b2 135
84769fd8 136 function parse_ma($qrec, $groupname)
137 {
138 $match_group = $this->matching_questions[$groupname];
0b3940f8 139 $phrase = trim($this->unxmlise($qrec['text']['0']['#']));
140 $answer = trim($this->unxmlise($qrec['answer']['0']['#']));
141 $answer = strip_tags( $answer );
53f2f306 142 $match_group->subquestions[] = $this->text_field($phrase);
84769fd8 143 $match_group->subanswers[] = $match_group->subchoices[$answer];
144 $this->matching_questions[$groupname] = $match_group;
145 return NULL;
146 }
f34488b2 147
84769fd8 148 function process_matches(&$questions)
149 {
150 if (empty($this->matching_questions)) {
151 return;
152 }
153 foreach($this->matching_questions as $match_group) {
154 $question = $this->defaultquestion();
294ce987 155 $htmltext = s($match_group->questiontext);
84769fd8 156 $question->questiontext = $htmltext;
53f2f306
JMV
157 $question->questiontextformat = FORMAT_HTML;
158 $question->questiontextfiles = array();
159 $question->name = shorten_text( $question->questiontext, 250 );
84769fd8 160 $question->qtype = MATCH;
53f2f306 161 $question = $this->add_blank_combined_feedback($question);
84769fd8 162 $question->subquestions = array();
163 $question->subanswers = array();
164 foreach($match_group->subquestions as $key => $value) {
294ce987 165 $htmltext = s($value);
84769fd8 166 $question->subquestions[] = $htmltext;
167
294ce987 168 $htmltext = s($match_group->subanswers[$key]);
84769fd8 169 $question->subanswers[] = $htmltext;
170 }
171 $questions[] = $question;
172 }
173 }
f34488b2 174
2befe778 175 function cleanUnicode($text) {
2befe778 176 return str_replace('&#x2019;', "'", $text);
177 }
06968068 178
4d188926 179 protected function readquestions($lines) {
84769fd8 180 /// Parses an array of lines into an array of questions,
181 /// where each item is a question object as defined by
182 /// readquestion().
06968068 183
84769fd8 184 $questions = array();
185 $currentquestion = array();
f34488b2 186
84769fd8 187 $text = implode($lines, ' ');
2befe778 188 $text = $this->cleanUnicode($text);
84769fd8 189
190 $xml = xmlize($text, 0);
06968068 191 if (!empty($xml['examview']['#']['matching-group'])) {
192 $this->parse_matching_groups($xml['examview']['#']['matching-group']);
193 }
f34488b2 194
84769fd8 195 $questionNode = $xml['examview']['#']['question'];
196 foreach($questionNode as $currentquestion) {
197 if ($question = $this->readquestion($currentquestion)) {
198 $questions[] = $question;
199 }
200 }
f34488b2 201
84769fd8 202 $this->process_matches($questions);
84769fd8 203 return $questions;
204 }
205 // end readquestions
f34488b2 206
84769fd8 207 function readquestion($qrec)
208 {
f34488b2 209
84769fd8 210 $type = trim($qrec['@']['type']);
211 $question = $this->defaultquestion();
a60b5c16 212 if (array_key_exists($type, $this->qtypes)) {
213 $question->qtype = $this->qtypes[$type];
214 }
215 else {
216 $question->qtype = null;
217 }
84769fd8 218 $question->single = 1;
219 // Only one answer is allowed
0b3940f8 220 $htmltext = $this->unxmlise($qrec['#']['text'][0]['#']);
84769fd8 221 $question->questiontext = $htmltext;
53f2f306
JMV
222 $question->questiontextformat = FORMAT_HTML;
223 $question->questiontextfiles = array();
85474b26 224 $question->name = shorten_text( $question->questiontext, 250 );
f34488b2 225
84769fd8 226 switch ($question->qtype) {
227 case MULTICHOICE:
228 $question = $this->parse_mc($qrec['#'], $question);
229 break;
230 case MATCH:
231 $groupname = trim($qrec['@']['group']);
232 $question = $this->parse_ma($qrec['#'], $groupname);
233 break;
234 case TRUEFALSE:
235 $question = $this->parse_tf_yn($qrec['#'], $question);
236 break;
237 case SHORTANSWER:
238 $question = $this->parse_co($qrec['#'], $question);
239 break;
85474b26 240 case ESSAY:
241 $question = $this->parse_sa($qrec['#'], $question);
242 break;
84769fd8 243 case NUMERICAL:
244 $question = $this->parse_nr($qrec['#'], $question);
245 break;
246 break;
247 default:
248 print("<p>Question type ".$type." import not supported for ".$question->questiontext."<p>");
249 $question = NULL;
250 }
251 // end switch ($question->qtype)
f34488b2 252
84769fd8 253 return $question;
254 }
255 // end readquestion
f34488b2 256
84769fd8 257 function parse_tf_yn($qrec, $question)
258 {
259 $choices = array('T' => 1, 'Y' => 1, 'F' => 0, 'N' => 0 );
260 $answer = trim($qrec['answer'][0]['#']);
261 $question->answer = $choices[$answer];
fa5cb18b 262 $question->correctanswer = $question->answer;
84769fd8 263 if ($question->answer == 1) {
53f2f306
JMV
264 $question->feedbacktrue = $this->text_field('Correct');
265 $question->feedbackfalse = $this->text_field('Incorrect');
84769fd8 266 } else {
53f2f306
JMV
267 $question->feedbacktrue = $this->text_field('Incorrect');
268 $question->feedbackfalse = $this->text_field('Correct');
84769fd8 269 }
270 return $question;
271 }
f34488b2 272
84769fd8 273 function parse_mc($qrec, $question)
274 {
53f2f306 275 $question = $this->add_blank_combined_feedback($question);
84769fd8 276 $answer = 'choice-'.strtolower(trim($qrec['answer'][0]['#']));
f34488b2 277
84769fd8 278 $choices = $qrec['choices'][0]['#'];
279 foreach($choices as $key => $value) {
280 if (strpos(trim($key),'choice-') !== FALSE) {
f34488b2 281
53f2f306 282 $question->answer[$key] = $this->text_field(s($this->unxmlise($value[0]['#'])));
84769fd8 283 if (strcmp($key, $answer) == 0) {
284 $question->fraction[$key] = 1;
53f2f306 285 $question->feedback[$key] = $this->text_field('Correct');
84769fd8 286 } else {
287 $question->fraction[$key] = 0;
53f2f306 288 $question->feedback[$key] = $this->text_field('Incorrect');
84769fd8 289 }
290 }
291 }
292 return $question;
293 }
f34488b2 294
84769fd8 295 function parse_co($qrec, $question)
296 {
297 $question->usecase = 0;
0b3940f8 298 $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
299 $answer = strip_tags( $answer );
84769fd8 300 $answers = explode("\n",$answer);
f34488b2 301
84769fd8 302 foreach($answers as $key => $value) {
303 $value = trim($value);
304 if (strlen($value) > 0) {
294ce987 305 $question->answer[$key] = $value;
84769fd8 306 $question->fraction[$key] = 1;
53f2f306 307 $question->feedback[$key] = $this->text_field("Correct");
84769fd8 308 }
309 }
310 return $question;
311 }
85474b26 312
313 function parse_sa($qrec, $question) {
314 $feedback = trim($this->unxmlise($qrec['answer'][0]['#']));
315 $question->feedback = $feedback;
316 $question->fraction = 0;
317 return $question;
318 }
f34488b2 319
84769fd8 320 function parse_nr($qrec, $question)
321 {
0b3940f8 322 $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
323 $answer = strip_tags( $answer );
84769fd8 324 $answers = explode("\n",$answer);
f34488b2 325
84769fd8 326 foreach($answers as $key => $value) {
327 $value = trim($value);
328 if (is_numeric($value)) {
329 $errormargin = 0;
330 $question->answer[$key] = $value;
331 $question->fraction[$key] = 1;
53f2f306 332 $question->feedback[$key] = $this->text_field("Correct");
84769fd8 333 $question->min[$key] = $question->answer[$key] - $errormargin;
334 $question->max[$key] = $question->answer[$key] + $errormargin;
335 }
336 }
337 return $question;
f34488b2 338 }
339
84769fd8 340}
341// end class
342
aeb15530 343