Merge branch 'MDL-63214-master' of git://github.com/sarjona/moodle

This commit is contained in:
David Monllao
2018-10-17 15:55:44 +02:00
24 changed files with 427 additions and 147 deletions
+65 -12
View File
@@ -46,6 +46,21 @@ class api {
*/
const MESSAGE_ACTION_DELETED = 2;
/**
* The privacy setting for being messaged by anyone within courses user is member of.
*/
const MESSAGE_PRIVACY_COURSEMEMBER = 0;
/**
* The privacy setting for being messaged only by contacts.
*/
const MESSAGE_PRIVACY_ONLYCONTACTS = 1;
/**
* The privacy setting for being messaged by anyone on the site.
*/
const MESSAGE_PRIVACY_SITE = 2;
/**
* Handles searching for messages in the message area.
*
@@ -882,7 +897,7 @@ class api {
}
// Load general messaging preferences.
$preferences->blocknoncontacts = get_user_preferences('message_blocknoncontacts', '', $user->id);
$preferences->blocknoncontacts = self::get_user_privacy_messaging_preference($user->id);
$preferences->mailformat = $user->mailformat;
$preferences->mailcharset = get_user_preferences('mailcharset', '', $user->id);
@@ -952,6 +967,36 @@ class api {
return true;
}
/**
* Get the messaging preference for a user.
* If the user has not any messaging privacy preference:
* - When $CFG->messagingallusers = false the default user preference is MESSAGE_PRIVACY_COURSEMEMBER.
* - When $CFG->messagingallusers = true the default user preference is MESSAGE_PRIVACY_SITE.
*
* @param int $userid The user identifier.
* @return int The default messaging preference.
*/
public static function get_user_privacy_messaging_preference(int $userid) : int {
global $CFG;
// When $CFG->messagingallusers is enabled, default value for the messaging preference will be "Anyone on the site";
// otherwise, the default value will be "My contacts and anyone in my courses".
if (empty($CFG->messagingallusers)) {
$defaultprefvalue = self::MESSAGE_PRIVACY_COURSEMEMBER;
} else {
$defaultprefvalue = self::MESSAGE_PRIVACY_SITE;
}
$privacypreference = get_user_preferences('message_blocknoncontacts', $defaultprefvalue, $userid);
// When the $CFG->messagingallusers privacy setting is disabled, MESSAGE_PRIVACY_SITE is
// also disabled, so it has to be replaced to MESSAGE_PRIVACY_COURSEMEMBER.
if (empty($CFG->messagingallusers) && $privacypreference == self::MESSAGE_PRIVACY_SITE) {
$privacypreference = self::MESSAGE_PRIVACY_COURSEMEMBER;
}
return $privacypreference;
}
/**
* 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
@@ -962,23 +1007,31 @@ class api {
* @return bool true if $sender is blocked, false otherwise.
*/
public static function is_user_non_contact_blocked($recipient, $sender = null) {
global $USER;
global $USER, $CFG;
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.
if (self::is_contact($sender->id, $recipient->id)) {
// 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;
}
$privacypreference = self::get_user_privacy_messaging_preference($recipient->id);
switch ($privacypreference) {
case self::MESSAGE_PRIVACY_SITE:
if (!empty($CFG->messagingallusers)) {
// Users can be messaged without being contacts or members of the same course.
break;
}
// When the $CFG->messagingallusers privacy setting is disabled, continue with the next
// case, because MESSAGE_PRIVACY_SITE is replaced to MESSAGE_PRIVACY_COURSEMEMBER.
case self::MESSAGE_PRIVACY_COURSEMEMBER:
// Confirm the sender and the recipient are both members of the same course.
if (enrol_sharing_course($recipient, $sender)) {
// All good, the recipient and the sender are members of the same course.
return false;
}
case self::MESSAGE_PRIVACY_ONLYCONTACTS:
// True if they aren't contacts (they can't send a message because of the privacy settings), false otherwise.
return !self::is_contact($sender->id, $recipient->id);
}
return false;