-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it looks like you copied the code from There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.