Skip to content

Commit

Permalink
Allow param previews of device experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
bwalkerl committed Jan 21, 2025
1 parent ab2d43e commit 7564d7a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
36 changes: 35 additions & 1 deletion classes/experiment_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,40 @@ public function get_experiments() {
return ($experiments != false) ? $experiments : array();
}

/**
* Get all experiments that should be run before session
* @return mixed
*/
public function get_before_session_experiments() {
$experiments = self::get_experiments();

// Filter array for all before session experiments.
return array_filter($experiments, function ($experiment) {
if ($experiment['scope'] == 'device') {
return true;
} else {
return false;
}
});
}

/**
* Get all experiments that should be run after config
* @return mixed
*/
public function get_after_config_experiments() {
$experiments = self::get_experiments();

// Filter array for all after config experiments.
return array_filter($experiments, function ($experiment) {
if (in_array($experiment['scope'], ['request', 'session'])) {
return true;
} else {
return false;
}
});
}

/**
* Get active requests
* @return mixed
Expand Down Expand Up @@ -333,7 +367,7 @@ public function get_active_session() {
public function get_active_device() {
$experiments = self::get_experiments();

// Filter array for only enabled session experiments.
// Filter array for only enabled device experiments.
return array_filter($experiments, function ($experiment) {
if ($experiment['enabled'] == 1 && $experiment['scope'] == 'device') {
return true;
Expand Down
21 changes: 19 additions & 2 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ function tool_abconfig_after_config() {
}
}

// Get all experiments.
$experiments = $manager->get_experiments();
// Get all after congig experiments and check params.
$experiments = $manager->get_after_config_experiments();
foreach ($experiments as $experiment => $contents) {

if (defined('CLI_SCRIPT') && CLI_SCRIPT) {
Expand Down Expand Up @@ -173,6 +173,23 @@ function tool_abconfig_before_session_start() {
// Setup experiment manager.
$manager = new tool_abconfig_experiment_manager();

// Get all before session experiments and check params.
$experiments = $manager->get_before_session_experiments();
foreach ($experiments as $experiment => $contents) {

// Check URL params, and fire any experiments in the params.
$condition = optional_param($experiment, null, PARAM_TEXT);
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']);
}
}

// First, Build a list of all commands that need to be executed.
$commandarray = [];

Expand Down

0 comments on commit 7564d7a

Please sign in to comment.