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_immediatecbm;
  18  
  19  use question_cbm;
  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 immediate cbm behaviour.
  31   *
  32   * @package    qbehaviour_immediatecbm
  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_immediatecbm_feedback_multichoice_right() {
  38  
  39          // Create a true-false question with correct answer true.
  40          $mc = \test_question_maker::make_a_multichoice_single_question();
  41          $this->start_attempt_at_question($mc, 'immediatecbm');
  42  
  43          $rightindex = $this->get_mc_right_answer_index($mc);
  44          $wrongindex = ($rightindex + 1) % 3;
  45  
  46          // Check the initial state.
  47          $this->check_current_state(question_state::$todo);
  48          $this->check_current_mark(null);
  49          $this->check_current_output(
  50                  $this->get_contains_question_text_expectation($mc),
  51                  $this->get_contains_mc_radio_expectation(0, true, false),
  52                  $this->get_contains_mc_radio_expectation(1, true, false),
  53                  $this->get_contains_mc_radio_expectation(2, true, false),
  54                  $this->get_contains_submit_button_expectation(true),
  55                  $this->get_does_not_contain_feedback_expectation());
  56          $this->assertEquals('A [' . question_cbm::get_short_string(question_cbm::HIGH) . ']',
  57                  $this->quba->get_right_answer_summary($this->slot));
  58          $this->assertMatchesRegularExpression('/' . preg_quote($mc->questiontext, '/') . '/',
  59                  $this->quba->get_question_summary($this->slot));
  60          $this->assertNull($this->quba->get_response_summary($this->slot));
  61  
  62          // Save the wrong answer.
  63          $this->process_submission(array('answer' => $wrongindex, '-certainty' => 1));
  64  
  65          // Verify.
  66          $this->check_current_state(question_state::$todo);
  67          $this->check_current_mark(null);
  68          $this->check_current_output(
  69                  $this->get_contains_mc_radio_expectation($wrongindex, true, true),
  70                  $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, true, false),
  71                  $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, true, false),
  72                  $this->get_contains_submit_button_expectation(true),
  73                  $this->get_does_not_contain_correctness_expectation(),
  74                  $this->get_does_not_contain_feedback_expectation());
  75  
  76          // Submit the right answer.
  77          $this->process_submission(
  78                  array('answer' => $rightindex, '-certainty' => 2, '-submit' => 1));
  79  
  80          // Verify.
  81          $this->check_current_state(question_state::$gradedright);
  82          $this->check_current_mark(2);
  83          $this->check_current_output(
  84                  $this->get_contains_mc_radio_expectation($rightindex, false, true),
  85                  $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
  86                  $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
  87                  $this->get_contains_correct_expectation());
  88          $this->assertEquals('A [' . question_cbm::get_short_string(2) . ']',
  89                  $this->quba->get_response_summary($this->slot));
  90  
  91          $numsteps = $this->get_step_count();
  92  
  93          // Finish the attempt - should not need to add a new state.
  94          $this->quba->finish_all_questions();
  95  
  96          // Verify.
  97          $this->assertEquals($numsteps, $this->get_step_count());
  98          $this->check_current_state(question_state::$gradedright);
  99          $this->check_current_mark(2);
 100          $this->check_current_output(
 101                  $this->get_contains_mc_radio_expectation($rightindex, false, true),
 102                  $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
 103                  $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
 104                  $this->get_contains_correct_expectation());
 105  
 106          // Process a manual comment.
 107          $this->manual_grade('Not good enough!', 0.5, FORMAT_HTML);
 108  
 109          // Verify.
 110          $this->check_current_state(question_state::$mangrpartial);
 111          $this->check_current_mark(0.5);
 112          $this->check_current_output(
 113                  $this->get_contains_partcorrect_expectation(),
 114                  new \question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
 115  
 116          // Now change the correct answer to the question, and regrade.
 117          $mc->answers[13]->fraction = -0.33333333;
 118          $mc->answers[15]->fraction = 1;
 119          $this->quba->regrade_all_questions();
 120  
 121          // Verify.
 122          $this->check_current_state(question_state::$mangrpartial);
 123          $this->check_current_mark(0.5);
 124          $this->check_current_output(
 125                  $this->get_contains_partcorrect_expectation());
 126  
 127          $autogradedstep = $this->get_step($this->get_step_count() - 2);
 128          $this->assertEqualsWithDelta($autogradedstep->get_fraction(), -2, 0.0000001);
 129      }
 130  
 131      public function test_immediatecbm_feedback_multichoice_try_to_submit_blank() {
 132  
 133          // Create a true-false question with correct answer true.
 134          $mc = \test_question_maker::make_a_multichoice_single_question();
 135          $this->start_attempt_at_question($mc, 'immediatecbm');
 136  
 137          // Check the initial state.
 138          $this->check_current_state(question_state::$todo);
 139          $this->check_current_mark(null);
 140          $this->check_current_output(
 141                  $this->get_contains_question_text_expectation($mc),
 142                  $this->get_contains_mc_radio_expectation(0, true, false),
 143                  $this->get_contains_mc_radio_expectation(1, true, false),
 144                  $this->get_contains_mc_radio_expectation(2, true, false),
 145                  $this->get_contains_submit_button_expectation(true),
 146                  $this->get_does_not_contain_feedback_expectation());
 147  
 148          // Submit nothing.
 149          $this->process_submission(array('-submit' => 1));
 150  
 151          // Verify.
 152          $this->check_current_state(question_state::$invalid);
 153          $this->check_current_mark(null);
 154          $this->check_current_output(
 155                  $this->get_contains_mc_radio_expectation(0, true, false),
 156                  $this->get_contains_mc_radio_expectation(1, true, false),
 157                  $this->get_contains_mc_radio_expectation(2, true, false),
 158                  $this->get_contains_submit_button_expectation(true),
 159                  $this->get_does_not_contain_correctness_expectation(),
 160                  $this->get_contains_validation_error_expectation());
 161  
 162          // Finish the attempt.
 163          $this->quba->finish_all_questions();
 164  
 165          // Verify.
 166          $this->check_current_state(question_state::$gaveup);
 167          $this->check_current_mark(null);
 168          $this->check_current_output(
 169                  $this->get_contains_mc_radio_expectation(0, false, false),
 170                  $this->get_contains_mc_radio_expectation(1, false, false),
 171                  $this->get_contains_mc_radio_expectation(2, false, false));
 172  
 173          // Process a manual comment.
 174          $this->manual_grade('Not good enough!', 0.5, FORMAT_HTML);
 175  
 176          // Verify.
 177          $this->check_current_state(question_state::$mangrpartial);
 178          $this->check_current_mark(0.5);
 179          $this->check_current_output(
 180                  $this->get_contains_partcorrect_expectation(),
 181                  new \question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
 182      }
 183  
 184      public function test_immediatecbm_feedback_shortanswer_try_to_submit_no_certainty() {
 185  
 186          // Create a short answer question with correct answer true.
 187          $sa = \test_question_maker::make_question('shortanswer');
 188          $this->start_attempt_at_question($sa, 'immediatecbm');
 189  
 190          // Check the initial state.
 191          $this->check_current_state(question_state::$todo);
 192          $this->check_current_mark(null);
 193          $this->check_current_output(
 194                  $this->get_contains_submit_button_expectation(true),
 195                  $this->get_does_not_contain_feedback_expectation());
 196  
 197          // Submit with certainty missing.
 198          $this->process_submission(array('-submit' => 1, 'answer' => 'frog'));
 199  
 200          // Verify.
 201          $this->check_current_state(question_state::$invalid);
 202          $this->check_current_mark(null);
 203          $this->check_current_output(
 204                  $this->get_contains_submit_button_expectation(true),
 205                  $this->get_does_not_contain_correctness_expectation(),
 206                  $this->get_contains_validation_error_expectation());
 207  
 208          // Now get it right.
 209          $this->process_submission(array('-submit' => 1, 'answer' => 'frog', '-certainty' => 3));
 210  
 211          // Verify.
 212          $this->check_current_state(question_state::$gradedright);
 213          $this->check_current_mark(3);
 214          $this->check_current_output(
 215                  $this->get_does_not_contain_validation_error_expectation());
 216      }
 217  
 218      public function test_immediatecbm_feedback_multichoice_wrong_on_finish() {
 219  
 220          // Create a true-false question with correct answer true.
 221          $mc = \test_question_maker::make_a_multichoice_single_question();
 222          $this->start_attempt_at_question($mc, 'immediatecbm');
 223  
 224          // Check the initial state.
 225          $this->check_current_state(question_state::$todo);
 226          $this->check_current_mark(null);
 227          $this->check_current_output(
 228                  $this->get_contains_question_text_expectation($mc),
 229                  $this->get_contains_mc_radio_expectation(0, true, false),
 230                  $this->get_contains_mc_radio_expectation(1, true, false),
 231                  $this->get_contains_mc_radio_expectation(2, true, false),
 232                  $this->get_contains_submit_button_expectation(true),
 233                  $this->get_does_not_contain_feedback_expectation());
 234  
 235          $rightindex = $this->get_mc_right_answer_index($mc);
 236          $wrongindex = ($rightindex + 1) % 3;
 237  
 238          // Save the wrong answer.
 239          $this->process_submission(array('answer' => $wrongindex, '-certainty' => 3));
 240  
 241          // Verify.
 242          $this->check_current_state(question_state::$todo);
 243          $this->check_current_mark(null);
 244          $this->check_current_output(
 245                  $this->get_contains_mc_radio_expectation($wrongindex, true, true),
 246                  $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, true, false),
 247                  $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, true, false),
 248                  $this->get_contains_submit_button_expectation(true),
 249                  $this->get_does_not_contain_correctness_expectation());
 250  
 251          // Finish the attempt.
 252          $this->quba->finish_all_questions();
 253  
 254          // Verify.
 255          $this->check_current_state(question_state::$gradedwrong);
 256          $this->check_current_mark(-6);
 257          $this->check_current_output(
 258                  $this->get_contains_mc_radio_expectation($wrongindex, false, true),
 259                  $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, false, false),
 260                  $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, false, false),
 261                  $this->get_contains_incorrect_expectation());
 262      }
 263  
 264      public function test_immediatecbm_cbm_truefalse_no_certainty_feedback_when_not_answered() {
 265  
 266          // Create a true-false question with correct answer true.
 267          $tf = \test_question_maker::make_question('truefalse', 'true');
 268          $this->start_attempt_at_question($tf, 'immediatecbm', 2);
 269  
 270          // Verify.
 271          $this->check_current_state(question_state::$todo);
 272          $this->check_current_mark(null);
 273          $this->check_current_output(
 274                  $this->get_does_not_contain_correctness_expectation(),
 275                  $this->get_contains_cbm_radio_expectation(1, true, false),
 276                  $this->get_does_not_contain_feedback_expectation());
 277  
 278          // Finish without answering.
 279          $this->quba->finish_all_questions();
 280  
 281          // Verify.
 282          $this->check_current_state(question_state::$gaveup);
 283          $this->check_current_mark(null);
 284          $this->check_current_output(
 285                  new \question_no_pattern_expectation('/class=\"im-feedback/'));
 286      }
 287  }