Skip to content

Commit

Permalink
issue #21: add condition related events
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Mar 6, 2024
1 parent 1c0841a commit 1852f37
Show file tree
Hide file tree
Showing 7 changed files with 488 additions and 12 deletions.
69 changes: 65 additions & 4 deletions classes/condition_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
namespace tool_dynamic_cohorts;

use core_component;
use tool_dynamic_cohorts\event\condition_created;
use tool_dynamic_cohorts\event\condition_deleted;
use tool_dynamic_cohorts\event\condition_updated;

/**
* Condition manager class.
Expand Down Expand Up @@ -74,19 +77,29 @@ public static function process_form(rule $rule, \stdClass $formdata): void {
if (empty($condition->get('id'))) {
$condition->set('ruleid', $rule->get('id'));
$condition->create();
self::trigger_condition_event(condition_created::class, $condition, []);
} else {
$toupdate[$condition->get('id')] = $condition;
}
}

$todelete = array_diff_key($oldconditions, $toupdate);

foreach ($todelete as $conditiontodelete) {
$conditiontodelete->delete();
}
self::delete_conditions($todelete);

foreach ($toupdate as $conditiontoupdate) {
$olddescription = $conditiontoupdate->get('configdata');
$instance = condition_base::get_instance(0, $conditiontoupdate->to_record());
if ($instance && !$instance->is_broken()) {
$olddescription = $instance->get_config_description();
}

$conditiontoupdate->save();

self::trigger_condition_event(condition_updated::class, $conditiontoupdate, [
'other' => [
'olddescription' => $olddescription,
],
]);
}
}
}
Expand Down Expand Up @@ -122,4 +135,52 @@ private static function process_condition_json(string $formjson): array {

return $conditions;
}

/**
* Delete conditions.
*
* @param \tool_dynamic_cohorts\condition[] $conditions A list of conditions to be deleted.
*/
public static function delete_conditions(array $conditions): void {
foreach ($conditions as $condition) {
if ($condition instanceof condition) {
$condition->delete();
self::trigger_condition_event(condition_deleted::class, $condition, [
'other' => [
'ruleid' => $condition->get('ruleid'),
],
]);
}
}
}

/**
* Trigger condition related event.
*
* @param string $eventclass Full event class name, e.g. \tool_dynamic_cohorts\event\condition_updated.
* @param condition $condition Related condition object.
* @param array $data Event related data.
*/
private static function trigger_condition_event(string $eventclass, condition $condition, array $data): void {
$instance = condition_base::get_instance(0, $condition->to_record());

if (!isset($data['other']['ruleid'])) {
$data['other']['ruleid'] = $condition->get('ruleid');
}

// In case that the class related to that condition is not found,
// we use data that we know about that condition such as class name and raw config.
if (!$instance) {
$name = $condition->get('classname');
$description = $condition->get('configdata');
} else {
$name = $instance->get_name();
$description = $instance->is_broken() ? $instance->get_broken_description() : $instance->get_config_description();
}

$data['other']['name'] = $name;
$data['other']['description'] = $description;

$eventclass::create($data)->trigger();
}
}
86 changes: 86 additions & 0 deletions classes/event/condition_created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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 condition created.
*
* @property-read array $other {
* Extra information about event.
* - string name: name of the condition instance.
* - string ruleid: related rule id.
* - string description: config data description.
* }
*
* @package tool_dynamic_cohorts
* @copyright 2024 Catalyst IT
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class condition_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:conditioncreated', 'tool_dynamic_cohorts');
}

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

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

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

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

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

}
86 changes: 86 additions & 0 deletions classes/event/condition_deleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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 condition deleted.
*
* @property-read array $other {
* Extra information about event.
* - string name: name of the condition instance.
* - string ruleid: related rule id.
* - string description: config data description.
* }
*
* @package tool_dynamic_cohorts
* @copyright 2024 Catalyst IT
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class condition_deleted extends base {

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

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

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

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

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

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

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

}
92 changes: 92 additions & 0 deletions classes/event/condition_updated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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 condition updated.
*
* @property-read array $other {
* Extra information about event.
* - string name: name of the condition instance.
* - string ruleid: related rule id.
* - string description: new config data description.
* - string olddescription: old config data description.
* }
*
* @package tool_dynamic_cohorts
* @copyright 2024 Catalyst IT
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class condition_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:conditionupdated', 'tool_dynamic_cohorts');
}

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

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

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

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

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

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

}
7 changes: 1 addition & 6 deletions classes/rule_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,7 @@ public static function delete_rule(rule $rule): void {

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();
}

condition_manager::delete_conditions($conditions);
cohort_manager::unmanage_cohort($rule->get('cohortid'));
}
}
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 @@ -49,6 +49,9 @@
$string['edit_rule'] = 'Edit rule';
$string['enabled'] = 'Enabled';
$string['enable_confirm'] = 'Are you sure you want to enable rule {$a}?';
$string['event:conditioncreated'] = 'Condition created';
$string['event:conditiondeleted'] = 'Condition deleted';
$string['event:conditionupdated'] = 'Condition updated';
$string['event:rulecreated'] = 'Rule created';
$string['event:ruleupdated'] = 'Rule updated';
$string['event:ruledeleted'] = 'Rule deleted';
Expand Down
Loading

0 comments on commit 1852f37

Please sign in to comment.