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/>.
16
17/**
18 * Missing word question importer.
19 *
20 * @package    qformat_missingword
21 * @copyright  1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * Missing word question importer.
31 *
32 * This Moodle class provides all functions necessary to import and export
33 * one-correct-answer multiple choice questions in this format:
34 *
35 *    As soon as we begin to explore our body parts as infants
36 *    we become students of {=anatomy and physiology ~reflexology
37 *    ~science ~experiment}, and in a sense we remain students for life.
38 *
39 * Each answer is separated with a tilde ~, and the correct answer is
40 * prefixed with an equals sign =
41 *
42 * Percentage weights can be included by following the tilde with the
43 * desired percent.  Comments can be included for each choice by following
44 * the comment with a hash mark ("#") and the comment.  Example:
45 *
46 *    This is {=the best answer#comment on the best answer ~75%a good
47 *    answer#comment on the good answer ~a wrong one#comment on the bad answer}
48 *
49 * @copyright  1999 onwards Martin Dougiamas {@link http://moodle.com}
50 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51 */
52class qformat_missingword extends qformat_default {
53
54    public function provide_import() {
55        return true;
56    }
57
58    public function readquestion($lines) {
59        // Given an array of lines known to define a question in
60        // this format, this function converts it into a question
61        // object suitable for processing and insertion into Moodle.
62
63        $question = $this->defaultquestion();
64        $comment = null; // Added by T Robb.
65        $text = implode(" ", $lines);
66
67        // Find answer section.
68
69        $answerstart = strpos($text, "{");
70        if ($answerstart === false) {
71            $this->error(get_string('beginanswernotfound', 'qformat_missingword'), $text);
72            return false;
73        }
74
75        $answerfinish = strpos($text, "}");
76        if ($answerfinish === false) {
77            $this->error(get_string('endanswernotfound', 'qformat_missingword'), $text);
78            return false;
79        }
80
81        $answerlength = $answerfinish - $answerstart;
82        $answertext = substr($text, $answerstart + 1, $answerlength - 1);
83
84        // Save the new question text.
85        $question->questiontext = substr_replace($text, "_____", $answerstart, $answerlength+1);
86        $question->name = $this->create_default_question_name($question->questiontext, get_string('questionname', 'question'));
87
88        // Parse the answers.
89        $answertext = str_replace("=", "~=", $answertext);
90        $answers = explode("~", $answertext);
91        if (isset($answers[0])) {
92            $answers[0] = trim($answers[0]);
93        }
94        if (empty($answers[0])) {
95            array_shift($answers);
96        }
97
98        $countanswers = count($answers);
99
100        switch ($countanswers) {
101            case 0:  // Invalid question.
102                $this->error(get_string('noanswerfound', 'qformat_missingword'), $answertext);
103                return false;
104
105            case 1:
106                $question->qtype = 'shortanswer';
107
108                $answer = trim($answers[0]);
109                if ($answer[0] == "=") {
110                    $answer = substr($answer, 1);
111                }
112                $question->answer[]   = $answer;
113                $question->fraction[] = 1;
114                $question->feedback[] = array('text' => '', 'format' => FORMAT_HTML);
115
116                return $question;
117
118            default:
119                $question->qtype = 'multichoice';
120                $question = $this->add_blank_combined_feedback($question);
121                $question->single = 1; // Only one answer allowed.
122
123                foreach ($answers as $key => $answer) {
124                    $answer = trim($answer);
125
126                    // Tom's addition starts here.
127                    $answeight = 0;
128                    if (strspn($answer, "1234567890%") > 0) {
129                        // Make sure that the percent sign is the last in the span.
130                        if (strpos($answer, "%") == strspn($answer, "1234567890%") - 1) {
131                            $answeight0 = substr($answer, 0, strspn($answer, "1234567890%"));
132                            $answeight = round(($answeight0/100), 2);
133                            $answer = substr($answer, (strspn($answer, "1234567890%")));
134                        }
135                    }
136                    if ($answer[0] == "=") {
137                        $answeight = 1;
138                    }
139                    // Remove the protective underscore for leading numbers in answers.
140                    if ($answer[0] == "_") {
141                        $answer = substr($answer, 1);
142                    }
143                    $answer = trim($answer);
144
145                    if (strpos($answer, "#") > 0) {
146                        $hashpos = strpos($answer, "#");
147                        $comment = substr(($answer), $hashpos+1);
148                        $answer  = substr($answer, 0, $hashpos);
149                    } else {
150                        $comment = " ";
151                    }
152                    // End of Tom's addition.
153
154                    if ($answer[0] == "=") {
155                        $question->fraction[$key] = $answeight;
156                        $answer = substr($answer, 1);
157                    } else {
158                        $question->fraction[$key] = $answeight;
159                    }
160                    $question->answer[$key]   = array('text' => $answer, 'format' => FORMAT_HTML);
161                    $question->feedback[$key] = array('text' => $comment, 'format' => FORMAT_HTML);
162                }
163
164                return $question;
165        }
166    }
167}
168
169
170