MDL-74812 gradingform_rubric: Fix text overlap in rubric levels
[moodle.git] / question / format / missingword / format.php
blob96f9a20d7cd1b27a9e6f52a762decf6230ca3014
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.
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.
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
26 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Missing word question importer.
32 * This Moodle class provides all functions necessary to import and export
33 * one-correct-answer multiple choice questions in this format:
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.
39 * Each answer is separated with a tilde ~, and the correct answer is
40 * prefixed with an equals sign =
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:
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}
49 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
50 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
52 class qformat_missingword extends qformat_default {
54 public function provide_import() {
55 return true;
58 /**
59 * Validate the given file.
61 * For more expensive or detailed integrity checks.
63 * @param stored_file $file the file to check
64 * @return string the error message that occurred while validating the given file
66 public function validate_file(stored_file $file): string {
67 return $this->validate_is_utf8_file($file);
70 public function readquestion($lines) {
71 // Given an array of lines known to define a question in
72 // this format, this function converts it into a question
73 // object suitable for processing and insertion into Moodle.
75 $question = $this->defaultquestion();
76 $comment = null; // Added by T Robb.
77 $text = implode(" ", $lines);
79 // Find answer section.
81 $answerstart = strpos($text, "{");
82 if ($answerstart === false) {
83 $this->error(get_string('beginanswernotfound', 'qformat_missingword'), $text);
84 return false;
87 $answerfinish = strpos($text, "}");
88 if ($answerfinish === false) {
89 $this->error(get_string('endanswernotfound', 'qformat_missingword'), $text);
90 return false;
93 $answerlength = $answerfinish - $answerstart;
94 $answertext = substr($text, $answerstart + 1, $answerlength - 1);
96 // Save the new question text.
97 $question->questiontext = substr_replace($text, "_____", $answerstart, $answerlength+1);
98 $question->name = $this->create_default_question_name($question->questiontext, get_string('questionname', 'question'));
100 // Parse the answers.
101 $answertext = str_replace("=", "~=", $answertext);
102 $answers = explode("~", $answertext);
103 if (isset($answers[0])) {
104 $answers[0] = trim($answers[0]);
106 if (empty($answers[0])) {
107 array_shift($answers);
110 $countanswers = count($answers);
112 switch ($countanswers) {
113 case 0: // Invalid question.
114 $this->error(get_string('noanswerfound', 'qformat_missingword'), $answertext);
115 return false;
117 case 1:
118 $question->qtype = 'shortanswer';
120 $answer = trim($answers[0]);
121 if ($answer[0] == "=") {
122 $answer = substr($answer, 1);
124 $question->answer[] = $answer;
125 $question->fraction[] = 1;
126 $question->feedback[] = array('text' => '', 'format' => FORMAT_HTML);
128 return $question;
130 default:
131 $question->qtype = 'multichoice';
132 $question = $this->add_blank_combined_feedback($question);
133 $question->single = 1; // Only one answer allowed.
135 foreach ($answers as $key => $answer) {
136 $answer = trim($answer);
138 // Tom's addition starts here.
139 $answeight = 0;
140 if (strspn($answer, "1234567890%") > 0) {
141 // Make sure that the percent sign is the last in the span.
142 if (strpos($answer, "%") == strspn($answer, "1234567890%") - 1) {
143 $answeight0 = substr($answer, 0, strspn($answer, "1234567890%"));
144 $answeight = round(($answeight0/100), 2);
145 $answer = substr($answer, (strspn($answer, "1234567890%")));
148 if ($answer[0] == "=") {
149 $answeight = 1;
151 // Remove the protective underscore for leading numbers in answers.
152 if ($answer[0] == "_") {
153 $answer = substr($answer, 1);
155 $answer = trim($answer);
157 if (strpos($answer, "#") > 0) {
158 $hashpos = strpos($answer, "#");
159 $comment = substr(($answer), $hashpos+1);
160 $answer = substr($answer, 0, $hashpos);
161 } else {
162 $comment = " ";
164 // End of Tom's addition.
166 if ($answer[0] == "=") {
167 $question->fraction[$key] = $answeight;
168 $answer = substr($answer, 1);
169 } else {
170 $question->fraction[$key] = $answeight;
172 $question->answer[$key] = array('text' => $answer, 'format' => FORMAT_HTML);
173 $question->feedback[$key] = array('text' => $comment, 'format' => FORMAT_HTML);
176 return $question;