From ab1bc5d222320758653692d011ae23d6ccc53f53 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 19 Feb 2014 21:25:51 -0800 Subject: [PATCH] MDL-40913 core_message: replaced 'write' add_to_log call with an event --- lang/en/message.php | 1 + lib/classes/event/message_sent.php | 99 ++++++++++++++++++++++++++++++++++++++ lib/messagelib.php | 12 +++++ message/index.php | 1 - message/tests/events_test.php | 29 +++++++++++ 5 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 lib/classes/event/message_sent.php diff --git a/lang/en/message.php b/lang/en/message.php index a97c1b25ef5..6180e03d3e6 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['eventmessagesent'] = 'Message sent'; $string['forced'] = 'Forced'; $string['formorethan'] = 'For more than'; $string['guestnoeditmessage'] = 'Guest user can not edit messaging options'; diff --git a/lib/classes/event/message_sent.php b/lib/classes/event/message_sent.php new file mode 100644 index 00000000000..2ddb1b20d0e --- /dev/null +++ b/lib/classes/event/message_sent.php @@ -0,0 +1,99 @@ +. + +/** + * Message sent 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_sent extends base { + + /** + * Init method. + */ + protected function init() { + $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('eventmessagesent', '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->userid . '\' sent a message to the user with the id \'' . + $this->relateduserid . '\'.'; + } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + return array(SITEID, 'message', 'write', 'index.php?user=' . $this->userid . '&id=' . $this->relateduserid . + '&history=1#m' . $this->other['messageid'], $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/lib/messagelib.php b/lib/messagelib.php index 51c71c4f321..0b4346f55e4 100644 --- a/lib/messagelib.php +++ b/lib/messagelib.php @@ -247,6 +247,18 @@ function message_send($eventdata) { } } + // Trigger event for sending a message. + $event = \core\event\message_sent::create(array( + 'userid' => $eventdata->userfrom->id, + 'context' => context_user::instance($eventdata->userfrom->id), + 'relateduserid' => $eventdata->userto->id, + 'other' => array( + 'messageid' => $messageid // Can't use this as the objectid as it can either be the id in the 'message_read' + // or 'message' table. + ) + )); + $event->trigger(); + return $messageid; } diff --git a/message/index.php b/message/index.php index b2d72d5e455..ff473c1d1ed 100644 --- a/message/index.php +++ b/message/index.php @@ -185,7 +185,6 @@ if ($currentuser && !empty($user2) && has_capability('moodle/site:sendmessage', if (!empty($messageid)) { //including the id of the user sending the message in the logged URL so the URL works for admins //note message ID may be misleading as the message may potentially get a different ID when moved from message to message_read - add_to_log(SITEID, 'message', 'write', 'index.php?user='.$user1->id.'&id='.$user2->id.'&history=1#m'.$messageid, $user1->id); redirect($CFG->wwwroot . '/message/index.php?viewing='.$viewing.'&id='.$user2->id); } } diff --git a/message/tests/events_test.php b/message/tests/events_test.php index a14c0ad0455..c011fbd67be 100644 --- a/message/tests/events_test.php +++ b/message/tests/events_test.php @@ -138,4 +138,33 @@ class core_message_events_testcase extends advanced_testcase { $expected = array(SITEID, 'message', 'unblock contact', 'index.php?user1=' . $user->id . '&user2=2', $user->id); $this->assertEventLegacyLogData($expected, $event); } + + /** + * Test the message sent event. + * + * We can not use the message_send() function in the unit test to check that the event was fired as there is a + * conditional check to ensure a fake message is sent during unit tests when calling that particular function. + */ + public function test_message_sent() { + $event = \core\event\message_sent::create(array( + 'userid' => 1, + 'context' => context_system::instance(), + 'relateduserid' => 2, + 'other' => array( + 'messageid' => 3 + ) + )); + + // Trigger and capturing the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\core\event\message_sent', $event); + $this->assertEquals(context_system::instance(), $event->get_context()); + $expected = array(SITEID, 'message', 'write', 'index.php?user=1&id=2&history=1#m3', 1); + $this->assertEventLegacyLogData($expected, $event); + } } -- 2.11.4.GIT