diff --git a/lang/en/message.php b/lang/en/message.php index 368953137d5..478fa1c78ce 100644 --- a/lang/en/message.php +++ b/lang/en/message.php @@ -54,6 +54,7 @@ $string['emptysearchstring'] = 'You must search for something'; $string['enabled'] = 'Enabled'; $string['errorcallingprocessor'] = 'Error calling defined output'; $string['errortranslatingdefault'] = 'Error translating default setting provided by plugin, using system defaults instead.'; +$string['eventmessagecontactadded'] = 'Message contact added'; $string['forced'] = 'Forced'; $string['formorethan'] = 'For more than'; $string['guestnoeditmessage'] = 'Guest user can not edit messaging options'; diff --git a/lib/classes/event/message_contact_added.php b/lib/classes/event/message_contact_added.php new file mode 100644 index 00000000000..7281048f236 --- /dev/null +++ b/lib/classes/event/message_contact_added.php @@ -0,0 +1,89 @@ +. + +/** + * Message contact added event class. + * + * @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_contact_added extends base { + + /** + * Init method. + */ + protected function init() { + $this->data['objecttable'] = 'message_contacts'; + $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('eventmessagecontactadded', '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 . '\' added the user with the id \'' . + $this->relateduserid . '\' to their contact list.'; + } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + return array(SITEID, 'message', 'add contact', 'index.php?user1=' . $this->relateduserid . + '&user2=' . $this->userid, $this->relateduserid); + } + + /** + * 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.'); + } + } +} diff --git a/message/index.php b/message/index.php index c04e6d51fad..60588413951 100644 --- a/message/index.php +++ b/message/index.php @@ -138,7 +138,6 @@ if (!empty($user2->id) && $user2realuser && ($user2->id != $USER->id)) { /// Process any contact maintenance requests there may be if ($addcontact and confirm_sesskey()) { - add_to_log(SITEID, 'message', 'add contact', 'index.php?user1='.$addcontact.'&user2='.$USER->id, $addcontact); message_add_contact($addcontact); redirect($CFG->wwwroot . '/message/index.php?viewing=contacts&id='.$addcontact); } diff --git a/message/lib.php b/message/lib.php index 4983b08e148..1ac823b9e6a 100644 --- a/message/lib.php +++ b/message/lib.php @@ -1021,7 +1021,18 @@ function message_add_contact($contactid, $blocked=0) { $contact->userid = $USER->id; $contact->contactid = $contactid; $contact->blocked = $blocked; - return $DB->insert_record('message_contacts', $contact, false); + $contact->id = $DB->insert_record('message_contacts', $contact); + + // Trigger event for adding a contact. + $event = \core\event\message_contact_added::create(array( + 'objectid' => $contact->id, + 'userid' => $contact->userid, + 'relateduserid' => $contact->contactid, + 'context' => context_user::instance($contact->userid) + )); + $event->trigger(); + + return true; } } diff --git a/message/tests/events_test.php b/message/tests/events_test.php new file mode 100644 index 00000000000..173200966c4 --- /dev/null +++ b/message/tests/events_test.php @@ -0,0 +1,62 @@ +. + +/** + * Events tests. + * + * @package core_message + * @category test + * @copyright 2014 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +class core_message_events_testcase extends advanced_testcase { + + /** + * Test set up. + * + * This is executed before running any test in this file. + */ + public function setUp() { + $this->resetAfterTest(); + } + + /** + * Test the message contact added event. + */ + public function test_message_contact_added() { + // Set this user as the admin. + $this->setAdminUser(); + + // Create a user to add to the admin's contact list. + $user = $this->getDataGenerator()->create_user(); + + // Trigger and capture the event when adding a contact. + $sink = $this->redirectEvents(); + message_add_contact($user->id); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\core\event\message_contact_added', $event); + $this->assertEquals(context_user::instance(2), $event->get_context()); + $expected = array(SITEID, 'message', 'add contact', 'index.php?user1=' . $user->id . + '&user2=2', $user->id); + $this->assertEventLegacyLogData($expected, $event); + } +}