21 if (!defined(
'SITE_ID') || !SITE_ID)
26 $cache = new \CPHPCache();
28 if ($cache->initCache(30*24*3600,
'abtest_active_'.SITE_ID,
'abtest/'))
30 $abtest = $cache->getVars();
34 $abtest = ABTestTable::getList(array(
35 'order' => array(
'SORT' =>
'ASC'),
37 '=SITE_ID' => SITE_ID,
44 $cache->startDataCache();
45 $cache->endDataCache($abtest);
52 if (!$abtest[
'MIN_AMOUNT'])
55 if ($capacity[
'min'] > 0)
57 $result = ABTestTable::update($abtest[
'ID'], array(
'MIN_AMOUNT' => $capacity[
'min']));
58 if ($result->isSuccess())
60 $cache->clean(
'abtest_active_'.SITE_ID,
'/abtest');
61 $abtest[
'MIN_AMOUNT'] = $capacity[
'min'];
66 if (intval($abtest[
'DURATION']) == -1)
68 if (intval($abtest[
'MIN_AMOUNT']) > 0)
71 if ($capacity[
'A'] >= $abtest[
'MIN_AMOUNT'] && $capacity[
'B'] >= $abtest[
'MIN_AMOUNT'])
78 else if (intval($abtest[
'DURATION']) > 0)
80 $end = clone $abtest[
'START_DATE'];
81 $end->add(intval($abtest[
'DURATION']).
' days');
83 if (time() > $end->format(
'U'))
118 global $USER, $APPLICATION;
122 if (!defined(
'SITE_ID') || !SITE_ID)
129 $isAbtestAdmin = is_object($USER) && $USER->canDoOperation(
'view_other_settings');
130 if ($isAbtestAdmin && !empty($_SESSION[
'ABTEST_MODE']))
132 if ($_SESSION[
'ABTEST_MODE'] ==
'reset')
134 if (!empty($activeTest))
135 $context = Helper::context($activeTest,
'N');
137 unset($_SESSION[
'ABTEST_MODE']);
139 else if (preg_match(
'/^(\d+)\|(A|B|N)$/', $_SESSION[
'ABTEST_MODE'], $matches))
141 if (!empty($activeTest) && $activeTest[
'ID'] == intval($matches[1]))
143 $context = Helper::context($activeTest, $matches[2]);
145 unset($_SESSION[
'ABTEST_MODE']);
149 $abtest = ABTestTable::getList(array(
150 'filter' => array(
'=ID' => intval($matches[1]),
'ENABLED' =>
'Y')
153 if (!empty($abtest) && $abtest[
'SITE_ID'] == SITE_ID)
154 $context = Helper::context($abtest, $matches[2]);
159 $cookieValue = $APPLICATION->get_cookie(
'ABTEST_'.SITE_ID);
161 if (empty($context) && !empty($activeTest))
163 $abtest = $activeTest;
165 if (!empty($cookieValue))
167 if (preg_match(
'/^'.intval($abtest[
'ID']).
'\|(A|B|N)$/i', $cookieValue, $matches))
168 $section = $matches[1];
173 $dice = mt_rand(1, 100);
175 if ($dice <= intval($abtest[
'PORTION'])/2)
177 else if ($dice <= intval($abtest[
'PORTION']))
183 $context = Helper::context($abtest, $section);
186 if (empty($activeTest) || $activeTest[
'ID'] == $context[
'abtest'])
188 $newValue = empty($activeTest) ? null : sprintf(
'%u|%s', $context[
'abtest'], $context[
'section']);
189 if (!(empty($cookieValue) && empty($newValue)) && $cookieValue !== $newValue)
191 $cookie =
new Main\Web\Cookie(sprintf(
'ABTEST_%s', SITE_ID), $newValue);
193 Main\Context::getCurrent()->getResponse()->addCookie($cookie);