Skip to content

Commit e8ec390

Browse files
authored
Merge pull request #22 from catalyst/issue6
issue #6: add rule related events
2 parents 5b489e1 + 2027422 commit e8ec390

7 files changed

+303
-3
lines changed

classes/event/rule_created.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
// This file is part of Moodle - https://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
16+
17+
namespace tool_dynamic_cohorts\event;
18+
19+
use core\event\base;
20+
21+
/**
22+
* Event triggered when a rule created.
23+
*
24+
* @property-read array $other {
25+
* Extra information about event.
26+
* - string ruleid: new rule id.
27+
* }
28+
*
29+
* @package tool_dynamic_cohorts
30+
* @copyright 2024 Catalyst IT
31+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32+
*/
33+
class rule_created extends base {
34+
35+
/**
36+
* Initialise the rule data.
37+
*/
38+
protected function init() {
39+
$this->data['edulevel'] = self::LEVEL_OTHER;
40+
$this->data['crud'] = 'u';
41+
$this->context = \context_system::instance();
42+
}
43+
44+
/**
45+
* Return localised event name.
46+
*
47+
* @return string
48+
*/
49+
public static function get_name(): string {
50+
return get_string('event:rulecreated', 'tool_dynamic_cohorts');
51+
}
52+
53+
/**
54+
* Returns description of what happened.
55+
*
56+
* @return string
57+
*/
58+
public function get_description(): string {
59+
return "User with id '{$this->userid}' created a dynamic cohort rule with id '{$this->other['ruleid']}'";
60+
}
61+
62+
/**
63+
* Validates the custom data.
64+
*
65+
* @throws \coding_exception if missing required data.
66+
*/
67+
protected function validate_data() {
68+
parent::validate_data();
69+
70+
if (!isset($this->other['ruleid'])) {
71+
throw new \coding_exception('The \'ruleid\' value must be set in other.');
72+
}
73+
}
74+
}

classes/event/rule_deleted.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
// This file is part of Moodle - https://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
16+
17+
namespace tool_dynamic_cohorts\event;
18+
19+
use core\event\base;
20+
21+
/**
22+
* Event triggered when a rule deleted.
23+
*
24+
* @property-read array $other {
25+
* Extra information about event.
26+
* - string ruleid: deleted rule id.
27+
* }
28+
*
29+
* @package tool_dynamic_cohorts
30+
* @copyright 2024 Catalyst IT
31+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32+
*/
33+
class rule_deleted extends base {
34+
35+
/**
36+
* Initialise the rule data.
37+
*/
38+
protected function init() {
39+
$this->data['edulevel'] = self::LEVEL_OTHER;
40+
$this->data['crud'] = 'u';
41+
$this->context = \context_system::instance();
42+
}
43+
44+
/**
45+
* Return localised event name.
46+
*
47+
* @return string
48+
*/
49+
public static function get_name(): string {
50+
return get_string('event:ruledeleted', 'tool_dynamic_cohorts');
51+
}
52+
53+
/**
54+
* Returns description of what happened.
55+
*
56+
* @return string
57+
*/
58+
public function get_description(): string {
59+
return "User with id '{$this->userid}' deleted a dynamic cohort rule with id '{$this->other['ruleid']}'";
60+
}
61+
62+
/**
63+
* Validates the custom data.
64+
*
65+
* @throws \coding_exception if missing required data.
66+
*/
67+
protected function validate_data() {
68+
parent::validate_data();
69+
70+
if (!isset($this->other['ruleid'])) {
71+
throw new \coding_exception('The \'ruleid\' value must be set in other.');
72+
}
73+
}
74+
}

