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.
   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   * Test helpers for the randomsamatch question type.
  19   *
  20   * @package    qtype_randomsamatch
  21   * @copyright  2013 Jean-Michel Vedrine
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/question/type/randomsamatch/question.php');
  30  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  31  
  32  
  33  /**
  34   * Test helper class for the randomsamatch question type.
  35   *
  36   * @copyright  2013 Jean-Michel Vedrine
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class qtype_randomsamatch_test_helper extends question_test_helper {
  40      public function get_test_questions() {
  41          return array('animals');
  42      }
  43  
  44      /**
  45       * Makes a randomsamatch question similar to the match question returned
  46       * by {@link make_a_matching_question}, but with no 'insect' distractor.
  47       * @return qtype_randomsamatch_question
  48       */
  49      public function make_randomsamatch_question_animals() {
  50          question_bank::load_question_definition_classes('randomsamatch');
  51          $q = new qtype_randomsamatch_question();
  52          test_question_maker::initialise_a_question($q);
  53          $q->name = 'Random shortanswer matching question';
  54          $q->questiontext = 'Classify the animals.';
  55          $q->generalfeedback = 'Frogs and toads are amphibians, the others are mammals.';
  56          $q->qtype = question_bank::get_qtype('randomsamatch');
  57          test_question_maker::set_standard_combined_feedback_fields($q);
  58          $q->shufflestems = false;
  59          $q->stems = array();
  60          $q->choices = array();
  61          $q->right = array();
  62          // Now we create 4 shortanswers question,
  63          // but we just fill the needed fields.
  64          question_bank::load_question_definition_classes('shortanswer');
  65          $sa1 = new qtype_shortanswer_question();
  66          test_question_maker::initialise_a_question($sa1);
  67          $sa1->id = 25;
  68          $sa1->questiontext = 'Dog';
  69          $sa1->answers = array(
  70              13 => new question_answer(13, 'Mammal', 1.0, 'Correct.', FORMAT_HTML),
  71              14 => new question_answer(14, 'Animal', 0.5, 'There is a betterresponse.', FORMAT_HTML),
  72              15 => new question_answer(15, '*', 0.0, 'That is a bad answer.', FORMAT_HTML),
  73          );
  74          $sa1->qtype = question_bank::get_qtype('shortanswer');
  75  
  76          $sa2 = new qtype_shortanswer_question();
  77          test_question_maker::initialise_a_question($sa2);
  78          $sa2->id = 26;
  79          $sa2->questiontext = 'Frog';
  80          $sa2->answers = array(
  81              16 => new question_answer(16, 'Amphibian', 1.0, 'Correct.', FORMAT_HTML),
  82              17 => new question_answer(17, 'A Prince', 1.0, 'Maybe.', FORMAT_HTML),
  83              18 => new question_answer(18, '*', 0.0, 'That is a bad answer.', FORMAT_HTML),
  84          );
  85          $sa2->qtype = question_bank::get_qtype('shortanswer');
  86  
  87          $sa3 = new qtype_shortanswer_question();
  88          test_question_maker::initialise_a_question($sa3);
  89          $sa3->id = 27;
  90          $sa3->questiontext = 'Toad';
  91          $sa3->answers = array(
  92              19 => new question_answer(19, 'Amphibian', 1.0, 'Correct.', FORMAT_HTML),
  93              20 => new question_answer(20, '*', 0.0, 'That is a bad answer.', FORMAT_HTML),
  94          );
  95          $sa3->qtype = question_bank::get_qtype('shortanswer');
  96  
  97          $sa4 = new qtype_shortanswer_question();
  98          test_question_maker::initialise_a_question($sa4);
  99          $sa4->id = 28;
 100          $sa4->questiontext = 'Cat';
 101          $sa4->answers = array(
 102              21 => new question_answer(21, 'Mammal', 1.0, 'Correct.', FORMAT_HTML),
 103          );
 104          $sa4->qtype = question_bank::get_qtype('shortanswer');
 105          $q->questionsloader = new qtype_randomsamatch_test_question_loader(array(), 4, array($sa1, $sa2, $sa3, $sa4));
 106          return $q;
 107      }
 108  }
 109  
 110  /**
 111   * Test implementation of {@link qtype_randomsamatch_question_loader}. Gets the questions
 112   * from an array passed to the constructor, rather than querying the database.
 113   *
 114   * @copyright  2013 Jean-Michel Vedrine
 115   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 116   */
 117  class qtype_randomsamatch_test_question_loader extends qtype_randomsamatch_question_loader {
 118      /** @var array hold available shortanswers questions to choose from. */
 119      protected $questions;
 120  
 121      /**
 122       * Constructor
 123       * @param array $availablequestions not used for tests.
 124       * @param int $choose how many questions to load (not used here).
 125       * @param array $questions array of questions to use.
 126       */
 127      public function __construct($availablequestions, $choose, $questions) {
 128          parent::__construct($availablequestions, $choose);
 129          $this->questions = $questions;
 130      }
 131  
 132      /**
 133       * Just return the shortanswers questions passed to the constructor.
 134       * @return array of short answer questions.
 135       */
 136      public function load_questions() {
 137          return $this->questions;
 138      }
 139  }