Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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  
  26  defined('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   */
  52  class qformat_missingword extends qformat_default {
  53  
  54      public function provide_import() {
  55          return true;
  56      }
  57  
  58      /**
  59       * Validate the given file.
  60       *
  61       * For more expensive or detailed integrity checks.
  62       *
  63       * @param stored_file $file the file to check
  64       * @return string the error message that occurred while validating the given file
  65       */
  66      public function validate_file(stored_file $file): string {
  67          return $this->validate_is_utf8_file($file);
  68      }
  69  
  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.
  74  
  75          $question = $this->defaultquestion();
  76          $comment = null; // Added by T Robb.
  77          $text = implode(" ", $lines);
  78  
  79          // Find answer section.
  80  
  81          $answerstart = strpos($text, "{");
  82          if ($answerstart === false) {
  83              $this->error(get_string('beginanswernotfound', 'qformat_missingword'), $text);
  84              return false;
  85          }
  86  
  87          $answerfinish = strpos($text, "}");
  88          if ($answerfinish === false) {
  89              $this->error(get_string('endanswernotfound', 'qformat_missingword'), $text);
  90              return false;
  91          }
  92  
  93          $answerlength = $answerfinish - $answerstart;
  94          $answertext = substr($text, $answerstart + 1, $answerlength - 1);
  95  
  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'));
  99  
 100          // Parse the answers.
 101          $answertext = str_replace("=", "~=", $answertext);
 102          $answers = explode("~", $answertext);
 103          if (isset($answers[0])) {
 104              $answers[0] = trim($answers[0]);
 105          }
 106          if (empty($answers[0])) {
 107              array_shift($answers);
 108          }
 109  
 110          $countanswers = count($answers);
 111  
 112          switch ($countanswers) {
 113              case 0:  // Invalid question.
 114                  $this->error(get_string('noanswerfound', 'qformat_missingword'), $answertext);
 115                  return false;
 116  
 117              case 1:
 118                  $question->qtype = 'shortanswer';
 119  
 120                  $answer = trim($answers[0]);
 121                  if ($answer[0] == "=") {
 122                      $answer = substr($answer, 1);
 123                  }
 124                  $question->answer[]   = $answer;
 125                  $question->fraction[] = 1;
 126                  $question->feedback[] = array('text' => '', 'format' => FORMAT_HTML);
 127  
 128                  return $question;
 129  
 130              default:
 131                  $question->qtype = 'multichoice';
 132                  $question = $this->add_blank_combined_feedback($question);
 133                  $question->single = 1; // Only one answer allowed.
 134  
 135                  foreach ($answers as $key => $answer) {
 136                      $answer = trim($answer);
 137  
 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%")));
 146                          }
 147                      }
 148                      if ($answer[0] == "=") {
 149                          $answeight = 1;
 150                      }
 151                      // Remove the protective underscore for leading numbers in answers.
 152                      if ($answer[0] == "_") {
 153                          $answer = substr($answer, 1);
 154                      }
 155                      $answer = trim($answer);
 156  
 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 = " ";
 163                      }
 164                      // End of Tom's addition.
 165  
 166                      if ($answer[0] == "=") {
 167                          $question->fraction[$key] = $answeight;
 168                          $answer = substr($answer, 1);
 169                      } else {
 170                          $question->fraction[$key] = $answeight;
 171                      }
 172                      $question->answer[$key]   = array('text' => $answer, 'format' => FORMAT_HTML);
 173                      $question->feedback[$key] = array('text' => $comment, 'format' => FORMAT_HTML);
 174                  }
 175  
 176                  return $question;
 177          }
 178      }
 179  }
 180  
 181