MDL-59859 enrol_manual: Improve modal creation ordering
[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',
31 ],
32 function(Template, $, Str, Config, Notification, ModalFactory, ModalEvents, Fragment) {
33
34 /**
35 * Constructor
36 *
2e1615f2 37 * @param {Object} options Object containing options. The only valid option at this time is contextid.
a60e8ba5
DW
38 * Each call to templates.render gets it's own instance of this class.
39 */
40 var QuickEnrolment = function(options) {
41 this.contextid = options.contextid;
42
43 this.initModal();
44 };
45 // Class variables and functions.
46
47 /** @var {number} courseid - */
48 QuickEnrolment.prototype.courseid = 0;
49
50 /** @var {Modal} modal */
51 QuickEnrolment.prototype.modal = null;
52
53 /**
54 * Private method
55 *
56 * @method initModal
57 * @private
58 */
59 QuickEnrolment.prototype.initModal = function() {
60 var triggerButtons = $('.enrolusersbutton.enrol_manual_plugin [type="submit"]');
61
b05f2cd7 62 var stringsPromise = Str.get_strings([
d77c73a3 63 {key: 'enroluserscohorts', component: 'enrol_manual'},
b05f2cd7
AN
64 {key: 'enrolusers', component: 'enrol_manual'},
65 ]);
66
67 var titlePromise = stringsPromise.then(function(strings) {
68 return strings[1];
69 });
70
71 var buttonPromise = stringsPromise.then(function(strings) {
72 return strings[0];
73 });
74
75 return ModalFactory.create({
76 type: ModalFactory.types.SAVE_CANCEL,
77 large: true,
78 title: titlePromise,
79 body: this.getBody()
80 }, triggerButtons)
81 .then(function(modal) {
82 this.modal = modal;
83
84 this.modal.setSaveButtonText(buttonPromise);
85
86 // We want the reset the form every time it is opened.
87 this.modal.getRoot().on(ModalEvents.hidden, function() {
88 this.modal.setBody(this.getBody());
a60e8ba5 89 }.bind(this));
b05f2cd7
AN
90
91 this.modal.getRoot().on(ModalEvents.save, this.submitForm.bind(this));
92 this.modal.getRoot().on('submit', 'form', this.submitFormAjax.bind(this));
93
94 return modal;
95 }.bind(this))
96 .fail(Notification.exception);
a60e8ba5
DW
97 };
98
99 /**
100 * This triggers a form submission, so that any mform elements can do final tricks before the form submission is processed.
101 *
102 * @method submitForm
2e1615f2 103 * @param {Event} e Form submission event.
a60e8ba5
DW
104 * @private
105 */
106 QuickEnrolment.prototype.submitForm = function(e) {
107 e.preventDefault();
108 this.modal.getRoot().find('form').submit();
109 };
110
111 /**
112 * Private method
113 *
114 * @method submitForm
115 * @private
2e1615f2 116 * @param {Event} e Form submission event.
a60e8ba5
DW
117 */
118 QuickEnrolment.prototype.submitFormAjax = function(e) {
119 // We don't want to do a real form submission.
120 e.preventDefault();
121
122 var formData = this.modal.getRoot().find('form').serialize();
123
124 this.modal.hide();
125
126 var settings = {
127 type: 'GET',
128 processData: false,
129 contentType: "application/json"
130 };
131
132 var script = Config.wwwroot + '/enrol/manual/ajax.php?' + formData;
133 $.ajax(script, settings)
134 .then(function(response) {
135
136 if (response.error) {
137 Notification.addNotification({
138 message: response.error,
139 type: "error"
140 });
141 } else {
142 // Reload the page, don't show changed data warnings.
143 if (typeof window.M.core_formchangechecker !== "undefined") {
144 window.M.core_formchangechecker.reset_form_dirty_state();
145 }
146 window.location.reload();
147 }
2e1615f2 148 return;
a60e8ba5
DW
149 })
150 .fail(Notification.exception);
151 };
152
153 /**
154 * Private method
155 *
156 * @method getBody
157 * @private
2e1615f2 158 * @return {Promise}
a60e8ba5
DW
159 */
160 QuickEnrolment.prototype.getBody = function() {
161 return Fragment.loadFragment('enrol_manual', 'enrol_users_form', this.contextid, {}).fail(Notification.exception);
162 };
163
164 /**
165 * Private method
166 *
167 * @method getFooter
168 * @private
2e1615f2 169 * @return {Promise}
a60e8ba5
DW
170 */
171 QuickEnrolment.prototype.getFooter = function() {
172 return Template.render('enrol_manual/enrol_modal_footer', {});
173 };
174
175 return /** @alias module:enrol_manual/quickenrolment */ {
176 // Public variables and functions.
177 /**
178 * Every call to init creates a new instance of the class with it's own event listeners etc.
179 *
180 * @method init
181 * @public
182 * @param {object} config - config variables for the module.
183 */
184 init: function(config) {
185 (new QuickEnrolment(config));
186 }
187 };
188});