From 5014d7bbb4852a9c2f3cc58ac0eb6ebc5ac77649 Mon Sep 17 00:00:00 2001 From: Tim Schroeder Date: Fri, 16 Jun 2023 18:07:51 +0200 Subject: [PATCH] MDL-78488 question bank: don't load statistics when they are disabled --- .../statistics/statistics_bulk_loader.php | 9 +++++++ .../statistics_bulk_loader_test.php | 27 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/question/classes/local/statistics/statistics_bulk_loader.php b/question/classes/local/statistics/statistics_bulk_loader.php index 60ad643a255..3191ac8e45e 100644 --- a/question/classes/local/statistics/statistics_bulk_loader.php +++ b/question/classes/local/statistics/statistics_bulk_loader.php @@ -43,6 +43,15 @@ class statistics_bulk_loader { * @return float[][] if a value is not available, it will be set to null. */ public static function load_aggregate_statistics(array $questionids, array $requiredstatistics): array { + // Prevent unnecessary statistics calculations. + if (empty($requiredstatistics)) { + $aggregates = []; + foreach ($questionids as $questionid) { + $aggregates[$questionid] = []; + } + return $aggregates; + } + $places = self::get_all_places_where_questions_were_attempted($questionids); // Set up blank two-dimensional arrays to store the running totals. Indexed by questionid and field name. diff --git a/question/tests/local/statistics/statistics_bulk_loader_test.php b/question/tests/local/statistics/statistics_bulk_loader_test.php index 09141bdab74..d0a4d49e956 100644 --- a/question/tests/local/statistics/statistics_bulk_loader_test.php +++ b/question/tests/local/statistics/statistics_bulk_loader_test.php @@ -553,4 +553,31 @@ class statistics_bulk_loader_test extends advanced_testcase { $this->assertEqualsWithDelta($expectedaveragediscriminationindex[3], $stats[$questions[4]->id]['discriminationindex'], self::PERCENT_DELTA); } + + /** + * Test with question statistics disabled + */ + public function test_statistics_disabled(): void { + $this->resetAfterTest(); + + // Prepare some quizzes and attempts. Exactly what is not important to this test. + $quiz1attempts = [$this->generate_attempt_answers([1, 0, 0, 0])]; + $quiz2attempts = [$this->generate_attempt_answers([1, 1, 1, 1])]; + [, , $questions] = $this->prepare_and_submit_quizzes($quiz1attempts, $quiz2attempts); + + // Prepare some useful arrays. + $expectedstats = [ + $questions[1]->id => [], + $questions[2]->id => [], + $questions[3]->id => [], + $questions[4]->id => [], + ]; + $questionids = array_keys($expectedstats); + + // Ask to load no statistics at all. + $stats = statistics_bulk_loader::load_aggregate_statistics($questionids, []); + + // Verify we got the right thing. + $this->assertEquals($expectedstats, $stats); + } }