Skip to content

Commit

Permalink
MDL-55449 messages: Remove support for stdClass messages.
Browse files Browse the repository at this point in the history
This also makes message_sent::create_from_ids() $courseid
parameter required.
  • Loading branch information
abgreeve committed Aug 7, 2018
1 parent 6e020b1 commit cc4952e
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 56 deletions.
14 changes: 2 additions & 12 deletions lib/classes/event/message_sent.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,20 @@
class message_sent extends base {
/**
* Create event using ids.
* @todo MDL-55449 Make $courseid mandatory in Moodle 3.6
* @param int $userfromid
* @param int $usertoid
* @param int $messageid
* @param int|null $courseid course id the event is related with. Use SITEID if no relation exists.
* @param int $courseid course id the event is related with.
* @return message_sent
*/
public static function create_from_ids($userfromid, $usertoid, $messageid, $courseid = null) {
public static function create_from_ids(int $userfromid, int $usertoid, 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;
}

// TODO: MDL-55449 Make $courseid mandatory in Moodle 3.6.
if (is_null($courseid)) {
// Arrived here with not defined $courseid to associate the event with.
// Let's default to SITEID and perform debugging so devs are aware. MDL-47162.
$courseid = SITEID;
debugging('message_sent::create_from_ids() needs a $courseid to be passed, nothing was detected. Please, change ' .
'the call to include it, using SITEID if the message is unrelated to any real course.', DEBUG_DEVELOPER);
}

$event = self::create(array(
'objectid' => $messageid,
'userid' => $userfromid,
Expand Down
18 changes: 1 addition & 17 deletions lib/classes/message/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,14 @@ class manager {
*
* NOTE: to be used from message_send() only.
*
* @todo MDL-55449 Drop support for stdClass in Moodle 3.6
* @param \core\message\message $eventdata fully prepared event data for processors
* @param \stdClass $savemessage the message saved in 'message' table
* @param array $processorlist list of processors for target user
* @return int $messageid the id from 'messages' (false is not returned)
*/
public static function send_message($eventdata, \stdClass $savemessage, array $processorlist) {
public static function send_message(message $eventdata, \stdClass $savemessage, array $processorlist) {
global $CFG;

// TODO MDL-55449 Drop support for stdClass in Moodle 3.6.
if (!($eventdata instanceof \stdClass) && !($eventdata instanceof message)) {
// Not a valid object.
throw new \coding_exception('Message should be of type stdClass or \core\message\message');
}

// TODO MDL-55449 Drop support for stdClass in Moodle 3.6.
if ($eventdata instanceof \stdClass) {
if (!isset($eventdata->courseid)) {
$eventdata->courseid = null;
}

debugging('eventdata as \stdClass is deprecated. Please use \core\message\message instead.', DEBUG_DEVELOPER);
}

require_once($CFG->dirroot.'/message/lib.php'); // This is most probably already included from messagelib.php file.

if (empty($processorlist)) {
Expand Down
12 changes: 1 addition & 11 deletions lib/messagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,13 @@
* Note: processor failure is is not reported as false return value,
* earlier versions did not do it consistently either.
*
* @todo MDL-55449 Drop support for stdClass in Moodle 3.6
* @category message
* @param \core\message\message $eventdata information about the message (component, userfrom, userto, ...)
* @return mixed the integer ID of the new message or false if there was a problem with submitted data
*/
function message_send($eventdata) {
function message_send(\core\message\message $eventdata) {
global $CFG, $DB;

// TODO MDL-55449 Drop support for stdClass in Moodle 3.6.
if ($eventdata instanceof \stdClass) {
if (!isset($eventdata->courseid)) {
$eventdata->courseid = null;
}

debugging('eventdata as \stdClass is deprecated. Please use core\message\message instead.', DEBUG_DEVELOPER);
}

//new message ID to return
$messageid = false;

Expand Down
4 changes: 4 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ information provided here is intended especially for developers.
policy and respect the privacy setting made by site administrators. The list of user identifiers should never be
hard-coded. Instead, the setting $CFG->showuseridentity should be always respected, which has always been the default
behaviour (MDL-59847).
* The function message_send() in messagelib.php will now only take the object \core\message\message as a parameter.
* The method message_sent::create_from_ids() parameter courseid is now required. A debugging
message was previously displayed, and the SITEID was used, when not provided.
* The method \core\message\manager::send_message() now only takes the object \core\message\message as the first parameter.

=== 3.5 ===

Expand Down
16 changes: 0 additions & 16 deletions message/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,22 +272,6 @@ public function test_mesage_sent_via_create_from_ids() {
$this->assertEquals(4, $event->other['courseid']);
}

public function test_mesage_sent_via_create_from_ids_without_other_courseid() {

// Creating a message_sent event without courseid leads to debugging + SITEID.
// TODO: MDL-55449 Ensure this leads to exception instead of debugging in Moodle 3.6.
$event = \core\event\message_sent::create_from_ids(1, 2, 3);

// Trigger and capturing the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);

$this->assertDebuggingCalled();
$this->assertEquals(SITEID, $event->other['courseid']);
}

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

0 comments on commit cc4952e

Please sign in to comment.