MDL-11515:
[moodle-linuxchix.git] / question / format / qti2 / format.php
blob42b49ad1734bd610da429ffaf2a354007b800ed9
1 <?php // $Id$
3 require_once("$CFG->dirroot/question/format/qti2/qt_common.php");
4 ////////////////////////////////////////////////////////////////////////////
5 /// IMS QTI 2.0 FORMAT
6 ///
7 /// HISTORY: created 28.01.2005 brian@mediagonal.ch
8 ////////////////////////////////////////////////////////////////////////////
10 // Based on format.php, included by ../../import.php
11 /**
12 * @package questionbank
13 * @subpackage importexport
15 define('CLOZE_TRAILING_TEXT_ID', 9999999);
17 class qformat_qti2 extends qformat_default {
19 var $lang;
21 function provide_export() {
22 return true;
25 function indent_xhtml($source, $indenter = ' ') {
26 // xml tidier-upper
27 // (c) Ari Koivula http://ventionline.com
29 // Remove all pre-existing formatting.
30 // Remove all newlines.
31 $source = str_replace("\n", '', $source);
32 $source = str_replace("\r", '', $source);
33 // Remove all tabs.
34 $source = str_replace("\t", '', $source);
35 // Remove all space after ">" and before "<".
36 $source = ereg_replace(">( )*", ">", $source);
37 $source = ereg_replace("( )*<", "<", $source);
39 // Iterate through the source.
40 $level = 0;
41 $source_len = strlen($source);
42 $pt = 0;
43 while ($pt < $source_len) {
44 if ($source{$pt} === '<') {
45 // We have entered a tag.
46 // Remember the point where the tag starts.
47 $started_at = $pt;
48 $tag_level = 1;
49 // If the second letter of the tag is "/", assume its an ending tag.
50 if ($source{$pt+1} === '/') {
51 $tag_level = -1;
53 // If the second letter of the tag is "!", assume its an "invisible" tag.
54 if ($source{$pt+1} === '!') {
55 $tag_level = 0;
57 // Iterate throught the source until the end of tag.
58 while ($source{$pt} !== '>') {
59 $pt++;
61 // If the second last letter is "/", assume its a self ending tag.
62 if ($source{$pt-1} === '/') {
63 $tag_level = 0;
65 $tag_lenght = $pt+1-$started_at;
67 // Decide the level of indention for this tag.
68 // If this was an ending tag, decrease indent level for this tag..
69 if ($tag_level === -1) {
70 $level--;
72 // Place the tag in an array with proper indention.
73 $array[] = str_repeat($indenter, $level).substr($source, $started_at, $tag_lenght);
74 // If this was a starting tag, increase the indent level after this tag.
75 if ($tag_level === 1) {
76 $level++;
78 // if it was a self closing tag, dont do shit.
80 // Were out of the tag.
81 // If next letter exists...
82 if (($pt+1) < $source_len) {
83 // ... and its not an "<".
84 if ($source{$pt+1} !== '<') {
85 $started_at = $pt+1;
86 // Iterate through the source until the start of new tag or until we reach the end of file.
87 while ($source{$pt} !== '<' && $pt < $source_len) {
88 $pt++;
90 // If we found a "<" (we didnt find the end of file)
91 if ($source{$pt} === '<') {
92 $tag_lenght = $pt-$started_at;
93 // Place the stuff in an array with proper indention.
94 $array[] = str_repeat($indenter, $level).substr($source, $started_at, $tag_lenght);
96 // If the next tag is "<", just advance pointer and let the tag indenter take care of it.
97 } else {
98 $pt++;
100 // If the next letter doesnt exist... Were done... well, almost..
101 } else {
102 break;
105 // Replace old source with the new one we just collected into our array.
106 $source = implode($array, "\n");
107 return $source;
110 function importpreprocess() {
111 global $CFG;
113 error("Sorry, importing this format is not yet implemented!",
114 "$CFG->wwwroot/mod/quiz/import.php?category=$category->id");
117 function exportpreprocess() {
118 global $CFG;
120 require_once("{$CFG->libdir}/smarty/Smarty.class.php");
122 // assign the language for the export: by parameter, SESSION, USER, or the default of 'en'
123 $lang = current_language();
124 $this->lang = $lang;
126 return parent::exportpreprocess();
130 function export_file_extension() {
131 // override default type so extension is .xml
133 return ".zip";
136 function get_qtype( $type_id ) {
137 // translates question type code number into actual name
139 switch( $type_id ) {
140 case TRUEFALSE:
141 $name = 'truefalse';
142 break;
143 case MULTICHOICE:
144 $name = 'multichoice';
145 break;
146 case SHORTANSWER:
147 $name = 'shortanswer';
148 break;
149 case NUMERICAL:
150 $name = 'numerical';
151 break;
152 case MATCH:
153 $name = 'matching';
154 break;
155 case DESCRIPTION:
156 $name = 'description';
157 break;
158 case MULTIANSWER:
159 $name = 'multianswer';
160 break;
161 default:
162 $name = 'Unknown';
164 return $name;
167 function writetext( $raw ) {
168 // generates <text></text> tags, processing raw text therein
170 // for now, don't allow any additional tags in text
171 // otherwise xml rules would probably get broken
172 $raw = strip_tags( $raw );
174 return "<text>$raw</text>\n";
179 * flattens $object['media'], copies $object['media'] to $path, and sets $object['mediamimetype']
181 * @param array &$object containing a field 'media'
182 * @param string $path the full path name to where the media files need to be copied
183 * @param int $courseid
184 * @return: mixed - true on success or in case of an empty media field, an error string if the file copy fails
186 function copy_and_flatten(&$object, $path, $courseid) {
187 global $CFG;
188 if (!empty($object['media'])) {
189 $location = $object['media'];
190 $object['media'] = $this->flatten_image_name($location);
191 if (!@copy("{$CFG->dataroot}/$courseid/$location", "$path/{$object['media']}")) {
192 return "Failed to copy {$CFG->dataroot}/$courseid/$location to $path/{$object['media']}";
194 if (empty($object['mediamimetype'])) {
195 $object['mediamimetype'] = mimeinfo('type', $object['media']);
198 return true;
201 * copies all files needed by the questions to the given $path, and flattens the file names
203 * @param array $questions the question objects
204 * @param string $path the full path name to where the media files need to be copied
205 * @param int $courseid
206 * @return mixed true on success, an array of error messages otherwise
208 function handle_questions_media(&$questions, $path, $courseid) {
209 global $CFG;
210 $errors = array();
211 foreach ($questions as $key=>$question) {
213 // todo: handle in-line media (specified in the question text)
214 if (!empty($question->image)) {
215 $location = $questions[$key]->image;
216 $questions[$key]->mediaurl = $this->flatten_image_name($location);
217 if (!@copy("{$CFG->dataroot}/$courseid/$location", "$path/{$questions[$key]->mediaurl}")) {
218 $errors[] = "Failed to copy {$CFG->dataroot}/$courseid/$location to $path/{$questions[$key]->mediaurl}";
220 if (empty($question->mediamimetype)) {
221 $questions[$key]->mediamimetype = mimeinfo('type', $question->image);
226 return empty($errors) ? true : $errors;
230 * exports the questions in a question category to the given location
232 * The parent class method was overridden because the IMS export consists of multiple files
234 * @param string $filename the directory name which will hold the exported files
235 * @return boolean - or errors out
237 function exportprocess() {
239 global $CFG;
240 $courseid = $this->course->id;
242 // create a directory for the exports (if not already existing)
243 if (!$export_dir = make_upload_directory($this->question_get_export_dir().'/'.$this->filename)) {
244 error( get_string('cannotcreatepath','quiz',$export_dir) );
246 $path = $CFG->dataroot.'/'.$this->question_get_export_dir().'/'.$this->filename;
248 // get the questions (from database) in this category
249 // $questions = get_records("question","category",$this->category->id);
250 $questions = get_questions_category( $this->category );
252 notify("Exporting ".count($questions)." questions.");
253 $count = 0;
255 // create the imsmanifest file
256 $smarty =& $this->init_smarty();
257 $this->add_qti_info($questions);
259 // copy files used by the main questions to the export directory
260 $result = $this->handle_questions_media($questions, $path, $courseid);
261 if ($result !== true) {
262 notify(implode("<br />", $result));
265 $manifestquestions = $this->objects_to_array($questions);
266 $manifestid = str_replace(array(':', '/'), array('-','_'), "question_category_{$this->category->id}---{$CFG->wwwroot}");
267 $smarty->assign('externalfiles', 1);
268 $smarty->assign('manifestidentifier', $manifestid);
269 $smarty->assign('quiztitle', "question_category_{$this->category->id}");
270 $smarty->assign('quizinfo', "All questions in category {$this->category->id}");
271 $smarty->assign('questions', $manifestquestions);
272 $smarty->assign('lang', $this->lang);
273 $smarty->error_reporting = 99;
274 $expout = $smarty->fetch('imsmanifest.tpl');
275 $filepath = $path.'/imsmanifest.xml';
276 if (empty($expout)) {
277 error("Unkown error - empty imsmanifest.xml");
279 if (!$fh=fopen($filepath,"w")) {
280 error("Cannot open for writing: $filepath");
282 if (!fwrite($fh, $expout)) {
283 error("Cannot write exported questions to $filepath");
285 fclose($fh);
287 // iterate through questions
288 foreach($questions as $question) {
290 // results are first written into string (and then to a file)
291 $count++;
292 echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
293 $expout = $this->writequestion( $question , null, true, $path) . "\n";
294 $expout = $this->presave_process( $expout );
296 $filepath = $path.'/'.$this->get_assesment_item_id($question) . ".xml";
297 if (!$fh=fopen($filepath,"w")) {
298 error("Cannot open for writing: $filepath");
300 if (!fwrite($fh, $expout)) {
301 error("Cannot write exported questions to $filepath");
303 fclose($fh);
307 // zip files into single export file
308 zip_files( array($path), "$path.zip" );
310 // remove the temporary directory
311 remove_dir( $path );
313 return true;
317 * exports a quiz (as opposed to exporting a category of questions)
319 * The parent class method was overridden because the IMS export consists of multiple files
321 * @param object $quiz
322 * @param array $questions - an array of question objects
323 * @param object $result - if set, contains result of calling quiz_grade_responses()
324 * @param string $redirect - a URL to redirect to in case of failure
325 * @param string $submiturl - the URL for the qti player to send the results to (e.g. attempt.php)
326 * @todo use $result in the ouput
328 function export_quiz($course, $quiz, $questions, $result, $redirect, $submiturl = null) {
329 $this->xml_entitize($course);
330 $this->xml_entitize($quiz);
331 $this->xml_entitize($questions);
332 $this->xml_entitize($result);
333 $this->xml_entitize($submiturl);
334 if (! $this->exportpreprocess(0, $course)) { // Do anything before that we need to
335 error("Error occurred during pre-processing!", $redirect);
337 if (! $this->exportprocess_quiz($quiz, $questions, $result, $submiturl, $course)) { // Process the export data
338 error("Error occurred during processing!", $redirect);
340 if (! $this->exportpostprocess()) { // In case anything needs to be done after
341 error("Error occurred during post-processing!", $redirect);
348 * This function is called to export a quiz (as opposed to exporting a category of questions)
350 * @uses $USER
351 * @param object $quiz
352 * @param array $questions - an array of question objects
353 * @param object $result - if set, contains result of calling quiz_grade_responses()
354 * @todo use $result in the ouput
356 function exportprocess_quiz($quiz, $questions, $result, $submiturl, $course) {
357 global $USER;
358 global $CFG;
360 $gradingmethod = array (1 => 'GRADEHIGHEST',
361 2 => 'GRADEAVERAGE',
362 3 => 'ATTEMPTFIRST' ,
363 4 => 'ATTEMPTLAST');
365 $questions = $this->quiz_export_prepare_questions($questions, $quiz->id, $course->id, $quiz->shuffleanswers);
367 $smarty =& $this->init_smarty();
368 $smarty->assign('questions', $questions);
370 // quiz level smarty variables
371 $manifestid = str_replace(array(':', '/'), array('-','_'), "quiz{$quiz->id}-{$CFG->wwwroot}");
372 $smarty->assign('manifestidentifier', $manifestid);
373 $smarty->assign('submiturl', $submiturl);
374 $smarty->assign('userid', $USER->id);
375 $smarty->assign('username', htmlspecialchars($USER->username, ENT_COMPAT, 'UTF-8'));
376 $smarty->assign('quiz_level_export', 1);
377 $smarty->assign('quiztitle', format_string($quiz->name,true)); //assigned specifically so as not to cause problems with category-level export
378 $smarty->assign('quiztimeopen', date('Y-m-d\TH:i:s', $quiz->timeopen)); // ditto
379 $smarty->assign('quiztimeclose', date('Y-m-d\TH:i:s', $quiz->timeclose)); // ditto
380 $smarty->assign('grademethod', $gradingmethod[$quiz->grademethod]);
381 $smarty->assign('quiz', $quiz);
382 $smarty->assign('course', $course);
383 $smarty->assign('lang', $this->lang);
384 $expout = $smarty->fetch('imsmanifest.tpl');
385 echo $expout;
386 return true;
393 * Prepares questions for quiz export
395 * The questions are changed as follows:
396 * - the question answers atached to the questions
397 * - image set to an http reference instead of a file path
398 * - qti specific info added
399 * - exporttext added, which contains an xml-formatted qti assesmentItem
401 * @param array $questions - an array of question objects
402 * @param int $quizid
403 * @return an array of question arrays
405 function quiz_export_prepare_questions($questions, $quizid, $courseid, $shuffleanswers = null) {
406 global $CFG;
407 // add the answers to the questions and format the image property
408 foreach ($questions as $key=>$question) {
409 $questions[$key] = get_question_data($question);
410 $questions[$key]->courseid = $courseid;
411 $questions[$key]->quizid = $quizid;
413 if ($question->image) {
415 if (empty($question->mediamimetype)) {
416 $questions[$key]->mediamimetype = mimeinfo('type',$question->image);
419 $localfile = (substr(strtolower($question->image), 0, 7) == 'http://') ? false : true;
421 if ($localfile) {
422 // create the http url that the player will need to access the file
423 if ($CFG->slasharguments) { // Use this method if possible for better caching
424 $questions[$key]->mediaurl = "$CFG->wwwroot/file.php/$question->image";
425 } else {
426 $questions[$key]->mediaurl = "$CFG->wwwroot/file.php?file=$question->image";
428 } else {
429 $questions[$key]->mediaurl = $question->image;
434 $this->add_qti_info($questions);
435 $questions = $this->questions_with_export_info($questions, $shuffleanswers);
436 $questions = $this->objects_to_array($questions);
437 return $questions;
441 * calls htmlspecialchars for each string field, to convert, for example, & to &amp;
443 * collections are processed recursively
445 * @param array $collection - an array or object or string
447 function xml_entitize(&$collection) {
448 if (is_array($collection)) {
449 foreach ($collection as $key=>$var) {
450 if (is_string($var)) {
451 $collection[$key]= htmlspecialchars($var, ENT_COMPAT, 'UTF-8');
452 } else if (is_array($var) || is_object($var)) {
453 $this->xml_entitize($collection[$key]);
456 } else if (is_object($collection)) {
457 $vars = get_object_vars($collection);
458 foreach ($vars as $key=>$var) {
459 if (is_string($var)) {
460 $collection->$key = htmlspecialchars($var, ENT_COMPAT, 'UTF-8');
461 } else if (is_array($var) || is_object($var)) {
462 $this->xml_entitize($collection->$key);
465 } else if (is_string($collection)) {
466 $collection = htmlspecialchars($collection, ENT_COMPAT, 'UTF-8');
471 * adds exporttext property to the questions
473 * Adds the qti export text to the questions
475 * @param array $questions - an array of question objects
476 * @return an array of question objects
478 function questions_with_export_info($questions, $shuffleanswers = null) {
479 $exportquestions = array();
480 foreach($questions as $key=>$question) {
481 $expout = $this->writequestion( $question , $shuffleanswers) . "\n";
482 $expout = $this->presave_process( $expout );
483 $key = $this->get_assesment_item_id($question);
484 $exportquestions[$key] = $question;
485 $exportquestions[$key]->exporttext = $expout;
487 return $exportquestions;
491 * Creates the export text for a question
493 * @todo handle in-line media (specified in the question/subquestion/answer text) for course-level exports
494 * @param object $question
495 * @param boolean $shuffleanswers whether or not to shuffle the answers
496 * @param boolean $courselevel whether or not this is a course-level export
497 * @param string $path provide the path to copy question media files to, if $courselevel == true
498 * @return string containing export text
500 function writequestion($question, $shuffleanswers = null, $courselevel = false, $path = '') {
501 // turns question into string
502 // question reflects database fields for general question and specific to type
503 global $CFG;
504 $expout = '';
505 //need to unencode the html entities in the questiontext field.
506 // the whole question object was earlier run throught htmlspecialchars in xml_entitize().
507 $question->questiontext = html_entity_decode($question->questiontext, ENT_COMPAT);
509 $hasimage = empty($question->image) ? 0 : 1;
510 $hassize = empty($question->mediax) ? 0 : 1;
512 $allowedtags = '<a><br><b><h1><h2><h3><h4><i><img><li><ol><strong><table><tr><td><th><u><ul><object>'; // all other tags will be stripped from question text
513 $smarty =& $this->init_smarty();
514 $assesmentitemid = $this->get_assesment_item_id($question);
515 $question_type = $this->get_qtype( $question->qtype );
516 $questionid = "question{$question->id}$question_type";
517 $smarty->assign('question_has_image', $hasimage);
518 $smarty->assign('hassize', $hassize);
519 $smarty->assign('questionid', $questionid);
520 $smarty->assign('assessmentitemidentifier', $assesmentitemid);
521 $smarty->assign('assessmentitemtitle', $question->name);
522 $smarty->assign('courselevelexport', $courselevel);
524 if ($question->qtype == MULTIANSWER) {
525 $question->questiontext = strip_tags($question->questiontext, $allowedtags . '<intro>');
526 $smarty->assign('questionText', $this->get_cloze_intro($question->questiontext));
527 } else {
528 $smarty->assign('questionText', strip_tags($question->questiontext, $allowedtags));
531 $smarty->assign('question', $question);
532 // the following two are left for compatibility; the templates should be changed, though, to make object tags for the questions
533 //$smarty->assign('questionimage', $question->image);
534 //$smarty->assign('questionimagealt', "image: $question->image");
536 // output depends on question type
537 switch($question->qtype) {
538 case TRUEFALSE:
539 $qanswers = $question->options->answers;
540 $answers[0] = (array)$qanswers['true'];
541 $answers[0]['answer'] = get_string("true", "quiz");
542 $answers[1] = (array)$qanswers['false'];
543 $answers[1]['answer'] = get_string("false", "quiz");
545 if (!empty($shuffleanswers)) {
546 $answers = $this->shuffle_things($answers);
549 if (isset($question->response)) {
550 $correctresponseid = $question->response[$questionid];
551 if ($answers[0]['id'] == $correctresponseid) {
552 $correctresponse = $answers[0];
553 } else {
554 $correctresponse = $answers[1];
557 else {
558 $correctresponse = '';
561 $smarty->assign('correctresponse', $correctresponse);
562 $smarty->assign('answers', $answers);
563 $expout = $smarty->fetch('choice.tpl');
564 break;
565 case MULTICHOICE:
566 $answers = $this->objects_to_array($question->options->answers);
567 if (!empty($shuffleanswers)) {
568 $answers = $this->shuffle_things($answers);
570 $correctresponses = $this->get_correct_answers($answers);
571 $correctcount = count($correctresponses);
574 $smarty->assign('responsedeclarationcardinality', $correctcount > 1 ? 'multiple' : 'single');
575 $smarty->assign('correctresponses', $correctresponses);
576 $smarty->assign('answers', $answers);
577 $smarty->assign('maxChoices', $question->options->single ? '1' : count($answers));
578 $expout = $smarty->fetch('choiceMultiple.tpl');
579 break;
580 case SHORTANSWER:
581 $answers = $this->objects_to_array($question->options->answers);
582 if (!empty($shuffleanswers)) {
583 $answers = $this->shuffle_things($answers);
586 $correctresponses = $this->get_correct_answers($answers);
587 $correctcount = count($correctresponses);
589 $smarty->assign('responsedeclarationcardinality', $correctcount > 1 ? 'multiple' : 'single');
590 $smarty->assign('correctresponses', $correctresponses);
591 $smarty->assign('answers', $answers);
592 $expout = $smarty->fetch('textEntry.tpl');
593 break;
594 case NUMERICAL:
595 $qanswer = array_pop( $question->options->answers );
596 $smarty->assign('lowerbound', $qanswer->answer - $qanswer->tolerance);
597 $smarty->assign('upperbound', $qanswer->answer + $qanswer->tolerance);
598 $smarty->assign('answer', $qanswer->answer);
599 $expout = $smarty->fetch('numerical.tpl');
600 break;
601 case MATCH:
602 $this->xml_entitize($question->options->subquestions);
603 $subquestions = $this->objects_to_array($question->options->subquestions);
604 if (!empty($shuffleanswers)) {
605 $subquestions = $this->shuffle_things($subquestions);
607 $setcount = count($subquestions);
609 $smarty->assign('setcount', $setcount);
610 $smarty->assign('matchsets', $subquestions);
611 $expout = $smarty->fetch('match.tpl');
612 break;
613 case DESCRIPTION:
614 $expout = $smarty->fetch('extendedText.tpl');
615 break;
616 // loss of get_answers() from quiz_embedded_close_qtype class during
617 // Gustav's refactor breaks MULTIANSWER badly - one for another day!!
619 case MULTIANSWER:
620 $answers = $this->get_cloze_answers_array($question);
621 $questions = $this->get_cloze_questions($question, $answers, $allowedtags);
623 $smarty->assign('cloze_trailing_text_id', CLOZE_TRAILING_TEXT_ID);
624 $smarty->assign('answers', $answers);
625 $smarty->assign('questions', $questions);
626 $expout = $smarty->fetch('composite.tpl');
627 break; */
628 default:
629 $smarty->assign('questionText', "This question type (Unknown: type $question_type) has not yet been implemented");
630 $expout = $smarty->fetch('notimplemented.tpl');
633 // run through xml tidy function
634 //$tidy_expout = $this->indent_xhtml( $expout, ' ' ) . "\n\n";
635 //return $tidy_expout;
636 return $expout;
640 * Gets an id to use for a qti assesment item
642 * @param object $question
643 * @return string containing a qti assesment item id
645 function get_assesment_item_id($question) {
646 return "question{$question->id}";
650 * gets the answers whose grade fraction > 0
652 * @param array $answers
653 * @return array (0-indexed) containing the answers whose grade fraction > 0
655 function get_correct_answers($answers)
657 $correctanswers = array();
658 foreach ($answers as $answer) {
659 if ($answer['fraction'] > 0) {
660 $correctanswers[] = $answer;
663 return $correctanswers;
667 * gets a new Smarty object, with the template and compile directories set
669 * @return object a smarty object
671 function & init_smarty() {
672 global $CFG;
674 // create smarty compile dir in dataroot
675 $path = $CFG->dataroot."/smarty_c";
676 if (!is_dir($path)) {
677 if (!mkdir($path, $CFG->directorypermissions)) {
678 error("Cannot create path: $path");
681 $smarty = new Smarty;
682 $smarty->template_dir = "{$CFG->dirroot}/question/format/qti2/templates";
683 $smarty->compile_dir = "$path";
684 return $smarty;
688 * converts an array of objects to an array of arrays (not recursively)
690 * @param array $objectarray
691 * @return array - an array of answer arrays
693 function objects_to_array($objectarray)
695 $arrayarray = array();
696 foreach ($objectarray as $object) {
697 $arrayarray[] = (array)$object;
699 return $arrayarray;
703 * gets a question's cloze answer objects as arrays containing only arrays and basic data types
705 * @param object $question
706 * @return array - an array of answer arrays
708 function get_cloze_answers_array($question) {
709 $answers = $this->get_answers($question);
710 $this->xml_entitize($answers);
711 foreach ($answers as $answerkey => $answer) {
712 $answers[$answerkey]->subanswers = $this->objects_to_array($answer->subanswers);
714 return $this->objects_to_array($answers);
718 * gets an array with text and question arrays for the given cloze question
720 * To make smarty processing easier, the returned text and question sub-arrays have an equal number of elements.
721 * If it is necessary to add a dummy element to the question sub-array, the question will be given an id of CLOZE_TRAILING_TEXT_ID.
723 * @param object $question
724 * @param array $answers - an array of arrays containing the question's answers
725 * @param string $allowabletags - tags not to strip out of the question text (e.g. '<i><br>')
726 * @return array with text and question arrays for the given cloze question
728 function get_cloze_questions($question, $answers, $allowabletags) {
729 $questiontext = strip_tags($question->questiontext, $allowabletags);
730 if (preg_match_all('/(.*){#([0-9]+)}/U', $questiontext, $matches)) {
731 // matches[1] contains the text inbetween the question blanks
732 // matches[2] contains the id of the question blanks (db: question_multianswer.positionkey)
734 // find any trailing text after the last {#XX} and add it to the array
735 if (preg_match('/.*{#[0-9]+}(.*)$/', $questiontext, $tail)) {
736 $matches[1][] = $tail[1];
737 $tailadded = true;
739 $questions['text'] = $matches[1];
740 $questions['question'] = array();
741 foreach ($matches[2] as $key => $questionid) {
742 foreach ($answers as $answer) {
743 if ($answer['positionkey'] == $questionid) {
744 $questions['question'][$key] = $answer;
745 break;
749 if ($tailadded) {
750 // to have a matching number of question and text array entries:
751 $questions['question'][] = array('id'=>CLOZE_TRAILING_TEXT_ID, 'answertype'=>SHORTANSWER);
754 } else {
755 $questions['text'][0] = $question->questiontext;
756 $questions['question'][0] = array('id'=>CLOZE_TRAILING_TEXT_ID, 'answertype'=>SHORTANSWER);
759 return $questions;
763 * strips out the <intro>...</intro> section, if any, and returns the text
765 * changes the text object passed to it.
767 * @param string $&text
768 * @return string the intro text, if there was an intro tag. '' otherwise.
770 function get_cloze_intro(&$text) {
771 if (preg_match('/(.*)?\<intro>(.+)?\<\/intro>(.*)/s', $text, $matches)) {
772 $text = $matches[1] . $matches[3];
773 return $matches[2];
775 else {
776 return '';
782 * adds qti metadata properties to the questions
784 * The passed array of questions is altered by this function
786 * @param &questions an array of question objects
788 function add_qti_info(&$questions)
790 foreach ($questions as $key=>$question) {
791 $questions[$key]->qtiinteractiontype = $this->get_qti_interaction_type($question->qtype);
792 $questions[$key]->qtiscoreable = $this->get_qti_scoreable($question);
793 $questions[$key]->qtisolutionavailable = $this->get_qti_solution_available($question);
799 * returns whether or not a given question is scoreable
801 * @param object $question
802 * @return boolean
804 function get_qti_scoreable($question) {
805 switch ($question->qtype) {
806 case DESCRIPTION:
807 return 'false';
808 default:
809 return 'true';
814 * returns whether or not a solution is available for a given question
816 * The results are based on whether or not Moodle stores answers for the given question type
818 * @param object $question
819 * @return boolean
821 function get_qti_solution_available($question) {
822 switch($question->qtype) {
823 case TRUEFALSE:
824 return 'true';
825 case MULTICHOICE:
826 return 'true';
827 case SHORTANSWER:
828 return 'true';
829 case NUMERICAL:
830 return 'true';
831 case MATCH:
832 return 'true';
833 case DESCRIPTION:
834 return 'false';
835 case MULTIANSWER:
836 return 'true';
837 default:
838 return 'true';
844 * maps a moodle question type to a qti 2.0 question type
846 * @param int type_id - the moodle question type
847 * @return string qti 2.0 question type
849 function get_qti_interaction_type($type_id) {
850 switch( $type_id ) {
851 case TRUEFALSE:
852 $name = 'choiceInteraction';
853 break;
854 case MULTICHOICE:
855 $name = 'choiceInteraction';
856 break;
857 case SHORTANSWER:
858 $name = 'textInteraction';
859 break;
860 case NUMERICAL:
861 $name = 'textInteraction';
862 break;
863 case MATCH:
864 $name = 'matchInteraction';
865 break;
866 case DESCRIPTION:
867 $name = 'extendedTextInteraction';
868 break;
869 case MULTIANSWER:
870 $name = 'textInteraction';
871 break;
872 default:
873 $name = 'textInteraction';
875 return $name;
879 * returns the given array, shuffled
882 * @param array $things
883 * @return array
885 function shuffle_things($things) {
886 $things = swapshuffle_assoc($things);
887 $oldthings = $things;
888 $things = array();
889 foreach ($oldthings as $key=>$value) {
890 $things[] = $value; // This loses the index key, but doesn't matter
892 return $things;
896 * returns a flattened image name - with all /, \ and : replaced with other characters
898 * used to convert a file or url to a qti-permissable identifier
900 * @param string name
901 * @return string
903 function flatten_image_name($name) {
904 return str_replace(array('/', '\\', ':'), array ('_','-','.'), $name);
907 function file_full_path($file, $courseid) {
908 global $CFG;
909 if (substr(strtolower($file), 0, 7) == 'http://') {
910 $url = $file;
911 } else if ($CFG->slasharguments) { // Use this method if possible for better caching
912 $url = "{$CFG->wwwroot}/file.php/$courseid/{$file}";
913 } else {
914 $url = "{$CFG->wwwroot}/file.php?file=/$courseid/{$file}";
916 return $url;