Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / question / format / hotpot / format.php
blobb08f0218a810936b728f3079e731f81f136dc12f
1 <?PHP // $Id$
2 ////////////////////////////////////////////////////////////////////////////
3 /// Hotpotatoes 5.0 and 6.0 Format
4 ///
5 /// This Moodle class provides all functions necessary to import
6 /// (export is not implemented ... yet)
7 ///
8 ////////////////////////////////////////////////////////////////////////////
10 // Based on default.php, included by ../import.php
11 /**
12 * @package questionbank
13 * @subpackage importexport
16 class qformat_hotpot extends qformat_default {
18 function provide_import() {
19 return true;
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
25 /// readquestion().
27 // set baseurl
28 global $CFG;
29 if ($CFG->slasharguments) {
30 $baseurl = "$CFG->wwwroot/file.php/{$this->course->id}/";
31 } else {
32 $baseurl = "$CFG->wwwroot/file.php?file=/{$this->course->id}/";
35 // get import file name
36 global $params;
37 if (isset($params) && !empty($params->choosefile)) {
38 // course file (Moodle >=1.6+)
39 $filename = $params->choosefile;
40 } else {
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
53 $xml->quiztype = '';
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']['#']";
59 break;
63 // convert xml to questions array
64 $questions = array();
65 switch ($xml->quiztype) {
66 case 'jcloze':
67 $this->process_jcloze($xml, $questions);
68 break;
69 case 'jcross':
70 $this->process_jcross($xml, $questions);
71 break;
72 case 'jmatch':
73 $this->process_jmatch($xml, $questions);
74 break;
75 case 'jmix':
76 $this->process_jmix($xml, $questions);
77 break;
78 case 'jbc':
79 case 'jquiz':
80 $this->process_jquiz($xml, $questions);
81 break;
82 default:
83 if (empty($xml->quiztype)) {
84 notice("Input file not recognized as a Hot Potatoes XML file");
85 } else {
86 notice("Unknown quiz type '$xml->quiztype'");
88 } // end switch
89 return $questions;
92 function process_jcloze(&$xml, &$questions) {
93 // define default grade (per cloze gap)
94 $defaultgrade = 1;
95 $gap_count = 0;
97 // detect old Moodles (1.4 and earlier)
98 global $CFG, $db;
99 $moodle_14 = false;
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') {
103 $moodle_14 = true;
108 // xml tags for the start of the gap-fill exercise
109 $tags = 'data,gap-fill';
111 $x = 0;
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();
117 } else {
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
128 if ($moodle_14) {
129 $question->answers = array();
130 } else {
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
137 $q = 0;
138 while ($text = $xml->xml_value($tags, $exercise."[$q]")) {
139 // add next bit of text
140 $question->questiontext .= $this->hotpot_prepare_str($text);
142 // check for a gap
143 $question_record = $exercise."['question-record'][$q]['#']";
144 if ($xml->xml_value($tags, $question_record)) {
146 // add gap
147 $gap_count ++;
148 $positionkey = $q+1;
149 $question->questiontext .= '{#'.$positionkey.'}';
151 // initialize answer settings
152 if ($moodle_14) {
153 $question->answers[$q]->positionkey = $positionkey;
154 $question->answers[$q]->answertype = SHORTANSWER;
155 $question->answers[$q]->norm = $defaultgrade;
156 $question->answers[$q]->alternatives = array();
157 } else {
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();
166 $answers = array();
169 // add answers
170 $a = 0;
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]['#']"));
175 if ($text) {
176 // set score (0=0%, 1=100%)
177 $fraction = empty($correct) ? 0 : 1;
178 // store answer
179 if ($moodle_14) {
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;
184 } else {
185 $wrapped->answer[] = $text;
186 $wrapped->fraction[] = $fraction;
187 $wrapped->feedback[] = $feedback;
188 $answers[] = (empty($fraction) ? '' : '=').$text.(empty($feedback) ? '' : ('#'.$feedback));
191 $a++;
193 // compile answers into question text, if necessary
194 if ($moodle_14) {
195 // do nothing
196 } else {
197 $wrapped->questiontext = '{'.$defaultgrade.':SHORTANSWER:'.implode('~', $answers).'}';
198 $question->options->questions[] = $wrapped;
200 } // end if gap
201 $q++;
202 } // end while $text
204 // define total grade for this exercise
205 $question->defaultgrade = $gap_count * $defaultgrade;
207 $questions[] = $question;
208 $x++;
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';
216 $x = 0;
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();
225 } else {
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;
240 $x++;
244 function process_jmatch(&$xml, &$questions) {
245 // define default grade (per matched pair)
246 $defaultgrade = 1;
247 $match_count = 0;
249 // xml tags to the start of the matching exercise
250 $tags = 'data,matching-exercise';
252 $x = 0;
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();
258 } else {
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();
271 $p = 0;
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) {
276 $match_count++;
277 $question->subquestions[$p] = $this->hotpot_prepare_str($left);
278 $question->subanswers[$p] = $this->hotpot_prepare_str($right);
280 $p++;
282 $question->defaultgrade = $match_count * $defaultgrade;
283 $questions[] = $question;
284 $x++;
288 function process_jmix(&$xml, &$questions) {
289 // define default grade (per segment)
290 $defaultgrade = 1;
291 $segment_count = 0;
293 // xml tags to the start of the jumbled order exercise
294 $tags = 'data,jumbled-order-exercise';
296 $x = 0;
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();
302 } else {
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();
314 $i = 0;
315 $segments = array();
316 while ($segment = $xml->xml_value($tags, $exercise."['main-order'][0]['#']['segment'][$i]['#']")) {
317 $segments[] = $this->hotpot_prepare_str($segment);
318 $segment_count++;
319 $i++;
321 $answer = implode(' ', $segments);
323 $this->hotpot_seed_RNG();
324 shuffle($segments);
326 $question->questiontext = $this->hotpot_get_reading($xml);
327 $question->questiontext .= $this->hotpot_get_instructions($xml);
328 $question->questiontext .= ' &nbsp; <NOBR><B>[ &nbsp; '.implode(' &nbsp; ', $segments).' &nbsp; ]</B></NOBR>';
330 $a = 0;
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]['#']"));
336 $a++;
338 $question->defaultgrade = $segment_count * $defaultgrade;
339 $questions[] = $question;
340 $x++;
343 function process_jquiz(&$xml, &$questions) {
344 // define default grade (per question)
345 $defaultgrade = 1;
347 // xml tags to the start of the questions
348 $tags = 'data,questions';
350 $x = 0;
351 while (($exercise = "[$x]['#']") && $xml->xml_value($tags, $exercise)) {
352 // there is usually only one 'questions' object in a single exercise
354 $q = 0;
355 while (($question_record = $exercise."['question-record'][$q]['#']") && $xml->xml_value($tags, $question_record)) {
357 if (method_exists($this, 'defaultquestion')) {
358 $question = $this->defaultquestion();
359 } else {
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']")) {
371 // HP6 JQuiz
372 $answers = $question_record."['answers'][0]['#']";
373 } else {
374 // HP5 JBC or JQuiz
375 $answers = $question_record;
377 if($xml->xml_value($tags, $question_record."['question-type']")) {
378 // HP6 JQuiz
379 $type = $xml->xml_value($tags, $question_record."['question-type'][0]['#']");
380 // 1 : multiple choice
381 // 2 : short-answer
382 // 3 : hybrid
383 // 4 : multiple select
384 } else {
385 // HP5
386 switch ($xml->quiztype) {
387 case 'jbc':
388 $must_select_all = $xml->xml_value($tags, $question_record."['must-select-all'][0]['#']");
389 if (empty($must_select_all)) {
390 $type = 1; // multichoice
391 } else {
392 $type = 4; // multiselect
394 break;
395 case 'jquiz':
396 $type = 2; // shortanswer
397 break;
398 default:
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;
407 if ($type==4) {
408 $a = 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)) {
412 // do nothing
413 } else {
414 $no_of_correct_answers++;
416 $a++;
419 $a = 0;
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)) {
426 $fraction = 0;
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);
430 } else {
431 if ($xml->xml_value($tags, $answer."['percent-correct']")) {
432 // HP6 JQuiz
433 $percent = $xml->xml_value($tags, $answer."['percent-correct'][0]['#']");
434 $fraction = $percent/100;
435 } else {
436 // HP5 JBC or JQuiz
437 $fraction = 1;
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]['#']"));
446 $a++;
448 $questions[] = $question;
449 $q++;
451 $x++;
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');
465 if ($x || $flag) {
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');
472 if (empty($text)) {
473 $text = "Hot Potatoes $xml->quiztype";
475 return $this->hotpot_prepare_str($text);
477 function hotpot_get_reading(&$xml) {
478 $str = '';
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);
495 } // end class
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='') {
502 if (empty($str)) {
503 $this->xml = array();
504 } else {
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('&#x003C;'=>'<', '&#x003E;'=>'>', '&#x0026;'=>'&'));
522 // remove white space between <table>, <ul|OL|DL> and <OBJECT|EMBED> parts
523 // (so it doesn't get converted to <br />)
524 $htmltags = '('
525 . 'TABLE|/?CAPTION|/?COL|/?COLGROUP|/?TBODY|/?TFOOT|/?THEAD|/?TD|/?TH|/?TR'
526 . '|OL|UL|/?LI'
527 . '|DL|/?DT|/?DD'
528 . '|EMBED|OBJECT|APPLET|/?PARAM'
529 //. '|SELECT|/?OPTION'
530 //. '|FIELDSET|/?LEGEND'
531 //. '|FRAMESET|/?FRAME'
532 . ')'
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);
554 return $value;
556 function encode_cdata(&$str, $tag) {
558 // conversion tables
559 static $HTML_ENTITIES = array(
560 '&apos;' => "'",
561 '&quot;' => '"',
562 '&lt;' => '<',
563 '&gt;' => '>',
564 '&amp;' => '&',
566 static $ILLEGAL_STRINGS = array(
567 "\r" => '',
568 "\n" => '&lt;br /&gt;',
569 ']]>' => '&#93;&#93;&#62;',
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(
603 1=>array(0=>0),
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)
609 $dec = 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 = '(?:(<)|(&lt;)|(&amp;#x003C;))'; // left angle bracket
621 $tagclose = '(?(2)>|(?(3)&gt;|(?(4)&amp;#x003E;)))'; // right angle bracket (to match left angle bracket)
623 $space = '\s+'; // at least one space
624 $anychar = '(?:[^>]*?)'; // any character
626 $quoteopen = '("|&quot;|&amp;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) {
633 if ($tag=='param') {
634 $url = '\S+?\.\S+?'; // must include a filename and have no spaces
635 } else {
636 $url = '.*?';
638 $search = "%($tagopen$tag$space$anychar$attribute=$quoteopen)($url)($quoteclose$anychar$tagclose)%ise";
639 $str = preg_replace($search, $replace, $str);
642 return $str;
645 function hotpot_convert_relative_url($baseurl, $filename, $opentag, $url, $closetag, $stripslashes=true) {
646 if ($stripslashes) {
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 "&", "&amp;" or "&amp;#x0026;amp;"
654 if (preg_match('|^'.'\w+=[^&]+'.'('.'&((amp;#x0026;)?amp;)?'.'\w+=[^&]+)*'.'$|', $url)) {
655 $query = $url;
656 $url = '';
657 $fragment = '';
659 // parse the $url into $matches
660 // [1] path
661 // [2] query string, if any
662 // [3] anchor fragment, if any
663 } else if (preg_match('|^'.'([^?]*)'.'((?:\\?[^#]*)?)'.'((?:#.*)?)'.'$|', $url, $matches)) {
664 $url = $matches[1];
665 $query = $matches[2];
666 $fragment = $matches[3];
668 // there appears to be no query or fragment in this url
669 } else {
670 $query = '';
671 $fragment = '';
674 if ($url) {
675 $url = hotpot_convert_url($baseurl, $filename, $url, false);
678 if ($query) {
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;
686 return $url;
689 function hotpot_convert_url($baseurl, $filename, $url, $stripslashes=true) {
690 // maintain a cache of converted urls
691 static $HOTPOT_RELATIVE_URLS = array();
693 if ($stripslashes) {
694 $url = stripslashes($url);
697 // is this an absolute url? (or javascript pseudo url)
698 if (preg_match('%^(http://|/|javascript:)%i', $url)) {
699 // do nothing
701 // has this relative url already been converted?
702 } else if (isset($HOTPOT_RELATIVE_URLS[$url])) {
703 $url = $HOTPOT_RELATIVE_URLS[$url];
705 } else {
706 $relativeurl = $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);
716 $url = $matches[2];
719 // add subdirectory, $dir, to $baseurl, if necessary
720 if ($dir && $dir<>'.') {
721 $baseurl .= "$dir/";
724 // prefix $url with $baseurl
725 $url = "$baseurl$url";
727 // add url to cache
728 $HOTPOT_RELATIVE_URLS[$relativeurl] = $url;
730 return $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);