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 |
* Quick enrolment AMD module.
|
|
|
18 |
*
|
|
|
19 |
* @module enrol_manual/quickenrolment
|
|
|
20 |
* @copyright 2016 Damyon Wiese <damyon@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
import * as DynamicTable from 'core_table/dynamic';
|
|
|
24 |
import * as Str from 'core/str';
|
|
|
25 |
import * as Toast from 'core/toast';
|
|
|
26 |
import Config from 'core/config';
|
|
|
27 |
import Fragment from 'core/fragment';
|
|
|
28 |
import ModalEvents from 'core/modal_events';
|
|
|
29 |
import Notification from 'core/notification';
|
|
|
30 |
import jQuery from 'jquery';
|
|
|
31 |
import Pending from 'core/pending';
|
|
|
32 |
import Prefetch from 'core/prefetch';
|
|
|
33 |
import ModalSaveCancel from 'core/modal_save_cancel';
|
|
|
34 |
|
|
|
35 |
const Selectors = {
|
|
|
36 |
cohortSelector: "#id_cohortlist",
|
|
|
37 |
triggerButtons: ".enrolusersbutton.enrol_manual_plugin [type='submit']",
|
|
|
38 |
unwantedHiddenFields: "input[value='_qf__force_multiselect_submission']",
|
|
|
39 |
buttonWrapper: '[data-region="wrapper"]',
|
|
|
40 |
};
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Get the content of the body for the specified context.
|
|
|
44 |
*
|
|
|
45 |
* @param {Number} contextId
|
|
|
46 |
* @returns {Promise}
|
|
|
47 |
*/
|
|
|
48 |
const getBodyForContext = contextId => {
|
|
|
49 |
return Fragment.loadFragment('enrol_manual', 'enrol_users_form', contextId, {});
|
|
|
50 |
};
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Get the dynamic table for the button.
|
|
|
54 |
*
|
|
|
55 |
* @param {HTMLElement} element
|
|
|
56 |
* @returns {HTMLElement}
|
|
|
57 |
*/
|
|
|
58 |
const getDynamicTableForElement = element => {
|
|
|
59 |
const wrapper = element.closest(Selectors.buttonWrapper);
|
|
|
60 |
|
|
|
61 |
return DynamicTable.getTableFromId(wrapper.dataset.tableUniqueid);
|
|
|
62 |
};
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Register the event listeners for this contextid.
|
|
|
66 |
*
|
|
|
67 |
* @param {Number} contextId
|
|
|
68 |
*/
|
|
|
69 |
const registerEventListeners = contextId => {
|
|
|
70 |
document.addEventListener('click', e => {
|
|
|
71 |
if (e.target.closest(Selectors.triggerButtons)) {
|
|
|
72 |
e.preventDefault();
|
|
|
73 |
|
|
|
74 |
showModal(getDynamicTableForElement(e.target), contextId);
|
|
|
75 |
|
|
|
76 |
return;
|
|
|
77 |
}
|
|
|
78 |
});
|
|
|
79 |
};
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Display the modal for this contextId.
|
|
|
83 |
*
|
|
|
84 |
* @param {HTMLElement} dynamicTable The table to beb refreshed when changes are made
|
|
|
85 |
* @param {Number} contextId
|
|
|
86 |
* @returns {Promise}
|
|
|
87 |
*/
|
|
|
88 |
const showModal = (dynamicTable, contextId) => {
|
|
|
89 |
const pendingPromise = new Pending('enrol_manual/quickenrolment:showModal');
|
|
|
90 |
|
|
|
91 |
return ModalSaveCancel.create({
|
|
|
92 |
large: true,
|
|
|
93 |
title: Str.get_string('enrolusers', 'enrol_manual'),
|
|
|
94 |
body: getBodyForContext(contextId),
|
|
|
95 |
buttons: {
|
|
|
96 |
save: Str.get_string('enrolusers', 'enrol_manual'),
|
|
|
97 |
},
|
|
|
98 |
show: true,
|
|
|
99 |
})
|
|
|
100 |
.then(modal => {
|
|
|
101 |
modal.getRoot().on(ModalEvents.save, e => {
|
|
|
102 |
// Trigger a form submission, so that any mform elements can do final tricks before the form submission
|
|
|
103 |
// is processed.
|
|
|
104 |
// The actual submit even tis captured in the next handler.
|
|
|
105 |
|
|
|
106 |
e.preventDefault();
|
|
|
107 |
modal.getRoot().find('form').submit();
|
|
|
108 |
});
|
|
|
109 |
|
|
|
110 |
modal.getRoot().on('submit', 'form', e => {
|
|
|
111 |
e.preventDefault();
|
|
|
112 |
|
|
|
113 |
submitFormAjax(dynamicTable, modal);
|
|
|
114 |
});
|
|
|
115 |
|
|
|
116 |
modal.getRoot().on(ModalEvents.hidden, () => {
|
|
|
117 |
modal.destroy();
|
|
|
118 |
});
|
|
|
119 |
|
|
|
120 |
return modal;
|
|
|
121 |
})
|
|
|
122 |
.then(modal => Promise.all([modal, modal.getBodyPromise()]))
|
|
|
123 |
.then(([modal, body]) => {
|
|
|
124 |
if (body.get(0).querySelector(Selectors.cohortSelector)) {
|
|
|
125 |
return modal.setSaveButtonText(Str.get_string('enroluserscohorts', 'enrol_manual')).then(() => modal);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
return modal;
|
|
|
129 |
})
|
|
|
130 |
.then(modal => {
|
|
|
131 |
pendingPromise.resolve();
|
|
|
132 |
|
|
|
133 |
return modal;
|
|
|
134 |
})
|
|
|
135 |
.catch(Notification.exception);
|
|
|
136 |
};
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Submit the form via ajax.
|
|
|
140 |
*
|
|
|
141 |
* @param {HTMLElement} dynamicTable
|
|
|
142 |
* @param {Object} modal
|
|
|
143 |
*/
|
|
|
144 |
const submitFormAjax = (dynamicTable, modal) => {
|
|
|
145 |
// Note: We use a jQuery object here so that we can use its serialize functionality.
|
|
|
146 |
const form = modal.getRoot().find('form');
|
|
|
147 |
|
|
|
148 |
// Before send the data through AJAX, we need to parse and remove some unwanted hidden fields.
|
|
|
149 |
// This hidden fields are added automatically by mforms and when it reaches the AJAX we get an error.
|
|
|
150 |
form.get(0).querySelectorAll(Selectors.unwantedHiddenFields).forEach(hiddenField => hiddenField.remove());
|
|
|
151 |
|
|
|
152 |
modal.hide();
|
|
|
153 |
modal.destroy();
|
|
|
154 |
|
|
|
155 |
jQuery.ajax(
|
|
|
156 |
`${Config.wwwroot}/enrol/manual/ajax.php?${form.serialize()}`,
|
|
|
157 |
{
|
|
|
158 |
type: 'GET',
|
|
|
159 |
processData: false,
|
|
|
160 |
contentType: "application/json",
|
|
|
161 |
}
|
|
|
162 |
)
|
|
|
163 |
.then(response => {
|
|
|
164 |
if (response.error) {
|
|
|
165 |
throw new Error(response.error);
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
return response.count;
|
|
|
169 |
})
|
|
|
170 |
.then(count => {
|
|
|
171 |
return Promise.all([
|
|
|
172 |
Str.get_string('totalenrolledusers', 'enrol', count),
|
|
|
173 |
DynamicTable.refreshTableContent(dynamicTable),
|
|
|
174 |
]);
|
|
|
175 |
})
|
|
|
176 |
.then(([notificationBody]) => notificationBody)
|
|
|
177 |
.then(notificationBody => Toast.add(notificationBody))
|
|
|
178 |
.catch(error => {
|
|
|
179 |
Notification.addNotification({
|
|
|
180 |
message: error.message,
|
|
|
181 |
type: 'error',
|
|
|
182 |
});
|
|
|
183 |
});
|
|
|
184 |
};
|
|
|
185 |
|
|
|
186 |
/**
|
|
|
187 |
* Set up quick enrolment for the manual enrolment plugin.
|
|
|
188 |
*
|
|
|
189 |
* @param {Number} contextid The context id to setup for
|
|
|
190 |
*/
|
|
|
191 |
export const init = ({contextid}) => {
|
|
|
192 |
registerEventListeners(contextid);
|
|
|
193 |
|
|
|
194 |
Prefetch.prefetchStrings('enrol_manual', [
|
|
|
195 |
'enrolusers',
|
|
|
196 |
'enroluserscohorts',
|
|
|
197 |
]);
|
|
|
198 |
|
|
|
199 |
Prefetch.prefetchString('enrol', 'totalenrolledusers');
|
|
|
200 |
};
|