From b642d2710b3ca0d29a3d601e1574a204a1e34f7c Mon Sep 17 00:00:00 2001 From: Ilya Tregubov Date: Mon, 4 Dec 2023 10:27:53 +0800 Subject: [PATCH] MDL-79708 question_bank: Fix legacy filter condition. build_query_from_filter method expects includesubcategories to be inside filteroptions array --- lib/db/upgrade.php | 21 +++++++++++++++++++++ mod/quiz/tests/quiz_question_restore_test.php | 20 ++++++++++++++++++++ question/classes/question_reference_manager.php | 6 +++++- version.php | 2 +- 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index d0a47fcc39e..92ccd85f4bb 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -864,5 +864,26 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2023120100.01); } + if ($oldversion < 2023120700.02) { + $sqllike = $DB->sql_like('filtercondition', '?'); + $params[] = '%includesubcategories%'; + + $sql = "SELECT qsr.* FROM {question_set_references} qsr WHERE $sqllike"; + $results = $DB->get_recordset_sql($sql, $params); + foreach ($results as $result) { + $filtercondition = json_decode($result->filtercondition); + if (isset($filtercondition->filter->category->includesubcategories)) { + $filtercondition->filter->category->filteroptions = + ['includesubcategories' => $filtercondition->filter->category->includesubcategories]; + unset($filtercondition->filter->category->includesubcategories); + $result->filtercondition = json_encode($filtercondition); + $DB->update_record('question_set_references', $result); + } + } + $results->close(); + + upgrade_main_savepoint(true, 2023120700.02); + } + return true; } diff --git a/mod/quiz/tests/quiz_question_restore_test.php b/mod/quiz/tests/quiz_question_restore_test.php index f08679b4d61..34da3eec3dd 100644 --- a/mod/quiz/tests/quiz_question_restore_test.php +++ b/mod/quiz/tests/quiz_question_restore_test.php @@ -16,6 +16,8 @@ namespace mod_quiz; +use core_question\question_reference_manager; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -558,6 +560,12 @@ class quiz_question_restore_test extends \advanced_testcase { $this->assertArrayHasKey('filter', $filterconditions); $this->assertArrayHasKey('category', $filterconditions['filter']); $this->assertArrayHasKey('qtagids', $filterconditions['filter']); + $this->assertArrayHasKey('filteroptions', $filterconditions['filter']['category']); + $this->assertArrayHasKey('includesubcategories', $filterconditions['filter']['category']['filteroptions']); + + // MDL-79708: Bad filter conversion check. + $this->assertArrayNotHasKey('includesubcategories', $filterconditions['filter']['category']); + $this->assertArrayNotHasKey('questioncategoryid', $filterconditions); $this->assertArrayNotHasKey('tags', $filterconditions); $expectedtags = \core_tag_tag::get_by_name_bulk(1, ['foo', 'bar']); @@ -567,6 +575,18 @@ class quiz_question_restore_test extends \advanced_testcase { $this->assertEquals($expectedcategory->id, $filterconditions['filter']['category']['values'][0]); $expectedcat = implode(',', [$expectedcategory->id, $expectedcategory->contextid]); $this->assertEquals($expectedcat, $filterconditions['cat']); + + // MDL-79708: Try to convert already converted filter. + $filterconditionsold = $filterconditions; + $filterconditions = question_reference_manager::convert_legacy_set_reference_filter_condition($filterconditions); + // Check that the filtercondition didn't change. + $this->assertEquals($filterconditionsold, $filterconditions); + + // MDL-79708: Try to convert a filter with previously bad conversion. + $filterconditions['filter']['category']['includesubcategories'] = 0; + unset($filterconditions['filter']['category']['filteroptions']); + $filterconditions = question_reference_manager::convert_legacy_set_reference_filter_condition($filterconditions); + $this->assertEquals($filterconditionsold, $filterconditions); } } } diff --git a/question/classes/question_reference_manager.php b/question/classes/question_reference_manager.php index 1ccdf5f2af9..f3bd6c5c1ed 100644 --- a/question/classes/question_reference_manager.php +++ b/question/classes/question_reference_manager.php @@ -94,7 +94,7 @@ class question_reference_manager { $filtercondition['filter']['category'] = [ 'jointype' => \qbank_managecategories\category_condition::JOINTYPE_DEFAULT, 'values' => [$filtercondition['questioncategoryid']], - 'includesubcategories' => $filtercondition['includingsubcategories'], + 'filteroptions' => ['includesubcategories' => $filtercondition['includingsubcategories']], ]; unset($filtercondition['questioncategoryid']); unset($filtercondition['includingsubcategories']); @@ -119,6 +119,10 @@ class question_reference_manager { $filtercondition['qpage'] = 0; $filtercondition['qperpage'] = 100; $filtercondition['jointype'] = \core\output\datafilter::JOINTYPE_ALL; + } else if (isset($filtercondition['filter']['category']['includesubcategories'])) { + $filtercondition['filter']['category']['filteroptions'] = + ['includesubcategories' => $filtercondition['filter']['category']['includesubcategories']]; + unset($filtercondition['filter']['category']['includesubcategories']); } return $filtercondition; } diff --git a/version.php b/version.php index ce97bb122db..2b1b0fe331b 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2023120700.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2023120700.02; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.4dev (Build: 20231207)'; // Human-friendly version name -- 2.11.4.GIT