{"record":{"id":"08515c0edfc9ecad","repo":"moodle/moodle","slug":"cannotfindcate","errorCode":"cannotfindcate","errorMessage":"cannotfindcate","messagePattern":"cannotfindcate","errorType":"exception","errorClass":"moodle_exception","httpStatus":null,"severity":"error","filePath":"public/question/classes/external/move_questions.php","lineNumber":106,"sourceCode":"            'questionids' => $questionids,\n            'returnurl' => $returnurlstring,\n        ] = self::validate_parameters(self::execute_parameters(), [\n            'newcontextid' => $newcontextid,\n            'newcategoryid' => $newcategoryid,\n            'questionids' => $questionids,\n            'returnurl' => $returnurlstring,\n        ]);\n\n        $newcontext = context::instance_by_id($newcontextid);\n        self::validate_context($newcontext);\n\n        \\core_question\\local\\bank\\helper::require_plugin_enabled('qbank_bulkmove');\n\n        $contexts = new \\core_question\\local\\bank\\question_edit_contexts($newcontext);\n        $contexts->require_cap('moodle/question:add');\n\n        if (!$targetcategory = $DB->get_record('question_categories', ['id' => $newcategoryid, 'contextid' => $newcontextid])) {\n            throw new \\moodle_exception('cannotfindcate', 'question');\n        }\n\n        \\qbank_bulkmove\\helper::bulk_move_questions($questionids, $targetcategory);\n        notification::success(get_string('questionsmoved', 'qbank_bulkmove'));\n\n        if ($returnurlstring) {\n            $returnurl = new moodle_url($returnurlstring);\n            $returnurl->param('cmid', $newcontext->instanceid);\n            $returnurl->param('cat', \"{$newcategoryid},{$newcontextid}\");\n            $returnurl->remove_params('category');\n            // We can only highlight 1 question, so only highlight if we're moving a single question.\n            $qids = explode(',', $questionids);\n            if (count($qids) === 1) {\n                $returnurl->param('lastchanged', reset($qids));\n            } else {\n                $returnurl->remove_params('lastchanged');\n            };\n            $filter = $returnurl->param('filter');","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/question/classes/external/move_questions.php#L88-L124","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n$contexts = new \\core_question\\local\\bank\\question_edit_contexts($newcontext);\n$contexts->require_cap('moodle/question:add');\nif (!$targetcategory = $DB->get_record('question_categories', ['id' => $newcategoryid, 'contextid' => $newcontextid])) {\n    throw new \\moodle_exception('cannotfindcate', 'question');\n}\n// after (caller guard)\nif (!$DB->record_exists('question_categories', ['id' => $newcategoryid, 'contextid' => $newcontextid])) {\n    $newcategoryid = question_get_default_category_id($newcontextid);\n}\n$contexts->require_cap('moodle/question:add');\n// then proceed with move_questions(..., $newcategoryid, $newcontextid, ...)","handlingStrategy":"validation","validationCode":"if (!$DB->record_exists('question_categories', ['id' => $newcategoryid, 'contextid' => $newcontextid])) {\n    throw new \\moodle_exception('cannotfindcate', 'question');\n}\nif (!has_capability('moodle/question:add', context::instance_by_id($newcontextid))) {\n    throw new \\moodle_exception('nopermissions', 'error');\n}","typeGuard":"function is_valid_target_category(int $categoryid, int $contextid, moodle_database $DB): bool {\n    return (bool) $DB->record_exists('question_categories', ['id' => $categoryid, 'contextid' => $contextid]);\n}","tryCatchPattern":"try {\n    // external move_questions call\n    question_move_questions($questionids, $newcategoryid, $newcontextid, ...);\n} catch (\\moodle_exception $e) {\n    if ($e->errorcode === 'cannotfindcate') {\n        // refresh category list and pick a valid target\n    }\n    throw $e;\n}","preventionTips":["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."],"tags":["moodle","web-service","question-bank","not-found"],"backgroundTag":"record-not-found","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}