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(
41 'mc' => 'multichoice',
43 'co' => 'shortanswer',
51 'sa' => 'shortanswer',
54 public $matching_questions = array();
56 public function provide_import() {
60 public function mime_type() {
61 return 'application/xml';
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
70 protected function unxmlise( $xml ) {
71 // If it's not an array then it's probably just data.
72 if (!is_array($xml)) {
75 // Otherwise parse the array.
77 foreach ($xml as $tag => $data) {
78 // If tag is '@' then it's attributes and we don't care.
80 $text = $text . $this->unxmlise( $data );
85 // Currently we throw the tags we found.
86 $text = strip_tags($text);
90 public function parse_matching_groups($matching_groups) {
91 if (empty($matching_groups)) {
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']['#']);
109 $this->matching_questions[$groupname] = $newgroup;
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;
123 protected function process_matches(&$questions) {
124 if (empty($this->matching_questions)) {
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);
142 foreach ($fiber as $subquestion) {
143 $question->subquestions[] = $this->text_field($subquestion);
144 $question->subanswers[] = $subchoice;
146 if ($subquestion == '') { // Then in this case, $subchoice is a distractor.
147 $question->subquestions[] = $this->text_field('');
148 $question->subanswers[] = $subchoice;
151 $questions[] = $question;
155 protected function cleanunicode($text) {
156 return str_replace('’', "'", $text);
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
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']);
175 $questionnode = $xml['examview']['#']['question'];
176 foreach ($questionnode as $currentquestion) {
177 if ($question = $this->readquestion($currentquestion)) {
178 $questions[] = $question;
182 $this->process_matches($questions);
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];
193 $question->qtype = null;
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) {
207 $question = $this->parse_mc($qrec['#'], $question);
210 $groupname = trim($qrec['@']['group']);
211 $question = $this->parse_ma($qrec['#'], $groupname);
214 $question = $this->parse_tf_yn($qrec['#'], $question);
217 $question = $this->parse_co($qrec['#'], $question);
220 $question = $this->parse_es($qrec['#'], $question);
223 $question = $this->parse_nr($qrec['#'], $question);
227 print("<p>Question type ".$type." import not supported for ".$question->questiontext."<p>");
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'));
243 $question->feedbacktrue = $this->text_field(get_string('incorrect', 'question'));
244 $question->feedbackfalse = $this->text_field(get_string('correct', 'question'));
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'));
262 $question->fraction[] = 0;
263 $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
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'));
284 $question->answer[] = '*';
285 $question->fraction[] = 0;
286 $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
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;
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)) {
311 $question->answer[] = $value;
312 $question->fraction[] = 1;
313 $question->feedback[] = $this->text_field(get_string('correct', 'question'));
314 $question->tolerance[] = $errormargin;