classes/event/rule_updated.php

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
// This file is part of Moodle - https://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
16+
17+
namespace tool_dynamic_cohorts\event;
18+
19+
use core\event\base;
20+
21+
/**
22+
* Event triggered when a rule updated.
23+
*
24+
* @property-read array $other {
25+
* Extra information about event.
26+
* - string ruleid: updated rule id.
27+
* }
28+
*
29+
* @package tool_dynamic_cohorts
30+
* @copyright 2024 Catalyst IT
31+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32+
*/
33+
class rule_updated extends base {
34+
35+
/**
36+
* Initialise the rule data.
37+
*/
38+
protected function init() {
39+
$this->data['edulevel'] = self::LEVEL_OTHER;
40+
$this->data['crud'] = 'u';
41+
$this->context = \context_system::instance();
42+
}
43+
44+
/**
45+
* Return localised event name.
46+
*
47+
* @return string
48+
*/
49+
public static function get_name(): string {
50+
return get_string('event:ruleupdated', 'tool_dynamic_cohorts');
51+
}
52+
53+
/**
54+
* Returns description of what happened.
55+
*
56+
* @return string
57+
*/
58+
public function get_description(): string {
59+
return "User with id '{$this->userid}' updated a dynamic cohort rule with id '{$this->other['ruleid']}'";
60+
}
61+
62+
/**
63+
* Validates the custom data.
64+
*
65+
* @throws \coding_exception if missing required data.
66+
*/
67+
protected function validate_data() {
68+
parent::validate_data();
69+
70+
if (!isset($this->other['ruleid'])) {
71+
throw new \coding_exception('The \'ruleid\' value must be set in other.');
72+
}
73+
}
74+
75+
}

classes/rule_manager.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
use moodle_url;
2020
use moodle_exception;
21+
use tool_dynamic_cohorts\event\rule_created;
22+
use tool_dynamic_cohorts\event\rule_deleted;
23+
use tool_dynamic_cohorts\event\rule_updated;
2124

2225
defined('MOODLE_INTERNAL') || die();
2326

