1 |
efrain |
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 |
* Report builder audiences
|
|
|
18 |
*
|
|
|
19 |
* @module core_reportbuilder/schedules
|
|
|
20 |
* @copyright 2021 Paul Holden <paulh@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
"use strict";
|
|
|
25 |
|
|
|
26 |
import {dispatchEvent} from 'core/event_dispatcher';
|
|
|
27 |
import 'core/inplace_editable';
|
|
|
28 |
import Notification from 'core/notification';
|
|
|
29 |
import Pending from 'core/pending';
|
|
|
30 |
import {prefetchStrings} from 'core/prefetch';
|
|
|
31 |
import {getString} from 'core/str';
|
|
|
32 |
import {add as addToast} from 'core/toast';
|
|
|
33 |
import * as reportEvents from 'core_reportbuilder/local/events';
|
|
|
34 |
import * as reportSelectors from 'core_reportbuilder/local/selectors';
|
|
|
35 |
import {createScheduleModal} from 'core_reportbuilder/local/repository/modals';
|
|
|
36 |
import {deleteSchedule, sendSchedule, toggleSchedule} from 'core_reportbuilder/local/repository/schedules';
|
|
|
37 |
|
|
|
38 |
let initialized = false;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Initialise schedules tab
|
|
|
42 |
*
|
|
|
43 |
* @param {Number} reportId
|
|
|
44 |
*/
|
|
|
45 |
export const init = reportId => {
|
|
|
46 |
prefetchStrings('core_reportbuilder', [
|
|
|
47 |
'deleteschedule',
|
|
|
48 |
'deletescheduleconfirm',
|
|
|
49 |
'disableschedule',
|
|
|
50 |
'editscheduledetails',
|
|
|
51 |
'enableschedule',
|
|
|
52 |
'newschedule',
|
|
|
53 |
'schedulecreated',
|
|
|
54 |
'scheduledeleted',
|
|
|
55 |
'schedulesent',
|
|
|
56 |
'scheduleupdated',
|
|
|
57 |
'sendschedule',
|
|
|
58 |
'sendscheduleconfirm',
|
|
|
59 |
]);
|
|
|
60 |
|
|
|
61 |
prefetchStrings('core', [
|
|
|
62 |
'confirm',
|
|
|
63 |
'delete',
|
|
|
64 |
]);
|
|
|
65 |
|
|
|
66 |
if (initialized) {
|
|
|
67 |
// We already added the event listeners (can be called multiple times by mustache template).
|
|
|
68 |
return;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
document.addEventListener('click', event => {
|
|
|
72 |
|
|
|
73 |
// Create schedule.
|
|
|
74 |
const scheduleCreate = event.target.closest(reportSelectors.actions.scheduleCreate);
|
|
|
75 |
if (scheduleCreate) {
|
|
|
76 |
event.preventDefault();
|
|
|
77 |
|
|
|
78 |
const scheduleModal = createScheduleModal(event.target, getString('newschedule', 'core_reportbuilder'), reportId);
|
|
|
79 |
scheduleModal.addEventListener(scheduleModal.events.FORM_SUBMITTED, () => {
|
|
|
80 |
getString('schedulecreated', 'core_reportbuilder')
|
|
|
81 |
.then(addToast)
|
|
|
82 |
.then(() => {
|
|
|
83 |
const reportElement = document.querySelector(reportSelectors.regions.report);
|
|
|
84 |
dispatchEvent(reportEvents.tableReload, {}, reportElement);
|
|
|
85 |
return;
|
|
|
86 |
})
|
|
|
87 |
.catch(Notification.exception);
|
|
|
88 |
});
|
|
|
89 |
|
|
|
90 |
scheduleModal.show();
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
// Toggle schedule.
|
|
|
94 |
const scheduleToggle = event.target.closest(reportSelectors.actions.scheduleToggle);
|
|
|
95 |
if (scheduleToggle) {
|
|
|
96 |
const pendingPromise = new Pending('core_reportbuilder/schedules:toggle');
|
|
|
97 |
const scheduleStateToggle = +!Number(scheduleToggle.dataset.state);
|
|
|
98 |
|
|
|
99 |
toggleSchedule(reportId, scheduleToggle.dataset.id, scheduleStateToggle)
|
|
|
100 |
.then(() => {
|
|
|
101 |
const tableRow = scheduleToggle.closest('tr');
|
|
|
102 |
tableRow.classList.toggle('text-muted');
|
|
|
103 |
|
|
|
104 |
scheduleToggle.dataset.state = scheduleStateToggle;
|
|
|
105 |
|
|
|
106 |
const stringKey = scheduleStateToggle ? 'disableschedule' : 'enableschedule';
|
|
|
107 |
return getString(stringKey, 'core_reportbuilder');
|
|
|
108 |
})
|
|
|
109 |
.then(toggleLabel => {
|
|
|
110 |
const labelContainer = scheduleToggle.parentElement.querySelector(`label[for="${scheduleToggle.id}"] > span`);
|
|
|
111 |
labelContainer.innerHTML = toggleLabel;
|
|
|
112 |
return pendingPromise.resolve();
|
|
|
113 |
})
|
|
|
114 |
.catch(Notification.exception);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
// Edit schedule.
|
|
|
118 |
const scheduleEdit = event.target.closest(reportSelectors.actions.scheduleEdit);
|
|
|
119 |
if (scheduleEdit) {
|
|
|
120 |
event.preventDefault();
|
|
|
121 |
|
|
|
122 |
// Use triggerElement to return focus to the action menu toggle.
|
|
|
123 |
const triggerElement = scheduleEdit.closest('.dropdown').querySelector('.dropdown-toggle');
|
|
|
124 |
const scheduleModal = createScheduleModal(triggerElement, getString('editscheduledetails', 'core_reportbuilder'),
|
|
|
125 |
reportId, scheduleEdit.dataset.scheduleId);
|
|
|
126 |
scheduleModal.addEventListener(scheduleModal.events.FORM_SUBMITTED, () => {
|
|
|
127 |
getString('scheduleupdated', 'core_reportbuilder')
|
|
|
128 |
.then(addToast)
|
|
|
129 |
.then(() => {
|
|
|
130 |
const reportElement = scheduleEdit.closest(reportSelectors.regions.report);
|
|
|
131 |
dispatchEvent(reportEvents.tableReload, {}, reportElement);
|
|
|
132 |
return;
|
|
|
133 |
})
|
|
|
134 |
.catch(Notification.exception);
|
|
|
135 |
});
|
|
|
136 |
|
|
|
137 |
scheduleModal.show();
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
// Send schedule.
|
|
|
141 |
const scheduleSend = event.target.closest(reportSelectors.actions.scheduleSend);
|
|
|
142 |
if (scheduleSend) {
|
|
|
143 |
event.preventDefault();
|
|
|
144 |
|
|
|
145 |
// Use triggerElement to return focus to the action menu toggle.
|
|
|
146 |
const triggerElement = scheduleSend.closest('.dropdown').querySelector('.dropdown-toggle');
|
|
|
147 |
Notification.saveCancelPromise(
|
|
|
148 |
getString('sendschedule', 'core_reportbuilder'),
|
|
|
149 |
getString('sendscheduleconfirm', 'core_reportbuilder', scheduleSend.dataset.scheduleName),
|
|
|
150 |
getString('confirm', 'core'),
|
|
|
151 |
{triggerElement}
|
|
|
152 |
).then(() => {
|
|
|
153 |
const pendingPromise = new Pending('core_reportbuilder/schedules:send');
|
|
|
154 |
|
|
|
155 |
return sendSchedule(reportId, scheduleSend.dataset.scheduleId)
|
|
|
156 |
.then(addToast(getString('schedulesent', 'core_reportbuilder')))
|
|
|
157 |
.then(() => pendingPromise.resolve())
|
|
|
158 |
.catch(Notification.exception);
|
|
|
159 |
}).catch(() => {
|
|
|
160 |
return;
|
|
|
161 |
});
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
// Delete schedule.
|
|
|
165 |
const scheduleDelete = event.target.closest(reportSelectors.actions.scheduleDelete);
|
|
|
166 |
if (scheduleDelete) {
|
|
|
167 |
event.preventDefault();
|
|
|
168 |
|
|
|
169 |
// Use triggerElement to return focus to the action menu toggle.
|
|
|
170 |
const triggerElement = scheduleDelete.closest('.dropdown').querySelector('.dropdown-toggle');
|
|
|
171 |
Notification.saveCancelPromise(
|
|
|
172 |
getString('deleteschedule', 'core_reportbuilder'),
|
|
|
173 |
getString('deletescheduleconfirm', 'core_reportbuilder', scheduleDelete.dataset.scheduleName),
|
|
|
174 |
getString('delete', 'core'),
|
|
|
175 |
{triggerElement}
|
|
|
176 |
).then(() => {
|
|
|
177 |
const pendingPromise = new Pending('core_reportbuilder/schedules:delete');
|
|
|
178 |
|
|
|
179 |
return deleteSchedule(reportId, scheduleDelete.dataset.scheduleId)
|
|
|
180 |
.then(addToast(getString('scheduledeleted', 'core_reportbuilder')))
|
|
|
181 |
.then(() => {
|
|
|
182 |
const reportElement = scheduleDelete.closest(reportSelectors.regions.report);
|
|
|
183 |
dispatchEvent(reportEvents.tableReload, {preservePagination: true}, reportElement);
|
|
|
184 |
return pendingPromise.resolve();
|
|
|
185 |
})
|
|
|
186 |
.catch(Notification.exception);
|
|
|
187 |
}).catch(() => {
|
|
|
188 |
return;
|
|
|
189 |
});
|
|
|
190 |
}
|
|
|
191 |
});
|
|
|
192 |
|
|
|
193 |
initialized = true;
|
|
|
194 |
};
|