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.
This commit is contained in:
Mark Johnson
2025-03-21 10:40:59 +00:00
parent c64e967ce6
commit 39923878e6
3 changed files with 36 additions and 2 deletions
+1 -1
View File
@@ -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)) {
@@ -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,
@@ -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.
*