Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 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  namespace quiz_statistics;
  17  
  18  defined('MOODLE_INTERNAL') || die();
  19  
  20  global $CFG;
  21  require_once($CFG->dirroot . '/mod/quiz/tests/quiz_question_helper_test_trait.php');
  22  
  23  use core\progress\none;
  24  use mod_quiz\grade_calculator;
  25  use mod_quiz\quiz_settings;
  26  
  27  /**
  28   * Unit tests for quiz_statistics\event\observer\slots_updated
  29   *
  30   * @package   quiz_statistics
  31   * @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
  32   * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   * @covers    \quiz_statistics\quiz_structure_modified
  35   */
  36  class quiz_structure_modified_test extends \advanced_testcase {
  37      use \quiz_question_helper_test_trait;
  38  
  39      /**
  40       * Clear the statistics cache for a quiz when it structure is modified.
  41       *
  42       * When recompute_quiz_sumgrades() is called, it should trigger this plugin's quiz_structure_modified callback
  43       * which clears the statistics cache for the quiz.
  44       *
  45       * @return void
  46       */
  47      public function test_clear_cache_on_structure_modified(): void {
  48          global $DB;
  49          $this->resetAfterTest(true);
  50  
  51          // Create and attempt a quiz.
  52          $generator = $this->getDataGenerator();
  53          $user = $generator->create_user();
  54          $course = $generator->create_course();
  55          $quiz = $this->create_test_quiz($course);
  56          $questiongenerator = $generator->get_plugin_generator('core_question');
  57          $category = $questiongenerator->create_question_category();
  58          $question = $questiongenerator->create_question('match', null, ['category' => $category->id]);
  59          $questiongenerator->update_question($question);
  60          quiz_add_quiz_question($question->id, $quiz);
  61          $this->attempt_quiz($quiz, $user);
  62  
  63          // Run the statistics calculation to prime the cache.
  64          $report = new \quiz_statistics_report();
  65          $questions = $report->load_and_initialise_questions_for_calculations($quiz);
  66          $report->get_all_stats_and_analysis(
  67              $quiz,
  68              $quiz->grademethod,
  69              \question_attempt::ALL_TRIES,
  70              new \core\dml\sql_join(),
  71              $questions,
  72              new none(),
  73          );
  74  
  75          $hashcode = quiz_statistics_qubaids_condition($quiz->id, new \core\dml\sql_join(), $quiz->grademethod)->get_hash_code();
  76  
  77          $this->assertTrue($DB->record_exists('quiz_statistics', ['hashcode' => $hashcode]));
  78          $this->assertTrue($DB->record_exists('question_statistics', ['hashcode' => $hashcode]));
  79          $this->assertTrue($DB->record_exists('question_response_analysis', ['hashcode' => $hashcode]));
  80  
  81          // Recompute sumgrades, which triggers the quiz_structure_modified callback.
  82          quiz_update_sumgrades($quiz);
  83  
  84          $this->assertFalse($DB->record_exists('quiz_statistics', ['hashcode' => $hashcode]));
  85          $this->assertFalse($DB->record_exists('question_statistics', ['hashcode' => $hashcode]));
  86          $this->assertFalse($DB->record_exists('question_response_analysis', ['hashcode' => $hashcode]));
  87      }
  88  }