Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'MDL-63214-master' of git://github.com/sarjona/moodle
  • Loading branch information
David Monllao committed Oct 17, 2018
2 parents a9bbbd7 + f7dfa9b commit 32b01d2
Show file tree
Hide file tree
Showing 24 changed files with 427 additions and 147 deletions.
5 changes: 5 additions & 0 deletions config-dist.php
Expand Up @@ -589,6 +589,11 @@
//
// $CFG->pdfexportfont = 'freesans';
//
// Use the following flag to enable messagingallusers and set the default preference
// value for existing users to allow them to be contacted by other site users.
//
// $CFG->keepmessagingallusersenabled = true;
//
//=========================================================================
// 7. SETTINGS FOR DEVELOPMENT SERVERS - not intended for production use!!!
//=========================================================================
Expand Down
2 changes: 2 additions & 0 deletions lang/en/deprecated.txt
Expand Up @@ -137,3 +137,5 @@ previewhtml,core
messagedselecteduserfailed,core
eventmessagecontactblocked,core_message
eventmessagecontactunblocked,core_message
userisblockingyou,core_message
userisblockingyounoncontact,core_message
9 changes: 7 additions & 2 deletions lang/en/message.php
Expand Up @@ -31,6 +31,10 @@
$string['blockedusers'] = 'Blocked users';
$string['blocknoncontacts'] = 'Prevent non-contacts from messaging me';
$string['canceledit'] = 'Cancel editing messages';
$string['contactableprivacy'] = 'Accept messages from:';
$string['contactableprivacy_onlycontacts'] = 'My contacts only';
$string['contactableprivacy_coursemember'] = 'My contacts and anyone in my courses';
$string['contactableprivacy_site'] = 'Anyone on the site';
$string['contactblocked'] = 'Contact blocked';
$string['contactrequests'] = 'Contact requests';
$string['contacts'] = 'Contacts';
Expand Down Expand Up @@ -181,8 +185,7 @@
$string['unknownuser'] = 'Unknown user';
$string['unreadnotification'] = 'Unread notification: {$a}';
$string['unreadnewmessage'] = 'New message from {$a}';
$string['userisblockingyou'] = 'This user has blocked you from sending messages to them';
$string['userisblockingyounoncontact'] = '{$a} only accepts messages from their contacts.';
$string['usercantbemessaged'] = 'You can\'t message {$a} due to their message preferences. Try adding them as a contact.';
$string['viewfullnotification'] = 'View full notification';
$string['viewinganotherusersmessagearea'] = 'You are viewing another user\'s message area.';
$string['viewmessageswith'] = 'View messages with {$a}';
Expand All @@ -195,3 +198,5 @@
$string['eventmessagecontactblocked'] = 'Message contact blocked';
$string['eventmessagecontactunblocked'] = 'Message contact unblocked';
$string['messagingdisabled'] = 'Messaging is disabled on this site, emails will be sent instead';
$string['userisblockingyou'] = 'This user has blocked you from sending messages to them';
$string['userisblockingyounoncontact'] = '{$a} only accepts messages from their contacts.';
41 changes: 25 additions & 16 deletions lib/db/upgrade.php
Expand Up @@ -2337,22 +2337,6 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2018091200.00);
}

if ($oldversion < 2018091400.01) {
if (!isset($CFG->messagingallusers)) {
// For existing instances, $CFG->messagingallusers would be same value $CFG->messaging has.
if (isset($CFG->messaging)) {
set_config('messagingallusers', $CFG->messaging);
} else {
// When $CFG->messaging is not set, default value for $CFG->messaging should be true,
// so $CFG->messagingallusers value should be true as well.
set_config('messagingallusers', 1);
}
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2018091400.01);
}

if ($oldversion < 2018091700.01) {
// Remove unused setting.
unset_config('messaginghidereadnotifications');
Expand Down Expand Up @@ -2524,5 +2508,30 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2018092800.03);
}

if ($oldversion < 2018101700.01) {
if (empty($CFG->keepmessagingallusersenabled)) {
// When it is not set, $CFG->messagingallusers should be disabled by default.
// When $CFG->messagingallusers = false, the default user preference is MESSAGE_PRIVACY_COURSEMEMBER
// (contacted by users sharing a course).
set_config('messagingallusers', false);
} else {
// When $CFG->keepmessagingallusersenabled is set to true, $CFG->messagingallusers is set to true.
set_config('messagingallusers', true);

// When $CFG->messagingallusers = true, the default user preference is MESSAGE_PRIVACY_SITE
// (contacted by all users site). So we need to set existing values from 0 (MESSAGE_PRIVACY_COURSEMEMBER)
// to 2 (MESSAGE_PRIVACY_SITE).
$DB->set_field(
'user_preferences',
'value',
\core_message\api::MESSAGE_PRIVACY_SITE,
array('name' => 'message_blocknoncontacts', 'value' => 0)
);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2018101700.01);
}

