diff --git a/question/format/learnwise/format.php b/question/format/learnwise/format.php index ae80f135b13..b4c679abcaf 100644 --- a/question/format/learnwise/format.php +++ b/question/format/learnwise/format.php @@ -42,10 +42,14 @@ defined('MOODLE_INTERNAL') || die(); */ class qformat_learnwise extends qformat_default { - function provide_import() { + public function provide_import() { return true; } + public function export_file_extension() { + return '.xml'; + } + protected function readquestions($lines) { $questions = array(); $currentquestion = array(); @@ -62,9 +66,9 @@ class qformat_learnwise extends qformat_default { return $questions; } - function readquestion($lines) { + protected function readquestion($lines) { $text = implode(' ', $lines); - $text = str_replace(array('\t','\n','\r','\''), array('','','','\\\''), $text); + $text = str_replace(array('\t','\n','\r'), array('','',''), $text); $startpos = strpos($text, ''); @@ -75,8 +79,8 @@ class qformat_learnwise extends qformat_default { preg_match("//i", $text, $matches); $type = strtolower($matches[1]); // multichoice or multianswerchoice - $questiontext = $this->unhtmlentities($this->stringbetween($text, '', '')); - $questionhint = $this->unhtmlentities($this->stringbetween($text, '', '')); + $questiontext = textlib::entities_to_utf8($this->stringbetween($text, '', '')); + $questionhint = textlib::entities_to_utf8($this->stringbetween($text, '', '')); $questionaward = $this->stringbetween($text, '', ''); $optionlist = $this->stringbetween($text, '', ''); @@ -89,10 +93,13 @@ class qformat_learnwise extends qformat_default { if ($type == 'multichoice') { foreach ($optionlist as $option) { + if (trim($option) === '') { + continue; + } $correct = $this->stringbetween($option, ' correct="', '">'); $answer = $this->stringbetween($option, '">', ''); $optionscorrect[$n] = $correct; - $optionstext[$n] = $this->unhtmlentities($answer); + $optionstext[$n] = textlib::entities_to_utf8($answer); ++$n; } } else if ($type == 'multianswerchoice') { @@ -102,6 +109,9 @@ class qformat_learnwise extends qformat_default { $optionsaward = array(); foreach ($optionlist as $option) { + if (trim($option) === '') { + continue; + } preg_match("/correct=\"([^\"]*)\"/i", $option, $correctmatch); preg_match("/award=\"([^\"]*)\"/i", $option, $awardmatch); @@ -115,7 +125,7 @@ class qformat_learnwise extends qformat_default { $answer = $this->stringbetween($option, '">', ''); $optionscorrect[$n] = $correct; - $optionstext[$n] = $this->unhtmlentities($answer); + $optionstext[$n] = textlib::entities_to_utf8($answer); $optionsaward[$n] = $award; ++$n; } @@ -127,22 +137,25 @@ class qformat_learnwise extends qformat_default { $question = $this->defaultquestion(); $question->qtype = MULTICHOICE; $question->name = $this->create_default_question_name($questiontext, get_string('questionname', 'question')); + $this->add_blank_combined_feedback($question); $question->questiontext = $questiontext; + $question->questiontextformat = FORMAT_HTML; $question->single = ($type == 'multichoice') ? 1 : 0; - $question->feedback[] = ''; $question->fraction = array(); $question->answer = array(); for ($n = 0; $n < count($optionstext); ++$n) { if ($optionstext[$n]) { - if (!isset($numcorrect)) { // single answer + if (!isset($numcorrect)) { + // Single answer. if ($optionscorrect[$n] == 'yes') { $fraction = (int) $questionaward; } else { $fraction = 0; } - } else { // mulitple answers + } else { + // Multiple answers. if ($optionscorrect[$n] == 'yes') { $fraction = $optionsaward[$n] / $totalaward; } else { @@ -150,15 +163,22 @@ class qformat_learnwise extends qformat_default { } } $question->fraction[] = $fraction; - $question->answer[] = $optionstext[$n]; - $question->feedback[] = ''; // no feedback in this type + $question->answer[] = array('text' => $optionstext[$n], 'format' => FORMAT_HTML); + $question->feedback[] = array('text' => '', 'format' => FORMAT_HTML); // No feedback in this type. } } return $question; } - function stringbetween($text, $start, $end) { + /** + * Extract the substring of $text between $start and $end. + * @param string $text text to analyse. + * @param string $start opening delimiter. + * @param string $end closing delimiter. + * @return string the requested substring. + */ + protected function stringbetween($text, $start, $end) { $startpos = strpos($text, $start) + strlen($start); $endpos = strpos($text, $end); @@ -166,13 +186,4 @@ class qformat_learnwise extends qformat_default { return substr($text, $startpos, $endpos - $startpos); } } - - function unhtmlentities($string) { - $transtable = get_html_translation_table(HTML_ENTITIES); - $transtable = array_flip($transtable); - return strtr($string, $transtable); - } - } - - diff --git a/question/format/learnwise/lang/en/qformat_learnwise.php b/question/format/learnwise/lang/en/qformat_learnwise.php index f94cf1da6b5..9c2229e1170 100644 --- a/question/format/learnwise/lang/en/qformat_learnwise.php +++ b/question/format/learnwise/lang/en/qformat_learnwise.php @@ -24,4 +24,4 @@ */ $string['pluginname'] = 'Learnwise format'; -$string['plugidnname_help'] = 'This format enables the import of multiple choice questions saved in Learnwise\'s XML format.'; +$string['pluginname_help'] = 'This format enables the import of multiple choice questions saved in Learnwise\'s XML format.';