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 |
* Module to add purposes.
|
|
|
18 |
*
|
|
|
19 |
* @module tool_dataprivacy/add_purpose
|
|
|
20 |
* @copyright 2018 David Monllao
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
define([
|
|
|
24 |
'jquery',
|
|
|
25 |
'core/str',
|
|
|
26 |
'core/ajax',
|
|
|
27 |
'core/notification',
|
|
|
28 |
'core/modal_save_cancel',
|
|
|
29 |
'core/modal_events',
|
|
|
30 |
'core/fragment',
|
|
|
31 |
'core_form/changechecker',
|
|
|
32 |
], function(
|
|
|
33 |
$,
|
|
|
34 |
Str,
|
|
|
35 |
Ajax,
|
|
|
36 |
Notification,
|
|
|
37 |
ModalSaveCancel,
|
|
|
38 |
ModalEvents,
|
|
|
39 |
Fragment,
|
|
|
40 |
FormChangeChecker
|
|
|
41 |
) {
|
|
|
42 |
|
|
|
43 |
var SELECTORS = {
|
|
|
44 |
PURPOSE_LINK: '[data-add-element="purpose"]',
|
|
|
45 |
};
|
|
|
46 |
|
|
|
47 |
var AddPurpose = function(contextId) {
|
|
|
48 |
this.contextId = contextId;
|
|
|
49 |
|
|
|
50 |
var stringKeys = [
|
|
|
51 |
{
|
|
|
52 |
key: 'addpurpose',
|
|
|
53 |
component: 'tool_dataprivacy'
|
|
|
54 |
},
|
|
|
55 |
{
|
|
|
56 |
key: 'save',
|
|
|
57 |
component: 'admin'
|
|
|
58 |
}
|
|
|
59 |
];
|
|
|
60 |
this.strings = Str.get_strings(stringKeys);
|
|
|
61 |
|
|
|
62 |
this.registerEventListeners();
|
|
|
63 |
};
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* @var {int} contextId
|
|
|
67 |
* @private
|
|
|
68 |
*/
|
|
|
69 |
AddPurpose.prototype.contextId = 0;
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* @var {Promise}
|
|
|
73 |
* @private
|
|
|
74 |
*/
|
|
|
75 |
AddPurpose.prototype.strings = 0;
|
|
|
76 |
|
|
|
77 |
AddPurpose.prototype.registerEventListeners = function() {
|
|
|
78 |
|
|
|
79 |
var trigger = $(SELECTORS.PURPOSE_LINK);
|
|
|
80 |
trigger.on('click', function() {
|
|
|
81 |
this.strings.then(function(strings) {
|
|
|
82 |
return Promise.all([
|
|
|
83 |
ModalSaveCancel.create({
|
|
|
84 |
title: strings[0],
|
|
|
85 |
body: '',
|
|
|
86 |
}),
|
|
|
87 |
strings[1],
|
|
|
88 |
]).then(function([modal, string]) {
|
|
|
89 |
this.setupFormModal(modal, string);
|
|
|
90 |
}.bind(this));
|
|
|
91 |
}.bind(this))
|
|
|
92 |
.catch(Notification.exception);
|
|
|
93 |
}.bind(this));
|
|
|
94 |
|
|
|
95 |
};
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* @method getBody
|
|
|
99 |
* @param {Object} formdata
|
|
|
100 |
* @private
|
|
|
101 |
* @return {Promise}
|
|
|
102 |
*/
|
|
|
103 |
AddPurpose.prototype.getBody = function(formdata) {
|
|
|
104 |
|
|
|
105 |
var params = null;
|
|
|
106 |
if (typeof formdata !== "undefined") {
|
|
|
107 |
params = {jsonformdata: JSON.stringify(formdata)};
|
|
|
108 |
}
|
|
|
109 |
// Get the content of the modal.
|
|
|
110 |
return Fragment.loadFragment('tool_dataprivacy', 'addpurpose_form', this.contextId, params);
|
|
|
111 |
};
|
|
|
112 |
|
|
|
113 |
AddPurpose.prototype.setupFormModal = function(modal, saveText) {
|
|
|
114 |
modal.setLarge();
|
|
|
115 |
|
|
|
116 |
modal.setSaveButtonText(saveText);
|
|
|
117 |
|
|
|
118 |
// We want to reset the form every time it is opened.
|
|
|
119 |
modal.getRoot().on(ModalEvents.hidden, this.destroy.bind(this));
|
|
|
120 |
|
|
|
121 |
modal.setBody(this.getBody());
|
|
|
122 |
|
|
|
123 |
// We catch the modal save event, and use it to submit the form inside the modal.
|
|
|
124 |
// Triggering a form submission will give JS validation scripts a chance to check for errors.
|
|
|
125 |
modal.getRoot().on(ModalEvents.save, this.submitForm.bind(this));
|
|
|
126 |
// We also catch the form submit event and use it to submit the form with ajax.
|
|
|
127 |
modal.getRoot().on('submit', 'form', this.submitFormAjax.bind(this));
|
|
|
128 |
|
|
|
129 |
this.modal = modal;
|
|
|
130 |
|
|
|
131 |
modal.show();
|
|
|
132 |
};
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* This triggers a form submission, so that any mform elements can do final tricks before the form submission is processed.
|
|
|
136 |
*
|
|
|
137 |
* @method submitForm
|
|
|
138 |
* @param {Event} e Form submission event.
|
|
|
139 |
* @private
|
|
|
140 |
*/
|
|
|
141 |
AddPurpose.prototype.submitForm = function(e) {
|
|
|
142 |
e.preventDefault();
|
|
|
143 |
this.modal.getRoot().find('form').submit();
|
|
|
144 |
};
|
|
|
145 |
|
|
|
146 |
AddPurpose.prototype.submitFormAjax = function(e) {
|
|
|
147 |
// We don't want to do a real form submission.
|
|
|
148 |
e.preventDefault();
|
|
|
149 |
|
|
|
150 |
// Convert all the form elements values to a serialised string.
|
|
|
151 |
var formData = this.modal.getRoot().find('form').serialize();
|
|
|
152 |
|
|
|
153 |
Ajax.call([{
|
|
|
154 |
methodname: 'tool_dataprivacy_create_purpose_form',
|
|
|
155 |
args: {jsonformdata: JSON.stringify(formData)},
|
|
|
156 |
done: function(data) {
|
|
|
157 |
if (data.validationerrors) {
|
|
|
158 |
this.modal.setBody(this.getBody(formData));
|
|
|
159 |
} else {
|
|
|
160 |
this.close();
|
|
|
161 |
}
|
|
|
162 |
}.bind(this),
|
|
|
163 |
|
|
|
164 |
fail: Notification.exception
|
|
|
165 |
}]);
|
|
|
166 |
};
|
|
|
167 |
|
|
|
168 |
AddPurpose.prototype.close = function() {
|
|
|
169 |
this.destroy();
|
|
|
170 |
document.location.reload();
|
|
|
171 |
};
|
|
|
172 |
|
|
|
173 |
AddPurpose.prototype.destroy = function() {
|
|
|
174 |
FormChangeChecker.resetAllFormDirtyStates();
|
|
|
175 |
this.modal.destroy();
|
|
|
176 |
};
|
|
|
177 |
|
|
|
178 |
AddPurpose.prototype.removeListeners = function() {
|
|
|
179 |
$(SELECTORS.PURPOSE_LINK).off('click');
|
|
|
180 |
};
|
|
|
181 |
|
|
|
182 |
return /** @alias module:tool_dataprivacy/add_purpose */ {
|
|
|
183 |
getInstance: function(contextId) {
|
|
|
184 |
return new AddPurpose(contextId);
|
|
|
185 |
}
|
|
|
186 |
};
|
|
|
187 |
}
|
|
|
188 |
);
|