Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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 readquestions($lines) {
  65          $questions = array();
  66          $question = null;
  67          $endchar = chr(13);
  68          $linenumber = 0;
  69          foreach ($lines as $line) {
  70              $stp = strpos($line, $endchar, 0);
  71              $newlines = explode($endchar, $line);
  72              $linescount = count($newlines);
  73              for ($i=0; $i < $linescount; $i++) {
  74                  $linenumber++;
  75                  $nowline = trim($newlines[$i]);
  76                  // Go through the array and build an object called $question
  77                  // When done, add $question to $questions.
  78                  if (strlen($nowline) < 2) {
  79                      continue;
  80                  }
  81                  if (preg_match('/^[A-Z][).][ \t]?/', $nowline)) {
  82                      if (is_null($question)) {
  83                          // We have a response line, but we aren't currently in a question.
  84                          $this->error(get_string('questionnotstarted', 'qformat_aiken', $linenumber));
  85                          continue;
  86                      }
  87  
  88                      // A choice. Trim off the label and space, then save.
  89                      $question->answer[] = $this->text_field(
  90                              htmlspecialchars(trim(substr($nowline, 2)), ENT_NOQUOTES));
  91                      $question->fraction[] = 0;
  92                      $question->feedback[] = $this->text_field('');
  93                  } else if (preg_match('/^ANSWER:/', $nowline)) {
  94                      if (is_null($question)) {
  95                          // We have an answer line, but we aren't currently in a question.
  96                          $this->error(get_string('questionnotstarted', 'qformat_aiken', $linenumber));
  97                          continue;
  98                      }
  99  
 100                      // The line that indicates the correct answer. This question is finised.
 101                      $ans = trim(substr($nowline, strpos($nowline, ':') + 1));
 102                      $ans = substr($ans, 0, 1);
 103                      // We want to map A to 0, B to 1, etc.
 104                      $rightans = ord($ans) - ord('A');
 105  
 106                      if (count($question->answer) < 2) {
 107                          // The multichoice question requires at least 2 answers, or there will be a failure later.
 108                          $this->error(get_string('questionmissinganswers', 'qformat_aiken', $linenumber), '', $question->name);
 109                          $question = null;
 110                          continue;
 111                      }
 112  
 113                      $question->fraction[$rightans] = 1;
 114                      $questions[] = $question;
 115  
 116                      // Clear variable for next question set.
 117                      $question = null;
 118                      continue;
 119                  } else {
 120                      // Must be the first line of a new question, since no recognised prefix.
 121                      if (!is_null($question)) {
 122                          // In this case, there was already an open question that we didn't complete. It is being discarded.
 123                          $this->error(get_string('questionnotcomplete', 'qformat_aiken', $linenumber), '', $question->name);
 124                      }
 125  
 126                      $question = $this->defaultquestion();
 127                      $question->qtype = 'multichoice';
 128                      $question->name = $this->create_default_question_name($nowline, get_string('questionname', 'question'));
 129                      $question->questiontext = htmlspecialchars(trim($nowline), ENT_NOQUOTES);
 130                      $question->questiontextformat = FORMAT_HTML;
 131                      $question->generalfeedback = '';
 132                      $question->generalfeedbackformat = FORMAT_HTML;
 133                      $question->single = 1;
 134                      $question->answer = array();
 135                      $question->fraction = array();
 136                      $question->feedback = array();
 137                      $question->correctfeedback = $this->text_field('');
 138                      $question->partiallycorrectfeedback = $this->text_field('');
 139                      $question->incorrectfeedback = $this->text_field('');
 140                  }
 141              }
 142          }
 143          return $questions;
 144      }
 145  
 146      protected function text_field($text) {
 147          return array(
 148              'text' => htmlspecialchars(trim($text), ENT_NOQUOTES),
 149              'format' => FORMAT_HTML,
 150              'files' => array(),
 151          );
 152      }
 153  
 154      public function readquestion($lines) {
 155          // This is no longer needed but might still be called by default.php.
 156          return;
 157      }
 158  
 159      public function exportpreprocess() {
 160          // This format is not able to export categories.
 161          $this->setCattofile(false);
 162          return true;
 163      }
 164  
 165      public function writequestion($question) {
 166          $endchar = "\n";
 167  
 168          // Only export multichoice questions.
 169          if ($question->qtype != 'multichoice') {
 170              return null;
 171          }
 172  
 173          // Do not export multichoice multi questions.
 174          if (!$question->options->single) {
 175              return null;
 176          }
 177  
 178          // Aiken format is not able to handle question with more than 26 answers.
 179          if (count($question->options->answers) > 26) {
 180              return null;
 181          }
 182  
 183          // Export the question displaying message.
 184          $expout = str_replace("\n", '', question_utils::to_plain_text($question->questiontext,
 185                  $question->questiontextformat, array('para' => false, 'newlines' => false))) . $endchar;
 186          $num = 0;
 187          foreach ($question->options->answers as $answer) {
 188              $number = chr(ord('A') + $num);
 189              $expout .= $number . ') ' . str_replace("\n", '', question_utils::to_plain_text($answer->answer,
 190                      $answer->answerformat, array('para' => false, 'newlines' => false))) . $endchar;
 191              if ($answer->fraction > .99) {
 192                  $correctanswer = $number;
 193              }
 194              $num++;
 195          }
 196          // Add the correct answer.
 197          $expout .= 'ANSWER: ' . $correctanswer;
 198  
 199          return $expout;
 200      }
 201  }
 202  
 203