moodle/moodle · error · invalid_parameter_exception

Invalid params. The competency does not belong to the…

Error message

Invalid params. The competency does not belong to the course.

What it means

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.

Solutions

  1. Verify the competency is actually linked to the course (tool_lp_coursecompetencies / api::list_competencies_in_course) before exporting
  2. Validate courseid+competencyid pairing server-side; don't trust client-supplied IDs
  3. Wrap export in try-catch on \invalid_parameter_exception and show a 'not found in this course' message
  4. Refresh the page/re-fetch data if the competency was just unlinked

Example fix

// before
$exporter = new \tool_lp\output\user_competency_summary_in_course($courseid, $userid, $competencyid);
$html = $renderer->render($exporter->export_for_template($output));
// after
if (in_array($competencyid, array_map(fn($c) => $c->get('id'), \core_competency\api::list_competencies_in_course($courseid)))) {
    $exporter = new \tool_lp\output\user_competency_summary_in_course($courseid, $userid, $competencyid);
    $html = $renderer->render($exporter->export_for_template($output));
}
Defensive patterns

Strategy: validation

Validate before calling

$courseCompIds = array_map(fn($c) => $c->get('id'), \core_competency\api::list_competencies_in_course($courseid));
$valid = in_array($competencyid, $courseCompIds);

Try / catch

try {
    $data = $exporter->export_for_template($output);
} catch (\invalid_parameter_exception $e) {
    if (strpos($e->getMessage(), 'does not belong to the course') !== false) {
        // show 'competency not in this course' and re-fetch the list
    }
    throw $e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/ef38af72987c6d2a. Report an issue: GitHub.

Appendix: source

Thrown at public/admin/tool/lp/classes/output/user_competency_summary_in_course.php:76

    public function __construct($userid, $competencyid, $courseid) {
        $this->userid = $userid;
        $this->competencyid = $competencyid;
        $this->courseid = $courseid;
    }

    /**
     * Export the data.
     *
     * @param renderer_base $output
     * @return stdClass
     */
    public function export_for_template(renderer_base $output) {
        global $DB;

        $usercompetencycourse = api::get_user_competency_in_course($this->courseid, $this->userid, $this->competencyid);
        $competency = $usercompetencycourse->get_competency();
        if (empty($usercompetencycourse) || empty($competency)) {
            throw new \invalid_parameter_exception('Invalid params. The competency does not belong to the course.');
        }

        $relatedcompetencies = api::list_related_competencies($competency->get('id'));
        $user = $DB->get_record('user', array('id' => $this->userid));
        $evidence = api::list_evidence_in_course($this->userid, $this->courseid, $this->competencyid);
        $course = $DB->get_record('course', array('id' => $this->courseid));

        $params = array(
            'competency' => $competency,
            'usercompetencycourse' => $usercompetencycourse,
            'evidence' => $evidence,
            'user' => $user,
            'course' => $course,
            'scale' => $competency->get_scale(),
            'relatedcompetencies' => $relatedcompetencies
        );
        $exporter = new user_competency_summary_in_course_exporter(null, $params);
        $data = $exporter->export($output);

View on GitHub (pinned to 48ea8c3322)