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_deferredcbm;
  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 deferred feedback with certainty base marking behaviour.
  31   *
  32   * @package    qbehaviour_deferredcbm
  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_deferred_cbm_truefalse_high_certainty() {
  38  
  39          // Create a true-false question with correct answer true.
  40          $tf = \test_question_maker::make_question('truefalse', 'true');
  41          $this->start_attempt_at_question($tf, 'deferredcbm', 2);
  42  
  43          // Verify.
  44          $this->check_current_state(question_state::$todo);
  45          $this->check_output_contains_lang_string('notyetanswered', 'question');
  46          $this->check_current_mark(null);
  47          $this->check_current_output(
  48                  $this->get_contains_question_text_expectation($tf),
  49                  $this->get_contains_tf_true_radio_expectation(true, false),
  50                  $this->get_contains_tf_false_radio_expectation(true, false),
  51                  $this->get_contains_cbm_radio_expectation(1, true, false),
  52                  $this->get_contains_cbm_radio_expectation(2, true, false),
  53                  $this->get_contains_cbm_radio_expectation(3, true, false),
  54                  $this->get_does_not_contain_feedback_expectation());
  55  
  56          // Process the data extracted for this question.
  57          $this->process_submission(array('answer' => 1, '-certainty' => 3));
  58  
  59          // Verify.
  60          $this->check_current_state(question_state::$complete);
  61          $this->check_output_contains_lang_string('answersaved', 'question');
  62          $this->check_current_mark(null);
  63          $this->check_current_output(
  64                  $this->get_contains_tf_true_radio_expectation(true, true),
  65                  $this->get_contains_cbm_radio_expectation(3, true, true),
  66                  $this->get_does_not_contain_correctness_expectation(),
  67                  $this->get_does_not_contain_feedback_expectation());
  68  
  69          // Process the same data again, check it does not create a new step.
  70          $numsteps = $this->get_step_count();
  71          $this->process_submission(array('answer' => 1, '-certainty' => 3));
  72          $this->check_step_count($numsteps);
  73  
  74          // Process different data, check it creates a new step.
  75          $this->process_submission(array('answer' => 1, '-certainty' => 1));
  76          $this->check_step_count($numsteps + 1);
  77          $this->check_current_state(question_state::$complete);
  78  
  79          // Change back, check it creates a new step.
  80          $this->process_submission(array('answer' => 1, '-certainty' => 3));
  81          $this->check_step_count($numsteps + 2);
  82  
  83          // Finish the attempt.
  84          $this->quba->finish_all_questions();
  85  
  86          // Verify.
  87          $this->check_current_state(question_state::$gradedright);
  88          $this->check_current_mark(6);
  89          $this->check_current_output(
  90                  $this->get_contains_tf_true_radio_expectation(false, true),
  91                  $this->get_contains_cbm_radio_expectation(3, false, true),
  92                  $this->get_contains_correct_expectation());
  93  
  94          // Process a manual comment.
  95          $this->manual_grade('Not good enough!', 5, FORMAT_HTML);
  96  
  97          // Verify.
  98          $this->check_current_state(question_state::$mangrright);
  99          $this->check_current_mark(5);
 100          $this->check_current_output(new \question_pattern_expectation('/' .
 101                  preg_quote('Not good enough!', '/') . '/'));
 102  
 103          // Now change the correct answer to the question, and regrade.
 104          $tf->rightanswer = false;
 105          $this->quba->regrade_all_questions();
 106  
 107          // Verify.
 108          $this->check_current_state(question_state::$mangrright);
 109          $this->check_current_mark(5);
 110          $autogradedstep = $this->get_step($this->get_step_count() - 2);
 111          $this->assertEqualsWithDelta(-6, $autogradedstep->get_fraction(), 0.0000001);
 112      }
 113  
 114      public function test_deferred_cbm_truefalse_low_certainty() {
 115  
 116          // Create a true-false question with correct answer true.
 117          $tf = \test_question_maker::make_question('truefalse', 'true');
 118          $this->start_attempt_at_question($tf, 'deferredcbm', 2);
 119  
 120          // Verify.
 121          $this->check_current_state(question_state::$todo);
 122          $this->check_output_contains_lang_string('notyetanswered', 'question');
 123          $this->check_current_mark(null);
 124          $this->check_current_output(
 125                  $this->get_does_not_contain_correctness_expectation(),
 126                  $this->get_contains_cbm_radio_expectation(1, true, false),
 127                  $this->get_does_not_contain_feedback_expectation());
 128  
 129          // Submit ansewer with low certainty.
 130          $this->process_submission(array('answer' => 1, '-certainty' => 1));
 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($this->get_does_not_contain_correctness_expectation(),
 137                  $this->get_contains_cbm_radio_expectation(1, true, true),
 138                  $this->get_does_not_contain_feedback_expectation());
 139  
 140          // Finish the attempt.
 141          $this->quba->finish_all_questions();
 142  
 143          // Verify.
 144          $this->check_current_state(question_state::$gradedright);
 145          $this->check_current_mark(2);
 146          $this->check_current_output($this->get_contains_correct_expectation(),
 147                  $this->get_contains_cbm_radio_expectation(1, false, true));
 148          $this->assertEquals(get_string('true', 'qtype_truefalse') . ' [' .
 149                  question_cbm::get_short_string(question_cbm::LOW) . ']',
 150                  $this->quba->get_response_summary($this->slot));
 151      }
 152  
 153      public function test_deferred_cbm_truefalse_default_certainty() {
 154  
 155          // Create a true-false question with correct answer true.
 156          $tf = \test_question_maker::make_question('truefalse', 'true');
 157          $this->start_attempt_at_question($tf, 'deferredcbm', 2);
 158  
 159          // Verify.
 160          $this->check_current_state(question_state::$todo);
 161          $this->check_output_contains_lang_string('notyetanswered', 'question');
 162          $this->check_current_mark(null);
 163          $this->check_current_output(
 164                  $this->get_does_not_contain_correctness_expectation(),
 165                  $this->get_contains_cbm_radio_expectation(1, true, false),
 166                  $this->get_does_not_contain_feedback_expectation());
 167  
 168          // Submit ansewer with low certainty and finish the attempt.
 169          $this->process_submission(array('answer' => 1));
 170          $this->quba->finish_all_questions();
 171  
 172          // Verify.
 173          $qa = $this->quba->get_question_attempt($this->slot);
 174          $this->check_current_state(question_state::$gradedright);
 175          $this->check_current_mark(2);
 176          $this->check_current_output($this->get_contains_correct_expectation(),
 177                  $this->get_contains_cbm_radio_expectation(1, false, false),
 178                  new \question_pattern_expectation('/' . preg_quote(
 179                          get_string('assumingcertainty', 'qbehaviour_deferredcbm',
 180                          question_cbm::get_string(
 181                              $qa->get_last_behaviour_var('_assumedcertainty'))), '/') . '/'));
 182          $this->assertEquals(get_string('true', 'qtype_truefalse'),
 183                  $this->quba->get_response_summary($this->slot));
 184      }
 185  
 186      public function test_deferredcbm_resume_multichoice_single() {
 187  
 188          // Create a multiple-choice question.
 189          $mc = \test_question_maker::make_a_multichoice_single_question();
 190  
 191          // Attempt it getting it wrong.
 192          $this->start_attempt_at_question($mc, 'deferredcbm', 1);
 193          $rightindex = $this->get_mc_right_answer_index($mc);
 194          $wrongindex = ($rightindex + 1) % 3;
 195          $this->process_submission(array('answer' => $wrongindex, '-certainty' => 2));
 196          $this->quba->finish_all_questions();
 197  
 198          // Verify.
 199          $this->check_current_state(question_state::$gradedwrong);
 200          $this->check_current_mark(-2);
 201          $this->check_current_output(
 202                  $this->get_contains_mc_radio_expectation($wrongindex, false, true),
 203                  $this->get_contains_cbm_radio_expectation(2, false, true),
 204                  $this->get_contains_incorrect_expectation());
 205          $this->assertEquals('A [' . question_cbm::get_short_string(question_cbm::HIGH) . ']',
 206                  $this->quba->get_right_answer_summary($this->slot));
 207          $this->assertMatchesRegularExpression('/' . preg_quote($mc->questiontext, '/') . '/',
 208                  $this->quba->get_question_summary($this->slot));
 209          $this->assertMatchesRegularExpression(
 210              '/(B|C) \[' . preg_quote(question_cbm::get_short_string(question_cbm::MED), '/') . '\]/',
 211              $this->quba->get_response_summary($this->slot));
 212  
 213          // Save the old attempt.
 214          $oldqa = $this->quba->get_question_attempt($this->slot);
 215  
 216          // Reinitialise.
 217          $this->setUp();
 218          $this->quba->set_preferred_behaviour('deferredcbm');
 219          $this->slot = $this->quba->add_question($mc, 1);
 220          $this->quba->start_question_based_on($this->slot, $oldqa);
 221  
 222          // Verify.
 223          $this->check_current_state(question_state::$complete);
 224          $this->check_output_contains_lang_string('notchanged', 'question');
 225          $this->check_current_mark(null);
 226          $this->check_current_output(
 227                  $this->get_contains_mc_radio_expectation($wrongindex, true, true),
 228                  $this->get_contains_cbm_radio_expectation(2, true, true),
 229                  $this->get_does_not_contain_feedback_expectation(),
 230                  $this->get_does_not_contain_correctness_expectation());
 231          $this->assertEquals('A [' . question_cbm::get_short_string(question_cbm::HIGH) . ']',
 232                  $this->quba->get_right_answer_summary($this->slot));
 233          $this->assertMatchesRegularExpression('/' . preg_quote($mc->questiontext, '/') . '/',
 234                  $this->quba->get_question_summary($this->slot));
 235          $this->assertNull($this->quba->get_response_summary($this->slot));
 236  
 237          // Now get it right.
 238          $this->process_submission(array('answer' => $rightindex, '-certainty' => 3));
 239          $this->quba->finish_all_questions();
 240  
 241          // Verify.
 242          $this->check_current_state(question_state::$gradedright);
 243          $this->check_current_mark(3);
 244          $this->check_current_output(
 245                  $this->get_contains_mc_radio_expectation($rightindex, false, true),
 246                  $this->get_contains_cbm_radio_expectation(question_cbm::HIGH, false, true),
 247                  $this->get_contains_correct_expectation());
 248          $this->assertMatchesRegularExpression(
 249              '/(A) \[' . preg_quote(question_cbm::get_short_string(question_cbm::HIGH), '/') . '\]/',
 250              $this->quba->get_response_summary($this->slot));
 251      }
 252  
 253      public function test_deferred_cbm_truefalse_no_certainty_feedback_when_not_answered() {
 254  
 255          // Create a true-false question with correct answer true.
 256          $tf = \test_question_maker::make_question('truefalse', 'true');
 257          $this->start_attempt_at_question($tf, 'deferredcbm', 2);
 258  
 259          // Verify.
 260          $this->check_current_state(question_state::$todo);
 261          $this->check_output_contains_lang_string('notyetanswered', 'question');
 262          $this->check_current_mark(null);
 263          $this->check_current_output(
 264                  $this->get_does_not_contain_correctness_expectation(),
 265                  $this->get_contains_cbm_radio_expectation(1, true, false),
 266                  $this->get_does_not_contain_feedback_expectation());
 267  
 268          // Finish without answering.
 269          $this->quba->finish_all_questions();
 270  
 271          // Verify.
 272          $this->check_current_state(question_state::$gaveup);
 273          $this->check_current_mark(null);
 274          $this->check_current_output(
 275                  new \question_no_pattern_expectation('/class=\"im-feedback/'));
 276      }
 277  }