1441 |
ariadna |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Various actions on badges - enabling, disabling, etc.
|
|
|
18 |
*
|
|
|
19 |
* @module core_badges/actions
|
|
|
20 |
* @copyright 2024 Sara Arjona <sara@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
import selectors from 'core_badges/selectors';
|
|
|
25 |
import Notification from 'core/notification';
|
|
|
26 |
import {prefetchStrings} from 'core/prefetch';
|
|
|
27 |
import {getString} from 'core/str';
|
|
|
28 |
import Ajax from 'core/ajax';
|
|
|
29 |
import Pending from 'core/pending';
|
|
|
30 |
import {dispatchEvent} from 'core/event_dispatcher';
|
|
|
31 |
import {add as addToast} from 'core/toast';
|
|
|
32 |
import * as reportEvents from 'core_reportbuilder/local/events';
|
|
|
33 |
import * as reportSelectors from 'core_reportbuilder/local/selectors';
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Initialize module.
|
|
|
37 |
*/
|
|
|
38 |
export const init = () => {
|
|
|
39 |
prefetchStrings('core_badges', [
|
|
|
40 |
'reviewconfirm',
|
|
|
41 |
'activatesuccess',
|
|
|
42 |
'deactivatesuccess',
|
|
|
43 |
'awardoncron',
|
|
|
44 |
'numawardstat',
|
|
|
45 |
]);
|
|
|
46 |
prefetchStrings('core', [
|
|
|
47 |
'confirm',
|
|
|
48 |
'enable',
|
|
|
49 |
]);
|
|
|
50 |
|
|
|
51 |
registerEventListeners();
|
|
|
52 |
};
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Register events for delete preset option in action menu.
|
|
|
56 |
*/
|
|
|
57 |
const registerEventListeners = () => {
|
|
|
58 |
document.addEventListener('click', (event) => {
|
|
|
59 |
const enableOption = event.target.closest(selectors.actions.enablebadge);
|
|
|
60 |
|
|
|
61 |
if (enableOption) {
|
|
|
62 |
event.preventDefault();
|
|
|
63 |
|
|
|
64 |
// Use triggerElement to return focus to the action menu toggle.
|
|
|
65 |
const reportElement = event.target.closest(reportSelectors.regions.report);
|
|
|
66 |
const triggerElement = reportElement ? enableOption.closest('.dropdown').querySelector('.dropdown-toggle') : null;
|
|
|
67 |
const badgeId = enableOption.dataset.badgeid;
|
|
|
68 |
const badgeName = enableOption.dataset.badgename;
|
|
|
69 |
|
|
|
70 |
Notification.saveCancelPromise(
|
|
|
71 |
getString('confirm', 'core'),
|
|
|
72 |
getString('reviewconfirm', 'core_badges', badgeName),
|
|
|
73 |
getString('enable', 'core'),
|
|
|
74 |
{triggerElement}
|
|
|
75 |
).then(() => {
|
|
|
76 |
return enableBadge(badgeId, badgeName, reportElement);
|
|
|
77 |
}).catch(() => {
|
|
|
78 |
return;
|
|
|
79 |
});
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
const disableOption = event.target.closest(selectors.actions.disablebadge);
|
|
|
83 |
if (disableOption) {
|
|
|
84 |
event.preventDefault();
|
|
|
85 |
const badgeId = disableOption.dataset.badgeid;
|
|
|
86 |
const badgeName = disableOption.dataset.badgename;
|
|
|
87 |
const reportElement = event.target.closest(reportSelectors.regions.report);
|
|
|
88 |
disableBadge(badgeId, badgeName, reportElement);
|
|
|
89 |
}
|
|
|
90 |
});
|
|
|
91 |
};
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Enable the badge.
|
|
|
95 |
*
|
|
|
96 |
* @param {Number} badgeId The id of the badge to enable.
|
|
|
97 |
* @param {String} badgeName The name of the badge to enable.
|
|
|
98 |
* @param {HTMLElement} reportElement the report element.
|
|
|
99 |
*/
|
|
|
100 |
async function enableBadge(badgeId, badgeName, reportElement) {
|
|
|
101 |
var request = {
|
|
|
102 |
methodname: 'core_badges_enable_badges',
|
|
|
103 |
args: {
|
|
|
104 |
badgeids: [badgeId],
|
|
|
105 |
}
|
|
|
106 |
};
|
|
|
107 |
|
|
|
108 |
const pendingPromise = new Pending('core_badges/enable');
|
|
|
109 |
try {
|
|
|
110 |
const result = await Ajax.call([request])[0];
|
|
|
111 |
if (reportElement) {
|
|
|
112 |
showEnableResultToast(badgeName, result);
|
|
|
113 |
// Report element is present, reload the table.
|
|
|
114 |
dispatchEvent(reportEvents.tableReload, {preservePagination: true}, reportElement);
|
|
|
115 |
} else {
|
|
|
116 |
// Report element is not present, add the parameters to the current page to display the message.
|
|
|
117 |
const awards = result.result?.pop().awards;
|
|
|
118 |
document.location = document.location.pathname + `?id=${badgeId}&awards=${awards}`;
|
|
|
119 |
}
|
|
|
120 |
} catch (error) {
|
|
|
121 |
Notification.exception(error);
|
|
|
122 |
}
|
|
|
123 |
pendingPromise.resolve();
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Show the result of enabling a badge.
|
|
|
128 |
*
|
|
|
129 |
* @param {String} badgeName The name of the badge to enable.
|
|
|
130 |
* @param {Object} result The result of enabling a badge.
|
|
|
131 |
*/
|
|
|
132 |
function showEnableResultToast(badgeName, result) {
|
|
|
133 |
if (result.result?.length > 0) {
|
|
|
134 |
addToast(getString('activatesuccess', 'core_badges', badgeName), {type: 'success'});
|
|
|
135 |
const awards = result.result?.pop().awards;
|
|
|
136 |
if (awards == 'cron') {
|
|
|
137 |
addToast(getString('awardoncron', 'core_badges', {badgename: badgeName}));
|
|
|
138 |
} else if (awards > 0) {
|
|
|
139 |
addToast(getString('numawardstat', 'core_badges', {badgename: badgeName, awards: awards}));
|
|
|
140 |
}
|
|
|
141 |
} else if (result.warnings.length > 0) {
|
|
|
142 |
addToast(result.warnings[0].message, {type: 'danger'});
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Disable the badge.
|
|
|
148 |
*
|
|
|
149 |
* @param {Number} badgeId The id of the badge to disable.
|
|
|
150 |
* @param {String} badgeName The name of the badge to enable.
|
|
|
151 |
* @param {HTMLElement} reportElement the report element.
|
|
|
152 |
*/
|
|
|
153 |
async function disableBadge(badgeId, badgeName, reportElement) {
|
|
|
154 |
var request = {
|
|
|
155 |
methodname: 'core_badges_disable_badges',
|
|
|
156 |
args: {
|
|
|
157 |
badgeids: [badgeId],
|
|
|
158 |
}
|
|
|
159 |
};
|
|
|
160 |
|
|
|
161 |
try {
|
|
|
162 |
const result = await Ajax.call([request])[0];
|
|
|
163 |
if (reportElement) {
|
|
|
164 |
// Report element is present, show the message in a toast and reload the table.
|
|
|
165 |
showDisableResultToast(badgeName, result);
|
|
|
166 |
dispatchEvent(reportEvents.tableReload, {preservePagination: true}, reportElement);
|
|
|
167 |
} else {
|
|
|
168 |
// Report element is not present, the page should be reloaded.
|
|
|
169 |
document.location = document.location.pathname + `?id=${badgeId}`;
|
|
|
170 |
}
|
|
|
171 |
} catch (error) {
|
|
|
172 |
Notification.exception(error);
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Show the result of disabling a badge.
|
|
|
178 |
*
|
|
|
179 |
* @param {String} badgeName The name of the badge to disable.
|
|
|
180 |
* @param {Object} result The result of disabling a badge.
|
|
|
181 |
*/
|
|
|
182 |
function showDisableResultToast(badgeName, result) {
|
|
|
183 |
if (result.result) {
|
|
|
184 |
addToast(
|
|
|
185 |
getString('deactivatesuccess', 'core_badges', badgeName),
|
|
|
186 |
{type: 'success'}
|
|
|
187 |
);
|
|
|
188 |
} else if (result.warnings.length > 0) {
|
|
|
189 |
addToast(
|
|
|
190 |
result.warnings[0].message,
|
|
|
191 |
{type: 'danger'}
|
|
|
192 |
);
|
|
|
193 |
}
|
|
|
194 |
}
|