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 * GIFT format question importer/exporter.
22 * @copyright 2003 Paul Tsuchido Shew
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
31 * The GIFT import filter was designed as an easy to use method
32 * for teachers writing questions as a text file. It supports most
33 * question types and the missing word format.
35 * Multiple Choice / Missing Word
36 * Who's buried in Grant's tomb?{~Grant ~Jefferson =no one}
37 * Grant is {~buried =entombed ~living} in Grant's tomb.
39 * Grant is buried in Grant's tomb.{FALSE}
41 * Who's buried in Grant's tomb?{=no one =nobody}
43 * When was Ulysses S. Grant born?{#1822:5}
45 * Match the following countries with their corresponding
46 * capitals.{=Canada->Ottawa =Italy->Rome =Japan->Tokyo}
48 * Comment lines start with a double backslash (//).
49 * Optional question names are enclosed in double colon(::).
50 * Answer feedback is indicated with hash mark (#).
51 * Percentage answer weights immediately follow the tilde (for
52 * multiple choice) or equal sign (for short answer and numerical),
53 * and are enclosed in percent signs (% %). See docs and examples.txt for more.
55 * This filter was written through the collaboration of numerous
56 * members of the Moodle community. It was originally based on
57 * the missingword format, which included code from Thomas Robb
58 * and others. Paul Tsuchido Shew wrote this filter in December 2003.
60 * @copyright 2003 Paul Tsuchido Shew
61 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
63 class qformat_gift extends qformat_default {
65 public function provide_import() {
69 public function provide_export() {
73 public function export_file_extension() {
77 protected function answerweightparser(&$answer) {
78 $answer = substr($answer, 1); // removes initial %
79 $end_position = strpos($answer, "%");
80 $answer_weight = substr($answer, 0, $end_position); // gets weight as integer
81 $answer_weight = $answer_weight/100; // converts to percent
82 $answer = substr($answer, $end_position+1); // removes comment from answer
83 return $answer_weight;
86 protected function commentparser($answer, $defaultformat) {
87 $bits = explode('#', $answer, 2);
88 $ans = $this->parse_text_with_format(trim($bits[0]), $defaultformat);
89 if (count($bits) > 1) {
90 $feedback = $this->parse_text_with_format(trim($bits[1]), $defaultformat);
92 $feedback = array('text' => '', 'format' => $defaultformat, 'files' => array());
94 return array($ans, $feedback);
97 protected function split_truefalse_comment($answer, $defaultformat) {
98 $bits = explode('#', $answer, 3);
99 $ans = $this->parse_text_with_format(trim($bits[0]), $defaultformat);
100 if (count($bits) > 1) {
101 $wrongfeedback = $this->parse_text_with_format(trim($bits[1]), $defaultformat);
103 $wrongfeedback = array('text' => '', 'format' => $defaultformat, 'files' => array());
105 if (count($bits) > 2) {
106 $rightfeedback = $this->parse_text_with_format(trim($bits[2]), $defaultformat);
108 $rightfeedback = array('text' => '', 'format' => $defaultformat, 'files' => array());
110 return array($ans, $wrongfeedback, $rightfeedback);
113 protected function escapedchar_pre($string) {
114 //Replaces escaped control characters with a placeholder BEFORE processing
116 $escapedcharacters = array("\\:", "\\#", "\\=", "\\{", "\\}", "\\~", "\\n" ); //dlnsk
117 $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010"); //dlnsk
119 $string = str_replace("\\\\", "&&092;", $string);
120 $string = str_replace($escapedcharacters, $placeholders, $string);
121 $string = str_replace("&&092;", "\\", $string);
125 protected function escapedchar_post($string) {
126 //Replaces placeholders with corresponding character AFTER processing is done
127 $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010"); //dlnsk
128 $characters = array(":", "#", "=", "{", "}", "~", "\n" ); //dlnsk
129 $string = str_replace($placeholders, $characters, $string);
133 protected function check_answer_count($min, $answers, $text) {
134 $countanswers = count($answers);
135 if ($countanswers < $min) {
136 $this->error(get_string('importminerror', 'qformat_gift'), $text);
143 protected function parse_text_with_format($text, $defaultformat = FORMAT_MOODLE) {
146 'format' => $defaultformat,
149 if (strpos($text, '[') === 0) {
150 $formatend = strpos($text, ']');
151 $result['format'] = $this->format_name_to_const(substr($text, 1, $formatend - 1));
152 if ($result['format'] == -1) {
153 $result['format'] = $defaultformat;
155 $result['text'] = substr($text, $formatend + 1);
158 $result['text'] = trim($this->escapedchar_post($result['text']));
162 public function readquestion($lines) {
163 // Given an array of lines known to define a question in this format, this function
164 // converts it into a question object suitable for processing and insertion into Moodle.
166 $question = $this->defaultquestion();
168 // define replaced by simple assignment, stop redefine notices
169 $gift_answerweight_regex = '/^%\-*([0-9]{1,2})\.?([0-9]*)%/';
171 // REMOVED COMMENTED LINES and IMPLODE
172 foreach ($lines as $key => $line) {
174 if (substr($line, 0, 2) == '//') {
179 $text = trim(implode(' ', $lines));
185 // Substitute escaped control characters with placeholders
186 $text = $this->escapedchar_pre($text);
188 // Look for category modifier
189 if (preg_match('~^\$CATEGORY:~', $text)) {
190 // $newcategory = $matches[1];
191 $newcategory = trim(substr($text, 10));
193 // build fake question to contain category
194 $question->qtype = 'category';
195 $question->category = $newcategory;
199 // QUESTION NAME parser
200 if (substr($text, 0, 2) == '::') {
201 $text = substr($text, 2);
203 $namefinish = strpos($text, '::');
204 if ($namefinish === false) {
205 $question->name = false;
206 // name will be assigned after processing question text below
208 $questionname = substr($text, 0, $namefinish);
209 $question->name = trim($this->escapedchar_post($questionname));
210 $text = trim(substr($text, $namefinish+2)); // Remove name from text
213 $question->name = false;
216 // Find the answer section.
217 $answerstart = strpos($text, '{');
218 $answerfinish = strpos($text, '}');
220 $description = false;
221 if ($answerstart === false && $answerfinish === false) {
222 // No answer means it's a description.
227 } else if ($answerstart === false || $answerfinish === false) {
228 $this->error(get_string('braceerror', 'qformat_gift'), $text);
232 $answerlength = $answerfinish - $answerstart;
233 $answertext = trim(substr($text, $answerstart + 1, $answerlength - 1));
236 // Format the question text, without answer, inserting "_____" as necessary.
238 $questiontext = $text;
239 } else if (substr($text, -1) == "}") {
240 // No blank line if answers follow question, outside of closing punctuation.
241 $questiontext = substr_replace($text, "", $answerstart, $answerlength + 1);
243 // Inserts blank line for missing word format.
244 $questiontext = substr_replace($text, "_____", $answerstart, $answerlength + 1);
247 // Look to see if there is any general feedback.
248 $gfseparator = strrpos($answertext, '####');
249 if ($gfseparator === false) {
250 $generalfeedback = '';
252 $generalfeedback = substr($answertext, $gfseparator + 4);
253 $answertext = trim(substr($answertext, 0, $gfseparator));
256 // Get questiontext format from questiontext.
257 $text = $this->parse_text_with_format($questiontext);
258 $question->questiontextformat = $text['format'];
259 $question->questiontext = $text['text'];
261 // Get generalfeedback format from questiontext.
262 $text = $this->parse_text_with_format($generalfeedback, $question->questiontextformat);
263 $question->generalfeedback = $text['text'];
264 $question->generalfeedbackformat = $text['format'];
266 // set question name if not already set
267 if ($question->name === false) {
268 $question->name = $question->questiontext;
271 // ensure name is not longer than 250 characters
272 $question->name = shorten_text($question->name, 200);
273 $question->name = strip_tags(substr($question->name, 0, 250));
275 // determine QUESTION TYPE
276 $question->qtype = NULL;
278 // give plugins first try
279 // plugins must promise not to intercept standard qtypes
280 // MDL-12346, this could be called from lesson mod which has its own base class =(
281 if (method_exists($this, 'try_importing_using_qtypes') && ($try_question = $this->try_importing_using_qtypes($lines, $question, $answertext))) {
282 return $try_question;
286 $question->qtype = DESCRIPTION;
288 } else if ($answertext == '') {
289 $question->qtype = ESSAY;
291 } else if ($answertext{0} == '#') {
292 $question->qtype = NUMERICAL;
294 } else if (strpos($answertext, '~') !== false) {
295 // only Multiplechoice questions contain tilde ~
296 $question->qtype = MULTICHOICE;
298 } else if (strpos($answertext, '=') !== false
299 && strpos($answertext, '->') !== false) {
300 // only Matching contains both = and ->
301 $question->qtype = MATCH;
303 } else { // either TRUEFALSE or SHORTANSWER
305 // TRUEFALSE question check
306 $truefalse_check = $answertext;
307 if (strpos($answertext, '#') > 0) {
308 // strip comments to check for TrueFalse question
309 $truefalse_check = trim(substr($answertext, 0, strpos($answertext,"#")));
312 $valid_tf_answers = array('T', 'TRUE', 'F', 'FALSE');
313 if (in_array($truefalse_check, $valid_tf_answers)) {
314 $question->qtype = TRUEFALSE;
316 } else { // Must be SHORTANSWER
317 $question->qtype = SHORTANSWER;
321 if (!isset($question->qtype)) {
322 $giftqtypenotset = get_string('giftqtypenotset', 'qformat_gift');
323 $this->error($giftqtypenotset, $text);
327 switch ($question->qtype) {
329 $question->defaultmark = 0;
330 $question->length = 0;
334 $question->responseformat = 'editor';
335 $question->responsefieldlines = 15;
336 $question->attachments = 0;
337 $question->graderinfo = array(
338 'text' => '', 'format' => FORMAT_HTML, 'files' => array());
342 if (strpos($answertext,"=") === false) {
343 $question->single = 0; // multiple answers are enabled if no single answer is 100% correct
345 $question->single = 1; // only one answer allowed (the default)
347 $question = $this->add_blank_combined_feedback($question);
349 $answertext = str_replace("=", "~=", $answertext);
350 $answers = explode("~", $answertext);
351 if (isset($answers[0])) {
352 $answers[0] = trim($answers[0]);
354 if (empty($answers[0])) {
355 array_shift($answers);
358 $countanswers = count($answers);
360 if (!$this->check_answer_count(2, $answers, $text)) {
364 foreach ($answers as $key => $answer) {
365 $answer = trim($answer);
367 // determine answer weight
368 if ($answer[0] == '=') {
370 $answer = substr($answer, 1);
372 } else if (preg_match($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
373 $answer_weight = $this->answerweightparser($answer);
375 } else { //default, i.e., wrong anwer
378 list($question->answer[$key], $question->feedback[$key]) =
379 $this->commentparser($answer, $question->questiontextformat);
380 $question->fraction[$key] = $answer_weight;
381 } // end foreach answer
386 $question = $this->add_blank_combined_feedback($question);
388 $answers = explode('=', $answertext);
389 if (isset($answers[0])) {
390 $answers[0] = trim($answers[0]);
392 if (empty($answers[0])) {
393 array_shift($answers);
396 if (!$this->check_answer_count(2,$answers,$text)) {
400 foreach ($answers as $key => $answer) {
401 $answer = trim($answer);
402 if (strpos($answer, "->") === false) {
403 $this->error(get_string('giftmatchingformat','qformat_gift'), $answer);
407 $marker = strpos($answer, '->');
408 $question->subquestions[$key] = $this->parse_text_with_format(
409 substr($answer, 0, $marker), $question->questiontextformat);
410 $question->subanswers[$key] = trim($this->escapedchar_post(
411 substr($answer, $marker + 2)));
417 list($answer, $wrongfeedback, $rightfeedback) =
418 $this->split_truefalse_comment($answertext, $question->questiontextformat);
420 if ($answer['text'] == "T" OR $answer['text'] == "TRUE") {
421 $question->correctanswer = 1;
422 $question->feedbacktrue = $rightfeedback;
423 $question->feedbackfalse = $wrongfeedback;
425 $question->correctanswer = 0;
426 $question->feedbacktrue = $wrongfeedback;
427 $question->feedbackfalse = $rightfeedback;
430 $question->penalty = 1;
435 // SHORTANSWER Question
436 $answers = explode("=", $answertext);
437 if (isset($answers[0])) {
438 $answers[0] = trim($answers[0]);
440 if (empty($answers[0])) {
441 array_shift($answers);
444 if (!$this->check_answer_count(1, $answers, $text)) {
448 foreach ($answers as $key => $answer) {
449 $answer = trim($answer);
452 if (preg_match($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
453 $answer_weight = $this->answerweightparser($answer);
454 } else { //default, i.e., full-credit anwer
458 list($answer, $question->feedback[$key]) = $this->commentparser(
459 $answer, $question->questiontextformat);
461 $question->answer[$key] = $answer['text'];
462 $question->fraction[$key] = $answer_weight;
468 // Note similarities to ShortAnswer
469 $answertext = substr($answertext, 1); // remove leading "#"
471 // If there is feedback for a wrong answer, store it for now.
472 if (($pos = strpos($answertext, '~')) !== false) {
473 $wrongfeedback = substr($answertext, $pos);
474 $answertext = substr($answertext, 0, $pos);
479 $answers = explode("=", $answertext);
480 if (isset($answers[0])) {
481 $answers[0] = trim($answers[0]);
483 if (empty($answers[0])) {
484 array_shift($answers);
487 if (count($answers) == 0) {
489 $giftnonumericalanswers = get_string('giftnonumericalanswers','qformat_gift');
490 $this->error($giftnonumericalanswers, $text);
494 foreach ($answers as $key => $answer) {
495 $answer = trim($answer);
498 if (preg_match($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
499 $answer_weight = $this->answerweightparser($answer);
500 } else { //default, i.e., full-credit anwer
504 list($answer, $question->feedback[$key]) = $this->commentparser(
505 $answer, $question->questiontextformat);
506 $question->fraction[$key] = $answer_weight;
507 $answer = $answer['text'];
509 //Calculate Answer and Min/Max values
510 if (strpos($answer,"..") > 0) { // optional [min]..[max] format
511 $marker = strpos($answer,"..");
512 $max = trim(substr($answer, $marker+2));
513 $min = trim(substr($answer, 0, $marker));
514 $ans = ($max + $min)/2;
516 } else if (strpos($answer, ':') > 0) { // standard [answer]:[errormargin] format
517 $marker = strpos($answer, ':');
518 $tol = trim(substr($answer, $marker+1));
519 $ans = trim(substr($answer, 0, $marker));
520 } else { // only one valid answer (zero errormargin)
522 $ans = trim($answer);
525 if (!(is_numeric($ans) || $ans = '*') || !is_numeric($tol)) {
526 $errornotnumbers = get_string('errornotnumbers');
527 $this->error($errornotnumbers, $text);
532 $question->answer[$key] = $ans;
533 $question->tolerance[$key] = $tol;
536 if ($wrongfeedback) {
538 $question->fraction[$key] = 0;
539 list($notused, $question->feedback[$key]) = $this->commentparser(
540 $wrongfeedback, $question->questiontextformat);
541 $question->answer[$key] = '*';
542 $question->tolerance[$key] = '';
548 $this->error(get_string('giftnovalidquestion', 'qformat_gift'), $text);
554 protected function add_blank_combined_feedback($question) {
555 $question->correctfeedback['text'] = '';
556 $question->correctfeedback['format'] = $question->questiontextformat;
557 $question->correctfeedback['files'] = array();
558 $question->partiallycorrectfeedback['text'] = '';
559 $question->partiallycorrectfeedback['format'] = $question->questiontextformat;
560 $question->partiallycorrectfeedback['files'] = array();
561 $question->incorrectfeedback['text'] = '';
562 $question->incorrectfeedback['format'] = $question->questiontextformat;
563 $question->incorrectfeedback['files'] = array();
567 protected function repchar($text, $notused = 0) {
568 // Escapes 'reserved' characters # = ~ {) :
570 $reserved = array( '\\', '#', '=', '~', '{', '}', ':', "\n", "\r");
571 $escaped = array('\\\\', '\#','\=','\~','\{','\}','\:', '\n', '' );
573 $newtext = str_replace($reserved, $escaped, $text);
578 * @param int $format one of the FORMAT_ constants.
579 * @return string the corresponding name.
581 protected function format_const_to_name($format) {
582 if ($format == FORMAT_MOODLE) {
584 } else if ($format == FORMAT_HTML) {
586 } else if ($format == FORMAT_PLAIN) {
588 } else if ($format == FORMAT_MARKDOWN) {
596 * @param int $format one of the FORMAT_ constants.
597 * @return string the corresponding name.
599 protected function format_name_to_const($format) {
600 if ($format == 'moodle') {
601 return FORMAT_MOODLE;
602 } else if ($format == 'html') {
604 } else if ($format == 'plain') {
606 } else if ($format == 'markdown') {
607 return FORMAT_MARKDOWN;
613 public function write_name($name) {
614 return '::' . $this->repchar($name) . '::';
617 public function write_questiontext($text, $format, $defaultformat = FORMAT_MOODLE) {
619 if ($text != '' && $format != $defaultformat) {
620 $output .= '[' . $this->format_const_to_name($format) . ']';
622 $output .= $this->repchar($text, $format);
627 * Outputs the general feedback for the question, if any. This needs to be the
628 * last thing before the }.
629 * @param object $question the question data.
630 * @param string $indent to put before the general feedback. Defaults to a tab.
631 * If this is not blank, a newline is added after the line.
633 public function write_general_feedback($question, $indent = "\t") {
634 $generalfeedback = $this->write_questiontext($question->generalfeedback,
635 $question->generalfeedbackformat, $question->questiontextformat);
637 if ($generalfeedback) {
638 $generalfeedback = '####' . $generalfeedback;
640 $generalfeedback = $indent . $generalfeedback . "\n";
644 return $generalfeedback;
647 public function writequestion($question) {
650 // Start with a comment
651 $expout = "// question: $question->id name: $question->name\n";
653 // output depends on question type
654 switch($question->qtype) {
657 // not a real question, used to insert category switch
658 $expout .= "\$CATEGORY: $question->category\n";
662 $expout .= $this->write_name($question->name);
663 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
667 $expout .= $this->write_name($question->name);
668 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
670 $expout .= $this->write_general_feedback($question, '');
675 $trueanswer = $question->options->answers[$question->options->trueanswer];
676 $falseanswer = $question->options->answers[$question->options->falseanswer];
677 if ($trueanswer->fraction == 1) {
678 $answertext = 'TRUE';
679 $rightfeedback = $this->write_questiontext($trueanswer->feedback,
680 $trueanswer->feedbackformat, $question->questiontextformat);
681 $wrongfeedback = $this->write_questiontext($falseanswer->feedback,
682 $falseanswer->feedbackformat, $question->questiontextformat);
684 $answertext = 'FALSE';
685 $rightfeedback = $this->write_questiontext($falseanswer->feedback,
686 $falseanswer->feedbackformat, $question->questiontextformat);
687 $wrongfeedback = $this->write_questiontext($trueanswer->feedback,
688 $trueanswer->feedbackformat, $question->questiontextformat);
691 $expout .= $this->write_name($question->name);
692 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
693 $expout .= '{' . $this->repchar($answertext);
694 if ($wrongfeedback) {
695 $expout .= '#' . $wrongfeedback;
696 } else if ($rightfeedback) {
699 if ($rightfeedback) {
700 $expout .= '#' . $rightfeedback;
702 $expout .= $this->write_general_feedback($question, '');
707 $expout .= $this->write_name($question->name);
708 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
710 foreach($question->options->answers as $answer) {
711 if ($answer->fraction == 1) {
713 } else if ($answer->fraction == 0) {
716 $weight = $answer->fraction * 100;
717 $answertext = '~%' . $weight . '%';
719 $expout .= "\t" . $answertext . $this->write_questiontext($answer->answer,
720 $answer->answerformat, $question->questiontextformat);
721 if ($answer->feedback != '') {
722 $expout .= '#' . $this->write_questiontext($answer->feedback,
723 $answer->feedbackformat, $question->questiontextformat);
727 $expout .= $this->write_general_feedback($question);
732 $expout .= $this->write_name($question->name);
733 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
735 foreach($question->options->answers as $answer) {
736 $weight = 100 * $answer->fraction;
737 $expout .= "\t=%" . $weight . '%' . $this->repchar($answer->answer) .
738 '#' . $this->write_questiontext($answer->feedback,
739 $answer->feedbackformat, $question->questiontextformat) . "\n";
741 $expout .= $this->write_general_feedback($question);
746 $expout .= $this->write_name($question->name);
747 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
749 foreach ($question->options->answers as $answer) {
750 if ($answer->answer != '' && $answer->answer != '*') {
751 $weight = 100 * $answer->fraction;
752 $expout .= "\t=%" . $weight . '%' . $answer->answer . ':' .
753 (float)$answer->tolerance . '#' . $this->write_questiontext($answer->feedback,
754 $answer->feedbackformat, $question->questiontextformat) . "\n";
756 $expout .= "\t~#" . $this->write_questiontext($answer->feedback,
757 $answer->feedbackformat, $question->questiontextformat) . "\n";
760 $expout .= $this->write_general_feedback($question);
765 $expout .= $this->write_name($question->name);
766 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
768 foreach($question->options->subquestions as $subquestion) {
769 $expout .= "\t=" . $this->write_questiontext($subquestion->questiontext,
770 $subquestion->questiontextformat, $question->questiontextformat) .
771 ' -> ' . $this->repchar($subquestion->answertext) . "\n";
773 $expout .= $this->write_general_feedback($question);
779 if ($out = $this->try_exporting_using_qtypes($question->qtype, $question)) {
782 $expout .= "Question type $question->qtype is not supported\n";
783 echo $OUTPUT->notification(get_string('nohandler', 'qformat_gift',
784 question_bank::get_qtype_name($question->qtype)));
788 // Add empty line to delimit questions