MDL-36941 core: convert existing api to use new table structure

Also deprecated the following functions -

1. message_move_userfrom_unread2read - It is not necessary
   for us to mark a message as read on user deletion.
2. message_get_blocked_users - Horrible logic used to
   determine if a user is blocked via reference on some
   randomly chosen 'isblocked' variable.
3. message_get_contacts - The same as above. This can be
   done in a much nicer way.
4. message_mark_message_read - We want two functions to do
   this to avoid confusing messages and notifications.
5. message_can_delete_message - This assumed the variable
   $message contained the 'useridto' property, which
   was present in the old table structure. We do not want
   future usages where a query is done on the new table
   and is simply passed as this won't contain this property.
6. message_delete_message - Same as above.
This commit is contained in:
Mark Nelson 2018-01-04 15:01:37 +08:00
parent 4cd439887a
commit 883ce42127
29 changed files with 1714 additions and 1096 deletions

View file

@ -54,7 +54,7 @@ class manager {
* @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 'message' or 'message_read' table (false is not returned)
* @return int $messageid the id from 'messages' (false is not returned)
*/
public static function send_message($eventdata, \stdClass $savemessage, array $processorlist) {
global $CFG;
@ -78,26 +78,26 @@ class manager {
if (empty($processorlist)) {
// Trigger event for sending a message - we need to do this before marking as read!
\core\event\message_sent::create_from_ids(
$eventdata->userfrom->id,
$eventdata->userto->id,
$savemessage->id,
$eventdata->courseid
if (!$eventdata->notification) {
\core\event\message_sent::create_from_ids(
$eventdata->userfrom->id,
$eventdata->userto->id,
$savemessage->id,
$eventdata->courseid
)->trigger();
if ($savemessage->notification or empty($CFG->messaging)) {
// If they have deselected all processors and its a notification mark it read. The user doesn't want to be bothered.
// The same goes if the messaging is completely disabled.
// We cannot insert directly to the message_read table because we want to get all events in proper order!
$messageid = message_mark_message_read($savemessage, time(), true);
} else {
// Just add it to the list of unread messages, there is no way it could be delivered to them,
// but they can read it via the messaging UI later.
$messageid = $savemessage->id;
}
return $messageid;
if ($eventdata->notification or empty($CFG->messaging)) {
// If they have deselected all processors and its a notification mark it read. The user doesn't want to be bothered.
// The same goes if the messaging is completely disabled.
if ($eventdata->notification) {
\core_message\api::mark_notification_as_read($eventdata->userto->id, $savemessage->id);
} else {
\core_message\api::mark_message_as_read($eventdata->userto->id, $savemessage->id);
}
}
return $savemessage->id;
}
// Let the manager do the sending or buffering when db transaction in progress.
@ -133,7 +133,6 @@ class manager {
return $savemessage->id;
}
$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;
@ -142,40 +141,36 @@ class manager {
$processor = \core_message\api::get_processed_processor_object($stdproc);
if (!$processor->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,
$eventdata->courseid
if (!$eventdata->notification) {
\core\event\message_sent::create_from_ids(
$eventdata->userfrom->id,
$eventdata->userto->id,
$savemessage->id,
$eventdata->courseid
)->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;
// 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.
if (empty($CFG->messaging) && $eventdata->notification) {
\core_message\api::mark_notification_as_read($eventdata->userto->id, $savemessage->id);
}
// If there is no more processors that want to process this we can mark the message as read.
if ($DB->count_records('message_working', array('unreadmessageid' => $savemessage->id)) == 0) {
if ($eventdata->notification) {
\core_message\api::mark_notification_as_read($eventdata->userto->id, $savemessage->id);
} else {
\core_message\api::mark_message_as_read($eventdata->userto->id, $savemessage->id);
}
}
return $savemessage->id;
}
/**