messagedselecteduserfailed,core
eventmessagecontactblocked,core_message
eventmessagecontactunblocked,core_message
+userisblockingyou,core_message
+userisblockingyounoncontact,core_message
\ No newline at end of file
$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';
$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}';
$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.';
\ No newline at end of file
set_config('messagingallusers', false);
} else {
// When $CFG->keepmessagingallusersenabled is set to true, $CFG->messagingallusers is set to true.
- // When $CFG->messagingallusers = true, the default user preference is MESSAGE_PRIVACY_SITE
- // (contacted by all users site).
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.
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"]',
};
/**
};
/**
- * 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();
userid: this.userId,
preferences: [
{
- type: checkbox.attr('data-preference-key'),
- value: ischecked ? 1 : 0,
+ type: container.attr('data-preference-key'),
+ value: value,
}
]
}
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));
};
*/
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.
*
}
// 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);
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
* @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;
}
list($sqluserids, $sqlparams) = $DB->get_in_or_equal($receivers);
$tousers = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
- $blocklist = array();
- $contactlist = array();
- $contactsqlparams = array_merge($sqlparams, [$USER->id], [$USER->id], $sqlparams);
- $rs = $DB->get_recordset_sql("SELECT *
- FROM {message_contacts}
- WHERE (userid $sqluserids AND contactid = ?)
- OR (userid = ? AND contactid $sqluserids)", $contactsqlparams);
- foreach ($rs as $record) {
- $useridtouse = $record->userid;
- if ($record->userid == $USER->id) {
- $useridtouse = $record->contactid;
- }
- $contactlist[$useridtouse] = true;
- }
- $rs->close();
- $blocksqlparams = array_merge($sqlparams, [$USER->id]);
- $rs = $DB->get_recordset_sql("SELECT *
- FROM {message_users_blocked}
- WHERE userid $sqluserids
- AND blockeduserid = ?", $blocksqlparams);
- foreach ($rs as $record) {
- $blocklist[$record->userid] = true;
- }
- $rs->close();
-
- $canreadallmessages = has_capability('moodle/site:readallmessages', $context);
$resultmessages = array();
foreach ($params['messages'] as $message) {
$resultmsg = array(); //the infos about the success of the operation
- //we are going to do some checking
- //code should match /messages/index.php checks
+ // We are going to do some checking.
+ // Code should match /messages/index.php checks.
$success = true;
- //check the user exists
+ // Check the user exists.
if (empty($tousers[$message['touserid']])) {
$success = false;
$errormessage = get_string('touserdoesntexist', 'message', $message['touserid']);
}
- //check that the touser is not blocking the current user
- if ($success and !empty($blocklist[$message['touserid']]) and !$canreadallmessages) {
+ // TODO MDL-31118 performance improvement - edit the function so we can pass an array instead userid
+ // Check if the recipient can be messaged by the sender.
+ if ($success && !\core_message\api::can_post_message($tousers[$message['touserid']], $USER)) {
$success = false;
- $errormessage = get_string('userisblockingyou', 'message');
+ $errormessage = get_string('usercantbemessaged', 'message', fullname(\core_user::get_user($message['touserid'])));
}
- // Check if the user is a contact
- //TODO MDL-31118 performance improvement - edit the function so we can pass an array instead userid
- $blocknoncontacts = get_user_preferences('message_blocknoncontacts', NULL, $message['touserid']);
- // message_blocknoncontacts option is on and current user is not in contact list
- if ($success && empty($contactlist[$message['touserid']]) && !empty($blocknoncontacts)) {
- // The user isn't a contact and they have selected to block non contacts so this message won't be sent.
- $success = false;
- $errormessage = get_string('userisblockingyounoncontact', 'message',
- fullname(core_user::get_user($message['touserid'])));
- }
-
- //now we can send the message (at least try)
+ // Now we can send the message (at least try).
if ($success) {
- //TODO MDL-31118 performance improvement - edit the function so we can pass an array instead one touser object
+ // TODO MDL-31118 performance improvement - edit the function so we can pass an array instead one touser object.
$success = message_post_message($USER, $tousers[$message['touserid']],
$message['text'], external_validate_format($message['textformat']));
}
- //build the resultmsg
+ // Build the resultmsg.
if (isset($message['clientmsgid'])) {
$resultmsg['clientmsgid'] = $message['clientmsgid'];
}
/**
* Unblock contacts.
*
- * @deprecated since Moodle 3.6
* @param array $userids array of user IDs.
* @param int $userid The id of the user we are unblocking the contacts for
* @return null
$result = array(
'warnings' => array(),
'preferences' => $notificationlistoutput->export_for_template($renderer),
- 'blocknoncontacts' => get_user_preferences('message_blocknoncontacts', '', $user->id) ? true : false,
+ 'blocknoncontacts' => \core_message\api::get_user_privacy_messaging_preference($user->id),
);
return $result;
}
return new external_function_parameters(
array(
'preferences' => self::get_preferences_structure(),
- 'blocknoncontacts' => new external_value(PARAM_BOOL, 'Whether to block or not messages from non contacts'),
+ 'blocknoncontacts' => new external_value(PARAM_INT, 'Privacy messaging setting to define who can message you'),
'warnings' => new external_warnings(),
)
);
* @return array
*/
function core_message_user_preferences() {
-
$preferences = [];
- $preferences['message_blocknoncontacts'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 0,
- 'choices' => array(0, 1));
+ $preferences['message_blocknoncontacts'] = array(
+ 'type' => PARAM_INT,
+ 'null' => NULL_NOT_ALLOWED,
+ 'default' => 0,
+ 'choices' => array(
+ \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
+ \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
+ \core_message\api::MESSAGE_PRIVACY_SITE
+ ),
+ 'cleancallback' => function ($value) {
+ global $CFG;
+
+ // When site-wide messaging between users is disabled, MESSAGE_PRIVACY_SITE should be converted.
+ if (empty($CFG->messagingallusers) && $value === \core_message\api::MESSAGE_PRIVACY_SITE) {
+ return \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER;
+ }
+ return $value;
+ }
+ );
$preferences['/^message_provider_([\w\d_]*)_logged(in|off)$/'] = array('isregex' => true, 'type' => PARAM_NOTAGS,
'null' => NULL_NOT_ALLOWED, 'default' => 'none',
'permissioncallback' => function ($user, $preferencename) {
Background:
Given the following "users" exist:
- | username | firstname | lastname | email |
+ | username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student2@example.com |
+ And the following "courses" exist:
+ | fullname | shortname |
+ | Course 1 | C1 |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | student1 | C1 | student |
+ | student2 | C1 | student |
And I log in as "student2"
And I send "Test message" message to "Student 1" user
And I log out
* @return string The text to render
*/
public function render_user_message_preferences($user) {
+ global $CFG;
+
// Filter out enabled, available system_configured and user_configured processors only.
$readyprocessors = array_filter(get_message_processors(), function($processor) {
return $processor->enabled &&
$notificationlistoutput = new \core_message\output\preferences\message_notification_list($readyprocessors,
$providers, $preferences, $user);
$context = $notificationlistoutput->export_for_template($this);
- $context['blocknoncontacts'] = get_user_preferences('message_blocknoncontacts', '', $user->id) ? true : false;
+
+ // Get the privacy settings options for being messaged.
+ $privacysetting = \core_message\api::get_user_privacy_messaging_preference($user->id);
+ $choices = array();
+ $choices[] = [
+ 'value' => \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
+ 'text' => get_string('contactableprivacy_onlycontacts', 'message'),
+ 'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS)
+ ];
+ $choices[] = [
+ 'value' => \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
+ 'text' => get_string('contactableprivacy_coursemember', 'message'),
+ 'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER)
+ ];
+ if (!empty($CFG->messagingallusers)) {
+ // Add the MESSAGE_PRIVACY_SITE option when site-wide messaging between users is enabled.
+ $choices[] = [
+ 'value' => \core_message\api::MESSAGE_PRIVACY_SITE,
+ 'text' => get_string('contactableprivacy_site', 'message'),
+ 'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_SITE)
+ ];
+ }
+ $context['privacychoices'] = $choices;
return $this->render_from_template('message/message_preferences', $context);
}
* userid The logged in user id
* disableall If the user has disabled notifications
* components The list of notification components
+ * privacychoices The choice options for the contactable privacy setting
Example context (json):
{
}
]
}
+ ],
+ "privacychoices": [
+ {
+ "value": 1,
+ "text": "My contacts only",
+ "checked": 0
+ },
+ {
+ "value": 2,
+ "text": "Anyone within courses I am a member of",
+ "checked": 1
+ }
]
}
}}
<div class="preferences-page-container" data-region="preferences-page-container">
<h2>{{#str}} messagepreferences, message {{/str}}</h2>
- <div class="checkbox-container" data-region="block-non-contacts-container">
- <input id="block-non-contacts"
- type="checkbox"
- data-user-id="{{userid}}"
- data-block-non-contacts
- data-preference-key="message_blocknoncontacts"
- {{#blocknoncontacts}}checked{{/blocknoncontacts}} />
- <label for="block-non-contacts">{{#str}} blocknoncontacts, message {{/str}}</label>
- {{> core/loading }}
- </div>
+ <div class="privacy-setting-container"
+ data-user-id="{{userid}}"
+ data-region="privacy-setting-container"
+ data-preference-key="message_blocknoncontacts">
+ <p>{{#str}} contactableprivacy, message {{/str}}</p>
+ {{#privacychoices}}
+ <input id="action-selection-option-{{value}}"
+ type="radio"
+ name="message_blocknoncontacts"
+ value="{{value}}"
+ {{#checked}}checked="checked"{{/checked}}/>
+ <label for="action-selection-option-{{value}}">{{text}}</label>
+ <br>
+ {{/privacychoices}}
+ </div><br>
<div class="preferences-container {{#disableall}}disabled{{/disableall}}"
data-user-id="{{userid}}"
data-region="preferences-container">
// Create user to add to the admin's block list.
$user1 = $this->getDataGenerator()->create_user();
+ $user2 = $this->getDataGenerator()->create_user();
$this->assertEquals(0, \core_message\api::count_blocked_users());
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
- // Set as the user 1.
+ // Set as the first user.
$this->setUser($user1);
- // They can post to someone else.
+ // With the default privacy setting, users can't message them.
+ $this->assertFalse(\core_message\api::can_post_message($user2));
+
+ // Enrol users to the same course.
+ $course = $this->getDataGenerator()->create_course();
+ $this->getDataGenerator()->enrol_user($user1->id, $course->id);
+ $this->getDataGenerator()->enrol_user($user2->id, $course->id);
+ // After enrolling users to the course, they should be able to message them with the default privacy setting.
$this->assertTrue(\core_message\api::can_post_message($user2));
}
$this->assertFalse(\core_message\api::can_post_message($user2));
}
+ /**
+ * Tests the user can post a message when they are contact.
+ */
+ public function test_can_post_message_when_contact() {
+ // Create some users.
+ $user1 = self::getDataGenerator()->create_user();
+ $user2 = self::getDataGenerator()->create_user();
+
+ // Set as the first user.
+ $this->setUser($user1);
+
+ // Check that we can not send user2 a message.
+ $this->assertFalse(\core_message\api::can_post_message($user2));
+
+ // Add users as contacts.
+ \core_message\api::add_contact($user1->id, $user2->id);
+
+ // Check that the return result is now true.
+ $this->assertTrue(\core_message\api::can_post_message($user2));
+ }
+
/**
* Tests the user can't post a message if they are not a contact and the user
* has requested messages only from contacts.
$this->setUser($user1);
// Set the second user's preference to not receive messages from non-contacts.
- set_user_preference('message_blocknoncontacts', 1, $user2->id);
+ set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
// Check that we can not send user 2 a message.
$this->assertFalse(\core_message\api::can_post_message($user2));
$this->assertFalse(\core_message\api::can_post_message($user1, $user2));
}
+ /**
+ * Tests the user can post a message when site-wide messaging setting is enabled,
+ * even if they are not a contact and are not members of the same course.
+ */
+ public function test_can_post_message_site_messaging_setting() {
+ // Create some users.
+ $user1 = self::getDataGenerator()->create_user();
+ $user2 = self::getDataGenerator()->create_user();
+
+ // Set as the first user.
+ $this->setUser($user1);
+
+ // Set the second user's preference to receive messages from everybody. As site-wide messaging setting
+ // is disabled by default, the value will be changed to MESSAGE_PRIVACY_COURSEMEMBER.
+ set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_SITE, $user2->id);
+ $this->assertFalse(\core_message\api::can_post_message($user2));
+
+ // Enable site-wide messagging privacy setting. The user will be able to receive messages from everybody.
+ set_config('messagingallusers', true);
+ // Check that we can send user2 a message.
+ $this->assertTrue(\core_message\api::can_post_message($user2));
+ }
+
+ /**
+ * Tests get_user_privacy_messaging_preference method.
+ */
+ public function test_get_user_privacy_messaging_preference() {
+ // Create some users.
+ $user1 = self::getDataGenerator()->create_user();
+ $user2 = self::getDataGenerator()->create_user();
+ $user3 = self::getDataGenerator()->create_user();
+
+ // Enable site-wide messagging privacy setting. The user will be able to receive messages from everybody.
+ set_config('messagingallusers', true);
+
+ // Set some user preferences.
+ set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_SITE, $user1->id);
+ set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
+
+ // Check the returned value for each user.
+ $this->assertEquals(
+ \core_message\api::MESSAGE_PRIVACY_SITE,
+ \core_message\api::get_user_privacy_messaging_preference($user1->id)
+ );
+ $this->assertEquals(
+ \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
+ \core_message\api::get_user_privacy_messaging_preference($user2->id)
+ );
+ $this->assertEquals(
+ \core_message\api::MESSAGE_PRIVACY_SITE,
+ \core_message\api::get_user_privacy_messaging_preference($user3->id)
+ );
+
+ // Disable site-wide messagging privacy setting. The user will be able to receive messages from members of their course.
+ set_config('messagingallusers', false);
+
+ // Check the returned value for each user.
+ $this->assertEquals(
+ \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
+ \core_message\api::get_user_privacy_messaging_preference($user1->id)
+ );
+ $this->assertEquals(
+ \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
+ \core_message\api::get_user_privacy_messaging_preference($user2->id)
+ );
+ $this->assertEquals(
+ \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
+ \core_message\api::get_user_privacy_messaging_preference($user3->id)
+ );
+ }
+
/**
* Tests that when blocking messages from non-contacts is enabled that
* non-contacts trying to send a message return false.
// Set as the first user.
$this->setUser($user1);
- // User hasn't sent their preference to block non-contacts, so should return false.
+ // By default, user only can be messaged by contacts and members of any of his/her courses.
+ $this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
+
+ // Enable all users privacy messaging and check now the default user's preference has been set to allow receiving
+ // messages from everybody.
+ set_config('messagingallusers', true);
+ // Check that the return result is now false because any site user can contact him/her.
$this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
// Set the second user's preference to not receive messages from non-contacts.
- set_user_preference('message_blocknoncontacts', 1, $user2->id);
-
- // Check that the return result is now true.
+ set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2->id);
+ // Check that the return result is still true (because is even more restricted).
$this->assertTrue(\core_message\api::is_user_non_contact_blocked($user2));
// Add the first user as a contact for the second user.
// Check that the return result is now false.
$this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
- // Set the first user's preference to not receive messages from non-contacts.
- set_user_preference('message_blocknoncontacts', 1, $user1->id);
- $this->setUser($user2);
- // Confirm it is still false. We want to ensure a contact request works both ways
- // as it is now an agreement between users.
- $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user1));
+ // Set the second user's preference to receive messages from course members.
+ set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER, $user2->id);
+ // Check that the return result is still false (because $user1 is still his/her contact).
+ $this->assertFalse(\core_message\api::is_user_non_contact_blocked($user2));
}
/**
// Get the contacts and the unread message count.
$messages = \core_message\api::get_contacts_with_unread_message_count($user2->id);
+
// Confirm the size is correct.
$this->assertCount(2, $messages);
ksort($messages);
Scenario: Delete all messages
Given the following "users" exist:
- | username | firstname | lastname | email |
+ | username | firstname | lastname | email |
| user1 | User | 1 | user1@example.com |
| user2 | User | 2 | user2@example.com |
+ And the following "courses" exist:
+ | fullname | shortname |
+ | Course 1 | C1 |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | user1 | C1 | student |
+ | user2 | C1 | student |
And I log in as "user2"
And I send "User 2 to User 1 message 1" message to "User 1" user
And I send "User 2 to User 1 message 2" message in the message area
Scenario: Delete messages
Given the following "users" exist:
- | username | firstname | lastname | email |
+ | username | firstname | lastname | email |
| user1 | User | 1 | user1@example.com |
| user2 | User | 2 | user2@example.com |
+ And the following "courses" exist:
+ | fullname | shortname |
+ | Course 1 | C1 |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | user1 | C1 | student |
+ | user2 | C1 | student |
And I log in as "user2"
And I send "User 2 to User 1 message 1" message to "User 1" user
And I send "User 2 to User 1 message 2" message in the message area
| username | firstname | lastname | email |
| user1 | User | 1 | user1@example.com |
| user2 | User | 2 | user2@example.com |
+ And the following "courses" exist:
+ | fullname | shortname |
+ | Course 1 | C1 |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | user1 | C1 | student |
+ | user2 | C1 | student |
And I log in as "user2"
And I send "User 2 to User 1" message to "User 1" user
And I log out
Background:
Given the following "users" exist:
- | username | firstname | lastname | email |
+ | username | firstname | lastname | email |
| user1 | User | 1 | user1@example.com |
| user2 | User | 2 | user2@example.com |
| user3 | User | 3 | user3@example.com |
+ And the following "courses" exist:
+ | fullname | shortname |
+ | Course 1 | C1 |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | user1 | C1 | student |
+ | user2 | C1 | student |
+ | user3 | C1 | student |
And I log in as "user2"
And I send "User 2 to User 1" message to "User 1" user
And I log out
Scenario: View messages from multiple users
Given the following "users" exist:
- | username | firstname | lastname | email |
+ | username | firstname | lastname | email |
| user1 | User | 1 | user1@example.com |
| user2 | User | 2 | user2@example.com |
| user3 | User | 3 | user3@example.com |
+ And the following "courses" exist:
+ | fullname | shortname |
+ | Course 1 | C1 |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | user1 | C1 | student |
+ | user2 | C1 | student |
+ | user3 | C1 | student |
And I log in as "user2"
And I send "User 2 to User 1" message to "User 1" user
And I log out
$sentmessages = core_message_external::send_instant_messages($messages);
$sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages);
+ $this->assertEquals(
+ get_string('usercantbemessaged', 'message', fullname(\core_user::get_user($message1['touserid']))),
+ array_pop($sentmessages)['errormessage']
+ );
+
+ // Add the user1 as a contact.
+ \core_message\api::add_contact($user1->id, $user2->id);
+
+ // Send message again. Now it should work properly.
+ $sentmessages = core_message_external::send_instant_messages($messages);
+ // We need to execute the return values cleaning process to simulate the web service server.
+ $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages);
$sentmessage = reset($sentmessages);
$sentmessage = reset($sentmessages);
- $this->assertEquals(get_string('userisblockingyou', 'message'), $sentmessage['errormessage']);
+ $this->assertEquals(get_string('usercantbemessaged', 'message', fullname($user2)), $sentmessage['errormessage']);
$this->assertEquals(0, $DB->count_records('messages'));
}
$this->setUser($user1);
// Set the user preference so user 2 does not accept messages from non-contacts.
- set_user_preference('message_blocknoncontacts', 1, $user2);
+ set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2);
// Create test message data.
$message1 = array();
$sentmessage = reset($sentmessages);
- $this->assertEquals(get_string('userisblockingyounoncontact', 'message', fullname($user2)), $sentmessage['errormessage']);
+ $this->assertEquals(get_string('usercantbemessaged', 'message', fullname($user2)), $sentmessage['errormessage']);
$this->assertEquals(0, $DB->count_records('messages'));
}
$this->setUser($user1);
// Set the user preference so user 2 does not accept messages from non-contacts.
- set_user_preference('message_blocknoncontacts', 1, $user2);
+ set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $user2);
\core_message\api::add_contact($user1->id, $user2->id);
$user = self::getDataGenerator()->create_user();
$this->setUser($user);
+ // Enable site-wide messagging privacy setting. The user will be able to receive messages from everybody.
+ set_config('messagingallusers', true);
+
// Set a couple of preferences to test.
set_user_preference('message_provider_moodle_instantmessage_loggedin', 'email', $user);
set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user);
- set_user_preference('message_blocknoncontacts', 1, $user);
+ set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_SITE, $user);
$prefs = core_message_external::get_user_message_preferences();
$prefs = external_api::clean_returnvalue(core_message_external::get_user_message_preferences_returns(), $prefs);
// Check components.
$this->assertCount(1, $prefs['preferences']['components']);
- $this->assertTrue($prefs['blocknoncontacts']);
+ $this->assertEquals(\core_message\api::MESSAGE_PRIVACY_SITE, $prefs['blocknoncontacts']);
// Check some preferences that we previously set.
$found = false;
// Set some message user preferences.
set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $USER->id);
set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'popup', $USER->id);
- set_user_preference('message_blocknoncontacts', 1, $USER->id);
+ set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $USER->id);
set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'inbound', $user->id);
// Set an unrelated preference.
- core_message_external::block_contacts, please use core_message_external::block_user instead.
- core_message_external::unblock_contacts, please use core_message_external::unblock_user instead.
- core_message_external::create_contacts, please use core_message_external::create_contact_request instead.
+* The following function has been added for getting the privacy messaging preference:
+ - get_user_privacy_messaging_preference()
=== 3.5 ===
Background:
Given the following "users" exist:
- | username | firstname | lastname | email |
- | user1 | User | One | one@example.com |
- | user2 | User | Two | two@example.com |
- | user3 | User | Three | three@example.com |
- | user4 | User | Four | four@example.com |
+ | username | firstname | lastname | email |
+ | user1 | User | One | one@example.com |
+ | user2 | User | Two | two@example.com |
+ | user3 | User | Three | three@example.com |
+ | user4 | User | Four | four@example.com |
+ And the following "courses" exist:
+ | fullname | shortname |
+ | Course 1 | C1 |
+ And the following "course enrolments" exist:
+ | user | course | role |
+ | user1 | C1 | student |
+ | user2 | C1 | student |
+ | user3 | C1 | student |
+ | user4 | C1 | student |
@javascript
Scenario: Deleting one user at a time