2 ////////////////////////////////////////////////////////////////////////////
3 /// Hotpotatoes 5.0 and 6.0 Format
5 /// This Moodle class provides all functions necessary to import
6 /// (export is not implemented ... yet)
8 ////////////////////////////////////////////////////////////////////////////
10 // Based on default.php, included by ../import.php
12 * @package questionbank
13 * @subpackage importexport
16 class qformat_hotpot
extends qformat_default
{
18 function provide_import() {
22 function readquestions ($lines) {
23 /// Parses an array of lines into an array of questions,
24 /// where each item is a question object as defined by
29 if ($CFG->slasharguments
) {
30 $baseurl = "$CFG->wwwroot/file.php/{$this->course->id}/";
32 $baseurl = "$CFG->wwwroot/file.php?file=/{$this->course->id}/";
35 // get import file name
37 if (isset($params) && !empty($params->choosefile
)) {
38 // course file (Moodle >=1.6+)
39 $filename = $params->choosefile
;
41 // uploaded file (all Moodles)
42 $filename = basename($_FILES['newfile']['tmp_name']);
45 // get hotpot file source
46 $source = implode($lines, " ");
47 $source = hotpot_convert_relative_urls($source, $baseurl, $filename);
49 // create xml tree for this hotpot
50 $xml = new hotpot_xml_tree($source);
52 // determine the quiz type
54 $keys = array_keys($xml->xml
);
55 foreach ($keys as $key) {
56 if (preg_match('/^(hotpot|textoys)-(\w+)-file$/i', $key, $matches)) {
57 $xml->quiztype
= strtolower($matches[2]);
58 $xml->xml_root
= "['$key']['#']";
63 // convert xml to questions array
65 switch ($xml->quiztype
) {
67 $this->process_jcloze($xml, $questions);
70 $this->process_jcross($xml, $questions);
73 $this->process_jmatch($xml, $questions);
76 $this->process_jmix($xml, $questions);
80 $this->process_jquiz($xml, $questions);
83 if (empty($xml->quiztype
)) {
84 notice("Input file not recognized as a Hot Potatoes XML file");
86 notice("Unknown quiz type '$xml->quiztype'");
92 function process_jcloze(&$xml, &$questions) {
93 // define default grade (per cloze gap)
97 // detect old Moodles (1.4 and earlier)
100 if ($columns = $db->MetaColumns("{$CFG->prefix}question_multianswer")) {
101 foreach ($columns as $column) {
102 if ($column->name
=='answers' ||
$column->name
=='positionkey' ||
$column->name
=='answertype' ||
$column->name
=='norm') {
108 // xml tags for the start of the gap-fill exercise
109 $tags = 'data,gap-fill';
112 while (($exercise = "[$x]['#']") && $xml->xml_value($tags, $exercise)) {
113 // there is usually only one exercise in a file
115 if (method_exists($this, 'defaultquestion')) {
116 $question = $this->defaultquestion();
118 $question = new stdClass();
119 $question->usecase
= 0; // Ignore case
120 $question->image
= ""; // No images with this format
122 $question->qtype
= MULTIANSWER
;
124 $question->name
= $this->hotpot_get_title($xml, $x);
125 $question->questiontext
= $this->hotpot_get_reading($xml);
127 // setup answer arrays
129 $question->answers
= array();
131 global $COURSE; // initialized in questions/import.php
132 $question->course
= $COURSE->id
;
133 $question->options
= new stdClass();
134 $question->options
->questions
= array(); // one for each gap
138 while ($text = $xml->xml_value($tags, $exercise."[$q]")) {
139 // add next bit of text
140 $question->questiontext
.= $this->hotpot_prepare_str($text);
143 $question_record = $exercise."['question-record'][$q]['#']";
144 if ($xml->xml_value($tags, $question_record)) {
149 $question->questiontext
.= '{#'.$positionkey.'}';
151 // initialize answer settings
153 $question->answers
[$q]->positionkey
= $positionkey;
154 $question->answers
[$q]->answertype
= SHORTANSWER
;
155 $question->answers
[$q]->norm
= $defaultgrade;
156 $question->answers
[$q]->alternatives
= array();
158 $wrapped = new stdClass();
159 $wrapped->qtype
= SHORTANSWER
;
160 $wrapped->usecase
= 0;
161 $wrapped->defaultgrade
= $defaultgrade;
162 $wrapped->questiontextformat
= 0;
163 $wrapped->answer
= array();
164 $wrapped->fraction
= array();
165 $wrapped->feedback
= array();
171 while (($answer=$question_record."['answer'][$a]['#']") && $xml->xml_value($tags, $answer)) {
172 $text = $this->hotpot_prepare_str($xml->xml_value($tags, $answer."['text'][0]['#']"));
173 $correct = $xml->xml_value($tags, $answer."['correct'][0]['#']");
174 $feedback = $this->hotpot_prepare_str($xml->xml_value($tags, $answer."['feedback'][0]['#']"));
176 // set score (0=0%, 1=100%)
177 $fraction = empty($correct) ?
0 : 1;
180 $question->answers
[$q]->alternatives
[$a] = new stdClass();
181 $question->answers
[$q]->alternatives
[$a]->answer
= $text;
182 $question->answers
[$q]->alternatives
[$a]->fraction
= $fraction;
183 $question->answers
[$q]->alternatives
[$a]->feedback
= $feedback;
185 $wrapped->answer
[] = $text;
186 $wrapped->fraction
[] = $fraction;
187 $wrapped->feedback
[] = $feedback;
188 $answers[] = (empty($fraction) ?
'' : '=').$text.(empty($feedback) ?
'' : ('#'.$feedback));
193 // compile answers into question text, if necessary
197 $wrapped->questiontext
= '{'.$defaultgrade.':SHORTANSWER:'.implode('~', $answers).'}';
198 $question->options
->questions
[] = $wrapped;
204 // define total grade for this exercise
205 $question->defaultgrade
= $gap_count * $defaultgrade;
207 $questions[] = $question;
209 } // end while $exercise
212 function process_jcross(&$xml, &$questions) {
213 // xml tags to the start of the crossword exercise clue items
214 $tags = 'data,crossword,clues,item';
217 while (($item = "[$x]['#']") && $xml->xml_value($tags, $item)) {
219 $text = $xml->xml_value($tags, $item."['def'][0]['#']");
220 $answer = $xml->xml_value($tags, $item."['word'][0]['#']");
222 if ($text && $answer) {
223 if (method_exists($this, 'defaultquestion')) {
224 $question = $this->defaultquestion();
226 $question = new stdClass();
227 $question->usecase
= 0; // Ignore case
228 $question->image
= ""; // No images with this format
230 $question->qtype
= SHORTANSWER
;
231 $question->name
= $this->hotpot_get_title($xml, $x, true);
233 $question->questiontext
= $this->hotpot_prepare_str($text);
234 $question->answer
= array($this->hotpot_prepare_str($answer));
235 $question->fraction
= array(1);
236 $question->feedback
= array('');
238 $questions[] = $question;
244 function process_jmatch(&$xml, &$questions) {
245 // define default grade (per matched pair)
249 // xml tags to the start of the matching exercise
250 $tags = 'data,matching-exercise';
253 while (($exercise = "[$x]['#']") && $xml->xml_value($tags, $exercise)) {
254 // there is usually only one exercise in a file
256 if (method_exists($this, 'defaultquestion')) {
257 $question = $this->defaultquestion();
259 $question = new stdClass();
260 $question->usecase
= 0; // Ignore case
261 $question->image
= ""; // No images with this format
263 $question->qtype
= MATCH
;
264 $question->name
= $this->hotpot_get_title($xml, $x);
266 $question->questiontext
= $this->hotpot_get_reading($xml);
267 $question->questiontext
.= $this->hotpot_get_instructions($xml);
269 $question->subquestions
= array();
270 $question->subanswers
= array();
272 while (($pair = $exercise."['pair'][$p]['#']") && $xml->xml_value($tags, $pair)) {
273 $left = $xml->xml_value($tags, $pair."['left-item'][0]['#']['text'][0]['#']");
274 $right = $xml->xml_value($tags, $pair."['right-item'][0]['#']['text'][0]['#']");
275 if ($left && $right) {
277 $question->subquestions
[$p] = $this->hotpot_prepare_str($left);
278 $question->subanswers
[$p] = $this->hotpot_prepare_str($right);
282 $question->defaultgrade
= $match_count * $defaultgrade;
283 $questions[] = $question;
288 function process_jmix(&$xml, &$questions) {
289 // define default grade (per segment)
293 // xml tags to the start of the jumbled order exercise
294 $tags = 'data,jumbled-order-exercise';
297 while (($exercise = "[$x]['#']") && $xml->xml_value($tags, $exercise)) {
298 // there is usually only one exercise in a file
300 if (method_exists($this, 'defaultquestion')) {
301 $question = $this->defaultquestion();
303 $question = new stdClass();
304 $question->usecase
= 0; // Ignore case
305 $question->image
= ""; // No images with this format
307 $question->qtype
= SHORTANSWER
;
308 $question->name
= $this->hotpot_get_title($xml, $x);
310 $question->answer
= array();
311 $question->fraction
= array();
312 $question->feedback
= array();
316 while ($segment = $xml->xml_value($tags, $exercise."['main-order'][0]['#']['segment'][$i]['#']")) {
317 $segments[] = $this->hotpot_prepare_str($segment);
321 $answer = implode(' ', $segments);
323 $this->hotpot_seed_RNG();
326 $question->questiontext
= $this->hotpot_get_reading($xml);
327 $question->questiontext
.= $this->hotpot_get_instructions($xml);
328 $question->questiontext
.= ' <NOBR><B>[ '.implode(' ', $segments).' ]</B></NOBR>';
331 while (!empty($answer)) {
332 $question->answer
[$a] = $answer;
333 $question->fraction
[$a] = 1;
334 $question->feedback
[$a] = '';
335 $answer = $this->hotpot_prepare_str($xml->xml_value($tags, $exercise."['alternate'][$a]['#']"));
338 $question->defaultgrade
= $segment_count * $defaultgrade;
339 $questions[] = $question;
343 function process_jquiz(&$xml, &$questions) {
344 // define default grade (per question)
347 // xml tags to the start of the questions
348 $tags = 'data,questions';
351 while (($exercise = "[$x]['#']") && $xml->xml_value($tags, $exercise)) {
352 // there is usually only one 'questions' object in a single exercise
355 while (($question_record = $exercise."['question-record'][$q]['#']") && $xml->xml_value($tags, $question_record)) {
357 if (method_exists($this, 'defaultquestion')) {
358 $question = $this->defaultquestion();
360 $question = new stdClass();
361 $question->usecase
= 0; // Ignore case
362 $question->image
= ""; // No images with this format
364 $question->defaultgrade
= $defaultgrade;
365 $question->name
= $this->hotpot_get_title($xml, $q, true);
367 $text = $xml->xml_value($tags, $question_record."['question'][0]['#']");
368 $question->questiontext
= $this->hotpot_prepare_str($text);
370 if ($xml->xml_value($tags, $question_record."['answers']")) {
372 $answers = $question_record."['answers'][0]['#']";
375 $answers = $question_record;
377 if($xml->xml_value($tags, $question_record."['question-type']")) {
379 $type = $xml->xml_value($tags, $question_record."['question-type'][0]['#']");
380 // 1 : multiple choice
383 // 4 : multiple select
386 switch ($xml->quiztype
) {
388 $must_select_all = $xml->xml_value($tags, $question_record."['must-select-all'][0]['#']");
389 if (empty($must_select_all)) {
390 $type = 1; // multichoice
392 $type = 4; // multiselect
396 $type = 2; // shortanswer
399 $type = 0; // unknown
402 $question->qtype
= ($type==2 ? SHORTANSWER
: MULTICHOICE
);
403 $question->single
= ($type==4 ?
0 : 1);
405 // workaround required to calculate scores for multiple select answers
406 $no_of_correct_answers = 0;
409 while (($answer = $answers."['answer'][$a]['#']") && $xml->xml_value($tags, $answer)) {
410 $correct = $xml->xml_value($tags, $answer."['correct'][0]['#']");
411 if (empty($correct)) {
414 $no_of_correct_answers++
;
420 $question->answer
= array();
421 $question->fraction
= array();
422 $question->feedback
= array();
423 while (($answer = $answers."['answer'][$a]['#']") && $xml->xml_value($tags, $answer)) {
424 $correct = $xml->xml_value($tags, $answer."['correct'][0]['#']");
425 if (empty($correct)) {
427 } else if ($type==4) { // multiple select
428 // strange behavior if the $fraction isn't exact to 5 decimal places
429 $fraction = round(1/$no_of_correct_answers, 5);
431 if ($xml->xml_value($tags, $answer."['percent-correct']")) {
433 $percent = $xml->xml_value($tags, $answer."['percent-correct'][0]['#']");
434 $fraction = $percent/100;
440 $answertext = $this->hotpot_prepare_str($xml->xml_value($tags, $answer."['text'][0]['#']"));
441 if ($answertext!='') {
442 $question->answer
[] = $answertext;
443 $question->fraction
[] = $fraction;
444 $question->feedback
[] = $this->hotpot_prepare_str($xml->xml_value($tags, $answer."['feedback'][0]['#']"));
448 $questions[] = $question;
455 function hotpot_seed_RNG() {
456 // seed the random number generator
457 static $HOTPOT_SEEDED_RNG = FALSE;
458 if (!$HOTPOT_SEEDED_RNG) {
459 srand((double) microtime() * 1000000);
460 $HOTPOT_SEEDED_RNG = TRUE;
463 function hotpot_get_title(&$xml, $x, $flag=false) {
464 $title = $xml->xml_value('data,title');
466 $title .= ' ('.($x+
1).')';
468 return $this->hotpot_prepare_str($title);
470 function hotpot_get_instructions(&$xml) {
471 $text = $xml->xml_value('hotpot-config-file,instructions');
473 $text = "Hot Potatoes $xml->quiztype";
475 return $this->hotpot_prepare_str($text);
477 function hotpot_get_reading(&$xml) {
479 $tags = 'data,reading';
480 if ($xml->xml_value("$tags,include-reading")) {
481 if ($title = $xml->xml_value("$tags,reading-title")) {
482 $str .= "<H3>$title</H3>";
484 if ($text = $xml->xml_value("$tags,reading-text")) {
485 $str .= "<P>$text</P>";
488 return $this->hotpot_prepare_str($str);
490 function hotpot_prepare_str($str) {
491 // convert html entities to unicode and add slashes
492 $str = preg_replace('/&#[x0-9A-F]+;/ie', "html_entity_decode('\\0',ENT_NOQUOTES,'UTF-8')", $str);
493 return addslashes($str);
497 // get the standard XML parser supplied with Moodle
498 require_once("$CFG->libdir/xmlize.php");
500 class hotpot_xml_tree
{
501 function hotpot_xml_tree($str, $xml_root='') {
503 $this->xml
= array();
505 // encode htmlentities in JCloze
506 $this->encode_cdata($str, 'gap-fill');
507 // xmlize (=convert xml to tree)
508 $this->xml
= xmlize($str, 0);
510 $this->xml_root
= $xml_root;
512 function xml_value($tags, $more_tags="[0]['#']") {
514 $tags = empty($tags) ?
'' : "['".str_replace(",", "'][0]['#']['", $tags)."']";
515 eval('$value = &$this->xml'.$this->xml_root
.$tags.$more_tags.';');
517 if (is_string($value)) {
519 // decode angle brackets and ampersands
520 $value = strtr($value, array('<'=>'<', '>'=>'>', '&'=>'&'));
522 // remove white space between <table>, <ul|OL|DL> and <OBJECT|EMBED> parts
523 // (so it doesn't get converted to <br />)
525 . 'TABLE|/?CAPTION|/?COL|/?COLGROUP|/?TBODY|/?TFOOT|/?THEAD|/?TD|/?TH|/?TR'
528 . '|EMBED|OBJECT|APPLET|/?PARAM'
529 //. '|SELECT|/?OPTION'
530 //. '|FIELDSET|/?LEGEND'
531 //. '|FRAMESET|/?FRAME'
534 $search = '#(<'.$htmltags.'[^>]*'.'>)\s+'.'(?='.'<'.')#is';
535 $value = preg_replace($search, '\\1', $value);
537 // replace remaining newlines with <br />
538 $value = str_replace("\n", '<br />', $value);
540 // encode unicode characters as HTML entities
541 // (in particular, accented charaters that have not been encoded by HP)
543 // multibyte unicode characters can be detected by checking the hex value of the first character
544 // 00 - 7F : ascii char (roman alphabet + punctuation)
545 // 80 - BF : byte 2, 3 or 4 of a unicode char
546 // C0 - DF : 1st byte of 2-byte char
547 // E0 - EF : 1st byte of 3-byte char
548 // F0 - FF : 1st byte of 4-byte char
549 // if the string doesn't match the above, it might be
550 // 80 - FF : single-byte, non-ascii char
551 $search = '#('.'[\xc0-\xdf][\x80-\xbf]'.'|'.'[\xe0-\xef][\x80-\xbf]{2}'.'|'.'[\xf0-\xff][\x80-\xbf]{3}'.'|'.'[\x80-\xff]'.')#se';
552 $value = preg_replace($search, "hotpot_utf8_to_html_entity('\\1')", $value);
556 function encode_cdata(&$str, $tag) {
559 static $HTML_ENTITIES = array(
566 static $ILLEGAL_STRINGS = array(
568 "\n" => '<br />',
569 ']]>' => ']]>',
572 // extract the $tag from the $str(ing), if possible
573 $pattern = '|(^.*<'.$tag.'[^>]*)(>.*<)(/'.$tag.'>.*$)|is';
574 if (preg_match($pattern, $str, $matches)) {
576 // encode problematic CDATA chars and strings
577 $matches[2] = strtr($matches[2], $ILLEGAL_STRINGS);
580 // if there are any ampersands in "open text"
581 // surround them by CDATA start and end markers
582 // (and convert HTML entities to plain text)
583 $search = '/>([^<]*&[^<]*)</e';
584 $replace = '"><![CDATA[".strtr("$1", $HTML_ENTITIES)."]]><"';
585 $matches[2] = preg_replace($search, $replace, $matches[2]);
587 $str = $matches[1].$matches[2].$matches[3];
592 function hotpot_utf8_to_html_entity($char) {
593 // http://www.zend.com/codex.php?id=835&single=1
595 // array used to figure what number to decrement from character order value
596 // according to number of characters used to map unicode to ascii by utf-8
597 static $HOTPOT_UTF8_DECREMENT = array(
598 1=>0, 2=>192, 3=>224, 4=>240
601 // the number of bits to shift each character by
602 static $HOTPOT_UTF8_SHIFT = array(
604 2=>array(0=>6, 1=>0),
605 3=>array(0=>12, 1=>6, 2=>0),
606 4=>array(0=>18, 1=>12, 2=>6, 3=>0)
610 $len = strlen($char);
611 for ($pos=0; $pos<$len; $pos++
) {
612 $ord = ord ($char{$pos});
613 $ord -= ($pos ?
128 : $HOTPOT_UTF8_DECREMENT[$len]);
614 $dec +
= ($ord << $HOTPOT_UTF8_SHIFT[$len][$pos]);
616 return '&#x'.sprintf('%04X', $dec).';';
619 function hotpot_convert_relative_urls($str, $baseurl, $filename) {
620 $tagopen = '(?:(<)|(<)|(&#x003C;))'; // left angle bracket
621 $tagclose = '(?(2)>|(?(3)>|(?(4)&#x003E;)))'; // right angle bracket (to match left angle bracket)
623 $space = '\s+'; // at least one space
624 $anychar = '(?:[^>]*?)'; // any character
626 $quoteopen = '("|"|&quot;)'; // open quote
627 $quoteclose = '\\5'; // close quote (to match open quote)
629 $replace = "hotpot_convert_relative_url('".$baseurl."', '".$filename."', '\\1', '\\6', '\\7')";
631 $tags = array('script'=>'src', 'link'=>'href', 'a'=>'href','img'=>'src','param'=>'value', 'object'=>'data', 'embed'=>'src');
632 foreach ($tags as $tag=>$attribute) {
634 $url = '\S+?\.\S+?'; // must include a filename and have no spaces
638 $search = "%($tagopen$tag$space$anychar$attribute=$quoteopen)($url)($quoteclose$anychar$tagclose)%ise";
639 $str = preg_replace($search, $replace, $str);
645 function hotpot_convert_relative_url($baseurl, $filename, $opentag, $url, $closetag, $stripslashes=true) {
647 $opentag = stripslashes($opentag);
648 $url = stripslashes($url);
649 $closetag = stripslashes($closetag);
652 // catch <PARAM name="FlashVars" value="TheSound=soundfile.mp3">
653 // ampersands can appear as "&", "&" or "&#x0026;amp;"
654 if (preg_match('|^'.'\w+=[^&]+'.'('.'&((amp;#x0026;)?amp;)?'.'\w+=[^&]+)*'.'$|', $url)) {
659 // parse the $url into $matches
661 // [2] query string, if any
662 // [3] anchor fragment, if any
663 } else if (preg_match('|^'.'([^?]*)'.'((?:\\?[^#]*)?)'.'((?:#.*)?)'.'$|', $url, $matches)) {
665 $query = $matches[2];
666 $fragment = $matches[3];
668 // there appears to be no query or fragment in this url
675 $url = hotpot_convert_url($baseurl, $filename, $url, false);
679 $search = '#'.'(file|src|thesound)='."([^&]+)".'#ise';
680 $replace = "'\\1='.hotpot_convert_url('".$baseurl."','".$filename."','\\2')";
681 $query = preg_replace($search, $replace, $query);
684 $url = $opentag.$url.$query.$fragment.$closetag;
689 function hotpot_convert_url($baseurl, $filename, $url, $stripslashes=true) {
690 // maintain a cache of converted urls
691 static $HOTPOT_RELATIVE_URLS = array();
694 $url = stripslashes($url);
697 // is this an absolute url? (or javascript pseudo url)
698 if (preg_match('%^(http://|/|javascript:)%i', $url)) {
701 // has this relative url already been converted?
702 } else if (isset($HOTPOT_RELATIVE_URLS[$url])) {
703 $url = $HOTPOT_RELATIVE_URLS[$url];
708 // get the subdirectory, $dir, of the quiz $filename
709 $dir = dirname($filename);
711 // allow for leading "./" and "../"
712 while (preg_match('|^(\.{1,2})/(.*)$|', $url, $matches)) {
713 if ($matches[1]=='..') {
714 $dir = dirname($dir);
719 // add subdirectory, $dir, to $baseurl, if necessary
720 if ($dir && $dir<>'.') {
724 // prefix $url with $baseurl
725 $url = "$baseurl$url";
728 $HOTPOT_RELATIVE_URLS[$relativeurl] = $url;
733 // allow importing in Moodle v1.4 (and less)
734 // same core functions but different class name
735 if (!class_exists("quiz_file_format")) {
736 class quiz_file_format
extends qformat_default
{
737 function readquestions ($lines) {
738 $format = new qformat_hotpot();
739 return $format->readquestions($lines);