/** * Send message to message processors. * * @param \stdClass|\core\message\message $eventdata * @param \stdClass $savemessage * @param array $processorlist * @return int $messageid */ protected static function send_message_to_processors($eventdata, \stdClass $savemessage, array $processorlist) { global $CFG, $DB; // We cannot communicate with external systems in DB transactions, // buffer the messages if necessary. if ($DB->is_transaction_started()) { // We need to clone all objects so that devs may not modify it from outside later. $eventdata = clone $eventdata; $eventdata->userto = clone $eventdata->userto; $eventdata->userfrom = clone $eventdata->userfrom; // Conserve some memory the same was as $USER setup does. unset($eventdata->userto->description); unset($eventdata->userfrom->description); self::$buffer[] = array($eventdata, $savemessage, $processorlist); return $savemessage->id; } $processors = get_message_processors(true); $failed = false; foreach ($processorlist as $procname) { // Let new messaging class add custom content based on the processor. $proceventdata = $eventdata instanceof message ? $eventdata->get_eventobject_for_processor($procname) : $eventdata; if (!$processors[$procname]->object->send_message($proceventdata)) { debugging('Error calling message processor ' . $procname); $failed = true; // Previously the $messageid = false here was overridden // by other processors and message_mark_message_read() below. } } // Trigger event for sending a message - must be done before marking as read. \core\event\message_sent::create_from_ids($eventdata->userfrom->id, $eventdata->userto->id, $savemessage->id)->trigger(); if (empty($CFG->messaging)) { // If messaging is disabled and they previously had forum notifications handled by the popup processor // or any processor that puts a row in message_working then the notification will remain forever // unread. To prevent this mark the message read if messaging is disabled. $messageid = message_mark_message_read($savemessage, time()); } else { if ($failed) { // Something failed, better keep it as unread then. $messageid = $savemessage->id; } else { if ($DB->count_records('message_working', array('unreadmessageid' => $savemessage->id)) == 0) { // If there is no more processors that want to process this we can move message to message_read. $messageid = message_mark_message_read($savemessage, time(), true); } else { // Some processor is still working on the data, let's keep it unread. $messageid = $savemessage->id; } } } return $messageid; }
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']); }