From cbfd685a7e32b6a0c793824c6fd441411b5dcc2b Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Fri, 29 Aug 2014 15:20:55 -0700 Subject: [PATCH] MDL-45818 core_message: use system context if user no longer exists --- message/lib.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/message/lib.php b/message/lib.php index 9408c0733fe..c7b9e7b7ab4 100644 --- a/message/lib.php +++ b/message/lib.php @@ -2375,11 +2375,18 @@ function message_mark_message_read($message, $timeread, $messageworkingempty=fal $DB->delete_records('message', array('id' => $messageid)); + // Get the context for the user who received the message. + $context = context_user::instance($message->useridto, IGNORE_MISSING); + // If the user no longer exists the context value will be false, in this case use the system context. + if ($context === false) { + $context = context_system::instance(); + } + // Trigger event for reading a message. $event = \core\event\message_viewed::create(array( 'objectid' => $messagereadid, 'userid' => $message->useridto, // Using the user who read the message as they are the ones performing the action. - 'context' => context_user::instance($message->useridto), + 'context' => $context, 'relateduserid' => $message->useridfrom, 'other' => array( 'messageid' => $messageid