Merge branch 'MDL-70326-MOODLE_310_STABLE' of https://github.com/durzo/moodle into...
[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 *
9d06b9ee 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
9c3afc5b 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 */
9d06b9ee 37class qformat_examview extends qformat_based_on_xml {
f34488b2 38
39 public $qtypes = array(
e231a3ff
TH
40 'tf' => 'truefalse',
41 'mc' => 'multichoice',
42 'yn' => 'truefalse',
43 'co' => 'shortanswer',
44 'ma' => 'match',
85474b26 45 'mtf' => 99,
e231a3ff 46 'nr' => 'numerical',
85474b26 47 'pr' => 99,
e231a3ff 48 'es' => 'essay',
85474b26 49 'ca' => 99,
50 'ot' => 99,
e231a3ff 51 'sa' => 'shortanswer',
9d06b9ee 52 );
f34488b2 53
d2fd7262 54 public $matchingquestions = array();
84769fd8 55
9c3afc5b 56 public function provide_import() {
84769fd8 57 return true;
58 }
f34488b2 59
2562c138
JMV
60 public function mime_type() {
61 return 'application/xml';
62 }
63
0b3940f8 64 /**
65 * unxmlise reconstructs part of the xml data structure in order
66 * to identify the actual data therein
67 * @param array $xml section of the xml data structure
68 * @return string data with evrything else removed
69 */
9c3afc5b
JMV
70 protected function unxmlise( $xml ) {
71 // If it's not an array then it's probably just data.
0b3940f8 72 if (!is_array($xml)) {
294ce987 73 $text = s($xml);
9c3afc5b
JMV
74 } else {
75 // Otherwise parse the array.
0b3940f8 76 $text = '';
9c3afc5b
JMV
77 foreach ($xml as $tag => $data) {
78 // If tag is '@' then it's attributes and we don't care.
f34488b2 79 if ($tag!=='@') {
0b3940f8 80 $text = $text . $this->unxmlise( $data );
81 }
84769fd8 82 }
84769fd8 83 }
0b3940f8 84
9c3afc5b 85 // Currently we throw the tags we found.
0b3940f8 86 $text = strip_tags($text);
87 return $text;
84769fd8 88 }
2562c138 89
d2fd7262
JMV
90 public function parse_matching_groups($matchinggroups) {
91 if (empty($matchinggroups)) {
84769fd8 92 return;
93 }
d2fd7262 94 foreach ($matchinggroups as $matchgroup) {
3a24ae8b 95 $newgroup = new stdClass();
d2fd7262
JMV
96 $groupname = trim($matchgroup['@']['name']);
97 $questiontext = $this->unxmlise($matchgroup['#']['text'][0]['#']);
84769fd8 98 $newgroup->questiontext = trim($questiontext);
99 $newgroup->subchoices = array();
100 $newgroup->subquestions = array();
101 $newgroup->subanswers = array();
d2fd7262 102 $choices = $matchgroup['#']['choices']['0']['#'];
9c3afc5b
JMV
103 foreach ($choices as $key => $value) {
104 if (strpos(trim($key), 'choice-') !== false) {
84769fd8 105 $key = strtoupper(trim(str_replace('choice-', '', $key)));
106 $newgroup->subchoices[$key] = trim($value['0']['#']);
107 }
108 }
109 $this->matching_questions[$groupname] = $newgroup;
110 }
111 }
f34488b2 112
9c3afc5b 113 protected function parse_ma($qrec, $groupname) {
d2fd7262 114 $matchgroup = $this->matching_questions[$groupname];
0b3940f8 115 $phrase = trim($this->unxmlise($qrec['text']['0']['#']));
116 $answer = trim($this->unxmlise($qrec['answer']['0']['#']));
117 $answer = strip_tags( $answer );
d2fd7262
JMV
118 $matchgroup->mappings[$phrase] = $matchgroup->subchoices[$answer];
119 $this->matching_questions[$groupname] = $matchgroup;
9c3afc5b 120 return null;
84769fd8 121 }
f34488b2 122
9c3afc5b 123 protected function process_matches(&$questions) {
84769fd8 124 if (empty($this->matching_questions)) {
125 return;
126 }
9d06b9ee 127
d2fd7262 128 foreach ($this->matching_questions as $matchgroup) {
84769fd8 129 $question = $this->defaultquestion();
d2fd7262 130 $htmltext = s($matchgroup->questiontext);
84769fd8 131 $question->questiontext = $htmltext;
2562c138
JMV
132 $question->questiontextformat = FORMAT_HTML;
133 $question->questiontextfiles = array();
cacb8fa0 134 $question->name = $this->create_default_question_name($question->questiontext, get_string('questionname', 'question'));
e231a3ff 135 $question->qtype = 'match';
2562c138 136 $question = $this->add_blank_combined_feedback($question);
84769fd8 137 $question->subquestions = array();
138 $question->subanswers = array();
d2fd7262
JMV
139 foreach ($matchgroup->subchoices as $subchoice) {
140 $fiber = array_keys ($matchgroup->mappings, $subchoice);
9d06b9ee
JMV
141 $subquestion = '';
142 foreach ($fiber as $subquestion) {
143 $question->subquestions[] = $this->text_field($subquestion);
144 $question->subanswers[] = $subchoice;
145 }
146 if ($subquestion == '') { // Then in this case, $subchoice is a distractor.
147 $question->subquestions[] = $this->text_field('');
148 $question->subanswers[] = $subchoice;
149 }
84769fd8 150 }
151 $questions[] = $question;
152 }
153 }
f34488b2 154
9c3afc5b 155 protected function cleanunicode($text) {
2befe778 156 return str_replace('&#x2019;', "'", $text);
157 }
06968068 158
9d06b9ee 159 public function readquestions($lines) {
9c3afc5b
JMV
160 // Parses an array of lines into an array of questions,
161 // where each item is a question object as defined by
162 // readquestion().
06968068 163
84769fd8 164 $questions = array();
165 $currentquestion = array();
f34488b2 166
ee439d01 167 $text = implode(' ', $lines);
9c3afc5b 168 $text = $this->cleanunicode($text);
84769fd8 169
170 $xml = xmlize($text, 0);
06968068 171 if (!empty($xml['examview']['#']['matching-group'])) {
172 $this->parse_matching_groups($xml['examview']['#']['matching-group']);
173 }
f34488b2 174
9c3afc5b
JMV
175 $questionnode = $xml['examview']['#']['question'];
176 foreach ($questionnode as $currentquestion) {
84769fd8 177 if ($question = $this->readquestion($currentquestion)) {
178 $questions[] = $question;
179 }
180 }
f34488b2 181
84769fd8 182 $this->process_matches($questions);
84769fd8 183 return $questions;
184 }
f34488b2 185
9c3afc5b 186 public function readquestion($qrec) {
a5d7862b 187 global $OUTPUT;
f34488b2 188
84769fd8 189 $type = trim($qrec['@']['type']);
190 $question = $this->defaultquestion();
a60b5c16 191 if (array_key_exists($type, $this->qtypes)) {
192 $question->qtype = $this->qtypes[$type];
9c3afc5b 193 } else {
a60b5c16 194 $question->qtype = null;
195 }
84769fd8 196 $question->single = 1;
9d06b9ee 197
9c3afc5b 198 // Only one answer is allowed.
0b3940f8 199 $htmltext = $this->unxmlise($qrec['#']['text'][0]['#']);
9d06b9ee
JMV
200
201 $question->questiontext = $this->cleaninput($htmltext);
2562c138
JMV
202 $question->questiontextformat = FORMAT_HTML;
203 $question->questiontextfiles = array();
cacb8fa0 204 $question->name = $this->create_default_question_name($question->questiontext, get_string('questionname', 'question'));
f34488b2 205
84769fd8 206 switch ($question->qtype) {
e231a3ff 207 case 'multichoice':
9c3afc5b
JMV
208 $question = $this->parse_mc($qrec['#'], $question);
209 break;
e231a3ff 210 case 'match':
9c3afc5b
JMV
211 $groupname = trim($qrec['@']['group']);
212 $question = $this->parse_ma($qrec['#'], $groupname);
213 break;
e231a3ff 214 case 'truefalse':
9c3afc5b
JMV
215 $question = $this->parse_tf_yn($qrec['#'], $question);
216 break;
e231a3ff 217 case 'shortanswer':
9c3afc5b
JMV
218 $question = $this->parse_co($qrec['#'], $question);
219 break;
e231a3ff 220 case 'essay':
3a24ae8b 221 $question = $this->parse_es($qrec['#'], $question);
9c3afc5b 222 break;
e231a3ff 223 case 'numerical':
9c3afc5b
JMV
224 $question = $this->parse_nr($qrec['#'], $question);
225 break;
226 break;
84769fd8 227 default:
a5d7862b 228 echo $OUTPUT->notification(get_string('unknownorunhandledtype', 'question', $type));
9c3afc5b 229 $question = null;
84769fd8 230 }
f34488b2 231
84769fd8 232 return $question;
233 }
f34488b2 234
9c3afc5b 235 protected function parse_tf_yn($qrec, $question) {
84769fd8 236 $choices = array('T' => 1, 'Y' => 1, 'F' => 0, 'N' => 0 );
237 $answer = trim($qrec['answer'][0]['#']);
238 $question->answer = $choices[$answer];
fa5cb18b 239 $question->correctanswer = $question->answer;
84769fd8 240 if ($question->answer == 1) {
9d06b9ee
JMV
241 $question->feedbacktrue = $this->text_field(get_string('correct', 'question'));
242 $question->feedbackfalse = $this->text_field(get_string('incorrect', 'question'));
84769fd8 243 } else {
9d06b9ee
JMV
244 $question->feedbacktrue = $this->text_field(get_string('incorrect', 'question'));
245 $question->feedbackfalse = $this->text_field(get_string('correct', 'question'));
84769fd8 246 }
247 return $question;
248 }
f34488b2 249
9c3afc5b 250 protected function parse_mc($qrec, $question) {
2562c138 251 $question = $this->add_blank_combined_feedback($question);
84769fd8 252 $answer = 'choice-'.strtolower(trim($qrec['answer'][0]['#']));
f34488b2 253
84769fd8 254 $choices = $qrec['choices'][0]['#'];
9c3afc5b
JMV
255 foreach ($choices as $key => $value) {
256 if (strpos(trim($key), 'choice-') !== false) {
f34488b2 257
9d06b9ee 258 $question->answer[] = $this->text_field(s($this->unxmlise($value[0]['#'])));
84769fd8 259 if (strcmp($key, $answer) == 0) {
9d06b9ee
JMV
260 $question->fraction[] = 1;
261 $question->feedback[] = $this->text_field(get_string('correct', 'question'));
84769fd8 262 } else {
9d06b9ee
JMV
263 $question->fraction[] = 0;
264 $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
84769fd8 265 }
266 }
267 }
268 return $question;
269 }
f34488b2 270
9c3afc5b 271 protected function parse_co($qrec, $question) {
84769fd8 272 $question->usecase = 0;
0b3940f8 273 $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
274 $answer = strip_tags( $answer );
9c3afc5b 275 $answers = explode("\n", $answer);
f34488b2 276
9c3afc5b 277 foreach ($answers as $key => $value) {
84769fd8 278 $value = trim($value);
279 if (strlen($value) > 0) {
9d06b9ee
JMV
280 $question->answer[] = $value;
281 $question->fraction[] = 1;
282 $question->feedback[] = $this->text_field(get_string('correct', 'question'));
84769fd8 283 }
284 }
9d06b9ee
JMV
285 $question->answer[] = '*';
286 $question->fraction[] = 0;
287 $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
288
84769fd8 289 return $question;
290 }
85474b26 291
3a24ae8b 292 protected function parse_es($qrec, $question) {
85474b26 293 $feedback = trim($this->unxmlise($qrec['answer'][0]['#']));
3a24ae8b 294 $question->graderinfo = $this->text_field($feedback);
cdcda6a0 295 $question->responsetemplate = $this->text_field('');
a4f765eb 296 $question->responserequired = 1;
85474b26 297 $question->feedback = $feedback;
3a24ae8b
JMV
298 $question->responseformat = 'editor';
299 $question->responsefieldlines = 15;
300 $question->attachments = 0;
a4f765eb 301 $question->attachmentsrequired = 0;
85474b26 302 $question->fraction = 0;
303 return $question;
304 }
f34488b2 305
9c3afc5b 306 protected function parse_nr($qrec, $question) {
0b3940f8 307 $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
308 $answer = strip_tags( $answer );
9c3afc5b 309 $answers = explode("\n", $answer);
f34488b2 310
9c3afc5b 311 foreach ($answers as $key => $value) {
84769fd8 312 $value = trim($value);
313 if (is_numeric($value)) {
314 $errormargin = 0;
9d06b9ee
JMV
315 $question->answer[] = $value;
316 $question->fraction[] = 1;
317 $question->feedback[] = $this->text_field(get_string('correct', 'question'));
318 $question->tolerance[] = $errormargin;
84769fd8 319 }
320 }
321 return $question;
f34488b2 322 }
323
84769fd8 324}
9c3afc5b 325// End class.
84769fd8 326
aeb15530 327