• Модуль: intranet
  • Путь к файлу: ~/bitrix/modules/intranet/lib/ustat/ustat.php
  • Класс: BitrixIntranetUStatUStat
  • Вызов: UStat::recountDeptartmentsActiveUsers
static function recountDeptartmentsActiveUsers($forUserId = null)
{
	$updates = array();

	list($deptData, $users) = static::getActivityInfo();

	// prepare data
	if (!empty($forUserId))
	{
		foreach ($deptData as $deptId => $department)
		{
			if (in_array($forUserId, $department['EMPLOYEES']))
			{
				$updates[$deptId] = $department['ACTIVE_USERS'];
			}
		}
	}
	else
	{
		foreach ($deptData as $deptId => $department)
		{
			$updates[$deptId] = $department['ACTIVE_USERS'];
		}
	}

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

	foreach ($updates as $deptId => $activeUsersCount)
	{
		$updResult = DepartmentDayTable::update(array('DEPT_ID' => $deptId, 'DAY' => $currentHour), array('ACTIVE_USERS' => $activeUsersCount));

		if (!$updResult->getAffectedRowsCount())
		{
			// if new ACTIVE_USERS value equal one in DB, affectedRows will return 0
			// in this case ignore duplicate entry error while trying to insert same values
			try
			{
				DepartmentDayTable::add(array('DEPT_ID' => $deptId, 'DAY' => $currentHour, 'ACTIVE_USERS' => $activeUsersCount));
			}
			catch (SqlException $e) {}
		}
	}
}