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 311 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   * Embedded answer (Cloze) question importer.
  19   *
  20   * @package   qformat_multianswer
  21   * @copyright 2003 Henrik Kaipe
  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   * Importer that imports a text file containing a single Multianswer question
  31   * from a text file.
  32   *
  33   * @copyright 2003 Henrik Kaipe
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class qformat_multianswer extends qformat_default {
  37  
  38      public function provide_import() {
  39          return true;
  40      }
  41  
  42      /**
  43       * Validate the given file.
  44       *
  45       * For more expensive or detailed integrity checks.
  46       *
  47       * @param stored_file $file the file to check
  48       * @return string the error message that occurred while validating the given file
  49       */
  50      public function validate_file(stored_file $file): string {
  51          return $this->validate_is_utf8_file($file);
  52      }
  53  
  54      public function readquestions($lines) {
  55          question_bank::get_qtype('multianswer'); // Ensure the multianswer code is loaded.
  56  
  57          // For this class the method has been simplified as
  58          // there can never be more than one question for a
  59          // multianswer import.
  60          $questions = array();
  61  
  62          $questiontext = array();
  63          $questiontext['text'] = implode('', $lines);
  64          $questiontext['format'] = FORMAT_MOODLE;
  65          $questiontext['itemid'] = '';
  66  
  67          $question = qtype_multianswer_extract_question($questiontext);
  68          $errors = qtype_multianswer_validate_question($question);
  69          if ($errors) {
  70              $this->error(get_string('invalidmultianswerquestion', 'qtype_multianswer', implode(' ', $errors)));
  71              return array();
  72          }
  73  
  74          $question->questiontext = $question->questiontext['text'];
  75          $question->questiontextformat = 0;
  76  
  77          $question->qtype = 'multianswer';
  78          $question->generalfeedback = '';
  79          $question->generalfeedbackformat = FORMAT_MOODLE;
  80          $question->length = 1;
  81          $question->penalty = 0.3333333;
  82          $question->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
  83          $question->version = 1;
  84          $question->versionid = 0;
  85          $question->questionbankentryid = 0;
  86  
  87          if (!empty($question)) {
  88              $question->name = $this->create_default_question_name($question->questiontext, get_string('questionname', 'question'));
  89              $questions[] = $question;
  90          }
  91  
  92          return $questions;
  93      }
  94  }