Skip to content

Commit 32b01d2

Browse files
author
David Monllao
committed
Merge branch 'MDL-63214-master' of git://github.com/sarjona/moodle
2 parents a9bbbd7 + f7dfa9b commit 32b01d2

24 files changed

+427
-147
lines changed

config-dist.php

+5
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,11 @@
589589
//
590590
// $CFG->pdfexportfont = 'freesans';
591591
//
592+
// Use the following flag to enable messagingallusers and set the default preference
593+
// value for existing users to allow them to be contacted by other site users.
594+
//
595+
// $CFG->keepmessagingallusersenabled = true;
596+
//
592597
//=========================================================================
593598
// 7. SETTINGS FOR DEVELOPMENT SERVERS - not intended for production use!!!
594599
//=========================================================================

lang/en/deprecated.txt

+2
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,5 @@ previewhtml,core
137137
messagedselecteduserfailed,core
138138
eventmessagecontactblocked,core_message
139139
eventmessagecontactunblocked,core_message
140+
userisblockingyou,core_message
141+
userisblockingyounoncontact,core_message

lang/en/message.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
$string['blockedusers'] = 'Blocked users';
3232
$string['blocknoncontacts'] = 'Prevent non-contacts from messaging me';
3333
$string['canceledit'] = 'Cancel editing messages';
34+
$string['contactableprivacy'] = 'Accept messages from:';
35+
$string['contactableprivacy_onlycontacts'] = 'My contacts only';
36+
$string['contactableprivacy_coursemember'] = 'My contacts and anyone in my courses';
37+
$string['contactableprivacy_site'] = 'Anyone on the site';
3438
$string['contactblocked'] = 'Contact blocked';
3539
$string['contactrequests'] = 'Contact requests';
3640
$string['contacts'] = 'Contacts';
@@ -181,8 +185,7 @@
181185
$string['unknownuser'] = 'Unknown user';
182186
$string['unreadnotification'] = 'Unread notification: {$a}';
183187
$string['unreadnewmessage'] = 'New message from {$a}';
184-
$string['userisblockingyou'] = 'This user has blocked you from sending messages to them';
185-
$string['userisblockingyounoncontact'] = '{$a} only accepts messages from their contacts.';
188+
$string['usercantbemessaged'] = 'You can\'t message {$a} due to their message preferences. Try adding them as a contact.';
186189
$string['viewfullnotification'] = 'View full notification';
187190
$string['viewinganotherusersmessagearea'] = 'You are viewing another user\'s message area.';
188191
$string['viewmessageswith'] = 'View messages with {$a}';
@@ -195,3 +198,5 @@
195198
$string['eventmessagecontactblocked'] = 'Message contact blocked';
196199
$string['eventmessagecontactunblocked'] = 'Message contact unblocked';
197200
$string['messagingdisabled'] = 'Messaging is disabled on this site, emails will be sent instead';
201+
$string['userisblockingyou'] = 'This user has blocked you from sending messages to them';
202+
$string['userisblockingyounoncontact'] = '{$a} only accepts messages from their contacts.';

lib/db/upgrade.php

+25-16
Original file line numberDiff line numberDiff line change
@@ -2337,22 +2337,6 @@ function xmldb_main_upgrade($oldversion) {
23372337
upgrade_main_savepoint(true, 2018091200.00);
23382338
}
23392339

2340-
if ($oldversion < 2018091400.01) {
2341-
if (!isset($CFG->messagingallusers)) {
2342-
// For existing instances, $CFG->messagingallusers would be same value $CFG->messaging has.
2343-
if (isset($CFG->messaging)) {
2344-
set_config('messagingallusers', $CFG->messaging);
2345-
} else {
2346-
// When $CFG->messaging is not set, default value for $CFG->messaging should be true,
2347-
// so $CFG->messagingallusers value should be true as well.
2348-
set_config('messagingallusers', 1);
2349-
}
2350-
}
2351-
2352-
// Main savepoint reached.
2353-
upgrade_main_savepoint(true, 2018091400.01);
2354-
}
2355-
23562340
if ($oldversion < 2018091700.01) {
23572341
// Remove unused setting.
23582342
unset_config('messaginghidereadnotifications');
@@ -2524,5 +2508,30 @@ function xmldb_main_upgrade($oldversion) {
25242508
upgrade_main_savepoint(true, 2018092800.03);
25252509
}
25262510

2511+
if ($oldversion < 2018101700.01) {
2512+
if (empty($CFG->keepmessagingallusersenabled)) {
2513+
// When it is not set, $CFG->messagingallusers should be disabled by default.
2514+
// When $CFG->messagingallusers = false, the default user preference is MESSAGE_PRIVACY_COURSEMEMBER
2515+
// (contacted by users sharing a course).
2516+
set_config('messagingallusers', false);
2517+
} else {
2518+
// When $CFG->keepmessagingallusersenabled is set to true, $CFG->messagingallusers is set to true.
2519+
set_config('messagingallusers', true);
2520+
2521+
// When $CFG->messagingallusers = true, the default user preference is MESSAGE_PRIVACY_SITE
2522+
// (contacted by all users site). So we need to set existing values from 0 (MESSAGE_PRIVACY_COURSEMEMBER)
2523+
// to 2 (MESSAGE_PRIVACY_SITE).
2524+
$DB->set_field(
2525+
'user_preferences',
2526+
'value',
2527+
\core_message\api::MESSAGE_PRIVACY_SITE,
2528+
array('name' => 'message_blocknoncontacts', 'value' => 0)
2529+
);
2530+
}
2531+
2532+
// Main savepoint reached.
2533+
upgrade_main_savepoint(true, 2018101700.01);
2534+
}
2535+
25272536
return true;
25282537
}

