moodle/moodle · error · moodle_exception

nopermissiontoupdatecalendar

nopermissiontoupdatecalendar

Error message

nopermissiontoupdatecalendar

What it means

moodle_exception('nopermissiontoupdatecalendar') is thrown by calendar_external::submit_create_update_form when the current user has no allowed calendar event types at all for the given course. calendar_get_allowed_event_types() returns an associative array of eventtype=>bool based on capabilities; if none are true the user cannot create or update any calendar event, so the external API aborts before form validation.

Solutions

  1. Grant the user an event-creation capability for the target context (e.g. moodle/calendar:manageownentries for user events, moodle/calendar:manageentries for course/site events) via role assignment or capability override.
  2. Check calendar_get_allowed_event_types($courseid) before calling submit_create_update_form and show an appropriate UI error instead of invoking the external function.
  3. If the intent is to test the no-permission path, expect this exception and catch it in the test rather than treating it as a failure.
  4. Verify $courseid resolves to the intended course; passing a course the user cannot act in narrows allowed types to empty.

Example fix

// before
$result = core_calendar_external::submit_create_update_form($formdata);
// after
$allowed = calendar_get_allowed_event_types($courseid);
if (!in_array(true, $allowed, true)) {
    throw new require_login_exception('nopermissiontoupdatecalendar');
}
$result = core_calendar_external::submit_create_update_form($formdata);
Defensive patterns

Strategy: validation

Validate before calling

$allowed = calendar_get_allowed_event_types($courseid);
if (!in_array(true, $allowed, true)) { // user cannot create any event type
    throw new moodle_exception('nopermissiontoupdatecalendar', 'calendar');
}

Try / catch

try {
    $result = core_calendar_external::submit_create_update_form($formdata);
} catch (moodle_exception $e) {
    if ($e->errorcode === 'nopermissiontoupdatecalendar') {
        redirect($returnurl, get_string('nopermissions', 'error'), null, \core\output\notification::NOTIFY_WARNING);
    }
    throw $e;
}

Prevention

When it happens

Trigger: Calling the core_calendar_submit_create_update_form external function as a user whose capabilities grant zero event types (no calendar:patholeusrvents/calendar:manageentries etc.) for the resolved context, e.g. a guest or a user with only view capability, with any courseid including SITEID.

Common situations: Developers testing the webservices/AJAX form create path with a default student or guest account; roles stripped of calendar capabilities after upgrades; tests that log in as a user lacking moodle/calendar:manageownentries before calling the external function.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


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

Appendix: source

Thrown at public/calendar/externallib.php:909

        self::validate_context($context);
        parse_str($params['formdata'], $data);

        if (WS_SERVER) {
            // Request via WS, ignore sesskey checks in form library.
            $USER->ignoresesskey = true;
        }

        $eventtype = isset($data['eventtype']) ? $data['eventtype'] : null;
        $coursekey = ($eventtype == 'group') ? 'groupcourseid' : 'courseid';
        $courseid = (!empty($data[$coursekey])) ? $data[$coursekey] : null;
        $editoroptions = \core_calendar\local\event\forms\create::build_editor_options($context);
        $formoptions = ['editoroptions' => $editoroptions, 'courseid' => $courseid];
        $allowedeeventtypes = calendar_get_allowed_event_types($courseid);

        // Event type validation.
        if (in_array(true, $allowedeeventtypes, true) === false) {
            throw new \moodle_exception('nopermissiontoupdatecalendar');
        }
        if (empty($eventtype) || !isset($allowedeeventtypes[$eventtype]) || $allowedeeventtypes[$eventtype] == false) {
            return ['validationerror' => true];
        }

        $formoptions['eventtypes'] = $allowedeeventtypes;
        if ($courseid) {
            require_once($CFG->libdir . '/grouplib.php');
            $groupcoursedata = groups_get_all_groups($courseid);
            if (!empty($groupcoursedata)) {
                $formoptions['groups'] = [];
                foreach ($groupcoursedata as $groupid => $groupdata) {
                    $formoptions['groups'][$groupid] = $groupdata->name;
                }
            }
        }

        if (!empty($data['id'])) {

View on GitHub (pinned to 48ea8c3322)