MDL-37172 Hardcoded strings in some question imports formats
[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     public function provide_import() {
56       return true;
57     }
59     public 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             $this->error(get_string('beginanswernotfound', 'qformat_missingword'), $text);
74             return false;
75         }
77         $answerfinish = strpos($text, "}");
78         if ($answerfinish === false) {
79             $this->error(get_string('endanswernotfound', 'qformat_missingword'), $text);
80             return false;
81         }
83         $answerlength = $answerfinish - $answerstart;
84         $answertext = substr($text, $answerstart + 1, $answerlength - 1);
86         // Save the new question text.
87         $question->questiontext = substr_replace($text, "_____", $answerstart, $answerlength+1);
88         $question->name = $this->create_default_question_name($question->questiontext, get_string('questionname', 'question'));
90         /// Parse the answers
91         $answertext = str_replace("=", "~=", $answertext);
92         $answers = explode("~", $answertext);
93         if (isset($answers[0])) {
94             $answers[0] = trim($answers[0]);
95         }
96         if (empty($answers[0])) {
97             array_shift($answers);
98         }
100         $countanswers = count($answers);
102         switch ($countanswers) {
103             case 0:  // Invalid question.
104                 $this->error(get_string('noanswerfound', 'qformat_missingword'), $answertext);
105                 return false;
107             case 1:
108                 $question->qtype = 'shortanswer';
110                 $answer = trim($answers[0]);
111                 if ($answer[0] == "=") {
112                     $answer = substr($answer, 1);
113                 }
114                 $question->answer[]   = $answer;
115                 $question->fraction[] = 1;
116                 $question->feedback[] = array('text' => '', 'format' => FORMAT_HTML);
118                 return $question;
120             default:
121                 $question->qtype = 'multichoice';
122                 $question = $this->add_blank_combined_feedback($question);
123                 $question->single = 1; // Only one answer allowed.
125                 foreach ($answers as $key => $answer) {
126                     $answer = trim($answer);
128                     // Tom's addition starts here
129                     $answeight = 0;
130                     if (strspn($answer,"1234567890%") > 0){
131                         //Make sure that the percent sign is the last in the span
132                         if (strpos($answer,"%") == strspn($answer,"1234567890%") - 1) {
133                             $answeight0 = substr($answer,0,strspn($answer,"1234567890%"));
134                             $answeight = round(($answeight0/100),2);
135                             $answer = substr($answer,(strspn($answer,"1234567890%")));
136                         }
137                     }
138                     if ($answer[0] == "="){
139                         $answeight = 1;
140                     }
141                     //remove the protective underscore for leading numbers in answers
142                     if ($answer[0] == "_"){
143                         $answer = substr($answer, 1);
144                     }
145                     $answer = trim($answer);
147                     if (strpos($answer,"#") > 0){
148                         $hashpos = strpos($answer,"#");
149                         $comment = substr(($answer),$hashpos+1);
150                         $answer  = substr($answer,0,$hashpos);
151                     } else {
152                         $comment = " ";
153                     }
154                     // End of Tom's addition
156                     if ($answer[0] == "=") {
157 #                       $question->fraction[$key] = 1;
158                         $question->fraction[$key] = $answeight;
159                         $answer = substr($answer, 1);
160                     } else {
161 #                       $question->fraction[$key] = 0;
162                         $question->fraction[$key] = $answeight;
163                     }
164                     $question->answer[$key]   = array('text' => $answer, 'format' => FORMAT_HTML);
165                     $question->feedback[$key] = array('text' => $comment, 'format' => FORMAT_HTML);
166                 }
168                 return $question;
169         }
170     }