c79998df2ee8cba117ff8e10c9130fbbf90f3352
[moodle.git] / question / format / missingword / format.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Missing word question importer.
19  *
20  * @package    qformat
21  * @subpackage missingword
22  * @copyright  1999 onwards Martin Dougiamas {@link http://moodle.com}
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
27 defined('MOODLE_INTERNAL') || die();
30 /**
31  * Missing word question importer.
32  *
33  * This Moodle class provides all functions necessary to import and export
34  * one-correct-answer multiple choice questions in this format:
35  *
36  *    As soon as we begin to explore our body parts as infants
37  *    we become students of {=anatomy and physiology ~reflexology
38  *    ~science ~experiment}, and in a sense we remain students for life.
39  *
40  * Each answer is separated with a tilde ~, and the correct answer is
41  * prefixed with an equals sign =
42  *
43  * Percentage weights can be included by following the tilde with the
44  * desired percent.  Comments can be included for each choice by following
45  * the comment with a hash mark ("#") and the comment.  Example:
46  *
47  *    This is {=the best answer#comment on the best answer ~75%a good
48  *    answer#comment on the good answer ~a wrong one#comment on the bad answer}
49  *
50  * @copyright  1999 onwards Martin Dougiamas {@link http://moodle.com}
51  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
52  */
53 class qformat_missingword extends qformat_default {
55     function provide_import() {
56       return true;
57     }
59     function readquestion($lines) {
60     /// Given an array of lines known to define a question in
61     /// this format, this function converts it into a question
62     /// object suitable for processing and insertion into Moodle.
64         $question = $this->defaultquestion();
65         ///$comment added by T Robb
66         $comment = NULL;
67         $text = implode(" ", $lines);
69         /// Find answer section
71         $answerstart = strpos($text, "{");
72         if ($answerstart === false) {
73             if ($this->displayerrors) {
74                 echo "<p>$text<p>Could not find a {";
75             }
76             return false;
77         }
79         $answerfinish = strpos($text, "}");
80         if ($answerfinish === false) {
81             if ($this->displayerrors) {
82                 echo "<p>$text<p>Could not find a }";
83             }
84             return false;
85         }
87         $answerlength = $answerfinish - $answerstart;
88         $answertext = substr($text, $answerstart + 1, $answerlength - 1);
90         /// Save the new question text
91         $question->questiontext = substr_replace($text, "_____", $answerstart, $answerlength+1);
92         $question->name = $this->create_default_question_name($question->questiontext, get_string('questionname', 'question'));
95         /// Parse the answers
96         $answertext = str_replace("=", "~=", $answertext);
97         $answers = explode("~", $answertext);
98         if (isset($answers[0])) {
99             $answers[0] = trim($answers[0]);
100         }
101         if (empty($answers[0])) {
102             array_shift($answers);
103         }
105         $countanswers = count($answers);
107         switch ($countanswers) {
108             case 0:  // invalid question
109                 if ($this->displayerrors) {
110                     echo "<p>No answers found in $answertext";
111                 }
112                 return false;
114             case 1:
115                 $question->qtype = 'shortanswer';
117                 $answer = trim($answers[0]);
118                 if ($answer[0] == "=") {
119                     $answer = substr($answer, 1);
120                 }
121                 $question->answer[]   = $answer;
122                 $question->fraction[] = 1;
123                 $question->feedback[] = "";
125                 return $question;
127             default:
128                 $question->qtype = 'multichoice';
130                 foreach ($answers as $key => $answer) {
131                     $answer = trim($answer);
133                     // Tom's addition starts here
134                     $answeight = 0;
135                     if (strspn($answer,"1234567890%") > 0){
136                         //Make sure that the percent sign is the last in the span
137                         if (strpos($answer,"%") == strspn($answer,"1234567890%") - 1) {
138                             $answeight0 = substr($answer,0,strspn($answer,"1234567890%"));
139                             $answeight = round(($answeight0/100),2);
140                             $answer = substr($answer,(strspn($answer,"1234567890%")));
141                         }
142                     }
143                     if ($answer[0] == "="){
144                         $answeight = 1;
145                     }
146                     //remove the protective underscore for leading numbers in answers
147                     if ($answer[0] == "_"){
148                         $answer = substr($answer, 1);
149                     }
150                     $answer = trim($answer);
152                     if (strpos($answer,"#") > 0){
153                         $hashpos = strpos($answer,"#");
154                         $comment = substr(($answer),$hashpos+1);
155                         $answer  = substr($answer,0,$hashpos);
156                     } else {
157                         $comment = " ";
158                     }
159                     // End of Tom's addition
161                     if ($answer[0] == "=") {
162 #                       $question->fraction[$key] = 1;
163                         $question->fraction[$key] = $answeight;
164                         $answer = substr($answer, 1);
165                     } else {
166 #                       $question->fraction[$key] = 0;
167                         $question->fraction[$key] = $answeight;
168                     }
169                     $question->answer[$key]   = $answer;
170                     $question->feedback[$key] = $comment;
171                 }
173                 return $question;
174         }
175     }