• Модуль: intranet
  • Путь к файлу: ~/bitrix/modules/intranet/lib/ustat/ustat.php
  • Класс: BitrixIntranetUStatUStat
  • Вызов: UStat::getSectionsSummaryInvolvement
static function getSectionsSummaryInvolvement(TypeDateTime $dateFrom, TypeDateTime $dateTo, $interval)
{
	if (!in_array($interval, array('hour', 'day', 'month'), true))
	{
		throw new MainArgumentException('Interval should be the "hour", or "day", or "month".');
	}

	if ($interval === 'hour')
	{
		$entity = UserHourTable::getEntity();

		$filter = array(
			'> array(
				ConvertTimeStamp($dateFrom->getTimestamp(), 'FULL'),
				ConvertTimeStamp($dateTo->getTimestamp(), 'FULL')
			)
		);
	}
	else
	{
		$entity = UserDayTable::getEntity();

		$filter = array(
			'> array(
				ConvertTimeStamp($dateFrom->getTimestamp()),
				ConvertTimeStamp($dateTo->getTimestamp())
			)
		);
	}

	$query = new EntityQuery($entity);

	foreach (UserHourTable::getSectionNames() as $sectionName)
	{
		$query->addSelect(new EntityExpressionField(
			$sectionName.'_IS_USED', 'CASE WHEN SUM(%s) > 0 THEN 1 ELSE 0 END', $sectionName
		));
	}

	$query->setFilter($filter);
	$query->setGroup('USER_ID');

	// ^ there was stats by user. now summarize it
	$finalQuery = new EntityQuery($query);

	$finalQuery->addSelect(new EntityExpressionField('USERS_COUNT', 'COUNT(1)'));

	foreach (UserHourTable::getSectionNames() as $sectionName)
	{
		$finalQuery->addSelect(new EntityExpressionField($sectionName.'_USAGE', 'SUM(%s)', $sectionName.'_IS_USED'));
	}

	$result = $finalQuery->exec();
	$stats = $result->fetch();

	if ($interval === 'hour')
	{
		// recount unique users from DAILY stats,
		// because there are empty records for each user
		$query = new EntityQuery(UserDayTable::getEntity());

		$query->addSelect(new EntityExpressionField('USERS_COUNT', 'COUNT(DISTINCT %s)', 'USER_ID'));

		$query->setFilter(array(
			'> array(
				ConvertTimeStamp($dateFrom->getTimestamp()),
				ConvertTimeStamp($dateTo->getTimestamp())
			)
		));

		$result = $query->exec();
		$row = $result->fetch();

		$stats['USERS_COUNT'] = $row['USERS_COUNT'];
	}

	return $stats;
}