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] [Versions 400 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   * Aiken format question importer.
  19   *
  20   * @package    qformat_aiken
  21   * @copyright  2003 Tom Robb <tom@robb.net>
  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   * Aiken format - a simple format for creating multiple choice questions (with
  31   * only one correct choice, and no feedback).
  32   *
  33   * The format looks like this:
  34   *
  35   * Question text
  36   * A) Choice #1
  37   * B) Choice #2
  38   * C) Choice #3
  39   * D) Choice #4
  40   * ANSWER: B
  41   *
  42   * That is,
  43   *  + question text all one one line.
  44   *  + then a number of choices, one to a line. Each line must comprise a letter,
  45   *    then ')' or '.', then a space, then the choice text.
  46   *  + Then a line of the form 'ANSWER: X' to indicate the correct answer.
  47   *
  48   * Be sure to word "All of the above" type choices like "All of these" in
  49   * case choices are being shuffled.
  50   *
  51   * @copyright  2003 Tom Robb <tom@robb.net>
  52   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  53   */
  54  class qformat_aiken extends qformat_default {
  55  
  56      public function provide_import() {
  57          return true;
  58      }
  59  
  60      public function provide_export() {
  61          return true;
  62      }
  63  
  64      public function validate_file(stored_file $file): string {
  65          return $this->validate_is_utf8_file($file);
  66      }
  67  
  68      public function readquestions($lines) {
  69          $questions = array();
  70          $question = null;
  71          $endchar = chr(13);
  72          $linenumber = 0;
  73          foreach ($lines as $line) {
  74              $stp = strpos($line, $endchar, 0);
  75              $newlines = explode($endchar, $line);
  76              $linescount = count($newlines);
  77              for ($i=0; $i < $linescount; $i++) {
  78                  $linenumber++;
  79                  $nowline = trim($newlines[$i]);
  80                  // Go through the array and build an object called $question
  81                  // When done, add $question to $questions.
  82                  if (strlen($nowline) < 2) {
  83                      continue;
  84                  }
  85                  if (preg_match('/^[A-Z][).][ \t]?/', $nowline)) {
  86                      if (is_null($question)) {
  87                          // We have a response line, but we aren't currently in a question.
  88                          $this->error(get_string('questionnotstarted', 'qformat_aiken', $linenumber));
  89                          continue;
  90                      }
  91  
  92                      // A choice. Trim off the label and space, then save.
  93                      $question->answer[] = $this->text_field(substr($nowline, 2));
  94                      $question->fraction[] = 0;
  95                      $question->feedback[] = $this->text_field('');
  96                  } else if (preg_match('/^ANSWER:/', $nowline)) {
  97                      if (is_null($question)) {
  98                          // We have an answer line, but we aren't currently in a question.
  99                          $this->error(get_string('questionnotstarted', 'qformat_aiken', $linenumber));
 100                          continue;
 101                      }
 102  
 103                      // The line that indicates the correct answer. This question is finised.
 104                      $ans = trim(substr($nowline, strpos($nowline, ':') + 1));
 105                      $ans = substr($ans, 0, 1);
 106                      // We want to map A to 0, B to 1, etc.
 107                      $rightans = ord($ans) - ord('A');
 108  
 109                      if (count($question->answer) < 2) {
 110                          // The multichoice question requires at least 2 answers, or there will be a failure later.
 111                          $this->error(get_string('questionmissinganswers', 'qformat_aiken', $linenumber), '', $question->name);
 112                          $question = null;
 113                          continue;
 114                      }
 115  
 116                      $question->fraction[$rightans] = 1;
 117                      $questions[] = $question;
 118  
 119                      // Clear variable for next question set.
 120                      $question = null;
 121                      continue;
 122                  } else {
 123                      // Must be the first line of a new question, since no recognised prefix.
 124                      if (!is_null($question)) {
 125                          // In this case, there was already an open question that we didn't complete. It is being discarded.
 126                          $this->error(get_string('questionnotcomplete', 'qformat_aiken', $linenumber), '', $question->name);
 127                      }
 128  
 129                      $question = $this->defaultquestion();
 130                      $question->qtype = 'multichoice';
 131                      $question->name = $this->create_default_question_name($nowline, get_string('questionname', 'question'));
 132                      $question->questiontext = htmlspecialchars(trim($nowline), ENT_NOQUOTES);
 133                      $question->questiontextformat = FORMAT_HTML;
 134                      $question->generalfeedback = '';
 135                      $question->generalfeedbackformat = FORMAT_HTML;
 136                      $question->single = 1;
 137                      $question->answer = array();
 138                      $question->fraction = array();
 139                      $question->feedback = array();
 140                      $question->correctfeedback = $this->text_field('');
 141                      $question->partiallycorrectfeedback = $this->text_field('');
 142                      $question->incorrectfeedback = $this->text_field('');
 143                  }
 144              }
 145          }
 146          return $questions;
 147      }
 148  
 149      protected function text_field($text) {
 150          return array(
 151              'text' => htmlspecialchars(trim($text), ENT_NOQUOTES),
 152              'format' => FORMAT_HTML,
 153              'files' => array(),
 154          );
 155      }
 156  
 157      public function readquestion($lines) {
 158          // This is no longer needed but might still be called by default.php.
 159          return;
 160      }
 161  
 162      public function exportpreprocess() {
 163          // This format is not able to export categories.
 164          $this->setCattofile(false);
 165          return true;
 166      }
 167  
 168      public function writequestion($question) {
 169          $endchar = "\n";
 170  
 171          // Only export multichoice questions.
 172          if ($question->qtype != 'multichoice') {
 173              return null;
 174          }
 175  
 176          // Do not export multichoice multi questions.
 177          if (!$question->options->single) {
 178              return null;
 179          }
 180  
 181          // Aiken format is not able to handle question with more than 26 answers.
 182          if (count($question->options->answers) > 26) {
 183              return null;
 184          }
 185  
 186          // Export the question displaying message.
 187          $expout = str_replace("\n", '', question_utils::to_plain_text($question->questiontext,
 188                  $question->questiontextformat, array('para' => false, 'newlines' => false))) . $endchar;
 189          $num = 0;
 190          foreach ($question->options->answers as $answer) {
 191              $number = chr(ord('A') + $num);
 192              $expout .= $number . ') ' . str_replace("\n", '', question_utils::to_plain_text($answer->answer,
 193                      $answer->answerformat, array('para' => false, 'newlines' => false))) . $endchar;
 194              if ($answer->fraction > .99) {
 195                  $correctanswer = $number;
 196              }
 197              $num++;
 198          }
 199          // Add the correct answer.
 200          $expout .= 'ANSWER: ' . $correctanswer;
 201  
 202          return $expout;
 203      }
 204  }
 205  
 206