Skip to content

Commit

Permalink
MDL-63724 core_message: add new group_message_sent event
Browse files Browse the repository at this point in the history
  • Loading branch information
snake committed Nov 12, 2018
1 parent 947d328 commit aa083ab
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 0 deletions.
1 change: 1 addition & 0 deletions lang/en/message.php
Expand Up @@ -52,6 +52,7 @@
$string['enabled'] = 'Enabled';
$string['errorcallingprocessor'] = 'Error calling defined output';
$string['errortranslatingdefault'] = 'Error translating default setting provided by plugin, using system defaults instead.';
$string['eventgroupmessagesent'] = 'Group message sent';
$string['eventnotificationviewed'] = 'Notification viewed';
$string['eventnotificationsent'] = 'Notification sent';
$string['eventmessagecontactadded'] = 'Message contact added';
Expand Down
157 changes: 157 additions & 0 deletions lib/classes/event/group_message_sent.php
@@ -0,0 +1,157 @@
<?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/>.

/**
* Group message sent event.
*
* @package core
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core\event;

defined('MOODLE_INTERNAL') || die();

/**
* Group message sent event class.
*
* @property-read array $other {
* Extra information about event.
*
* - int courseid: the id of the related course.
* - int conversationid: the id of the conversation in which the message was sent.
* }
*
* @package core
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class group_message_sent extends base {
/**
* Create event using ids.
* @param int $userfromid
* @param int $conversationid
* @param int $messageid
* @param int $courseid course id the event is related with.
* @return message_sent
*/
public static function create_from_ids(int $userfromid, int $conversationid, int $messageid, int $courseid) {
// We may be sending a message from the 'noreply' address, which means we are not actually sending a
// message from a valid user. In this case, we will set the userid to 0.
// Check if the userid is valid.
if (!\core_user::is_real_user($userfromid)) {
$userfromid = 0;
}

$event = self::create([
'objectid' => $messageid,
'userid' => $userfromid,
'context' => \context_system::instance(),
'other' => [
'courseid' => $courseid,
'conversationid' => $conversationid
]
]);

return $event;
}

/**
* Init method.
*/
protected function init() {
$this->data['objecttable'] = 'messages';
$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('eventgroupmessagesent', 'message');
}

/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
// There currently isn't a way to link back from a 'group message sent' event to a conversation.
// So, just return the user to the index page.
return new \moodle_url('/message/index.php');
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
$conversationid = $this->other['conversationid'];

// Check if we are sending from a valid user.
if (\core_user::is_real_user($this->userid)) {

return "The user with id '$this->userid' sent a message with id '$this->objectid' to the conversation " .
"with id '$conversationid'.";
}

return "A message with id '$this->objectid' was sent by the system to the conversation with id '$conversationid'.";
}

/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();

if (!isset($this->other['courseid'])) {
throw new \coding_exception('The \'courseid\' value must be set in other.');
}
if (!isset($this->other['conversationid'])) {
throw new \coding_exception('The \'conversationid\' value must be set in other.');
}
}

/**
* Get the object this event maps to.
*
* @return array|string object id mapping.
*/
public static function get_objectid_mapping() {
return ['db' => 'messages', 'restore' => base::NOT_MAPPED];
}

/**
* Get the item mappings for the 'other' fields for this event.
*
* @return array the array of other fields, mapped.
*/
public static function get_other_mapping() {
$othermapped = [];
$othermapped['courseid'] = ['db' => 'course', 'restore' => base::NOT_MAPPED];
$othermapped['conversationid'] = ['db' => 'message_conversations', 'restore' => base::NOT_MAPPED];
return $othermapped;
}
}
95 changes: 95 additions & 0 deletions message/tests/events_test.php
Expand Up @@ -238,6 +238,101 @@ public function test_mesage_sent_via_create_from_ids() {
$this->assertEquals(4, $event->other['courseid']);
}

/**
* Test the group message sent event.
*
* We can't test events in any testing of the message_send() function as there is a conditional PHPUNIT check in message_send,
* resulting in fake messages being generated and captured under test. As a result, none of the events code, nor message
* processor code is called during testing.
*/
public function test_group_message_sent() {
$event = \core\event\group_message_sent::create([
'objectid' => 3,
'userid' => 1,
'context' => context_system::instance(),
'other' => [
'courseid' => 4,
'conversationid' => 54
]
]);

// Trigger and capture 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\group_message_sent', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
$url = new moodle_url('/message/index.php');
$this->assertEquals($url, $event->get_url());
$this->assertEquals(3, $event->objectid);
$this->assertEquals(4, $event->other['courseid']);
$this->assertEquals(54, $event->other['conversationid']);
}

/**
* Test the group message sent event when created without a courseid.
*/
public function test_group_message_sent_without_other_courseid() {
// Creating a message_sent event without other[courseid] leads to exception.
$this->expectException('coding_exception');
$this->expectExceptionMessage('The \'courseid\' value must be set in other');

$event = \core\event\group_message_sent::create([
'userid' => 1,
'objectid' => 3,
'context' => context_system::instance(),
'relateduserid' => 2,
'other' => [
'conversationid' => 34
]
]);
}

/**
* Test the group message sent event when created without a conversationid.
*/
public function test_group_message_sent_without_other_conversationid() {
// Creating a message_sent event without other[courseid] leads to exception.
$this->expectException('coding_exception');
$this->expectExceptionMessage('The \'conversationid\' value must be set in other');

$event = \core\event\group_message_sent::create([
'userid' => 1,
'objectid' => 3,
'context' => context_system::instance(),
'relateduserid' => 2,
'other' => [
'courseid' => 44,
]
]);
}

/**
* Test the group message sent event using the create_from_ids() method.
*/
public function test_group_message_sent_via_create_from_ids() {
// Fields are: userfromid, conversationid, messageid, courseid.
$event = \core\event\group_message_sent::create_from_ids(1, 2, 3, 4);

// Trigger and capture 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\group_message_sent', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
$this->assertEquals(new moodle_url('/message/index.php'), $event->get_url());
$this->assertEquals(1, $event->userid);
$this->assertEquals(2, $event->other['conversationid']);
$this->assertEquals(3, $event->objectid);
$this->assertEquals(4, $event->other['courseid']);
}

/**
* Test the message viewed event.
*/
Expand Down

0 comments on commit aa083ab

Please sign in to comment.