Skip to content

Commit

Permalink
Merge pull request #110 from catalyst/issue108-MOODLE_404_STABLE
Browse files Browse the repository at this point in the history
issue #108: add global setting to disable realtime processing
  • Loading branch information
dmitriim authored Sep 9, 2024
2 parents 8aaf17d + c7369c1 commit 6b8570d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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
Expand Down
9 changes: 6 additions & 3 deletions classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions lang/en/tool_dynamic_cohorts.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,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). <br/> Please note: no cohort_member_removed events will be triggered when members are released from a cohort.';
$string['usercreated'] = 'User was created';
Expand Down
6 changes: 6 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
35 changes: 35 additions & 0 deletions tests/observer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
}
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'tool_dynamic_cohorts';
$plugin->release = 2024070900;
$plugin->version = 2024070900;
$plugin->release = 2024091000;
$plugin->version = 2024091000;
$plugin->requires = 2022112800;
$plugin->supported = [404, 404];
$plugin->maturity = MATURITY_STABLE;

0 comments on commit 6b8570d

Please sign in to comment.