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 * Blackboard question importer.
20 * @package qformat_blackboard
21 * @copyright 2003 Scott Elliott
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 * Blackboard question importer.
34 * @copyright 2003 Scott Elliott
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class qformat_blackboard extends qformat_based_on_xml {
38 // Is the current question's question text escaped HTML (true for most if not all Blackboard files).
39 public $ishtml = true;
42 public function provide_import() {
46 public function mime_type() {
47 return mimeinfo('type', '.dat');
51 * Some softwares put entities in exported files.
52 * This method try to clean up known problems.
53 * @param string str string to correct
54 * @return string the corrected string
56 public function cleaninput($str) {
60 $html_code_list = array(
71 $str = strtr($str, $html_code_list);
72 // Use textlib entities_to_utf8 function to convert only numerical entities.
73 $str = textlib::entities_to_utf8($str, false);
78 * Parse the array of lines into an array of questions
79 * this *could* burn memory - but it won't happen that much
81 * @param array of lines from the input file.
82 * @param stdClass $context
83 * @return array (of objects) question objects.
85 protected function readquestions($lines) {
87 $text = implode($lines, ' ');
90 // This converts xml to big nasty data structure,
91 // the 0 means keep white space as it is.
93 $xml = xmlize($text, 0, 'UTF-8', true);
94 } catch (xml_format_exception $e) {
95 $this->error($e->getMessage(), '');
101 $this->process_tf($xml, $questions);
102 $this->process_mc($xml, $questions);
103 $this->process_ma($xml, $questions);
104 $this->process_fib($xml, $questions);
105 $this->process_matching($xml, $questions);
106 $this->process_essay($xml, $questions);
112 * Do question import processing common to every qtype.
113 * @param array $questiondata the xml tree related to the current question
114 * @return object initialized question object.
116 public function process_common($questiondata) {
119 // This routine initialises the question object.
120 $question = $this->defaultquestion();
122 // Determine if the question is already escaped html.
123 $this->ishtml = $this->getpath($questiondata,
124 array('#', 'BODY', 0, '#', 'FLAGS', 0, '#', 'ISHTML', 0, '@', 'value'),
127 // Put questiontext in question object.
128 $text = $this->getpath($questiondata,
129 array('#', 'BODY', 0, '#', 'TEXT', 0, '#'),
130 '', true, get_string('importnotext', 'qformat_blackboard'));
133 $question->questiontext = $this->cleaninput($text);
134 $question->questiontextformat = FORMAT_HTML;
135 $question->questiontextfiles = array();
138 $question->questiontext = $text;
140 // Put name in question object. We must ensure it is not empty and it is less than 250 chars.
141 $question->name = shorten_text(strip_tags($question->questiontext), 200);
142 $question->name = substr($question->name, 0, 250);
143 if (!$question->name) {
144 $id = $this->getpath($questiondata,
145 array('@', 'id'), '', true);
146 $question->name = get_string('defaultname', 'qformat_blackboard' , $id);
149 $question->generalfeedback = '';
150 $question->generalfeedbackformat = FORMAT_HTML;
151 $question->generalfeedbackfiles = array();
153 // TODO : read the mark from the POOL TITLE QUESTIONLIST section.
154 $question->defaultmark = 1;
159 * Process Essay Questions
160 * @param array xml the xml tree
161 * @param array questions the questions already parsed
163 public function process_essay($xml, &$questions) {
165 if ($this->getpath($xml, array('POOL', '#', 'QUESTION_ESSAY'), false, false)) {
166 $essayquestions = $this->getpath($xml,
167 array('POOL', '#', 'QUESTION_ESSAY'), false, false);
172 foreach ($essayquestions as $thisquestion) {
174 $question = $this->process_common($thisquestion);
176 $question->qtype = 'essay';
178 $question->answer = '';
179 $answer = $this->getpath($thisquestion,
180 array('#', 'ANSWER', 0, '#', 'TEXT', 0, '#'), '', true);
181 $question->graderinfo = $this->text_field($this->cleaninput($answer));
182 $question->feedback = '';
183 $question->responseformat = 'editor';
184 $question->responsefieldlines = 15;
185 $question->attachments = 0;
186 $question->fraction = 0;
188 $questions[] = $question;
193 * Process True / False Questions
194 * @param array xml the xml tree
195 * @param array questions the questions already parsed
197 public function process_tf($xml, &$questions) {
199 if ($this->getpath($xml, array('POOL', '#', 'QUESTION_TRUEFALSE'), false, false)) {
200 $tfquestions = $this->getpath($xml,
201 array('POOL', '#', 'QUESTION_TRUEFALSE'), false, false);
206 foreach ($tfquestions as $thisquestion) {
208 $question = $this->process_common($thisquestion);
210 $question->qtype = 'truefalse';
211 $question->single = 1; // Only one answer is allowed.
213 $choices = $this->getpath($thisquestion, array('#', 'ANSWER'), array(), false);
215 $correct_answer = $this->getpath($thisquestion,
216 array('#', 'GRADABLE', 0, '#', 'CORRECTANSWER', 0, '@', 'answer_id'),
219 // First choice is true, second is false.
220 $id = $this->getpath($choices[0], array('@', 'id'), '', true);
221 $correctfeedback = $this->getpath($thisquestion,
222 array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_CORRECT', 0, '#'),
224 $incorrectfeedback = $this->getpath($thisquestion,
225 array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_INCORRECT', 0, '#'),
227 if (strcmp($id, $correct_answer) == 0) { // True is correct.
228 $question->answer = 1;
229 $question->feedbacktrue = $this->text_field($this->cleaninput($correctfeedback));
230 $question->feedbackfalse = $this->text_field($this->cleaninput($incorrectfeedback));
231 } else { // False is correct.
232 $question->answer = 0;
233 $question->feedbacktrue = $this->text_field($this->cleaninput($incorrectfeedback));
234 $question->feedbackfalse = $this->text_field($this->cleaninput($correctfeedback));
236 $question->correctanswer = $question->answer;
237 $questions[] = $question;
242 * Process Multiple Choice Questions with single answer
243 * @param array xml the xml tree
244 * @param array questions the questions already parsed
246 public function process_mc($xml, &$questions) {
248 if ($this->getpath($xml, array('POOL', '#', 'QUESTION_MULTIPLECHOICE'), false, false)) {
249 $mcquestions = $this->getpath($xml,
250 array('POOL', '#', 'QUESTION_MULTIPLECHOICE'), false, false);
255 foreach ($mcquestions as $thisquestion) {
257 $question = $this->process_common($thisquestion);
259 $correctfeedback = $this->getpath($thisquestion,
260 array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_CORRECT', 0, '#'),
262 $incorrectfeedback = $this->getpath($thisquestion,
263 array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_INCORRECT', 0, '#'),
265 $question->correctfeedback = $this->text_field($this->cleaninput($correctfeedback));
266 $question->partiallycorrectfeedback = $this->text_field('');
267 $question->incorrectfeedback = $this->text_field($this->cleaninput($incorrectfeedback));
269 $question->qtype = 'multichoice';
270 $question->single = 1; // Only one answer is allowed.
272 $choices = $this->getpath($thisquestion, array('#', 'ANSWER'), false, false);
273 $correct_answer_id = $this->getpath($thisquestion,
274 array('#', 'GRADABLE', 0, '#', 'CORRECTANSWER', 0, '@', 'answer_id'),
276 foreach ($choices as $choice) {
277 $choicetext = $this->getpath($choice, array('#', 'TEXT', 0, '#'), '', true);
278 // Put this choice in the question object.
279 $question->answer[] = $this->text_field($this->cleaninput($choicetext));
281 $choice_id = $this->getpath($choice, array('@', 'id'), '', true);
282 // If choice is the right answer, give 100% mark, otherwise give 0%.
283 if (strcmp ($choice_id, $correct_answer_id) == 0) {
284 $question->fraction[] = 1;
286 $question->fraction[] = 0;
288 // There is never feedback specific to each choice.
289 $question->feedback[] = $this->text_field('');
291 $questions[] = $question;
296 * Process Multiple Choice Questions With Multiple Answers
297 * @param array xml the xml tree
298 * @param array questions the questions already parsed
300 public function process_ma($xml, &$questions) {
301 if ($this->getpath($xml, array('POOL', '#', 'QUESTION_MULTIPLEANSWER'), false, false)) {
302 $maquestions = $this->getpath($xml,
303 array('POOL', '#', 'QUESTION_MULTIPLEANSWER'), false, false);
308 foreach ($maquestions as $thisquestion) {
309 $question = $this->process_common($thisquestion);
311 $correctfeedback = $this->getpath($thisquestion,
312 array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_CORRECT', 0, '#'),
314 $incorrectfeedback = $this->getpath($thisquestion,
315 array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_INCORRECT', 0, '#'),
317 $question->correctfeedback = $this->text_field($this->cleaninput($correctfeedback));
318 // As there is no partially correct feedback we use incorrect one.
319 $question->partiallycorrectfeedback = $this->text_field($this->cleaninput($incorrectfeedback));
320 $question->incorrectfeedback = $this->text_field($this->cleaninput($incorrectfeedback));
322 $question->qtype = 'multichoice';
323 $question->defaultmark = 1;
324 $question->single = 0; // More than one answers allowed.
326 $choices = $this->getpath($thisquestion, array('#', 'ANSWER'), false, false);
327 $correct_answer_ids = array();
328 foreach ($this->getpath($thisquestion,
329 array('#', 'GRADABLE', 0, '#', 'CORRECTANSWER'), false, false) as $correctanswer) {
330 if ($correctanswer) {
331 $correct_answer_ids[] = $this->getpath($correctanswer,
332 array('@', 'answer_id'),
336 $fraction = 1/count($correct_answer_ids);
338 foreach ($choices as $choice) {
339 $choicetext = $this->getpath($choice, array('#', 'TEXT', 0, '#'), '', true);
340 // Put this choice in the question object.
341 $question->answer[] = $this->text_field($this->cleaninput($choicetext));
343 $choice_id = $this->getpath($choice, array('@', 'id'), '', true);
345 $iscorrect = in_array($choice_id, $correct_answer_ids);
348 $question->fraction[] = $fraction;
350 $question->fraction[] = 0;
352 // There is never feedback specific to each choice.
353 $question->feedback[] = $this->text_field('');
355 $questions[] = $question;
360 * Process Fill in the Blank Questions
361 * @param array xml the xml tree
362 * @param array questions the questions already parsed
364 public function process_fib($xml, &$questions) {
365 if ($this->getpath($xml, array('POOL', '#', 'QUESTION_FILLINBLANK'), false, false)) {
366 $fibquestions = $this->getpath($xml,
367 array('POOL', '#', 'QUESTION_FILLINBLANK'), false, false);
372 foreach ($fibquestions as $thisquestion) {
374 $question = $this->process_common($thisquestion);
376 $question->qtype = 'shortanswer';
377 $question->usecase = 0; // Ignore case.
379 $correctfeedback = $this->getpath($thisquestion,
380 array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_CORRECT', 0, '#'),
382 $incorrectfeedback = $this->getpath($thisquestion,
383 array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_INCORRECT', 0, '#'),
385 $answers = $this->getpath($thisquestion, array('#', 'ANSWER'), false, false);
386 foreach ($answers as $answer) {
387 $question->answer[] = $this->getpath($answer,
388 array('#', 'TEXT', 0, '#'), '', true);
389 $question->fraction[] = 1;
390 $question->feedback[] = $this->text_field($this->cleaninput($correctfeedback));
392 $question->answer[] = '*';
393 $question->fraction[] = 0;
394 $question->feedback[] = $this->text_field($this->cleaninput($incorrectfeedback));
396 $questions[] = $question;
401 * Process Matching Questions
402 * @param array xml the xml tree
403 * @param array questions the questions already parsed
405 public function process_matching($xml, &$questions) {
406 if ($this->getpath($xml, array('POOL', '#', 'QUESTION_MATCH'), false, false)) {
407 $matchquestions = $this->getpath($xml,
408 array('POOL', '#', 'QUESTION_MATCH'), false, false);
412 // Blackboard questions can't be imported in core Moodle without a loss in data,
413 // as core match question don't allow HTML in subanswers. The contributed ddmatch
414 // question type support HTML in subanswers.
415 // The ddmatch question type is not part of core, so we need to check if it is defined.
416 $ddmatch_is_installed = question_bank::is_qtype_installed('ddmatch');
418 foreach ($matchquestions as $thisquestion) {
420 $question = $this->process_common($thisquestion);
421 if ($ddmatch_is_installed) {
422 $question->qtype = 'ddmatch';
424 $question->qtype = 'match';
427 $correctfeedback = $this->getpath($thisquestion,
428 array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_CORRECT', 0, '#'),
430 $incorrectfeedback = $this->getpath($thisquestion,
431 array('#', 'GRADABLE', 0, '#', 'FEEDBACK_WHEN_INCORRECT', 0, '#'),
433 $question->correctfeedback = $this->text_field($this->cleaninput($correctfeedback));
434 // As there is no partially correct feedback we use incorrect one.
435 $question->partiallycorrectfeedback = $this->text_field($this->cleaninput($incorrectfeedback));
436 $question->incorrectfeedback = $this->text_field($this->cleaninput($incorrectfeedback));
438 $choices = $this->getpath($thisquestion,
439 array('#', 'CHOICE'), false, false); // Blackboard "choices" are Moodle subanswers.
440 $answers = $this->getpath($thisquestion,
441 array('#', 'ANSWER'), false, false); // Blackboard "answers" are Moodle subquestions.
442 $correctanswers = $this->getpath($thisquestion,
443 array('#', 'GRADABLE', 0, '#', 'CORRECTANSWER'), false, false); // Mapping between choices and answers.
445 foreach ($correctanswers as $correctanswer) {
446 if ($correctanswer) {
447 $correct_choice_id = $this->getpath($correctanswer,
448 array('@', 'choice_id'), '', true);
449 $correct_answer_id = $this->getpath($correctanswer,
450 array('@', 'answer_id'),
452 $mappings[$correct_answer_id] = $correct_choice_id;
456 foreach ($choices as $choice) {
457 if ($ddmatch_is_installed) {
458 $choicetext = $this->text_field($this->cleaninput($this->getpath($choice,
459 array('#', 'TEXT', 0, '#'), '', true)));
461 $choicetext = trim(strip_tags($this->getpath($choice,
462 array('#', 'TEXT', 0, '#'), '', true)));
465 if ($choicetext != '') { // Only import non empty subanswers.
467 $choice_id = $this->getpath($choice,
468 array('@', 'id'), '', true);
469 $fiber = array_search($choice_id, $mappings);
470 $fiber = array_keys ($mappings, $choice_id);
471 foreach ($fiber as $correct_answer_id) {
472 // We have found a correspondance for this choice so we need to take the associated answer.
473 foreach ($answers as $answer) {
474 $current_ans_id = $this->getpath($answer,
475 array('@', 'id'), '', true);
476 if (strcmp ($current_ans_id, $correct_answer_id) == 0) {
477 $subquestion = $this->getpath($answer,
478 array('#', 'TEXT', 0, '#'), '', true);
482 $question->subquestions[] = $this->text_field($this->cleaninput($subquestion));
483 $question->subanswers[] = $choicetext;
486 if ($subquestion == '') { // Then in this case, $choice is a distractor.
487 $question->subquestions[] = $this->text_field('');
488 $question->subanswers[] = $choicetext;
493 // Verify that this matching question has enough subquestions and subanswers.
494 $subquestioncount = 0;
496 $subanswers = $question->subanswers;
497 foreach ($question->subquestions as $key => $subquestion) {
498 $subquestion = $subquestion['text'];
499 $subanswer = $subanswers[$key];
500 if ($subquestion != '') {
505 if ($subquestioncount < 2 || $subanswercount < 3) {
506 $this->error(get_string('notenoughtsubans', 'qformat_blackboard', $question->questiontext));
508 $questions[] = $question;