|
| 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\external; |
| 18 | + |
| 19 | +use context_system; |
| 20 | +use core_external\external_api; |
| 21 | +use core_external\external_function_parameters; |
| 22 | +use core_external\external_multiple_structure; |
| 23 | +use core_external\external_value; |
| 24 | +use core_external\external_single_structure; |
| 25 | +use Throwable; |
| 26 | +use tool_dynamic_cohorts\condition_base; |
| 27 | +use tool_dynamic_cohorts\event\rule_updated; |
| 28 | +use tool_dynamic_cohorts\rule; |
| 29 | +use invalid_parameter_exception; |
| 30 | +use tool_dynamic_cohorts\rule_manager; |
| 31 | + |
| 32 | +/** |
| 33 | + * Rules external APIs. |
| 34 | + * |
| 35 | + * @package tool_dynamic_cohorts |
| 36 | + * @copyright 2024 Catalyst IT |
| 37 | + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 38 | + */ |
| 39 | +class rules extends external_api { |
| 40 | + |
| 41 | + /** |
| 42 | + * Describes the parameters for delete_rule webservice. |
| 43 | + * |
| 44 | + * @return external_function_parameters |
| 45 | + */ |
| 46 | + public static function delete_rules_parameters(): external_function_parameters { |
| 47 | + return new external_function_parameters( |
| 48 | + [ |
| 49 | + 'ruleids' => new external_multiple_structure(new external_value(PARAM_INT, 'Rule IDs') |
| 50 | + , 'List of rule id to delete.'), |
| 51 | + ] |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Deletes provided rules. |
| 57 | + * |
| 58 | + * @param array $ruleids Rule IDs. |
| 59 | + * @return array |
| 60 | + */ |
| 61 | + public static function delete_rules(array $ruleids): array { |
| 62 | + global $DB; |
| 63 | + |
| 64 | + self::validate_parameters(self::delete_rules_parameters(), ['ruleids' => $ruleids]); |
| 65 | + |
| 66 | + self::validate_context(context_system::instance()); |
| 67 | + require_capability('tool/dynamic_cohorts:manage', context_system::instance()); |
| 68 | + |
| 69 | + // We would like to treat deletion for multiple rules as one operation. |
| 70 | + // If one failed we would like to fail whole call and roll back. |
| 71 | + $transaction = $DB->start_delegated_transaction(); |
| 72 | + try { |
| 73 | + foreach ($ruleids as $ruleid) { |
| 74 | + $rule = rule::get_record(['id' => (int) $ruleid], MUST_EXIST); |
| 75 | + if (empty($rule)) { |
| 76 | + throw new invalid_parameter_exception('Rule does not exist. ID: ' . $ruleid); |
| 77 | + } |
| 78 | + rule_manager::delete_rule($rule); |
| 79 | + } |
| 80 | + } catch (throwable $ex) { |
| 81 | + $transaction->rollback($ex); |
| 82 | + } |
| 83 | + |
| 84 | + $transaction->allow_commit(); |
| 85 | + |
| 86 | + return []; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Returns description of method result value. |
| 91 | + * |
| 92 | + * @return external_single_structure |
| 93 | + */ |
| 94 | + public static function delete_rules_returns(): external_single_structure { |
| 95 | + return new external_single_structure([]); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Enable or disable rule. |
| 100 | + * |
| 101 | + * @return external_function_parameters |
| 102 | + */ |
| 103 | + public static function toggle_status_parameters(): external_function_parameters { |
| 104 | + return new external_function_parameters([ |
| 105 | + 'ruleid' => new external_value(PARAM_INT, 'The rule ID to toggle'), |
| 106 | + ]); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Toggle rule. |
| 111 | + * |
| 112 | + * @param int $ruleid Rule ID. |
| 113 | + * @return array |
| 114 | + */ |
| 115 | + public static function toggle_status(int $ruleid): array { |
| 116 | + self::validate_parameters(self::delete_rules_parameters(), ['ruleid' => $ruleid]); |
| 117 | + |
| 118 | + self::validate_context(context_system::instance()); |
| 119 | + require_capability('tool/dynamic_cohorts:manage', context_system::instance()); |
| 120 | + |
| 121 | + $rule = rule::get_record(['id' => $ruleid], MUST_EXIST); |
| 122 | + if (empty($rule)) { |
| 123 | + throw new invalid_parameter_exception('Rule does not exist.'); |
| 124 | + } |
| 125 | + |
| 126 | + if ($rule->is_broken()) { |
| 127 | + // Disable broken rule |
| 128 | + $rule->set('enabled', 0); |
| 129 | + $rule->save(); |
| 130 | + rule_updated::create(['other' => ['ruleid' => $rule->get('id')]])->trigger(); |
| 131 | + |
| 132 | + throw new invalid_parameter_exception('A broken rule can\'t be enabled'); |
| 133 | + } |
| 134 | + |
| 135 | + $newvalue = (int) !$rule->is_enabled(); |
| 136 | + $rule->set('enabled', $newvalue); |
| 137 | + $rule->save(); |
| 138 | + rule_updated::create(['other' => ['ruleid' => $rule->get('id')]])->trigger(); |
| 139 | + |
| 140 | + return [ |
| 141 | + 'ruleid' => $rule->get('id'), |
| 142 | + 'enabled' => $newvalue, |
| 143 | + ]; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Returns description of method result value. |
| 148 | + * |
| 149 | + * @return external_single_structure |
| 150 | + */ |
| 151 | + public static function toggle_status_returns(): external_single_structure { |
| 152 | + return new external_single_structure([ |
| 153 | + 'ruleid' => new external_value(PARAM_INT, 'The rule ID'), |
| 154 | + 'enabled' => new external_value(PARAM_INT, 'New status for the rule'), |
| 155 | + ]); |
| 156 | + } |
| 157 | +} |
0 commit comments