Skip to content

Commit

Permalink
Issue #71: Use new 'after_config' hook for Moodle 4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
djarran committed Oct 18, 2024
1 parent b033b5c commit 409f8c2
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 3 deletions.
124 changes: 124 additions & 0 deletions classes/hook_callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,128 @@ public static function before_footer_html_generation(\core\hook\output\before_fo

tool_abconfig_execute_js('footer');
}

/**
* Runs after config has been set.
*
* @param \core\hook\before_http_headers $hook
* @return void|null
*/
public static function after_config(\core\hook\after_config $hook) {
if (!get_config('tool_abconfig', 'version')) {
// Do nothing if plugin install not completed.
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
}
}
5 changes: 5 additions & 0 deletions db/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@
'callback' => '\tool_abconfig\hook_callbacks::before_footer_html_generation',
'priority' => 0,
],
[
'hook' => \core\hook\after_config::class,
'callback' => '\tool_abconfig\hook_callbacks::after_config',
'priority' => 0,
],
];
4 changes: 4 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

/**
* After config
*
* This is a legacy callback that is used for compatibility with older Moodle versions.
* Moodle 4.4+ will use tool_abconfig\hook_callbacks::after_config instead.
*
* @return void|null
*/
function tool_abconfig_after_config() {
Expand Down
6 changes: 3 additions & 3 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024060400; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2024060400; // Same as version.
$plugin->version = 2024060401; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2024060401; // Same as version.
$plugin->requires = 2014051217;
$plugin->supported = [38, 404]; // Available as of Moodle 3.8.0 or later.
$plugin->supported = [38, 405]; // Available as of Moodle 3.8.0 or later.
$plugin->component = "tool_abconfig";
$plugin->maturity = MATURITY_STABLE;

0 comments on commit 409f8c2

Please sign in to comment.