Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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