From 291fdacfd527f86d1fb1cac3519207ed1fe0d826 Mon Sep 17 00:00:00 2001 From: Simon Adams Date: Fri, 26 Jul 2024 11:52:23 +0100 Subject: [PATCH] MDL-71378 core_question: Only show module banks in question filters --- lang/en/question.php | 7 ----- .../custom_category_condition_helper.php | 3 +++ .../classes/category_condition.php | 5 +++- .../bank/managecategories/classes/helper.php | 27 +++++++++++++++---- question/classes/local/bank/view.php | 1 + question/tests/version_test.php | 7 ++--- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lang/en/question.php b/lang/en/question.php index 93fb0e5c170..2dd7cbd77de 100644 --- a/lang/en/question.php +++ b/lang/en/question.php @@ -121,13 +121,6 @@ $string['donothing'] = 'Don\'t copy or move files or change links.'; $string['editcategories'] = 'Edit categories'; $string['editcategories_help'] = 'Rather than keeping everything in one big list, questions may be arranged into categories and subcategories. -Each category has a context which determines where the questions in the category can be used: - -* Activity context - Questions only available in the activity module -* Course context - Questions available in all activity modules in the course -* Course category context - Questions available in all activity modules and courses in the course category -* System context - Questions available in all courses and activities on the site - Categories are also used for random questions, as questions are selected from a particular category.'; $string['editcategories_link'] = 'question/category'; $string['editcategory'] = 'Edit category'; diff --git a/mod/quiz/classes/question/bank/filter/custom_category_condition_helper.php b/mod/quiz/classes/question/bank/filter/custom_category_condition_helper.php index 5164f0f52bb..37d0fec06db 100644 --- a/mod/quiz/classes/question/bank/filter/custom_category_condition_helper.php +++ b/mod/quiz/classes/question/bank/filter/custom_category_condition_helper.php @@ -37,6 +37,9 @@ class custom_category_condition_helper extends \qbank_managecategories\helper { global $CFG; $pcontexts = []; foreach ($contexts as $context) { + if ($context->contextlevel !== CONTEXT_MODULE) { + continue; + } $pcontexts[] = $context->id; } $contextslist = join(', ', $pcontexts); diff --git a/question/bank/managecategories/classes/category_condition.php b/question/bank/managecategories/classes/category_condition.php index 8975f64d3c8..6ff8882e556 100644 --- a/question/bank/managecategories/classes/category_condition.php +++ b/question/bank/managecategories/classes/category_condition.php @@ -57,7 +57,9 @@ class category_condition extends condition { return; } $this->cat = $qbank->get_pagevars('cat'); - $this->contexts = $qbank->contexts->having_one_edit_tab_cap($qbank->get_pagevars('tabname')); + $this->contexts = array_filter($qbank->contexts->having_one_edit_tab_cap($qbank->get_pagevars('tabname')), + static fn($context) => $context->contextlevel === CONTEXT_MODULE + ); $this->course = $qbank->course; [$categoryid, $contextid] = self::validate_category_param($this->cat); @@ -72,6 +74,7 @@ class category_condition extends condition { } /** + * MDL-71378 TODO: Not used and to be deprecated anyway * Return default category * * @return \stdClass default category diff --git a/question/bank/managecategories/classes/helper.php b/question/bank/managecategories/classes/helper.php index cbe0bfc4545..283467f30eb 100644 --- a/question/bank/managecategories/classes/helper.php +++ b/question/bank/managecategories/classes/helper.php @@ -293,7 +293,7 @@ class helper { * Get all the category objects, including a count of the number of questions in that category, * for all the categories in the lists $contexts. * - * @param string $contexts + * @param string $contexts comma separated list of contextids * @param string $sortorder used as the ORDER BY clause in the select statement. * @param bool $top Whether to return the top categories or not. * @param int $showallversions 1 to show all versions not only the latest. @@ -307,6 +307,20 @@ class helper { int $showallversions = 0, ): array { global $DB; + + $contextids = explode(',', $contexts); + foreach ($contextids as $contextid) { + $context = context::instance_by_id($contextid); + if ($context->contextlevel === CONTEXT_MODULE) { + $validcontexts[] = $contextid; + } + } + if (empty($validcontexts)) { + return []; + } + + [$insql, $inparams] = $DB->get_in_or_equal($validcontexts); + $topwhere = $top ? '' : 'AND c.parent <> 0'; $statuscondition = "AND (qv.status = '" . question_version_status::QUESTION_STATUS_READY . "' " . " OR qv.status = '" . question_version_status::QUESTION_STATUS_DRAFT . "' )"; @@ -319,7 +333,7 @@ class helper { WHERE q.parent = '0' $statuscondition AND c.id = qbe.questioncategoryid - AND ($showallversions = 1 + AND ({$showallversions} = 1 OR (qv.version = (SELECT MAX(v.version) FROM {question_versions} v JOIN {question_bank_entries} be ON be.id = v.questionbankentryid @@ -328,10 +342,10 @@ class helper { ) ) AS questioncount FROM {question_categories} c - WHERE c.contextid IN ($contexts) $topwhere - ORDER BY $sortorder"; + WHERE c.contextid {$insql} {$topwhere} + ORDER BY {$sortorder}"; - return $DB->get_records_sql($sql); + return $DB->get_records_sql($sql, $inparams); } /** @@ -357,6 +371,9 @@ class helper { global $CFG; $pcontexts = []; foreach ($contexts as $context) { + if ($context->contextlevel !== CONTEXT_MODULE) { + continue; + } $pcontexts[] = $context->id; } $contextslist = join(', ', $pcontexts); diff --git a/question/classes/local/bank/view.php b/question/classes/local/bank/view.php index b0f69dec348..6d51e2dfb76 100644 --- a/question/classes/local/bank/view.php +++ b/question/classes/local/bank/view.php @@ -251,6 +251,7 @@ class view { // Create the url of the new question page to forward to. $this->returnurl = $pageurl->out_as_local_url(false); $this->editquestionurl = new \moodle_url('/question/bank/editquestion/question.php', ['returnurl' => $this->returnurl]); + // MDL-71378 TODO: refactor this. Must require cm. if ($this->cm !== null) { $this->editquestionurl->param('cmid', $this->cm->id); } else { diff --git a/question/tests/version_test.php b/question/tests/version_test.php index f390da16116..463c3e326ba 100644 --- a/question/tests/version_test.php +++ b/question/tests/version_test.php @@ -203,15 +203,16 @@ class version_test extends \advanced_testcase { $qcategory = $this->qgenerator->create_question_category(['contextid' => $this->context->id]); $qcategorychild = $this->qgenerator->create_question_category(['contextid' => $this->context->id, 'parent' => $qcategory->id]); - $systemcontext = \context_system::instance(); - $qcategorysys = $this->qgenerator->create_question_category(['contextid' => $systemcontext->id]); + $qbank = self::getDataGenerator()->create_module('qbank', ['course' => $this->course->id]); + $bankcontext = \context_module::instance($qbank->cmid); + $qcategorysys = $this->qgenerator->create_question_category(['contextid' => $bankcontext->id]); $question = $this->qgenerator->create_question('shortanswer', null, ['category' => $qcategorychild->id]); $questiondefinition = question_bank::load_question($question->id); // Add it to the quiz. quiz_add_quiz_question($question->id, $this->quiz); - // Move the category to system context. + // Move the category to qbank context. $manager = new category_manager(); $manager->move_questions_and_delete_category($qcategorychild->id, $qcategorysys->id);