{"record":{"id":"ef38af72987c6d2a","repo":"moodle/moodle","slug":"invalid-params-the-competency-does-not-belong-to-the-course","errorCode":null,"errorMessage":"Invalid params. The competency does not belong to the course.","messagePattern":"Invalid params\\. The competency does not belong to the course\\.","errorType":"error_code","errorClass":"invalid_parameter_exception","httpStatus":null,"severity":"error","filePath":"public/admin/tool/lp/classes/output/user_competency_summary_in_course.php","lineNumber":76,"sourceCode":"    public function __construct($userid, $competencyid, $courseid) {\n        $this->userid = $userid;\n        $this->competencyid = $competencyid;\n        $this->courseid = $courseid;\n    }\n\n    /**\n     * Export the data.\n     *\n     * @param renderer_base $output\n     * @return stdClass\n     */\n    public function export_for_template(renderer_base $output) {\n        global $DB;\n\n        $usercompetencycourse = api::get_user_competency_in_course($this->courseid, $this->userid, $this->competencyid);\n        $competency = $usercompetencycourse->get_competency();\n        if (empty($usercompetencycourse) || empty($competency)) {\n            throw new \\invalid_parameter_exception('Invalid params. The competency does not belong to the course.');\n        }\n\n        $relatedcompetencies = api::list_related_competencies($competency->get('id'));\n        $user = $DB->get_record('user', array('id' => $this->userid));\n        $evidence = api::list_evidence_in_course($this->userid, $this->courseid, $this->competencyid);\n        $course = $DB->get_record('course', array('id' => $this->courseid));\n\n        $params = array(\n            'competency' => $competency,\n            'usercompetencycourse' => $usercompetencycourse,\n            'evidence' => $evidence,\n            'user' => $user,\n            'course' => $course,\n            'scale' => $competency->get_scale(),\n            'relatedcompetencies' => $relatedcompetencies\n        );\n        $exporter = new user_competency_summary_in_course_exporter(null, $params);\n        $data = $exporter->export($output);","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/admin/tool/lp/classes/output/user_competency_summary_in_course.php#L58-L94","documentation":"user_competency_summary_in_course::export_for_template() throws \\invalid_parameter_exception with the literal message 'Invalid params. The competency does not belong to the course.' when api::get_user_competency_in_course($courseid, $userid, $competencyid) yields no matching user competency/competency record for that course. Note api::get_user_competency_in_course usually throws first if nothing is found, so reaching the throw implies an inconsistent or empty result.","triggerScenarios":"Constructing the exporter with a competencyid that is not linked to the given course, then calling export_for_template() — e.g. courseid/competencyid mismatch from a crafted request, competency unlinked from the course between page load and export, or the competency deleted.","commonSituations":"External tools (web services, deep links) passing competency IDs from a different course; stale links after removing competencies from a course template; data cleanup scripts unlinking course competencies while old URLs persist.","solutions":["Verify the competency is actually linked to the course (tool_lp_coursecompetencies / api::list_competencies_in_course) before exporting","Validate courseid+competencyid pairing server-side; don't trust client-supplied IDs","Wrap export in try-catch on \\invalid_parameter_exception and show a 'not found in this course' message","Refresh the page/re-fetch data if the competency was just unlinked"],"exampleFix":"// before\n$exporter = new \\tool_lp\\output\\user_competency_summary_in_course($courseid, $userid, $competencyid);\n$html = $renderer->render($exporter->export_for_template($output));\n// after\nif (in_array($competencyid, array_map(fn($c) => $c->get('id'), \\core_competency\\api::list_competencies_in_course($courseid)))) {\n    $exporter = new \\tool_lp\\output\\user_competency_summary_in_course($courseid, $userid, $competencyid);\n    $html = $renderer->render($exporter->export_for_template($output));\n}","handlingStrategy":"validation","validationCode":"$courseCompIds = array_map(fn($c) => $c->get('id'), \\core_competency\\api::list_competencies_in_course($courseid));\n$valid = in_array($competencyid, $courseCompIds);","typeGuard":null,"tryCatchPattern":"try {\n    $data = $exporter->export_for_template($output);\n} catch (\\invalid_parameter_exception $e) {\n    if (strpos($e->getMessage(), 'does not belong to the course') !== false) {\n        // show 'competency not in this course' and re-fetch the list\n    }\n    throw $e;\n}","preventionTips":["Derive competency IDs from the course's competency list, never from client input","Re-validate links after template/course competency edits","Catch invalid_parameter_exception in web-service wrappers"],"tags":["moodle","invalid-params","competency","validation"],"backgroundTag":"invalid-argument-value","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"}