MDL-63211 core_message: deprecated functions and added new API

This commit is contained in:
Mark Nelson 2018-08-30 12:08:29 +08:00
parent bacebf3738
commit 0b3eadcd96
6 changed files with 674 additions and 155 deletions

View file

@ -151,156 +151,6 @@ function message_format_message_text($message, $forcetexttohtml = false) {
return format_text($messagetext, $format, $options);
}
/**
* Add the selected user as a contact for the current user
*
* @param int $contactid the ID of the user to add as a contact
* @param int $blocked 1 if you wish to block the contact
* @param int $userid the user ID of the user we want to add the contact for, defaults to current user if not specified.
* @return bool/int false if the $contactid isnt a valid user id. True if no changes made.
* Otherwise returns the result of update_record() or insert_record()
*/
function message_add_contact($contactid, $blocked = 0, $userid = 0) {
global $USER, $DB;
if (!$DB->record_exists('user', array('id' => $contactid))) { // invalid userid
return false;
}
if (empty($userid)) {
$userid = $USER->id;
}
// Check if a record already exists as we may be changing blocking status.
if (($contact = $DB->get_record('message_contacts', array('userid' => $userid, 'contactid' => $contactid))) !== false) {
// Check if blocking status has been changed.
if ($contact->blocked != $blocked) {
$contact->blocked = $blocked;
$DB->update_record('message_contacts', $contact);
if ($blocked == 1) {
// Trigger event for blocking a contact.
$event = \core\event\message_contact_blocked::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();
} 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;
} else {
// No change to blocking status.
return true;
}
} else {
// New contact record.
$contact = new stdClass();
$contact->userid = $userid;
$contact->contactid = $contactid;
$contact->blocked = $blocked;
$contact->id = $DB->insert_record('message_contacts', $contact);
$eventparams = array(
'objectid' => $contact->id,
'userid' => $contact->userid,
'relateduserid' => $contact->contactid,
'context' => context_user::instance($contact->userid)
);
if ($blocked) {
$event = \core\event\message_contact_blocked::create($eventparams);
} else {
$event = \core\event\message_contact_added::create($eventparams);
}
// Trigger event.
$event->trigger();
return true;
}
}
/**
* remove a contact
*
* @param int $contactid the user ID of the contact to remove
* @param int $userid the user ID of the user we want to remove the contacts for, defaults to current user if not specified.
* @return bool returns the result of delete_records()
*/
function message_remove_contact($contactid, $userid = 0) {
global $USER, $DB;
if (empty($userid)) {
$userid = $USER->id;
}
if ($contact = $DB->get_record('message_contacts', array('userid' => $userid, 'contactid' => $contactid))) {
$DB->delete_records('message_contacts', array('id' => $contact->id));
// Trigger event for removing a contact.
$event = \core\event\message_contact_removed::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;
}
return false;
}
/**
* Unblock a contact. Note that this reverts the previously blocked user back to a non-contact.
*
* @param int $contactid the user ID of the contact to unblock
* @param int $userid the user ID of the user we want to unblock the contact for, defaults to current user
* if not specified.
* @return bool returns the result of delete_records()
*/
function message_unblock_contact($contactid, $userid = 0) {
return message_add_contact($contactid, 0, $userid);
}
/**
* Block a user.
*
* @param int $contactid the user ID of the user to block
* @param int $userid the user ID of the user we want to unblock the contact for, defaults to current user
* if not specified.
* @return bool
*/
function message_block_contact($contactid, $userid = 0) {
return message_add_contact($contactid, 1, $userid);
}
/**
* Load a user's contact record
*
* @param int $contactid the user ID of the user whose contact record you want
* @return array message contacts
*/
function message_get_contact($contactid) {
global $USER, $DB;
return $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid));
}
/**
* Search through course users.
*