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