Skip to content

Commit a4425df

Browse files
committed
WIP
1 parent ab7261d commit a4425df

File tree

8 files changed

+116
-10
lines changed

8 files changed

+116
-10
lines changed

amd/build/manage_rules.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/manage_rules.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/src/manage_rules.js

+99
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@ import {get_string as getString} from 'core/str';
2929
import * as DynamicTable from 'core_table/dynamic';
3030
import Fragment from 'core/fragment';
3131
import ModalCancel from 'core/modal_cancel';
32+
import DynamicTableSelectors from 'core_table/local/dynamic/selectors';
33+
import {add as notifyUser} from 'core/toast';
3234

3335
/**
3436
* A list of used selectors.
3537
*/
3638
const SELECTORS = {
3739
RULE_MATCHING_USERS: 'tool-dynamic-cohorts-matching-users',
3840
RULE_CONDITIONS: '.tool-dynamic-cohorts-condition-view',
41+
RULE_TOGGLE: '.tool-dynamic-cohorts-rule-toggle',
42+
RULE_DELETE: '.tool-dynamic-cohorts-rule-delete',
3943
};
4044

4145
/**
@@ -45,13 +49,18 @@ export const init = () => {
4549
loadMatchingUsers(document);
4650
initMatchingUsersModals(document);
4751
initRuleConditionsModals(document);
52+
initRuleToggle(document);
53+
initRuleDelete(document);
54+
4855

4956
document.addEventListener(DynamicTable.Events.tableContentRefreshed, e => {
5057
const tableRoot = DynamicTable.getTableFromId(e.target.dataset.tableUniqueid);
5158

5259
initMatchingUsersModals(tableRoot);
5360
loadMatchingUsers(tableRoot);
5461
initRuleConditionsModals(tableRoot);
62+
initRuleToggle(tableRoot);
63+
initRuleDelete(tableRoot);
5564
});
5665
};
5766

@@ -169,3 +178,93 @@ const initRuleConditionsModals = (root) => {
169178
});
170179
});
171180
};
181+
182+
/**
183+
* Send feedback to a user.
184+
*
185+
* @param {string} action Action to send feedback about.
186+
*/
187+
const sendFeedback = (action) => {
188+
getString('completed:' + action, 'tool_dynamic_cohorts')
189+
.then(message => {
190+
notifyUser(message);
191+
}).catch(Notification.exception);
192+
};
193+
194+
/**
195+
*
196+
* @param link
197+
* @returns {*}
198+
*/
199+
const getDynamicTableFromLink = (link) => {
200+
return link.closest(DynamicTableSelectors.main.region);
201+
};
202+
203+
204+
/**
205+
* Initialise displaying each rule conditions in a modal.
206+
*
207+
* @param {Element} root
208+
*/
209+
const initRuleToggle = (root) => {
210+
root.querySelectorAll(SELECTORS.RULE_TOGGLE).forEach(link => {
211+
let ruleid = link.dataset.ruleid;
212+
let action = link.dataset.action;
213+
link.addEventListener('click', function(e) {
214+
e.preventDefault();
215+
Notification.confirm(
216+
getString('confirm', 'moodle'),
217+
getString(action + '_confirm', 'tool_dynamic_cohorts', ruleid),
218+
getString('yes', 'moodle'),
219+
getString('no', 'moodle'),
220+
function () {
221+
Ajax.call([{
222+
methodname: 'tool_dynamic_cohorts_toggle_rule_status',
223+
args: {ruleid: ruleid},
224+
done: function () {
225+
sendFeedback(action);
226+
DynamicTable.refreshTableContent(getDynamicTableFromLink(link))
227+
.catch(Notification.exception);
228+
},
229+
fail: function (response) {
230+
Notification.exception(response);
231+
}
232+
}]);
233+
});
234+
});
235+
});
236+
};
237+
238+
/**
239+
* Initialise displaying each rule conditions in a modal.
240+
*
241+
* @param {Element} root
242+
*/
243+
const initRuleDelete = (root) => {
244+
root.querySelectorAll(SELECTORS.RULE_DELETE).forEach(link => {
245+
let ruleid = link.dataset.ruleid;
246+
let action = link.dataset.action;
247+
link.addEventListener('click', function(e) {
248+
e.preventDefault();
249+
Notification.confirm(
250+
getString('confirm', 'moodle'),
251+
getString(action + '_confirm', 'tool_dynamic_cohorts', ruleid),
252+
getString('yes', 'moodle'),
253+
getString('no', 'moodle'),
254+
function () {
255+
Ajax.call([{
256+
methodname: 'tool_dynamic_cohorts_delete_rules`',
257+
args: {ruleids: {ruleid}},
258+
done: function () {
259+
sendFeedback(action);
260+
DynamicTable.refreshTableContent(getDynamicTableFromLink(link))
261+
.catch(Notification.exception);
262+
},
263+
fail: function (response) {
264+
Notification.exception(response);
265+
}
266+
}]);
267+
});
268+
});
269+
});
270+
};

