MDL-34808 qformat examview Add phpunit tests to examview import format
[moodle.git] / question / format / examview / format.php
1 <?php
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/>.
17 /**
18  * Examview question importer.
19  *
20  * @package    qformat_examview
21  * @copyright  2005 Howard Miller
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir . '/xmlize.php');
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  */
37 class qformat_examview extends qformat_based_on_xml {
39     public $qtypes = array(
40         'tf' => TRUEFALSE,
41         'mc' => MULTICHOICE,
42         'yn' => TRUEFALSE,
43         'co' => SHORTANSWER,
44         'ma' => MATCH,
45         'mtf' => 99,
46         'nr' => NUMERICAL,
47         'pr' => 99,
48         'es' => ESSAY,
49         'ca' => 99,
50         'ot' => 99,
51         'sa' => SHORTANSWER,
52     );
54     public $matching_questions = array();
56     public function provide_import() {
57         return true;
58     }
60     public function mime_type() {
61         return 'application/xml';
62     }
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) {
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     }
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      */
92     protected function unxmlise( $xml ) {
93         // If it's not an array then it's probably just data.
94         if (!is_array($xml)) {
95             $text = s($xml);
96         } else {
97             // Otherwise parse the array.
98             $text = '';
99             foreach ($xml as $tag => $data) {
100                 // If tag is '@' then it's attributes and we don't care.
101                 if ($tag!=='@') {
102                     $text = $text . $this->unxmlise( $data );
103                 }
104             }
105         }
107         // Currently we throw the tags we found.
108         $text = strip_tags($text);
109         return $text;
110     }
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     }
125     public function parse_matching_groups($matching_groups) {
126         if (empty($matching_groups)) {
127             return;
128         }
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']['#']);
142                 }
143             }
144             $this->matching_questions[$groupname] = $newgroup;
145         }
146     }
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;
155         return null;
156     }
158     protected function process_matches(&$questions) {
159         if (empty($this->matching_questions)) {
160             return;
161         }
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);
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                 }
185             }
186             $questions[] = $question;
187         }
188     }
190     protected function cleanunicode($text) {
191         return str_replace('&#x2019;', "'", $text);
192     }
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
197         // readquestion().
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']);
208         }
210         $questionnode = $xml['examview']['#']['question'];
211         foreach ($questionnode as $currentquestion) {
212             if ($question = $this->readquestion($currentquestion)) {
213                 $questions[] = $question;
214             }
215         }
217         $this->process_matches($questions);
218         return $questions;
219     }
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];
227         } else {
228             $question->qtype = null;
229         }
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) {
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:
255                 $question = $this->parse_es($qrec['#'], $question);
256                 break;
257             case NUMERICAL:
258                 $question = $this->parse_nr($qrec['#'], $question);
259                 break;
260                 break;
261             default:
262                 print("<p>Question type ".$type." import not supported for ".$question->questiontext."<p>");
263                 $question = null;
264         }
266         return $question;
267     }
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'));
277         } else {
278             $question->feedbacktrue = $this->text_field(get_string('incorrect', 'question'));
279             $question->feedbackfalse = $this->text_field(get_string('correct', 'question'));
280         }
281         return $question;
282     }
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'));
296                 } else {
297                     $question->fraction[] = 0;
298                     $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
299                 }
300             }
301         }
302         return $question;
303     }
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'));
317             }
318         }
319         $question->answer[] = '*';
320         $question->fraction[] = 0;
321         $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
323         return $question;
324     }
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;
334         return $question;
335     }
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)) {
345                 $errormargin = 0;
346                 $question->answer[] = $value;
347                 $question->fraction[] = 1;
348                 $question->feedback[] = $this->text_field(get_string('correct', 'question'));
349                 $question->tolerance[] = $errormargin;
350             }
351         }
352         return $question;
353     }
356 // End class.