Skip to content

Commit 4beb75c

Browse files
committed
issue #80: release members once a cohort is not managed by the plugin
1 parent e00d5a3 commit 4beb75c

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

classes/cohort_manager.php

+7
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,19 @@ public static function manage_cohort(int $cohortid): void {
7777
* @param int $cohortid Cohort ID.
7878
*/
7979
public static function unmanage_cohort(int $cohortid): void {
80+
global $DB;
81+
8082
$cohorts = self::get_cohorts();
8183

8284
if (!empty($cohorts[$cohortid])) {
8385
$cohort = $cohorts[$cohortid];
8486
$cohort->component = '';
8587
cohort_update_cohort($cohort);
88+
89+
// Brutally delete members if configured to do so. No cohort_member_removed events will be triggered.
90+
if (get_config('tool_dynamic_cohorts', 'releasemembers')) {
91+
$DB->delete_records('cohort_members', ['cohortid' => $cohortid]);
92+
}
8693
}
8794
}
8895
}

lang/en/tool_dynamic_cohorts.php

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@
141141
$string['rule_entity.description'] = 'Description';
142142
$string['rule_entity.bulkprocessing'] = 'Bulk processing';
143143
$string['rule_entity.status'] = 'Status';
144+
$string['settings:releasemembers'] = 'Release members';
145+
$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.';
144146
$string['usercreated'] = 'User was created';
145147
$string['usercreatedin'] = 'Users who were created in the last {$a}';
146148
$string['usercreatedtime'] = 'Users who were created {$a->operator} {$a->time}';

settings.php

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@
2525
defined('MOODLE_INTERNAL') || die();
2626

2727
$ADMIN->add('accounts', new admin_category('tool_dynamic_cohorts', get_string('pluginname', 'tool_dynamic_cohorts')));
28+
29+
if ($hassiteconfig) {
30+
$settings = new admin_settingpage('tool_dynamic_cohorts_settings', new lang_string('settings'));
31+
$settings->add(new admin_setting_configcheckbox(
32+
'tool_dynamic_cohorts/releasemembers',
33+
new lang_string('settings:releasemembers', 'tool_dynamic_cohorts'),
34+
new lang_string('settings:releasemembers_desc', 'tool_dynamic_cohorts'),
35+
0
36+
));
37+
$ADMIN->add('tool_dynamic_cohorts', $settings);
38+
}
39+
2840
$ADMIN->add('tool_dynamic_cohorts', new admin_externalpage(
2941
'tool_dynamic_cohorts_rules',
3042
get_string('managerules', 'tool_dynamic_cohorts'),

tests/cohort_manager_test.php

+29
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,33 @@ public function test_unmanage_cohort() {
8787
cohort_manager::unmanage_cohort($cohort->id);
8888
$this->assertEquals('', $DB->get_field('cohort', 'component', ['id' => $cohort->id]));
8989
}
90+
91+
/**
92+
* Test un managing cohort removes members when configured.
93+
*/
94+
public function test_unmanage_cohort_removes_members() {
95+
global $DB;
96+
97+
$this->resetAfterTest();
98+
99+
$user1 = $this->getDataGenerator()->create_user();
100+
$user2 = $this->getDataGenerator()->create_user();
101+
102+
$cohort = $this->getDataGenerator()->create_cohort(['component' => 'tool_dynamic_cohorts']);
103+
cohort_add_member($cohort->id, $user1->id);
104+
cohort_add_member($cohort->id, $user2->id);
105+
106+
$this->assertEquals('tool_dynamic_cohorts', $DB->get_field('cohort', 'component', ['id' => $cohort->id]));
107+
$this->assertCount(2, $DB->get_records('cohort_members', ['cohortid' => $cohort->id]));
108+
109+
set_config('releasemembers', 0, 'tool_dynamic_cohorts');
110+
cohort_manager::unmanage_cohort($cohort->id);
111+
$this->assertEquals('', $DB->get_field('cohort', 'component', ['id' => $cohort->id]));
112+
$this->assertCount(2, $DB->get_records('cohort_members', ['cohortid' => $cohort->id]));
113+
114+
set_config('releasemembers', 1, 'tool_dynamic_cohorts');
115+
cohort_manager::unmanage_cohort($cohort->id);
116+
$this->assertEquals('', $DB->get_field('cohort', 'component', ['id' => $cohort->id]));
117+
$this->assertCount(0, $DB->get_records('cohort_members', ['cohortid' => $cohort->id]));
118+
}
90119
}

version.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
defined('MOODLE_INTERNAL') || die();
2626

2727
$plugin->component = 'tool_dynamic_cohorts';
28-
$plugin->release = 2024042000;
29-
$plugin->version = 2024042000;
28+
$plugin->release = 2024042001;
29+
$plugin->version = 2024042001;
3030
$plugin->requires = 2022112800;
3131
$plugin->supported = [401, 403];
3232
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)