MDL-40913 core_message: replaced 'unblock contact' add_to_log call with an event

This commit is contained in:
Mark Nelson
2014-03-07 15:15:43 -08:00
parent 3f3d9521de
commit cd60936523
5 changed files with 127 additions and 3 deletions
+1
View File
@@ -57,6 +57,7 @@ $string['errortranslatingdefault'] = 'Error translating default setting provided
$string['eventmessagecontactadded'] = 'Message contact added';
$string['eventmessagecontactblocked'] = 'Message contact blocked';
$string['eventmessagecontactremoved'] = 'Message contact removed';
$string['eventmessagecontactunblocked'] = 'Message contact unblocked';
$string['forced'] = 'Forced';
$string['formorethan'] = 'For more than';
$string['guestnoeditmessage'] = 'Guest user can not edit messaging options';
@@ -0,0 +1,89 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Message contact unblocked event class.
*
* @package core
* @copyright 2014 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;
defined('MOODLE_INTERNAL') || die();
class message_contact_unblocked 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('eventmessagecontactunblocked', '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 . '\' unblocked 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', 'unblock contact', 'index.php?user1=' . $this->relateduserid .
'&amp;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.');
}
}
}
-1
View File
@@ -148,7 +148,6 @@ if ($blockcontact and confirm_sesskey()) {
message_block_contact($blockcontact);
}
if ($unblockcontact and confirm_sesskey()) {
add_to_log(SITEID, 'message', 'unblock contact', 'index.php?user1='.$unblockcontact.'&amp;user2='.$USER->id, $unblockcontact);
message_unblock_contact($unblockcontact);
}
+11 -2
View File
@@ -1020,6 +1020,16 @@ function message_add_contact($contactid, $blocked=0) {
));
$event->add_record_snapshot('message_contacts', $contact);
$event->trigger();
} else {
// Trigger event for unblocking a contact.
$event = \core\event\message_contact_unblocked::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;
@@ -1084,8 +1094,7 @@ function message_remove_contact($contactid) {
* @return bool returns the result of delete_records()
*/
function message_unblock_contact($contactid) {
global $USER, $DB;
return $DB->delete_records('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid));
return message_add_contact($contactid, 0);
}
/**
+26
View File
@@ -112,4 +112,30 @@ class core_message_events_testcase extends advanced_testcase {
$expected = array(SITEID, 'message', 'block contact', 'index.php?user1=' . $user->id . '&amp;user2=2', $user->id);
$this->assertEventLegacyLogData($expected, $event);
}
/**
* Test the message contact unblocked event.
*/
public function test_message_contact_unblocked() {
// 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 unblocking a contact.
$sink = $this->redirectEvents();
message_unblock_contact($user->id);
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\message_contact_unblocked', $event);
$this->assertEquals(context_user::instance(2), $event->get_context());
$expected = array(SITEID, 'message', 'unblock contact', 'index.php?user1=' . $user->id . '&amp;user2=2', $user->id);
$this->assertEventLegacyLogData($expected, $event);
}
}