classes/reportbuilder/local/systemreports/matching_users.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@ protected function initialise(): void {
4848
$this->add_entity($userentity);
4949

5050
$conditions = $rule->get_condition_records();
51+
$sql = condition_manager::build_sql_data($conditions, $rule->get('operator'));
52+
5153
if (empty($conditions) || $rule->is_broken()) {
5254
// No conditions. Filter out all users.
5355
$this->add_base_condition_sql(' true = false');
56+
} else {
57+
$this->add_base_condition_sql($sql->get_where(), $sql->get_params());
5458
}
5559

56-
$sql = condition_manager::build_sql_data($conditions, $rule->get('operator'));
57-
5860
$this->add_base_fields('DISTINCT u.id');
5961
$this->add_join($sql->get_join());
60-
$this->add_base_condition_sql($sql->get_where(), $sql->get_params());
6162

6263
$this->add_column_from_entity('user:fullnamewithlink');
6364
$this->add_column_from_entity('user:username');

classes/reportbuilder/local/systemreports/rules.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ protected function add_actions(): void {
149149
$this->add_action((new action(
150150
new moodle_url('/admin/tool/dynamic_cohorts/toggle.php', ['ruleid' => ':id', 'sesskey' => sesskey()]),
151151
new pix_icon('t/hide', '', 'core'),
152-
[],
152+
['class' => 'tool-dynamic-cohorts-rule-toggle', 'data-ruleid' => ':id', 'data-action' => 'enable'],
153153
false,
154154
new lang_string('enable')
155155
))->add_callback(function(\stdClass $row): bool {
@@ -159,7 +159,7 @@ protected function add_actions(): void {
159159
$this->add_action((new action(
160160
new moodle_url('/admin/tool/dynamic_cohorts/toggle.php', ['ruleid' => ':id', 'sesskey' => sesskey()]),
161161
new pix_icon('t/show', '', 'core'),
162-
[],
162+
['class' => 'tool-dynamic-cohorts-rule-toggle', 'data-ruleid' => ':id', 'data-action' => 'disable'],
163163
false,
164164
new lang_string('disable')
165165
))->add_callback(function(\stdClass $row): bool {
@@ -177,7 +177,7 @@ protected function add_actions(): void {
177177
$this->add_action((new action(
178178
new moodle_url('/admin/tool/dynamic_cohorts/delete.php', ['ruleid' => ':id', 'sesskey' => sesskey()]),
179179
new pix_icon('t/delete', '', 'core'),
180-
[],
180+
['class' => 'tool-dynamic-cohorts-rule-delete', 'data-ruleid' => ':id', 'data-action' => 'delete'],
181181
false,
182182
new lang_string('delete')
183183
)));

classes/rule_manager.php

+3
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ public static function get_matching_users(rule $rule, ?int $userid = null): arra
264264
public static function get_matching_users_count(rule $rule, ?int $userid = null): int {
265265
global $DB;
266266

267+
// TODO: add caching as we would need to return users count on each page refresh.
268+
// Cache should be rebuilt on every rule change for a specific rule.
269+
267270
$conditions = $rule->get_condition_records();
268271

269272
if (empty($conditions)) {

lang/en/tool_dynamic_cohorts.php

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
$string['cohortid'] = 'Cohort';
4343
$string['cohortswith'] = 'Cohort(s) with field';
4444
$string['cohortid_help'] = 'A cohort to manage as part of this rule. Only cohorts that are not managed by other plugins are displayed in this list.';
45+
$string['completed:delete'] = 'Rule has been deleted';
46+
$string['completed:enable'] = 'Rule has been enabled';
47+
$string['completed:disable'] = 'Rule has been disabled';
4548
$string['completiondate'] = 'Completion date';
4649
$string['completionisdisabled'] = 'Completion is disabled for configured course';
4750
$string['condition'] = 'Condition';

version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
$plugin->component = 'tool_dynamic_cohorts';
2828
$plugin->release = 2024112100;
29-
$plugin->version = 2024112100;
29+
$plugin->version = 2024112101;
3030
$plugin->requires = 2022112800;
3131
$plugin->supported = [404, 405];
3232
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)