updated language detection
[moodle.git] / question / format / qti2 / format.php
CommitLineData
a3182bd7 1<?php // $Id$
f55b3d10 2require_once("$CFG->dirroot/question/format/qti2/qt_common.php");
84769fd8 3////////////////////////////////////////////////////////////////////////////
4/// IMS QTI 2.0 FORMAT
5///
6/// HISTORY: created 28.01.2005 brian@mediagonal.ch
7////////////////////////////////////////////////////////////////////////////
8
9// Based on format.php, included by ../../import.php
10
11define('CLOZE_TRAILING_TEXT_ID', 9999999);
12
f5565b69 13class qformat_qti2 extends qformat_default {
84769fd8 14
15 var $lang;
16
17 function provide_export() {
18 return true;
19 }
20
21 function indent_xhtml($source, $indenter = ' ') {
22 // xml tidier-upper
23 // (c) Ari Koivula http://ventionline.com
24
25 // Remove all pre-existing formatting.
26 // Remove all newlines.
27 $source = str_replace("\n", '', $source);
28 $source = str_replace("\r", '', $source);
29 // Remove all tabs.
30 $source = str_replace("\t", '', $source);
31 // Remove all space after ">" and before "<".
32 $source = ereg_replace(">( )*", ">", $source);
33 $source = ereg_replace("( )*<", "<", $source);
34
35 // Iterate through the source.
36 $level = 0;
37 $source_len = strlen($source);
38 $pt = 0;
39 while ($pt < $source_len) {
40 if ($source{$pt} === '<') {
41 // We have entered a tag.
42 // Remember the point where the tag starts.
43 $started_at = $pt;
44 $tag_level = 1;
45 // If the second letter of the tag is "/", assume its an ending tag.
46 if ($source{$pt+1} === '/') {
47 $tag_level = -1;
48 }
49 // If the second letter of the tag is "!", assume its an "invisible" tag.
50 if ($source{$pt+1} === '!') {
51 $tag_level = 0;
52 }
53 // Iterate throught the source until the end of tag.
54 while ($source{$pt} !== '>') {
55 $pt++;
56 }
57 // If the second last letter is "/", assume its a self ending tag.
58 if ($source{$pt-1} === '/') {
59 $tag_level = 0;
60 }
61 $tag_lenght = $pt+1-$started_at;
62
63 // Decide the level of indention for this tag.
64 // If this was an ending tag, decrease indent level for this tag..
65 if ($tag_level === -1) {
66 $level--;
67 }
68 // Place the tag in an array with proper indention.
69 $array[] = str_repeat($indenter, $level).substr($source, $started_at, $tag_lenght);
70 // If this was a starting tag, increase the indent level after this tag.
71 if ($tag_level === 1) {
72 $level++;
73 }
74 // if it was a self closing tag, dont do shit.
75 }
76 // Were out of the tag.
77 // If next letter exists...
78 if (($pt+1) < $source_len) {
79 // ... and its not an "<".
80 if ($source{$pt+1} !== '<') {
81 $started_at = $pt+1;
82 // Iterate through the source until the start of new tag or until we reach the end of file.
83 while ($source{$pt} !== '<' && $pt < $source_len) {
84 $pt++;
85 }
86 // If we found a "<" (we didnt find the end of file)
87 if ($source{$pt} === '<') {
88 $tag_lenght = $pt-$started_at;
89 // Place the stuff in an array with proper indention.
90 $array[] = str_repeat($indenter, $level).substr($source, $started_at, $tag_lenght);
91 }
92 // If the next tag is "<", just advance pointer and let the tag indenter take care of it.
93 } else {
94 $pt++;
95 }
96 // If the next letter doesnt exist... Were done... well, almost..
97 } else {
98 break;
99 }
100 }
101 // Replace old source with the new one we just collected into our array.
102 $source = implode($array, "\n");
103 return $source;
104 }
105
106 function importpreprocess($category) {
107 global $CFG;
108
109 error("Sorry, importing this format is not yet implemented!",
110 "$CFG->wwwroot/mod/quiz/import.php?category=$category->id");
111 }
112
113 function exportpreprocess($category, $course, $lang = null) {
114 global $CFG;
115
116 require_once("{$CFG->libdir}/smarty/Smarty.class.php");
117
118 // assign the language for the export: by parameter, SESSION, USER, or the default of 'en'
119 if (is_null($lang)) {
a3182bd7 120 $lang = current_language();
84769fd8 121 }
122 $this->lang = $lang;
123
124 return parent::exportpreprocess($category, $course);
125 }
126
127
128 function export_file_extension() {
129 // override default type so extension is .xml
130
131 return ".zip";
132 }
133
134 function get_qtype( $type_id ) {
135 // translates question type code number into actual name
136
137 switch( $type_id ) {
138 case TRUEFALSE:
139 $name = 'truefalse';
140 break;
141 case MULTICHOICE:
142 $name = 'multichoice';
143 break;
144 case SHORTANSWER:
145 $name = 'shortanswer';
146 break;
147 case NUMERICAL:
148 $name = 'numerical';
149 break;
150 case MATCH:
151 $name = 'matching';
152 break;
153 case DESCRIPTION:
154 $name = 'description';
155 break;
156 case MULTIANSWER:
157 $name = 'multianswer';
158 break;
159 default:
160 $name = 'Unknown';
161 }
162 return $name;
163 }
164
165 function writetext( $raw ) {
166 // generates <text></text> tags, processing raw text therein
167
168 // for now, don't allow any additional tags in text
169 // otherwise xml rules would probably get broken
170 $raw = strip_tags( $raw );
171
172 return "<text>$raw</text>\n";
173 }
174
175
176/**
177 * flattens $object['media'], copies $object['media'] to $path, and sets $object['mediamimetype']
178 *
179 * @param array &$object containing a field 'media'
180 * @param string $path the full path name to where the media files need to be copied
181 * @param int $courseid
182 * @return: mixed - true on success or in case of an empty media field, an error string if the file copy fails
183 */
184function copy_and_flatten(&$object, $path, $courseid) {
185 global $CFG;
186 if (!empty($object['media'])) {
187 $location = $object['media'];
188 $object['media'] = $this->flatten_image_name($location);
189 if (!@copy("{$CFG->dataroot}/$courseid/$location", "$path/{$object['media']}")) {
190 return "Failed to copy {$CFG->dataroot}/$courseid/$location to $path/{$object['media']}";
191 }
192 if (empty($object['mediamimetype'])) {
193 $object['mediamimetype'] = mimeinfo('type', $object['media']);
194 }
195 }
196 return true;
197}
198/**
199 * copies all files needed by the questions to the given $path, and flattens the file names
200 *
201 * @param array $questions the question objects
202 * @param string $path the full path name to where the media files need to be copied
203 * @param int $courseid
204 * @return mixed true on success, an array of error messages otherwise
205 */
206function handle_questions_media(&$questions, $path, $courseid) {
207 global $CFG;
208 $errors = array();
209 foreach ($questions as $key=>$question) {
210
211 // todo: handle in-line media (specified in the question text)
212 if (!empty($question->image)) {
213 $location = $questions[$key]->image;
214 $questions[$key]->mediaurl = $this->flatten_image_name($location);
215 if (!@copy("{$CFG->dataroot}/$courseid/$location", "$path/{$questions[$key]->mediaurl}")) {
216 $errors[] = "Failed to copy {$CFG->dataroot}/$courseid/$location to $path/{$questions[$key]->mediaurl}";
217 }
218 if (empty($question->mediamimetype)) {
219 $questions[$key]->mediamimetype = mimeinfo('type', $question->image);
220 }
221 }
222 }
223
224 return empty($errors) ? true : $errors;
225}
226
227/**
228 * exports the questions in a question category to the given location
229 *
230 * The parent class method was overridden because the IMS export consists of multiple files
231 *
232 * @param string $filename the directory name which will hold the exported files
233 * @return boolean - or errors out
234 */
235 function exportprocess($filename) {
236
237 global $CFG;
238
239 // create a directory for the exports (if not already existing)
240 $dirname = get_string("exportfilename","quiz");
241 $courseid = $this->course->id;
242 $path = $CFG->dataroot.'/'.$courseid.'/'.$dirname;
243 if (!is_dir($path)) {
244 if (!mkdir($path, $CFG->directorypermissions)) {
245 error("Cannot create path: $path");
246 }
247 }
248 // create directory for this particular IMS export, if not already existing
249 $path = $path."/".$filename;
250 if (!is_dir($path)) {
251 if (!mkdir($path, $CFG->directorypermissions)) {
252 error("Cannot create path: $path");
253 }
254 }
255
256 // get the questions (from database) in this category
062f1125 257 // $questions = get_records("question","category",$this->category->id);
84769fd8 258 $questions = get_questions_category( $this->category );
259
260 notify("Exporting ".count($questions)." questions.");
261 $count = 0;
262
263 // create the imsmanifest file
264 $smarty =& $this->init_smarty();
265 $this->add_qti_info($questions);
266
267 // copy files used by the main questions to the export directory
268 $result = $this->handle_questions_media($questions, $path, $courseid);
269 if ($result !== true) {
270 notify(implode("<br />", $result));
271 }
272
273 $manifestquestions = $this->objects_to_array($questions);
274 $manifestid = str_replace(array(':', '/'), array('-','_'), "question_category_{$this->category->id}---{$CFG->wwwroot}");
275 $smarty->assign('externalfiles', 1);
276 $smarty->assign('manifestidentifier', $manifestid);
277 $smarty->assign('quiztitle', "question_category_{$this->category->id}");
278 $smarty->assign('quizinfo', "All questions in category {$this->category->id}");
279 $smarty->assign('questions', $manifestquestions);
280 $smarty->assign('lang', $this->lang);
281 $smarty->error_reporting = 99;
282 $expout = $smarty->fetch('imsmanifest.tpl');
283 $filepath = $path.'/imsmanifest.xml';
284 if (!$fh=fopen($filepath,"w")) {
285 error("Cannot open for writing: $filepath");
286 }
287 if (!fwrite($fh, $expout)) {
288 error("Cannot write exported questions to $filepath");
289 }
290 fclose($fh);
291
292 // iterate through questions
293 foreach($questions as $question) {
294
295 // results are first written into string (and then to a file)
296 $count++;
297 echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
298 $expout = $this->writequestion( $question , null, true, $path) . "\n";
299 $expout = $this->presave_process( $expout );
300
301 $filepath = $path.'/'.$this->get_assesment_item_id($question) . ".xml";
302 if (!$fh=fopen($filepath,"w")) {
303 error("Cannot open for writing: $filepath");
304 }
305 if (!fwrite($fh, $expout)) {
306 error("Cannot write exported questions to $filepath");
307 }
308 fclose($fh);
309
310 }
311
312 // zip files into single export file
313 zip_files( array($path), "$path.zip" );
314
315 // remove the temporary directory
316 delDirContents( $path );
317
318 return true;
319 }
320
321/**
322 * exports a quiz (as opposed to exporting a category of questions)
323 *
324 * The parent class method was overridden because the IMS export consists of multiple files
325 *
326 * @param object $quiz
327 * @param array $questions - an array of question objects
328 * @param object $result - if set, contains result of calling quiz_grade_responses()
329 * @param string $redirect - a URL to redirect to in case of failure
330 * @param string $submiturl - the URL for the qti player to send the results to (e.g. attempt.php)
331 * @todo use $result in the ouput
332 */
333 function export_quiz($course, $quiz, $questions, $result, $redirect, $submiturl = null) {
334 $this->xml_entitize($course);
335 $this->xml_entitize($quiz);
336 $this->xml_entitize($questions);
337 $this->xml_entitize($result);
338 $this->xml_entitize($submiturl);
339 if (! $this->exportpreprocess(0, $course)) { // Do anything before that we need to
340 error("Error occurred during pre-processing!", $redirect);
341 }
342 if (! $this->exportprocess_quiz($quiz, $questions, $result, $submiturl, $course)) { // Process the export data
343 error("Error occurred during processing!", $redirect);
344 }
345 if (! $this->exportpostprocess()) { // In case anything needs to be done after
346 error("Error occurred during post-processing!", $redirect);
347 }
348
349 }
350
351
352/**
353 * This function is called to export a quiz (as opposed to exporting a category of questions)
354 *
355 * @uses $USER
356 * @param object $quiz
357 * @param array $questions - an array of question objects
358 * @param object $result - if set, contains result of calling quiz_grade_responses()
359 * @todo use $result in the ouput
360 */
361 function exportprocess_quiz($quiz, $questions, $result, $submiturl, $course) {
362 global $USER;
363 global $CFG;
364
365 $gradingmethod = array (1 => 'GRADEHIGHEST',
366 2 => 'GRADEAVERAGE',
367 3 => 'ATTEMPTFIRST' ,
368 4 => 'ATTEMPTLAST');
369
370 $questions = $this->quiz_export_prepare_questions($questions, $quiz->id, $course->id, $quiz->shuffleanswers);
371
372 $smarty =& $this->init_smarty();
373 $smarty->assign('questions', $questions);
374
375 // quiz level smarty variables
376 $manifestid = str_replace(array(':', '/'), array('-','_'), "quiz{$quiz->id}-{$CFG->wwwroot}");
377 $smarty->assign('manifestidentifier', $manifestid);
378 $smarty->assign('submiturl', $submiturl);
379 $smarty->assign('userid', $USER->id);
380 $smarty->assign('username', htmlspecialchars($USER->username, ENT_COMPAT, 'UTF-8'));
381 $smarty->assign('quiz_level_export', 1);
382 $smarty->assign('quiztitle', format_string($quiz->name,true)); //assigned specifically so as not to cause problems with category-level export
383 $smarty->assign('quiztimeopen', date('Y-m-d\TH:i:s', $quiz->timeopen)); // ditto
384 $smarty->assign('quiztimeclose', date('Y-m-d\TH:i:s', $quiz->timeclose)); // ditto
385 $smarty->assign('grademethod', $gradingmethod[$quiz->grademethod]);
386 $smarty->assign('quiz', $quiz);
387 $smarty->assign('course', $course);
388 $smarty->assign('lang', $this->lang);
389 $expout = $smarty->fetch('imsmanifest.tpl');
390 echo utf8_encode($expout);
391 return true;
392 }
393
394
395
396
397/**
398 * Prepares questions for quiz export
399 *
400 * The questions are changed as follows:
401 * - the question answers atached to the questions
402 * - image set to an http reference instead of a file path
403 * - qti specific info added
404 * - exporttext added, which contains an xml-formatted qti assesmentItem
405 *
406 * @param array $questions - an array of question objects
407 * @param int $quizid
408 * @return an array of question arrays
409 */
410 function quiz_export_prepare_questions($questions, $quizid, $courseid, $shuffleanswers = null) {
411 global $CFG;
412 // add the answers to the questions and format the image property
413 foreach ($questions as $key=>$question) {
414 $questions[$key] = get_question_data($question);
415 $questions[$key]->courseid = $courseid;
416 $questions[$key]->quizid = $quizid;
417
418 if ($question->image) {
419
420 if (empty($question->mediamimetype)) {
421 $questions[$key]->mediamimetype = mimeinfo('type',$question->image);
422 }
423
424 $localfile = (substr(strtolower($question->image), 0, 7) == 'http://') ? false : true;
425
426 if ($localfile) {
427 // create the http url that the player will need to access the file
428 if ($CFG->slasharguments) { // Use this method if possible for better caching
429 $questions[$key]->mediaurl = "$CFG->wwwroot/file.php/$question->image";
430 } else {
431 $questions[$key]->mediaurl = "$CFG->wwwroot/file.php?file=$question->image";
432 }
433 } else {
434 $questions[$key]->mediaurl = $question->image;
435 }
436 }
437 }
438
439 $this->add_qti_info($questions);
440 $questions = $this->questions_with_export_info($questions, $shuffleanswers);
441 $questions = $this->objects_to_array($questions);
442 return $questions;
443 }
444
445/**
446 * calls htmlspecialchars for each string field, to convert, for example, & to &amp;
447 *
448 * collections are processed recursively
449 *
450 * @param array $collection - an array or object or string
451 */
452function xml_entitize(&$collection) {
453 if (is_array($collection)) {
454 foreach ($collection as $key=>$var) {
455 if (is_string($var)) {
456 $collection[$key]= htmlspecialchars($var, ENT_COMPAT, 'UTF-8');
457 } else if (is_array($var) || is_object($var)) {
458 $this->xml_entitize($collection[$key]);
459 }
460 }
461 } else if (is_object($collection)) {
462 $vars = get_object_vars($collection);
463 foreach ($vars as $key=>$var) {
464 if (is_string($var)) {
465 $collection->$key = htmlspecialchars($var, ENT_COMPAT, 'UTF-8');
466 } else if (is_array($var) || is_object($var)) {
467 $this->xml_entitize($collection->$key);
468 }
469 }
470 } else if (is_string($collection)) {
471 $collection = htmlspecialchars($collection, ENT_COMPAT, 'UTF-8');
472 }
473}
474
475/**
476 * adds exporttext property to the questions
477 *
478 * Adds the qti export text to the questions
479 *
480 * @param array $questions - an array of question objects
481 * @return an array of question objects
482 */
483 function questions_with_export_info($questions, $shuffleanswers = null) {
484 $exportquestions = array();
485 foreach($questions as $key=>$question) {
486 $expout = $this->writequestion( $question , $shuffleanswers) . "\n";
487 $expout = $this->presave_process( $expout );
488 $key = $this->get_assesment_item_id($question);
489 $exportquestions[$key] = $question;
490 $exportquestions[$key]->exporttext = $expout;
491 }
492 return $exportquestions;
493 }
494
495/**
496 * Creates the export text for a question
497 *
498 * @todo handle in-line media (specified in the question/subquestion/answer text) for course-level exports
499 * @param object $question
500 * @param boolean $shuffleanswers whether or not to shuffle the answers
501 * @param boolean $courselevel whether or not this is a course-level export
502 * @param string $path provide the path to copy question media files to, if $courselevel == true
503 * @return string containing export text
504 */
505 function writequestion($question, $shuffleanswers = null, $courselevel = false, $path = '') {
506 // turns question into string
507 // question reflects database fields for general question and specific to type
508 global $CFG;
509 $expout = '';
510 //need to unencode the html entities in the questiontext field.
511 // the whole question object was earlier run throught htmlspecialchars in xml_entitize().
512 $question->questiontext = html_entity_decode($question->questiontext, ENT_COMPAT);
513
514 $hasimage = empty($question->image) ? 0 : 1;
515 $hassize = empty($question->mediax) ? 0 : 1;
516
517 $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
518 $smarty =& $this->init_smarty();
519 $assesmentitemid = $this->get_assesment_item_id($question);
520 $question_type = $this->get_qtype( $question->qtype );
521 $questionid = "question{$question->id}$question_type";
522 $smarty->assign('question_has_image', $hasimage);
523 $smarty->assign('hassize', $hassize);
524 $smarty->assign('questionid', $questionid);
525 $smarty->assign('assessmentitemidentifier', $assesmentitemid);
526 $smarty->assign('assessmentitemtitle', $question->name);
527 $smarty->assign('courselevelexport', $courselevel);
528
529 if ($question->qtype == MULTIANSWER) {
530 $question->questiontext = strip_tags($question->questiontext, $allowedtags . '<intro>');
531 $smarty->assign('questionText', $this->get_cloze_intro($question->questiontext));
532 } else {
533 $smarty->assign('questionText', strip_tags($question->questiontext, $allowedtags));
534 }
535
536 $smarty->assign('question', $question);
537 // the following two are left for compatibility; the templates should be changed, though, to make object tags for the questions
538 //$smarty->assign('questionimage', $question->image);
539 //$smarty->assign('questionimagealt', "image: $question->image");
540
541 // output depends on question type
542 switch($question->qtype) {
543 case TRUEFALSE:
544 $qanswers = $question->options->answers;
545 $answers[0] = (array)$qanswers['true'];
546 $answers[0]['answer'] = get_string("true", "quiz");
547 $answers[1] = (array)$qanswers['false'];
548 $answers[1]['answer'] = get_string("false", "quiz");
549
550 if (!empty($shuffleanswers)) {
551 $answers = $this->shuffle_things($answers);
552 }
553
554 if (isset($question->response)) {
555 $correctresponseid = $question->response[$questionid];
556 if ($answers[0]['id'] == $correctresponseid) {
557 $correctresponse = $answers[0];
558 } else {
559 $correctresponse = $answers[1];
560 }
561 }
562 else {
563 $correctresponse = '';
564 }
565
566 $smarty->assign('correctresponse', $correctresponse);
567 $smarty->assign('answers', $answers);
568 $expout = $smarty->fetch('choice.tpl');
569 break;
570 case MULTICHOICE:
571 $answers = $this->objects_to_array($question->options->answers);
572 if (!empty($shuffleanswers)) {
573 $answers = $this->shuffle_things($answers);
574 }
575 $correctresponses = $this->get_correct_answers($answers);
576 $correctcount = count($correctresponses);
577
578
579 $smarty->assign('responsedeclarationcardinality', $correctcount > 1 ? 'multiple' : 'single');
580 $smarty->assign('correctresponses', $correctresponses);
581 $smarty->assign('answers', $answers);
582 $smarty->assign('maxChoices', $question->options->single ? '1' : count($answers));
583 $expout = $smarty->fetch('choiceMultiple.tpl');
584 break;
585 case SHORTANSWER:
586 $answers = $this->objects_to_array($question->options->answers);
587 if (!empty($shuffleanswers)) {
588 $answers = $this->shuffle_things($answers);
589 }
590
591 $correctresponses = $this->get_correct_answers($answers);
592 $correctcount = count($correctresponses);
593
594 $smarty->assign('responsedeclarationcardinality', $correctcount > 1 ? 'multiple' : 'single');
595 $smarty->assign('correctresponses', $correctresponses);
596 $smarty->assign('answers', $answers);
597 $expout = $smarty->fetch('textEntry.tpl');
598 break;
599 case NUMERICAL:
600 $qanswer = array_pop( $question->options->answers );
601 $smarty->assign('lowerbound', $qanswer->answer - $qanswer->tolerance);
602 $smarty->assign('upperbound', $qanswer->answer + $qanswer->tolerance);
603 $smarty->assign('answer', $qanswer->answer);
604 $expout = $smarty->fetch('numerical.tpl');
605 break;
606 case MATCH:
607 $this->xml_entitize($question->options->subquestions);
608 $subquestions = $this->objects_to_array($question->options->subquestions);
609 if (!empty($shuffleanswers)) {
610 $subquestions = $this->shuffle_things($subquestions);
611 }
612 $setcount = count($subquestions);
613
614 $smarty->assign('setcount', $setcount);
615 $smarty->assign('matchsets', $subquestions);
616 $expout = $smarty->fetch('match.tpl');
617 break;
618 case DESCRIPTION:
619 $expout = $smarty->fetch('extendedText.tpl');
620 break;
621 // loss of get_answers() from quiz_embedded_close_qtype class during
622 // Gustav's refactor breaks MULTIANSWER badly - one for another day!!
623 /*
624 case MULTIANSWER:
625 $answers = $this->get_cloze_answers_array($question);
626 $questions = $this->get_cloze_questions($question, $answers, $allowedtags);
627
628 $smarty->assign('cloze_trailing_text_id', CLOZE_TRAILING_TEXT_ID);
629 $smarty->assign('answers', $answers);
630 $smarty->assign('questions', $questions);
631 $expout = $smarty->fetch('composite.tpl');
632 break; */
633 default:
634 $smarty->assign('questionText', "This question type (Unknown: type $question_type) has not yet been implemented");
635 $expout = $smarty->fetch('notimplemented.tpl');
636 }
637
638 // run through xml tidy function
639 //$tidy_expout = $this->indent_xhtml( $expout, ' ' ) . "\n\n";
640 //return $tidy_expout;
641 return $expout;
642 }
643
644/**
645 * Gets an id to use for a qti assesment item
646 *
647 * @param object $question
648 * @return string containing a qti assesment item id
649 */
650 function get_assesment_item_id($question) {
651 return "question{$question->id}";
652 }
653
654/**
655 * gets the answers whose grade fraction > 0
656 *
657 * @param array $answers
658 * @return array (0-indexed) containing the answers whose grade fraction > 0
659 */
660 function get_correct_answers($answers)
661 {
662 $correctanswers = array();
663 foreach ($answers as $answer) {
664 if ($answer['fraction'] > 0) {
665 $correctanswers[] = $answer;
666 }
667 }
668 return $correctanswers;
669 }
670
671/**
672 * gets a new Smarty object, with the template and compile directories set
673 *
674 * @return object a smarty object
675 */
676 function & init_smarty() {
677 global $CFG;
678
679 // create smarty compile dir in dataroot
680 $path = $CFG->dataroot."/smarty_c";
681 if (!is_dir($path)) {
682 if (!mkdir($path, $CFG->directorypermissions)) {
683 error("Cannot create path: $path");
684 }
685 }
686 $smarty = new Smarty;
687 $smarty->template_dir = "{$CFG->dirroot}/mod/quiz/format/qti2/templates";
688 $smarty->compile_dir = "$path";
689 return $smarty;
690 }
691
692/**
693 * converts an array of objects to an array of arrays (not recursively)
694 *
695 * @param array $objectarray
696 * @return array - an array of answer arrays
697 */
698 function objects_to_array($objectarray)
699 {
700 $arrayarray = array();
701 foreach ($objectarray as $object) {
702 $arrayarray[] = (array)$object;
703 }
704 return $arrayarray;
705 }
706
707/**
708 * gets a question's cloze answer objects as arrays containing only arrays and basic data types
709 *
710 * @param object $question
711 * @return array - an array of answer arrays
712 */
713 function get_cloze_answers_array($question) {
714 $answers = $this->get_answers($question);
715 $this->xml_entitize($answers);
716 foreach ($answers as $answerkey => $answer) {
717 $answers[$answerkey]->subanswers = $this->objects_to_array($answer->subanswers);
718 }
719 return $this->objects_to_array($answers);
720 }
721
722/**
723 * gets an array with text and question arrays for the given cloze question
724 *
725 * To make smarty processing easier, the returned text and question sub-arrays have an equal number of elements.
726 * 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.
727 *
728 * @param object $question
729 * @param array $answers - an array of arrays containing the question's answers
730 * @param string $allowabletags - tags not to strip out of the question text (e.g. '<i><br>')
731 * @return array with text and question arrays for the given cloze question
732 */
733 function get_cloze_questions($question, $answers, $allowabletags) {
734 $questiontext = strip_tags($question->questiontext, $allowabletags);
735 if (preg_match_all('/(.*){#([0-9]+)}/U', $questiontext, $matches)) {
736 // matches[1] contains the text inbetween the question blanks
32a189d6 737 // matches[2] contains the id of the question blanks (db: question_multianswer.positionkey)
84769fd8 738
739 // find any trailing text after the last {#XX} and add it to the array
740 if (preg_match('/.*{#[0-9]+}(.*)$/', $questiontext, $tail)) {
741 $matches[1][] = $tail[1];
742 $tailadded = true;
743 }
744 $questions['text'] = $matches[1];
745 $questions['question'] = array();
746 foreach ($matches[2] as $key => $questionid) {
747 foreach ($answers as $answer) {
748 if ($answer['positionkey'] == $questionid) {
749 $questions['question'][$key] = $answer;
750 break;
751 }
752 }
753 }
754 if ($tailadded) {
755 // to have a matching number of question and text array entries:
756 $questions['question'][] = array('id'=>CLOZE_TRAILING_TEXT_ID, 'answertype'=>SHORTANSWER);
757 }
758
759 } else {
760 $questions['text'][0] = $question->questiontext;
761 $questions['question'][0] = array('id'=>CLOZE_TRAILING_TEXT_ID, 'answertype'=>SHORTANSWER);
762 }
763
764 return $questions;
765 }
766
767/**
768 * strips out the <intro>...</intro> section, if any, and returns the text
769 *
770 * changes the text object passed to it.
771 *
772 * @param string $&text
773 * @return string the intro text, if there was an intro tag. '' otherwise.
774 */
775 function get_cloze_intro(&$text) {
776 if (preg_match('/(.*)?\<intro>(.+)?\<\/intro>(.*)/s', $text, $matches)) {
777 $text = $matches[1] . $matches[3];
778 return $matches[2];
779 }
780 else {
781 return '';
782 }
783 }
784
785
786/**
787 * adds qti metadata properties to the questions
788 *
789 * The passed array of questions is altered by this function
790 *
791 * @param &questions an array of question objects
792 */
793 function add_qti_info(&$questions)
794 {
795 foreach ($questions as $key=>$question) {
796 $questions[$key]->qtiinteractiontype = $this->get_qti_interaction_type($question->qtype);
797 $questions[$key]->qtiscoreable = $this->get_qti_scoreable($question);
798 $questions[$key]->qtisolutionavailable = $this->get_qti_solution_available($question);
799 }
800
801 }
802
803/**
804 * returns whether or not a given question is scoreable
805 *
806 * @param object $question
807 * @return boolean
808 */
809 function get_qti_scoreable($question) {
810 switch ($question->qtype) {
811 case DESCRIPTION:
812 return 'false';
813 default:
814 return 'true';
815 }
816 }
817
818/**
819 * returns whether or not a solution is available for a given question
820 *
821 * The results are based on whether or not Moodle stores answers for the given question type
822 *
823 * @param object $question
824 * @return boolean
825 */
826 function get_qti_solution_available($question) {
827 switch($question->qtype) {
828 case TRUEFALSE:
829 return 'true';
830 case MULTICHOICE:
831 return 'true';
832 case SHORTANSWER:
833 return 'true';
834 case NUMERICAL:
835 return 'true';
836 case MATCH:
837 return 'true';
838 case DESCRIPTION:
839 return 'false';
840 case MULTIANSWER:
841 return 'true';
842 default:
843 return 'true';
844 }
845
846 }
847
848/**
849 * maps a moodle question type to a qti 2.0 question type
850 *
851 * @param int type_id - the moodle question type
852 * @return string qti 2.0 question type
853 */
854 function get_qti_interaction_type($type_id) {
855 switch( $type_id ) {
856 case TRUEFALSE:
857 $name = 'choiceInteraction';
858 break;
859 case MULTICHOICE:
860 $name = 'choiceInteraction';
861 break;
862 case SHORTANSWER:
863 $name = 'textInteraction';
864 break;
865 case NUMERICAL:
866 $name = 'textInteraction';
867 break;
868 case MATCH:
869 $name = 'matchInteraction';
870 break;
871 case DESCRIPTION:
872 $name = 'extendedTextInteraction';
873 break;
874 case MULTIANSWER:
875 $name = 'textInteraction';
876 break;
877 default:
878 $name = 'textInteraction';
879 }
880 return $name;
881 }
882
883/**
884 * returns the given array, shuffled
885 *
886 *
887 * @param array $things
888 * @return array
889 */
890 function shuffle_things($things) {
891 $things = swapshuffle_assoc($things);
892 $oldthings = $things;
893 $things = array();
894 foreach ($oldthings as $key=>$value) {
895 $things[] = $value; // This loses the index key, but doesn't matter
896 }
897 return $things;
898 }
899
900/**
901 * returns a flattened image name - with all /, \ and : replaced with other characters
902 *
903 * used to convert a file or url to a qti-permissable identifier
904 *
905 * @param string name
906 * @return string
907 */
908 function flatten_image_name($name) {
909 return str_replace(array('/', '\\', ':'), array ('_','-','.'), $name);
910 }
911
912 function file_full_path($file, $courseid) {
913 global $CFG;
914 if (substr(strtolower($file), 0, 7) == 'http://') {
915 $url = $file;
916 } else if ($CFG->slasharguments) { // Use this method if possible for better caching
917 $url = "{$CFG->wwwroot}/file.php/$courseid/{$file}";
918 } else {
919 $url = "{$CFG->wwwroot}/file.php?file=/$courseid/{$file}";
920 }
921 return $url;
922 }
923
924}
925
926?>