From 39923878e61fc9774eefaaa9e5fd0fbbc4147b42 Mon Sep 17 00:00:00 2001 From: Mark Johnson Date: Fri, 21 Mar 2025 10:40:59 +0000 Subject: [PATCH] MDL-83272 qbank_managecategories: Fix exclusion of current category The "currentcat" parameter contains the categoryid, but is being compared to a string like "contextid,categoryid" so was not being matched correctly to exclude the current category from the list of options. --- question/bank/managecategories/category.php | 2 +- .../bank/managecategories/classes/helper.php | 2 +- .../managecategories/tests/helper_test.php | 34 +++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/question/bank/managecategories/category.php b/question/bank/managecategories/category.php index 9afd4626867..c878bbd3203 100644 --- a/question/bank/managecategories/category.php +++ b/question/bank/managecategories/category.php @@ -67,7 +67,7 @@ if ($todelete) { if ($questionstomove) { $categorycontext = context::instance_by_id($category->contextid); $moveform = new question_move_form($thispageurl, - ['contexts' => [$categorycontext], 'currentcat' => $todelete]); + ['contexts' => [$categorycontext], 'currentcat' => "$todelete"]); if ($moveform->is_cancelled()) { $thispageurl->remove_all_params(); if (!is_null($cmid)) { diff --git a/question/bank/managecategories/classes/helper.php b/question/bank/managecategories/classes/helper.php index f4c7a543dd6..cffed3beaf0 100644 --- a/question/bank/managecategories/classes/helper.php +++ b/question/bank/managecategories/classes/helper.php @@ -393,7 +393,7 @@ class helper { foreach ($categories as $category) { if ($category->contextid == $contextid) { $cid = $category->id; - if ($currentcat != $cid || $currentcat == 0) { + if ("{$currentcat},{$contextid}" != $cid || $currentcat == 0) { $a = new \stdClass(); $a->name = format_string( $category->indentedname, diff --git a/question/bank/managecategories/tests/helper_test.php b/question/bank/managecategories/tests/helper_test.php index a10f8f3b7d7..f502d0f1740 100644 --- a/question/bank/managecategories/tests/helper_test.php +++ b/question/bank/managecategories/tests/helper_test.php @@ -296,6 +296,40 @@ final class helper_test extends manage_category_test_base { } } + /** + * Test that question_category_options function does not include the current category. + * + * @covers ::question_category_options + */ + public function test_question_category_options_exclude_current(): void { + $this->setAdminUser(); + $this->resetAfterTest(); + + // Create categories. + $quiz = $this->create_quiz(); + $qcategory1 = $this->create_question_category_for_a_quiz($quiz); + $qcategory2 = $this->create_question_category_for_a_quiz($quiz, ['parent' => $qcategory1->id]); + $qcategory3 = $this->create_question_category_for_a_quiz($quiz); + + $contexts = new \core_question\local\bank\question_edit_contexts(\context_module::instance($quiz->cmid)); + + $categorycontexts = helper::question_category_options($contexts->having_cap('moodle/question:add')); + // We get all categories without the currentcat parameter. + $categorycontext = $categorycontexts['Quiz: ' . $quiz->name]; + $this->assertCount(3, $categorycontext); + + // The currentcat category is excluded. + $newcategorycontexts = helper::question_category_options( + $contexts->having_cap('moodle/question:add'), + currentcat: $qcategory2->id, + ); + $newcategorycontext = $newcategorycontexts['Quiz: ' . $quiz->name]; + $this->assertCount(2, $newcategorycontext); + $this->assertContains($qcategory1->name, $newcategorycontext); + $this->assertNotContains($qcategory2->name, $newcategorycontext); + $this->assertContains($qcategory3->name, $newcategorycontext); + } + /** * Test that get_categories_for_contexts function returns the correct question count number. *