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 39 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  namespace qbehaviour_interactivecountback;
  18  
  19  use question_hint_with_parts;
  20  use question_state;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  require_once (__DIR__ . '/../../../engine/lib.php');
  26  require_once (__DIR__ . '/../../../engine/tests/helpers.php');
  27  
  28  
  29  /**
  30   * Unit tests for the interactive with countback behaviour.
  31   *
  32   * @package    qbehaviour_interactivecountback
  33   * @copyright  2009 The Open University
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class walkthrough_test extends \qbehaviour_walkthrough_test_base {
  37      public function test_interactive_feedback_match_reset() {
  38  
  39          // Create a matching question.
  40          $m = \test_question_maker::make_question('match');
  41          $m->shufflestems = false;
  42          $m->hints = array(
  43              new question_hint_with_parts(0, 'This is the first hint.', FORMAT_HTML, true, true),
  44              new question_hint_with_parts(0, 'This is the second hint.', FORMAT_HTML, true, true),
  45          );
  46          $this->start_attempt_at_question($m, 'interactive', 12);
  47  
  48          $choiceorder = $m->get_choice_order();
  49          $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
  50          $choices = [];
  51          foreach ($choiceorder as $key => $choice) {
  52              $choices[$key] = $m->choices[$choice];
  53          }
  54  
  55          // Check the initial state.
  56          $this->check_current_state(question_state::$todo);
  57          $this->check_current_mark(null);
  58          $this->assertEquals('interactivecountback',
  59                  $this->quba->get_question_attempt($this->slot)->get_behaviour_name());
  60          $this->check_current_output(
  61                  $this->get_contains_question_text_expectation($m),
  62                  $this->get_contains_submit_button_expectation(true),
  63                  $this->get_does_not_contain_feedback_expectation(),
  64                  $this->get_tries_remaining_expectation(3),
  65                  $this->get_does_not_contain_num_parts_correct(),
  66                  $this->get_no_hint_visible_expectation());
  67          $this->check_output_contains_selectoptions(
  68                  $this->get_contains_select_expectation('sub0', $choices, null, true),
  69                  $this->get_contains_select_expectation('sub1', $choices, null, true),
  70                  $this->get_contains_select_expectation('sub2', $choices, null, true),
  71                  $this->get_contains_select_expectation('sub3', $choices, null, true));
  72  
  73          // Submit an answer with two right, and two wrong.
  74          $this->process_submission(array('sub0' => $orderforchoice[1],
  75                  'sub1' => $orderforchoice[1], 'sub2' => $orderforchoice[1],
  76                  'sub3' => $orderforchoice[1], '-submit' => 1));
  77  
  78          // Verify.
  79          $this->check_current_state(question_state::$todo);
  80          $this->check_current_mark(null);
  81          $this->check_current_output(
  82                  $this->get_does_not_contain_submit_button_expectation(),
  83                  $this->get_contains_try_again_button_expectation(true),
  84                  $this->get_does_not_contain_correctness_expectation(),
  85                  new \question_pattern_expectation('/Tries remaining: 2/'),
  86                  $this->get_contains_hint_expectation('This is the first hint'),
  87                  $this->get_contains_num_parts_correct(2),
  88                  $this->get_contains_standard_partiallycorrect_combined_feedback_expectation(),
  89                  $this->get_contains_hidden_expectation(
  90                          $this->quba->get_field_prefix($this->slot) . 'sub0', $orderforchoice[1]),
  91                  $this->get_contains_hidden_expectation(
  92                          $this->quba->get_field_prefix($this->slot) . 'sub1', '0'),
  93                  $this->get_contains_hidden_expectation(
  94                          $this->quba->get_field_prefix($this->slot) . 'sub2', '0'),
  95                  $this->get_contains_hidden_expectation(
  96                          $this->quba->get_field_prefix($this->slot) . 'sub3', $orderforchoice[1]));
  97          $this->check_output_contains_selectoptions(
  98                  $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[1], false),
  99                  $this->get_contains_select_expectation('sub1', $choices, $orderforchoice[1], false),
 100                  $this->get_contains_select_expectation('sub2', $choices, $orderforchoice[1], false),
 101                  $this->get_contains_select_expectation('sub3', $choices, $orderforchoice[1], false));
 102  
 103          // Check that extract responses will return the reset data.
 104          $prefix = $this->quba->get_field_prefix($this->slot);
 105          $this->assertEquals(array('sub0' => 1),
 106                  $this->quba->extract_responses($this->slot, array($prefix . 'sub0' => 1)));
 107  
 108          // Do try again.
 109          $this->process_submission(array('sub0' => $orderforchoice[1],
 110                  'sub3' => $orderforchoice[1], '-tryagain' => 1));
 111  
 112          // Verify.
 113          $this->check_current_state(question_state::$todo);
 114          $this->check_current_mark(null);
 115          $this->check_current_output(
 116                  $this->get_contains_submit_button_expectation(true),
 117                  $this->get_does_not_contain_correctness_expectation(),
 118                  $this->get_does_not_contain_feedback_expectation(),
 119                  $this->get_tries_remaining_expectation(2),
 120                  $this->get_no_hint_visible_expectation());
 121          $this->check_output_contains_selectoptions(
 122                  $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[1], true),
 123                  $this->get_contains_select_expectation('sub1', $choices, null, true),
 124                  $this->get_contains_select_expectation('sub2', $choices, null, true),
 125                  $this->get_contains_select_expectation('sub3', $choices, $orderforchoice[1], true));
 126  
 127          // Submit the right answer.
 128          $this->process_submission(array('sub0' => $orderforchoice[1],
 129                  'sub1' => $orderforchoice[2], 'sub2' => $orderforchoice[2],
 130                  'sub3' => $orderforchoice[1], '-submit' => 1));
 131  
 132          // Verify.
 133          $this->check_current_state(question_state::$gradedright);
 134          $this->check_current_mark(10);
 135          $this->check_current_output(
 136                  $this->get_does_not_contain_submit_button_expectation(),
 137                  $this->get_does_not_contain_try_again_button_expectation(),
 138                  $this->get_contains_correct_expectation(),
 139                  $this->get_contains_standard_correct_combined_feedback_expectation(),
 140                  new \question_no_pattern_expectation('/class="control\b[^"]*\bpartiallycorrect"/'));
 141          $this->check_output_contains_selectoptions(
 142                  $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[1], false),
 143                  $this->get_contains_select_expectation('sub1', $choices, $orderforchoice[2], false),
 144                  $this->get_contains_select_expectation('sub2', $choices, $orderforchoice[2], false),
 145                  $this->get_contains_select_expectation('sub3', $choices, $orderforchoice[1], false));
 146      }
 147  }