Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / question / format / missingword / format.php
blob0f1e9523afaae25f87cb6e0a7c071d7ee75e53c2
1 <?php // $Id$
2 /// Modified by Tom Robb 12 June 2003 to include percentage and comment insertion
3 /// facility.
5 ////////////////////////////////////////////////////////////////////////////
6 /// MISSING WORD FORMAT
7 ///
8 /// This Moodle class provides all functions necessary to import and export
9 /// one-correct-answer multiple choice questions in this format:
10 ///
11 /// As soon as we begin to explore our body parts as infants
12 /// we become students of {=anatomy and physiology ~reflexology
13 /// ~science ~experiment}, and in a sense we remain students for life.
14 ///
15 /// Each answer is separated with a tilde ~, and the correct answer is
16 /// prefixed with an equals sign =
17 ///
18 /// Percentage weights can be included by following the tilde with the
19 /// desired percent. Comments can be included for each choice by following
20 /// the comment with a hash mark ("#") and the comment. Example:
21 ///
22 /// This is {=the best answer#comment on the best answer ~75%a good
23 /// answer#comment on the good answer ~a wrong one#comment on the bad answer}
24 ///
25 ////////////////////////////////////////////////////////////////////////////
27 // Based on format.php, included by ../../import.php
28 /**
29 * @package questionbank
30 * @subpackage importexport
32 class qformat_missingword extends qformat_default {
34 function provide_import() {
35 return true;
38 function readquestion($lines) {
39 /// Given an array of lines known to define a question in
40 /// this format, this function converts it into a question
41 /// object suitable for processing and insertion into Moodle.
43 $question = $this->defaultquestion();
44 ///$comment added by T Robb
45 $comment = NULL;
46 $text = implode(" ", $lines);
48 /// Find answer section
50 $answerstart = strpos($text, "{");
51 if ($answerstart === false) {
52 if ($this->displayerrors) {
53 echo "<p>$text<p>Could not find a {";
55 return false;
58 $answerfinish = strpos($text, "}");
59 if ($answerfinish === false) {
60 if ($this->displayerrors) {
61 echo "<p>$text<p>Could not find a }";
63 return false;
66 $answerlength = $answerfinish - $answerstart;
67 $answertext = substr($text, $answerstart + 1, $answerlength - 1);
69 /// Save the new question text
70 $question->questiontext = addslashes(substr_replace($text, "_____", $answerstart, $answerlength+1));
71 $question->name = $question->questiontext;
74 /// Parse the answers
75 $answertext = str_replace("=", "~=", $answertext);
76 $answers = explode("~", $answertext);
77 if (isset($answers[0])) {
78 $answers[0] = trim($answers[0]);
80 if (empty($answers[0])) {
81 array_shift($answers);
84 $countanswers = count($answers);
86 switch ($countanswers) {
87 case 0: // invalid question
88 if ($this->displayerrors) {
89 echo "<p>No answers found in $answertext";
91 return false;
93 case 1:
94 $question->qtype = SHORTANSWER;
96 $answer = trim($answers[0]);
97 if ($answer[0] == "=") {
98 $answer = substr($answer, 1);
100 $question->answer[] = addslashes($answer);
101 $question->fraction[] = 1;
102 $question->feedback[] = "";
104 return $question;
106 default:
107 $question->qtype = MULTICHOICE;
109 foreach ($answers as $key => $answer) {
110 $answer = trim($answer);
112 // Tom's addition starts here
113 $answeight = 0;
114 if (strspn($answer,"1234567890%") > 0){
115 //Make sure that the percent sign is the last in the span
116 if (strpos($answer,"%") == strspn($answer,"1234567890%") - 1) {
117 $answeight0 = substr($answer,0,strspn($answer,"1234567890%"));
118 $answeight = round(($answeight0/100),2);
119 $answer = substr($answer,(strspn($answer,"1234567890%")));
122 if ($answer[0] == "="){
123 $answeight = 1;
125 //remove the protective underscore for leading numbers in answers
126 if ($answer[0] == "_"){
127 $answer = substr($answer, 1);
129 $answer = trim($answer);
131 if (strpos($answer,"#") > 0){
132 $hashpos = strpos($answer,"#");
133 $comment = addslashes(substr(($answer),$hashpos+1));
134 $answer = substr($answer,0,$hashpos);
135 } else {
136 $comment = " ";
138 // End of Tom's addition
140 if ($answer[0] == "=") {
141 # $question->fraction[$key] = 1;
142 $question->fraction[$key] = $answeight;
143 $answer = substr($answer, 1);
144 } else {
145 # $question->fraction[$key] = 0;
146 $question->fraction[$key] = $answeight;
148 $question->answer[$key] = addslashes($answer);
149 $question->feedback[$key] = $comment;
152 return $question;