/** * Returns A/B-test related attribute types * * @return array */ public static function onGetAttributeTypes() { $sections = array('A' => array('NAME' => Localization\Loc::getMessage('ABTEST_CONV_TEST_SECTION_A_NAME')), 'B' => array('NAME' => Localization\Loc::getMessage('ABTEST_CONV_TEST_SECTION_B_NAME'))); return array('abtest' => array('MODULE' => 'abtest', 'NAME' => Localization\Loc::getMessage('ABTEST_CONVATTR_TEST_NAME'), 'SORT' => 5000, 'GET_VALUES' => function (array $ids) { $result = ABTestTable::getList(array('select' => array('ID', 'NAME'), 'filter' => array('ID' => $ids), 'order' => array('SORT' => 'ASC'))); $values = array(); while ($abtest = $result->fetch()) { if (empty($abtest['NAME'])) { $abtest['NAME'] = str_replace('#ID#', $abtest['ID'], Localization\Loc::getMessage('ABTEST_CONV_TEST_TITLE')); } $values[$abtest['ID']] = array('NAME' => $abtest['NAME']); } return $values; }), 'abtest_section' => array('MODULE' => 'abtest', 'NAME' => Localization\Loc::getMessage('ABTEST_CONVATTR_TEST_SECTION_NAME'), 'SORT' => 5100, 'GET_VALUES' => function (array $ids) use($sections) { $values = array(); foreach ($ids as $id) { if (!empty($sections[$id])) { $values[$id] = $sections[$id]; } } return $values; })); }
/** * Returns current A/B-test context * * @return array|null */ public static function getContext() { global $USER, $APPLICATION; static $context; if (!defined('SITE_ID') || !SITE_ID) { return null; } if (empty($context)) { $activeTest = Helper::getActiveTest(); if ($USER->canDoOperation('view_other_settings') && !empty($_SESSION['ABTEST_MODE'])) { if ($_SESSION['ABTEST_MODE'] == 'reset') { if (!empty($activeTest)) { $context = Helper::context($activeTest, 'N'); } unset($_SESSION['ABTEST_MODE']); } else { if (preg_match('/^(\\d+)\\|(A|B|N)$/', $_SESSION['ABTEST_MODE'], $matches)) { if (!empty($activeTest) && $activeTest['ID'] == intval($matches[1])) { $context = Helper::context($activeTest, $matches[2]); unset($_SESSION['ABTEST_MODE']); } else { $abtest = ABTestTable::getList(array('filter' => array('=ID' => intval($matches[1]), 'ENABLED' => 'Y')))->fetch(); if (!empty($abtest) && $abtest['SITE_ID'] == SITE_ID) { $context = Helper::context($abtest, $matches[2]); } } } } } if (empty($context) && !empty($activeTest)) { $abtest = $activeTest; if ($cookie = $APPLICATION->get_cookie('ABTEST_' . SITE_ID)) { if (preg_match('/^' . intval($abtest['ID']) . '\\|(A|B|N)$/i', $cookie, $matches)) { $section = $matches[1]; } } if (empty($section)) { $dice = mt_rand(1, 100); if ($dice <= intval($abtest['PORTION']) / 2) { $section = 'A'; } else { if ($dice <= intval($abtest['PORTION'])) { $section = 'B'; } else { $section = 'N'; } } } $context = Helper::context($abtest, $section); } if (empty($activeTest)) { $APPLICATION->set_cookie('ABTEST_' . SITE_ID, null); } else { if ($activeTest['ID'] == $context['abtest']) { $APPLICATION->set_cookie('ABTEST_' . SITE_ID, intval($context['abtest']) . '|' . $context['section']); } } } return $context; }