MDL-54744 core_message: deprecated functions in message/lib.php

Also -

1) Made note of the deprecation as well as the functions and
   files that were removed earlier in upgrade.txt.
2) Added new functions to api.php to replace the deprecated ones
   that were being used in unit tests.
3) Removed files that are no longer being used.
This commit is contained in:
Mark Nelson
2016-10-07 16:35:25 +08:00
parent 7b477f5982
commit 5b0769db68
12 changed files with 881 additions and 1068 deletions
+118
View File
@@ -673,4 +673,122 @@ class api {
return $preferences;
}
/**
* Count the number of users blocked by a user.
*
* @param \stdClass $user The user object
* @return int the number of blocked users
*/
public static function count_blocked_users($user = null) {
global $USER, $DB;
if (empty($user)) {
$user = $USER;
}
$sql = "SELECT count(mc.id)
FROM {message_contacts} mc
WHERE mc.userid = :userid AND mc.blocked = 1";
return $DB->count_records_sql($sql, array('userid' => $user->id));
}
/**
* Determines if a user is permitted to send another user a private message.
* If no sender is provided then it defaults to the logged in user.
*
* @param \stdClass $recipient The user object.
* @param \stdClass|null $sender The user object.
* @return bool true if user is permitted, false otherwise.
*/
public static function can_post_message($recipient, $sender = null) {
global $USER;
if (is_null($sender)) {
// The message is from the logged in user, unless otherwise specified.
$sender = $USER;
}
if (!has_capability('moodle/site:sendmessage', \context_system::instance(), $sender)) {
return false;
}
// The recipient blocks messages from non-contacts and the
// sender isn't a contact.
if (self::is_user_non_contact_blocked($recipient, $sender)) {
return false;
}
// The recipient has specifically blocked this sender.
if (self::is_user_blocked($recipient, $sender)) {
return false;
}
return true;
}
/**
* Checks if the recipient is allowing messages from users that aren't a
* contact. If not then it checks to make sure the sender is in the
* recipient's contacts.
*
* @param \stdClass $recipient The user object.
* @param \stdClass|null $sender The user object.
* @return bool true if $sender is blocked, false otherwise.
*/
public static function is_user_non_contact_blocked($recipient, $sender = null) {
global $USER, $DB;
if (is_null($sender)) {
// The message is from the logged in user, unless otherwise specified.
$sender = $USER;
}
$blockednoncontacts = get_user_preferences('message_blocknoncontacts', '', $recipient->id);
if (!empty($blockednoncontacts)) {
// Confirm the sender is a contact of the recipient.
$exists = $DB->record_exists('message_contacts', array('userid' => $recipient->id, 'contactid' => $sender->id));
if ($exists) {
// All good, the recipient is a contact of the sender.
return false;
} else {
// Oh no, the recipient is not a contact. Looks like we can't send the message.
return true;
}
}
return false;
}
/**
* Checks if the recipient has specifically blocked the sending user.
*
* Note: This function will always return false if the sender has the
* readallmessages capability at the system context level.
*
* @param object $recipient User object.
* @param object $sender User object.
* @return bool true if $sender is blocked, false otherwise.
*/
public static function is_user_blocked($recipient, $sender = null) {
global $USER, $DB;
if (is_null($sender)) {
// The message is from the logged in user, unless otherwise specified.
$sender = $USER;
}
$systemcontext = \context_system::instance();
if (has_capability('moodle/site:readallmessages', $systemcontext, $sender)) {
return false;
}
if ($contact = $DB->get_record('message_contacts', array('userid' => $recipient->id, 'contactid' => $sender->id))) {
if ($contact->blocked) {
return true;
}
}
return false;
}
}