- Модуль: tasks
- Путь к файлу: ~/bitrix/modules/tasks/lib/internals/task/viewed.php
- Класс: BitrixTasksInternalsTaskViewedTable
- Вызов: ViewedTable::readGroups
static function readGroups(int $userId, array $groupIds, bool $closedOnly = false): void
{
$connection = Application::getConnection();
$sqlHelper = $connection->getSqlHelper();
$viewedDate = $sqlHelper->convertToDbDateTime(new DateTime());
$intGroupIds = array_map(function($el) {
return (int) $el;
}, $groupIds);
$condition = [];
if (count($groupIds) === 1)
{
$condition[] = 'T.GROUP_ID = '. array_shift($groupIds);
}
else
{
$condition[] = 'T.GROUP_ID IN ('. implode(",", $intGroupIds) .')';
}
if ($closedOnly)
{
$condition[] = 'T.STATUS = '. Status::COMPLETED;
}
$condition[] = 'TV.VIEWED_DATE IS NULL';
$condition[] = 'FM.POST_DATE >= T.CREATED_DATE';
$condition[] = 'FM.NEW_TOPIC = 'N'';
$condition = '(' . implode(') AND (', $condition) . ')';
$sql = "
SELECT DISTINCT T.ID as ID
FROM b_tasks T
LEFT JOIN b_tasks_viewed TV ON TV.TASK_ID = T.ID AND TV.USER_ID = {$userId}
LEFT JOIN b_forum_message FM ON FM.TOPIC_ID = T.FORUM_TOPIC_ID
WHERE
{$condition}
";
$res = $connection->query($sql);
$inserts = [];
while ($row = $res->fetch())
{
$inserts[] = '(' . (int)$row['ID'] . ', ' . $userId . ', ' . $viewedDate . ')';
}
$chunks = array_chunk($inserts, self::STEP_LIMIT);
unset($inserts);
foreach ($chunks as $chunk)
{
$sql = "
INSERT INTO b_tasks_viewed (TASK_ID, USER_ID, VIEWED_DATE)
VALUES " . implode(',', $chunk) . "
ON DUPLICATE KEY UPDATE VIEWED_DATE = {$viewedDate}
";
$connection->query($sql);
}
}