MDL-68464 enrol_manual: Rewrite quickenrolment to use ES6
[moodle.git] / enrol / manual / amd / src / quickenrolment.js
CommitLineData
a60e8ba5
DW
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 */
c50f5af2
AN
23import jQuery from 'jquery';
24import * as Str from 'core/str';
25import Fragment from 'core/fragment';
26import ModalEvents from 'core/modal_events';
27import ModalFactory from 'core/modal_factory';
28import Notification from 'core/notification';
29import Templates from 'core/templates';
30import Config from 'core/config';
31import Pending from 'core/pending';
32
33const Selectors = {
34 cohortSelector: "#id_cohortlist",
35 triggerButtons: ".enrolusersbutton.enrol_manual_plugin [type='submit']",
36 unwantedHiddenFields: ":input[value='_qf__force_multiselect_submission']"
37};
38
39const QuickEnrolment = class {
40 constructor(contextId) {
41 this.contextId = contextId;
a60e8ba5
DW
42
43 this.initModal();
c50f5af2 44 }
a60e8ba5
DW
45
46 /**
47 * Private method
48 *
49 * @method initModal
50 * @private
51 */
c50f5af2
AN
52 initModal() {
53 var triggerButtons = jQuery(Selectors.triggerButtons);
a60e8ba5 54
c50f5af2 55 jQuery.when(
e759c9ed
AN
56 Str.get_strings([
57 {key: 'enroluserscohorts', component: 'enrol_manual'},
58 {key: 'enrolusers', component: 'enrol_manual'},
59 ]),
60 ModalFactory.create({
61 type: ModalFactory.types.SAVE_CANCEL,
62 large: true,
63 }, triggerButtons)
64 )
65 .then(function(strings, modal) {
b05f2cd7
AN
66 this.modal = modal;
67
e759c9ed
AN
68 modal.setTitle(strings[1]);
69 modal.setSaveButtonText(strings[1]);
70
71 modal.getRoot().on(ModalEvents.save, this.submitForm.bind(this));
72 modal.getRoot().on('submit', 'form', this.submitFormAjax.bind(this));
9b0f2923 73
e759c9ed
AN
74 // We want the reset the form every time it is opened.
75 modal.getRoot().on(ModalEvents.hidden, function() {
76 modal.setBody('');
9b0f2923
MN
77 });
78
e759c9ed 79 modal.getRoot().on(ModalEvents.shown, function() {
604887ce 80 var pendingPromise = new Pending('enrol_manual/quickenrolment:initModal:shown');
e759c9ed
AN
81 var bodyPromise = this.getBody();
82 bodyPromise.then(function(html) {
c50f5af2 83 var stringIndex = jQuery(html).find(Selectors.cohortSelector).length ? 0 : 1;
e759c9ed 84 modal.setSaveButtonText(strings[stringIndex]);
b05f2cd7 85
e759c9ed
AN
86 return;
87 })
604887ce
AN
88 .then(pendingPromise.resolve)
89 .catch(Notification.exception);
b05f2cd7 90
e759c9ed
AN
91 modal.setBody(bodyPromise);
92 }.bind(this));
b05f2cd7 93
e759c9ed 94 return;
b05f2cd7
AN
95 }.bind(this))
96 .fail(Notification.exception);
c50f5af2
AN
97
98 }
a60e8ba5
DW
99
100 /**
101 * This triggers a form submission, so that any mform elements can do final tricks before the form submission is processed.
102 *
103 * @method submitForm
2e1615f2 104 * @param {Event} e Form submission event.
a60e8ba5
DW
105 * @private
106 */
c50f5af2 107 submitForm(e) {
a60e8ba5
DW
108 e.preventDefault();
109 this.modal.getRoot().find('form').submit();
c50f5af2 110 }
a60e8ba5
DW
111
112 /**
113 * Private method
114 *
115 * @method submitForm
116 * @private
2e1615f2 117 * @param {Event} e Form submission event.
a60e8ba5 118 */
c50f5af2 119 submitFormAjax(e) {
a60e8ba5
DW
120 // We don't want to do a real form submission.
121 e.preventDefault();
122
7a963e92
SL
123 var form = this.modal.getRoot().find('form');
124
125 // Before send the data through AJAX, we need to parse and remove some unwanted hidden fields.
126 // This hidden fields are added automatically by mforms and when it reaches the AJAX we get an error.
c50f5af2 127 var hidden = form.find(Selectors.unwantedHiddenFields);
e759c9ed 128 hidden.each(function() {
c50f5af2 129 jQuery(this).remove();
7a963e92
SL
130 });
131
132 var formData = form.serialize();
a60e8ba5
DW
133
134 this.modal.hide();
135
136 var settings = {
137 type: 'GET',
138 processData: false,
139 contentType: "application/json"
140 };
141
142 var script = Config.wwwroot + '/enrol/manual/ajax.php?' + formData;
c50f5af2 143 jQuery.ajax(script, settings)
a60e8ba5
DW
144 .then(function(response) {
145
146 if (response.error) {
147 Notification.addNotification({
148 message: response.error,
149 type: "error"
150 });
151 } else {
152 // Reload the page, don't show changed data warnings.
153 if (typeof window.M.core_formchangechecker !== "undefined") {
154 window.M.core_formchangechecker.reset_form_dirty_state();
155 }
156 window.location.reload();
157 }
2e1615f2 158 return;
a60e8ba5
DW
159 })
160 .fail(Notification.exception);
c50f5af2 161 }
a60e8ba5
DW
162
163 /**
164 * Private method
165 *
166 * @method getBody
167 * @private
2e1615f2 168 * @return {Promise}
a60e8ba5 169 */
c50f5af2
AN
170 getBody() {
171 return Fragment.loadFragment('enrol_manual', 'enrol_users_form', this.contextId, {}).fail(Notification.exception);
172 }
a60e8ba5
DW
173
174 /**
175 * Private method
176 *
177 * @method getFooter
178 * @private
2e1615f2 179 * @return {Promise}
a60e8ba5 180 */
c50f5af2
AN
181 getFooter() {
182 return Templates.render('enrol_manual/enrol_modal_footer', {});
183 }
184};
185
186export const init = ({contextid}) => {
187 new QuickEnrolment(contextid);
188};