Skip to content

Commit

Permalink
MDL-67853 message: Remove on/offline settings on message preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyserver committed Jan 27, 2022
1 parent e8ad1ea commit d74bd79
Show file tree
Hide file tree
Showing 44 changed files with 695 additions and 681 deletions.
87 changes: 34 additions & 53 deletions admin/message.php
Expand Up @@ -48,69 +48,52 @@
foreach ($providers as $provider) {
$componentproviderbase = $provider->component.'_'.$provider->name;
$disableprovidersetting = $componentproviderbase.'_disable';
$providerdisabled = false;
if (!isset($form->$disableprovidersetting)) {
$providerdisabled = true;
$newpreferences[$disableprovidersetting] = 1;
} else {
$newpreferences[$disableprovidersetting] = 0;
}

foreach (array('permitted', 'loggedin', 'loggedoff') as $setting) {
$value = null;
$componentprovidersetting = $componentproviderbase.'_'.$setting;
if ($setting == 'permitted') {
// If we deal with permitted select element, we need to create individual
// setting for each possible processor. Note that this block will
// always be processed first after entring parental foreach iteration
// so we can change form values on this stage.
foreach ($processors as $processor) {
$value = '';
if (isset($form->{$componentprovidersetting}[$processor->name])) {
$value = $form->{$componentprovidersetting}[$processor->name];
}
// Ensure that loggedin loggedoff options are set correctly for this permission.
if (($value == 'disallowed') || $providerdisabled) {
// It might be better to unset them, but I can't figure out why that cause error.
$form->{$componentproviderbase.'_loggedin'}[$processor->name] = 0;
$form->{$componentproviderbase.'_loggedoff'}[$processor->name] = 0;
} else if ($value == 'forced') {
$form->{$componentproviderbase.'_loggedin'}[$processor->name] = 1;
$form->{$componentproviderbase.'_loggedoff'}[$processor->name] = 1;
}
// Record the site preference.
$newpreferences[$processor->name.'_provider_'.$componentprovidersetting] = $value;
}
} else {
$newsettings = array();
if (property_exists($form, $componentprovidersetting)) {
// We must be processing loggedin or loggedoff checkboxes.
// Store defained comma-separated processors as setting value.
// Using array_filter eliminates elements set to 0 above.
$newsettings = array_keys(array_filter($form->{$componentprovidersetting}));
$componentprovidersetting = $componentproviderbase.'_locked';
foreach ($processors as $processor) {
$value = 0;
if (isset($form->{$componentprovidersetting}[$processor->name])) {
$value = $form->{$componentprovidersetting}[$processor->name];
if ($value == 'on') {
$value = 1;
}
}

// Let's join existing setting values for disabled processors.
$property = 'message_provider_'.$componentprovidersetting;
if (property_exists($preferences, $property)) {
$existingsetting = $preferences->$property;
foreach ($disabledprocessors as $disable) {
if (strpos($existingsetting, $disable->name) > -1) {
$newsettings[] = $disable->name;
}
}
}
// Record the site preference.
$newpreferences[$processor->name.'_provider_'.$componentprovidersetting] = $value;
}

$componentprovidersetting = $componentproviderbase.'_enabled';
$newsettings = [];
if (isset($form->$componentprovidersetting)) {
// Store defined comma-separated processors as setting value.
// Using array_filter eliminates elements set to 0 above.
$newsettings = array_keys(array_filter($form->{$componentprovidersetting}));
}

$value = join(',', $newsettings);
if (empty($value)) {
$value = null;
// Let's join existing setting values for disabled processors.
$property = 'message_provider_'.$componentprovidersetting;
if (property_exists($preferences, $property)) {
$existingsetting = $preferences->$property;
foreach ($disabledprocessors as $disable) {
if (strpos($existingsetting, $disable->name) > -1) {
$newsettings[] = $disable->name;
}
}
if ($setting != 'permitted') {
// We have already recoded site preferences for 'permitted' type.
$newpreferences['message_provider_'.$componentprovidersetting] = $value;
}
}

$value = join(',', $newsettings);
if (empty($value)) {
$value = null;
}

// Record the site preference.
$newpreferences['message_provider_'.$componentprovidersetting] = $value;
}

// Update database.
Expand Down Expand Up @@ -142,8 +125,6 @@

// Page settings
$PAGE->set_context(context_system::instance());
$PAGE->requires->js_init_call('M.core_message.init_defaultoutputs');

$renderer = $PAGE->get_renderer('core', 'message');

// Display the page.
Expand Down
10 changes: 5 additions & 5 deletions admin/tool/dataprivacy/db/messages.php
Expand Up @@ -28,24 +28,24 @@
// Notify Data Protection Officer about incoming data requests.
'contactdataprotectionofficer' => [
'defaults' => [
'popup' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDIN + MESSAGE_DEFAULT_LOGGEDOFF,
'email' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDIN + MESSAGE_DEFAULT_LOGGEDOFF,
'popup' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_ENABLED,
'email' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_ENABLED,
],
'capability' => 'tool/dataprivacy:managedatarequests'
],

// Notify user about the processing results of their data request.
'datarequestprocessingresults' => [
'defaults' => [
'popup' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDIN + MESSAGE_DEFAULT_LOGGEDOFF,
'email' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDIN + MESSAGE_DEFAULT_LOGGEDOFF,
'popup' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_ENABLED,
'email' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_ENABLED,
]
],

// Notify Data Protection Officer about exceptions.
'notifyexceptions' => [
'defaults' => [
'email' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDIN + MESSAGE_DEFAULT_LOGGEDOFF,
'email' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_ENABLED,
],
'capability' => 'tool/dataprivacy:managedatarequests'
],
Expand Down
7 changes: 7 additions & 0 deletions lang/en/deprecated.txt
Expand Up @@ -82,3 +82,10 @@ navmethod,core_grades
dropdown,core_grades
tabs,core_grades
combo,core_grades
defaults,core_message
loggedin_help,core_message
loggedindescription,core_message
loggedoff_help,core_message
loggedoffdescription,core_message
sendingvia,core_message
sendingviawhen,core_message
21 changes: 14 additions & 7 deletions lang/en/message.php
Expand Up @@ -51,7 +51,6 @@
$string['conversationactions'] = 'Conversation actions menu';
$string['decline'] = 'Decline';
$string['defaultmessageoutputs'] = 'Notification settings';
$string['defaults'] = 'Defaults';
$string['deleteallconfirm'] = "Are you sure you would like to delete this entire conversation? This will not delete it for other conversation participants.";
$string['deleteallmessages'] = "Delete all messages";
$string['deleteallselfconfirm'] = "Are you sure you would like to delete this entire personal conversation?";
Expand Down Expand Up @@ -93,11 +92,7 @@
$string['isnotinyourcontacts'] = '{$a} is not in your contacts';
$string['loadmore'] = 'Load more';
$string['loggedin'] = 'Online';
$string['loggedin_help'] = 'Configure how you would like to receive notifications when you are logged into Moodle';
$string['loggedindescription'] = 'When you are logged into Moodle';
$string['loggedoff'] = 'Offline';
$string['loggedoff_help'] = 'Configure how you would like to receive notifications when you are not logged into Moodle';
$string['loggedoffdescription'] = 'When you are not logged into Moodle';
$string['managemessageoutputs'] = 'Default notification preferences';
$string['messageoutputs'] = 'Notification plugins';
$string['messagepreferences'] = 'Message preferences';
Expand Down Expand Up @@ -213,6 +208,9 @@
$string['privacy:request:preference:set'] = 'The value of the setting \'{$a->name}\' was \'{$a->value}\'';
$string['privacy:export:conversationprefix'] = 'Conversation: ';
$string['processorsettings'] = 'Processor settings';
$string['providerenabled'] = 'Sending "{$a}" enabled status';
$string['providerprocesorislocked'] = '"{$a->provider}" on "{$a->processor}" is locked';
$string['providerprocesorisdisallowed'] = '"{$a->provider}" on "{$a->processor}" is disallowed';
$string['removecontact'] = 'Remove contact';
$string['removecontactconfirm'] = 'Are you sure you want to remove {$a} from your contacts?';
$string['removecoursefilter'] = 'Remove filter for course {$a}';
Expand All @@ -231,9 +229,9 @@
$string['selfconversationdefaultmessage'] = 'Save draft messages, links, notes etc. to access later.';
$string['send'] = 'Send';
$string['sender'] = '{$a}:';
$string['sendingvia'] = 'Sending "{$a->provider}" via "{$a->processor}"';
$string['sendingviawhen'] = 'Sending "{$a->provider}" via "{$a->processor}" when {$a->state}';
$string['sendcontactrequest'] = 'Send contact request';
$string['sendingviaenabled'] = 'Sending "{$a->provider}" via "{$a->processor}" enabled status';
$string['sendingvialocked'] = 'Sending "{$a->provider}" via "{$a->processor}" locked status';
$string['sendmessage'] = 'Send message';
$string['sendbulkmessage'] = 'Send message to {$a} people';
$string['sendbulkmessagesingle'] = 'Send message to 1 person';
Expand Down Expand Up @@ -279,3 +277,12 @@
// Deprecated since Moodle 3.9.
$string['messagecontactrequestsnotification'] = '{$a} is requesting to be added as a contact.';
$string['messagecontactrequestsnotificationsubject'] = 'Contact request from {$a}';

// Deprecated since Moodle 4.0.
$string['defaults'] = 'Defaults';
$string['loggedin_help'] = 'Configure how you would like to receive notifications when you are logged into Moodle';
$string['loggedindescription'] = 'When you are logged into Moodle';
$string['loggedoff_help'] = 'Configure how you would like to receive notifications when you are not logged into Moodle';
$string['loggedoffdescription'] = 'When you are not logged into Moodle';
$string['sendingvia'] = 'Sending "{$a->provider}" via "{$a->processor}"';
$string['sendingviawhen'] = 'Sending "{$a->provider}" via "{$a->processor}" when {$a->state}';
39 changes: 16 additions & 23 deletions lib/classes/message/manager.php
Expand Up @@ -208,20 +208,6 @@ public static function send_message_to_conversation(message $eventdata, \stdClas
return false;
}

// Set the online state.
if (isset($CFG->block_online_users_timetosee)) {
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
} else {
$timetoshowusers = 300;
}

// Work out if the user is logged in or not.
$userstate = 'loggedoff';
if (!empty($localisedeventdata->userto->lastaccess)
&& (time() - $timetoshowusers) < $localisedeventdata->userto->lastaccess) {
$userstate = 'loggedin';
}

// Fill in the array of processors to be used based on default and user preferences.
// Do not process muted conversations.
$processorlist = [];
Expand All @@ -233,37 +219,44 @@ public static function send_message_to_conversation(message $eventdata, \stdClas
}

// First find out permissions.
$defaultpreference = $processor->name . '_provider_' . $preferencebase . '_permitted';
if (isset($defaultpreferences->{$defaultpreference})) {
$permitted = $defaultpreferences->{$defaultpreference};
$defaultlockedpreference = $processor->name . '_provider_' . $preferencebase . '_locked';
$locked = false;
if (isset($defaultpreferences->{$defaultlockedpreference})) {
$locked = $defaultpreferences->{$defaultlockedpreference};
} else {
// MDL-25114 They supplied an $eventdata->component $eventdata->name combination which doesn't
// exist in the message_provider table (thus there is no default settings for them).
$preferrormsg = "Could not load preference $defaultpreference. Make sure the component and name you supplied
to message_send() are valid.";
$preferrormsg = "Could not load preference $defaultlockedpreference.
Make sure the component and name you supplied to message_send() are valid.";
throw new coding_exception($preferrormsg);
}

$enabledpreference = 'message_provider_'.$preferencebase . '_enabled';
$forced = false;
if ($locked && isset($defaultpreferences->{$enabledpreference})) {
$forced = $defaultpreferences->{$enabledpreference};
}

// Find out if user has configured this output.
// Some processors cannot function without settings from the user.
$userisconfigured = $processor->object->is_user_configured($recipient);

// DEBUG: notify if we are forcing unconfigured output.
if ($permitted == 'forced' && !$userisconfigured) {
if ($forced && !$userisconfigured) {
debugging('Attempt to force message delivery to user who has "' . $processor->name .
'" output unconfigured', DEBUG_NORMAL);
}

// Populate the list of processors we will be using.
if (!$eventdata->notification && $processor->object->force_process_messages()) {
$processorlist[] = $processor->name;
} else if ($permitted == 'forced' && $userisconfigured) {
} else if ($forced && $userisconfigured) {
// An admin is forcing users to use this message processor. Use this processor unconditionally.
$processorlist[] = $processor->name;
} else if ($permitted == 'permitted' && $userisconfigured && !$recipient->emailstop) {
} else if (!$locked && $userisconfigured && !$recipient->emailstop) {
// User has not disabled notifications.
// See if user set any notification preferences, otherwise use site default ones.
$userpreferencename = 'message_provider_' . $preferencebase . '_' . $userstate;
$userpreferencename = 'message_provider_' . $preferencebase . '_enabled';
if ($userpreference = get_user_preferences($userpreferencename, null, $recipient)) {
if (in_array($processor->name, explode(',', $userpreference))) {
$processorlist[] = $processor->name;
Expand Down

0 comments on commit d74bd79

Please sign in to comment.