{"record":{"id":"bdb6e4cfe1fdf1e3","repo":"moodle/moodle","slug":"invalidenrolinstance","errorCode":"invalidenrolinstance","errorMessage":"invalidenrolinstance","messagePattern":"invalidenrolinstance","errorType":"error_code","errorClass":"moodle_exception","httpStatus":null,"severity":"error","filePath":"public/enrol/guest/classes/form/enrol_form.php","lineNumber":52,"sourceCode":"    /** @var \\stdClass */\n    protected $instance;\n\n    /**\n     * Returns the instance of the enrolment method\n     *\n     * @throws \\moodle_exception\n     * @return \\stdClass\n     */\n    protected function get_instance(): \\stdClass {\n        global $DB, $CFG;\n        require_once($CFG->dirroot . '/lib/enrollib.php');\n        if ($this->instance === null) {\n            $courseid = $this->optional_param('id', 0, PARAM_INT);\n            $instanceid = $this->optional_param('instance', 0, PARAM_INT);\n            // We need enrol_get_instances() to validate that the enrolment method is enabled.\n            $instances = enrol_get_instances($courseid, true);\n            if (empty($instances[$instanceid]) || $instances[$instanceid]->enrol !== 'guest') {\n                throw new \\moodle_exception('invalidenrolinstance', 'enrol');\n            }\n            $this->instance = $instances[$instanceid] ?? null;\n        }\n        return $this->instance;\n    }\n\n    #[\\Override]\n    public function definition() {\n        $mform = $this->_form;\n\n        $mform->addElement('password', 'guestpassword', get_string('password', 'enrol_guest'));\n\n        $mform->addElement('hidden', 'id');\n        $mform->setType('id', PARAM_INT);\n\n        $mform->addElement('hidden', 'instance');\n        $mform->setType('instance', PARAM_INT);\n    }","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/enrol/guest/classes/form/enrol_form.php#L34-L70","documentation":"Thrown by enrol_guest\\form\\enrol_form::get_instance() when the requested enrolment instance cannot be resolved: the 'id' (course) or 'instance' request parameter is missing/0, the instance does not exist among the course's ENABLED enrolment instances, or it exists but its enrol method is not 'guest'. enrol_get_instances() is deliberately used so only enabled methods validate. All dynamic-form handlers (validation, submission, context, URL) funnel through get_instance(), so any request with a bad instance id raises this.","triggerScenarios":"Submitting the guest enrol form with a stale/wrong ?instance= parameter; the guest enrolment method was disabled (so enrol_get_instances($courseid, true) omits it); the instance was deleted; an instance of another enrol method's id is passed; instance=0 when the param is absent.","commonSituations":"A course page cached a self-enrol link before the guest method was disabled; an admin toggled 'guest access' off while users had the form open; a bookmarked/EMailed enrol URL pointing at an old instance id; form submitted after the instance id changed.","solutions":["Confirm the guest enrolment method is enabled for the course: Course administration > Enrolment methods, set Guest access to enabled.","Reload the course page to obtain a fresh enrol link with the correct current instance id.","Verify in code before use: $instances = enrol_get_instances($courseid, true); if (empty($instances[$instanceid]) || $instances[$instanceid]->enrol !== 'guest') { ... }.","Check the enrol table: SELECT id,status,enrol FROM {enrol} WHERE courseid=? AND enrol='guest' to confirm the instance exists and is enabled (status = ENROL_INSTANCE_ENABLED)."],"exampleFix":"// before\n$instances = enrol_get_instances($courseid, true);\nif (empty($instances[$instanceid]) || $instances[$instanceid]->enrol !== 'guest') {\n    throw new \\moodle_exception('invalidenrolinstance', 'enrol');\n}\n// after (validate in caller first)\n$instances = enrol_get_instances($courseid, ENROL_INSTANCE_ENABLED);\n$guest = array_filter($instances, fn($i) => $i->enrol === 'guest');\nif (empty($guest)) { redirect(new moodle_url('/course/view.php', ['id'=>$courseid])); }","handlingStrategy":"validation","validationCode":"$instances = enrol_get_instances($courseid, true);\n$valid = !empty($instances[$instanceid]) && $instances[$instanceid]->enrol === 'guest';\nif (!$valid) { redirect(new moodle_url('/course/view.php', ['id'=>$courseid])); }","typeGuard":"function isGuestInstance(?stdClass $instance): bool {\n    return $instance !== null && isset($instance->enrol) && $instance->enrol === 'guest' && isset($instance->id);\n}","tryCatchPattern":"try {\n    $instance = $form->get_instance();\n} catch (\\moodle_exception $e) {\n    if ($e->errorcode === 'invalidenrolinstance') {\n        redirect(new moodle_url('/course/view.php', ['id'=>$courseid]), get_string('invalidenrolinstance', 'enrol'));\n    }\n    throw $e;\n}","preventionTips":["Always pass the current instance id from a freshly loaded course page, not cached links.","Confirm the guest enrol method is enabled before advertising guest access.","Check {enrol} rows (enrol='guest', status=enabled) when debugging.","Regenerate enrol links after deleting/recreating enrolment instances."],"tags":["moodle","enrolment","guest-access","invalid-parameter"],"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"}