Skip to content

Commit

Permalink
MDL-79708 question_bank: Fix legacy filter condition.
Browse files Browse the repository at this point in the history
build_query_from_filter method expects includesubcategories
to be inside filteroptions array
  • Loading branch information
ilyatregubov committed Dec 11, 2023
1 parent a891866 commit b642d27
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
21 changes: 21 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
20 changes: 20 additions & 0 deletions mod/quiz/tests/quiz_question_restore_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace mod_quiz;

use core_question\question_reference_manager;

defined('MOODLE_INTERNAL') || die();

global $CFG;
Expand Down Expand Up @@ -558,6 +560,12 @@ public function test_pre_43_quiz_restore_for_random_question_filtercondition() {
$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']);
Expand All @@ -567,6 +575,18 @@ public function test_pre_43_quiz_restore_for_random_question_filtercondition() {
$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);
}
}
}
6 changes: 5 additions & 1 deletion question/classes/question_reference_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static function convert_legacy_set_reference_filter_condition(array $filt
$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']);
Expand All @@ -119,6 +119,10 @@ public static function convert_legacy_set_reference_filter_condition(array $filt
$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;
}
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b642d27

Please sign in to comment.