• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/classes/general/user.php
  • Класс: CAllUser
  • Вызов: CAllUser::GetOnlineStatus
static function GetOnlineStatus($userId, $lastseen, $now = false)
{
	$userId = intval($userId);

	if ($lastseen instanceof MainTypeDateTime)
	{
		$lastseen = $lastseen->getTimestamp();
	}
	else
	{
		if (is_int($lastseen))
		{
			$lastseen = intval($lastseen);
		}
		else
		{
			$lastseen = 0;
		}
	}

	if ($now === false)
	{
		$now = time();
	}
	else
	{
		if ($now instanceof MainTypeDateTime)
		{
			$now = $now->getTimestamp();
		}
		else
		{
			$now = intval($now);
		}
	}

	$result = [
		'IS_ONLINE' => false,
		'STATUS' => self::STATUS_OFFLINE,
		'STATUS_TEXT' => GetMessage('USER_STATUS_OFFLINE'),
		'LAST_SEEN' => $lastseen,
		'LAST_SEEN_TEXT' => '',
		'NOW' => $now,
	];

	if ($lastseen === false)
	{
		return $result;
	}

	$result['IS_ONLINE'] = $now - $lastseen <= static::GetSecondsForLimitOnline();
	$result['STATUS'] = $result['IS_ONLINE'] ? self::STATUS_ONLINE : self::STATUS_OFFLINE;
	$result['STATUS_TEXT'] = GetMessage('USER_STATUS_' . strtoupper($result['STATUS']));

	if ($lastseen && $now - $lastseen > 300)
	{
		$result['LAST_SEEN_TEXT'] = static::FormatLastActivityDate($lastseen, $now);
	}

	if ($userId > 0)
	{
		if ($result['IS_ONLINE'])
		{
			foreach (GetModuleEvents('main', 'OnUserOnlineStatusGetCustomOnlineStatus', true) as $arEvent)
			{
				$customStatus = ExecuteModuleEventEx($arEvent, [$userId, $lastseen, $now, self::STATUS_ONLINE]);
				if (is_array($customStatus))
				{
					if (!empty($customStatus['STATUS']) && !empty($customStatus['STATUS_TEXT']))
					{
						$result['STATUS'] = strtolower($customStatus['STATUS']);
						$result['STATUS_TEXT'] = $customStatus['STATUS_TEXT'];
					}
					if (isset($customStatus['LAST_SEEN']) && intval($customStatus['LAST_SEEN']) > 0)
					{
						$result['LAST_SEEN'] = intval($customStatus['LAST_SEEN']);
					}
					if (isset($customStatus['LAST_SEEN_TEXT']))
					{
						$result['LAST_SEEN_TEXT'] = $customStatus['LAST_SEEN_TEXT'];
					}
				}
			}
		}
		else
		{
			foreach (GetModuleEvents('main', 'OnUserOnlineStatusGetCustomOfflineStatus', true) as $arEvent)
			{
				$customStatus = ExecuteModuleEventEx($arEvent, [$userId, $lastseen, $now, self::STATUS_OFFLINE]);
				if (is_array($customStatus))
				{
					if (!empty($customStatus['STATUS']) && !empty($customStatus['STATUS_TEXT']))
					{
						$result['STATUS'] = strtolower($customStatus['STATUS']);
						$result['STATUS_TEXT'] = $customStatus['STATUS_TEXT'];
					}
					if (isset($customStatus['LAST_SEEN']) && intval($customStatus['LAST_SEEN']) > 0)
					{
						$result['LAST_SEEN'] = intval($customStatus['LAST_SEEN']);
					}
					if (isset($customStatus['LAST_SEEN_TEXT']))
					{
						$result['LAST_SEEN_TEXT'] = $customStatus['LAST_SEEN_TEXT'];
					}
				}
			}
		}
	}

	return $result;
}