diff --git a/classes/hook_callbacks.php b/classes/hook_callbacks.php index a55ebba..2d7e215 100644 --- a/classes/hook_callbacks.php +++ b/classes/hook_callbacks.php @@ -66,115 +66,6 @@ public static function after_config(\core\hook\after_config $hook) { return; } - try { - global $SESSION, $USER; - - // Setup experiment manager. - $manager = new \tool_abconfig_experiment_manager(); - - // Check if the param to disable ABconfig is present, if so, exit. - if (!optional_param('abconfig', true, PARAM_BOOL)) { - if (is_siteadmin()) { - return null; - } - } - - // Get all experiments. - $experiments = $manager->get_experiments(); - foreach ($experiments as $experiment => $contents) { - - if (defined('CLI_SCRIPT') && CLI_SCRIPT) { - // Check ENV vars set on the cli. - $condition = getenv('ABCONFIG_' . strtoupper($experiment)); - } else { - - // Check URL params, and fire any experiments in the params. - $condition = optional_param($experiment, null, PARAM_TEXT); - - // Only admins can fire additional experiments. - if (!is_siteadmin()) { - break; - } - } - - if (empty($condition)) { - continue; - } - - // Ensure condition set exists before executing. - if (array_key_exists($condition, $contents['conditions'])) { - tool_abconfig_execute_command_array($contents['conditions'][$condition]['commands'], - $contents['shortname']); - } - } - - $commandarray = array(); - - // First, Build a list of all commands that need to be executed. - - // Start with request scope. - $requestexperiments = $manager->get_active_request(); - if (!empty($requestexperiments)) { - foreach ($requestexperiments as $record) { - - // Make admin immune unless enabled for admin. - if (is_siteadmin()) { - if ($record['adminenabled'] == 0) { - continue; - } - } - - $conditionrecords = $record['conditions']; - - // Remove all conditions that contain the user ip in the whitelist. - $crecords = array(); - - foreach ($conditionrecords as $conditionrecord) { - $iplist = $conditionrecord['ipwhitelist']; - $users = !empty($conditionrecord['users']) ? json_decode($conditionrecord['users']) : []; - if (empty($users) || in_array($USER->id, $users)) { - if (!remoteip_in_list($iplist)) { - array_push($crecords, $conditionrecord); - } - } - } - - // Increment through conditions until one is selected. - $condition = ''; - $num = rand(1, 100); - $prevtotal = 0; - foreach ($crecords as $crecord) { - // If random number is within this range, set condition and break, else increment total. - if ($num > $prevtotal && $num <= ($prevtotal + $crecord['value'])) { - $commandarray[$record['shortname']] = $crecord['commands']; - // Do not select any more conditions. - break; - } else { - // Not this record, increment lower bound, and move on. - $prevtotal += $crecord['value']; - } - } - } - } - - // Now session scope. - $sessionexperiments = $manager->get_active_session(); - if (!empty($sessionexperiments)) { - foreach ($sessionexperiments as $record) { - // Check if a session var has been set for this experiment, only care if has been set. - $unique = 'abconfig_'.$record['shortname']; - if (property_exists($SESSION, $unique) && $SESSION->$unique != '') { - $commandarray[$record['shortname']] = $record['conditions'][$SESSION->$unique]['commands']; - } - } - } - - // Now, execute all commands in the arrays. - foreach ($commandarray as $shortname => $command) { - tool_abconfig_execute_command_array($command, $shortname); - } - } catch (\Exception $e) { // @codingStandardsIgnoreStart - // Catch exceptions from stuff not existing during installation process, fail silently - } // @codingStandardsIgnoreEnd + tool_abconfig_after_config(); } }