MDL-34808 qformat examview Add phpunit tests to examview import format
[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 *
1ff3389a 20 * @package qformat_examview
d3603157
TH
21 * @copyright 2005 Howard Miller
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41a89a07 23 */
84769fd8 24
84769fd8 25
a17b297d
TH
26defined('MOODLE_INTERNAL') || die();
27
0940ba1f 28require_once($CFG->libdir . '/xmlize.php');
d3603157
TH
29
30
31/**
32 * Examview question importer.
33 *
34 * @copyright 2005 Howard Miller
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
1ff3389a 37class qformat_examview extends qformat_based_on_xml {
f34488b2 38
39 public $qtypes = array(
85474b26 40 'tf' => TRUEFALSE,
41 'mc' => MULTICHOICE,
42 'yn' => TRUEFALSE,
43 'co' => SHORTANSWER,
44 'ma' => MATCH,
45 'mtf' => 99,
46 'nr' => NUMERICAL,
47 'pr' => 99,
e3f79e93 48 'es' => ESSAY,
85474b26 49 'ca' => 99,
50 'ot' => 99,
1ff3389a
JMV
51 'sa' => SHORTANSWER,
52 );
f34488b2 53
54 public $matching_questions = array();
84769fd8 55
0940ba1f 56 public function provide_import() {
84769fd8 57 return true;
58 }
f34488b2 59
53f2f306
JMV
60 public function mime_type() {
61 return 'application/xml';
62 }
63
1ff3389a
JMV
64 /**
65 * Some softwares put entities in exported files.
66 * This method try to clean up known problems.
67 * @param string str string to correct
68 * @return string the corrected string
69 */
70 public function cleaninput($str) {
71
72 $html_code_list = array(
73 "&#039;" => "'",
74 "&#8217;" => "'",
75 "&#8220;" => "\"",
76 "&#8221;" => "\"",
77 "&#8211;" => "-",
78 "&#8212;" => "-",
79 );
80 $str = strtr($str, $html_code_list);
81 // Use textlib entities_to_utf8 function to convert only numerical entities.
82 $str = textlib::entities_to_utf8( $str, false);
83 return $str;
84 }
85
0b3940f8 86 /**
87 * unxmlise reconstructs part of the xml data structure in order
88 * to identify the actual data therein
89 * @param array $xml section of the xml data structure
90 * @return string data with evrything else removed
91 */
0940ba1f
JMV
92 protected function unxmlise( $xml ) {
93 // If it's not an array then it's probably just data.
0b3940f8 94 if (!is_array($xml)) {
294ce987 95 $text = s($xml);
0940ba1f
JMV
96 } else {
97 // Otherwise parse the array.
0b3940f8 98 $text = '';
0940ba1f
JMV
99 foreach ($xml as $tag => $data) {
100 // If tag is '@' then it's attributes and we don't care.
f34488b2 101 if ($tag!=='@') {
0b3940f8 102 $text = $text . $this->unxmlise( $data );
103 }
84769fd8 104 }
84769fd8 105 }
0b3940f8 106
0940ba1f 107 // Currently we throw the tags we found.
0b3940f8 108 $text = strip_tags($text);
109 return $text;
84769fd8 110 }
53f2f306
JMV
111
112 protected function add_blank_combined_feedback($question) {
113 $question->correctfeedback['text'] = '';
114 $question->correctfeedback['format'] = $question->questiontextformat;
115 $question->correctfeedback['files'] = array();
116 $question->partiallycorrectfeedback['text'] = '';
117 $question->partiallycorrectfeedback['format'] = $question->questiontextformat;
118 $question->partiallycorrectfeedback['files'] = array();
119 $question->incorrectfeedback['text'] = '';
120 $question->incorrectfeedback['format'] = $question->questiontextformat;
121 $question->incorrectfeedback['files'] = array();
122 return $question;
123 }
f34488b2 124
1ff3389a 125 public function parse_matching_groups($matching_groups) {
84769fd8 126 if (empty($matching_groups)) {
127 return;
128 }
0940ba1f 129 foreach ($matching_groups as $match_group) {
e3f79e93 130 $newgroup = new stdClass();
84769fd8 131 $groupname = trim($match_group['@']['name']);
0b3940f8 132 $questiontext = $this->unxmlise($match_group['#']['text'][0]['#']);
84769fd8 133 $newgroup->questiontext = trim($questiontext);
134 $newgroup->subchoices = array();
135 $newgroup->subquestions = array();
136 $newgroup->subanswers = array();
137 $choices = $match_group['#']['choices']['0']['#'];
0940ba1f
JMV
138 foreach ($choices as $key => $value) {
139 if (strpos(trim($key), 'choice-') !== false) {
84769fd8 140 $key = strtoupper(trim(str_replace('choice-', '', $key)));
141 $newgroup->subchoices[$key] = trim($value['0']['#']);
142 }
143 }
144 $this->matching_questions[$groupname] = $newgroup;
145 }
146 }
f34488b2 147
0940ba1f 148 protected function parse_ma($qrec, $groupname) {
84769fd8 149 $match_group = $this->matching_questions[$groupname];
0b3940f8 150 $phrase = trim($this->unxmlise($qrec['text']['0']['#']));
151 $answer = trim($this->unxmlise($qrec['answer']['0']['#']));
152 $answer = strip_tags( $answer );
1ff3389a 153 $match_group->mappings[$phrase] = $match_group->subchoices[$answer];
84769fd8 154 $this->matching_questions[$groupname] = $match_group;
0940ba1f 155 return null;
84769fd8 156 }
f34488b2 157
0940ba1f 158 protected function process_matches(&$questions) {
84769fd8 159 if (empty($this->matching_questions)) {
160 return;
161 }
1ff3389a 162
0940ba1f 163 foreach ($this->matching_questions as $match_group) {
84769fd8 164 $question = $this->defaultquestion();
294ce987 165 $htmltext = s($match_group->questiontext);
84769fd8 166 $question->questiontext = $htmltext;
53f2f306
JMV
167 $question->questiontextformat = FORMAT_HTML;
168 $question->questiontextfiles = array();
169 $question->name = shorten_text( $question->questiontext, 250 );
84769fd8 170 $question->qtype = MATCH;
53f2f306 171 $question = $this->add_blank_combined_feedback($question);
84769fd8 172 $question->subquestions = array();
173 $question->subanswers = array();
1ff3389a
JMV
174 foreach ($match_group->subchoices as $subchoice) {
175 $fiber = array_keys ($match_group->mappings, $subchoice);
176 $subquestion = '';
177 foreach ($fiber as $subquestion) {
178 $question->subquestions[] = $this->text_field($subquestion);
179 $question->subanswers[] = $subchoice;
180 }
181 if ($subquestion == '') { // Then in this case, $subchoice is a distractor.
182 $question->subquestions[] = $this->text_field('');
183 $question->subanswers[] = $subchoice;
184 }
84769fd8 185 }
186 $questions[] = $question;
187 }
188 }
f34488b2 189
0940ba1f 190 protected function cleanunicode($text) {
2befe778 191 return str_replace('&#x2019;', "'", $text);
192 }
06968068 193
1ff3389a 194 public function readquestions($lines) {
0940ba1f
JMV
195 // Parses an array of lines into an array of questions,
196 // where each item is a question object as defined by
197 // readquestion().
06968068 198
84769fd8 199 $questions = array();
200 $currentquestion = array();
f34488b2 201
84769fd8 202 $text = implode($lines, ' ');
0940ba1f 203 $text = $this->cleanunicode($text);
84769fd8 204
205 $xml = xmlize($text, 0);
06968068 206 if (!empty($xml['examview']['#']['matching-group'])) {
207 $this->parse_matching_groups($xml['examview']['#']['matching-group']);
208 }
f34488b2 209
0940ba1f
JMV
210 $questionnode = $xml['examview']['#']['question'];
211 foreach ($questionnode as $currentquestion) {
84769fd8 212 if ($question = $this->readquestion($currentquestion)) {
213 $questions[] = $question;
214 }
215 }
f34488b2 216
84769fd8 217 $this->process_matches($questions);
84769fd8 218 return $questions;
219 }
f34488b2 220
0940ba1f 221 public function readquestion($qrec) {
f34488b2 222
84769fd8 223 $type = trim($qrec['@']['type']);
224 $question = $this->defaultquestion();
a60b5c16 225 if (array_key_exists($type, $this->qtypes)) {
226 $question->qtype = $this->qtypes[$type];
0940ba1f 227 } else {
a60b5c16 228 $question->qtype = null;
229 }
84769fd8 230 $question->single = 1;
1ff3389a 231
0940ba1f 232 // Only one answer is allowed.
0b3940f8 233 $htmltext = $this->unxmlise($qrec['#']['text'][0]['#']);
1ff3389a
JMV
234
235 $question->questiontext = $this->cleaninput($htmltext);
53f2f306
JMV
236 $question->questiontextformat = FORMAT_HTML;
237 $question->questiontextfiles = array();
85474b26 238 $question->name = shorten_text( $question->questiontext, 250 );
f34488b2 239
84769fd8 240 switch ($question->qtype) {
0940ba1f
JMV
241 case MULTICHOICE:
242 $question = $this->parse_mc($qrec['#'], $question);
243 break;
244 case MATCH:
245 $groupname = trim($qrec['@']['group']);
246 $question = $this->parse_ma($qrec['#'], $groupname);
247 break;
248 case TRUEFALSE:
249 $question = $this->parse_tf_yn($qrec['#'], $question);
250 break;
251 case SHORTANSWER:
252 $question = $this->parse_co($qrec['#'], $question);
253 break;
254 case ESSAY:
e3f79e93 255 $question = $this->parse_es($qrec['#'], $question);
0940ba1f
JMV
256 break;
257 case NUMERICAL:
258 $question = $this->parse_nr($qrec['#'], $question);
259 break;
260 break;
84769fd8 261 default:
0940ba1f
JMV
262 print("<p>Question type ".$type." import not supported for ".$question->questiontext."<p>");
263 $question = null;
84769fd8 264 }
f34488b2 265
84769fd8 266 return $question;
267 }
f34488b2 268
0940ba1f 269 protected function parse_tf_yn($qrec, $question) {
84769fd8 270 $choices = array('T' => 1, 'Y' => 1, 'F' => 0, 'N' => 0 );
271 $answer = trim($qrec['answer'][0]['#']);
272 $question->answer = $choices[$answer];
fa5cb18b 273 $question->correctanswer = $question->answer;
84769fd8 274 if ($question->answer == 1) {
1ff3389a
JMV
275 $question->feedbacktrue = $this->text_field(get_string('correct', 'question'));
276 $question->feedbackfalse = $this->text_field(get_string('incorrect', 'question'));
84769fd8 277 } else {
1ff3389a
JMV
278 $question->feedbacktrue = $this->text_field(get_string('incorrect', 'question'));
279 $question->feedbackfalse = $this->text_field(get_string('correct', 'question'));
84769fd8 280 }
281 return $question;
282 }
f34488b2 283
0940ba1f 284 protected function parse_mc($qrec, $question) {
53f2f306 285 $question = $this->add_blank_combined_feedback($question);
84769fd8 286 $answer = 'choice-'.strtolower(trim($qrec['answer'][0]['#']));
f34488b2 287
84769fd8 288 $choices = $qrec['choices'][0]['#'];
0940ba1f
JMV
289 foreach ($choices as $key => $value) {
290 if (strpos(trim($key), 'choice-') !== false) {
f34488b2 291
1ff3389a 292 $question->answer[] = $this->text_field(s($this->unxmlise($value[0]['#'])));
84769fd8 293 if (strcmp($key, $answer) == 0) {
1ff3389a
JMV
294 $question->fraction[] = 1;
295 $question->feedback[] = $this->text_field(get_string('correct', 'question'));
84769fd8 296 } else {
1ff3389a
JMV
297 $question->fraction[] = 0;
298 $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
84769fd8 299 }
300 }
301 }
302 return $question;
303 }
f34488b2 304
0940ba1f 305 protected function parse_co($qrec, $question) {
84769fd8 306 $question->usecase = 0;
0b3940f8 307 $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
308 $answer = strip_tags( $answer );
0940ba1f 309 $answers = explode("\n", $answer);
f34488b2 310
0940ba1f 311 foreach ($answers as $key => $value) {
84769fd8 312 $value = trim($value);
313 if (strlen($value) > 0) {
1ff3389a
JMV
314 $question->answer[] = $value;
315 $question->fraction[] = 1;
316 $question->feedback[] = $this->text_field(get_string('correct', 'question'));
84769fd8 317 }
318 }
1ff3389a
JMV
319 $question->answer[] = '*';
320 $question->fraction[] = 0;
321 $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
322
84769fd8 323 return $question;
324 }
85474b26 325
e3f79e93 326 protected function parse_es($qrec, $question) {
85474b26 327 $feedback = trim($this->unxmlise($qrec['answer'][0]['#']));
e3f79e93 328 $question->graderinfo = $this->text_field($feedback);
85474b26 329 $question->feedback = $feedback;
e3f79e93
JMV
330 $question->responseformat = 'editor';
331 $question->responsefieldlines = 15;
332 $question->attachments = 0;
85474b26 333 $question->fraction = 0;
334 return $question;
335 }
f34488b2 336
0940ba1f 337 protected function parse_nr($qrec, $question) {
0b3940f8 338 $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
339 $answer = strip_tags( $answer );
0940ba1f 340 $answers = explode("\n", $answer);
f34488b2 341
0940ba1f 342 foreach ($answers as $key => $value) {
84769fd8 343 $value = trim($value);
344 if (is_numeric($value)) {
345 $errormargin = 0;
1ff3389a
JMV
346 $question->answer[] = $value;
347 $question->fraction[] = 1;
348 $question->feedback[] = $this->text_field(get_string('correct', 'question'));
349 $question->tolerance[] = $errormargin;
84769fd8 350 }
351 }
352 return $question;
f34488b2 353 }
354
84769fd8 355}
0940ba1f 356// End class.
84769fd8 357
aeb15530 358