Skip to content

Commit da38508

Browse files
committed
WIP
1 parent 7a285f6 commit da38508

File tree

5 files changed

+185
-24
lines changed

5 files changed

+185
-24
lines changed

classes/external/condition_form.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,13 @@
1818

1919
use context_system;
2020
use moodle_exception;
21-
use external_api;
22-
use external_function_parameters;
23-
use external_single_structure;
24-
use external_value;
21+
use core_external\external_api;
22+
use core_external\external_function_parameters;
23+
use core_external\external_single_structure;
24+
use core_external\external_value;
2525
use tool_dynamic_cohorts\condition_base;
2626
use tool_dynamic_cohorts\condition_form as form;
2727

28-
defined('MOODLE_INTERNAL') || die();
29-
30-
require_once($CFG->dirroot . '/lib/externallib.php');
31-
3228
/**
3329
* Condition form AJAX submission.
3430
*

classes/external/matching_users.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,13 @@
1717
namespace tool_dynamic_cohorts\external;
1818

1919
use context_system;
20-
use external_api;
21-
use external_function_parameters;
22-
use external_value;
20+
use core_external\external_api;
21+
use core_external\external_function_parameters;
22+
use core_external\external_value;
2323
use tool_dynamic_cohorts\rule;
2424
use invalid_parameter_exception;
2525
use tool_dynamic_cohorts\rule_manager;
2626

27-
defined('MOODLE_INTERNAL') || die();
28-
29-
require_once($CFG->dirroot . '/lib/externallib.php');
30-
3127
/**
3228
* Matching users external APIs.
3329
*

classes/external/rule_conditions.php

+5-9
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,15 @@
1717
namespace tool_dynamic_cohorts\external;
1818

1919
use context_system;
20-
use external_api;
21-
use external_function_parameters;
22-
use external_value;
23-
use external_multiple_structure;
24-
use external_single_structure;
20+
use core_external\external_api;
21+
use core_external\external_function_parameters;
22+
use core_external\external_value;
23+
use core_external\external_multiple_structure;
24+
use core_external\external_single_structure;
2525
use tool_dynamic_cohorts\condition_base;
2626
use tool_dynamic_cohorts\rule;
2727
use invalid_parameter_exception;
2828

29-
defined('MOODLE_INTERNAL') || die();
30-
31-
require_once($CFG->dirroot . '/lib/externallib.php');
32-
3329
/**
3430
* Rule conditions external APIs.
3531
*

classes/external/rules.php

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
}

db/services.php

+16
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,20 @@
4949
'capabilities' => 'tool/dynamic_cohorts:manage',
5050
'ajax' => true,
5151
],
52+
'tool_dynamic_cohorts_delete_rules' => [
53+
'classname' => 'tool_dynamic_cohorts\external\rules',
54+
'methodname' => 'delete_rules',
55+
'description' => 'Delete provided rules',
56+
'type' => 'write',
57+
'capabilities' => 'tool/dynamic_cohorts:manage',
58+
'ajax' => true,
59+
],
60+
'tool_dynamic_cohorts_toggle_rule_status' => [
61+
'classname' => 'tool_dynamic_cohorts\external\rules',
62+
'methodname' => 'toggle_status',
63+
'description' => 'Toggles status for given rule (disable or enable)',
64+
'type' => 'write',
65+
'capabilities' => 'tool/dynamic_cohorts:manage',
66+
'ajax' => true,
67+
],
5268
];

0 commit comments

Comments
 (0)