• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/lib/integration/forum/task/usertopic.php
  • Класс: BitrixTasksIntegrationForumTaskUserTopic
  • Вызов: UserTopic::readGroups
static function readGroups(int $userId, array $groupIds, bool $closedOnly = false): void
{
	if (!static::includeModule())
	{
		return;
	}

	$connection = Application::getConnection();
	$sqlHelper = $connection->getSqlHelper();

	$forumId = Comment::getForumId();
	$lastVisit = $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.FORUM_TOPIC_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 . ', ' . $forumId . ', ' . $lastVisit . ')';
	}

	$chunks = array_chunk($inserts, self::STEP_LIMIT);
	unset($inserts);

	foreach ($chunks as $chunk)
	{
		$sql = "
			INSERT INTO b_forum_user_topic (TOPIC_ID, USER_ID, FORUM_ID, LAST_VISIT)
			VALUES " . implode(',', $chunk) . "
			ON DUPLICATE KEY UPDATE LAST_VISIT = {$lastVisit}
		";
		$connection->query($sql);
	}
}