MDL-14431 - followup: after the rename qti2 -> qti_two, there were still some referen...
[moodle.git] / question / format / qti_two / format.php
CommitLineData
fb2ce31b 1<?php // $Id$
2
6e0ddb6b 3require_once("$CFG->dirroot/question/format/qti_two/qt_common.php");
fb2ce31b 4////////////////////////////////////////////////////////////////////////////
5/// IMS QTI 2.0 FORMAT
6///
7/// HISTORY: created 28.01.2005 brian@mediagonal.ch
8////////////////////////////////////////////////////////////////////////////
9
10// Based on format.php, included by ../../import.php
11/**
12 * @package questionbank
13 * @subpackage importexport
14 */
15define('CLOZE_TRAILING_TEXT_ID', 9999999);
16
bd379592 17class qformat_qti_two extends qformat_default {
6e01bf15 18
fb2ce31b 19 var $lang;
20
21 function provide_export() {
22 return true;
23 }
6e01bf15 24
25 function indent_xhtml($source, $indenter = ' ') {
fb2ce31b 26 // xml tidier-upper
27 // (c) Ari Koivula http://ventionline.com
6e01bf15 28
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);
38
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;
52 }
53 // If the second letter of the tag is "!", assume its an "invisible" tag.
54 if ($source{$pt+1} === '!') {
55 $tag_level = 0;
56 }
57 // Iterate throught the source until the end of tag.
58 while ($source{$pt} !== '>') {
59 $pt++;
60 }
61 // If the second last letter is "/", assume its a self ending tag.
62 if ($source{$pt-1} === '/') {
63 $tag_level = 0;
64 }
65 $tag_lenght = $pt+1-$started_at;
66
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--;
71 }
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++;
77 }
78 // if it was a self closing tag, dont do shit.
79 }
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++;
89 }
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);
95 }
96 // If the next tag is "<", just advance pointer and let the tag indenter take care of it.
97 } else {
98 $pt++;
99 }
100 // If the next letter doesnt exist... Were done... well, almost..
101 } else {
102 break;
103 }
104 }
105 // Replace old source with the new one we just collected into our array.
106 $source = implode($array, "\n");
107 return $source;
108 }
109
fb2ce31b 110 function importpreprocess() {
111 global $CFG;
6e01bf15 112
113 error("Sorry, importing this format is not yet implemented!",
114 "$CFG->wwwroot/mod/quiz/import.php?category=$category->id");
fb2ce31b 115 }
116
117 function exportpreprocess() {
118 global $CFG;
6e01bf15 119
fb2ce31b 120 require_once("{$CFG->libdir}/smarty/Smarty.class.php");
6e01bf15 121
fb2ce31b 122 // assign the language for the export: by parameter, SESSION, USER, or the default of 'en'
123 $lang = current_language();
124 $this->lang = $lang;
6e01bf15 125
fb2ce31b 126 return parent::exportpreprocess();
127 }
6e01bf15 128
129
fb2ce31b 130 function export_file_extension() {
131 // override default type so extension is .xml
6e01bf15 132
fb2ce31b 133 return ".zip";
134 }
6e01bf15 135
fb2ce31b 136 function get_qtype( $type_id ) {
137 // translates question type code number into actual name
6e01bf15 138
fb2ce31b 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';
163 }
164 return $name;
165 }
166
167 function writetext( $raw ) {
6e01bf15 168 // generates <text></text> tags, processing raw text therein
169
170 // for now, don't allow any additional tags in text
fb2ce31b 171 // otherwise xml rules would probably get broken
172 $raw = strip_tags( $raw );
6e01bf15 173
fb2ce31b 174 return "<text>$raw</text>\n";
175 }
6e01bf15 176
fb2ce31b 177
178/**
179 * flattens $object['media'], copies $object['media'] to $path, and sets $object['mediamimetype']
180 *
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
6e01bf15 185 */
fb2ce31b 186function 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']}";
193 }
194 if (empty($object['mediamimetype'])) {
195 $object['mediamimetype'] = mimeinfo('type', $object['media']);
196 }
197 }
198 return true;
6e01bf15 199}
fb2ce31b 200/**
201 * copies all files needed by the questions to the given $path, and flattens the file names
202 *
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
6e01bf15 207 */
fb2ce31b 208function handle_questions_media(&$questions, $path, $courseid) {
209 global $CFG;
210 $errors = array();
211 foreach ($questions as $key=>$question) {
6e01bf15 212
fb2ce31b 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}";
219 }
220 if (empty($question->mediamimetype)) {
221 $questions[$key]->mediamimetype = mimeinfo('type', $question->image);
222 }
223 }
224 }
6e01bf15 225
fb2ce31b 226 return empty($errors) ? true : $errors;
227}
228
229/**
230 * exports the questions in a question category to the given location
231 *
232 * The parent class method was overridden because the IMS export consists of multiple files
233 *
234 * @param string $filename the directory name which will hold the exported files
235 * @return boolean - or errors out
6e01bf15 236 */
fb2ce31b 237 function exportprocess() {
238
239 global $CFG;
240 $courseid = $this->course->id;
241
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)) {
6e01bf15 244 error( get_string('cannotcreatepath','quiz',$export_dir) );
fb2ce31b 245 }
246 $path = $CFG->dataroot.'/'.$this->question_get_export_dir().'/'.$this->filename;
247
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 );
6e01bf15 251
fb2ce31b 252 notify("Exporting ".count($questions)." questions.");
253 $count = 0;
254
255 // create the imsmanifest file
256 $smarty =& $this->init_smarty();
257 $this->add_qti_info($questions);
fb2ce31b 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));
262 }
6e01bf15 263
fb2ce31b 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)) {
6e01bf15 276 error("Unkown error - empty imsmanifest.xml");
fb2ce31b 277 }
278 if (!$fh=fopen($filepath,"w")) {
6e01bf15 279 error("Cannot open for writing: $filepath");
fb2ce31b 280 }
281 if (!fwrite($fh, $expout)) {
6e01bf15 282 error("Cannot write exported questions to $filepath");
fb2ce31b 283 }
284 fclose($fh);
285
286 // iterate through questions
287 foreach($questions as $question) {
6e01bf15 288
fb2ce31b 289 // results are first written into string (and then to a file)
290 $count++;
6e01bf15 291 echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
fb2ce31b 292 $expout = $this->writequestion( $question , null, true, $path) . "\n";
293 $expout = $this->presave_process( $expout );
6e01bf15 294
fb2ce31b 295 $filepath = $path.'/'.$this->get_assesment_item_id($question) . ".xml";
296 if (!$fh=fopen($filepath,"w")) {
6e01bf15 297 error("Cannot open for writing: $filepath");
fb2ce31b 298 }
299 if (!fwrite($fh, $expout)) {
6e01bf15 300 error("Cannot write exported questions to $filepath");
fb2ce31b 301 }
302 fclose($fh);
6e01bf15 303
fb2ce31b 304 }
305
306 // zip files into single export file
307 zip_files( array($path), "$path.zip" );
308
309 // remove the temporary directory
310 remove_dir( $path );
6e01bf15 311
fb2ce31b 312 return true;
313 }
6e01bf15 314
fb2ce31b 315/**
316 * exports a quiz (as opposed to exporting a category of questions)
317 *
318 * The parent class method was overridden because the IMS export consists of multiple files
319 *
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
6e01bf15 326 */
fb2ce31b 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
6e01bf15 334 error("Error occurred during pre-processing!", $redirect);
fb2ce31b 335 }
336 if (! $this->exportprocess_quiz($quiz, $questions, $result, $submiturl, $course)) { // Process the export data
6e01bf15 337 error("Error occurred during processing!", $redirect);
fb2ce31b 338 }
339 if (! $this->exportpostprocess()) { // In case anything needs to be done after
6e01bf15 340 error("Error occurred during post-processing!", $redirect);
fb2ce31b 341 }
342
343 }
6e01bf15 344
fb2ce31b 345
346/**
347 * This function is called to export a quiz (as opposed to exporting a category of questions)
348 *
349 * @uses $USER
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
6e01bf15 354 */
fb2ce31b 355 function exportprocess_quiz($quiz, $questions, $result, $submiturl, $course) {
356 global $USER;
357 global $CFG;
358
359 $gradingmethod = array (1 => 'GRADEHIGHEST',
360 2 => 'GRADEAVERAGE',
361 3 => 'ATTEMPTFIRST' ,
362 4 => 'ATTEMPTLAST');
363
364 $questions = $this->quiz_export_prepare_questions($questions, $quiz->id, $course->id, $quiz->shuffleanswers);
365
366 $smarty =& $this->init_smarty();
367 $smarty->assign('questions', $questions);
6e01bf15 368
fb2ce31b 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);
6e01bf15 373 $smarty->assign('userid', $USER->id);
fb2ce31b 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');
384 echo $expout;
385 return true;
386 }
387
6e01bf15 388
389
fb2ce31b 390
391/**
392 * Prepares questions for quiz export
393 *
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
399 *
400 * @param array $questions - an array of question objects
401 * @param int $quizid
402 * @return an array of question arrays
6e01bf15 403 */
fb2ce31b 404 function quiz_export_prepare_questions($questions, $quizid, $courseid, $shuffleanswers = null) {
405 global $CFG;
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;
6e01bf15 411
fb2ce31b 412 if ($question->image) {
6e01bf15 413
fb2ce31b 414 if (empty($question->mediamimetype)) {
6e01bf15 415 $questions[$key]->mediamimetype = mimeinfo('type',$question->image);
fb2ce31b 416 }
6e01bf15 417
fb2ce31b 418 $localfile = (substr(strtolower($question->image), 0, 7) == 'http://') ? false : true;
419
420 if ($localfile) {
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";
424 } else {
425 $questions[$key]->mediaurl = "$CFG->wwwroot/file.php?file=$question->image";
6e01bf15 426 }
fb2ce31b 427 } else {
428 $questions[$key]->mediaurl = $question->image;
429 }
430 }
431 }
432
433 $this->add_qti_info($questions);
434 $questions = $this->questions_with_export_info($questions, $shuffleanswers);
435 $questions = $this->objects_to_array($questions);
436 return $questions;
437 }
438
439/**
440 * calls htmlspecialchars for each string field, to convert, for example, & to &amp;
6e01bf15 441 *
fb2ce31b 442 * collections are processed recursively
443 *
444 * @param array $collection - an array or object or string
6e01bf15 445 */
fb2ce31b 446function 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]);
6e01bf15 453 }
fb2ce31b 454 }
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);
6e01bf15 462 }
fb2ce31b 463 }
464 } else if (is_string($collection)) {
465 $collection = htmlspecialchars($collection, ENT_COMPAT, 'UTF-8');
466 }
467}
6e01bf15 468
fb2ce31b 469/**
470 * adds exporttext property to the questions
471 *
472 * Adds the qti export text to the questions
473 *
474 * @param array $questions - an array of question objects
475 * @return an array of question objects
6e01bf15 476 */
fb2ce31b 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;
485 }
486 return $exportquestions;
487 }
488
489/**
490 * Creates the export text for a question
491 *
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
6e01bf15 498 */
fb2ce31b 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
502 global $CFG;
503 $expout = '';
6e01bf15 504 //need to unencode the html entities in the questiontext field.
fb2ce31b 505 // the whole question object was earlier run throught htmlspecialchars in xml_entitize().
506 $question->questiontext = html_entity_decode($question->questiontext, ENT_COMPAT);
6e01bf15 507
fb2ce31b 508 $hasimage = empty($question->image) ? 0 : 1;
509 $hassize = empty($question->mediax) ? 0 : 1;
6e01bf15 510
fb2ce31b 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);
6e01bf15 522
fb2ce31b 523 if ($question->qtype == MULTIANSWER) {
524 $question->questiontext = strip_tags($question->questiontext, $allowedtags . '<intro>');
525 $smarty->assign('questionText', $this->get_cloze_intro($question->questiontext));
526 } else {
527 $smarty->assign('questionText', strip_tags($question->questiontext, $allowedtags));
528 }
6e01bf15 529
fb2ce31b 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");
6e01bf15 534
fb2ce31b 535 // output depends on question type
536 switch($question->qtype) {
537 case TRUEFALSE:
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");
6e01bf15 543
fb2ce31b 544 if (!empty($shuffleanswers)) {
545 $answers = $this->shuffle_things($answers);
546 }
6e01bf15 547
548 if (isset($question->response)) {
fb2ce31b 549 $correctresponseid = $question->response[$questionid];
550 if ($answers[0]['id'] == $correctresponseid) {
551 $correctresponse = $answers[0];
552 } else {
553 $correctresponse = $answers[1];
554 }
555 }
556 else {
557 $correctresponse = '';
558 }
6e01bf15 559
fb2ce31b 560 $smarty->assign('correctresponse', $correctresponse);
561 $smarty->assign('answers', $answers);
562 $expout = $smarty->fetch('choice.tpl');
563 break;
564 case MULTICHOICE:
565 $answers = $this->objects_to_array($question->options->answers);
fb2ce31b 566 $correctresponses = $this->get_correct_answers($answers);
567 $correctcount = count($correctresponses);
6e01bf15 568 $smarty->assign('responsedeclarationcardinality', $question->options->single ? 'single' : 'multiple');
569 $smarty->assign('operator', $question->options->single ? 'match' : 'member');
fb2ce31b 570 $smarty->assign('correctresponses', $correctresponses);
571 $smarty->assign('answers', $answers);
572 $smarty->assign('maxChoices', $question->options->single ? '1' : count($answers));
6e01bf15 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);
fb2ce31b 579 $expout = $smarty->fetch('choiceMultiple.tpl');
580 break;
581 case SHORTANSWER:
582 $answers = $this->objects_to_array($question->options->answers);
583 if (!empty($shuffleanswers)) {
584 $answers = $this->shuffle_things($answers);
585 }
6e01bf15 586
fb2ce31b 587 $correctresponses = $this->get_correct_answers($answers);
588 $correctcount = count($correctresponses);
589
590 $smarty->assign('responsedeclarationcardinality', $correctcount > 1 ? 'multiple' : 'single');
591 $smarty->assign('correctresponses', $correctresponses);
592 $smarty->assign('answers', $answers);
593 $expout = $smarty->fetch('textEntry.tpl');
594 break;
595 case NUMERICAL:
596 $qanswer = array_pop( $question->options->answers );
6e01bf15 597 $smarty->assign('lowerbound', $qanswer->answer - $qanswer->tolerance);
598 $smarty->assign('upperbound', $qanswer->answer + $qanswer->tolerance);
599 $smarty->assign('answer', $qanswer->answer);
fb2ce31b 600 $expout = $smarty->fetch('numerical.tpl');
601 break;
602 case MATCH:
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);
607 }
608 $setcount = count($subquestions);
6e01bf15 609
fb2ce31b 610 $smarty->assign('setcount', $setcount);
611 $smarty->assign('matchsets', $subquestions);
612 $expout = $smarty->fetch('match.tpl');
613 break;
614 case DESCRIPTION:
615 $expout = $smarty->fetch('extendedText.tpl');
616 break;
617 // loss of get_answers() from quiz_embedded_close_qtype class during
618 // Gustav's refactor breaks MULTIANSWER badly - one for another day!!
619 /*
620 case MULTIANSWER:
621 $answers = $this->get_cloze_answers_array($question);
622 $questions = $this->get_cloze_questions($question, $answers, $allowedtags);
6e01bf15 623
624 $smarty->assign('cloze_trailing_text_id', CLOZE_TRAILING_TEXT_ID);
fb2ce31b 625 $smarty->assign('answers', $answers);
626 $smarty->assign('questions', $questions);
627 $expout = $smarty->fetch('composite.tpl');
628 break; */
629 default:
630 $smarty->assign('questionText', "This question type (Unknown: type $question_type) has not yet been implemented");
631 $expout = $smarty->fetch('notimplemented.tpl');
632 }
6e01bf15 633
fb2ce31b 634 // run through xml tidy function
635 //$tidy_expout = $this->indent_xhtml( $expout, ' ' ) . "\n\n";
636 //return $tidy_expout;
637 return $expout;
638 }
639
640/**
641 * Gets an id to use for a qti assesment item
642 *
643 * @param object $question
644 * @return string containing a qti assesment item id
6e01bf15 645 */
fb2ce31b 646 function get_assesment_item_id($question) {
647 return "question{$question->id}";
648 }
649
650/**
651 * gets the answers whose grade fraction > 0
652 *
653 * @param array $answers
654 * @return array (0-indexed) containing the answers whose grade fraction > 0
6e01bf15 655 */
fb2ce31b 656 function get_correct_answers($answers)
657 {
658 $correctanswers = array();
659 foreach ($answers as $answer) {
660 if ($answer['fraction'] > 0) {
661 $correctanswers[] = $answer;
662 }
663 }
664 return $correctanswers;
665 }
666
667/**
668 * gets a new Smarty object, with the template and compile directories set
669 *
670 * @return object a smarty object
6e01bf15 671 */
fb2ce31b 672 function & init_smarty() {
673 global $CFG;
674
675 // create smarty compile dir in dataroot
676 $path = $CFG->dataroot."/smarty_c";
677 if (!is_dir($path)) {
678 if (!mkdir($path, $CFG->directorypermissions)) {
6e01bf15 679 error("Cannot create path: $path");
fb2ce31b 680 }
681 }
682 $smarty = new Smarty;
6e0ddb6b 683 $smarty->template_dir = "{$CFG->dirroot}/question/format/qti_two/templates";
fb2ce31b 684 $smarty->compile_dir = "$path";
685 return $smarty;
686 }
687
688/**
689 * converts an array of objects to an array of arrays (not recursively)
690 *
691 * @param array $objectarray
692 * @return array - an array of answer arrays
6e01bf15 693 */
fb2ce31b 694 function objects_to_array($objectarray)
695 {
696 $arrayarray = array();
697 foreach ($objectarray as $object) {
698 $arrayarray[] = (array)$object;
699 }
700 return $arrayarray;
701 }
6e01bf15 702
fb2ce31b 703/**
704 * gets a question's cloze answer objects as arrays containing only arrays and basic data types
705 *
706 * @param object $question
707 * @return array - an array of answer arrays
6e01bf15 708 */
fb2ce31b 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);
714 }
715 return $this->objects_to_array($answers);
716 }
6e01bf15 717
fb2ce31b 718/**
719 * gets an array with text and question arrays for the given cloze question
720 *
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.
723 *
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
6e01bf15 728 */
fb2ce31b 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)
6e01bf15 734
fb2ce31b 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];
738 $tailadded = true;
739 }
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;
746 break;
747 }
748 }
749 }
750 if ($tailadded) {
751 // to have a matching number of question and text array entries:
752 $questions['question'][] = array('id'=>CLOZE_TRAILING_TEXT_ID, 'answertype'=>SHORTANSWER);
753 }
6e01bf15 754
fb2ce31b 755 } else {
756 $questions['text'][0] = $question->questiontext;
757 $questions['question'][0] = array('id'=>CLOZE_TRAILING_TEXT_ID, 'answertype'=>SHORTANSWER);
758 }
6e01bf15 759
fb2ce31b 760 return $questions;
761 }
6e01bf15 762
fb2ce31b 763/**
764 * strips out the <intro>...</intro> section, if any, and returns the text
765 *
766 * changes the text object passed to it.
767 *
768 * @param string $&text
769 * @return string the intro text, if there was an intro tag. '' otherwise.
6e01bf15 770 */
fb2ce31b 771 function get_cloze_intro(&$text) {
772 if (preg_match('/(.*)?\<intro>(.+)?\<\/intro>(.*)/s', $text, $matches)) {
773 $text = $matches[1] . $matches[3];
774 return $matches[2];
775 }
776 else {
777 return '';
778 }
6e01bf15 779 }
780
fb2ce31b 781
782/**
783 * adds qti metadata properties to the questions
784 *
6e01bf15 785 * The passed array of questions is altered by this function
fb2ce31b 786 *
787 * @param &questions an array of question objects
6e01bf15 788 */
fb2ce31b 789 function add_qti_info(&$questions)
790 {
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);
795 }
6e01bf15 796
fb2ce31b 797 }
6e01bf15 798
fb2ce31b 799/**
800 * returns whether or not a given question is scoreable
801 *
802 * @param object $question
803 * @return boolean
6e01bf15 804 */
fb2ce31b 805 function get_qti_scoreable($question) {
806 switch ($question->qtype) {
807 case DESCRIPTION:
808 return 'false';
809 default:
810 return 'true';
811 }
812 }
6e01bf15 813
fb2ce31b 814/**
6e01bf15 815 * returns whether or not a solution is available for a given question
fb2ce31b 816 *
817 * The results are based on whether or not Moodle stores answers for the given question type
818 *
819 * @param object $question
820 * @return boolean
6e01bf15 821 */
fb2ce31b 822 function get_qti_solution_available($question) {
823 switch($question->qtype) {
824 case TRUEFALSE:
825 return 'true';
826 case MULTICHOICE:
827 return 'true';
828 case SHORTANSWER:
829 return 'true';
830 case NUMERICAL:
831 return 'true';
832 case MATCH:
833 return 'true';
834 case DESCRIPTION:
835 return 'false';
836 case MULTIANSWER:
837 return 'true';
838 default:
839 return 'true';
840 }
6e01bf15 841
fb2ce31b 842 }
6e01bf15 843
fb2ce31b 844/**
845 * maps a moodle question type to a qti 2.0 question type
846 *
847 * @param int type_id - the moodle question type
848 * @return string qti 2.0 question type
6e01bf15 849 */
fb2ce31b 850 function get_qti_interaction_type($type_id) {
851 switch( $type_id ) {
852 case TRUEFALSE:
853 $name = 'choiceInteraction';
854 break;
855 case MULTICHOICE:
856 $name = 'choiceInteraction';
857 break;
858 case SHORTANSWER:
859 $name = 'textInteraction';
860 break;
861 case NUMERICAL:
862 $name = 'textInteraction';
863 break;
864 case MATCH:
865 $name = 'matchInteraction';
866 break;
867 case DESCRIPTION:
868 $name = 'extendedTextInteraction';
869 break;
870 case MULTIANSWER:
871 $name = 'textInteraction';
872 break;
873 default:
874 $name = 'textInteraction';
875 }
876 return $name;
877 }
878
879/**
880 * returns the given array, shuffled
881 *
882 *
883 * @param array $things
884 * @return array
6e01bf15 885 */
fb2ce31b 886 function shuffle_things($things) {
6e01bf15 887 $things = swapshuffle_assoc($things);
fb2ce31b 888 $oldthings = $things;
889 $things = array();
890 foreach ($oldthings as $key=>$value) {
891 $things[] = $value; // This loses the index key, but doesn't matter
892 }
893 return $things;
894 }
6e01bf15 895
fb2ce31b 896/**
897 * returns a flattened image name - with all /, \ and : replaced with other characters
898 *
899 * used to convert a file or url to a qti-permissable identifier
900 *
901 * @param string name
6e01bf15 902 * @return string
903 */
fb2ce31b 904 function flatten_image_name($name) {
905 return str_replace(array('/', '\\', ':'), array ('_','-','.'), $name);
906 }
907
908 function file_full_path($file, $courseid) {
909 global $CFG;
910 if (substr(strtolower($file), 0, 7) == 'http://') {
911 $url = $file;
912 } else if ($CFG->slasharguments) { // Use this method if possible for better caching
913 $url = "{$CFG->wwwroot}/file.php/$courseid/{$file}";
914 } else {
915 $url = "{$CFG->wwwroot}/file.php?file=/$courseid/{$file}";
916 }
917 return $url;
918 }
919
920}
921
6e01bf15 922?>