diff --git a/lang/en/message.php b/lang/en/message.php index 9971890b57f..79ba22c4585 100644 --- a/lang/en/message.php +++ b/lang/en/message.php @@ -55,6 +55,7 @@ $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['eventmessagecontactblocked'] = 'Message contact blocked'; $string['eventmessagecontactremoved'] = 'Message contact removed'; $string['forced'] = 'Forced'; $string['formorethan'] = 'For more than'; diff --git a/lib/classes/event/message_contact_blocked.php b/lib/classes/event/message_contact_blocked.php new file mode 100644 index 00000000000..7ea6bd43587 --- /dev/null +++ b/lib/classes/event/message_contact_blocked.php @@ -0,0 +1,89 @@ +. + +/** + * Message contact blocked 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_blocked extends base { + + /** + * Init method. + */ + protected function init() { + $this->data['objecttable'] = 'message_contacts'; + $this->data['crud'] = 'u'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventmessagecontactblocked', '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 . '\' blocked the user with the id \'' . + $this->relateduserid . '\' on their contact list.'; + } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + return array(SITEID, 'message', 'block 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 8c361ac2971..450464b0546 100644 --- a/message/index.php +++ b/message/index.php @@ -145,7 +145,6 @@ if ($removecontact and confirm_sesskey()) { message_remove_contact($removecontact); } if ($blockcontact and confirm_sesskey()) { - add_to_log(SITEID, 'message', 'block contact', 'index.php?user1='.$blockcontact.'&user2='.$USER->id, $blockcontact); message_block_contact($blockcontact); } if ($unblockcontact and confirm_sesskey()) { diff --git a/message/lib.php b/message/lib.php index 03ec61471af..cb37ef8a9b4 100644 --- a/message/lib.php +++ b/message/lib.php @@ -1003,13 +1003,26 @@ function message_add_contact($contactid, $blocked=0) { return false; } + // Check if a record already exists as we may be changing blocking status. if (($contact = $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid))) !== false) { - // A record already exists. We may be changing blocking status. - + // Check if blocking status has been changed. if ($contact->blocked !== $blocked) { - // Blocking status has been changed. $contact->blocked = $blocked; - return $DB->update_record('message_contacts', $contact); + $DB->update_record('message_contacts', $contact); + + if ($blocked == 1) { + // Trigger event for blocking a contact. + $event = \core\event\message_contact_blocked::create(array( + 'objectid' => $contact->id, + 'userid' => $contact->userid, + 'relateduserid' => $contact->contactid, + 'context' => context_user::instance($contact->userid) + )); + $event->add_record_snapshot('message_contacts', $contact); + $event->trigger(); + } + + return true; } else { // No change to blocking status. return true; @@ -1076,9 +1089,10 @@ function message_unblock_contact($contactid) { } /** - * block a user + * Block a user. * * @param int $contactid the user ID of the user to block + * @return bool */ function message_block_contact($contactid) { return message_add_contact($contactid, 1); diff --git a/message/tests/events_test.php b/message/tests/events_test.php index 919a133b263..76922a7216d 100644 --- a/message/tests/events_test.php +++ b/message/tests/events_test.php @@ -86,4 +86,30 @@ class core_message_events_testcase extends advanced_testcase { '&user2=2', $user->id); $this->assertEventLegacyLogData($expected, $event); } + + /** + * Test the message contact blocked event. + */ + public function test_message_contact_blocked() { + // Set this user as the admin. + $this->setAdminUser(); + + // Create a user to add to the admin's contact list. + $user = $this->getDataGenerator()->create_user(); + + // Add the user to the admin's contact list. + message_add_contact($user->id); + + // Trigger and capture the event when blocking a contact. + $sink = $this->redirectEvents(); + message_block_contact($user->id); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\core\event\message_contact_blocked', $event); + $this->assertEquals(context_user::instance(2), $event->get_context()); + $expected = array(SITEID, 'message', 'block contact', 'index.php?user1=' . $user->id . '&user2=2', $user->id); + $this->assertEventLegacyLogData($expected, $event); + } }