weekly release 2.3.1+
[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
0940ba1f 29require_once($CFG->libdir . '/xmlize.php');
d3603157
TH
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,
e3f79e93 49 'es' => ESSAY,
85474b26 50 'ca' => 99,
51 'ot' => 99,
e3f79e93 52 'sa' => SHORTANSWER
85474b26 53 );
f34488b2 54
55 public $matching_questions = array();
84769fd8 56
0940ba1f 57 public function provide_import() {
84769fd8 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 */
0940ba1f
JMV
71 protected function unxmlise( $xml ) {
72 // If it's not an array then it's probably just data.
0b3940f8 73 if (!is_array($xml)) {
294ce987 74 $text = s($xml);
0940ba1f
JMV
75 } else {
76 // Otherwise parse the array.
0b3940f8 77 $text = '';
0940ba1f
JMV
78 foreach ($xml as $tag => $data) {
79 // If tag is '@' then it's attributes and we don't care.
f34488b2 80 if ($tag!=='@') {
0b3940f8 81 $text = $text . $this->unxmlise( $data );
82 }
84769fd8 83 }
84769fd8 84 }
0b3940f8 85
0940ba1f 86 // Currently we throw the tags we found.
0b3940f8 87 $text = strip_tags($text);
88 return $text;
84769fd8 89 }
53f2f306
JMV
90 protected function text_field($text) {
91 return array(
92 'text' => htmlspecialchars(trim($text), ENT_NOQUOTES),
93 'format' => FORMAT_HTML,
94 'files' => array(),
95 );
96 }
97
98 protected function add_blank_combined_feedback($question) {
99 $question->correctfeedback['text'] = '';
100 $question->correctfeedback['format'] = $question->questiontextformat;
101 $question->correctfeedback['files'] = array();
102 $question->partiallycorrectfeedback['text'] = '';
103 $question->partiallycorrectfeedback['format'] = $question->questiontextformat;
104 $question->partiallycorrectfeedback['files'] = array();
105 $question->incorrectfeedback['text'] = '';
106 $question->incorrectfeedback['format'] = $question->questiontextformat;
107 $question->incorrectfeedback['files'] = array();
108 return $question;
109 }
f34488b2 110
0940ba1f 111 protected function parse_matching_groups($matching_groups) {
84769fd8 112 if (empty($matching_groups)) {
113 return;
114 }
0940ba1f 115 foreach ($matching_groups as $match_group) {
e3f79e93 116 $newgroup = new stdClass();
84769fd8 117 $groupname = trim($match_group['@']['name']);
0b3940f8 118 $questiontext = $this->unxmlise($match_group['#']['text'][0]['#']);
84769fd8 119 $newgroup->questiontext = trim($questiontext);
120 $newgroup->subchoices = array();
121 $newgroup->subquestions = array();
122 $newgroup->subanswers = array();
123 $choices = $match_group['#']['choices']['0']['#'];
0940ba1f
JMV
124 foreach ($choices as $key => $value) {
125 if (strpos(trim($key), 'choice-') !== false) {
84769fd8 126 $key = strtoupper(trim(str_replace('choice-', '', $key)));
127 $newgroup->subchoices[$key] = trim($value['0']['#']);
128 }
129 }
130 $this->matching_questions[$groupname] = $newgroup;
131 }
132 }
f34488b2 133
0940ba1f 134 protected function parse_ma($qrec, $groupname) {
84769fd8 135 $match_group = $this->matching_questions[$groupname];
0b3940f8 136 $phrase = trim($this->unxmlise($qrec['text']['0']['#']));
137 $answer = trim($this->unxmlise($qrec['answer']['0']['#']));
138 $answer = strip_tags( $answer );
e3f79e93 139 $match_group->subquestions[] = $phrase;
84769fd8 140 $match_group->subanswers[] = $match_group->subchoices[$answer];
141 $this->matching_questions[$groupname] = $match_group;
0940ba1f 142 return null;
84769fd8 143 }
f34488b2 144
0940ba1f 145 protected function process_matches(&$questions) {
84769fd8 146 if (empty($this->matching_questions)) {
147 return;
148 }
0940ba1f 149 foreach ($this->matching_questions as $match_group) {
84769fd8 150 $question = $this->defaultquestion();
294ce987 151 $htmltext = s($match_group->questiontext);
84769fd8 152 $question->questiontext = $htmltext;
53f2f306
JMV
153 $question->questiontextformat = FORMAT_HTML;
154 $question->questiontextfiles = array();
155 $question->name = shorten_text( $question->questiontext, 250 );
84769fd8 156 $question->qtype = MATCH;
53f2f306 157 $question = $this->add_blank_combined_feedback($question);
84769fd8 158 $question->subquestions = array();
159 $question->subanswers = array();
0940ba1f 160 foreach ($match_group->subquestions as $key => $value) {
294ce987 161 $htmltext = s($value);
e3f79e93 162 $question->subquestions[] = $this->text_field($htmltext);
84769fd8 163
294ce987 164 $htmltext = s($match_group->subanswers[$key]);
84769fd8 165 $question->subanswers[] = $htmltext;
166 }
167 $questions[] = $question;
168 }
169 }
f34488b2 170
0940ba1f 171 protected function cleanunicode($text) {
2befe778 172 return str_replace('&#x2019;', "'", $text);
173 }
06968068 174
4d188926 175 protected function readquestions($lines) {
0940ba1f
JMV
176 // Parses an array of lines into an array of questions,
177 // where each item is a question object as defined by
178 // readquestion().
06968068 179
84769fd8 180 $questions = array();
181 $currentquestion = array();
f34488b2 182
84769fd8 183 $text = implode($lines, ' ');
0940ba1f 184 $text = $this->cleanunicode($text);
84769fd8 185
186 $xml = xmlize($text, 0);
06968068 187 if (!empty($xml['examview']['#']['matching-group'])) {
188 $this->parse_matching_groups($xml['examview']['#']['matching-group']);
189 }
f34488b2 190
0940ba1f
JMV
191 $questionnode = $xml['examview']['#']['question'];
192 foreach ($questionnode as $currentquestion) {
84769fd8 193 if ($question = $this->readquestion($currentquestion)) {
194 $questions[] = $question;
195 }
196 }
f34488b2 197
84769fd8 198 $this->process_matches($questions);
84769fd8 199 return $questions;
200 }
f34488b2 201
0940ba1f 202 public function readquestion($qrec) {
f34488b2 203
84769fd8 204 $type = trim($qrec['@']['type']);
205 $question = $this->defaultquestion();
a60b5c16 206 if (array_key_exists($type, $this->qtypes)) {
207 $question->qtype = $this->qtypes[$type];
0940ba1f 208 } else {
a60b5c16 209 $question->qtype = null;
210 }
84769fd8 211 $question->single = 1;
0940ba1f 212 // Only one answer is allowed.
0b3940f8 213 $htmltext = $this->unxmlise($qrec['#']['text'][0]['#']);
84769fd8 214 $question->questiontext = $htmltext;
53f2f306
JMV
215 $question->questiontextformat = FORMAT_HTML;
216 $question->questiontextfiles = array();
85474b26 217 $question->name = shorten_text( $question->questiontext, 250 );
f34488b2 218
84769fd8 219 switch ($question->qtype) {
0940ba1f
JMV
220 case MULTICHOICE:
221 $question = $this->parse_mc($qrec['#'], $question);
222 break;
223 case MATCH:
224 $groupname = trim($qrec['@']['group']);
225 $question = $this->parse_ma($qrec['#'], $groupname);
226 break;
227 case TRUEFALSE:
228 $question = $this->parse_tf_yn($qrec['#'], $question);
229 break;
230 case SHORTANSWER:
231 $question = $this->parse_co($qrec['#'], $question);
232 break;
233 case ESSAY:
e3f79e93 234 $question = $this->parse_es($qrec['#'], $question);
0940ba1f
JMV
235 break;
236 case NUMERICAL:
237 $question = $this->parse_nr($qrec['#'], $question);
238 break;
239 break;
84769fd8 240 default:
0940ba1f
JMV
241 print("<p>Question type ".$type." import not supported for ".$question->questiontext."<p>");
242 $question = null;
84769fd8 243 }
f34488b2 244
84769fd8 245 return $question;
246 }
f34488b2 247
0940ba1f 248 protected function parse_tf_yn($qrec, $question) {
84769fd8 249 $choices = array('T' => 1, 'Y' => 1, 'F' => 0, 'N' => 0 );
250 $answer = trim($qrec['answer'][0]['#']);
251 $question->answer = $choices[$answer];
fa5cb18b 252 $question->correctanswer = $question->answer;
84769fd8 253 if ($question->answer == 1) {
53f2f306
JMV
254 $question->feedbacktrue = $this->text_field('Correct');
255 $question->feedbackfalse = $this->text_field('Incorrect');
84769fd8 256 } else {
53f2f306
JMV
257 $question->feedbacktrue = $this->text_field('Incorrect');
258 $question->feedbackfalse = $this->text_field('Correct');
84769fd8 259 }
260 return $question;
261 }
f34488b2 262
0940ba1f 263 protected function parse_mc($qrec, $question) {
53f2f306 264 $question = $this->add_blank_combined_feedback($question);
84769fd8 265 $answer = 'choice-'.strtolower(trim($qrec['answer'][0]['#']));
f34488b2 266
84769fd8 267 $choices = $qrec['choices'][0]['#'];
0940ba1f
JMV
268 foreach ($choices as $key => $value) {
269 if (strpos(trim($key), 'choice-') !== false) {
f34488b2 270
53f2f306 271 $question->answer[$key] = $this->text_field(s($this->unxmlise($value[0]['#'])));
84769fd8 272 if (strcmp($key, $answer) == 0) {
273 $question->fraction[$key] = 1;
53f2f306 274 $question->feedback[$key] = $this->text_field('Correct');
84769fd8 275 } else {
276 $question->fraction[$key] = 0;
53f2f306 277 $question->feedback[$key] = $this->text_field('Incorrect');
84769fd8 278 }
279 }
280 }
281 return $question;
282 }
f34488b2 283
0940ba1f 284 protected function parse_co($qrec, $question) {
84769fd8 285 $question->usecase = 0;
0b3940f8 286 $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
287 $answer = strip_tags( $answer );
0940ba1f 288 $answers = explode("\n", $answer);
f34488b2 289
0940ba1f 290 foreach ($answers as $key => $value) {
84769fd8 291 $value = trim($value);
292 if (strlen($value) > 0) {
294ce987 293 $question->answer[$key] = $value;
84769fd8 294 $question->fraction[$key] = 1;
53f2f306 295 $question->feedback[$key] = $this->text_field("Correct");
84769fd8 296 }
297 }
298 return $question;
299 }
85474b26 300
e3f79e93 301 protected function parse_es($qrec, $question) {
85474b26 302 $feedback = trim($this->unxmlise($qrec['answer'][0]['#']));
e3f79e93 303 $question->graderinfo = $this->text_field($feedback);
85474b26 304 $question->feedback = $feedback;
e3f79e93
JMV
305 $question->responseformat = 'editor';
306 $question->responsefieldlines = 15;
307 $question->attachments = 0;
85474b26 308 $question->fraction = 0;
309 return $question;
310 }
f34488b2 311
0940ba1f 312 protected function parse_nr($qrec, $question) {
0b3940f8 313 $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
314 $answer = strip_tags( $answer );
0940ba1f 315 $answers = explode("\n", $answer);
f34488b2 316
0940ba1f 317 foreach ($answers as $key => $value) {
84769fd8 318 $value = trim($value);
319 if (is_numeric($value)) {
320 $errormargin = 0;
321 $question->answer[$key] = $value;
322 $question->fraction[$key] = 1;
53f2f306 323 $question->feedback[$key] = $this->text_field("Correct");
e3f79e93 324 $question->tolerance[$key] = $errormargin;
84769fd8 325 }
326 }
327 return $question;
f34488b2 328 }
329
84769fd8 330}
0940ba1f 331// End class.
84769fd8 332
aeb15530 333