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 310 and 311] [Versions 39 and 311]

   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_deferredfeedback;
  18  
  19  use question_state;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once (__DIR__ . '/../../../engine/lib.php');
  25  require_once (__DIR__ . '/../../../engine/tests/helpers.php');
  26  
  27  
  28  /**
  29   * Unit tests for the deferred feedback behaviour.
  30   *
  31   * @package    qbehaviour_deferredfeedback
  32   * @copyright  2009 The Open University
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class walkthrough_test extends \qbehaviour_walkthrough_test_base {
  36      public function test_deferredfeedback_feedback_truefalse() {
  37  
  38          // Create a true-false question with correct answer true.
  39          $tf = \test_question_maker::make_question('truefalse', 'true');
  40          $this->start_attempt_at_question($tf, 'deferredfeedback', 2);
  41  
  42          // Check the initial state.
  43          $this->check_current_state(question_state::$todo);
  44          $this->check_output_contains_lang_string('notyetanswered', 'question');
  45          $this->check_current_mark(null);
  46          $this->check_current_output($this->get_contains_question_text_expectation($tf),
  47                  $this->get_does_not_contain_feedback_expectation());
  48          $this->assertEquals(get_string('true', 'qtype_truefalse'),
  49                  $this->quba->get_right_answer_summary($this->slot));
  50          $this->assertMatchesRegularExpression('/' . preg_quote($tf->questiontext, '/') . '/',
  51                  $this->quba->get_question_summary($this->slot));
  52          $this->assertNull($this->quba->get_response_summary($this->slot));
  53  
  54          // Process a true answer and check the expected result.
  55          $this->process_submission(array('answer' => 1));
  56  
  57          $this->check_current_state(question_state::$complete);
  58          $this->check_output_contains_lang_string('answersaved', 'question');
  59          $this->check_current_mark(null);
  60          $this->check_current_output($this->get_contains_tf_true_radio_expectation(true, true),
  61                  $this->get_does_not_contain_correctness_expectation(),
  62                  $this->get_does_not_contain_feedback_expectation());
  63  
  64          // Process the same data again, check it does not create a new step.
  65          $numsteps = $this->get_step_count();
  66          $this->process_submission(array('answer' => 1));
  67          $this->check_step_count($numsteps);
  68  
  69          // Process different data, check it creates a new step.
  70          $this->process_submission(array('answer' => 0));
  71          $this->check_step_count($numsteps + 1);
  72          $this->check_current_state(question_state::$complete);
  73  
  74          // Change back, check it creates a new step.
  75          $this->process_submission(array('answer' => 1));
  76          $this->check_step_count($numsteps + 2);
  77  
  78          // Finish the attempt.
  79          $this->quba->finish_all_questions();
  80  
  81          // Verify.
  82          $this->check_current_state(question_state::$gradedright);
  83          $this->check_current_mark(2);
  84          $this->check_current_output($this->get_contains_correct_expectation(),
  85                  $this->get_contains_tf_true_radio_expectation(false, true),
  86                  new \question_pattern_expectation('/class="r0 correct"/'));
  87          $this->assertEquals(get_string('true', 'qtype_truefalse'),
  88                  $this->quba->get_response_summary($this->slot));
  89  
  90          // Process a manual comment.
  91          $this->manual_grade('Not good enough!', 1, FORMAT_HTML);
  92  
  93          $this->check_current_state(question_state::$mangrpartial);
  94          $this->check_current_mark(1);
  95          $this->check_current_output(
  96                  new \question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
  97  
  98          // Now change the correct answer to the question, and regrade.
  99          $tf->rightanswer = false;
 100          $this->quba->regrade_all_questions();
 101  
 102          // Verify.
 103          $this->check_current_state(question_state::$mangrpartial);
 104          $this->check_current_mark(1);
 105  
 106          $autogradedstep = $this->get_step($this->get_step_count() - 2);
 107          $this->assertEqualsWithDelta($autogradedstep->get_fraction(), 0, 0.0000001);
 108      }
 109  
 110      public function test_deferredfeedback_feedback_multichoice_single() {
 111  
 112          // Create a true-false question with correct answer true.
 113          $mc = \test_question_maker::make_a_multichoice_single_question();
 114          $this->start_attempt_at_question($mc, 'deferredfeedback', 3);
 115  
 116          // Start a deferred feedback attempt and add the question to it.
 117          $rightindex = $this->get_mc_right_answer_index($mc);
 118  
 119          $this->check_current_state(question_state::$todo);
 120          $this->check_output_contains_lang_string('notyetanswered', 'question');
 121          $this->check_current_mark(null);
 122          $this->check_current_output(
 123                  $this->get_contains_question_text_expectation($mc),
 124                  $this->get_contains_mc_radio_expectation(0, true, false),
 125                  $this->get_contains_mc_radio_expectation(1, true, false),
 126                  $this->get_contains_mc_radio_expectation(2, true, false),
 127                  $this->get_does_not_contain_feedback_expectation());
 128  
 129          // Process the data extracted for this question.
 130          $this->process_submission(array('answer' => $rightindex));
 131  
 132          // Verify.
 133          $this->check_current_state(question_state::$complete);
 134          $this->check_output_contains_lang_string('answersaved', 'question');
 135          $this->check_current_mark(null);
 136          $this->check_current_output(
 137                  $this->get_contains_mc_radio_expectation($rightindex, true, true),
 138                  $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, true, false),
 139                  $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, true, false),
 140                  $this->get_does_not_contain_correctness_expectation(),
 141                  $this->get_does_not_contain_feedback_expectation());
 142  
 143          // Finish the attempt.
 144          $this->quba->finish_all_questions();
 145  
 146          // Verify.
 147          $this->check_current_state(question_state::$gradedright);
 148          $this->check_current_mark(3);
 149          $this->check_current_output(
 150                  $this->get_contains_mc_radio_expectation($rightindex, false, true),
 151                  $this->get_contains_correct_expectation());
 152  
 153          // Now change the correct answer to the question, and regrade.
 154          $mc->answers[13]->fraction = -0.33333333;
 155          $mc->answers[14]->fraction = 1;
 156          $this->quba->regrade_all_questions();
 157  
 158          // Verify.
 159          $this->check_current_state(question_state::$gradedwrong);
 160          $this->check_current_mark(-1);
 161          $this->check_current_output(
 162                  $this->get_contains_incorrect_expectation());
 163      }
 164  
 165      public function test_deferredfeedback_resume_multichoice_single() {
 166  
 167          // Create a multiple-choice question.
 168          $mc = \test_question_maker::make_a_multichoice_single_question();
 169  
 170          // Attempt it getting it wrong.
 171          $this->start_attempt_at_question($mc, 'deferredfeedback', 3);
 172          $rightindex = $this->get_mc_right_answer_index($mc);
 173          $wrongindex = ($rightindex + 1) % 3;
 174  
 175          $this->check_current_state(question_state::$todo);
 176          $this->check_output_contains_lang_string('notyetanswered', 'question');
 177          $this->check_current_mark(null);
 178          $this->process_submission(array('answer' => $wrongindex));
 179          $this->quba->finish_all_questions();
 180  
 181          // Verify.
 182          $this->check_current_state(question_state::$gradedwrong);
 183          $this->check_current_mark(-1);
 184          $this->check_current_output(
 185                  $this->get_contains_mc_radio_expectation($wrongindex, false, true),
 186                  $this->get_contains_incorrect_expectation());
 187  
 188          // Save the old attempt.
 189          $oldqa = $this->quba->get_question_attempt($this->slot);
 190  
 191          // Reinitialise.
 192          $this->setUp();
 193          $this->quba->set_preferred_behaviour('deferredfeedback');
 194          $this->slot = $this->quba->add_question($mc, 3);
 195          $this->quba->start_question_based_on($this->slot, $oldqa);
 196  
 197          // Verify.
 198          $this->check_current_state(question_state::$complete);
 199          $this->check_output_contains_lang_string('notchanged', 'question');
 200          $this->check_current_mark(null);
 201          $this->check_current_output(
 202                  $this->get_contains_mc_radio_expectation($wrongindex, true, true),
 203                  $this->get_does_not_contain_feedback_expectation(),
 204                  $this->get_does_not_contain_correctness_expectation());
 205  
 206          // Now get it right.
 207          $this->process_submission(array('answer' => $rightindex));
 208          $this->quba->finish_all_questions();
 209  
 210          // Verify.
 211          $this->check_current_state(question_state::$gradedright);
 212          $this->check_current_mark(3);
 213          $this->check_current_output(
 214                  $this->get_contains_mc_radio_expectation($rightindex, false, true),
 215                  $this->get_contains_correct_expectation());
 216      }
 217  
 218      public function test_deferredfeedback_resume_multichoice_single_emptyanswer_first() {
 219  
 220          // Create a multiple-choice question.
 221          $mc = \test_question_maker::make_a_multichoice_single_question();
 222  
 223          // Attempt it and submit empty.
 224          $this->start_attempt_at_question($mc, 'deferredfeedback', 3);
 225          $rightindex = $this->get_mc_right_answer_index($mc);
 226          $wrongindex = ($rightindex + 1) % 3;
 227  
 228          $this->check_current_state(question_state::$todo);
 229          $this->check_output_contains_lang_string('notyetanswered', 'question');
 230          $this->check_current_mark(null);
 231          $this->process_submission(array('-submit' => 1));
 232          $this->quba->finish_all_questions();
 233  
 234          // Verify.
 235          $this->check_current_state(question_state::$gaveup);
 236          $this->check_current_mark(null);
 237          $this->check_current_output(
 238                  $this->get_contains_mc_radio_expectation(0, false, false),
 239                  $this->get_contains_mc_radio_expectation(1, false, false),
 240                  $this->get_contains_mc_radio_expectation(2, false, false),
 241                  $this->get_contains_general_feedback_expectation($mc));
 242  
 243          // Save the old attempt.
 244          $oldqa = $this->quba->get_question_attempt($this->slot);
 245  
 246          // Reinitialise.
 247          $this->setUp();
 248          $this->quba->set_preferred_behaviour('deferredfeedback');
 249          $this->slot = $this->quba->add_question($mc, 3);
 250          $this->quba->start_question_based_on($this->slot, $oldqa);
 251  
 252          // Verify.
 253          $this->check_current_state(question_state::$todo);
 254          $this->check_output_contains_lang_string('notyetanswered', 'question');
 255          $this->check_current_mark(null);
 256          $this->check_current_output(
 257                  $this->get_contains_mc_radio_expectation(0, true, false),
 258                  $this->get_contains_mc_radio_expectation(1, true, false),
 259                  $this->get_contains_mc_radio_expectation(2, true, false),
 260                  $this->get_does_not_contain_feedback_expectation(),
 261                  $this->get_does_not_contain_correctness_expectation());
 262  
 263          // Now get it wrong.
 264          $this->process_submission(array('answer' => $wrongindex));
 265          $this->quba->finish_all_questions();
 266  
 267          // Verify.
 268          $this->check_current_state(question_state::$gradedwrong);
 269          $this->check_current_mark(-1);
 270          $this->check_current_output(
 271                  $this->get_contains_mc_radio_expectation($wrongindex, false, true),
 272                  $this->get_contains_incorrect_expectation());
 273  
 274          // Save the old attempt.
 275          $oldqa = $this->quba->get_question_attempt($this->slot);
 276  
 277          // Reinitialise.
 278          $this->setUp();
 279          $this->quba->set_preferred_behaviour('deferredfeedback');
 280          $this->slot = $this->quba->add_question($mc, 3);
 281          $this->quba->start_question_based_on($this->slot, $oldqa);
 282  
 283          // Verify.
 284          $this->check_current_state(question_state::$complete);
 285          $this->check_output_contains_lang_string('notchanged', 'question');
 286          $this->check_current_mark(null);
 287          $this->check_current_output(
 288                  $this->get_contains_mc_radio_expectation($wrongindex, true, true),
 289                  $this->get_does_not_contain_feedback_expectation(),
 290                  $this->get_does_not_contain_correctness_expectation());
 291  
 292          // Now get it right.
 293          $this->process_submission(array('answer' => $rightindex));
 294          $this->quba->finish_all_questions();
 295  
 296          // Verify.
 297          $this->check_current_state(question_state::$gradedright);
 298          $this->check_current_mark(3);
 299          $this->check_current_output(
 300                  $this->get_contains_mc_radio_expectation($rightindex, false, true),
 301                  $this->get_contains_correct_expectation());
 302      }
 303  }