mirror of
https://github.com/moodle/moodle.git
synced 2025-08-06 01:16:44 +02:00
added unicode support (all Hot Potatoes quiz types)
This commit is contained in:
parent
4e302ae4c6
commit
b18fd5ca39
1 changed files with 281 additions and 119 deletions
|
@ -9,8 +9,6 @@
|
||||||
|
|
||||||
// Based on default.php, included by ../import.php
|
// Based on default.php, included by ../import.php
|
||||||
|
|
||||||
require_once ("$CFG->libdir/xmlize.php");
|
|
||||||
|
|
||||||
class quiz_format_hotpot extends quiz_default_format {
|
class quiz_format_hotpot extends quiz_default_format {
|
||||||
|
|
||||||
function provide_import() {
|
function provide_import() {
|
||||||
|
@ -24,41 +22,43 @@ class quiz_format_hotpot extends quiz_default_format {
|
||||||
|
|
||||||
// parse the xml source
|
// parse the xml source
|
||||||
$source = implode($lines, " ");
|
$source = implode($lines, " ");
|
||||||
$xml = xmlize($source);
|
$xml = new hotpot_xml_tree($source);
|
||||||
|
|
||||||
// determine the quiz type
|
// determine the quiz type
|
||||||
$quiztype = '';
|
$xml->quiztype = '';
|
||||||
$keys = array_keys($xml);
|
$keys = array_keys($xml->xml);
|
||||||
foreach ($keys as $key) {
|
foreach ($keys as $key) {
|
||||||
if (preg_match('/(hotpot|textoys)-(\w+)-file/i', $key, $matches)) {
|
if (preg_match('/^(hotpot|textoys)-(\w+)-file$/i', $key, $matches)) {
|
||||||
$quiztype = strtolower($matches[2]);
|
$xml->quiztype = strtolower($matches[2]);
|
||||||
$xml = $xml[$matches[0]];
|
$xml->xml_root = "['$key']['#']";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (empty($xml->quiztype)) {
|
||||||
$questions = array();
|
notice("Input file not recognized as a Hot Potatoes XML file");
|
||||||
switch ($quiztype) {
|
} else {
|
||||||
case 'jcloze':
|
$questions = array();
|
||||||
process_jcloze($xml, $questions);
|
switch ($xml->quiztype) {
|
||||||
break;
|
case 'jcloze':
|
||||||
case 'jcross':
|
process_jcloze($xml, $questions);
|
||||||
process_jcross($xml, $questions);
|
break;
|
||||||
break;
|
case 'jcross':
|
||||||
case 'jmatch':
|
process_jcross($xml, $questions);
|
||||||
process_jmatch($xml, $questions);
|
break;
|
||||||
break;
|
case 'jmatch':
|
||||||
case 'jmix':
|
process_jmatch($xml, $questions);
|
||||||
process_jmix($xml, $questions);
|
break;
|
||||||
break;
|
case 'jmix':
|
||||||
case 'jquiz':
|
process_jmix($xml, $questions);
|
||||||
process_jquiz($xml, $questions);
|
break;
|
||||||
break;
|
case 'jquiz':
|
||||||
default:
|
process_jquiz($xml, $questions);
|
||||||
notice("Unknown quiz type '$quiztype'");
|
break;
|
||||||
break;
|
default:
|
||||||
} // end switch
|
notice("Unknown quiz type '$quiztype'");
|
||||||
|
break;
|
||||||
|
} // end switch
|
||||||
|
}
|
||||||
return $questions;
|
return $questions;
|
||||||
}
|
}
|
||||||
} // end class
|
} // end class
|
||||||
|
@ -66,6 +66,7 @@ class quiz_format_hotpot extends quiz_default_format {
|
||||||
function process_jcloze(&$xml, &$questions) {
|
function process_jcloze(&$xml, &$questions) {
|
||||||
// define default grade (per cloze gap)
|
// define default grade (per cloze gap)
|
||||||
$defaultgrade = 1;
|
$defaultgrade = 1;
|
||||||
|
$gap_count = 0;
|
||||||
|
|
||||||
// detect old Moodles (1.4 and earlier)
|
// detect old Moodles (1.4 and earlier)
|
||||||
global $CFG, $db;
|
global $CFG, $db;
|
||||||
|
@ -78,8 +79,11 @@ function process_jcloze(&$xml, &$questions) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// xml tags for the start of the gap-fill exercise
|
||||||
|
$tags = 'data,gap-fill';
|
||||||
|
|
||||||
$x = 0;
|
$x = 0;
|
||||||
while ($exercise = &$xml['#']['data'][0]['#']['gap-fill'][$x]['#']) {
|
while (($exercise = "[$x]['#']") && $xml->xml_value($tags, $exercise)) {
|
||||||
// there is usually only one exercise in a file
|
// there is usually only one exercise in a file
|
||||||
|
|
||||||
$question = new stdClass();
|
$question = new stdClass();
|
||||||
|
@ -102,78 +106,87 @@ function process_jcloze(&$xml, &$questions) {
|
||||||
}
|
}
|
||||||
|
|
||||||
$q = 0;
|
$q = 0;
|
||||||
while ($question_record = &$exercise['question-record'][$q]['#']) {
|
while ($text = $xml->xml_value($tags, $exercise."[$q]")) {
|
||||||
$positionkey = $q+1;
|
// add next bit of text
|
||||||
if (isset($exercise[$q])) {
|
$question->questiontext .= addslashes($text);
|
||||||
$question->questiontext .= addslashes($exercise[$q]);
|
|
||||||
}
|
|
||||||
$question->questiontext .= '{#'.$positionkey.'}';
|
|
||||||
|
|
||||||
// initialize answer settings
|
// check for a gap
|
||||||
if ($moodle_14) {
|
$question_record = $exercise."['question-record'][$q]['#']";
|
||||||
$question->answers[$q]->positionkey = $positionkey;
|
if ($xml->xml_value($tags, $question_record)) {
|
||||||
$question->answers[$q]->answertype = SHORTANSWER;
|
|
||||||
$question->answers[$q]->norm = $defaultgrade;
|
|
||||||
$question->answers[$q]->alternatives = array();
|
|
||||||
} else {
|
|
||||||
$wrapped = new stdClass();
|
|
||||||
$wrapped->qtype = SHORTANSWER;
|
|
||||||
$wrapped->usecase = 0;
|
|
||||||
$wrapped->defaultgrade = $defaultgrade;
|
|
||||||
$wrapped->questiontextformat = 0;
|
|
||||||
$wrapped->answer = array();
|
|
||||||
$wrapped->fraction = array();
|
|
||||||
$wrapped->feedback = array();
|
|
||||||
$answers = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
$a = 0;
|
// add gap
|
||||||
while ($answer = &$question_record['answer'][$a]['#']) {
|
$gap_count ++;
|
||||||
// extract answer details from XML
|
$positionkey = $q+1;
|
||||||
$text = addslashes($answer['text'][0]['#']);
|
$question->questiontext .= '{#'.$positionkey.'}';
|
||||||
$fraction = empty($answer['correct'][0]['#']) ? 0 : 1;
|
|
||||||
$feedback = addslashes($answer['feedback'][0]['#']);
|
// initialize answer settings
|
||||||
// store answer
|
|
||||||
if ($moodle_14) {
|
if ($moodle_14) {
|
||||||
$question->answers[$q]->alternatives[$a] = new stdClass();
|
$question->answers[$q]->positionkey = $positionkey;
|
||||||
$question->answers[$q]->alternatives[$a]->answer = $text;
|
$question->answers[$q]->answertype = SHORTANSWER;
|
||||||
$question->answers[$q]->alternatives[$a]->fraction = $fraction;
|
$question->answers[$q]->norm = $defaultgrade;
|
||||||
$question->answers[$q]->alternatives[$a]->feedback = $feedback;
|
$question->answers[$q]->alternatives = array();
|
||||||
} else {
|
} else {
|
||||||
$wrapped->answer[] = $text;
|
$wrapped = new stdClass();
|
||||||
$wrapped->fraction[] = $fraction;
|
$wrapped->qtype = SHORTANSWER;
|
||||||
$wrapped->feedback[] = $feedback;
|
$wrapped->usecase = 0;
|
||||||
$answers[] = (empty($fraction) ? '' : '=').$text.(empty($feedback) ? '' : ('#'.$feedback));
|
$wrapped->defaultgrade = $defaultgrade;
|
||||||
|
$wrapped->questiontextformat = 0;
|
||||||
|
$wrapped->answer = array();
|
||||||
|
$wrapped->fraction = array();
|
||||||
|
$wrapped->feedback = array();
|
||||||
|
$answers = array();
|
||||||
}
|
}
|
||||||
$a++;
|
|
||||||
}
|
$a = 0;
|
||||||
// compile answers into question text, if necessary
|
while (($answer=$question_record."['answer'][$a]['#']") && $xml->xml_value($tags, $answer)) {
|
||||||
if ($moodle_14) {
|
$text = addslashes($xml->xml_value($tags, $answer."['text'][0]['#']"));
|
||||||
// do nothing
|
$correct = $xml->xml_value($tags, $answer."['correct'][0]['#']");
|
||||||
} else {
|
$feedback = addslashes($xml->xml_value($tags, $answer."['feedback'][0]['#']"));
|
||||||
$wrapped->questiontext = '{'.$defaultgrade.':SHORTANSWER:'.implode('~', $answers).'}';
|
if ($text) {
|
||||||
$question->options->questions[] = $wrapped;
|
// set score (0=0%, 1=100%)
|
||||||
}
|
$fraction = empty($correct) ? 0 : 1;
|
||||||
|
// store answer
|
||||||
|
if ($moodle_14) {
|
||||||
|
$question->answers[$q]->alternatives[$a] = new stdClass();
|
||||||
|
$question->answers[$q]->alternatives[$a]->answer = $text;
|
||||||
|
$question->answers[$q]->alternatives[$a]->fraction = $fraction;
|
||||||
|
$question->answers[$q]->alternatives[$a]->feedback = $feedback;
|
||||||
|
} else {
|
||||||
|
$wrapped->answer[] = $text;
|
||||||
|
$wrapped->fraction[] = $fraction;
|
||||||
|
$wrapped->feedback[] = $feedback;
|
||||||
|
$answers[] = (empty($fraction) ? '' : '=').$text.(empty($feedback) ? '' : ('#'.$feedback));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$a++;
|
||||||
|
}
|
||||||
|
// compile answers into question text, if necessary
|
||||||
|
if ($moodle_14) {
|
||||||
|
// do nothing
|
||||||
|
} else {
|
||||||
|
$wrapped->questiontext = '{'.$defaultgrade.':SHORTANSWER:'.implode('~', $answers).'}';
|
||||||
|
$question->options->questions[] = $wrapped;
|
||||||
|
}
|
||||||
|
} // end if gap
|
||||||
$q++;
|
$q++;
|
||||||
}
|
} // end while $text
|
||||||
// add trailing text, if any
|
|
||||||
if (isset($exercise[$q])) {
|
|
||||||
$question->questiontext .= addslashes($exercise[$q]);
|
|
||||||
}
|
|
||||||
// define total grade for this exercise
|
// define total grade for this exercise
|
||||||
$question->defaultgrade = $q * $defaultgrade;
|
$question->defaultgrade = $gap_count * $defaultgrade;
|
||||||
|
|
||||||
$questions[] = $question;
|
$questions[] = $question;
|
||||||
$x++;
|
$x++;
|
||||||
}
|
} // end while $exercise
|
||||||
}
|
}
|
||||||
|
|
||||||
function process_jcross(&$xml, &$questions) {
|
function process_jcross(&$xml, &$questions) {
|
||||||
|
// xml tags to the start of the crossword exercise items
|
||||||
|
$tags = 'data,crossword,clues,item';
|
||||||
$x = 0;
|
$x = 0;
|
||||||
while ($item = &$xml['#']['data'][0]['#']['crossword'][0]['#']['clues'][0]['#']['item'][$x]['#']) {
|
while (($item = "[$x]['#']") && $xml->xml_value($tags, $item)) {
|
||||||
|
|
||||||
$text = $item['def'][0]['#'];
|
$text = $xml->xml_value($tags, $item."['def'][0]['#']");
|
||||||
$answer = $item['word'][0]['#'];
|
$answer = $xml->xml_value($tags, $item."['word'][0]['#']");
|
||||||
|
|
||||||
if ($text && $answer) {
|
if ($text && $answer) {
|
||||||
$question = new stdClass();
|
$question = new stdClass();
|
||||||
|
@ -194,38 +207,49 @@ function process_jcross(&$xml, &$questions) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function process_jmatch(&$xml, &$questions) {
|
function process_jmatch(&$xml, &$questions) {
|
||||||
|
$defaultgrade = 1;
|
||||||
|
$match_count = 0;
|
||||||
|
$tags = 'data,matching-exercise';
|
||||||
|
|
||||||
$x = 0;
|
$x = 0;
|
||||||
while ($exercise = &$xml['#']['data'][0]['#']['matching-exercise'][$x]['#']) {
|
while (($exercise = "[$x]['#']") && $xml->xml_value($tags, $exercise)) {
|
||||||
// there is usually only one exercise in a file
|
// there is usually only one exercise in a file
|
||||||
|
|
||||||
$question = new stdClass();
|
$question = new stdClass();
|
||||||
|
|
||||||
$question->qtype = MATCH;
|
$question->qtype = MATCH;
|
||||||
$question->defaultgrade = 1;
|
|
||||||
$question->usecase = 0; // Ignore case
|
$question->usecase = 0; // Ignore case
|
||||||
$question->image = ""; // No images with this format
|
$question->image = ""; // No images with this format
|
||||||
$question->name = get_hotpotatoes_title($xml, $x);
|
$question->name = get_hotpotatoes_title($xml, $x);
|
||||||
|
|
||||||
$question->questiontext = get_hotpotatoes_reading($xml);
|
$question->questiontext = get_hotpotatoes_reading($xml);
|
||||||
$question->questiontext .= get_hotpotatoes_instructions($xml, 'jmatch');
|
$question->questiontext .= get_hotpotatoes_instructions($xml);
|
||||||
|
|
||||||
$question->subquestions = array();
|
$question->subquestions = array();
|
||||||
$question->subanswers = array();
|
$question->subanswers = array();
|
||||||
$p = 0;
|
$p = 0;
|
||||||
while ($pair = &$exercise['pair'][$p]['#']) {
|
while (($pair = $exercise."['pair'][$p]['#']") && $xml->xml_value($tags, $pair)) {
|
||||||
$question->subquestions[$p] = addslashes($pair['left-item'][0]['#']['text'][0]['#']);
|
$left = $xml->xml_value($tags, $pair."['left-item'][0]['#']['text'][0]['#']");
|
||||||
$question->subanswers[$p] = addslashes($pair['right-item'][0]['#']['text'][0]['#']);
|
$right = $xml->xml_value($tags, $pair."['right-item'][0]['#']['text'][0]['#']");
|
||||||
|
if ($left && $right) {
|
||||||
|
$match_count++;
|
||||||
|
$question->subquestions[$p] = addslashes($left);
|
||||||
|
$question->subanswers[$p] = addslashes($right);
|
||||||
|
}
|
||||||
$p++;
|
$p++;
|
||||||
}
|
}
|
||||||
|
$question->defaultgrade = $match_count * $defaultgrade;
|
||||||
$questions[] = $question;
|
$questions[] = $question;
|
||||||
$x++;
|
$x++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function process_jmix(&$xml, &$questions) {
|
function process_jmix(&$xml, &$questions) {
|
||||||
|
$defaultgrade = 1;
|
||||||
|
$segment_count = 0;
|
||||||
|
$tags = 'data,jumbled-order-exercise';
|
||||||
$x = 0;
|
$x = 0;
|
||||||
while ($exercise = &$xml['#']['data'][0]['#']['jumbled-order-exercise'][$x]['#']) {
|
while (($exercise = "[$x]['#']") && $xml->xml_value($tags, $exercise)) {
|
||||||
// there is usually only one exercise in a file
|
// there is usually only one exercise in a file
|
||||||
|
|
||||||
$question = new stdClass();
|
$question = new stdClass();
|
||||||
|
@ -240,8 +264,9 @@ function process_jmix(&$xml, &$questions) {
|
||||||
|
|
||||||
$i = 0;
|
$i = 0;
|
||||||
$segments = array();
|
$segments = array();
|
||||||
while ($segment = &$exercise['main-order'][0]['#']['segment'][$i]['#']) {
|
while ($segment = $xml->xml_value($tags, $exercise."['main-order'][0]['#']['segment'][$i]['#']")) {
|
||||||
$segments[] = addslashes($segment);
|
$segments[] = addslashes($segment);
|
||||||
|
$segment_count++;
|
||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
$answer = implode(' ', $segments);
|
$answer = implode(' ', $segments);
|
||||||
|
@ -250,7 +275,7 @@ function process_jmix(&$xml, &$questions) {
|
||||||
shuffle($segments);
|
shuffle($segments);
|
||||||
|
|
||||||
$question->questiontext = get_hotpotatoes_reading($xml);
|
$question->questiontext = get_hotpotatoes_reading($xml);
|
||||||
$question->questiontext .= get_hotpotatoes_instructions($xml, 'jmix');
|
$question->questiontext .= get_hotpotatoes_instructions($xml);
|
||||||
$question->questiontext .= ' <NOBR><B>[ '.implode(' ', $segments).' ]</B></NOBR>';
|
$question->questiontext .= ' <NOBR><B>[ '.implode(' ', $segments).' ]</B></NOBR>';
|
||||||
|
|
||||||
$a = 0;
|
$a = 0;
|
||||||
|
@ -258,29 +283,33 @@ function process_jmix(&$xml, &$questions) {
|
||||||
$question->answer[$a] = $answer;
|
$question->answer[$a] = $answer;
|
||||||
$question->fraction[$a] = 1;
|
$question->fraction[$a] = 1;
|
||||||
$question->feedback[$a] = '';
|
$question->feedback[$a] = '';
|
||||||
$answer = addslashes($exercise['alternate'][$a++]['#']);
|
$answer = addslashes($xml->xml_value($tags, $exercise."['alternate'][$a++]['#']"));
|
||||||
}
|
}
|
||||||
|
$question->defaultgrade = $segment_count * $defaultgrade;
|
||||||
$questions[] = $question;
|
$questions[] = $question;
|
||||||
$x++;
|
$x++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function process_jquiz(&$xml, &$questions) {
|
function process_jquiz(&$xml, &$questions) {
|
||||||
|
$defaultgrade = 1;
|
||||||
|
$tags = 'data,questions';
|
||||||
$x = 0;
|
$x = 0;
|
||||||
while ($exercise = &$xml['#']['data'][0]['#']['questions'][$x]['#']) {
|
while (($exercise = "[$x]['#']") && $xml->xml_value($tags, $exercise)) {
|
||||||
// there is usually only one 'questions' object in a single exercise
|
// there is usually only one 'questions' object in a single exercise
|
||||||
|
|
||||||
$q = 0;
|
$q = 0;
|
||||||
while ($question_record = &$exercise['question-record'][$q]['#']) {
|
while (($question_record = $exercise."['question-record'][$q]['#']") && $xml->xml_value($tags, $question_record)) {
|
||||||
|
|
||||||
$question = new stdClass();
|
$question = new stdClass();
|
||||||
$question->defaultgrade = 1;
|
$question->defaultgrade = $defaultgrade;
|
||||||
$question->usecase = 0; // Ignore case
|
$question->usecase = 0; // Ignore case
|
||||||
$question->image = ""; // No images with this format
|
$question->image = ""; // No images with this format
|
||||||
$question->name = get_hotpotatoes_title($xml, $q, true);
|
$question->name = get_hotpotatoes_title($xml, $q, true);
|
||||||
|
|
||||||
$question->questiontext = addslashes($question_record['question'][0]['#']);
|
$text = $xml->xml_value($tags, $question_record."['question'][0]['#']");
|
||||||
|
$question->questiontext = addslashes($text);
|
||||||
|
|
||||||
$type = $question_record['question-type'][0]['#'];
|
$type = $xml->xml_value($tags, $question_record."['question-type'][0]['#']");
|
||||||
// 1 : multiple choice
|
// 1 : multiple choice
|
||||||
// 2 : short-answer
|
// 2 : short-answer
|
||||||
// 3 : hybrid
|
// 3 : hybrid
|
||||||
|
@ -288,12 +317,18 @@ function process_jquiz(&$xml, &$questions) {
|
||||||
$question->qtype = ($type==2 ? SHORTANSWER : MULTICHOICE);
|
$question->qtype = ($type==2 ? SHORTANSWER : MULTICHOICE);
|
||||||
$question->single = ($type==4 ? 0 : 1);
|
$question->single = ($type==4 ? 0 : 1);
|
||||||
|
|
||||||
|
// XML tags to the "answers" tree
|
||||||
|
$answers = $question_record."['answers'][0]['#']";
|
||||||
|
|
||||||
// workaround required to calculate scores for multiple select answers
|
// workaround required to calculate scores for multiple select answers
|
||||||
$no_of_correct_answers = 0;
|
$no_of_correct_answers = 0;
|
||||||
if ($type==4) {
|
if ($type==4) {
|
||||||
$a = 0;
|
$a = 0;
|
||||||
while ($answer = &$question_record['answers'][0]['#']['answer'][$a]['#']) {
|
while (($answer = $answers."['answer'][$a]['#']") && $xml->xml_value($tags, $answer)) {
|
||||||
if (!empty($answer['correct'][0]['#'])) {
|
$correct = $xml->xml_value($tags, $answer."['correct'][0]['#']");
|
||||||
|
if (empty($correct)) {
|
||||||
|
// do nothing
|
||||||
|
} else {
|
||||||
$no_of_correct_answers++;
|
$no_of_correct_answers++;
|
||||||
}
|
}
|
||||||
$a++;
|
$a++;
|
||||||
|
@ -303,18 +338,20 @@ function process_jquiz(&$xml, &$questions) {
|
||||||
$question->answer = array();
|
$question->answer = array();
|
||||||
$question->fraction = array();
|
$question->fraction = array();
|
||||||
$question->feedback = array();
|
$question->feedback = array();
|
||||||
while ($answer = &$question_record['answers'][0]['#']['answer'][$a]['#']) {
|
while (($answer = $answers."['answer'][$a]['#']") && $xml->xml_value($tags, $answer)) {
|
||||||
if (empty($answer['correct'][0]['#'])) {
|
$correct = $xml->xml_value($tags, $answer."['correct'][0]['#']");
|
||||||
|
if (empty($correct)) {
|
||||||
$fraction = 0;
|
$fraction = 0;
|
||||||
} else if ($type==4) { // multiple select
|
} else if ($type==4) { // multiple select
|
||||||
// strange behavior if the $fraction isn't exact to 5 decimal places
|
// strange behavior if the $fraction isn't exact to 5 decimal places
|
||||||
$fraction = round(1/$no_of_correct_answers, 5);
|
$fraction = round(1/$no_of_correct_answers, 5);
|
||||||
} else {
|
} else {
|
||||||
$fraction = $answer['percent-correct'][0]['#']/100;
|
$percent = $xml->xml_value($tags, $answer."['percent-correct'][0]['#']");
|
||||||
|
$fraction = $percent/100;
|
||||||
}
|
}
|
||||||
$question->feedback[] = addslashes($answer['feedback'][0]['#']);
|
|
||||||
$question->fraction[] = $fraction;
|
$question->fraction[] = $fraction;
|
||||||
$question->answer[] = addslashes($answer['text'][0]['#']);
|
$question->feedback[] = addslashes($xml->xml_value($tags, $answer."['feedback'][0]['#']"));
|
||||||
|
$question->answer[] = addslashes($xml->xml_value($tags, $answer."['text'][0]['#']"));
|
||||||
$a++;
|
$a++;
|
||||||
}
|
}
|
||||||
$questions[] = $question;
|
$questions[] = $question;
|
||||||
|
@ -332,27 +369,27 @@ function seed_hotpotatoes_RNG() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function get_hotpotatoes_title(&$xml, $x, $flag=false) {
|
function get_hotpotatoes_title(&$xml, $x, $flag=false) {
|
||||||
$title = $xml['#']['data'][0]['#']['title'][0]['#'];
|
$title = $xml->xml_value('data,title');
|
||||||
if ($x || $flag) {
|
if ($x || $flag) {
|
||||||
$title .= ' ('.($x+1).')';
|
$title .= ' ('.($x+1).')';
|
||||||
}
|
}
|
||||||
return addslashes($title);
|
return addslashes($title);
|
||||||
}
|
}
|
||||||
function get_hotpotatoes_instructions(&$xml, $quiztype) {
|
function get_hotpotatoes_instructions(&$xml) {
|
||||||
$text = $xml['#']['hotpot-config-file'][0]['#'][$quiztype][0]['#']['instructions'][0]['#'];
|
$text = $xml->xml_value('hotpot-config-file,instructions');
|
||||||
if (empty($text)) {
|
if (empty($text)) {
|
||||||
$text = "Hot Potatoes $quiztype";
|
$text = "Hot Potatoes $xml->quiztype";
|
||||||
}
|
}
|
||||||
return addslashes($text);
|
return addslashes($text);
|
||||||
}
|
}
|
||||||
function get_hotpotatoes_reading(&$xml) {
|
function get_hotpotatoes_reading(&$xml) {
|
||||||
$str = '';
|
$str = '';
|
||||||
$obj = &$xml['#']['data'][0]['#']['reading'][0]['#'];
|
$tags = 'data,reading';
|
||||||
if ($obj['include-reading'][0]['#']) {
|
if ($xml->xml_value("$tags,include-reading")) {
|
||||||
if ($title = $obj['reading-title'][0]['#']) {
|
if ($title = $xml->xml_value("$tags,reading-title")) {
|
||||||
$str .= "<H3>$title</H3>";
|
$str .= "<H3>$title</H3>";
|
||||||
}
|
}
|
||||||
if ($text = $obj['reading-text'][0]['#']) {
|
if ($text = $xml->xml_value("$tags,reading-text")) {
|
||||||
$str .= "<P>$text</P>";
|
$str .= "<P>$text</P>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -370,4 +407,129 @@ if (!class_exists("quiz_file_format")) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// get the standard XML parser supplied with Moodle
|
||||||
|
require_once("$CFG->libdir/xmlize.php");
|
||||||
|
|
||||||
|
class hotpot_xml_tree {
|
||||||
|
function hotpot_xml_tree($str, $xml_root='') {
|
||||||
|
if (empty($str)) {
|
||||||
|
$this->xml = array();
|
||||||
|
} else {
|
||||||
|
// encode htmlentities in JCloze
|
||||||
|
$this->encode_cdata($str, 'gap-fill');
|
||||||
|
// encode as utf8
|
||||||
|
$str = utf8_encode($str);
|
||||||
|
// xmlize (=convert to tree)
|
||||||
|
$this->xml = xmlize($str, 0);
|
||||||
|
}
|
||||||
|
$this->xml_root = $xml_root;
|
||||||
|
}
|
||||||
|
function xml_value($tags, $more_tags="[0]['#']") {
|
||||||
|
|
||||||
|
$tags = empty($tags) ? '' : "['".str_replace(",", "'][0]['#']['", $tags)."']";
|
||||||
|
eval('$value = &$this->xml'.$this->xml_root.$tags.$more_tags.';');
|
||||||
|
|
||||||
|
if (is_string($value)) {
|
||||||
|
$value = utf8_decode($value);
|
||||||
|
|
||||||
|
// decode angle brackets
|
||||||
|
$value = strtr($value, array('<'=>'<', '>'=>'>'));
|
||||||
|
|
||||||
|
// remove white space between <table>, <ul|OL|DL> and <OBJECT|EMBED> parts
|
||||||
|
// (so it doesn't get converted to <br />)
|
||||||
|
$htmltags = '('
|
||||||
|
. 'TABLE|/?CAPTION|/?COL|/?COLGROUP|/?TBODY|/?TFOOT|/?THEAD|/?TD|/?TH|/?TR'
|
||||||
|
. '|OL|UL|/?LI'
|
||||||
|
. '|DL|/?DT|/?DD'
|
||||||
|
. '|EMBED|OBJECT|APPLET|/?PARAM'
|
||||||
|
//. '|SELECT|/?OPTION'
|
||||||
|
//. '|FIELDSET|/?LEGEND'
|
||||||
|
//. '|FRAMESET|/?FRAME'
|
||||||
|
. ')'
|
||||||
|
;
|
||||||
|
$search = '#(<'.$htmltags.'[^>]*'.'>)\s+'.'(?='.'<'.')#is';
|
||||||
|
$value = preg_replace($search, '\\1', $value);
|
||||||
|
|
||||||
|
// replace remaining newlines with <br />
|
||||||
|
$value = str_replace("\n", '<br />', $value);
|
||||||
|
|
||||||
|
// encode unicode characters as HTML entities
|
||||||
|
// (in particular, accented charaters that have not been encoded by HP)
|
||||||
|
|
||||||
|
// unicode characetsr can be detected by checking the hex value of a character
|
||||||
|
// 00 - 7F : ascii char (roman alphabet + punctuation)
|
||||||
|
// 80 - BF : byte 2, 3 or 4 of a unicode char
|
||||||
|
// C0 - DF : 1st byte of 2-byte char
|
||||||
|
// E0 - EF : 1st byte of 3-byte char
|
||||||
|
// F0 - FF : 1st byte of 4-byte char
|
||||||
|
// if the string doesn't match the above, it might be
|
||||||
|
// 80 - FF : single-byte, non-ascii char
|
||||||
|
$search = '#('.'[\xc0-\xdf][\x80-\xbf]'.'|'.'[\xe0-\xef][\x80-\xbf]{2}'.'|'.'[\xf0-\xff][\x80-\xbf]{3}'.'|'.'[\x80-\xff]'.')#se';
|
||||||
|
$value = preg_replace($search, "hotpot_utf8_to_html_entity('\\1')", $value);
|
||||||
|
}
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
function encode_cdata(&$str, $tag) {
|
||||||
|
|
||||||
|
// conversion tables
|
||||||
|
static $HTML_ENTITIES = array(
|
||||||
|
''' => "'",
|
||||||
|
'"' => '"',
|
||||||
|
'<' => '<',
|
||||||
|
'>' => '>',
|
||||||
|
'&' => '&',
|
||||||
|
);
|
||||||
|
static $ILLEGAL_STRINGS = array(
|
||||||
|
"\r" => '',
|
||||||
|
"\n" => '<br />',
|
||||||
|
']]>' => ']]>',
|
||||||
|
);
|
||||||
|
|
||||||
|
// extract the $tag from the $str(ing), if possible
|
||||||
|
$pattern = '|(^.*<'.$tag.'[^>]*)(>.*<)(/'.$tag.'>.*$)|is';
|
||||||
|
if (preg_match($pattern, $str, $matches)) {
|
||||||
|
|
||||||
|
// encode problematic CDATA chars and strings
|
||||||
|
$matches[2] = strtr($matches[2], $ILLEGAL_STRINGS);
|
||||||
|
|
||||||
|
// if there are any ampersands in "open text"
|
||||||
|
// surround them by CDATA start and end markers
|
||||||
|
// (and convert HTML entities to plain text)
|
||||||
|
$search = '/>([^<]*&[^<]*)</e';
|
||||||
|
$replace = '"><![CDATA[".strtr("$1", $HTML_ENTITIES)."]]><"';
|
||||||
|
$matches[2] = preg_replace($search, $replace, $matches[2]);
|
||||||
|
|
||||||
|
$str = $matches[1].$matches[2].$matches[3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function hotpot_utf8_to_html_entity($char) {
|
||||||
|
// http://www.zend.com/codex.php?id=835&single=1
|
||||||
|
|
||||||
|
// array used to figure what number to decrement from character order value
|
||||||
|
// according to number of characters used to map unicode to ascii by utf-8
|
||||||
|
static $HOTPOT_UTF8_DECREMENT = array(
|
||||||
|
1=>0, 2=>192, 3=>224, 4=>240
|
||||||
|
);
|
||||||
|
|
||||||
|
// the number of bits to shift each character by
|
||||||
|
static $HOTPOT_UTF8_SHIFT = array(
|
||||||
|
1=>array(0=>0),
|
||||||
|
2=>array(0=>6, 1=>0),
|
||||||
|
3=>array(0=>12, 1=>6, 2=>0),
|
||||||
|
4=>array(0=>18, 1=>12, 2=>6, 3=>0)
|
||||||
|
);
|
||||||
|
|
||||||
|
$dec = 0;
|
||||||
|
$len = strlen($char);
|
||||||
|
for ($pos=0; $pos<$len; $pos++) {
|
||||||
|
$ord = ord ($char{$pos});
|
||||||
|
$ord -= ($pos ? 128 : $HOTPOT_UTF8_DECREMENT[$len]);
|
||||||
|
$dec += ($ord << $HOTPOT_UTF8_SHIFT[$len][$pos]);
|
||||||
|
}
|
||||||
|
return '&#x'.sprintf('%04X', $dec).';';
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue