Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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 quiz_responses;
  18  
  19  use mod_quiz\quiz_attempt;
  20  use question_bank;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  require_once($CFG->dirroot . '/mod/quiz/tests/attempt_walkthrough_from_csv_test.php');
  26  require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php');
  27  require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
  28  
  29  /**
  30   * Quiz attempt walk through using data from csv file.
  31   *
  32   * @package    quiz_responses
  33   * @category   test
  34   * @copyright  2013 The Open University
  35   * @author     Jamie Pratt <me@jamiep.org>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class responses_from_steps_walkthrough_test extends \mod_quiz\attempt_walkthrough_from_csv_test {
  39      protected function get_full_path_of_csv_file(string $setname, string $test): string {
  40          // Overridden here so that __DIR__ points to the path of this file.
  41          return  __DIR__."/fixtures/{$setname}{$test}.csv";
  42      }
  43  
  44      /**
  45       * @var string[] names of the files which contain the test data.
  46       */
  47      protected $files = ['questions', 'steps', 'responses'];
  48  
  49      /**
  50       * Create a quiz add questions to it, walk through quiz attempts and then check results.
  51       *
  52       * @param array $quizsettings settings to override default settings for quiz created by generator. Taken from quizzes.csv.
  53       * @param array $csvdata of data read from csv file "questionsXX.csv", "stepsXX.csv" and "responsesXX.csv".
  54       * @dataProvider get_data_for_walkthrough
  55       */
  56      public function test_walkthrough_from_csv($quizsettings, $csvdata) {
  57  
  58          $this->resetAfterTest(true);
  59          question_bank::get_qtype('random')->clear_caches_before_testing();
  60  
  61          $this->create_quiz($quizsettings, $csvdata['questions']);
  62  
  63          $quizattemptids = $this->walkthrough_attempts($csvdata['steps']);
  64  
  65          foreach ($csvdata['responses'] as $responsesfromcsv) {
  66              $responses = $this->explode_dot_separated_keys_to_make_subindexs($responsesfromcsv);
  67  
  68              if (!isset($quizattemptids[$responses['quizattempt']])) {
  69                  throw new \coding_exception("There is no quizattempt {$responses['quizattempt']}!");
  70              }
  71              $this->assert_response_test($quizattemptids[$responses['quizattempt']], $responses);
  72          }
  73      }
  74  
  75      protected function assert_response_test($quizattemptid, $responses) {
  76          $quizattempt = quiz_attempt::create($quizattemptid);
  77  
  78          foreach ($responses['slot'] as $slot => $tests) {
  79              $slothastests = false;
  80              foreach ($tests as $test) {
  81                  if ('' !== $test) {
  82                      $slothastests = true;
  83                  }
  84              }
  85              if (!$slothastests) {
  86                  continue;
  87              }
  88              $qa = $quizattempt->get_question_attempt($slot);
  89              $stepswithsubmit = $qa->get_steps_with_submitted_response_iterator();
  90              $step = $stepswithsubmit[$responses['submittedstepno']];
  91              if (null === $step) {
  92                  throw new \coding_exception("There is no step no {$responses['submittedstepno']} ".
  93                                             "for slot $slot in quizattempt {$responses['quizattempt']}!");
  94              }
  95              foreach (['responsesummary', 'fraction', 'state'] as $column) {
  96                  if (isset($tests[$column]) && $tests[$column] != '') {
  97                      switch($column) {
  98                          case 'responsesummary' :
  99                              $actual = $qa->get_question()->summarise_response($step->get_qt_data());
 100                              break;
 101                          case 'fraction' :
 102                              if (count($stepswithsubmit) == $responses['submittedstepno']) {
 103                                  // If this is the last step then we need to look at the fraction after the question has been
 104                                  // finished.
 105                                  $actual = $qa->get_fraction();
 106                              } else {
 107                                  $actual = $step->get_fraction();
 108                              }
 109                             break;
 110                          case 'state' :
 111                              if (count($stepswithsubmit) == $responses['submittedstepno']) {
 112                                  // If this is the last step then we need to look at the state after the question has been
 113                                  // finished.
 114                                  $state = $qa->get_state();
 115                              } else {
 116                                  $state = $step->get_state();
 117                              }
 118                              $actual = substr(get_class($state), strlen('question_state_'));
 119                      }
 120                      $expected = $tests[$column];
 121                      $failuremessage = "Error in  quizattempt {$responses['quizattempt']} in $column, slot $slot, ".
 122                      "submittedstepno {$responses['submittedstepno']}";
 123                      $this->assertEquals($expected, $actual, $failuremessage);
 124                  }
 125              }
 126          }
 127      }
 128  }