Skip to content

Commit 20ec8e7

Browse files
Tracking: Improve message tracking (remove duplicate and add seeing sent/received) (#5237)
1 parent 4fb383d commit 20ec8e7

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

main/inc/lib/message.lib.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3049,6 +3049,57 @@ public static function getUsersThatHadConversationWithUser($userId, $startDate =
30493049
return $userList;
30503050
}
30513051

3052+
/**
3053+
* Retrieves a list of users with whom the specified user has exchanged messages within an optional date range.
3054+
*
3055+
* @param int $userId The user ID for whom to retrieve message exchange.
3056+
* @param string|null $startDate Start date to filter the messages (optional).
3057+
* @param string|null $endDate End date to filter the messages (optional).
3058+
*
3059+
* @return array Array of user information for each user with whom the specified user has exchanged messages.
3060+
*/
3061+
public static function getMessageExchangeWithUser($userId, $startDate = null, $endDate = null)
3062+
{
3063+
$messagesTable = Database::get_main_table(TABLE_MESSAGE);
3064+
$userId = (int) $userId;
3065+
3066+
if ($startDate !== null) {
3067+
$startDate = Database::escape_string($startDate);
3068+
}
3069+
if ($endDate !== null) {
3070+
$endDate = Database::escape_string($endDate);
3071+
}
3072+
3073+
$sql = "SELECT DISTINCT user_sender_id AS user_id
3074+
FROM $messagesTable
3075+
WHERE user_receiver_id = $userId" .
3076+
($startDate ? " AND send_date >= '$startDate'" : "") .
3077+
($endDate ? " AND send_date <= '$endDate'" : "") .
3078+
" UNION
3079+
SELECT DISTINCT user_receiver_id
3080+
FROM $messagesTable
3081+
WHERE user_sender_id = $userId" .
3082+
($startDate ? " AND send_date >= '$startDate'" : "") .
3083+
($endDate ? " AND send_date <= '$endDate'" : "");
3084+
3085+
$result = Database::query($sql);
3086+
$users = Database::store_result($result);
3087+
3088+
$userList = [];
3089+
foreach ($users as $userData) {
3090+
$userId = $userData['user_id'];
3091+
if (empty($userId)) {
3092+
continue;
3093+
}
3094+
$userInfo = api_get_user_info($userId);
3095+
if ($userInfo) {
3096+
$userList[$userId] = $userInfo;
3097+
}
3098+
}
3099+
3100+
return $userList;
3101+
}
3102+
30523103
/**
30533104
* @param int $userId
30543105
* @param int $otherUserId

main/mySpace/myStudents.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,11 +2405,11 @@ function ($hookHeader) {
24052405
$allow = api_get_configuration_value('allow_user_message_tracking');
24062406
if ($allow && (api_is_drh() || api_is_platform_admin())) {
24072407
if ($filterMessages) {
2408-
$users = MessageManager::getUsersThatHadConversationWithUser($student_id, $coachAccessStartDate, $coachAccessEndDate);
2408+
$users = MessageManager::getMessageExchangeWithUser($student_id, $coachAccessStartDate, $coachAccessEndDate);
24092409
} else {
2410-
$users = MessageManager::getUsersThatHadConversationWithUser($student_id);
2410+
$users = MessageManager::getMessageExchangeWithUser($student_id);
24112411
}
2412-
$users = MessageManager::getUsersThatHadConversationWithUser($student_id);
2412+
24132413
echo Display::page_subheader2(get_lang('MessageTracking'));
24142414

24152415
$table = new HTML_Table(['class' => 'table']);

0 commit comments

Comments
 (0)