moodle/moodle · error · moodle_exception
cannotfindcate
cannotfindcate
Error message
cannotfindcate
What it means
Thrown by the qbank_bulkmove external service (public/question/classes/external/move_questions.php) when the target category id+contextid pair supplied for a bulk question move does not exist in the question_categories table. After verifying the caller has 'moodle/question:add' in the target context, it looks up question_categories by ['id' => $newcategoryid, 'contextid' => $newcontextid] and throws moodle_exception('cannotfindcate', 'question') on failure, before \qbank_bulkmove\helper::bulk_move_questions() runs.
Solutions
- Confirm the target category id exists: $DB->record_exists('question_categories', ['id' => $newcategoryid, 'contextid' => $newcontextid]) before calling.
- Re-fetch the category list for the target context from the question bank UI and use a current id.
- Verify $newcontextid is the correct context for that category (course category vs course vs module context).
- If categories were deleted, recreate a target category in the desired context and retry the move.
- Validate the capability prerequisite too: the user needs moodle/question:add in the target context.
Example fix
// before
$contexts = new \core_question\local\bank\question_edit_contexts($newcontext);
$contexts->require_cap('moodle/question:add');
if (!$targetcategory = $DB->get_record('question_categories', ['id' => $newcategoryid, 'contextid' => $newcontextid])) {
throw new \moodle_exception('cannotfindcate', 'question');
}
// after (caller guard)
if (!$DB->record_exists('question_categories', ['id' => $newcategoryid, 'contextid' => $newcontextid])) {
$newcategoryid = question_get_default_category_id($newcontextid);
}
$contexts->require_cap('moodle/question:add');
// then proceed with move_questions(..., $newcategoryid, $newcontextid, ...) Defensive patterns
Strategy: validation
Validate before calling
if (!$DB->record_exists('question_categories', ['id' => $newcategoryid, 'contextid' => $newcontextid])) {
throw new \moodle_exception('cannotfindcate', 'question');
}
if (!has_capability('moodle/question:add', context::instance_by_id($newcontextid))) {
throw new \moodle_exception('nopermissions', 'error');
} Type guard
function is_valid_target_category(int $categoryid, int $contextid, moodle_database $DB): bool {
return (bool) $DB->record_exists('question_categories', ['id' => $categoryid, 'contextid' => $contextid]);
} Try / catch
try {
// external move_questions call
question_move_questions($questionids, $newcategoryid, $newcontextid, ...);
} catch (\moodle_exception $e) {
if ($e->errorcode === 'cannotfindcate') {
// refresh category list and pick a valid target
}
throw $e;
} Prevention
- Always pair the category id with its exact contextid from a fresh DB query, never cached values.
- Re-fetch categories after course resets, restores, or deletions before bulk moves.
- Verify moodle/question:add capability in the target context before attempting moves.
When it happens
Trigger: Calling the move_questions external function (or its web-service/AmD wrapper) with a $newcategoryid that does not exist, or an existing category whose contextid does not match $newcontextid — e.g. a stale category id after a context or course deletion, or mixing up ids from a different context.
Common situations: Bulk-move UI pointing at a deleted category; client code caching category ids across course resets; passing a contextid from a different course/module than the category; version-migration cleanups that removed question_categories rows.
Understand the failure class
Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.
Related errors
AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14).
Data as JSON: /api/errors/08515c0edfc9ecad.
Report an issue: GitHub.
Appendix: source
Thrown at public/question/classes/external/move_questions.php:106
'questionids' => $questionids,
'returnurl' => $returnurlstring,
] = self::validate_parameters(self::execute_parameters(), [
'newcontextid' => $newcontextid,
'newcategoryid' => $newcategoryid,
'questionids' => $questionids,
'returnurl' => $returnurlstring,
]);
$newcontext = context::instance_by_id($newcontextid);
self::validate_context($newcontext);
\core_question\local\bank\helper::require_plugin_enabled('qbank_bulkmove');
$contexts = new \core_question\local\bank\question_edit_contexts($newcontext);
$contexts->require_cap('moodle/question:add');
if (!$targetcategory = $DB->get_record('question_categories', ['id' => $newcategoryid, 'contextid' => $newcontextid])) {
throw new \moodle_exception('cannotfindcate', 'question');
}
\qbank_bulkmove\helper::bulk_move_questions($questionids, $targetcategory);
notification::success(get_string('questionsmoved', 'qbank_bulkmove'));
if ($returnurlstring) {
$returnurl = new moodle_url($returnurlstring);
$returnurl->param('cmid', $newcontext->instanceid);
$returnurl->param('cat', "{$newcategoryid},{$newcontextid}");
$returnurl->remove_params('category');
// We can only highlight 1 question, so only highlight if we're moving a single question.
$qids = explode(',', $questionids);
if (count($qids) === 1) {
$returnurl->param('lastchanged', reset($qids));
} else {
$returnurl->remove_params('lastchanged');
};
$filter = $returnurl->param('filter');View on GitHub (pinned to 48ea8c3322)