@@ -96,11 +99,13 @@ public static function process_form(\stdClass $formdata): rule {
9699
if (empty($formdata->id)) {
97100
$rule = new rule(0, $ruledata);
98101
$rule->create();
102+
rule_created::create(['other' => ['ruleid' => $rule->get('id')]])->trigger();
99103
} else {
100104
$rule = new rule($formdata->id);
101105
$oldcohortid = $rule->get('cohortid');
102106
$rule->from_record($ruledata);
103107
$rule->update();
108+
rule_updated::create(['other' => ['ruleid' => $rule->get('id')]])->trigger();
104109
}
105110

106111
cohort_manager::unmanage_cohort($oldcohortid);
@@ -145,10 +150,13 @@ private static function validate_submitted_data(\stdClass $formdata): void {
145150
*
146151
* @param rule $rule
147152
*/
148-
public static function delete_rule(rule $rule) {
153+
public static function delete_rule(rule $rule): void {
154+
$oldruleid = $rule->get('id');
149155
$conditions = $rule->get_condition_records();
150156

151157
if ($rule->delete()) {
158+
rule_deleted::create(['other' => ['ruleid' => $oldruleid]])->trigger();
159+
152160
// Delete related condition in a loop to be able to trigger events.
153161
foreach ($conditions as $condition) {
154162
$condition->delete();

lang/en/tool_dynamic_cohorts.php

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
$string['edit_rule'] = 'Edit rule';
4343
$string['enabled'] = 'Enabled';
4444
$string['enable_confirm'] = 'Are you sure you want to enable rule {$a}?';
45+
$string['event:rulecreated'] = 'Rule created';
46+
$string['event:ruleupdated'] = 'Rule updated';
47+
$string['event:ruledeleted'] = 'Rule deleted';
4548
$string['managerules'] = 'Manage rules';
4649
$string['managecohorts'] = 'Manage cohorts';
4750
$string['name'] = 'Rule name';

tests/rule_manager_test.php

+65-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
use moodle_url;
2020
use moodle_exception;
21+
use tool_dynamic_cohorts\event\rule_created;
22+
use tool_dynamic_cohorts\event\rule_deleted;
23+
use tool_dynamic_cohorts\event\rule_updated;
2124

2225
/**
2326
* Tests for rule manager class.
@@ -255,7 +258,7 @@ public function test_process_rule_form_with_cohort_managed_by_another_rule() {
255258
}
256259

257260
/**
258-
* Test trying to submit form data and not updating the cohort.
261+
* Test submitting form data keeps cohort.
259262
*/
260263
public function test_process_rule_form_update_rule_form_keeping_cohort() {
261264
global $DB;
@@ -273,6 +276,43 @@ public function test_process_rule_form_update_rule_form_keeping_cohort() {
273276
$formdata = ['id' => $rule->get('id'), 'name' => 'Test1',
274277
'cohortid' => $cohort->id, 'description' => 'D', 'conditionjson' => '', 'bulkprocessing' => 1];
275278
rule_manager::process_form((object)$formdata);
279+
280+
$this->assertEquals('tool_dynamic_cohorts', $DB->get_field('cohort', 'component', ['id' => $cohort->id]));
281+
}
282+
283+
/**
284+
* Test triggering events.
285+
*/
286+
public function test_process_rule_form_triggers_events() {
287+
$this->resetAfterTest();
288+
289+
$cohort = $this->getDataGenerator()->create_cohort();
290+
291+
$eventsink = $this->redirectEvents();
292+
$formdata = ['name' => 'Test1', 'cohortid' => $cohort->id, 'description' => 'D',
293+
'conditionjson' => '', 'bulkprocessing' => 1];
294+
$rule = rule_manager::process_form((object) $formdata);
295+
296+
$events = array_filter($eventsink->get_events(), function ($event) {
297+
return $event instanceof rule_created;
298+
});
299+
300+
$this->assertCount(1, $events);
301+
$this->assertEquals($rule->get('id'), reset($events)->other['ruleid']);
302+
$eventsink->clear();
303+
304+
// Update the rule, changing the name. Should work as cohort is the same.
305+
$formdata = ['id' => $rule->get('id'), 'name' => 'Test1',
306+
'cohortid' => $cohort->id, 'description' => 'D', 'conditionjson' => '', 'bulkprocessing' => 1];
307+
rule_manager::process_form((object) $formdata);
308+
309+
$events = array_filter($eventsink->get_events(), function ($event) {
310+
return $event instanceof rule_updated;
311+
});
312+
313+
$this->assertCount(1, $events);
314+
$this->assertEquals($rule->get('id'), reset($events)->other['ruleid']);
315+
$eventsink->clear();
276316
}
277317

278318
/**
@@ -346,4 +386,28 @@ public function test_deleting_rule_releases_cohorts() {
346386
rule_manager::delete_rule($rule2);
347387
$this->assertEquals('', $DB->get_field('cohort', 'component', ['id' => $cohort2->id]));
348388
}
389+
390+
/**
391+
* Test deleting a rule triggers event.
392+
*/
393+
public function test_deleting_rule_triggers_event() {
394+
$this->resetAfterTest();
395+
396+
$cohort = $this->getDataGenerator()->create_cohort(['component' => 'tool_dynamic_cohorts']);
397+
398+
$rule = new rule(0, (object)['name' => 'Test rule', 'cohortid' => $cohort->id]);
399+
$rule->save();
400+
$expectedruleid = $rule->get('id');
401+
402+
$eventsink = $this->redirectEvents();
403+
404+
rule_manager::delete_rule($rule);
405+
406+
$events = array_filter($eventsink->get_events(), function ($event) {
407+
return $event instanceof rule_deleted;
408+
});
409+
410+
$this->assertCount(1, $events);
411+
$this->assertEquals($expectedruleid, reset($events)->other['ruleid']);
412+
}
349413
}

toggle.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
use core\output\notification;
2626
use tool_dynamic_cohorts\rule;
27-
use tool_dynamic_cohorts\rule_manager;
27+
use tool_dynamic_cohorts\event\rule_updated;
2828

2929
require_once(__DIR__ . '/../../../config.php');
3030
require_once($CFG->libdir . '/adminlib.php');
@@ -59,6 +59,7 @@
5959
} else if (data_submitted() && confirm_sesskey()) {
6060
$rule->set('enabled', $newvalue);
6161
$rule->save();
62+
rule_updated::create(['other' => ['ruleid' => $rule->get('id')]])->trigger();
6263
redirect($manageurl, $message, null, $messagetype);
6364
}
6465
} else {
@@ -68,5 +69,6 @@
6869

6970
$rule->set('enabled', $newvalue);
7071
$rule->save();
72+
rule_updated::create(['other' => ['ruleid' => $rule->get('id')]])->trigger();
7173
redirect($manageurl, $message, null, $messagetype);
7274
}

0 commit comments

Comments
 (0)