From 6078abcd9b8747a082485bcbd36fd8e69ec37b92 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Mon, 4 Mar 2024 17:20:23 +1100 Subject: [PATCH] issue #3: add persistent classes --- classes/condition.php | 56 +++++++++++++++++++++++++ classes/rule.php | 97 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 classes/condition.php create mode 100644 classes/rule.php diff --git a/classes/condition.php b/classes/condition.php new file mode 100644 index 0000000..c9088ca --- /dev/null +++ b/classes/condition.php @@ -0,0 +1,56 @@ +. + +namespace classes; + +use core\persistent; + +/** + * Conditions persistent class. + * + * @package tool_cohortmanager + * @author Dmitrii Metelkin + * @copyright 2022 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class condition extends persistent { + + /** @var string table. */ + const TABLE = 'tool_dynamic_cohorts_c'; + + /** + * Return the definition of the properties of this model. + * + * @return array + */ + protected static function define_properties() { + return [ + 'ruleid' => [ + 'type' => PARAM_INT, + ], + 'classname' => [ + 'type' => PARAM_TEXT, + ], + 'configdata' => [ + 'type' => PARAM_RAW, + 'default' => '{}', + ], + 'sortorder' => [ + 'type' => PARAM_INT, + ], + ]; + } +} diff --git a/classes/rule.php b/classes/rule.php new file mode 100644 index 0000000..1e1b281 --- /dev/null +++ b/classes/rule.php @@ -0,0 +1,97 @@ +. + +namespace classes; + +use core\persistent; + +/** + * Rules persistent + * + * @package tool_dynamic_cohorts + * @copyright 2024 Catalyst IT + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class rule extends persistent { + + /** @var string table. */ + const TABLE = 'tool_dynamic_cohorts'; + + /** + * Return the definition of the properties of this model. + * + * @return array + */ + protected static function define_properties() { + return [ + 'name' => [ + 'type' => PARAM_TEXT, + ], + 'description' => [ + 'type' => PARAM_TEXT, + 'default' => null, + 'null' => NULL_ALLOWED, + ], + 'cohortid' => [ + 'type' => PARAM_INT, + 'default' => 0, + ], + 'enabled' => [ + 'type' => PARAM_INT, + 'default' => 0, + ], + 'bulkprocessing' => [ + 'type' => PARAM_INT, + 'default' => 0, + ], + 'broken' => [ + 'type' => PARAM_INT, + 'default' => 0, + ], + ]; + } + + /** + * Get a list of condition records for that rule. + * + * @return condition[] + */ + public function get_condition_records(): array { + $conditions = []; + foreach (condition::get_records(['ruleid' => $this->get('id')], 'position') as $condition) { + $conditions[$condition->get('id')] = $condition; + } + + return $conditions; + } + + /** + * Return if the rule is enabled. + * + * @return bool + */ + public function is_enabled() : bool { + return (bool)$this->get('enabled'); + } + + /** + * Check if this rule should process in bulk. + * @return bool + */ + public function is_bulk_processing(): bool { + return (bool) $this->get('bulkprocessing'); + } +}