Added hooks to qtype import/export
[moodle-linuxchix.git] / question / format / gift / format.php
blob662b885ef7dda4843e50a4633ff84b91a1495751
1 <?php // $Id$
2 //
3 ///////////////////////////////////////////////////////////////
4 // The GIFT import filter was designed as an easy to use method
5 // for teachers writing questions as a text file. It supports most
6 // question types and the missing word format.
7 //
8 // Multiple Choice / Missing Word
9 // Who's buried in Grant's tomb?{~Grant ~Jefferson =no one}
10 // Grant is {~buried =entombed ~living} in Grant's tomb.
11 // True-False:
12 // Grant is buried in Grant's tomb.{FALSE}
13 // Short-Answer.
14 // Who's buried in Grant's tomb?{=no one =nobody}
15 // Numerical
16 // When was Ulysses S. Grant born?{#1822:5}
17 // Matching
18 // Match the following countries with their corresponding
19 // capitals.{=Canada->Ottawa =Italy->Rome =Japan->Tokyo}
21 // Comment lines start with a double backslash (//).
22 // Optional question names are enclosed in double colon(::).
23 // Answer feedback is indicated with hash mark (#).
24 // Percentage answer weights immediately follow the tilde (for
25 // multiple choice) or equal sign (for short answer and numerical),
26 // and are enclosed in percent signs (% %). See docs and examples.txt for more.
27 //
28 // This filter was written through the collaboration of numerous
29 // members of the Moodle community. It was originally based on
30 // the missingword format, which included code from Thomas Robb
31 // and others. Paul Tsuchido Shew wrote this filter in December 2003.
32 //////////////////////////////////////////////////////////////////////////
33 // Based on default.php, included by ../import.php
34 /**
35 * @package questionbank
36 * @subpackage importexport
38 class qformat_gift extends qformat_default {
40 function provide_import() {
41 return true;
44 function provide_export() {
45 return true;
48 function answerweightparser(&$answer) {
49 $answer = substr($answer, 1); // removes initial %
50 $end_position = strpos($answer, "%");
51 $answer_weight = substr($answer, 0, $end_position); // gets weight as integer
52 $answer_weight = $answer_weight/100; // converts to percent
53 $answer = substr($answer, $end_position+1); // removes comment from answer
54 return $answer_weight;
58 function commentparser(&$answer) {
59 if (strpos($answer,"#") > 0){
60 $hashpos = strpos($answer,"#");
61 $comment = substr($answer, $hashpos+1);
62 $comment = addslashes(trim($this->escapedchar_post($comment)));
63 $answer = substr($answer, 0, $hashpos);
64 } else {
65 $comment = " ";
67 return $comment;
70 function split_truefalse_comment($comment){
71 // splits up comment around # marks
72 // returns an array of true/false feedback
73 $bits = explode('#',$comment);
74 $feedback = array('wrong' => $bits[0]);
75 if (count($bits) >= 2) {
76 $feedback['right'] = $bits[1];
77 } else {
78 $feedback['right'] = '';
80 return $feedback;
83 function escapedchar_pre($string) {
84 //Replaces escaped control characters with a placeholder BEFORE processing
86 $escapedcharacters = array("\\:", "\\#", "\\=", "\\{", "\\}", "\\~", "\\n" ); //dlnsk
87 $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010" ); //dlnsk
89 $string = str_replace("\\\\", "&&092;", $string);
90 $string = str_replace($escapedcharacters, $placeholders, $string);
91 $string = str_replace("&&092;", "\\", $string);
92 return $string;
95 function escapedchar_post($string) {
96 //Replaces placeholders with corresponding character AFTER processing is done
97 $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010"); //dlnsk
98 $characters = array(":", "#", "=", "{", "}", "~", "\n" ); //dlnsk
99 $string = str_replace($placeholders, $characters, $string);
100 return $string;
103 function check_answer_count( $min, $answers, $text ) {
104 $countanswers = count($answers);
105 if ($countanswers < $min) {
106 $importminerror = get_string( 'importminerror', 'quiz' );
107 $this->error( $importminerror, $text );
108 return false;
111 return true;
115 function readquestion($lines) {
116 // Given an array of lines known to define a question in this format, this function
117 // converts it into a question object suitable for processing and insertion into Moodle.
119 $question = $this->defaultquestion();
120 $comment = NULL;
121 // define replaced by simple assignment, stop redefine notices
122 $gift_answerweight_regex = "^%\-*([0-9]{1,2})\.?([0-9]*)%";
124 // REMOVED COMMENTED LINES and IMPLODE
125 foreach ($lines as $key => $line) {
126 $line = trim($line);
127 if (substr($line, 0, 2) == "//") {
128 $lines[$key] = " ";
132 $text = trim(implode(" ", $lines));
134 if ($text == "") {
135 return false;
138 // Substitute escaped control characters with placeholders
139 $text = $this->escapedchar_pre($text);
141 // Look for category modifier
142 if (ereg( '^\$CATEGORY:', $text)) {
143 // $newcategory = $matches[1];
144 $newcategory = trim(substr( $text, 10 ));
146 // build fake question to contain category
147 $question->qtype = 'category';
148 $question->category = $newcategory;
149 return $question;
152 // QUESTION NAME parser
153 if (substr($text, 0, 2) == "::") {
154 $text = substr($text, 2);
156 $namefinish = strpos($text, "::");
157 if ($namefinish === false) {
158 $question->name = false;
159 // name will be assigned after processing question text below
160 } else {
161 $questionname = substr($text, 0, $namefinish);
162 $question->name = addslashes(trim($this->escapedchar_post($questionname)));
163 $text = trim(substr($text, $namefinish+2)); // Remove name from text
165 } else {
166 $question->name = false;
170 // FIND ANSWER section
171 // no answer means its a description
172 $answerstart = strpos($text, "{");
173 $answerfinish = strpos($text, "}");
175 $description = false;
176 if (($answerstart === false) and ($answerfinish === false)) {
177 $description = true;
178 $answertext = '';
179 $answerlength = 0;
181 elseif (!(($answerstart !== false) and ($answerfinish !== false))) {
182 $this->error( get_string( 'braceerror', 'quiz' ), $text );
183 return false;
185 else {
186 $answerlength = $answerfinish - $answerstart;
187 $answertext = trim(substr($text, $answerstart + 1, $answerlength - 1));
190 // Format QUESTION TEXT without answer, inserting "_____" as necessary
191 if ($description) {
192 $questiontext = $text;
194 elseif (substr($text, -1) == "}") {
195 // no blank line if answers follow question, outside of closing punctuation
196 $questiontext = substr_replace($text, "", $answerstart, $answerlength+1);
197 } else {
198 // inserts blank line for missing word format
199 $questiontext = substr_replace($text, "_____", $answerstart, $answerlength+1);
202 // get questiontext format from questiontext
203 $oldquestiontext = $questiontext;
204 $questiontextformat = 0;
205 if (substr($questiontext,0,1)=='[') {
206 $questiontext = substr( $questiontext,1 );
207 $rh_brace = strpos( $questiontext, ']' );
208 $qtformat= substr( $questiontext, 0, $rh_brace );
209 $questiontext = substr( $questiontext, $rh_brace+1 );
210 if (!$questiontextformat = text_format_name( $qtformat )) {
211 $questiontext = $oldquestiontext;
214 $question->questiontextformat = $questiontextformat;
215 $question->questiontext = addslashes(trim($this->escapedchar_post($questiontext)));
217 // set question name if not already set
218 if ($question->name === false) {
219 $question->name = $question->questiontext;
222 // ensure name is not longer than 250 characters
223 $question->name = shorten_text( $question->name, 250 );
225 // determine QUESTION TYPE
226 $question->qtype = NULL;
228 if ($description) {
229 $question->qtype = DESCRIPTION;
231 elseif ($answertext == '') {
232 $question->qtype = ESSAY;
234 elseif ($answertext{0} == "#"){
235 $question->qtype = NUMERICAL;
237 } elseif (strpos($answertext, "~") !== false) {
238 // only Multiplechoice questions contain tilde ~
239 $question->qtype = MULTICHOICE;
241 } elseif (strpos($answertext, "=") !== false
242 && strpos($answertext, "->") !== false) {
243 // only Matching contains both = and ->
244 $question->qtype = MATCH;
246 } else { // either TRUEFALSE or SHORTANSWER
248 // TRUEFALSE question check
249 $truefalse_check = $answertext;
250 if (strpos($answertext,"#") > 0){
251 // strip comments to check for TrueFalse question
252 $truefalse_check = trim(substr($answertext, 0, strpos($answertext,"#")));
255 $valid_tf_answers = array("T", "TRUE", "F", "FALSE");
256 if (in_array($truefalse_check, $valid_tf_answers)) {
257 $question->qtype = TRUEFALSE;
259 } else { // Must be SHORTANSWER
260 $question->qtype = SHORTANSWER;
264 if (!isset($question->qtype)) {
265 // try for plugins
266 if ($question = $this->try_importing_using_qtypes( $lines, $question, $answertext )) {
267 return $question;
269 $giftqtypenotset = get_string('giftqtypenotset','quiz');
270 $this->error( $giftqtypenotset, $text );
271 return false;
274 switch ($question->qtype) {
275 case DESCRIPTION:
276 return $question;
277 break;
278 case ESSAY:
279 $question->feedback = '';
280 $question->fraction = 0;
281 return $question;
282 break;
283 case MULTICHOICE:
284 if (strpos($answertext,"=") === false) {
285 $question->single = 0; // multiple answers are enabled if no single answer is 100% correct
286 } else {
287 $question->single = 1; // only one answer allowed (the default)
290 $answertext = str_replace("=", "~=", $answertext);
291 $answers = explode("~", $answertext);
292 if (isset($answers[0])) {
293 $answers[0] = trim($answers[0]);
295 if (empty($answers[0])) {
296 array_shift($answers);
299 $countanswers = count($answers);
301 if (!$this->check_answer_count( 2,$answers,$text )) {
302 return false;
303 break;
306 foreach ($answers as $key => $answer) {
307 $answer = trim($answer);
309 // determine answer weight
310 if ($answer[0] == "=") {
311 $answer_weight = 1;
312 $answer = substr($answer, 1);
314 } elseif (ereg($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
315 $answer_weight = $this->answerweightparser($answer);
317 } else { //default, i.e., wrong anwer
318 $answer_weight = 0;
320 $question->fraction[$key] = $answer_weight;
321 $question->feedback[$key] = $this->commentparser($answer); // commentparser also removes comment from $answer
322 $question->answer[$key] = addslashes($this->escapedchar_post($answer));
323 $question->correctfeedback = '';
324 $question->partiallycorrectfeedback = '';
325 $question->incorrectfeedback = '';
326 } // end foreach answer
328 //$question->defaultgrade = 1;
329 //$question->image = ""; // No images with this format
330 return $question;
331 break;
333 case MATCH:
334 $answers = explode("=", $answertext);
335 if (isset($answers[0])) {
336 $answers[0] = trim($answers[0]);
338 if (empty($answers[0])) {
339 array_shift($answers);
342 if (!$this->check_answer_count( 2,$answers,$text )) {
343 return false;
344 break;
347 foreach ($answers as $key => $answer) {
348 $answer = trim($answer);
349 if (strpos($answer, "->") === false) {
350 $giftmatchingformat = get_string('giftmatchingformat','quiz');
351 $this->error($giftmatchingformat, $answer );
352 return false;
353 break 2;
356 $marker = strpos($answer,"->");
357 $question->subquestions[$key] = addslashes(trim($this->escapedchar_post(substr($answer, 0, $marker))));
358 $question->subanswers[$key] = addslashes(trim($this->escapedchar_post(substr($answer, $marker+2))));
360 } // end foreach answer
362 return $question;
363 break;
365 case TRUEFALSE:
366 $answer = $answertext;
367 $comment = $this->commentparser($answer); // commentparser also removes comment from $answer
368 $feedback = $this->split_truefalse_comment($comment);
370 if ($answer == "T" OR $answer == "TRUE") {
371 $question->answer = 1;
372 $question->feedbacktrue = $feedback['right'];
373 $question->feedbackfalse = $feedback['wrong'];
374 } else {
375 $question->answer = 0;
376 $question->feedbackfalse = $feedback['right'];
377 $question->feedbacktrue = $feedback['wrong'];
380 $question->correctanswer = $question->answer;
382 return $question;
383 break;
385 case SHORTANSWER:
386 // SHORTANSWER Question
387 $answers = explode("=", $answertext);
388 if (isset($answers[0])) {
389 $answers[0] = trim($answers[0]);
391 if (empty($answers[0])) {
392 array_shift($answers);
395 if (!$this->check_answer_count( 1,$answers,$text )) {
396 return false;
397 break;
400 foreach ($answers as $key => $answer) {
401 $answer = trim($answer);
403 // Answer Weight
404 if (ereg($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
405 $answer_weight = $this->answerweightparser($answer);
406 } else { //default, i.e., full-credit anwer
407 $answer_weight = 1;
409 $question->fraction[$key] = $answer_weight;
410 $question->feedback[$key] = $this->commentparser($answer); //commentparser also removes comment from $answer
411 $question->answer[$key] = addslashes($this->escapedchar_post($answer));
412 } // end foreach
414 //$question->usecase = 0; // Ignore case
415 //$question->defaultgrade = 1;
416 //$question->image = ""; // No images with this format
417 return $question;
418 break;
420 case NUMERICAL:
421 // Note similarities to ShortAnswer
422 $answertext = substr($answertext, 1); // remove leading "#"
424 // If there is feedback for a wrong answer, store it for now.
425 if (($pos = strpos($answertext, '~')) !== false) {
426 $wrongfeedback = substr($answertext, $pos);
427 $answertext = substr($answertext, 0, $pos);
428 } else {
429 $wrongfeedback = '';
432 $answers = explode("=", $answertext);
433 if (isset($answers[0])) {
434 $answers[0] = trim($answers[0]);
436 if (empty($answers[0])) {
437 array_shift($answers);
440 if (count($answers) == 0) {
441 // invalid question
442 $giftnonumericalanswers = get_string('giftnonumericalanswers','quiz');
443 $this->error( $giftnonumericalanswers, $text );
444 return false;
445 break;
448 foreach ($answers as $key => $answer) {
449 $answer = trim($answer);
451 // Answer weight
452 if (ereg($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
453 $answer_weight = $this->answerweightparser($answer);
454 } else { //default, i.e., full-credit anwer
455 $answer_weight = 1;
457 $question->fraction[$key] = $answer_weight;
458 $question->feedback[$key] = $this->commentparser($answer); //commentparser also removes comment from $answer
460 //Calculate Answer and Min/Max values
461 if (strpos($answer,"..") > 0) { // optional [min]..[max] format
462 $marker = strpos($answer,"..");
463 $max = trim(substr($answer, $marker+2));
464 $min = trim(substr($answer, 0, $marker));
465 $ans = ($max + $min)/2;
466 $tol = $max - $ans;
467 } elseif (strpos($answer,":") > 0){ // standard [answer]:[errormargin] format
468 $marker = strpos($answer,":");
469 $tol = trim(substr($answer, $marker+1));
470 $ans = trim(substr($answer, 0, $marker));
471 } else { // only one valid answer (zero errormargin)
472 $tol = 0;
473 $ans = trim($answer);
476 if (!(is_numeric($ans) || $ans = '*') || !is_numeric($tol)) {
477 $errornotnumbers = get_string( 'errornotnumbers' );
478 $this->error( $errornotnumbers, $text );
479 return false;
480 break;
483 // store results
484 $question->answer[$key] = $ans;
485 $question->tolerance[$key] = $tol;
486 } // end foreach
488 if ($wrongfeedback) {
489 $key += 1;
490 $question->fraction[$key] = 0;
491 $question->feedback[$key] = $this->commentparser($wrongfeedback);
492 $question->answer[$key] = '';
493 $question->tolerance[$key] = '';
496 return $question;
497 break;
499 default:
500 $giftnovalidquestion = get_string('giftnovalidquestion','quiz');
501 $this->error( $giftnovalidquestion, $text );
502 return false;
503 break;
505 } // end switch ($question->qtype)
507 } // end function readquestion($lines)
509 function repchar( $text, $format=0 ) {
510 // escapes 'reserved' characters # = ~ { ) : and removes new lines
511 // also pushes text through format routine
512 $reserved = array( '#', '=', '~', '{', '}', ':', "\n","\r");
513 $escaped = array( '\#','\=','\~','\{','\}','\:','\n','' ); //dlnsk
515 $newtext = str_replace( $reserved, $escaped, $text );
516 $format = 0; // turn this off for now
517 if ($format) {
518 $newtext = format_text( $format );
520 return $newtext;
523 function writequestion( $question ) {
524 // turns question into string
525 // question reflects database fields for general question and specific to type
527 // initial string;
528 $expout = "";
530 // add comment
531 $expout .= "// question: $question->id name: $question->name \n";
533 // get question text format
534 $textformat = $question->questiontextformat;
535 $tfname = "";
536 if ($textformat!=FORMAT_MOODLE) {
537 $tfname = text_format_name( (int)$textformat );
538 $tfname = "[$tfname]";
541 // output depends on question type
542 switch($question->qtype) {
543 case 'category':
544 // not a real question, used to insert category switch
545 $expout .= "\$CATEGORY: $question->category\n";
546 break;
547 case DESCRIPTION:
548 $expout .= '::'.$this->repchar($question->name).'::';
549 $expout .= $tfname;
550 $expout .= $this->repchar( $question->questiontext, $textformat);
551 break;
552 case ESSAY:
553 $expout .= '::'.$this->repchar($question->name).'::';
554 $expout .= $tfname;
555 $expout .= $this->repchar( $question->questiontext, $textformat);
556 $expout .= "{}\n";
557 break;
558 case TRUEFALSE:
559 $trueanswer = $question->options->answers[$question->options->trueanswer];
560 $falseanswer = $question->options->answers[$question->options->falseanswer];
561 if ($trueanswer->fraction == 1) {
562 $answertext = 'TRUE';
563 $right_feedback = $trueanswer->feedback;
564 $wrong_feedback = $falseanswer->feedback;
565 } else {
566 $answertext = 'FALSE';
567 $right_feedback = $falseanswer->feedback;
568 $wrong_feedback = $trueanswer->feedback;
571 $wrong_feedback = $this->repchar($wrong_feedback);
572 $right_feedback = $this->repchar($right_feedback);
573 $expout .= "::".$this->repchar($question->name)."::".$tfname.$this->repchar( $question->questiontext,$textformat )."{".$this->repchar( $answertext );
574 if ($wrong_feedback) {
575 $expout .= "#" . $wrong_feedback;
576 } else if ($right_feedback) {
577 $expout .= "#";
579 if ($right_feedback) {
580 $expout .= "#" . $right_feedback;
582 $expout .= "}\n";
583 break;
584 case MULTICHOICE:
585 $expout .= "::".$this->repchar($question->name)."::".$tfname.$this->repchar( $question->questiontext, $textformat )."{\n";
586 foreach($question->options->answers as $answer) {
587 if ($answer->fraction==1) {
588 $answertext = '=';
590 elseif ($answer->fraction==0) {
591 $answertext = '~';
593 else {
594 $export_weight = $answer->fraction*100;
595 $answertext = "~%$export_weight%";
597 $expout .= "\t".$answertext.$this->repchar( $answer->answer );
598 if ($answer->feedback!="") {
599 $expout .= "#".$this->repchar( $answer->feedback );
601 $expout .= "\n";
603 $expout .= "}\n";
604 break;
605 case SHORTANSWER:
606 $expout .= "::".$this->repchar($question->name)."::".$tfname.$this->repchar( $question->questiontext, $textformat )."{\n";
607 foreach($question->options->answers as $answer) {
608 $weight = 100 * $answer->fraction;
609 $expout .= "\t=%".$weight."%".$this->repchar( $answer->answer )."#".$this->repchar( $answer->feedback )."\n";
611 $expout .= "}\n";
612 break;
613 case NUMERICAL:
614 $expout .= "::".$this->repchar($question->name)."::".$tfname.$this->repchar( $question->questiontext, $textformat )."{#\n";
615 foreach ($question->options->answers as $answer) {
616 if ($answer->answer != '') {
617 $expout .= "\t=".$answer->answer.":".(float)$answer->tolerance."#".$this->repchar( $answer->feedback )."\n";
618 } else {
619 $expout .= "\t~#".$this->repchar( $answer->feedback )."\n";
622 $expout .= "}\n";
623 break;
624 case MATCH:
625 $expout .= "::".$this->repchar($question->name)."::".$tfname.$this->repchar( $question->questiontext, $textformat )."{\n";
626 foreach($question->options->subquestions as $subquestion) {
627 $expout .= "\t=".$this->repchar( $subquestion->questiontext )." -> ".$this->repchar( $subquestion->answertext )."\n";
629 $expout .= "}\n";
630 break;
631 case DESCRIPTION:
632 $expout .= "// DESCRIPTION type is not supported\n";
633 break;
634 case MULTIANSWER:
635 $expout .= "// CLOZE type is not supported\n";
636 break;
637 default:
638 // check for plugins
639 if ($out = $this->try_exporting_using_qtypes( $question->qtype, $question )) {
640 $expout .= $out;
642 else {
643 notify("No handler for qtype '$question->qtype' for GIFT export" );
646 // add empty line to delimit questions
647 $expout .= "\n";
648 return $expout;