moodle/moodle · error · coding_exception

The 'relateduserid' must be set.

Error message

The 'relateduserid' must be set.

What it means

Moodle event classes validate their event data in validate_data(); this \coding_exception is thrown by mod/assign's feedback_viewed event when the 'relateduserid' property was not set before the event was triggered. Every feedback_viewed event must record which user the viewed feedback belongs to, since this field is required for event observers, analytics and the events list rendering. It is a developer error (coding_exception), not a runtime condition.

Solutions

  1. Set 'relateduserid' in the data array passed to feedback_viewed::create(), using the id of the user whose feedback is being viewed.
  2. If the viewing user is the graded user, set relateduserid to the grade's userid (grade->userid).
  3. Run the event's unit tests or trigger once in dev with debugging set to DEVELOPER so validate_data() fails early during development.
  4. Check mod/assign locallib (assign->trigger/record feedback_viewed) for the canonical call site and mirror its fields.

Example fix

// before
\mod_assign\event\feedback_viewed::create(['objectid' => $gradeid, 'context' => $context, 'other' => ['assignid' => $assignid]]);
// after
\mod_assign\event\feedback_viewed::create(['objectid' => $gradeid, 'context' => $context, 'relateduserid' => $grade->userid, 'other' => ['assignid' => $assignid]]);
Defensive patterns

Strategy: validation

Validate before calling

$data = ['objectid' => $gradeid, 'context' => $context, 'relateduserid' => $userid, 'other' => ['assignid' => $assignid]];
if (empty($data['relateduserid'])) { throw new \moodle_exception('relateduserid required before triggering feedback_viewed'); }
$event = \mod_assign\event\feedback_viewed::create($data);

Type guard

function isFeedbackViewedDataReady(array $data): bool {
    return isset($data['relateduserid'], $data['other']['assignid'], $data['objectid'], $data['context']);
}

Try / catch

try {
    $event = \mod_assign\event\feedback_viewed::create($data);
} catch (\coding_exception $e) {
    debugging('feedback_viewed event data incomplete: ' . $e->getMessage(), DEBUG_DEVELOPER);
}

Prevention

When it happens

Trigger: Triggering \mod_assign\event\feedback_viewed::create() without passing 'relateduserid' in the event data array, e.g. omitting ['objectid' => $gradeid, 'relateduserid' => $userid, 'other' => ['assignid' => ...]].

Common situations: Copy-pasted trigger code from another assign event that has no relateduserid; refactors that drop event fields; third-party mod_assign plugins raising the event manually with incomplete data.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at public/mod/assign/classes/event/feedback_viewed.php:104

     * Returns description of what happened.
     *
     * @return string
     */
    public function get_description() {
        return "The user with id '$this->userid' viewed the feedback for the user with id '$this->relateduserid' " .
            "for the assignment with course module id '$this->contextinstanceid'.";
    }

    /**
     * Custom validation.
     *
     * @throws \coding_exception
     */
    protected function validate_data() {
        parent::validate_data();

        if (!isset($this->relateduserid)) {
            throw new \coding_exception('The \'relateduserid\' must be set.');
        }

        if (!isset($this->other['assignid'])) {
            throw new \coding_exception('The \'assignid\' value must be set in other.');
        }
    }

    public static function get_objectid_mapping() {
        return array('db' => 'assign_grades', 'restore' => 'grade');
    }

    public static function get_other_mapping() {
        $othermapped = array();
        $othermapped['assignid'] = array('db' => 'assign', 'restore' => 'assign');

        return $othermapped;
    }
}

View on GitHub (pinned to 48ea8c3322)