MDL-71378 core_question: Only show module banks in question filters

This commit is contained in:
Simon Adams 2024-07-26 11:52:23 +01:00
parent a6c4ddada5
commit 291fdacfd5
6 changed files with 34 additions and 16 deletions

View File

@ -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';

View File

@ -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);

View File

@ -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

View File

@ -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);

View File

@ -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 {

View File

@ -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);