From 57afd82ab0636f232d597d0c02f0b776410c4c8d Mon Sep 17 00:00:00 2001 From: Amaia Anabitarte Date: Fri, 14 Dec 2018 16:14:32 +0100 Subject: [PATCH] MDL-64400 core_messages: Allow cron to delete unread notifications too --- admin/settings/subsystems.php | 24 +++++++++++++++++++-- lang/en/admin.php | 2 ++ lib/classes/task/messaging_cleanup_task.php | 11 +++++++--- mod/forum/tests/mail_test.php | 1 + 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/admin/settings/subsystems.php b/admin/settings/subsystems.php index db263854258d0..a108b4277a959 100644 --- a/admin/settings/subsystems.php +++ b/admin/settings/subsystems.php @@ -21,8 +21,28 @@ 0) ); - $options = array(DAYSECS=>new lang_string('secondstotime86400'), WEEKSECS=>new lang_string('secondstotime604800'), 2620800=>new lang_string('nummonths', 'moodle', 1), 15724800=>new lang_string('nummonths', 'moodle', 6),0=>new lang_string('never')); - $optionalsubsystems->add(new admin_setting_configselect('messagingdeletereadnotificationsdelay', new lang_string('messagingdeletereadnotificationsdelay', 'admin'), new lang_string('configmessagingdeletereadnotificationsdelay', 'admin'), 604800, $options)); + $options = array( + DAYSECS => new lang_string('secondstotime86400'), + WEEKSECS => new lang_string('secondstotime604800'), + 2620800 => new lang_string('nummonths', 'moodle', 1), + 7862400 => new lang_string('nummonths', 'moodle', 3), + 15724800 => new lang_string('nummonths', 'moodle', 6), + 0 => new lang_string('never') + ); + $optionalsubsystems->add(new admin_setting_configselect( + 'messagingdeletereadnotificationsdelay', + new lang_string('messagingdeletereadnotificationsdelay', 'admin'), + new lang_string('configmessagingdeletereadnotificationsdelay', 'admin'), + 604800, + $options) + ); + $optionalsubsystems->add(new admin_setting_configselect( + 'messagingdeleteallnotificationsdelay', + new lang_string('messagingdeleteallnotificationsdelay', 'admin'), + new lang_string('configmessagingdeleteallnotificationsdelay', 'admin'), + 2620800, + $options) + ); $optionalsubsystems->add(new admin_setting_configcheckbox('messagingallowemailoverride', new lang_string('messagingallowemailoverride', 'admin'), new lang_string('configmessagingallowemailoverride','admin'), 0)); diff --git a/lang/en/admin.php b/lang/en/admin.php index 243344f2560e3..c2aad42f376cf 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -280,6 +280,7 @@ $string['configmessaging'] = 'If enabled, users can send messages to other users on the site.'; $string['configmessagingallowemailoverride'] = 'Allow users to have email message notifications sent to an email address other than the email address in their profile'; $string['configmessagingdeletereadnotificationsdelay'] = 'Read notifications can be deleted to save space. How long after a notification is read can it be deleted?'; +$string['configmessagingdeleteallnotificationsdelay'] = 'Read and unread notifications can be deleted to save space. How long after a notification is created can it be deleted?'; $string['configmessagingallusers'] = 'If enabled, users can view the list of all users on the site when selecting someone to message, and their message preferences include the option to accept messages from anyone on the site. If disabled, users can only view the list of users in their courses, and they have just two options in message preferences - to accept messages from their contacts only, or their contacts and anyone in their courses.'; $string['configminpassworddigits'] = 'Passwords must have at least these many digits.'; $string['configminpasswordlength'] = 'Passwords must be at least these many characters long.'; @@ -769,6 +770,7 @@ $string['messagingallowemailoverride'] = 'Notification email override'; $string['messagingallusers'] = 'Allow site-wide messaging'; $string['messagingdeletereadnotificationsdelay'] = 'Delete read notifications'; +$string['messagingdeleteallnotificationsdelay'] = 'Delete all notifications'; $string['minpassworddigits'] = 'Digits'; $string['minpasswordlength'] = 'Password length'; $string['minpasswordlower'] = 'Lowercase letters'; diff --git a/lib/classes/task/messaging_cleanup_task.php b/lib/classes/task/messaging_cleanup_task.php index 6d1a2c4d69f54..15b781f92908d 100644 --- a/lib/classes/task/messaging_cleanup_task.php +++ b/lib/classes/task/messaging_cleanup_task.php @@ -46,13 +46,18 @@ public function execute() { $timenow = time(); - // Cleanup messaging. + // Cleanup read and unread notifications. + if (!empty($CFG->messagingdeleteallnotificationsdelay)) { + $notificationdeletetime = $timenow - $CFG->messagingdeleteallnotificationsdelay; + $params = array('notificationdeletetime' => $notificationdeletetime); + $DB->delete_records_select('notifications', 'timecreated < :notificationdeletetime', $params); + } + + // Cleanup read notifications. if (!empty($CFG->messagingdeletereadnotificationsdelay)) { $notificationdeletetime = $timenow - $CFG->messagingdeletereadnotificationsdelay; $params = array('notificationdeletetime' => $notificationdeletetime); $DB->delete_records_select('notifications', 'timeread < :notificationdeletetime', $params); } - } - } diff --git a/mod/forum/tests/mail_test.php b/mod/forum/tests/mail_test.php index e96ba94cc17dd..088af18c87220 100644 --- a/mod/forum/tests/mail_test.php +++ b/mod/forum/tests/mail_test.php @@ -287,6 +287,7 @@ public function test_cron_message_includes_courseid() { // Reset the message sink for other tests. $this->helper->messagesink = $this->redirectMessages(); $event = reset($events); + $this->assertEquals($course->id, $event->other['courseid']); }