return true;
}
11 changes: 11 additions & 0 deletions lib/upgrade.txt
Expand Up @@ -148,6 +148,17 @@ the groupid field.
'itemid' => 2
];
These files will be then copied to the gradebook file area.
* Allow users to choose who can message them for privacy reasons, with a 'growing circle of contactability':
- Added $CFG->messagingallusers, for enabling messaging to all site users. Default value: 0.
When $CFG->messagingallusers = false users can choose being contacted by only contacts or contacts and users sharing a course with them.
In that case, the default user preference is MESSAGE_PRIVACY_COURSEMEMBER (users sharing a course).
When $CFG->messagingallusers = true users have a new option for the privacy messaging preferences: "Anyone on the site". In that case,
the default user preference is MESSAGE_PRIVACY_SITE (all site users).
- Added $CFG->keepmessagingallusersenabled setting to config.php to force enabling $CFG->messagingallusers during the upgrading process.
Default value: 0.
When $CFG->keepmessagingallusersenabled is set to true, $CFG->messagingallusers will be also set to true to enable messaging site users.
However, when it is empty, $CFG->messagingallusers will be disabled during the upgrading process, so the users will only be able to
message contacts and users sharing a course with them.

=== 3.5 ===

Expand Down
2 changes: 1 addition & 1 deletion message/amd/build/message_preferences.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 22 additions & 22 deletions message/amd/src/message_preferences.js
Expand Up @@ -29,8 +29,7 @@ define(['jquery', 'core/ajax', 'core/notification',
var SELECTORS = {
PREFERENCE: '[data-state]',
PREFERENCES_CONTAINER: '[data-region="preferences-container"]',
BLOCK_NON_CONTACTS: '[data-region="block-non-contacts-container"] [data-block-non-contacts]',
BLOCK_NON_CONTACTS_CONTAINER: '[data-region="block-non-contacts-container"]',
CONTACTABLE_PRIVACY_CONTAINER: '[data-region="privacy-setting-container"]',
};

/**
Expand All @@ -56,16 +55,15 @@ define(['jquery', 'core/ajax', 'core/notification',
};

/**
* Update the block messages from non-contacts user preference in the DOM and
* Update the contactable privacy user preference in the DOM and
* send a request to update on the server.
*
* @return {Promise}
* @method saveBlockNonContactsStatus
* @method saveContactablePrivacySetting
*/
MessagePreferences.prototype.saveBlockNonContactsStatus = function() {
var checkbox = this.root.find(SELECTORS.BLOCK_NON_CONTACTS);
var container = this.root.find(SELECTORS.BLOCK_NON_CONTACTS_CONTAINER);
var ischecked = checkbox.prop('checked');
MessagePreferences.prototype.saveContactablePrivacySetting = function() {
var container = this.root.find(SELECTORS.CONTACTABLE_PRIVACY_CONTAINER);
var value = $("input[type='radio']:checked").val();

if (container.hasClass('loading')) {
return $.Deferred().resolve();
Expand All @@ -79,8 +77,8 @@ define(['jquery', 'core/ajax', 'core/notification',
userid: this.userId,
preferences: [
{
type: checkbox.attr('data-preference-key'),
value: ischecked ? 1 : 0,
type: container.attr('data-preference-key'),
value: value,
}
]
}
Expand All @@ -103,20 +101,22 @@ define(['jquery', 'core/ajax', 'core/notification',
CustomEvents.events.activate
]);

this.root.on(CustomEvents.events.activate, SELECTORS.BLOCK_NON_CONTACTS, function() {
this.saveBlockNonContactsStatus();
}.bind(this));

this.root.on('change', function(e) {
if (!this.preferencesDisabled()) {
var preferencesContainer = $(e.target).closest(SELECTORS.PREFERENCES_CONTAINER);
var preferenceElement = $(e.target).closest(SELECTORS.PREFERENCE);
var messagePreference = new MessageNotificationPreference(preferencesContainer, this.userId);
// Add listener for privacy setting radio buttons change.
if (e.target.name == 'message_blocknoncontacts') {
this.saveContactablePrivacySetting();
} else {
// Add listener for processor preferences.
if (!this.preferencesDisabled()) {
var preferencesContainer = $(e.target).closest(SELECTORS.PREFERENCES_CONTAINER);
var preferenceElement = $(e.target).closest(SELECTORS.PREFERENCE);
var messagePreference = new MessageNotificationPreference(preferencesContainer, this.userId);

preferenceElement.addClass('loading');
messagePreference.save().always(function() {
preferenceElement.removeClass('loading');
});
preferenceElement.addClass('loading');
messagePreference.save().always(function() {
preferenceElement.removeClass('loading');
});
}
}
}.bind(this));
};
Expand Down
77 changes: 65 additions & 12 deletions message/classes/api.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -882,7 +897,7 @@ public static function get_all_message_preferences($processors, $providers, $use
}

// 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);

Expand Down Expand Up @@ -952,6 +967,36 @@ public static function can_post_message($recipient, $sender = null) {
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
Expand All @@ -962,23 +1007,31 @@ public static function can_post_message($recipient, $sender = null) {
* @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;
Expand Down

0 comments on commit 32b01d2

Please sign in to comment.