Skip to content

Commit

Permalink
issue #6: add rule related events
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Mar 6, 2024
1 parent 5b489e1 commit 61c923a
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 2 deletions.
74 changes: 74 additions & 0 deletions classes/event/rule_created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.

namespace tool_dynamic_cohorts\event;

use core\event\base;

/**
* Event triggered when a rule created.
*
* @property-read array $other {
* Extra information about event.
* - string ruleid: new rule id.
* }
*
* @package tool_dynamic_cohorts
* @copyright 2024 Catalyst IT
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class rule_created extends base {

/**
* Initialise the rule data.
*/
protected function init() {
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['crud'] = 'u';
$this->context = \context_system::instance();
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name(): string {
return get_string('event:rulecreated', 'tool_dynamic_cohorts');
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description(): string {
return "User with id '{$this->userid}' created a dynamic cohort rule with id '{$this->other['ruleid']}'";
}

/**
* Validates the custom data.
*
* @throws \coding_exception if missing required data.
*/
protected function validate_data() {
parent::validate_data();

if (!isset($this->other['ruleid'])) {
throw new \coding_exception('The \'ruleid\' value must be set in other.');
}
}
}
74 changes: 74 additions & 0 deletions classes/event/rule_deleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.

namespace tool_dynamic_cohorts\event;

use core\event\base;

/**
* Event triggered when a rule deleted.
*
* @property-read array $other {
* Extra information about event.
* - string ruleid: deleted rule id.
* }
*
* @package tool_dynamic_cohorts
* @copyright 2024 Catalyst IT
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class rule_deleted extends base {

/**
* Initialise the rule data.
*/
protected function init() {
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['crud'] = 'u';
$this->context = \context_system::instance();
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name(): string {
return get_string('event:ruledeleted', 'tool_dynamic_cohorts');
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description(): string {
return "User with id '{$this->userid}' deleted a dynamic cohort rule with id '{$this->other['ruleid']}'";
}

/**
* Validates the custom data.
*
* @throws \coding_exception if missing required data.
*/
protected function validate_data() {
parent::validate_data();

if (!isset($this->other['ruleid'])) {
throw new \coding_exception('The \'ruleid\' value must be set in other.');
}
}
}
75 changes: 75 additions & 0 deletions classes/event/rule_updated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.

namespace tool_dynamic_cohorts\event;

use core\event\base;

/**
* Event triggered when a rule updated.
*
* @property-read array $other {
* Extra information about event.
* - string ruleid: updated rule id.
* }
*
* @package tool_dynamic_cohorts
* @copyright 2024 Catalyst IT
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class rule_updated extends base {

/**
* Initialise the rule data.
*/
protected function init() {
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['crud'] = 'u';
$this->context = \context_system::instance();
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name(): string {
return get_string('event:ruleupdated', 'tool_dynamic_cohorts');
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description(): string {
return "User with id '{$this->userid}' updated a dynamic cohort rule with id '{$this->other['ruleid']}'";
}

/**
* Validates the custom data.
*
* @throws \coding_exception if missing required data.
*/
protected function validate_data() {
parent::validate_data();

if (!isset($this->other['ruleid'])) {
throw new \coding_exception('The \'ruleid\' value must be set in other.');
}
}

}
10 changes: 9 additions & 1 deletion classes/rule_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

use moodle_url;
use moodle_exception;
use tool_dynamic_cohorts\event\rule_created;
use tool_dynamic_cohorts\event\rule_deleted;
use tool_dynamic_cohorts\event\rule_updated;

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

Expand Down Expand Up @@ -96,11 +99,13 @@ public static function process_form(\stdClass $formdata): rule {
if (empty($formdata->id)) {
$rule = new rule(0, $ruledata);
$rule->create();
rule_created::create(['other' => ['ruleid' => $rule->get('id')]])->trigger();
} else {
$rule = new rule($formdata->id);
$oldcohortid = $rule->get('cohortid');
$rule->from_record($ruledata);
$rule->update();
rule_updated::create(['other' => ['ruleid' => $rule->get('id')]])->trigger();
}

cohort_manager::unmanage_cohort($oldcohortid);
Expand Down Expand Up @@ -145,10 +150,13 @@ private static function validate_submitted_data(\stdClass $formdata): void {
*
* @param rule $rule
*/
public static function delete_rule(rule $rule) {
public static function delete_rule(rule $rule): void {
$oldruleid = $rule->get('id');
$conditions = $rule->get_condition_records();

if ($rule->delete()) {
rule_deleted::create(['other' => ['ruleid' => $oldruleid]])->trigger();

// Delete related condition in a loop to be able to trigger events.
foreach ($conditions as $condition) {
$condition->delete();
Expand Down
3 changes: 3 additions & 0 deletions lang/en/tool_dynamic_cohorts.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
$string['edit_rule'] = 'Edit rule';
$string['enabled'] = 'Enabled';
$string['enable_confirm'] = 'Are you sure you want to enable rule {$a}?';
$string['event:rulecreated'] = 'Rule created';
$string['event:ruleupdated'] = 'Rule updated';
$string['event:ruledeleted'] = 'Rule deleted';
$string['managerules'] = 'Manage rules';
$string['managecohorts'] = 'Manage cohorts';
$string['name'] = 'Rule name';
Expand Down
66 changes: 65 additions & 1 deletion tests/rule_manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

use moodle_url;
use moodle_exception;
use tool_dynamic_cohorts\event\rule_created;
use tool_dynamic_cohorts\event\rule_deleted;
use tool_dynamic_cohorts\event\rule_updated;

/**
* Tests for rule manager class.
Expand Down Expand Up @@ -255,7 +258,7 @@ public function test_process_rule_form_with_cohort_managed_by_another_rule() {
}

/**
* Test trying to submit form data and not updating the cohort.
* Test submitting form data keeps cohort.
*/
public function test_process_rule_form_update_rule_form_keeping_cohort() {
global $DB;
Expand All @@ -273,6 +276,43 @@ public function test_process_rule_form_update_rule_form_keeping_cohort() {
$formdata = ['id' => $rule->get('id'), 'name' => 'Test1',
'cohortid' => $cohort->id, 'description' => 'D', 'conditionjson' => '', 'bulkprocessing' => 1];
rule_manager::process_form((object)$formdata);

$this->assertEquals('tool_dynamic_cohorts', $DB->get_field('cohort', 'component', ['id' => $cohort->id]));
}

/**
* Test triggering events.
*/
public function test_process_rule_form_triggers_events() {
$this->resetAfterTest();

$cohort = $this->getDataGenerator()->create_cohort();

$eventsink = $this->redirectEvents();
$formdata = ['name' => 'Test1', 'cohortid' => $cohort->id, 'description' => 'D',
'conditionjson' => '', 'bulkprocessing' => 1];
$rule = rule_manager::process_form((object) $formdata);

$events = array_filter($eventsink->get_events(), function ($event) {
return $event instanceof rule_created;
});

$this->assertCount(1, $events);
$this->assertEquals($rule->get('id'), reset($events)->other['ruleid']);
$eventsink->clear();

// Update the rule, changing the name. Should work as cohort is the same.
$formdata = ['id' => $rule->get('id'), 'name' => 'Test1',
'cohortid' => $cohort->id, 'description' => 'D', 'conditionjson' => '', 'bulkprocessing' => 1];
rule_manager::process_form((object) $formdata);

$events = array_filter($eventsink->get_events(), function ($event) {
return $event instanceof rule_updated;
});

$this->assertCount(1, $events);
$this->assertEquals($rule->get('id'), reset($events)->other['ruleid']);
$eventsink->clear();
}

/**
Expand Down Expand Up @@ -346,4 +386,28 @@ public function test_deleting_rule_releases_cohorts() {
rule_manager::delete_rule($rule2);
$this->assertEquals('', $DB->get_field('cohort', 'component', ['id' => $cohort2->id]));
}

/**
* Test deleting a rule triggers event.
*/
public function test_deleting_rule_triggers_event() {
$this->resetAfterTest();

$cohort = $this->getDataGenerator()->create_cohort(['component' => 'tool_dynamic_cohorts']);

$rule = new rule(0, (object)['name' => 'Test rule', 'cohortid' => $cohort->id]);
$rule->save();
$expectedruleid = $rule->get('id');

$eventsink = $this->redirectEvents();

rule_manager::delete_rule($rule);

$events = array_filter($eventsink->get_events(), function ($event) {
return $event instanceof rule_deleted;
});

$this->assertCount(1, $events);
$this->assertEquals($expectedruleid, reset($events)->other['ruleid']);
}
}

0 comments on commit 61c923a

Please sign in to comment.