• Модуль: intranet
  • Путь к файлу: ~/bitrix/modules/intranet/lib/ustat/ustat.php
  • Класс: BitrixIntranetUStatUStat
  • Вызов: UStat::incrementCounter
static function incrementCounter($section, $userId = null)
{
	// try to update
	// if no update for DAY table, then:
	//   check if user is absent today, then we need to update ACTIVE_USERS counters for depts and company
	// endthen;

	// check userId emptiness
	if (empty($userId))
	{
		// try to get current user id
		if (isset($GLOBALS['USER']) && is_object($GLOBALS['USER']) && $GLOBALS['USER'] instanceof CUser)
		{
			/** @var CUser[] $GLOBALS */
			$userId = (int) $GLOBALS['USER']->getId();
		}
		else
		{
			return false;
		}
	}

	// avoid bots
	if (MainLoader::includeModule('im'))
	{
		$botCache = BitrixImBot::getListCache();

		if (isset($botCache[$userId]))
		{
			return false;
		}
	}

	// check if user is in intranet and has a department
	$usersDepartments = static::getUsersDepartments();

	if (!isset($usersDepartments[$userId]))
	{
		return false;
	}

	// check if this counter has already been incremented during this hit
	if (isset(static::$affectedUsers[$userId][$section]))
	{
		return false;
	}
	static::$affectedUsers[$userId][$section] = true;

	// do increment
	$currentHour = new TypeDateTime(date('Y-m-d H:00:00'), 'Y-m-d H:00:00');

	// hourly stats
	$updResult = UserHourTable::update(
		array('USER_ID' => $userId, 'HOUR' => $currentHour),
		array($section => new SqlExpression('?# + 1', $section), 'TOTAL' => new SqlExpression('?# + 1', 'TOTAL'))
	);

	if (!$updResult->getAffectedRowsCount())
	{
		try
		{
			UserHourTable::add(array('USER_ID' => $userId, 'HOUR' => $currentHour, $section => 1, 'TOTAL' => 1));
		}
		catch (SqlException $e) {}
	}

	// daily stats
	$updResult = UserDayTable::update(
		array('USER_ID' => $userId, 'DAY' => $currentHour),
		array($section => new SqlExpression('?# + 1', $section), 'TOTAL' => new SqlExpression('?# + 1', 'TOTAL'))
	);

	if (!$updResult->getAffectedRowsCount())
	{
		try
		{
			UserDayTable::add(array('USER_ID' => $userId, 'DAY' => $currentHour, $section => 1, 'TOTAL' => 1));
		}
		catch (SqlException $e) {}

		// check if recounting ACTIVE_USERS is required
		$calendData = CIntranetUtils::getAbsenceData(array(
			'DATE_START' => ConvertTimeStamp(mktime(0, 0, 0), 'FULL'), // current day start
			'DATE_FINISH' => ConvertTimeStamp(mktime(23, 59, 59), 'FULL'), // current day end
			'USERS' => array($userId),
			'PER_USER' => false
		));

		$userAbsentsToday = static::checkTodayAbsence($calendData);


		if ($userAbsentsToday)
		{
			static::recountDeptartmentsActiveUsers($userId);
			static::recountCompanyActiveUsers();
		}
	}

	// get user departments
	$allUDepts = static::getUsersDepartments();
	$userDepts = $allUDepts[$userId];
	$departmentHitStat = new DepartmentHitStat($userDepts);
	$departmentHitStat->hour($section, $currentHour);
	$departmentHitStat->day($section, $currentHour);
}