From d4f89cd803ec1f80a17923f16424b7af28ba39c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luca=20B=C3=B6sch?= Date: Fri, 28 Feb 2025 14:43:18 +0100 Subject: [PATCH] MDL-84695 mod_qbank: More precise get_qbank_ids_of_type_in_course query. --- .../classes/local/bank/question_bank_helper.php | 8 +++++--- .../local/bank/question_bank_helper_test.php | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/question/classes/local/bank/question_bank_helper.php b/question/classes/local/bank/question_bank_helper.php index 378b84ec0eb..a5c3b93dd78 100644 --- a/question/classes/local/bank/question_bank_helper.php +++ b/question/classes/local/bank/question_bank_helper.php @@ -529,11 +529,13 @@ class question_bank_helper { $sql = "SELECT cm.id FROM {course_modules} cm JOIN {modules} m ON m.id = cm.module - JOIN {{$defaultyactivityname}} q ON q.id = cm.instance AND cm.module = m.id + JOIN {{$defaultyactivityname}} q ON q.id = cm.instance WHERE cm.course = :course - AND q.type = :type"; + AND q.type = :type + AND m.name = :modulename"; - return $DB->get_fieldset_sql($sql, ['type' => $subtype, 'course' => $course->id]); + return $DB->get_fieldset_sql($sql, ['type' => $subtype, 'course' => $course->id, + 'modulename' => $defaultyactivityname]); } return []; diff --git a/question/tests/local/bank/question_bank_helper_test.php b/question/tests/local/bank/question_bank_helper_test.php index b58084f610a..e99b784da91 100644 --- a/question/tests/local/bank/question_bank_helper_test.php +++ b/question/tests/local/bank/question_bank_helper_test.php @@ -380,6 +380,10 @@ final class question_bank_helper_test extends \advanced_testcase { self::setAdminUser(); $course = self::getDataGenerator()->create_course(); + // Create module other than a qbank. + $wiki = self::getDataGenerator()->create_module('wiki', [ + 'course' => $course->id, + ]); $modinfo = get_fast_modinfo($course); $qbanks = $modinfo->get_instances_of('qbank'); $this->assertCount(0, $qbanks); @@ -389,6 +393,18 @@ final class question_bank_helper_test extends \advanced_testcase { $this->assertEquals(get_string('systembank', 'question'), $qbank->get_name()); $modrecord = $DB->get_record('qbank', ['id' => $qbank->instance]); $this->assertEquals(question_bank_helper::TYPE_SYSTEM, $modrecord->type); + // Swap the qbank instance record for one with the same ID as the wiki instance. + $newqbank = clone($modrecord); + $newqbank->id = $wiki->id; + $DB->insert_record_raw('qbank', $newqbank, customsequence: true); + $DB->delete_records('qbank', ['id' => $qbank->id]); + $DB->set_field('course_modules', 'instance', $newqbank->id, ['instance' => $qbank->instance]); + // Retry the above again. + \course_modinfo::purge_course_caches([$course->id]); + $qbank = question_bank_helper::get_default_open_instance_system_type($course); + $this->assertEquals(get_string('systembank', 'question'), $qbank->get_name()); + $modrecord = $DB->get_record('qbank', ['id' => $qbank->instance]); + $this->assertEquals(question_bank_helper::TYPE_SYSTEM, $modrecord->type); } /**