moodle/moodle · error · moodle_exception

contextnotallowed

contextnotallowed

Error message

contextnotallowed

What it means

The content bank upload form's check_access_for_dynamic_submission() verifies the submitted context is one the content bank is allowed to operate in via core_contentbank::is_context_allowed(). If not, it throws moodle_exception 'contextnotallowed' from core_contentbank. This guards against uploading content into system/user contexts or contexts lacking content-bank support.

Solutions

  1. Pass a valid, content-bank-allowed contextid (typically a course or module context) to the form.
  2. Check (new \core_contentbank\contentbank())->is_context_allowed($context) before rendering/submitting the form.
  3. If embedding the content bank in a custom plugin, ensure the plugin's context is registered as allowed for content bank use.

Example fix

// before
$mform = new \contentbank\form\upload_files(null, ['contextid' => $contextid]); // contextid=1 (system)
// after
$context = context::instance_by_id($contextid, MUST_EXIST);
if (!(new \core_contentbank\contentbank())->is_context_allowed($context)) {
    $contextid = $coursecontext->id; // fall back to an allowed context
}
Defensive patterns

Strategy: validation

Validate before calling

$context = context::instance_by_id($contextid, MUST_EXIST);
if (!(new \core_contentbank\contentbank())->is_context_allowed($context)) {
    throw new \moodle_exception('contextnotallowed', 'core_contentbank');
}

Prevention

When it happens

Trigger: Submitting the upload_files dynamic form with a contextid parameter pointing at a context type the content bank disallows (e.g. system context, user context without enabled content bank usage).

Common situations: Misconfigured page parameters, crafted URLs/forms posting an arbitrary contextid, or plugins embedding the content bank in unsupported contexts.

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/0fa11833e2163f8a. Report an issue: GitHub.

Appendix: source

Thrown at public/contentbank/classes/form/upload_files.php:77

            $errors['file'] = get_string('userquotalimit', 'error');
        }
        return $errors;
    }

    /**
     * Check if current user has access to this form, otherwise throw exception
     *
     * Sometimes permission check may depend on the action and/or id of the entity.
     * If necessary, form data is available in $this->_ajaxformdata or
     * by calling $this->optional_param()
     */
    protected function check_access_for_dynamic_submission(): void {
        require_capability('moodle/contentbank:upload', $this->get_context_for_dynamic_submission());

        // Check the context used by the content bank is allowed.
        $cb = new \core_contentbank\contentbank();
        if (!$cb->is_context_allowed($this->get_context_for_dynamic_submission())) {
            throw new \moodle_exception('contextnotallowed', 'core_contentbank');
        }

        // If $id is defined, the file content will be replaced (instead of uploading a new one).
        // Check that the user has the right permissions to replace this content file.
        $id = $this->optional_param('id', null, PARAM_INT);
        if ($id) {
            $content = $cb->get_content_from_id($id);
            $contenttype = $content->get_content_type_instance();
            if (!$contenttype->can_manage($content) || !$contenttype->can_upload()) {
                throw new \moodle_exception('nopermissions', 'error', '', null, get_string('replacecontent', 'contentbank'));
            }
        }
    }

    /**
     * Returns form context
     *
     * If context depends on the form data, it is available in $this->_ajaxformdata or

View on GitHub (pinned to 48ea8c3322)