diff --git a/lang/en/message.php b/lang/en/message.php index 6180e03d3e6..9ebe157085a 100644 --- a/lang/en/message.php +++ b/lang/en/message.php @@ -58,6 +58,7 @@ $string['eventmessagecontactadded'] = 'Message contact added'; $string['eventmessagecontactblocked'] = 'Message contact blocked'; $string['eventmessagecontactremoved'] = 'Message contact removed'; $string['eventmessagecontactunblocked'] = 'Message contact unblocked'; +$string['eventmessageread'] = 'Message read'; $string['eventmessagesent'] = 'Message sent'; $string['forced'] = 'Forced'; $string['formorethan'] = 'For more than'; diff --git a/lib/classes/event/message_read.php b/lib/classes/event/message_read.php new file mode 100644 index 00000000000..6ba652e513f --- /dev/null +++ b/lib/classes/event/message_read.php @@ -0,0 +1,90 @@ +. + +/** + * Message read event class. + * + * @property-read array $other { + * Extra information about event. + * + * - int messageid: the id of the message. + * } + * + * @package core + * @copyright 2014 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +class message_read extends base { + + /** + * Init method. + */ + protected function init() { + $this->data['objecttable'] = 'message_read'; + $this->data['crud'] = 'c'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventmessageread', 'message'); + } + + /** + * Returns relevant URL. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('message/index.php', array('user1' => $this->relateduserid, 'user2' => $this->userid)); + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return 'The user with the id \'' . $this->relateduserid . '\' read a message from the user with the id \'' . + $this->userid . '\'.'; + } + + /** + * Custom validation. + * + * @throws \coding_exception + * @return void + */ + protected function validate_data() { + parent::validate_data(); + + if (!isset($this->relateduserid)) { + throw new \coding_exception('The \'relateduserid\' must be set.'); + } + + if (!isset($this->other['messageid'])) { + throw new \coding_exception('The \'messageid\' needs to be set in $other.'); + } + } +} diff --git a/message/lib.php b/message/lib.php index 4521a91aacf..e80819d2777 100644 --- a/message/lib.php +++ b/message/lib.php @@ -2376,7 +2376,21 @@ function message_mark_message_read($message, $timeread, $messageworkingempty=fal $DB->delete_records('message_working', array('unreadmessageid' => $messageid)); } $messagereadid = $DB->insert_record('message_read', $message); + $DB->delete_records('message', array('id' => $messageid)); + + // Trigger event for reading a message. + $event = \core\event\message_read::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), + 'relateduserid' => $message->useridfrom, + 'other' => array( + 'messageid' => $messageid + ) + )); + $event->trigger(); + return $messagereadid; } diff --git a/message/tests/events_test.php b/message/tests/events_test.php index c011fbd67be..a4d2bcb54f7 100644 --- a/message/tests/events_test.php +++ b/message/tests/events_test.php @@ -167,4 +167,29 @@ class core_message_events_testcase extends advanced_testcase { $expected = array(SITEID, 'message', 'write', 'index.php?user=1&id=2&history=1#m3', 1); $this->assertEventLegacyLogData($expected, $event); } + + /** + * Test the message read event. + */ + public function test_message_read() { + global $DB; + + // Create a message to mark as read. + $message = new stdClass(); + $message->useridfrom = '1'; + $message->useridto = '2'; + $message->subject = 'Subject'; + $message->message = 'Message'; + $message->id = $DB->insert_record('message', $message); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + message_mark_message_read($message, time()); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\core\event\message_read', $event); + $this->assertEquals(context_user::instance(2), $event->get_context()); + } }