lib/upgrade.txt

+11
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ the groupid field.
148148
'itemid' => 2
149149
];
150150
These files will be then copied to the gradebook file area.
151+
* Allow users to choose who can message them for privacy reasons, with a 'growing circle of contactability':
152+
- Added $CFG->messagingallusers, for enabling messaging to all site users. Default value: 0.
153+
When $CFG->messagingallusers = false users can choose being contacted by only contacts or contacts and users sharing a course with them.
154+
In that case, the default user preference is MESSAGE_PRIVACY_COURSEMEMBER (users sharing a course).
155+
When $CFG->messagingallusers = true users have a new option for the privacy messaging preferences: "Anyone on the site". In that case,
156+
the default user preference is MESSAGE_PRIVACY_SITE (all site users).
157+
- Added $CFG->keepmessagingallusersenabled setting to config.php to force enabling $CFG->messagingallusers during the upgrading process.
158+
Default value: 0.
159+
When $CFG->keepmessagingallusersenabled is set to true, $CFG->messagingallusers will be also set to true to enable messaging site users.
160+
However, when it is empty, $CFG->messagingallusers will be disabled during the upgrading process, so the users will only be able to
161+
message contacts and users sharing a course with them.
151162

152163
=== 3.5 ===
153164

message/amd/build/message_preferences.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

