2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Examview question importer.
20 * @package qformat_examview
21 * @copyright 2005 Howard Miller
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir . '/xmlize.php');
32 * Examview question importer.
34 * @copyright 2005 Howard Miller
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class qformat_examview extends qformat_based_on_xml {
39 public $qtypes = array(
54 public $matching_questions = array();
56 public function provide_import() {
60 public function mime_type() {
61 return 'application/xml';
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
70 public function cleaninput($str) {
72 $html_code_list = array(
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);
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
92 protected function unxmlise( $xml ) {
93 // If it's not an array then it's probably just data.
94 if (!is_array($xml)) {
97 // Otherwise parse the array.
99 foreach ($xml as $tag => $data) {
100 // If tag is '@' then it's attributes and we don't care.
102 $text = $text . $this->unxmlise( $data );
107 // Currently we throw the tags we found.
108 $text = strip_tags($text);
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();
125 public function parse_matching_groups($matching_groups) {
126 if (empty($matching_groups)) {
129 foreach ($matching_groups as $match_group) {
130 $newgroup = new stdClass();
131 $groupname = trim($match_group['@']['name']);
132 $questiontext = $this->unxmlise($match_group['#']['text'][0]['#']);
133 $newgroup->questiontext = trim($questiontext);
134 $newgroup->subchoices = array();
135 $newgroup->subquestions = array();
136 $newgroup->subanswers = array();
137 $choices = $match_group['#']['choices']['0']['#'];
138 foreach ($choices as $key => $value) {
139 if (strpos(trim($key), 'choice-') !== false) {
140 $key = strtoupper(trim(str_replace('choice-', '', $key)));
141 $newgroup->subchoices[$key] = trim($value['0']['#']);
144 $this->matching_questions[$groupname] = $newgroup;
148 protected function parse_ma($qrec, $groupname) {
149 $match_group = $this->matching_questions[$groupname];
150 $phrase = trim($this->unxmlise($qrec['text']['0']['#']));
151 $answer = trim($this->unxmlise($qrec['answer']['0']['#']));
152 $answer = strip_tags( $answer );
153 $match_group->mappings[$phrase] = $match_group->subchoices[$answer];
154 $this->matching_questions[$groupname] = $match_group;
158 protected function process_matches(&$questions) {
159 if (empty($this->matching_questions)) {
163 foreach ($this->matching_questions as $match_group) {
164 $question = $this->defaultquestion();
165 $htmltext = s($match_group->questiontext);
166 $question->questiontext = $htmltext;
167 $question->questiontextformat = FORMAT_HTML;
168 $question->questiontextfiles = array();
169 $question->name = shorten_text( $question->questiontext, 250 );
170 $question->qtype = MATCH;
171 $question = $this->add_blank_combined_feedback($question);
172 $question->subquestions = array();
173 $question->subanswers = array();
174 foreach ($match_group->subchoices as $subchoice) {
175 $fiber = array_keys ($match_group->mappings, $subchoice);
177 foreach ($fiber as $subquestion) {
178 $question->subquestions[] = $this->text_field($subquestion);
179 $question->subanswers[] = $subchoice;
181 if ($subquestion == '') { // Then in this case, $subchoice is a distractor.
182 $question->subquestions[] = $this->text_field('');
183 $question->subanswers[] = $subchoice;
186 $questions[] = $question;
190 protected function cleanunicode($text) {
191 return str_replace('’', "'", $text);
194 public function readquestions($lines) {
195 // Parses an array of lines into an array of questions,
196 // where each item is a question object as defined by
199 $questions = array();
200 $currentquestion = array();
202 $text = implode($lines, ' ');
203 $text = $this->cleanunicode($text);
205 $xml = xmlize($text, 0);
206 if (!empty($xml['examview']['#']['matching-group'])) {
207 $this->parse_matching_groups($xml['examview']['#']['matching-group']);
210 $questionnode = $xml['examview']['#']['question'];
211 foreach ($questionnode as $currentquestion) {
212 if ($question = $this->readquestion($currentquestion)) {
213 $questions[] = $question;
217 $this->process_matches($questions);
221 public function readquestion($qrec) {
223 $type = trim($qrec['@']['type']);
224 $question = $this->defaultquestion();
225 if (array_key_exists($type, $this->qtypes)) {
226 $question->qtype = $this->qtypes[$type];
228 $question->qtype = null;
230 $question->single = 1;
232 // Only one answer is allowed.
233 $htmltext = $this->unxmlise($qrec['#']['text'][0]['#']);
235 $question->questiontext = $this->cleaninput($htmltext);
236 $question->questiontextformat = FORMAT_HTML;
237 $question->questiontextfiles = array();
238 $question->name = shorten_text( $question->questiontext, 250 );
240 switch ($question->qtype) {
242 $question = $this->parse_mc($qrec['#'], $question);
245 $groupname = trim($qrec['@']['group']);
246 $question = $this->parse_ma($qrec['#'], $groupname);
249 $question = $this->parse_tf_yn($qrec['#'], $question);
252 $question = $this->parse_co($qrec['#'], $question);
255 $question = $this->parse_es($qrec['#'], $question);
258 $question = $this->parse_nr($qrec['#'], $question);
262 print("<p>Question type ".$type." import not supported for ".$question->questiontext."<p>");
269 protected function parse_tf_yn($qrec, $question) {
270 $choices = array('T' => 1, 'Y' => 1, 'F' => 0, 'N' => 0 );
271 $answer = trim($qrec['answer'][0]['#']);
272 $question->answer = $choices[$answer];
273 $question->correctanswer = $question->answer;
274 if ($question->answer == 1) {
275 $question->feedbacktrue = $this->text_field(get_string('correct', 'question'));
276 $question->feedbackfalse = $this->text_field(get_string('incorrect', 'question'));
278 $question->feedbacktrue = $this->text_field(get_string('incorrect', 'question'));
279 $question->feedbackfalse = $this->text_field(get_string('correct', 'question'));
284 protected function parse_mc($qrec, $question) {
285 $question = $this->add_blank_combined_feedback($question);
286 $answer = 'choice-'.strtolower(trim($qrec['answer'][0]['#']));
288 $choices = $qrec['choices'][0]['#'];
289 foreach ($choices as $key => $value) {
290 if (strpos(trim($key), 'choice-') !== false) {
292 $question->answer[] = $this->text_field(s($this->unxmlise($value[0]['#'])));
293 if (strcmp($key, $answer) == 0) {
294 $question->fraction[] = 1;
295 $question->feedback[] = $this->text_field(get_string('correct', 'question'));
297 $question->fraction[] = 0;
298 $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
305 protected function parse_co($qrec, $question) {
306 $question->usecase = 0;
307 $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
308 $answer = strip_tags( $answer );
309 $answers = explode("\n", $answer);
311 foreach ($answers as $key => $value) {
312 $value = trim($value);
313 if (strlen($value) > 0) {
314 $question->answer[] = $value;
315 $question->fraction[] = 1;
316 $question->feedback[] = $this->text_field(get_string('correct', 'question'));
319 $question->answer[] = '*';
320 $question->fraction[] = 0;
321 $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
326 protected function parse_es($qrec, $question) {
327 $feedback = trim($this->unxmlise($qrec['answer'][0]['#']));
328 $question->graderinfo = $this->text_field($feedback);
329 $question->feedback = $feedback;
330 $question->responseformat = 'editor';
331 $question->responsefieldlines = 15;
332 $question->attachments = 0;
333 $question->fraction = 0;
337 protected function parse_nr($qrec, $question) {
338 $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
339 $answer = strip_tags( $answer );
340 $answers = explode("\n", $answer);
342 foreach ($answers as $key => $value) {
343 $value = trim($value);
344 if (is_numeric($value)) {
346 $question->answer[] = $value;
347 $question->fraction[] = 1;
348 $question->feedback[] = $this->text_field(get_string('correct', 'question'));
349 $question->tolerance[] = $errormargin;