{"record":{"id":"cf0ed8727c0e396d","repo":"moodle/moodle","slug":"the-relateduserid-must-be-set-feedback-viewed","errorCode":null,"errorMessage":"The 'relateduserid' must be set.","messagePattern":"The 'relateduserid' must be set\\.","errorType":"exception","errorClass":"coding_exception","httpStatus":null,"severity":"error","filePath":"public/mod/assign/classes/event/feedback_viewed.php","lineNumber":104,"sourceCode":"     * Returns description of what happened.\n     *\n     * @return string\n     */\n    public function get_description() {\n        return \"The user with id '$this->userid' viewed the feedback for the user with id '$this->relateduserid' \" .\n            \"for the assignment with course module id '$this->contextinstanceid'.\";\n    }\n\n    /**\n     * Custom validation.\n     *\n     * @throws \\coding_exception\n     */\n    protected function validate_data() {\n        parent::validate_data();\n\n        if (!isset($this->relateduserid)) {\n            throw new \\coding_exception('The \\'relateduserid\\' must be set.');\n        }\n\n        if (!isset($this->other['assignid'])) {\n            throw new \\coding_exception('The \\'assignid\\' value must be set in other.');\n        }\n    }\n\n    public static function get_objectid_mapping() {\n        return array('db' => 'assign_grades', 'restore' => 'grade');\n    }\n\n    public static function get_other_mapping() {\n        $othermapped = array();\n        $othermapped['assignid'] = array('db' => 'assign', 'restore' => 'assign');\n\n        return $othermapped;\n    }\n}","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/mod/assign/classes/event/feedback_viewed.php#L86-L122","documentation":"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.","triggerScenarios":"Triggering \\mod_assign\\event\\feedback_viewed::create() without passing 'relateduserid' in the event data array, e.g. omitting ['objectid' => $gradeid, 'relateduserid' => $userid, 'other' => ['assignid' => ...]].","commonSituations":"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.","solutions":["Set 'relateduserid' in the data array passed to feedback_viewed::create(), using the id of the user whose feedback is being viewed.","If the viewing user is the graded user, set relateduserid to the grade's userid (grade->userid).","Run the event's unit tests or trigger once in dev with debugging set to DEVELOPER so validate_data() fails early during development.","Check mod/assign locallib (assign->trigger/record feedback_viewed) for the canonical call site and mirror its fields."],"exampleFix":"// before\n\\mod_assign\\event\\feedback_viewed::create(['objectid' => $gradeid, 'context' => $context, 'other' => ['assignid' => $assignid]]);\n// after\n\\mod_assign\\event\\feedback_viewed::create(['objectid' => $gradeid, 'context' => $context, 'relateduserid' => $grade->userid, 'other' => ['assignid' => $assignid]]);","handlingStrategy":"validation","validationCode":"$data = ['objectid' => $gradeid, 'context' => $context, 'relateduserid' => $userid, 'other' => ['assignid' => $assignid]];\nif (empty($data['relateduserid'])) { throw new \\moodle_exception('relateduserid required before triggering feedback_viewed'); }\n$event = \\mod_assign\\event\\feedback_viewed::create($data);","typeGuard":"function isFeedbackViewedDataReady(array $data): bool {\n    return isset($data['relateduserid'], $data['other']['assignid'], $data['objectid'], $data['context']);\n}","tryCatchPattern":"try {\n    $event = \\mod_assign\\event\\feedback_viewed::create($data);\n} catch (\\coding_exception $e) {\n    debugging('feedback_viewed event data incomplete: ' . $e->getMessage(), DEBUG_DEVELOPER);\n}","preventionTips":["Always set relateduserid to the user whose feedback is viewed (usually the grade's userid).","Use events unit tests / phpunit event providers to verify required fields.","Copy event payloads from core call sites rather than writing them from scratch."],"tags":["moodle","events","coding-exception","assign"],"backgroundTag":"missing-required-argument","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"}