• Модуль: intranet
  • Путь к файлу: ~/bitrix/modules/intranet/lib/ustat/ustat.php
  • Класс: BitrixIntranetUStatUStat
  • Вызов: UStat::recountCompanyActiveUsers
static function recountCompanyActiveUsers()
{
	// if no record for today, then
	//  - update last record involment before today (usually yesterday)
	//  - insert new record for today
	// else
	//  - update record

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

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

	// today active users
	$activeUsers = array();

	foreach ($users as $k => $user)
	{
		if (!$user['ABSENT'])
		{
			$activeUsers[$k] = $user;
		}
	}

	// current record
	$todayRow = DepartmentDayTable::getByPrimary(array('DEPT_ID' => 0, 'DAY' => ConvertTimeStamp(time(), "SHORT")))->fetch();

	// if no record for today, then
	if (empty($todayRow))
	{
		// today is a new day!

		// update last record involvement before today (usually yesterday)
		$lastRow = DepartmentDayTable::getRow(array(
			'filter' => array('=DEPT_ID' => 0, ' ConvertTimeStamp(time(), "SHORT")),
			'order' => array('DAY' => 'DESC'),
			'limit' => 1
		));

		if (!empty($lastRow))
		{
			$lastRowDate = is_object($lastRow['DAY']) ? $lastRow['DAY'] : new TypeDate($lastRow['DAY'], 'Y-m-d');
			static::recountDailyInvolvement($lastRowDate);
		}

		// insert new record for today
		try
		{
			DepartmentDayTable::add(array('DEPT_ID' => 0, 'DAY' => $currentDay, 'ACTIVE_USERS' => count($activeUsers)));
		}
		catch (SqlException $e) {}

		// insert empty users row for unique users stats (we need it for instrument's involvement)
		foreach (array_keys($activeUsers) as $userId)
		{
			try
			{
				UserDayTable::add(array('USER_ID' => $userId, 'DAY' => $currentDay));
			}
			catch (SqlException $e) {}
		}
	}
	else
	{
		// update current record
		if ($todayRow['ACTIVE_USERS'] != count($activeUsers))
		{
			DepartmentDayTable::update(array('DEPT_ID' => 0, 'DAY' => $currentDay), array('ACTIVE_USERS' => count($activeUsers)));
		}
	}
}