From 1e75faeaded0877d48af2ca3a20c64a2e84ffc42 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 15 Oct 2021 09:55:09 +0200 Subject: [PATCH] MDL-72586 message: Use count SQL in get_unread_notification_count --- .../external/get_unread_notification_count.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/message/classes/external/get_unread_notification_count.php b/message/classes/external/get_unread_notification_count.php index 402c70e13f9..65e02207c82 100644 --- a/message/classes/external/get_unread_notification_count.php +++ b/message/classes/external/get_unread_notification_count.php @@ -58,7 +58,7 @@ class get_unread_notification_count extends external_api { * @throws \moodle_exception */ public static function execute(int $useridto): int { - global $USER; + global $USER, $DB; $params = self::validate_parameters( self::execute_parameters(), @@ -83,9 +83,14 @@ class get_unread_notification_count extends external_api { throw new moodle_exception('accessdenied', 'admin'); } - $messages = message_get_messages($useridto, 0, 1, MESSAGE_GET_UNREAD); - - return count($messages); + return $DB->count_records_sql( + "SELECT COUNT(n.id) + FROM {notifications} n + LEFT JOIN {user} u ON (u.id = n.useridfrom AND u.deleted = 0) + WHERE n.useridto = ? + AND n.timeread IS NULL", + [$useridto], + ); } /**