From 20ec8e75c5c4f780b411eb0b30eda8b5683d69d5 Mon Sep 17 00:00:00 2001 From: juancp-contidosdixitais <93096561+juancp-contidosdixitais@users.noreply.github.com> Date: Sat, 19 Oct 2024 01:19:12 +0200 Subject: [PATCH] Tracking: Improve message tracking (remove duplicate and add seeing sent/received) (#5237) --- main/inc/lib/message.lib.php | 51 ++++++++++++++++++++++++++++++++++++ main/mySpace/myStudents.php | 6 ++--- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/main/inc/lib/message.lib.php b/main/inc/lib/message.lib.php index 21c380d3c8e..584e370c7c9 100755 --- a/main/inc/lib/message.lib.php +++ b/main/inc/lib/message.lib.php @@ -3049,6 +3049,57 @@ public static function getUsersThatHadConversationWithUser($userId, $startDate = return $userList; } + /** + * Retrieves a list of users with whom the specified user has exchanged messages within an optional date range. + * + * @param int $userId The user ID for whom to retrieve message exchange. + * @param string|null $startDate Start date to filter the messages (optional). + * @param string|null $endDate End date to filter the messages (optional). + * + * @return array Array of user information for each user with whom the specified user has exchanged messages. + */ + public static function getMessageExchangeWithUser($userId, $startDate = null, $endDate = null) + { + $messagesTable = Database::get_main_table(TABLE_MESSAGE); + $userId = (int) $userId; + + if ($startDate !== null) { + $startDate = Database::escape_string($startDate); + } + if ($endDate !== null) { + $endDate = Database::escape_string($endDate); + } + + $sql = "SELECT DISTINCT user_sender_id AS user_id + FROM $messagesTable + WHERE user_receiver_id = $userId" . + ($startDate ? " AND send_date >= '$startDate'" : "") . + ($endDate ? " AND send_date <= '$endDate'" : "") . + " UNION + SELECT DISTINCT user_receiver_id + FROM $messagesTable + WHERE user_sender_id = $userId" . + ($startDate ? " AND send_date >= '$startDate'" : "") . + ($endDate ? " AND send_date <= '$endDate'" : ""); + + $result = Database::query($sql); + $users = Database::store_result($result); + + $userList = []; + foreach ($users as $userData) { + $userId = $userData['user_id']; + if (empty($userId)) { + continue; + } + $userInfo = api_get_user_info($userId); + if ($userInfo) { + $userList[$userId] = $userInfo; + } + } + + return $userList; + } + /** * @param int $userId * @param int $otherUserId diff --git a/main/mySpace/myStudents.php b/main/mySpace/myStudents.php index 6c2b92acf18..73fef8f1a3d 100755 --- a/main/mySpace/myStudents.php +++ b/main/mySpace/myStudents.php @@ -2405,11 +2405,11 @@ function ($hookHeader) { $allow = api_get_configuration_value('allow_user_message_tracking'); if ($allow && (api_is_drh() || api_is_platform_admin())) { if ($filterMessages) { - $users = MessageManager::getUsersThatHadConversationWithUser($student_id, $coachAccessStartDate, $coachAccessEndDate); + $users = MessageManager::getMessageExchangeWithUser($student_id, $coachAccessStartDate, $coachAccessEndDate); } else { - $users = MessageManager::getUsersThatHadConversationWithUser($student_id); + $users = MessageManager::getMessageExchangeWithUser($student_id); } - $users = MessageManager::getUsersThatHadConversationWithUser($student_id); + echo Display::page_subheader2(get_lang('MessageTracking')); $table = new HTML_Table(['class' => 'table']);