|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 00053 class qformat_missingword extends qformat_default { 00054 00055 function provide_import() { 00056 return true; 00057 } 00058 00059 function readquestion($lines) { 00063 00064 $question = $this->defaultquestion(); 00066 $comment = NULL; 00067 $text = implode(" ", $lines); 00068 00070 00071 $answerstart = strpos($text, "{"); 00072 if ($answerstart === false) { 00073 if ($this->displayerrors) { 00074 echo "<p>$text<p>Could not find a {"; 00075 } 00076 return false; 00077 } 00078 00079 $answerfinish = strpos($text, "}"); 00080 if ($answerfinish === false) { 00081 if ($this->displayerrors) { 00082 echo "<p>$text<p>Could not find a }"; 00083 } 00084 return false; 00085 } 00086 00087 $answerlength = $answerfinish - $answerstart; 00088 $answertext = substr($text, $answerstart + 1, $answerlength - 1); 00089 00091 $question->questiontext = substr_replace($text, "_____", $answerstart, $answerlength+1); 00092 $question->name = $question->questiontext; 00093 00094 00096 $answertext = str_replace("=", "~=", $answertext); 00097 $answers = explode("~", $answertext); 00098 if (isset($answers[0])) { 00099 $answers[0] = trim($answers[0]); 00100 } 00101 if (empty($answers[0])) { 00102 array_shift($answers); 00103 } 00104 00105 $countanswers = count($answers); 00106 00107 switch ($countanswers) { 00108 case 0: // invalid question 00109 if ($this->displayerrors) { 00110 echo "<p>No answers found in $answertext"; 00111 } 00112 return false; 00113 00114 case 1: 00115 $question->qtype = SHORTANSWER; 00116 00117 $answer = trim($answers[0]); 00118 if ($answer[0] == "=") { 00119 $answer = substr($answer, 1); 00120 } 00121 $question->answer[] = $answer; 00122 $question->fraction[] = 1; 00123 $question->feedback[] = ""; 00124 00125 return $question; 00126 00127 default: 00128 $question->qtype = MULTICHOICE; 00129 00130 foreach ($answers as $key => $answer) { 00131 $answer = trim($answer); 00132 00133 // Tom's addition starts here 00134 $answeight = 0; 00135 if (strspn($answer,"1234567890%") > 0){ 00136 //Make sure that the percent sign is the last in the span 00137 if (strpos($answer,"%") == strspn($answer,"1234567890%") - 1) { 00138 $answeight0 = substr($answer,0,strspn($answer,"1234567890%")); 00139 $answeight = round(($answeight0/100),2); 00140 $answer = substr($answer,(strspn($answer,"1234567890%"))); 00141 } 00142 } 00143 if ($answer[0] == "="){ 00144 $answeight = 1; 00145 } 00146 //remove the protective underscore for leading numbers in answers 00147 if ($answer[0] == "_"){ 00148 $answer = substr($answer, 1); 00149 } 00150 $answer = trim($answer); 00151 00152 if (strpos($answer,"#") > 0){ 00153 $hashpos = strpos($answer,"#"); 00154 $comment = substr(($answer),$hashpos+1); 00155 $answer = substr($answer,0,$hashpos); 00156 } else { 00157 $comment = " "; 00158 } 00159 // End of Tom's addition 00160 00161 if ($answer[0] == "=") { 00162 # $question->fraction[$key] = 1; 00163 $question->fraction[$key] = $answeight; 00164 $answer = substr($answer, 1); 00165 } else { 00166 # $question->fraction[$key] = 0; 00167 $question->fraction[$key] = $answeight; 00168 } 00169 $question->answer[$key] = $answer; 00170 $question->feedback[$key] = $comment; 00171 } 00172 00173 return $question; 00174 } 00175 } 00176 } 00177 00178