Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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   * Test helper code for the multiple choice question type.
  19   *
  20   * @package    qtype_multichoice
  21   * @copyright  2013 The Open University
  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   * Test helper class for the multiple choice question type.
  31   *
  32   * @copyright  2013 The Open University
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class qtype_multichoice_test_helper extends question_test_helper {
  36      public function get_test_questions() {
  37          return array('two_of_four', 'one_of_four');
  38      }
  39  
  40      /**
  41       * Get the question data, as it would be loaded by get_question_options.
  42       * @return object
  43       */
  44      public static function get_multichoice_question_data_two_of_four() {
  45          global $USER;
  46  
  47          $qdata = new stdClass();
  48  
  49          $qdata->createdby = $USER->id;
  50          $qdata->modifiedby = $USER->id;
  51          $qdata->qtype = 'multichoice';
  52          $qdata->name = 'Multiple choice question';
  53          $qdata->questiontext = 'Which are the odd numbers?';
  54          $qdata->questiontextformat = FORMAT_HTML;
  55          $qdata->generalfeedback = 'The odd numbers are One and Three.';
  56          $qdata->generalfeedbackformat = FORMAT_HTML;
  57          $qdata->defaultmark = 1;
  58          $qdata->length = 1;
  59          $qdata->penalty = 0.3333333;
  60          $qdata->hidden = 0;
  61  
  62          $qdata->options = new stdClass();
  63          $qdata->options->shuffleanswers = 1;
  64          $qdata->options->answernumbering = '123';
  65          $qdata->options->showstandardinstruction = 0;
  66          $qdata->options->layout = 0;
  67          $qdata->options->single = 0;
  68          $qdata->options->correctfeedback =
  69                  test_question_maker::STANDARD_OVERALL_CORRECT_FEEDBACK;
  70          $qdata->options->correctfeedbackformat = FORMAT_HTML;
  71          $qdata->options->partiallycorrectfeedback =
  72                  test_question_maker::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK;
  73          $qdata->options->partiallycorrectfeedbackformat = FORMAT_HTML;
  74          $qdata->options->shownumcorrect = 1;
  75          $qdata->options->incorrectfeedback =
  76                  test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK;
  77          $qdata->options->incorrectfeedbackformat = FORMAT_HTML;
  78  
  79          $qdata->options->answers = array(
  80              13 => (object) array(
  81                  'id' => 13,
  82                  'answer' => 'One',
  83                  'answerformat' => FORMAT_PLAIN,
  84                  'fraction' => 0.5,
  85                  'feedback' => 'One is odd.',
  86                  'feedbackformat' => FORMAT_HTML,
  87              ),
  88              14 => (object) array(
  89                  'id' => 14,
  90                  'answer' => 'Two',
  91                  'answerformat' => FORMAT_PLAIN,
  92                  'fraction' => 0.0,
  93                  'feedback' => 'Two is even.',
  94                  'feedbackformat' => FORMAT_HTML,
  95              ),
  96              15 => (object) array(
  97                  'id' => 15,
  98                  'answer' => 'Three',
  99                  'answerformat' => FORMAT_PLAIN,
 100                  'fraction' => 0.5,
 101                  'feedback' => 'Three is odd.',
 102                  'feedbackformat' => FORMAT_HTML,
 103              ),
 104              16 => (object) array(
 105                  'id' => 16,
 106                  'answer' => 'Four',
 107                  'answerformat' => FORMAT_PLAIN,
 108                  'fraction' => 0.0,
 109                  'feedback' => 'Four is even.',
 110                  'feedbackformat' => FORMAT_HTML,
 111              ),
 112          );
 113  
 114          $qdata->hints = array(
 115              1 => (object) array(
 116                  'hint' => 'Hint 1.',
 117                  'hintformat' => FORMAT_HTML,
 118                  'shownumcorrect' => 1,
 119                  'clearwrong' => 0,
 120                  'options' => 0,
 121              ),
 122              2 => (object) array(
 123                  'hint' => 'Hint 2.',
 124                  'hintformat' => FORMAT_HTML,
 125                  'shownumcorrect' => 1,
 126                  'clearwrong' => 1,
 127                  'options' => 1,
 128              ),
 129          );
 130  
 131          return $qdata;
 132      }
 133      /**
 134       * Get the question data, as it would be loaded by get_question_options.
 135       * @return object
 136       */
 137      public static function get_multichoice_question_form_data_two_of_four() {
 138          $qdata = new stdClass();
 139  
 140          $qdata->name = 'multiple choice question';
 141          $qdata->questiontext = array('text' => 'Which are the odd numbers?', 'format' => FORMAT_HTML);
 142          $qdata->generalfeedback = array('text' => 'The odd numbers are One and Three.', 'format' => FORMAT_HTML);
 143          $qdata->defaultmark = 1;
 144          $qdata->noanswers = 5;
 145          $qdata->numhints = 2;
 146          $qdata->penalty = 0.3333333;
 147  
 148          $qdata->shuffleanswers = 1;
 149          $qdata->answernumbering = '123';
 150          $qdata->showstandardinstruction = 0;
 151          $qdata->single = '0';
 152          $qdata->correctfeedback = array('text' => test_question_maker::STANDARD_OVERALL_CORRECT_FEEDBACK,
 153                                                   'format' => FORMAT_HTML);
 154          $qdata->partiallycorrectfeedback = array('text' => test_question_maker::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK,
 155                                                            'format' => FORMAT_HTML);
 156          $qdata->shownumcorrect = 1;
 157          $qdata->incorrectfeedback = array('text' => test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK,
 158                                                     'format' => FORMAT_HTML);
 159          $qdata->fraction = array('0.5', '0.0', '0.5', '0.0', '0.0');
 160          $qdata->answer = array(
 161              0 => array(
 162                  'text' => 'One',
 163                  'format' => FORMAT_PLAIN
 164              ),
 165              1 => array(
 166                  'text' => 'Two',
 167                  'format' => FORMAT_PLAIN
 168              ),
 169              2 => array(
 170                  'text' => 'Three',
 171                  'format' => FORMAT_PLAIN
 172              ),
 173              3 => array(
 174                  'text' => 'Four',
 175                  'format' => FORMAT_PLAIN
 176              ),
 177              4 => array(
 178                  'text' => '',
 179                  'format' => FORMAT_PLAIN
 180              )
 181          );
 182  
 183          $qdata->feedback = array(
 184              0 => array(
 185                  'text' => 'One is odd.',
 186                  'format' => FORMAT_HTML
 187              ),
 188              1 => array(
 189                  'text' => 'Two is even.',
 190                  'format' => FORMAT_HTML
 191              ),
 192              2 => array(
 193                  'text' => 'Three is odd.',
 194                  'format' => FORMAT_HTML
 195              ),
 196              3 => array(
 197                  'text' => 'Four is even.',
 198                  'format' => FORMAT_HTML
 199              ),
 200              4 => array(
 201                  'text' => '',
 202                  'format' => FORMAT_HTML
 203              )
 204          );
 205  
 206          $qdata->hint = array(
 207              0 => array(
 208                  'text' => 'Hint 1.',
 209                  'format' => FORMAT_HTML
 210              ),
 211              1 => array(
 212                  'text' => 'Hint 2.',
 213                  'format' => FORMAT_HTML
 214              )
 215          );
 216          $qdata->hintclearwrong = array(0, 1);
 217          $qdata->hintshownumcorrect = array(1, 1);
 218  
 219          return $qdata;
 220      }
 221  
 222      /**
 223       * Get the question data, as it would be loaded by get_question_options.
 224       * @return object
 225       */
 226      public static function get_multichoice_question_data_one_of_four() {
 227          global $USER;
 228  
 229          $qdata = new stdClass();
 230  
 231          $qdata->createdby = $USER->id;
 232          $qdata->modifiedby = $USER->id;
 233          $qdata->qtype = 'multichoice';
 234          $qdata->name = 'Multiple choice question';
 235          $qdata->questiontext = 'Which is the oddest number?';
 236          $qdata->questiontextformat = FORMAT_HTML;
 237          $qdata->generalfeedback = 'The oddest number is One.'; // Arguable possibly but it is a quick way to make a variation on
 238                                                                  //this question with one correct answer.
 239          $qdata->generalfeedbackformat = FORMAT_HTML;
 240          $qdata->defaultmark = 1;
 241          $qdata->length = 1;
 242          $qdata->penalty = 0.3333333;
 243          $qdata->hidden = 0;
 244  
 245          $qdata->options = new stdClass();
 246          $qdata->options->shuffleanswers = 1;
 247          $qdata->options->answernumbering = '123';
 248          $qdata->options->showstandardinstruction = 0;
 249          $qdata->options->layout = 0;
 250          $qdata->options->single = 1;
 251          $qdata->options->correctfeedback =
 252              test_question_maker::STANDARD_OVERALL_CORRECT_FEEDBACK;
 253          $qdata->options->correctfeedbackformat = FORMAT_HTML;
 254          $qdata->options->partiallycorrectfeedback =
 255              test_question_maker::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK;
 256          $qdata->options->partiallycorrectfeedbackformat = FORMAT_HTML;
 257          $qdata->options->shownumcorrect = 1;
 258          $qdata->options->incorrectfeedback =
 259              test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK;
 260          $qdata->options->incorrectfeedbackformat = FORMAT_HTML;
 261  
 262          $qdata->options->answers = array(
 263              13 => (object) array(
 264                  'id' => 13,
 265                  'answer' => 'One',
 266                  'answerformat' => FORMAT_PLAIN,
 267                  'fraction' => 1,
 268                  'feedback' => 'One is the oddest.',
 269                  'feedbackformat' => FORMAT_HTML,
 270              ),
 271              14 => (object) array(
 272                  'id' => 14,
 273                  'answer' => 'Two',
 274                  'answerformat' => FORMAT_PLAIN,
 275                  'fraction' => 0.0,
 276                  'feedback' => 'Two is even.',
 277                  'feedbackformat' => FORMAT_HTML,
 278              ),
 279              15 => (object) array(
 280                  'id' => 15,
 281                  'answer' => 'Three',
 282                  'answerformat' => FORMAT_PLAIN,
 283                  'fraction' => 0,
 284                  'feedback' => 'Three is odd.',
 285                  'feedbackformat' => FORMAT_HTML,
 286              ),
 287              16 => (object) array(
 288                  'id' => 16,
 289                  'answer' => 'Four',
 290                  'answerformat' => FORMAT_PLAIN,
 291                  'fraction' => 0.0,
 292                  'feedback' => 'Four is even.',
 293                  'feedbackformat' => FORMAT_HTML,
 294              ),
 295          );
 296  
 297          $qdata->hints = array(
 298              1 => (object) array(
 299                  'hint' => 'Hint 1.',
 300                  'hintformat' => FORMAT_HTML,
 301                  'shownumcorrect' => 1,
 302                  'clearwrong' => 0,
 303                  'options' => 0,
 304              ),
 305              2 => (object) array(
 306                  'hint' => 'Hint 2.',
 307                  'hintformat' => FORMAT_HTML,
 308                  'shownumcorrect' => 1,
 309                  'clearwrong' => 1,
 310                  'options' => 1,
 311              ),
 312          );
 313  
 314          return $qdata;
 315      }
 316      /**
 317       * Get the question data, as it would be loaded by get_question_options.
 318       * @return object
 319       */
 320      public static function get_multichoice_question_form_data_one_of_four() {
 321          $qdata = new stdClass();
 322  
 323          $qdata->name = 'multiple choice question';
 324          $qdata->questiontext = array('text' => 'Which is the oddest number?', 'format' => FORMAT_HTML);
 325          $qdata->generalfeedback = array('text' => 'The oddest number is One.', 'format' => FORMAT_HTML);
 326          $qdata->defaultmark = 1;
 327          $qdata->noanswers = 5;
 328          $qdata->numhints = 2;
 329          $qdata->penalty = 0.3333333;
 330  
 331          $qdata->shuffleanswers = 1;
 332          $qdata->answernumbering = '123';
 333          $qdata->showstandardinstruction = 0;
 334          $qdata->single = '1';
 335          $qdata->correctfeedback = array('text' => test_question_maker::STANDARD_OVERALL_CORRECT_FEEDBACK,
 336                                          'format' => FORMAT_HTML);
 337          $qdata->partiallycorrectfeedback = array('text' => test_question_maker::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK,
 338                                                   'format' => FORMAT_HTML);
 339          $qdata->shownumcorrect = 1;
 340          $qdata->incorrectfeedback = array('text' => test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK,
 341                                            'format' => FORMAT_HTML);
 342          $qdata->fraction = array('1.0', '0.0', '0.0', '0.0', '0.0');
 343          $qdata->answer = array(
 344              0 => array(
 345                  'text' => 'One',
 346                  'format' => FORMAT_PLAIN
 347              ),
 348              1 => array(
 349                  'text' => 'Two',
 350                  'format' => FORMAT_PLAIN
 351              ),
 352              2 => array(
 353                  'text' => 'Three',
 354                  'format' => FORMAT_PLAIN
 355              ),
 356              3 => array(
 357                  'text' => 'Four',
 358                  'format' => FORMAT_PLAIN
 359              ),
 360              4 => array(
 361                  'text' => '',
 362                  'format' => FORMAT_PLAIN
 363              )
 364          );
 365  
 366          $qdata->feedback = array(
 367              0 => array(
 368                  'text' => 'One is the oddest.',
 369                  'format' => FORMAT_HTML
 370              ),
 371              1 => array(
 372                  'text' => 'Two is even.',
 373                  'format' => FORMAT_HTML
 374              ),
 375              2 => array(
 376                  'text' => 'Three is odd.',
 377                  'format' => FORMAT_HTML
 378              ),
 379              3 => array(
 380                  'text' => 'Four is even.',
 381                  'format' => FORMAT_HTML
 382              ),
 383              4 => array(
 384                  'text' => '',
 385                  'format' => FORMAT_HTML
 386              )
 387          );
 388  
 389          $qdata->hint = array(
 390              0 => array(
 391                  'text' => 'Hint 1.',
 392                  'format' => FORMAT_HTML
 393              ),
 394              1 => array(
 395                  'text' => 'Hint 2.',
 396                  'format' => FORMAT_HTML
 397              )
 398          );
 399          $qdata->hintclearwrong = array(0, 1);
 400          $qdata->hintshownumcorrect = array(1, 1);
 401  
 402          return $qdata;
 403      }
 404  }