Skip to content

Commit e64dfa9

Browse files
committed
issue #116: add WS to delete and toggle rules
1 parent ba32823 commit e64dfa9

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

classes/external/rules.php

+12-14
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,26 @@ public static function delete_rules_parameters(): external_function_parameters {
5858
* @return array
5959
*/
6060
public static function delete_rules(array $ruleids): array {
61-
global $DB;
62-
6361
self::validate_parameters(self::delete_rules_parameters(), ['ruleids' => $ruleids]);
6462

6563
$context = context_system::instance();
6664
self::validate_context($context);
6765
require_capability('tool/dynamic_cohorts:manage', $context);
6866

6967
// 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]);
75-
if (empty($rule)) {
76-
throw new invalid_parameter_exception('Rule does not exist. ID: ' . $ruleid);
77-
}
78-
rule_manager::delete_rule($rule);
79-
//$transaction->allow_commit();
68+
// So let's check that all rules exist and then delete them.
69+
// Otherwise throw an exception and fail whole WS call.
70+
$rulestodelete = [];
71+
foreach ($ruleids as $ruleid) {
72+
$rule = rule::get_record(['id' => (int) $ruleid]);
73+
if (empty($rule)) {
74+
throw new invalid_parameter_exception('Rule does not exist. ID: ' . $ruleid);
8075
}
81-
} catch (throwable $ex) {
82-
$transaction->rollback($ex);
76+
$rulestodelete[] = $rule;
77+
}
78+
79+
foreach ($rulestodelete as $rule) {
80+
rule_manager::delete_rule($rule);
8381
}
8482

8583
return [];

tests/external/rules_test.php

-3
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ public function test_delete_rules_keep_rules_when_one_is_invalid() {
9393

9494
$this->assertCount(2, rule::get_records());
9595

96-
$this->expectException(\required_capability_exception::class);
97-
$this->expectExceptionMessage('Rule does not exist. ID: 777');
98-
9996
try {
10097
rules::delete_rules([$rule1->get('id'), $rule2->get('id'), 777]);
10198
} catch (\invalid_parameter_exception $exception) {

0 commit comments

Comments
 (0)