3 require_once("$CFG->dirroot/question/format/qti_two/qt_common.php");
4 ////////////////////////////////////////////////////////////////////////////
7 /// HISTORY: created 28.01.2005 brian@mediagonal.ch
8 ////////////////////////////////////////////////////////////////////////////
10 // Based on format.php, included by ../../import.php
12 * @package questionbank
13 * @subpackage importexport
15 define('CLOZE_TRAILING_TEXT_ID', 9999999);
17 class qformat_qti_two extends qformat_default {
21 function provide_export() {
25 function indent_xhtml($source, $indenter = ' ') {
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);
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.
41 $source_len = strlen($source);
43 while ($pt < $source_len) {
44 if ($source{$pt} === '<') {
45 // We have entered a tag.
46 // Remember the point where the tag starts.
49 // If the second letter of the tag is "/", assume its an ending tag.
50 if ($source{$pt+1} === '/') {
53 // If the second letter of the tag is "!", assume its an "invisible" tag.
54 if ($source{$pt+1} === '!') {
57 // Iterate throught the source until the end of tag.
58 while ($source{$pt} !== '>') {
61 // If the second last letter is "/", assume its a self ending tag.
62 if ($source{$pt-1} === '/') {
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) {
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) {
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} !== '<') {
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) {
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.
100 // If the next letter doesnt exist... Were done... well, almost..
105 // Replace old source with the new one we just collected into our array.
106 $source = implode($array, "\n");
110 function importpreprocess() {
113 error("Sorry, importing this format is not yet implemented!",
114 "$CFG->wwwroot/mod/quiz/import.php?category=$category->id");
117 function exportpreprocess() {
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();
126 return parent::exportpreprocess();
130 function export_file_extension() {
131 // override default type so extension is .xml
136 function get_qtype( $type_id ) {
137 // translates question type code number into actual name
144 $name = 'multichoice';
147 $name = 'shortanswer';
156 $name = 'description';
159 $name = 'multianswer';
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) {
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']);
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) {
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() {
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.");
255 // create the imsmanifest file
256 $smarty =& $this->init_smarty();
257 $this->add_qti_info($questions);
258 // copy files used by the main questions to the export directory
259 $result = $this->handle_questions_media($questions, $path, $courseid);
260 if ($result !== true) {
261 notify(implode("<br />", $result));
264 $manifestquestions = $this->objects_to_array($questions);
265 $manifestid = str_replace(array(':', '/'), array('-','_'), "question_category_{$this->category->id}---{$CFG->wwwroot}");
266 $smarty->assign('externalfiles', 1);
267 $smarty->assign('manifestidentifier', $manifestid);
268 $smarty->assign('quiztitle', "question_category_{$this->category->id}");
269 $smarty->assign('quizinfo', "All questions in category {$this->category->id}");
270 $smarty->assign('questions', $manifestquestions);
271 $smarty->assign('lang', $this->lang);
272 $smarty->error_reporting = 99;
273 $expout = $smarty->fetch('imsmanifest.tpl');
274 $filepath = $path.'/imsmanifest.xml';
275 if (empty($expout)) {
276 error("Unkown error - empty imsmanifest.xml");
278 if (!$fh=fopen($filepath,"w")) {
279 error("Cannot open for writing: $filepath");
281 if (!fwrite($fh, $expout)) {
282 error("Cannot write exported questions to $filepath");
286 // iterate through questions
287 foreach($questions as $question) {
289 // results are first written into string (and then to a file)
291 echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
292 $expout = $this->writequestion( $question , null, true, $path) . "\n";
293 $expout = $this->presave_process( $expout );
295 $filepath = $path.'/'.$this->get_assesment_item_id($question) . ".xml";
296 if (!$fh=fopen($filepath,"w")) {
297 error("Cannot open for writing: $filepath");
299 if (!fwrite($fh, $expout)) {
300 error("Cannot write exported questions to $filepath");
306 // zip files into single export file
307 zip_files( array($path), "$path.zip" );
309 // remove the temporary directory
316 * exports a quiz (as opposed to exporting a category of questions)
318 * The parent class method was overridden because the IMS export consists of multiple files
320 * @param object $quiz
321 * @param array $questions - an array of question objects
322 * @param object $result - if set, contains result of calling quiz_grade_responses()
323 * @param string $redirect - a URL to redirect to in case of failure
324 * @param string $submiturl - the URL for the qti player to send the results to (e.g. attempt.php)
325 * @todo use $result in the ouput
327 function export_quiz($course, $quiz, $questions, $result, $redirect, $submiturl = null) {
328 $this->xml_entitize($course);
329 $this->xml_entitize($quiz);
330 $this->xml_entitize($questions);
331 $this->xml_entitize($result);
332 $this->xml_entitize($submiturl);
333 if (! $this->exportpreprocess(0, $course)) { // Do anything before that we need to
334 error("Error occurred during pre-processing!", $redirect);
336 if (! $this->exportprocess_quiz($quiz, $questions, $result, $submiturl, $course)) { // Process the export data
337 error("Error occurred during processing!", $redirect);
339 if (! $this->exportpostprocess()) { // In case anything needs to be done after
340 error("Error occurred during post-processing!", $redirect);
347 * This function is called to export a quiz (as opposed to exporting a category of questions)
350 * @param object $quiz
351 * @param array $questions - an array of question objects
352 * @param object $result - if set, contains result of calling quiz_grade_responses()
353 * @todo use $result in the ouput
355 function exportprocess_quiz($quiz, $questions, $result, $submiturl, $course) {
359 $gradingmethod = array (1 => 'GRADEHIGHEST',
361 3 => 'ATTEMPTFIRST' ,
364 $questions = $this->quiz_export_prepare_questions($questions, $quiz->id, $course->id, $quiz->shuffleanswers);
366 $smarty =& $this->init_smarty();
367 $smarty->assign('questions', $questions);
369 // quiz level smarty variables
370 $manifestid = str_replace(array(':', '/'), array('-','_'), "quiz{$quiz->id}-{$CFG->wwwroot}");
371 $smarty->assign('manifestidentifier', $manifestid);
372 $smarty->assign('submiturl', $submiturl);
373 $smarty->assign('userid', $USER->id);
374 $smarty->assign('username', htmlspecialchars($USER->username, ENT_COMPAT, 'UTF-8'));
375 $smarty->assign('quiz_level_export', 1);
376 $smarty->assign('quiztitle', format_string($quiz->name,true)); //assigned specifically so as not to cause problems with category-level export
377 $smarty->assign('quiztimeopen', date('Y-m-d\TH:i:s', $quiz->timeopen)); // ditto
378 $smarty->assign('quiztimeclose', date('Y-m-d\TH:i:s', $quiz->timeclose)); // ditto
379 $smarty->assign('grademethod', $gradingmethod[$quiz->grademethod]);
380 $smarty->assign('quiz', $quiz);
381 $smarty->assign('course', $course);
382 $smarty->assign('lang', $this->lang);
383 $expout = $smarty->fetch('imsmanifest.tpl');
392 * Prepares questions for quiz export
394 * The questions are changed as follows:
395 * - the question answers atached to the questions
396 * - image set to an http reference instead of a file path
397 * - qti specific info added
398 * - exporttext added, which contains an xml-formatted qti assesmentItem
400 * @param array $questions - an array of question objects
402 * @return an array of question arrays
404 function quiz_export_prepare_questions($questions, $quizid, $courseid, $shuffleanswers = null) {
406 // add the answers to the questions and format the image property
407 foreach ($questions as $key=>$question) {
408 $questions[$key] = get_question_data($question);
409 $questions[$key]->courseid = $courseid;
410 $questions[$key]->quizid = $quizid;
412 if ($question->image) {
414 if (empty($question->mediamimetype)) {
415 $questions[$key]->mediamimetype = mimeinfo('type',$question->image);
418 $localfile = (substr(strtolower($question->image), 0, 7) == 'http://') ? false : true;
421 // create the http url that the player will need to access the file
422 if ($CFG->slasharguments) { // Use this method if possible for better caching
423 $questions[$key]->mediaurl = "$CFG->wwwroot/file.php/$question->image";
425 $questions[$key]->mediaurl = "$CFG->wwwroot/file.php?file=$question->image";
428 $questions[$key]->mediaurl = $question->image;
433 $this->add_qti_info($questions);
434 $questions = $this->questions_with_export_info($questions, $shuffleanswers);
435 $questions = $this->objects_to_array($questions);
440 * calls htmlspecialchars for each string field, to convert, for example, & to &
442 * collections are processed recursively
444 * @param array $collection - an array or object or string
446 function xml_entitize(&$collection) {
447 if (is_array($collection)) {
448 foreach ($collection as $key=>$var) {
449 if (is_string($var)) {
450 $collection[$key]= htmlspecialchars($var, ENT_COMPAT, 'UTF-8');
451 } else if (is_array($var) || is_object($var)) {
452 $this->xml_entitize($collection[$key]);
455 } else if (is_object($collection)) {
456 $vars = get_object_vars($collection);
457 foreach ($vars as $key=>$var) {
458 if (is_string($var)) {
459 $collection->$key = htmlspecialchars($var, ENT_COMPAT, 'UTF-8');
460 } else if (is_array($var) || is_object($var)) {
461 $this->xml_entitize($collection->$key);
464 } else if (is_string($collection)) {
465 $collection = htmlspecialchars($collection, ENT_COMPAT, 'UTF-8');
470 * adds exporttext property to the questions
472 * Adds the qti export text to the questions
474 * @param array $questions - an array of question objects
475 * @return an array of question objects
477 function questions_with_export_info($questions, $shuffleanswers = null) {
478 $exportquestions = array();
479 foreach($questions as $key=>$question) {
480 $expout = $this->writequestion( $question , $shuffleanswers) . "\n";
481 $expout = $this->presave_process( $expout );
482 $key = $this->get_assesment_item_id($question);
483 $exportquestions[$key] = $question;
484 $exportquestions[$key]->exporttext = $expout;
486 return $exportquestions;
490 * Creates the export text for a question
492 * @todo handle in-line media (specified in the question/subquestion/answer text) for course-level exports
493 * @param object $question
494 * @param boolean $shuffleanswers whether or not to shuffle the answers
495 * @param boolean $courselevel whether or not this is a course-level export
496 * @param string $path provide the path to copy question media files to, if $courselevel == true
497 * @return string containing export text
499 function writequestion($question, $shuffleanswers = null, $courselevel = false, $path = '') {
500 // turns question into string
501 // question reflects database fields for general question and specific to type
504 //need to unencode the html entities in the questiontext field.
505 // the whole question object was earlier run throught htmlspecialchars in xml_entitize().
506 $question->questiontext = html_entity_decode($question->questiontext, ENT_COMPAT);
508 $hasimage = empty($question->image) ? 0 : 1;
509 $hassize = empty($question->mediax) ? 0 : 1;
511 $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
512 $smarty =& $this->init_smarty();
513 $assesmentitemid = $this->get_assesment_item_id($question);
514 $question_type = $this->get_qtype( $question->qtype );
515 $questionid = "question{$question->id}$question_type";
516 $smarty->assign('question_has_image', $hasimage);
517 $smarty->assign('hassize', $hassize);
518 $smarty->assign('questionid', $questionid);
519 $smarty->assign('assessmentitemidentifier', $assesmentitemid);
520 $smarty->assign('assessmentitemtitle', $question->name);
521 $smarty->assign('courselevelexport', $courselevel);
523 if ($question->qtype == MULTIANSWER) {
524 $question->questiontext = strip_tags($question->questiontext, $allowedtags . '<intro>');
525 $smarty->assign('questionText', $this->get_cloze_intro($question->questiontext));
527 $smarty->assign('questionText', strip_tags($question->questiontext, $allowedtags));
530 $smarty->assign('question', $question);
531 // the following two are left for compatibility; the templates should be changed, though, to make object tags for the questions
532 //$smarty->assign('questionimage', $question->image);
533 //$smarty->assign('questionimagealt', "image: $question->image");
535 // output depends on question type
536 switch($question->qtype) {
538 $qanswers = $question->options->answers;
539 $answers[0] = (array)$qanswers['true'];
540 $answers[0]['answer'] = get_string("true", "quiz");
541 $answers[1] = (array)$qanswers['false'];
542 $answers[1]['answer'] = get_string("false", "quiz");
544 if (!empty($shuffleanswers)) {
545 $answers = $this->shuffle_things($answers);
548 if (isset($question->response)) {
549 $correctresponseid = $question->response[$questionid];
550 if ($answers[0]['id'] == $correctresponseid) {
551 $correctresponse = $answers[0];
553 $correctresponse = $answers[1];
557 $correctresponse = '';
560 $smarty->assign('correctresponse', $correctresponse);
561 $smarty->assign('answers', $answers);
562 $expout = $smarty->fetch('choice.tpl');
565 $answers = $this->objects_to_array($question->options->answers);
566 $correctresponses = $this->get_correct_answers($answers);
567 $correctcount = count($correctresponses);
568 $smarty->assign('responsedeclarationcardinality', $question->options->single ? 'single' : 'multiple');
569 $smarty->assign('operator', $question->options->single ? 'match' : 'member');
570 $smarty->assign('correctresponses', $correctresponses);
571 $smarty->assign('answers', $answers);
572 $smarty->assign('maxChoices', $question->options->single ? '1' : count($answers));
573 $smarty->assign('maxChoices', $question->options->single ? '1' : count($answers));
574 $smarty->assign('shuffle', empty($shuffleanswers) ? 'false' : 'true');
575 $smarty->assign('generalfeedback', $question->generalfeedback);
576 $smarty->assign('correctfeedback', $question->options->correctfeedback);
577 $smarty->assign('partiallycorrectfeedback', $question->options->partiallycorrectfeedback);
578 $smarty->assign('incorrectfeedback', $question->options->incorrectfeedback);
579 $expout = $smarty->fetch('choiceMultiple.tpl');
582 $answers = $this->objects_to_array($question->options->answers);
583 if (!empty($shuffleanswers)) {
584 $answers = $this->shuffle_things($answers);
587 $correctresponses = $this->get_correct_answers($answers);
588 $correctcount = count($correctresponses);
590 $smarty->assign('responsedeclarationcardinality', $correctcount > 1 ? 'multiple' : 'single');
591 $smarty->assign('correctresponses', $correctresponses);
592 $smarty->assign('answers', $answers);
593 $expout = $smarty->fetch('textEntry.tpl');
596 $qanswer = array_pop( $question->options->answers );
597 $smarty->assign('lowerbound', $qanswer->answer - $qanswer->tolerance);
598 $smarty->assign('upperbound', $qanswer->answer + $qanswer->tolerance);
599 $smarty->assign('answer', $qanswer->answer);
600 $expout = $smarty->fetch('numerical.tpl');
603 $this->xml_entitize($question->options->subquestions);
604 $subquestions = $this->objects_to_array($question->options->subquestions);
605 if (!empty($shuffleanswers)) {
606 $subquestions = $this->shuffle_things($subquestions);
608 $setcount = count($subquestions);
610 $smarty->assign('setcount', $setcount);
611 $smarty->assign('matchsets', $subquestions);
612 $expout = $smarty->fetch('match.tpl');
615 $expout = $smarty->fetch('extendedText.tpl');
617 // loss of get_answers() from quiz_embedded_close_qtype class during
618 // Gustav's refactor breaks MULTIANSWER badly - one for another day!!
621 $answers = $this->get_cloze_answers_array($question);
622 $questions = $this->get_cloze_questions($question, $answers, $allowedtags);
624 $smarty->assign('cloze_trailing_text_id', CLOZE_TRAILING_TEXT_ID);
625 $smarty->assign('answers', $answers);
626 $smarty->assign('questions', $questions);
627 $expout = $smarty->fetch('composite.tpl');
630 $smarty->assign('questionText', "This question type (Unknown: type $question_type) has not yet been implemented");
631 $expout = $smarty->fetch('notimplemented.tpl');
634 // run through xml tidy function
635 //$tidy_expout = $this->indent_xhtml( $expout, ' ' ) . "\n\n";
636 //return $tidy_expout;
641 * Gets an id to use for a qti assesment item
643 * @param object $question
644 * @return string containing a qti assesment item id
646 function get_assesment_item_id($question) {
647 return "question{$question->id}";
651 * gets the answers whose grade fraction > 0
653 * @param array $answers
654 * @return array (0-indexed) containing the answers whose grade fraction > 0
656 function get_correct_answers($answers)
658 $correctanswers = array();
659 foreach ($answers as $answer) {
660 if ($answer['fraction'] > 0) {
661 $correctanswers[] = $answer;
664 return $correctanswers;
668 * gets a new Smarty object, with the template and compile directories set
670 * @return object a smarty object
672 function & init_smarty() {
675 // create smarty compile dir in dataroot
676 $path = $CFG->dataroot."/smarty_c";
677 if (!is_dir($path)) {
678 if (!mkdir($path, $CFG->directorypermissions)) {
679 error("Cannot create path: $path");
682 $smarty = new Smarty;
683 $smarty->template_dir = "{$CFG->dirroot}/question/format/qti_two/templates";
684 $smarty->compile_dir = "$path";
689 * converts an array of objects to an array of arrays (not recursively)
691 * @param array $objectarray
692 * @return array - an array of answer arrays
694 function objects_to_array($objectarray)
696 $arrayarray = array();
697 foreach ($objectarray as $object) {
698 $arrayarray[] = (array)$object;
704 * gets a question's cloze answer objects as arrays containing only arrays and basic data types
706 * @param object $question
707 * @return array - an array of answer arrays
709 function get_cloze_answers_array($question) {
710 $answers = $this->get_answers($question);
711 $this->xml_entitize($answers);
712 foreach ($answers as $answerkey => $answer) {
713 $answers[$answerkey]->subanswers = $this->objects_to_array($answer->subanswers);
715 return $this->objects_to_array($answers);
719 * gets an array with text and question arrays for the given cloze question
721 * To make smarty processing easier, the returned text and question sub-arrays have an equal number of elements.
722 * 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.
724 * @param object $question
725 * @param array $answers - an array of arrays containing the question's answers
726 * @param string $allowabletags - tags not to strip out of the question text (e.g. '<i><br>')
727 * @return array with text and question arrays for the given cloze question
729 function get_cloze_questions($question, $answers, $allowabletags) {
730 $questiontext = strip_tags($question->questiontext, $allowabletags);
731 if (preg_match_all('/(.*){#([0-9]+)}/U', $questiontext, $matches)) {
732 // matches[1] contains the text inbetween the question blanks
733 // matches[2] contains the id of the question blanks (db: question_multianswer.positionkey)
735 // find any trailing text after the last {#XX} and add it to the array
736 if (preg_match('/.*{#[0-9]+}(.*)$/', $questiontext, $tail)) {
737 $matches[1][] = $tail[1];
740 $questions['text'] = $matches[1];
741 $questions['question'] = array();
742 foreach ($matches[2] as $key => $questionid) {
743 foreach ($answers as $answer) {
744 if ($answer['positionkey'] == $questionid) {
745 $questions['question'][$key] = $answer;
751 // to have a matching number of question and text array entries:
752 $questions['question'][] = array('id'=>CLOZE_TRAILING_TEXT_ID, 'answertype'=>SHORTANSWER);
756 $questions['text'][0] = $question->questiontext;
757 $questions['question'][0] = array('id'=>CLOZE_TRAILING_TEXT_ID, 'answertype'=>SHORTANSWER);
764 * strips out the <intro>...</intro> section, if any, and returns the text
766 * changes the text object passed to it.
768 * @param string $&text
769 * @return string the intro text, if there was an intro tag. '' otherwise.
771 function get_cloze_intro(&$text) {
772 if (preg_match('/(.*)?\<intro>(.+)?\<\/intro>(.*)/s', $text, $matches)) {
773 $text = $matches[1] . $matches[3];
783 * adds qti metadata properties to the questions
785 * The passed array of questions is altered by this function
787 * @param &questions an array of question objects
789 function add_qti_info(&$questions)
791 foreach ($questions as $key=>$question) {
792 $questions[$key]->qtiinteractiontype = $this->get_qti_interaction_type($question->qtype);
793 $questions[$key]->qtiscoreable = $this->get_qti_scoreable($question);
794 $questions[$key]->qtisolutionavailable = $this->get_qti_solution_available($question);
800 * returns whether or not a given question is scoreable
802 * @param object $question
805 function get_qti_scoreable($question) {
806 switch ($question->qtype) {
815 * returns whether or not a solution is available for a given question
817 * The results are based on whether or not Moodle stores answers for the given question type
819 * @param object $question
822 function get_qti_solution_available($question) {
823 switch($question->qtype) {
845 * maps a moodle question type to a qti 2.0 question type
847 * @param int type_id - the moodle question type
848 * @return string qti 2.0 question type
850 function get_qti_interaction_type($type_id) {
853 $name = 'choiceInteraction';
856 $name = 'choiceInteraction';
859 $name = 'textInteraction';
862 $name = 'textInteraction';
865 $name = 'matchInteraction';
868 $name = 'extendedTextInteraction';
871 $name = 'textInteraction';
874 $name = 'textInteraction';
880 * returns the given array, shuffled
883 * @param array $things
886 function shuffle_things($things) {
887 $things = swapshuffle_assoc($things);
888 $oldthings = $things;
890 foreach ($oldthings as $key=>$value) {
891 $things[] = $value; // This loses the index key, but doesn't matter
897 * returns a flattened image name - with all /, \ and : replaced with other characters
899 * used to convert a file or url to a qti-permissable identifier
904 function flatten_image_name($name) {
905 return str_replace(array('/', '\\', ':'), array ('_','-','.'), $name);
908 function file_full_path($file, $courseid) {
910 if (substr(strtolower($file), 0, 7) == 'http://') {
912 } else if ($CFG->slasharguments) { // Use this method if possible for better caching
913 $url = "{$CFG->wwwroot}/file.php/$courseid/{$file}";
915 $url = "{$CFG->wwwroot}/file.php?file=/$courseid/{$file}";