Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #71: Use new 'after_config' hook for Moodle 4.5 #72

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

@tuanngocnguyen tuanngocnguyen Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please fix this @param type, I think ci complains about this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching that, updated this one.

* @return void|null
*/
public static function after_config(\core\hook\after_config $hook) {
if (!get_config('tool_abconfig', 'version')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like you copied the code from tool_abconfig_after_config. Is there any way to abstract the to avoid code duplication?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right - it is just copy and pasted. As we're not creating separate stable branches and the function of the callback remains the same, we could just call the original tool_abconfig_after_config function from the new hook.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @djarran ,

Please update the PR with the call to existing tool_abconfig_after_config, I will review and merge the PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tuanngocnguyen, this has been done now. Thank you

// 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;