message/amd/src/message_preferences.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ define(['jquery', 'core/ajax', 'core/notification',
2929
var SELECTORS = {
3030
PREFERENCE: '[data-state]',
3131
PREFERENCES_CONTAINER: '[data-region="preferences-container"]',
32-
BLOCK_NON_CONTACTS: '[data-region="block-non-contacts-container"] [data-block-non-contacts]',
33-
BLOCK_NON_CONTACTS_CONTAINER: '[data-region="block-non-contacts-container"]',
32+
CONTACTABLE_PRIVACY_CONTAINER: '[data-region="privacy-setting-container"]',
3433
};
3534

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

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

7068
if (container.hasClass('loading')) {
7169
return $.Deferred().resolve();
@@ -79,8 +77,8 @@ define(['jquery', 'core/ajax', 'core/notification',
7977
userid: this.userId,
8078
preferences: [
8179
{
82-
type: checkbox.attr('data-preference-key'),
83-
value: ischecked ? 1 : 0,
80+
type: container.attr('data-preference-key'),
81+
value: value,
8482
}
8583
]
8684
}
@@ -103,20 +101,22 @@ define(['jquery', 'core/ajax', 'core/notification',
103101
CustomEvents.events.activate
104102
]);
105103

106-
this.root.on(CustomEvents.events.activate, SELECTORS.BLOCK_NON_CONTACTS, function() {
107-
this.saveBlockNonContactsStatus();
108-
}.bind(this));
109-
110104
this.root.on('change', function(e) {
111-
if (!this.preferencesDisabled()) {
112-
var preferencesContainer = $(e.target).closest(SELECTORS.PREFERENCES_CONTAINER);
113-
var preferenceElement = $(e.target).closest(SELECTORS.PREFERENCE);
114-
var messagePreference = new MessageNotificationPreference(preferencesContainer, this.userId);
105+
// Add listener for privacy setting radio buttons change.
106+
if (e.target.name == 'message_blocknoncontacts') {
107+
this.saveContactablePrivacySetting();
108+
} else {
109+
// Add listener for processor preferences.
110+
if (!this.preferencesDisabled()) {
111+
var preferencesContainer = $(e.target).closest(SELECTORS.PREFERENCES_CONTAINER);
112+
var preferenceElement = $(e.target).closest(SELECTORS.PREFERENCE);
113+
var messagePreference = new MessageNotificationPreference(preferencesContainer, this.userId);
115114

116-
preferenceElement.addClass('loading');
117-
messagePreference.save().always(function() {
118-
preferenceElement.removeClass('loading');
119-
});
115+
preferenceElement.addClass('loading');
116+
messagePreference.save().always(function() {
117+
preferenceElement.removeClass('loading');
118+
});
119+
}
120120
}
121121
}.bind(this));
122122
};

message/classes/api.php

+65-12
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ class api {
4646
*/
4747
const MESSAGE_ACTION_DELETED = 2;
4848

49+
/**
50+
* The privacy setting for being messaged by anyone within courses user is member of.
51+
*/
52+
const MESSAGE_PRIVACY_COURSEMEMBER = 0;
53+
54+
/**
55+
* The privacy setting for being messaged only by contacts.
56+
*/
57+
const MESSAGE_PRIVACY_ONLYCONTACTS = 1;
58+
59+
/**
60+
* The privacy setting for being messaged by anyone on the site.
61+
*/
62+
const MESSAGE_PRIVACY_SITE = 2;
63+
4964
/**
5065
* Handles searching for messages in the message area.
5166
*
@@ -882,7 +897,7 @@ public static function get_all_message_preferences($processors, $providers, $use
882897
}
883898

884899
// Load general messaging preferences.
885-
$preferences->blocknoncontacts = get_user_preferences('message_blocknoncontacts', '', $user->id);
900+
$preferences->blocknoncontacts = self::get_user_privacy_messaging_preference($user->id);
886901
$preferences->mailformat = $user->mailformat;
887902
$preferences->mailcharset = get_user_preferences('mailcharset', '', $user->id);
888903

@@ -952,6 +967,36 @@ public static function can_post_message($recipient, $sender = null) {
952967
return true;
953968
}
954969

970+
/**
971+
* Get the messaging preference for a user.
972+
* If the user has not any messaging privacy preference:
973+
* - When $CFG->messagingallusers = false the default user preference is MESSAGE_PRIVACY_COURSEMEMBER.
974+
* - When $CFG->messagingallusers = true the default user preference is MESSAGE_PRIVACY_SITE.
975+
*
976+
* @param int $userid The user identifier.
977+
* @return int The default messaging preference.
978+
*/
979+
public static function get_user_privacy_messaging_preference(int $userid) : int {
980+
global $CFG;
981+
982+
// When $CFG->messagingallusers is enabled, default value for the messaging preference will be "Anyone on the site";
983+
// otherwise, the default value will be "My contacts and anyone in my courses".
984+
if (empty($CFG->messagingallusers)) {
985+
$defaultprefvalue = self::MESSAGE_PRIVACY_COURSEMEMBER;
986+
} else {
987+
$defaultprefvalue = self::MESSAGE_PRIVACY_SITE;
988+
}
989+
$privacypreference = get_user_preferences('message_blocknoncontacts', $defaultprefvalue, $userid);
990+
991+
// When the $CFG->messagingallusers privacy setting is disabled, MESSAGE_PRIVACY_SITE is
992+
// also disabled, so it has to be replaced to MESSAGE_PRIVACY_COURSEMEMBER.
993+
if (empty($CFG->messagingallusers) && $privacypreference == self::MESSAGE_PRIVACY_SITE) {
994+
$privacypreference = self::MESSAGE_PRIVACY_COURSEMEMBER;
995+
}
996+
997+
return $privacypreference;
998+
}
999+
9551000
/**
9561001
* Checks if the recipient is allowing messages from users that aren't a
9571002
* contact. If not then it checks to make sure the sender is in the
@@ -962,23 +1007,31 @@ public static function can_post_message($recipient, $sender = null) {
9621007
* @return bool true if $sender is blocked, false otherwise.
9631008
*/
9641009
public static function is_user_non_contact_blocked($recipient, $sender = null) {
965-
global $USER;
1010+
global $USER, $CFG;
9661011

9671012
if (is_null($sender)) {
9681013
// The message is from the logged in user, unless otherwise specified.
9691014
$sender = $USER;
9701015
}
9711016

972-
$blockednoncontacts = get_user_preferences('message_blocknoncontacts', '', $recipient->id);
973-
if (!empty($blockednoncontacts)) {
974-
// Confirm the sender is a contact of the recipient.
975-
if (self::is_contact($sender->id, $recipient->id)) {
976-
// All good, the recipient is a contact of the sender.
977-
return false;
978-
} else {
979-
// Oh no, the recipient is not a contact. Looks like we can't send the message.
980-
return true;
981-
}
1017+
$privacypreference = self::get_user_privacy_messaging_preference($recipient->id);
1018+
switch ($privacypreference) {
1019+
case self::MESSAGE_PRIVACY_SITE:
1020+
if (!empty($CFG->messagingallusers)) {
1021+
// Users can be messaged without being contacts or members of the same course.
1022+
break;
1023+
}
1024+
// When the $CFG->messagingallusers privacy setting is disabled, continue with the next
1025+
// case, because MESSAGE_PRIVACY_SITE is replaced to MESSAGE_PRIVACY_COURSEMEMBER.
1026+
case self::MESSAGE_PRIVACY_COURSEMEMBER:
1027+
// Confirm the sender and the recipient are both members of the same course.
1028+
if (enrol_sharing_course($recipient, $sender)) {
1029+
// All good, the recipient and the sender are members of the same course.
1030+
return false;
1031+
}
1032+
case self::MESSAGE_PRIVACY_ONLYCONTACTS:
1033+
// True if they aren't contacts (they can't send a message because of the privacy settings), false otherwise.
1034+
return !self::is_contact($sender->id, $recipient->id);
9821035
}
9831036

9841037
return false;

0 commit comments

Comments
 (0)