Skip to content

Commit

Permalink
MDL-56389 message: allow marking notifications as read up to given time.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Jan 23, 2020
1 parent fd12600 commit df2544e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
7 changes: 6 additions & 1 deletion message/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1730,9 +1730,10 @@ public static function mark_all_messages_as_read($userid, $conversationid = null
*
* @param int $touserid the id of the message recipient
* @param int|null $fromuserid the id of the message sender, null if all messages
* @param int|null $timecreatedto mark notifications created before this time as read
* @return void
*/
public static function mark_all_notifications_as_read($touserid, $fromuserid = null) {
public static function mark_all_notifications_as_read($touserid, $fromuserid = null, $timecreatedto = null) {
global $DB;

$notificationsql = "SELECT n.*
Expand All @@ -1744,6 +1745,10 @@ public static function mark_all_notifications_as_read($touserid, $fromuserid = n
$notificationsql .= " AND useridfrom = ?";
$notificationsparams[] = $fromuserid;
}
if (!empty($timecreatedto)) {
$notificationsql .= " AND timecreated <= ?";
$notificationsparams[] = $timecreatedto;
}

$notifications = $DB->get_recordset_sql($notificationsql, $notificationsparams);
foreach ($notifications as $notification) {
Expand Down
10 changes: 8 additions & 2 deletions message/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3255,6 +3255,9 @@ public static function mark_all_notifications_as_read_parameters() {
'useridfrom' => new external_value(
PARAM_INT, 'the user id who send the message, 0 for any user. -10 or -20 for no-reply or support user',
VALUE_DEFAULT, 0),
'timecreatedto' => new external_value(
PARAM_INT, 'mark messages created before this time as read, 0 for all messages',
VALUE_DEFAULT, 0),
)
);
}
Expand All @@ -3267,16 +3270,18 @@ public static function mark_all_notifications_as_read_parameters() {
* @throws moodle_exception
* @param int $useridto the user id who received the message
* @param int $useridfrom the user id who send the message. -10 or -20 for no-reply or support user
* @param int $timecreatedto mark message created before this time as read, 0 for all messages
* @return external_description
*/
public static function mark_all_notifications_as_read($useridto, $useridfrom) {
public static function mark_all_notifications_as_read($useridto, $useridfrom, $timecreatedto = 0) {
global $USER;

$params = self::validate_parameters(
self::mark_all_notifications_as_read_parameters(),
array(
'useridto' => $useridto,
'useridfrom' => $useridfrom,
'timecreatedto' => $timecreatedto,
)
);

Expand All @@ -3285,6 +3290,7 @@ public static function mark_all_notifications_as_read($useridto, $useridfrom) {

$useridto = $params['useridto'];
$useridfrom = $params['useridfrom'];
$timecreatedto = $params['timecreatedto'];

if (!empty($useridto)) {
if (core_user::is_real_user($useridto)) {
Expand All @@ -3306,7 +3312,7 @@ public static function mark_all_notifications_as_read($useridto, $useridfrom) {
throw new moodle_exception('accessdenied', 'admin');
}

\core_message\api::mark_all_notifications_as_read($useridto, $useridfrom);
\core_message\api::mark_all_notifications_as_read($useridto, $useridfrom, $timecreatedto);

return true;
}
Expand Down
40 changes: 40 additions & 0 deletions message/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,46 @@ public function test_mark_all_notifications_as_read() {
$this->assertCount(0, $unreadnotifications);
}

public function test_mark_all_notifications_as_read_time_created_to() {
global $DB;

$this->resetAfterTest(true);

$sender1 = $this->getDataGenerator()->create_user();
$sender2 = $this->getDataGenerator()->create_user();

$recipient = $this->getDataGenerator()->create_user();
$this->setUser($recipient);

// Record messages as sent on one second intervals.
$time = time();

$this->send_message($sender1, $recipient, 'Message 1', 1, $time);
$this->send_message($sender2, $recipient, 'Message 2', 1, $time + 1);
$this->send_message($sender1, $recipient, 'Message 3', 1, $time + 2);
$this->send_message($sender2, $recipient, 'Message 4', 1, $time + 3);

// Mark notifications sent from sender1 up until the second message; should only mark the first notification as read.
core_message_external::mark_all_notifications_as_read($recipient->id, $sender1->id, $time + 1);

$params = [$recipient->id];

$this->assertEquals(1, $DB->count_records_select('notifications', 'useridto = ? AND timeread IS NOT NULL', $params));
$this->assertEquals(3, $DB->count_records_select('notifications', 'useridto = ? AND timeread IS NULL', $params));

// Mark all notifications as read from any sender up to the time the third message was sent.
core_message_external::mark_all_notifications_as_read($recipient->id, 0, $time + 2);

$this->assertEquals(3, $DB->count_records_select('notifications', 'useridto = ? AND timeread IS NOT NULL', $params));
$this->assertEquals(1, $DB->count_records_select('notifications', 'useridto = ? AND timeread IS NULL', $params));

// Mark all notifications as read from any sender with a time after all messages were sent.
core_message_external::mark_all_notifications_as_read($recipient->id, 0, $time + 10);

$this->assertEquals(4, $DB->count_records_select('notifications', 'useridto = ? AND timeread IS NOT NULL', $params));
$this->assertEquals(0, $DB->count_records_select('notifications', 'useridto = ? AND timeread IS NULL', $params));
}

/**
* Test get_user_notification_preferences
*/
Expand Down

0 comments on commit df2544e

Please sign in to comment.