• Модуль: abtest
  • Путь к файлу: ~/bitrix/modules/abtest/lib/helper.php
  • Класс: BitrixABTestHelper
  • Вызов: Helper::getActiveTest
static function getActiveTest()
{
	static $abtest;
	static $defined;

	if (!defined('SITE_ID') || !SITE_ID)
		return null;

	if (empty($defined))
	{
		$cache = new CPHPCache();

		if ($cache->initCache(30*24*3600, 'abtest_active_'.SITE_ID, 'abtest/'))
		{
			$abtest = $cache->getVars();
		}
		else
		{
			$abtest = ABTestTable::getList(array(
				'order' => array('SORT' => 'ASC'),
				'filter' => array(
					'=SITE_ID'      => SITE_ID,
					'=ACTIVE'       => 'Y',
					'<=START_DATE' => new TypeDateTime()
				),
				'limit' => 1
			))->fetch() ?: [];

			$cache->startDataCache();
			$cache->endDataCache($abtest);
		}

		$defined = true;

		if (!empty($abtest))
		{
			if (!$abtest['MIN_AMOUNT'])
			{
				$capacity = AdminHelper::getSiteCapacity($abtest['SITE_ID']);
				if ($capacity['min'] > 0)
				{
					$result = ABTestTable::update($abtest['ID'], array('MIN_AMOUNT' => $capacity['min']));
					if ($result->isSuccess())
					{
						$cache->clean('abtest_active_'.SITE_ID, '/abtest');
						$abtest['MIN_AMOUNT'] = $capacity['min'];
					}
				}
			}

			if (intval($abtest['DURATION']) == -1)
			{
				if (intval($abtest['MIN_AMOUNT']) > 0)
				{
					$capacity = AdminHelper::getTestCapacity($abtest['ID']);
					if ($capacity['A'] >= $abtest['MIN_AMOUNT'] && $capacity['B'] >= $abtest['MIN_AMOUNT'])
					{
						Helper::stopTest($abtest['ID'], true);
						$abtest = null;
					}
				}
			}
			else if (intval($abtest['DURATION']) > 0)
			{
				$end = clone $abtest['START_DATE'];
				$end->add(intval($abtest['DURATION']).' days');

				if (time() > $end->format('U'))
				{
					Helper::stopTest($abtest['ID'], true);
					$abtest = null;
				}
			}
		}
	}

	return $abtest;
}