Align properly buttons and drop-down in navigation menu.
[moodle.git] / question / format / gift / format.php
CommitLineData
84769fd8 1<?php // $Id$
f1abd39f 2//
84769fd8 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}
20//
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
41a89a07 34/**
35 * @package questionbank
36 * @subpackage importexport
37 */
f5565b69 38class qformat_gift extends qformat_default {
84769fd8 39
40 function provide_import() {
09db6da2 41 return true;
84769fd8 42 }
43
44 function provide_export() {
09db6da2 45 return true;
84769fd8 46 }
47
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;
55 }
56
57
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 = " ";
66 }
67 return $comment;
68 }
69
70 function split_truefalse_comment($comment){
09db6da2 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'] = '';
79 }
80 return $feedback;
84769fd8 81 }
82
83 function escapedchar_pre($string) {
84 //Replaces escaped control characters with a placeholder BEFORE processing
85
dfdce7fb 86 $escapedcharacters = array("\\:", "\\#", "\\=", "\\{", "\\}", "\\~", "\\n" ); //dlnsk
87 $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010" ); //dlnsk
84769fd8 88
89 $string = str_replace("\\\\", "&&092;", $string);
90 $string = str_replace($escapedcharacters, $placeholders, $string);
91 $string = str_replace("&&092;", "\\", $string);
92 return $string;
93 }
94
95 function escapedchar_post($string) {
96 //Replaces placeholders with corresponding character AFTER processing is done
dfdce7fb 97 $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010"); //dlnsk
98 $characters = array(":", "#", "=", "{", "}", "~", "\n" ); //dlnsk
84769fd8 99 $string = str_replace($placeholders, $characters, $string);
100 return $string;
101 }
102
103 function check_answer_count( $min, $answers, $text ) {
09db6da2 104 $countanswers = count($answers);
105 if ($countanswers < $min) {
f3701561 106 $importminerror = get_string( 'importminerror', 'quiz' );
107 $this->error( $importminerror, $text );
09db6da2 108 return false;
109 }
110
111 return true;
84769fd8 112 }
113
114
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.
118
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]*)%";
123
124 // REMOVED COMMENTED LINES and IMPLODE
125 foreach ($lines as $key => $line) {
09db6da2 126 $line = trim($line);
127 if (substr($line, 0, 2) == "//") {
84769fd8 128 $lines[$key] = " ";
09db6da2 129 }
84769fd8 130 }
131
132 $text = trim(implode(" ", $lines));
133
134 if ($text == "") {
84769fd8 135 return false;
136 }
137
138 // Substitute escaped control characters with placeholders
139 $text = $this->escapedchar_pre($text);
140
b39c7aad 141 // Look for category modifier
5363b555 142 if (ereg( '^\$CATEGORY:', $text)) {
143 // $newcategory = $matches[1];
144 $newcategory = trim(substr( $text, 10 ));
b39c7aad 145
146 // build fake question to contain category
147 $question->qtype = 'category';
148 $question->category = $newcategory;
149 return $question;
150 }
5363b555 151
84769fd8 152 // QUESTION NAME parser
153 if (substr($text, 0, 2) == "::") {
154 $text = substr($text, 2);
155
156 $namefinish = strpos($text, "::");
157 if ($namefinish === false) {
158 $question->name = false;
159 // name will be assigned after processing question text below
09db6da2 160 } else {
84769fd8 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
164 }
165 } else {
166 $question->name = false;
167 }
168
169
170 // FIND ANSWER section
171 $answerstart = strpos($text, "{");
172 if ($answerstart === false) {
532344b2 173 $giftleftbraceerror = get_string( 'giftleftbraceerror', 'quiz' );
174 $this->error( $giftleftbraceerror, $text );
84769fd8 175 return false;
176 }
177
178 $answerfinish = strpos($text, "}");
179 if ($answerfinish === false) {
532344b2 180 $giftrightbraceerror = get_string( 'giftrightbraceerror', 'quiz' );
181 $this->error( $giftrightbraceerror, $text );
84769fd8 182 return false;
183 }
184
185 $answerlength = $answerfinish - $answerstart;
186 $answertext = trim(substr($text, $answerstart + 1, $answerlength - 1));
187
188 // Format QUESTION TEXT without answer, inserting "_____" as necessary
189 if (substr($text, -1) == "}") {
190 // no blank line if answers follow question, outside of closing punctuation
191 $questiontext = substr_replace($text, "", $answerstart, $answerlength+1);
192 } else {
193 // inserts blank line for missing word format
194 $questiontext = substr_replace($text, "_____", $answerstart, $answerlength+1);
195 }
196
197 // get questiontext format from questiontext
198 $oldquestiontext = $questiontext;
199 $questiontextformat = 0;
200 if (substr($questiontext,0,1)=='[') {
09db6da2 201 $questiontext = substr( $questiontext,1 );
202 $rh_brace = strpos( $questiontext, ']' );
203 $qtformat= substr( $questiontext, 0, $rh_brace );
204 $questiontext = substr( $questiontext, $rh_brace+1 );
205 if (!$questiontextformat = text_format_name( $qtformat )) {
206 $questiontext = $oldquestiontext;
207 }
84769fd8 208 }
209 $question->questiontextformat = $questiontextformat;
210 $question->questiontext = addslashes(trim($this->escapedchar_post($questiontext)));
211
212 // set question name if not already set
213 if ($question->name === false) {
214 $question->name = $question->questiontext;
215 }
216
8daaaafc 217 // ensure name is not longer than 250 characters
218 $question->name = shorten_text( $question->name, 250 );
84769fd8 219
09db6da2 220 // determine QUESTION TYPE
84769fd8 221 $question->qtype = NULL;
222
223 if ($answertext{0} == "#"){
224 $question->qtype = NUMERICAL;
225
226 } elseif (strpos($answertext, "~") !== false) {
227 // only Multiplechoice questions contain tilde ~
228 $question->qtype = MULTICHOICE;
229
230 } elseif (strpos($answertext, "=") !== false
09db6da2 231 && strpos($answertext, "->") !== false) {
232 // only Matching contains both = and ->
84769fd8 233 $question->qtype = MATCH;
234
235 } else { // either TRUEFALSE or SHORTANSWER
236
237 // TRUEFALSE question check
238 $truefalse_check = $answertext;
239 if (strpos($answertext,"#") > 0){
240 // strip comments to check for TrueFalse question
241 $truefalse_check = trim(substr($answertext, 0, strpos($answertext,"#")));
242 }
243
244 $valid_tf_answers = array("T", "TRUE", "F", "FALSE");
245 if (in_array($truefalse_check, $valid_tf_answers)) {
246 $question->qtype = TRUEFALSE;
247
248 } else { // Must be SHORTANSWER
249 $question->qtype = SHORTANSWER;
250 }
251 }
252
253 if (!isset($question->qtype)) {
532344b2 254 $giftqtypenotset = get_string('giftqtypenotset','quiz');
255 $this->error( $giftqtypenotset, $text );
84769fd8 256 return false;
257 }
258
259 switch ($question->qtype) {
260 case MULTICHOICE:
261 if (strpos($answertext,"=") === false) {
262 $question->single = 0; // multiple answers are enabled if no single answer is 100% correct
263 } else {
264 $question->single = 1; // only one answer allowed (the default)
265 }
266
267 $answertext = str_replace("=", "~=", $answertext);
268 $answers = explode("~", $answertext);
269 if (isset($answers[0])) {
270 $answers[0] = trim($answers[0]);
271 }
272 if (empty($answers[0])) {
273 array_shift($answers);
274 }
275
276 $countanswers = count($answers);
277
2befe778 278 if (!$this->check_answer_count( 2,$answers,$text )) {
279 return false;
280 break;
84769fd8 281 }
282
283 foreach ($answers as $key => $answer) {
284 $answer = trim($answer);
285
286 // determine answer weight
287 if ($answer[0] == "=") {
288 $answer_weight = 1;
289 $answer = substr($answer, 1);
290
291 } elseif (ereg($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
292 $answer_weight = $this->answerweightparser($answer);
293
294 } else { //default, i.e., wrong anwer
295 $answer_weight = 0;
296 }
297 $question->fraction[$key] = $answer_weight;
298 $question->feedback[$key] = $this->commentparser($answer); // commentparser also removes comment from $answer
172f6d95 299 $question->answer[$key] = addslashes($this->escapedchar_post($answer));
300 $question->correctfeedback = '';
301 $question->partiallycorrectfeedback = '';
302 $question->incorrectfeedback = '';
84769fd8 303 } // end foreach answer
304
305 //$question->defaultgrade = 1;
306 //$question->image = ""; // No images with this format
307 return $question;
308 break;
309
310 case MATCH:
311 $answers = explode("=", $answertext);
312 if (isset($answers[0])) {
313 $answers[0] = trim($answers[0]);
314 }
315 if (empty($answers[0])) {
316 array_shift($answers);
317 }
318
2befe778 319 if (!$this->check_answer_count( 2,$answers,$text )) {
320 return false;
321 break;
84769fd8 322 }
323
324 foreach ($answers as $key => $answer) {
325 $answer = trim($answer);
87ee4968 326 if (strpos($answer, "->") === false) {
532344b2 327 $giftmatchingformat = get_string('giftmatchingformat','quiz');
328 $this->error($giftmatchingformat, $answer );
84769fd8 329 return false;
330 break 2;
331 }
332
333 $marker = strpos($answer,"->");
334 $question->subquestions[$key] = addslashes(trim($this->escapedchar_post(substr($answer, 0, $marker))));
335 $question->subanswers[$key] = addslashes(trim($this->escapedchar_post(substr($answer, $marker+2))));
336
337 } // end foreach answer
338
84769fd8 339 return $question;
340 break;
341
342 case TRUEFALSE:
343 $answer = $answertext;
344 $comment = $this->commentparser($answer); // commentparser also removes comment from $answer
09db6da2 345 $feedback = $this->split_truefalse_comment($comment);
84769fd8 346
347 if ($answer == "T" OR $answer == "TRUE") {
348 $question->answer = 1;
09db6da2 349 $question->feedbacktrue = $feedback['right'];
350 $question->feedbackfalse = $feedback['wrong'];
84769fd8 351 } else {
352 $question->answer = 0;
09db6da2 353 $question->feedbackfalse = $feedback['right'];
354 $question->feedbacktrue = $feedback['wrong'];
84769fd8 355 }
356
7939a4a0 357 $question->correctanswer = $question->answer;
358
84769fd8 359 return $question;
360 break;
361
362 case SHORTANSWER:
363 // SHORTANSWER Question
364 $answers = explode("=", $answertext);
365 if (isset($answers[0])) {
366 $answers[0] = trim($answers[0]);
367 }
368 if (empty($answers[0])) {
369 array_shift($answers);
370 }
371
2befe778 372 if (!$this->check_answer_count( 1,$answers,$text )) {
373 return false;
374 break;
84769fd8 375 }
376
377 foreach ($answers as $key => $answer) {
378 $answer = trim($answer);
379
380 // Answer Weight
381 if (ereg($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
382 $answer_weight = $this->answerweightparser($answer);
383 } else { //default, i.e., full-credit anwer
384 $answer_weight = 1;
385 }
386 $question->fraction[$key] = $answer_weight;
387 $question->feedback[$key] = $this->commentparser($answer); //commentparser also removes comment from $answer
388 $question->answer[$key] = addslashes($this->escapedchar_post($answer));
389 } // end foreach
390
391 //$question->usecase = 0; // Ignore case
392 //$question->defaultgrade = 1;
393 //$question->image = ""; // No images with this format
394 return $question;
395 break;
396
397 case NUMERICAL:
398 // Note similarities to ShortAnswer
399 $answertext = substr($answertext, 1); // remove leading "#"
400
1fe641f7 401 // If there is feedback for a wrong answer, store it for now.
402 if (($pos = strpos($answertext, '~')) !== false) {
403 $wrongfeedback = substr($answertext, $pos);
404 $answertext = substr($answertext, 0, $pos);
405 } else {
406 $wrongfeedback = '';
407 }
408
84769fd8 409 $answers = explode("=", $answertext);
410 if (isset($answers[0])) {
411 $answers[0] = trim($answers[0]);
412 }
413 if (empty($answers[0])) {
414 array_shift($answers);
415 }
416
417 if (count($answers) == 0) {
418 // invalid question
532344b2 419 $giftnonumericalanswers = get_string('giftnonumericalanswers','quiz');
420 $this->error( $giftnonumericalanswers, $text );
84769fd8 421 return false;
422 break;
423 }
424
425 foreach ($answers as $key => $answer) {
426 $answer = trim($answer);
427
428 // Answer weight
429 if (ereg($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
430 $answer_weight = $this->answerweightparser($answer);
431 } else { //default, i.e., full-credit anwer
432 $answer_weight = 1;
433 }
434 $question->fraction[$key] = $answer_weight;
435 $question->feedback[$key] = $this->commentparser($answer); //commentparser also removes comment from $answer
436
437 //Calculate Answer and Min/Max values
438 if (strpos($answer,"..") > 0) { // optional [min]..[max] format
439 $marker = strpos($answer,"..");
440 $max = trim(substr($answer, $marker+2));
441 $min = trim(substr($answer, 0, $marker));
442 $ans = ($max + $min)/2;
443 $tol = $max - $ans;
444 } elseif (strpos($answer,":") > 0){ // standard [answer]:[errormargin] format
445 $marker = strpos($answer,":");
446 $tol = trim(substr($answer, $marker+1));
447 $ans = trim(substr($answer, 0, $marker));
448 } else { // only one valid answer (zero errormargin)
449 $tol = 0;
450 $ans = trim($answer);
451 }
452
09db6da2 453 if (!(is_numeric($ans) || $ans = '*') || !is_numeric($tol)) {
f3701561 454 $errornotnumbers = get_string( 'errornotnumbers' );
455 $this->error( $errornotnumbers, $text );
84769fd8 456 return false;
457 break;
458 }
459
460 // store results
461 $question->answer[$key] = $ans;
462 $question->tolerance[$key] = $tol;
463 } // end foreach
464
1fe641f7 465 if ($wrongfeedback) {
466 $key += 1;
467 $question->fraction[$key] = 0;
468 $question->feedback[$key] = $this->commentparser($wrongfeedback);
469 $question->answer[$key] = '';
470 $question->tolerance[$key] = '';
471 }
472
84769fd8 473 return $question;
474 break;
475
476 default:
532344b2 477 $giftnovalidquestion = get_string('giftnovalidquestion','quiz');
478 $this->error( $giftnovalidquestion, $text );
84769fd8 479 return false;
480 break;
481
482 } // end switch ($question->qtype)
483
484 } // end function readquestion($lines)
485
486function repchar( $text, $format=0 ) {
dfdce7fb 487 // escapes 'reserved' characters # = ~ { ) : and removes new lines
84769fd8 488 // also pushes text through format routine
dfdce7fb 489 $reserved = array( '#', '=', '~', '{', '}', ':', "\n","\r");
490 $escaped = array( '\#','\=','\~','\{','\}','\:','\n','' ); //dlnsk
84769fd8 491
492 $newtext = str_replace( $reserved, $escaped, $text );
493 $format = 0; // turn this off for now
494 if ($format) {
09db6da2 495 $newtext = format_text( $format );
84769fd8 496 }
497 return $newtext;
498 }
499
500function writequestion( $question ) {
501 // turns question into string
502 // question reflects database fields for general question and specific to type
503
504 // initial string;
505 $expout = "";
506
507 // add comment
508 $expout .= "// question: $question->id name: $question->name \n";
509
510 // get question text format
511 $textformat = $question->questiontextformat;
512 $tfname = "";
513 if ($textformat!=FORMAT_MOODLE) {
09db6da2 514 $tfname = text_format_name( (int)$textformat );
515 $tfname = "[$tfname]";
84769fd8 516 }
517
518 // output depends on question type
519 switch($question->qtype) {
f1abd39f 520 case 'category':
521 // not a real question, used to insert category switch
522 $expout .= "\$CATEGORY: $question->category\n";
523 break;
84769fd8 524 case TRUEFALSE:
09db6da2 525 $trueanswer = $question->options->answers[$question->options->trueanswer];
526 $falseanswer = $question->options->answers[$question->options->falseanswer];
527 if ($trueanswer->fraction == 1) {
528 $answertext = 'TRUE';
529 $right_feedback = $trueanswer->feedback;
530 $wrong_feedback = $falseanswer->feedback;
531 } else {
532 $answertext = 'FALSE';
533 $right_feedback = $falseanswer->feedback;
534 $wrong_feedback = $trueanswer->feedback;
84769fd8 535 }
36e2232e 536
09db6da2 537 $wrong_feedback = $this->repchar($wrong_feedback);
538 $right_feedback = $this->repchar($right_feedback);
539 $expout .= "::".$this->repchar($question->name)."::".$tfname.$this->repchar( $question->questiontext,$textformat )."{".$this->repchar( $answertext );
540 if ($wrong_feedback) {
541 $expout .= "#" . $wrong_feedback;
542 } else if ($right_feedback) {
543 $expout .= "#";
84769fd8 544 }
09db6da2 545 if ($right_feedback) {
546 $expout .= "#" . $right_feedback;
84769fd8 547 }
548 $expout .= "}\n";
549 break;
550 case MULTICHOICE:
dfdce7fb 551 $expout .= "::".$this->repchar($question->name)."::".$tfname.$this->repchar( $question->questiontext, $textformat )."{\n";
84769fd8 552 foreach($question->options->answers as $answer) {
553 if ($answer->fraction==1) {
554 $answertext = '=';
555 }
556 elseif ($answer->fraction==0) {
557 $answertext = '~';
558 }
559 else {
09db6da2 560 $export_weight = $answer->fraction*100;
561 $answertext = "~%$export_weight%";
84769fd8 562 }
563 $expout .= "\t".$answertext.$this->repchar( $answer->answer );
564 if ($answer->feedback!="") {
565 $expout .= "#".$this->repchar( $answer->feedback );
566 }
567 $expout .= "\n";
568 }
569 $expout .= "}\n";
570 break;
571 case SHORTANSWER:
dfdce7fb 572 $expout .= "::".$this->repchar($question->name)."::".$tfname.$this->repchar( $question->questiontext, $textformat )."{\n";
84769fd8 573 foreach($question->options->answers as $answer) {
574 $weight = 100 * $answer->fraction;
575 $expout .= "\t=%".$weight."%".$this->repchar( $answer->answer )."#".$this->repchar( $answer->feedback )."\n";
576 }
577 $expout .= "}\n";
578 break;
579 case NUMERICAL:
dfdce7fb 580 $expout .= "::".$this->repchar($question->name)."::".$tfname.$this->repchar( $question->questiontext, $textformat )."{#\n";
1fe641f7 581 foreach ($question->options->answers as $answer) {
1fe641f7 582 if ($answer->answer != '') {
583 $expout .= "\t=".$answer->answer.":".(float)$answer->tolerance."#".$this->repchar( $answer->feedback )."\n";
584 } else {
585 $expout .= "\t~#".$this->repchar( $answer->feedback )."\n";
586 }
587 }
84769fd8 588 $expout .= "}\n";
589 break;
590 case MATCH:
dfdce7fb 591 $expout .= "::".$this->repchar($question->name)."::".$tfname.$this->repchar( $question->questiontext, $textformat )."{\n";
84769fd8 592 foreach($question->options->subquestions as $subquestion) {
593 $expout .= "\t=".$this->repchar( $subquestion->questiontext )." -> ".$this->repchar( $subquestion->answertext )."\n";
594 }
595 $expout .= "}\n";
596 break;
597 case DESCRIPTION:
598 $expout .= "// DESCRIPTION type is not supported\n";
599 break;
600 case MULTIANSWER:
601 $expout .= "// CLOZE type is not supported\n";
602 break;
603 default:
604 notify("No handler for qtype $question->qtype for GIFT export" );
605 }
606 // add empty line to delimit questions
607 $expout .= "\n";
608 return $expout;
609}
610}
611?>