Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * This file contains tests that walks a question through the adaptive (no penalties)k
  19   * behaviour.
  20   *
  21   * @package    qbehaviour
  22   * @subpackage adaptivenopenalty
  23   * @copyright  2009 The Open University
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once (__DIR__ . '/../../../engine/lib.php');
  32  require_once (__DIR__ . '/../../../engine/tests/helpers.php');
  33  
  34  
  35  /**
  36   * Unit tests for the adaptive (no penalties) behaviour.
  37   *
  38   * @copyright  2009 The Open University
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class qbehaviour_adaptivenopenalty_walkthrough_test extends qbehaviour_walkthrough_test_base {
  42  
  43      protected function get_does_not_contain_gradingdetails_expectation() {
  44          return new question_no_pattern_expectation('/class="gradingdetails"/');
  45      }
  46  
  47      protected function get_does_not_contain_penalty_info_expectation() {
  48          $penaltyinfo = get_string('gradingdetailspenalty', 'qbehaviour_adaptive', 'XXXXX');
  49          $penaltypattern = '/'.str_replace('XXXXX', '\\w*', preg_quote($penaltyinfo, '/')).'/';
  50          return new question_no_pattern_expectation($penaltypattern);
  51      }
  52  
  53      protected function get_does_not_contain_total_penalty_expectation() {
  54          $penaltyinfo = get_string('gradingdetailspenaltytotal', 'qbehaviour_adaptive', 'XXXXX');
  55          $penaltypattern = '/'.str_replace('XXXXX', '\\w*', preg_quote($penaltyinfo, '/')).'/';
  56          return new question_no_pattern_expectation($penaltypattern);
  57      }
  58  
  59      public function test_multichoice() {
  60  
  61          // Create a multiple choice, single response question.
  62          $mc = test_question_maker::make_a_multichoice_single_question();
  63          $mc->penalty = 0.3333333;
  64          $this->start_attempt_at_question($mc, 'adaptivenopenalty', 3);
  65  
  66          $rightindex = $this->get_mc_right_answer_index($mc);
  67          $wrongindex = ($rightindex + 1) % 3;
  68  
  69          // Check the initial state.
  70          $this->check_current_state(question_state::$todo);
  71          $this->check_current_mark(null);
  72          $this->check_current_output(
  73                  $this->get_contains_marked_out_of_summary(),
  74                  $this->get_contains_question_text_expectation($mc),
  75                  $this->get_contains_mc_radio_expectation(0, true, false),
  76                  $this->get_contains_mc_radio_expectation(1, true, false),
  77                  $this->get_contains_mc_radio_expectation(2, true, false),
  78                  $this->get_contains_submit_button_expectation(true),
  79                  $this->get_does_not_contain_feedback_expectation());
  80  
  81          // Process a submit.
  82          $this->process_submission(array('answer' => $wrongindex, '-submit' => 1));
  83  
  84          // Verify.
  85          $this->check_current_state(question_state::$todo);
  86          $this->check_current_mark(0);
  87          $this->check_current_output(
  88                  $this->get_contains_mark_summary(0),
  89                  $this->get_contains_mc_radio_expectation($wrongindex, true, true),
  90                  $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, true, false),
  91                  $this->get_contains_mc_radio_expectation(($wrongindex + 2) % 3, true, false),
  92                  $this->get_contains_incorrect_expectation(),
  93                  $this->get_does_not_contain_penalty_info_expectation(),
  94                  $this->get_does_not_contain_total_penalty_expectation());
  95          $this->assertRegExp('/B|C/',
  96                  $this->quba->get_response_summary($this->slot));
  97  
  98          // Process a change of answer to the right one, but not sumbitted.
  99          $this->process_submission(array('answer' => $rightindex));
 100  
 101          // Verify.
 102          $this->check_current_state(question_state::$todo);
 103          $this->check_current_mark(0);
 104          $this->check_current_output(
 105                  $this->get_contains_mark_summary(0),
 106                  $this->get_contains_mc_radio_expectation($rightindex, true, true),
 107                  $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, true, false),
 108                  $this->get_contains_mc_radio_expectation(($rightindex + 2) % 3, true, false));
 109          $this->assertRegExp('/B|C/',
 110                  $this->quba->get_response_summary($this->slot));
 111  
 112          // Now submit the right answer.
 113          $this->process_submission(array('answer' => $rightindex, '-submit' => 1));
 114  
 115          // Verify.
 116          $this->check_current_state(question_state::$complete);
 117          $this->check_current_mark(3);
 118          $this->check_current_output(
 119                  $this->get_contains_mark_summary(3),
 120                  $this->get_contains_mc_radio_expectation($rightindex, true, true),
 121                  $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, true, false),
 122                  $this->get_contains_mc_radio_expectation(($rightindex + 2) % 3, true, false),
 123                  $this->get_contains_correct_expectation(),
 124                  $this->get_does_not_contain_penalty_info_expectation(),
 125                  $this->get_does_not_contain_total_penalty_expectation());
 126          $this->assertEquals('A',
 127                  $this->quba->get_response_summary($this->slot));
 128  
 129          // Finish the attempt.
 130          $this->quba->finish_all_questions();
 131  
 132          // Verify.
 133          $this->check_current_state(question_state::$gradedright);
 134          $this->check_current_mark(3);
 135          $this->check_current_output(
 136                  $this->get_contains_mark_summary(3),
 137                  $this->get_contains_mc_radio_expectation($rightindex, false, true),
 138                  $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
 139                  $this->get_contains_mc_radio_expectation(($rightindex + 2) % 3, false, false),
 140                  $this->get_contains_correct_expectation());
 141  
 142          // Process a manual comment.
 143          $this->manual_grade('Not good enough!', 1, FORMAT_HTML);
 144  
 145          // Verify.
 146          $this->check_current_state(question_state::$mangrpartial);
 147          $this->check_current_mark(1);
 148          $this->check_current_output(
 149                  $this->get_contains_mark_summary(1),
 150                  new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
 151  
 152          // Now change the correct answer to the question, and regrade.
 153          $mc->answers[13]->fraction = -0.33333333;
 154          $mc->answers[14]->fraction = 1; // We don't know which "wrong" index we chose above!
 155          $mc->answers[15]->fraction = 1; // Therefore, treat answers B and C with the same score.
 156          $this->quba->regrade_all_questions();
 157  
 158          // Verify.
 159          $this->check_current_state(question_state::$mangrpartial);
 160          $this->check_current_mark(1);
 161          $this->check_current_output(
 162                  $this->get_contains_mark_summary(1),
 163                  $this->get_contains_partcorrect_expectation());
 164  
 165          $autogradedstep = $this->get_step($this->get_step_count() - 3);
 166          $this->assertEquals($autogradedstep->get_fraction(), 1, '', 0.0000001);
 167      }
 168  
 169      public function test_multichoice2() {
 170  
 171          // Create a multiple choice, multiple response question.
 172          $mc = test_question_maker::make_a_multichoice_multi_question();
 173          $mc->penalty = 0.3333333;
 174          $mc->shuffleanswers = 0;
 175          $this->start_attempt_at_question($mc, 'adaptivenopenalty', 2);
 176  
 177          // Check the initial state.
 178          $this->check_current_state(question_state::$todo);
 179          $this->check_current_mark(null);
 180          $this->check_current_output(
 181                  $this->get_contains_marked_out_of_summary(),
 182                  $this->get_contains_question_text_expectation($mc),
 183                  $this->get_contains_submit_button_expectation(true),
 184                  $this->get_does_not_contain_feedback_expectation());
 185  
 186          // Process a submit.
 187          $this->process_submission(array('choice0' => 1, 'choice2' => 1, '-submit' => 1));
 188  
 189          // Verify.
 190          $this->check_current_state(question_state::$complete);
 191          $this->check_current_mark(2);
 192          $this->check_current_output(
 193                  $this->get_contains_mark_summary(2),
 194                  $this->get_contains_submit_button_expectation(true),
 195                  $this->get_contains_correct_expectation(),
 196                  $this->get_does_not_contain_penalty_info_expectation(),
 197                  $this->get_does_not_contain_total_penalty_expectation());
 198  
 199          // Save the same correct answer again. Should no do anything.
 200          $numsteps = $this->get_step_count();
 201          $this->process_submission(array('choice0' => 1, 'choice2' => 1));
 202  
 203          // Verify.
 204          $this->check_step_count($numsteps);
 205          $this->check_current_state(question_state::$complete);
 206  
 207          // Finish the attempt.
 208          $this->quba->finish_all_questions();
 209  
 210          // Verify.
 211          $this->check_step_count($numsteps + 1);
 212          $this->check_current_state(question_state::$gradedright);
 213          $this->check_current_mark(2);
 214          $this->check_current_output(
 215                  $this->get_contains_mark_summary(2),
 216                  $this->get_does_not_contain_submit_button_expectation(),
 217                  $this->get_contains_correct_expectation());
 218      }
 219  
 220      public function test_numerical_invalid() {
 221  
 222          // Create a numerical question
 223          $numq = test_question_maker::make_question('numerical', 'pi');
 224          $numq->penalty = 0.1;
 225          $this->start_attempt_at_question($numq, 'adaptivenopenalty');
 226  
 227          // Check the initial state.
 228          $this->check_current_state(question_state::$todo);
 229          $this->check_current_mark(null);
 230          $this->check_current_output(
 231                  $this->get_contains_marked_out_of_summary(),
 232                  $this->get_contains_submit_button_expectation(true),
 233                  $this->get_does_not_contain_feedback_expectation());
 234  
 235          // Submit a non-numerical answer.
 236          $this->process_submission(array('-submit' => 1, 'answer' => 'Pi'));
 237  
 238          // Verify.
 239          $this->check_current_state(question_state::$invalid);
 240          $this->check_current_mark(null);
 241          $this->check_current_output(
 242                  $this->get_contains_marked_out_of_summary(1),
 243                  $this->get_contains_submit_button_expectation(true),
 244                  $this->get_does_not_contain_correctness_expectation(),
 245                  $this->get_contains_validation_error_expectation(),
 246                  $this->get_does_not_contain_feedback_expectation());
 247  
 248          // Submit an incorrect answer.
 249          $this->process_submission(array('-submit' => 1, 'answer' => '-5'));
 250  
 251          // Verify.
 252          $this->check_current_state(question_state::$todo);
 253          $this->check_current_mark(0);
 254          $this->check_current_output(
 255                  $this->get_contains_mark_summary(0),
 256                  $this->get_contains_submit_button_expectation(true),
 257                  $this->get_contains_incorrect_expectation(),
 258                  $this->get_does_not_contain_penalty_info_expectation(),
 259                  $this->get_does_not_contain_total_penalty_expectation(),
 260                  $this->get_does_not_contain_validation_error_expectation());
 261  
 262          // Submit another non-numerical answer.
 263          $this->process_submission(array('-submit' => 1, 'answer' => 'Pi*2'));
 264  
 265          // Verify.
 266          $this->check_current_state(question_state::$invalid);
 267          $this->check_current_mark(0);
 268          $this->check_current_output(
 269                  $this->get_contains_mark_summary(0),
 270                  $this->get_contains_submit_button_expectation(true),
 271                  $this->get_does_not_contain_correctness_expectation(),
 272                  $this->get_contains_validation_error_expectation(),
 273                  $this->get_does_not_contain_gradingdetails_expectation());
 274  
 275          // Submit the correct answer.
 276          $this->process_submission(array('-submit' => 1, 'answer' => '3.14'));
 277  
 278          // Verify.
 279          $this->check_current_state(question_state::$complete);
 280          $this->check_current_mark(1.0);
 281          $this->check_current_output(
 282                  $this->get_contains_mark_summary(1.0),
 283                  $this->get_contains_submit_button_expectation(true),
 284                  $this->get_contains_correct_expectation(),
 285                  $this->get_does_not_contain_validation_error_expectation());
 286  
 287          // Submit another non-numerical answer.
 288          $this->process_submission(array('-submit' => 1, 'answer' => 'Pi/3'));
 289  
 290          // Verify.
 291          $this->check_current_state(question_state::$invalid);
 292          $this->check_current_mark(1.0);
 293          $this->check_current_output(
 294                  $this->get_contains_mark_summary(1.0),
 295                  $this->get_contains_submit_button_expectation(true),
 296                  $this->get_does_not_contain_correctness_expectation(),
 297                  $this->get_contains_validation_error_expectation(),
 298                  $this->get_does_not_contain_gradingdetails_expectation());
 299  
 300          // Finish the attempt.
 301          $this->quba->finish_all_questions();
 302  
 303          // Verify.
 304          $this->check_current_state(question_state::$gradedwrong);
 305          $this->check_current_mark(1.0);
 306          $this->check_current_output(
 307                  $this->get_contains_mark_summary(1.0),
 308                  $this->get_does_not_contain_submit_button_expectation(),
 309                  $this->get_contains_incorrect_expectation(),
 310                  $this->get_does_not_contain_validation_error_expectation());
 311      }
 312  }