diff --git a/README.md b/README.md index e2abc9b..ae9ea96 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,10 @@ Rules can be processed by two mechanisms: 1. By cron: When a rule is created or updated, there may be many users that need to be added or removed from a cohort. This process is handled by cron, and depending on how many users are matched by a rule, this process can take some time. For rules matching large sets of users, some [configuration options](#rule-processing-options) are provided which may be useful to server administrators. 2. By event: rules may also listen to certain events. When one of these events triggers, appropriate rules will be checked and the user will be added to the appropriate cohort immediately. For example, the User standard profile field rule listens to the "User created" and "User updated" events. +### Disabling realtime processing (processing on event) + +There is a global admin setting that allows administrator to enable or disable realtime rule processing. This may be useful if processing taking too long and blocking the user interface. + # Configuration ## Prerequisites diff --git a/classes/observer.php b/classes/observer.php index 161555b..84e6b6f 100644 --- a/classes/observer.php +++ b/classes/observer.php @@ -33,9 +33,12 @@ class observer { * @param base $event The event. */ public static function process_event(base $event): void { - foreach (condition_manager::get_conditions_with_event($event) as $condition) { - foreach (rule_manager::get_rules_with_condition($condition) as $rule) { - rule_manager::process_rule($rule, self::get_userid_from_event($event)); + // Check if realtime processing enabled globally. + if (get_config('tool_dynamic_cohorts', 'realtime')) { + foreach (condition_manager::get_conditions_with_event($event) as $condition) { + foreach (rule_manager::get_rules_with_condition($condition) as $rule) { + rule_manager::process_rule($rule, self::get_userid_from_event($event)); + } } } } diff --git a/lang/en/tool_dynamic_cohorts.php b/lang/en/tool_dynamic_cohorts.php index c5267ae..600c09c 100644 --- a/lang/en/tool_dynamic_cohorts.php +++ b/lang/en/tool_dynamic_cohorts.php @@ -147,6 +147,8 @@ $string['rule_entity.description'] = 'Description'; $string['rule_entity.bulkprocessing'] = 'Bulk processing'; $string['rule_entity.status'] = 'Status'; +$string['settings:realtime'] = 'Real time processing'; +$string['settings:realtime_desc'] = 'When enabled, rules with conditions that support triggering on the event will be processed synchronously as part of the event. Use caution when enabling as long running rule processing will block the user interface.'; $string['settings:releasemembers'] = 'Release members'; $string['settings:releasemembers_desc'] = 'If enabled all members will be removed from a cohort once it\'s not managed by the plugin (e.g a rule is deleted or cohort for a rule is changed).
Please note: no cohort_member_removed events will be triggered when members are released from a cohort.'; $string['usercreated'] = 'User was created'; diff --git a/settings.php b/settings.php index 2825e58..405fbb1 100644 --- a/settings.php +++ b/settings.php @@ -34,6 +34,12 @@ new lang_string('settings:releasemembers_desc', 'tool_dynamic_cohorts'), 0 )); + $settings->add(new admin_setting_configcheckbox( + 'tool_dynamic_cohorts/realtime', + new lang_string('settings:realtime', 'tool_dynamic_cohorts'), + new lang_string('settings:realtime_desc', 'tool_dynamic_cohorts'), + 1 + )); $ADMIN->add('tool_dynamic_cohorts', $settings); } diff --git a/tests/observer_test.php b/tests/observer_test.php index 1ca46af..81b159b 100644 --- a/tests/observer_test.php +++ b/tests/observer_test.php @@ -114,4 +114,39 @@ public function test_user_updating_triggers_rule_processing() { user_update_user($user2, false); $this->assertEquals(2, $DB->count_records('cohort_members', ['cohortid' => $this->cohort->id])); } + + /** + * Test that user creation event doesn't trigger rule processing for that user if realtime processing is disabled globally. + */ + public function test_realtime_rule_processing_when_disabled_globally() { + global $DB; + + set_config('realtime', 0, 'tool_dynamic_cohorts'); + + $rule = new rule(0, (object)['name' => 'Test rule 1', 'enabled' => 1, 'cohortid' => $this->cohort->id]); + $rule->save(); + + $condition = condition_base::get_instance(0, (object)[ + 'classname' => 'tool_dynamic_cohorts\local\tool_dynamic_cohorts\condition\user_profile', + ]); + + // Condition username starts with user to catch both users. + $condition->set_config_data([ + 'profilefield' => 'username', + 'username_operator' => user_profile::TEXT_STARTS_WITH, + 'username_value' => 'user', + ]); + + $record = $condition->get_record(); + $record->set('ruleid', $rule->get('id')); + $record->set('sortorder', 0); + $record->save(); + + $this->assertEquals(0, $DB->count_records('cohort_members', ['cohortid' => $this->cohort->id])); + + $this->getDataGenerator()->create_user(['username' => 'user1']); + $this->getDataGenerator()->create_user(['username' => 'user2']); + + $this->assertEquals(0, $DB->count_records('cohort_members', ['cohortid' => $this->cohort->id])); + } } diff --git a/version.php b/version.php index 36ca5dc..a147e8b 100644 --- a/version.php +++ b/version.php @@ -26,7 +26,7 @@ $plugin->component = 'tool_dynamic_cohorts'; $plugin->release = 2024051001; -$plugin->version = 2024051001; +$plugin->version = 2024051003; $plugin->requires = 2022112800; $plugin->supported = [401, 403]; $plugin->maturity = MATURITY_STABLE;