diff --git a/question/classes/local/bank/question_bank_helper.php b/question/classes/local/bank/question_bank_helper.php index eb193835c7f..405d59c1547 100644 --- a/question/classes/local/bank/question_bank_helper.php +++ b/question/classes/local/bank/question_bank_helper.php @@ -525,13 +525,16 @@ class question_bank_helper { $defaultyactivityname = self::get_default_question_bank_activity_name(); $qbanks = $modinfo->get_instances_of($defaultyactivityname); + $whereclause = "AND m.name = '" . $defaultyactivityname . "'"; + if (!empty($qbanks)) { $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 " . + $whereclause; return $DB->get_fieldset_sql($sql, ['type' => $subtype, 'course' => $course->id]); } diff --git a/question/tests/local/bank/question_bank_helper_test.php b/question/tests/local/bank/question_bank_helper_test.php index 36a4ec9d88c..e2b6cb2d1d8 100644 --- a/question/tests/local/bank/question_bank_helper_test.php +++ b/question/tests/local/bank/question_bank_helper_test.php @@ -379,6 +379,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); @@ -388,6 +392,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); } /**