on-demand release 3.9dev+
[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 */
23define(['core/templates',
24 'jquery',
25 'core/str',
26 'core/config',
27 'core/notification',
28 'core/modal_factory',
29 'core/modal_events',
30 'core/fragment',
604887ce 31 'core/pending',
a60e8ba5 32 ],
604887ce 33 function(Template, $, Str, Config, Notification, ModalFactory, ModalEvents, Fragment, Pending) {
a60e8ba5 34
9b0f2923
MN
35 /** @type {Object} The list of selectors for the quick enrolment modal. */
36 var SELECTORS = {
37 COHORTSELECT: "#id_cohortlist",
38 TRIGGERBUTTONS: ".enrolusersbutton.enrol_manual_plugin [type='submit']",
39 UNWANTEDHIDDENFIELDS: ":input[value='_qf__force_multiselect_submission']"
40 };
41
a60e8ba5
DW
42 /**
43 * Constructor
44 *
2e1615f2 45 * @param {Object} options Object containing options. The only valid option at this time is contextid.
a60e8ba5
DW
46 * Each call to templates.render gets it's own instance of this class.
47 */
48 var QuickEnrolment = function(options) {
49 this.contextid = options.contextid;
50
51 this.initModal();
52 };
53 // Class variables and functions.
54
55 /** @var {number} courseid - */
56 QuickEnrolment.prototype.courseid = 0;
57
58 /** @var {Modal} modal */
59 QuickEnrolment.prototype.modal = null;
60
61 /**
62 * Private method
63 *
64 * @method initModal
65 * @private
66 */
67 QuickEnrolment.prototype.initModal = function() {
9b0f2923 68 var triggerButtons = $(SELECTORS.TRIGGERBUTTONS);
a60e8ba5 69
e759c9ed
AN
70 $.when(
71 Str.get_strings([
72 {key: 'enroluserscohorts', component: 'enrol_manual'},
73 {key: 'enrolusers', component: 'enrol_manual'},
74 ]),
75 ModalFactory.create({
76 type: ModalFactory.types.SAVE_CANCEL,
77 large: true,
78 }, triggerButtons)
79 )
80 .then(function(strings, modal) {
b05f2cd7
AN
81 this.modal = modal;
82
e759c9ed
AN
83 modal.setTitle(strings[1]);
84 modal.setSaveButtonText(strings[1]);
85
86 modal.getRoot().on(ModalEvents.save, this.submitForm.bind(this));
87 modal.getRoot().on('submit', 'form', this.submitFormAjax.bind(this));
9b0f2923 88
e759c9ed
AN
89 // We want the reset the form every time it is opened.
90 modal.getRoot().on(ModalEvents.hidden, function() {
91 modal.setBody('');
9b0f2923
MN
92 });
93
e759c9ed 94 modal.getRoot().on(ModalEvents.shown, function() {
604887ce 95 var pendingPromise = new Pending('enrol_manual/quickenrolment:initModal:shown');
e759c9ed
AN
96 var bodyPromise = this.getBody();
97 bodyPromise.then(function(html) {
98 var stringIndex = $(html).find(SELECTORS.COHORTSELECT).length ? 0 : 1;
99 modal.setSaveButtonText(strings[stringIndex]);
b05f2cd7 100
e759c9ed
AN
101 return;
102 })
604887ce
AN
103 .then(pendingPromise.resolve)
104 .catch(Notification.exception);
b05f2cd7 105
e759c9ed
AN
106 modal.setBody(bodyPromise);
107 }.bind(this));
b05f2cd7 108
e759c9ed 109 return;
b05f2cd7
AN
110 }.bind(this))
111 .fail(Notification.exception);
a60e8ba5
DW
112 };
113
114 /**
115 * This triggers a form submission, so that any mform elements can do final tricks before the form submission is processed.
116 *
117 * @method submitForm
2e1615f2 118 * @param {Event} e Form submission event.
a60e8ba5
DW
119 * @private
120 */
121 QuickEnrolment.prototype.submitForm = function(e) {
122 e.preventDefault();
123 this.modal.getRoot().find('form').submit();
124 };
125
126 /**
127 * Private method
128 *
129 * @method submitForm
130 * @private
2e1615f2 131 * @param {Event} e Form submission event.
a60e8ba5
DW
132 */
133 QuickEnrolment.prototype.submitFormAjax = function(e) {
134 // We don't want to do a real form submission.
135 e.preventDefault();
136
7a963e92
SL
137 var form = this.modal.getRoot().find('form');
138
139 // Before send the data through AJAX, we need to parse and remove some unwanted hidden fields.
140 // This hidden fields are added automatically by mforms and when it reaches the AJAX we get an error.
9b0f2923 141 var hidden = form.find(SELECTORS.UNWANTEDHIDDENFIELDS);
e759c9ed 142 hidden.each(function() {
e6a87bd1 143 $(this).remove();
7a963e92
SL
144 });
145
146 var formData = form.serialize();
a60e8ba5
DW
147
148 this.modal.hide();
149
150 var settings = {
151 type: 'GET',
152 processData: false,
153 contentType: "application/json"
154 };
155
156 var script = Config.wwwroot + '/enrol/manual/ajax.php?' + formData;
157 $.ajax(script, settings)
158 .then(function(response) {
159
160 if (response.error) {
161 Notification.addNotification({
162 message: response.error,
163 type: "error"
164 });
165 } else {
166 // Reload the page, don't show changed data warnings.
167 if (typeof window.M.core_formchangechecker !== "undefined") {
168 window.M.core_formchangechecker.reset_form_dirty_state();
169 }
170 window.location.reload();
171 }
2e1615f2 172 return;
a60e8ba5
DW
173 })
174 .fail(Notification.exception);
175 };
176
177 /**
178 * Private method
179 *
180 * @method getBody
181 * @private
2e1615f2 182 * @return {Promise}
a60e8ba5
DW
183 */
184 QuickEnrolment.prototype.getBody = function() {
185 return Fragment.loadFragment('enrol_manual', 'enrol_users_form', this.contextid, {}).fail(Notification.exception);
186 };
187
188 /**
189 * Private method
190 *
191 * @method getFooter
192 * @private
2e1615f2 193 * @return {Promise}
a60e8ba5
DW
194 */
195 QuickEnrolment.prototype.getFooter = function() {
196 return Template.render('enrol_manual/enrol_modal_footer', {});
197 };
198
199 return /** @alias module:enrol_manual/quickenrolment */ {
200 // Public variables and functions.
201 /**
202 * Every call to init creates a new instance of the class with it's own event listeners etc.
203 *
204 * @method init
205 * @public
206 * @param {object} config - config variables for the module.
207 */
208 init: function(config) {
209 (new QuickEnrolment(config));
210 }
211 };
212});