d13126619f60c420bba5d2285e119f904b52ff33
[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      * 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      */
70     protected function unxmlise( $xml ) {
71         // If it's not an array then it's probably just data.
72         if (!is_array($xml)) {
73             $text = s($xml);
74         } else {
75             // Otherwise parse the array.
76             $text = '';
77             foreach ($xml as $tag => $data) {
78                 // If tag is '@' then it's attributes and we don't care.
79                 if ($tag!=='@') {
80                     $text = $text . $this->unxmlise( $data );
81                 }
82             }
83         }
85         // Currently we throw the tags we found.
86         $text = strip_tags($text);
87         return $text;
88     }
90     public function parse_matching_groups($matching_groups) {
91         if (empty($matching_groups)) {
92             return;
93         }
94         foreach ($matching_groups as $match_group) {
95             $newgroup = new stdClass();
96             $groupname = trim($match_group['@']['name']);
97             $questiontext = $this->unxmlise($match_group['#']['text'][0]['#']);
98             $newgroup->questiontext = trim($questiontext);
99             $newgroup->subchoices = array();
100             $newgroup->subquestions = array();
101             $newgroup->subanswers = array();
102             $choices = $match_group['#']['choices']['0']['#'];
103             foreach ($choices as $key => $value) {
104                 if (strpos(trim($key), 'choice-') !== false) {
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     }
113     protected function parse_ma($qrec, $groupname) {
114         $match_group = $this->matching_questions[$groupname];
115         $phrase = trim($this->unxmlise($qrec['text']['0']['#']));
116         $answer = trim($this->unxmlise($qrec['answer']['0']['#']));
117         $answer = strip_tags( $answer );
118         $match_group->mappings[$phrase] = $match_group->subchoices[$answer];
119         $this->matching_questions[$groupname] = $match_group;
120         return null;
121     }
123     protected function process_matches(&$questions) {
124         if (empty($this->matching_questions)) {
125             return;
126         }
128         foreach ($this->matching_questions as $match_group) {
129             $question = $this->defaultquestion();
130             $htmltext = s($match_group->questiontext);
131             $question->questiontext = $htmltext;
132             $question->questiontextformat = FORMAT_HTML;
133             $question->questiontextfiles = array();
134             $question->name = shorten_text( $question->questiontext, 250 );
135             $question->qtype = 'match';
136             $question = $this->add_blank_combined_feedback($question);
137             $question->subquestions = array();
138             $question->subanswers = array();
139             foreach ($match_group->subchoices as $subchoice) {
140                 $fiber = array_keys ($match_group->mappings, $subchoice);
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                 }
150             }
151             $questions[] = $question;
152         }
153     }
155     protected function cleanunicode($text) {
156         return str_replace('&#x2019;', "'", $text);
157     }
159     public function readquestions($lines) {
160         // Parses an array of lines into an array of questions,
161         // where each item is a question object as defined by
162         // readquestion().
164         $questions = array();
165         $currentquestion = array();
167         $text = implode($lines, ' ');
168         $text = $this->cleanunicode($text);
170         $xml = xmlize($text, 0);
171         if (!empty($xml['examview']['#']['matching-group'])) {
172             $this->parse_matching_groups($xml['examview']['#']['matching-group']);
173         }
175         $questionnode = $xml['examview']['#']['question'];
176         foreach ($questionnode as $currentquestion) {
177             if ($question = $this->readquestion($currentquestion)) {
178                 $questions[] = $question;
179             }
180         }
182         $this->process_matches($questions);
183         return $questions;
184     }
186     public function readquestion($qrec) {
188         $type = trim($qrec['@']['type']);
189         $question = $this->defaultquestion();
190         if (array_key_exists($type, $this->qtypes)) {
191             $question->qtype = $this->qtypes[$type];
192         } else {
193             $question->qtype = null;
194         }
195         $question->single = 1;
197         // Only one answer is allowed.
198         $htmltext = $this->unxmlise($qrec['#']['text'][0]['#']);
200         $question->questiontext = $this->cleaninput($htmltext);
201         $question->questiontextformat = FORMAT_HTML;
202         $question->questiontextfiles = array();
203         $question->name = shorten_text( $question->questiontext, 250 );
205         switch ($question->qtype) {
206             case 'multichoice':
207                 $question = $this->parse_mc($qrec['#'], $question);
208                 break;
209             case 'match':
210                 $groupname = trim($qrec['@']['group']);
211                 $question = $this->parse_ma($qrec['#'], $groupname);
212                 break;
213             case 'truefalse':
214                 $question = $this->parse_tf_yn($qrec['#'], $question);
215                 break;
216             case 'shortanswer':
217                 $question = $this->parse_co($qrec['#'], $question);
218                 break;
219             case 'essay':
220                 $question = $this->parse_es($qrec['#'], $question);
221                 break;
222             case 'numerical':
223                 $question = $this->parse_nr($qrec['#'], $question);
224                 break;
225                 break;
226             default:
227                 print("<p>Question type ".$type." import not supported for ".$question->questiontext."<p>");
228                 $question = null;
229         }
231         return $question;
232     }
234     protected function parse_tf_yn($qrec, $question) {
235         $choices = array('T' => 1, 'Y' => 1, 'F' => 0, 'N' => 0 );
236         $answer = trim($qrec['answer'][0]['#']);
237         $question->answer = $choices[$answer];
238         $question->correctanswer = $question->answer;
239         if ($question->answer == 1) {
240             $question->feedbacktrue = $this->text_field(get_string('correct', 'question'));
241             $question->feedbackfalse = $this->text_field(get_string('incorrect', 'question'));
242         } else {
243             $question->feedbacktrue = $this->text_field(get_string('incorrect', 'question'));
244             $question->feedbackfalse = $this->text_field(get_string('correct', 'question'));
245         }
246         return $question;
247     }
249     protected function parse_mc($qrec, $question) {
250         $question = $this->add_blank_combined_feedback($question);
251         $answer = 'choice-'.strtolower(trim($qrec['answer'][0]['#']));
253         $choices = $qrec['choices'][0]['#'];
254         foreach ($choices as $key => $value) {
255             if (strpos(trim($key), 'choice-') !== false) {
257                 $question->answer[] = $this->text_field(s($this->unxmlise($value[0]['#'])));
258                 if (strcmp($key, $answer) == 0) {
259                     $question->fraction[] = 1;
260                     $question->feedback[] = $this->text_field(get_string('correct', 'question'));
261                 } else {
262                     $question->fraction[] = 0;
263                     $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
264                 }
265             }
266         }
267         return $question;
268     }
270     protected function parse_co($qrec, $question) {
271         $question->usecase = 0;
272         $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
273         $answer = strip_tags( $answer );
274         $answers = explode("\n", $answer);
276         foreach ($answers as $key => $value) {
277             $value = trim($value);
278             if (strlen($value) > 0) {
279                 $question->answer[] = $value;
280                 $question->fraction[] = 1;
281                 $question->feedback[] = $this->text_field(get_string('correct', 'question'));
282             }
283         }
284         $question->answer[] = '*';
285         $question->fraction[] = 0;
286         $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
288         return $question;
289     }
291     protected function parse_es($qrec, $question) {
292         $feedback = trim($this->unxmlise($qrec['answer'][0]['#']));
293         $question->graderinfo =  $this->text_field($feedback);
294         $question->feedback = $feedback;
295         $question->responseformat = 'editor';
296         $question->responsefieldlines = 15;
297         $question->attachments = 0;
298         $question->fraction = 0;
299         return $question;
300     }
302     protected function parse_nr($qrec, $question) {
303         $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
304         $answer = strip_tags( $answer );
305         $answers = explode("\n", $answer);
307         foreach ($answers as $key => $value) {
308             $value = trim($value);
309             if (is_numeric($value)) {
310                 $errormargin = 0;
311                 $question->answer[] = $value;
312                 $question->fraction[] = 1;
313                 $question->feedback[] = $this->text_field(get_string('correct', 'question'));
314                 $question->tolerance[] = $errormargin;
315             }
316         }
317         return $question;
318     }
321 // End class.