Merge branch 'MDL-69089-master' of git://github.com/aanabit/moodle
[moodle.git] / contentbank / amd / src / actions.js
CommitLineData
45192e9a
SA
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 manage content bank actions, such as delete or rename.
18 *
19 * @module core_contentbank/actions
20 * @package core_contentbank
21 * @copyright 2020 Sara Arjona <sara@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24define([
25 'jquery',
26 'core/ajax',
27 'core/notification',
28 'core/str',
29 'core/templates',
30 'core/url',
31 'core/modal_factory',
32 'core/modal_events'],
33function($, Ajax, Notification, Str, Templates, Url, ModalFactory, ModalEvents) {
34
35 /**
36 * List of action selectors.
37 *
38 * @type {{DELETE_CONTENT: string}}
39 */
40 var ACTIONS = {
41 DELETE_CONTENT: '[data-action="deletecontent"]',
448012c6 42 RENAME_CONTENT: '[data-action="renamecontent"]',
45192e9a
SA
43 };
44
45 /**
46 * Actions class.
47 */
48 var Actions = function() {
49 this.registerEvents();
50 };
51
52 /**
53 * Register event listeners.
54 */
55 Actions.prototype.registerEvents = function() {
56 $(ACTIONS.DELETE_CONTENT).click(function(e) {
57 e.preventDefault();
58
59 var contentname = $(this).data('contentname');
60 var contentid = $(this).data('contentid');
61 var contextid = $(this).data('contextid');
62
63 var strings = [
64 {
65 key: 'deletecontent',
66 component: 'core_contentbank'
67 },
68 {
69 key: 'deletecontentconfirm',
70 component: 'core_contentbank',
71 param: {
72 name: contentname,
73 }
74 },
75 {
76 key: 'delete',
77 component: 'core'
78 },
79 ];
80
81 var deleteButtonText = '';
82 Str.get_strings(strings).then(function(langStrings) {
83 var modalTitle = langStrings[0];
84 var modalContent = langStrings[1];
85 deleteButtonText = langStrings[2];
86
87 return ModalFactory.create({
88 title: modalTitle,
89 body: modalContent,
90 type: ModalFactory.types.SAVE_CANCEL,
91 large: true
92 });
93 }).done(function(modal) {
94 modal.setSaveButtonText(deleteButtonText);
95 modal.getRoot().on(ModalEvents.save, function() {
96 // The action is now confirmed, sending an action for it.
97 return deleteContent(contentid, contextid);
98 });
99
100 // Handle hidden event.
101 modal.getRoot().on(ModalEvents.hidden, function() {
102 // Destroy when hidden.
103 modal.destroy();
104 });
105
106 // Show the modal.
107 modal.show();
108
109 return;
110 }).catch(Notification.exception);
111 });
448012c6
AA
112
113 $(ACTIONS.RENAME_CONTENT).click(function(e) {
114 e.preventDefault();
115
116 var contentname = $(this).data('contentname');
117 var contentid = $(this).data('contentid');
118
119 var strings = [
120 {
121 key: 'renamecontent',
122 component: 'core_contentbank'
123 },
124 {
125 key: 'rename',
126 component: 'core_contentbank'
127 },
128 ];
129
130 var saveButtonText = '';
131 Str.get_strings(strings).then(function(langStrings) {
132 var modalTitle = langStrings[0];
133 saveButtonText = langStrings[1];
134
135 return ModalFactory.create({
136 title: modalTitle,
137 body: Templates.render('core_contentbank/renamecontent', {'contentid': contentid, 'name': contentname}),
138 type: ModalFactory.types.SAVE_CANCEL
139 });
140 }).then(function(modal) {
141 modal.setSaveButtonText(saveButtonText);
5ea98dc5 142 modal.getRoot().on(ModalEvents.save, function(e) {
448012c6 143 // The action is now confirmed, sending an action for it.
5ea98dc5
AA
144 var newname = $("#newname").val().trim();
145 if (newname) {
146 renameContent(contentid, newname);
147 } else {
148 var errorStrings = [
149 {
150 key: 'error',
151 },
152 {
153 key: 'emptynamenotallowed',
154 component: 'core_contentbank',
155 },
156 ];
157 Str.get_strings(errorStrings).then(function(langStrings) {
158 Notification.alert(langStrings[0], langStrings[1]);
159 }).catch(Notification.exception);
160 e.preventDefault();
161 }
448012c6
AA
162 });
163
164 // Handle hidden event.
165 modal.getRoot().on(ModalEvents.hidden, function() {
166 // Destroy when hidden.
167 modal.destroy();
168 });
169
170 // Show the modal.
171 modal.show();
172
173 return;
174 }).catch(Notification.exception);
175 });
45192e9a
SA
176 };
177
178 /**
179 * Delete content from the content bank.
180 *
181 * @param {int} contentid The content to delete.
182 * @param {int} contextid The contextid where the content belongs.
183 */
184 function deleteContent(contentid, contextid) {
185 var request = {
186 methodname: 'core_contentbank_delete_content',
187 args: {
188 contentids: {contentid}
189 }
190 };
191
192 var requestType = 'success';
193 Ajax.call([request])[0].then(function(data) {
194 if (data.result) {
088786d2 195 return 'contentdeleted';
45192e9a
SA
196 }
197 requestType = 'error';
088786d2 198 return 'contentnotdeleted';
45192e9a
SA
199
200 }).done(function(message) {
201 var params = {
202 contextid: contextid
203 };
204 if (requestType == 'success') {
205 params.statusmsg = message;
206 } else {
207 params.errormsg = message;
208 }
209 // Redirect to the main content bank page and display the message as a notification.
210 window.location.href = Url.relativeUrl('contentbank/index.php', params, false);
211 }).fail(Notification.exception);
212 }
213
448012c6
AA
214 /**
215 * Rename content in the content bank.
216 *
217 * @param {int} contentid The content to rename.
218 * @param {string} name The new name for the content.
219 */
220 function renameContent(contentid, name) {
221 var request = {
222 methodname: 'core_contentbank_rename_content',
223 args: {
224 contentid: contentid,
225 name: name
226 }
227 };
228 var requestType = 'success';
229 Ajax.call([request])[0].then(function(data) {
5ea98dc5 230 if (data.result) {
088786d2 231 return 'contentrenamed';
448012c6
AA
232 }
233 requestType = 'error';
5ea98dc5 234 return data.warnings[0].message;
448012c6
AA
235
236 }).then(function(message) {
237 var params = null;
238 if (requestType == 'success') {
239 params = {
240 id: contentid,
241 statusmsg: message
242 };
243 // Redirect to the content view page and display the message as a notification.
244 window.location.href = Url.relativeUrl('contentbank/view.php', params, false);
245 } else {
246 // Fetch error notifications.
247 Notification.addNotification({
248 message: message,
249 type: 'error'
250 });
251 Notification.fetchNotifications();
252 }
253 return;
254 }).catch(Notification.exception);
255 }
256
45192e9a
SA
257 return /** @alias module:core_contentbank/actions */ {
258 // Public variables and functions.
259
260 /**
261 * Initialise the contentbank actions.
262 *
263 * @method init
264 * @return {Actions}
265 */
266 'init': function() {
267 return new Actions();
268 }
269 };
270});