Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

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