Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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