Skip to content

Commit

Permalink
Tracking: Improve message tracking (remove duplicate and add seeing s…
Browse files Browse the repository at this point in the history
…ent/received) (#5237)
  • Loading branch information
juancp-contidosdixitais authored Oct 18, 2024
1 parent 4fb383d commit 20ec8e7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
51 changes: 51 additions & 0 deletions main/inc/lib/message.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions main/mySpace/myStudents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down

0 comments on commit 20ec8e7

Please sign in to comment.