MDL-69047 core_contentbank: Hard coding status messages
[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);
142 modal.getRoot().on(ModalEvents.save, function() {
143 // The action is now confirmed, sending an action for it.
144 var newname = $("#newname").val();
145 return renameContent(contentid, newname);
146 });
147
148 // Handle hidden event.
149 modal.getRoot().on(ModalEvents.hidden, function() {
150 // Destroy when hidden.
151 modal.destroy();
152 });
153
154 // Show the modal.
155 modal.show();
156
157 return;
158 }).catch(Notification.exception);
159 });
45192e9a
SA
160 };
161
162 /**
163 * Delete content from the content bank.
164 *
165 * @param {int} contentid The content to delete.
166 * @param {int} contextid The contextid where the content belongs.
167 */
168 function deleteContent(contentid, contextid) {
169 var request = {
170 methodname: 'core_contentbank_delete_content',
171 args: {
172 contentids: {contentid}
173 }
174 };
175
176 var requestType = 'success';
177 Ajax.call([request])[0].then(function(data) {
178 if (data.result) {
4f0f1bd8 179 return 'contentdeleted';
45192e9a
SA
180 }
181 requestType = 'error';
4f0f1bd8 182 return 'contentnotdeleted';
45192e9a
SA
183
184 }).done(function(message) {
185 var params = {
186 contextid: contextid
187 };
188 if (requestType == 'success') {
189 params.statusmsg = message;
190 } else {
191 params.errormsg = message;
192 }
193 // Redirect to the main content bank page and display the message as a notification.
194 window.location.href = Url.relativeUrl('contentbank/index.php', params, false);
195 }).fail(Notification.exception);
196 }
197
448012c6
AA
198 /**
199 * Rename content in the content bank.
200 *
201 * @param {int} contentid The content to rename.
202 * @param {string} name The new name for the content.
203 */
204 function renameContent(contentid, name) {
205 var request = {
206 methodname: 'core_contentbank_rename_content',
207 args: {
208 contentid: contentid,
209 name: name
210 }
211 };
212 var requestType = 'success';
213 Ajax.call([request])[0].then(function(data) {
214 if (data) {
4f0f1bd8 215 return 'contentrenamed';
448012c6
AA
216 }
217 requestType = 'error';
4f0f1bd8 218 return 'contentnotrenamed';
448012c6
AA
219
220 }).then(function(message) {
221 var params = null;
222 if (requestType == 'success') {
223 params = {
224 id: contentid,
225 statusmsg: message
226 };
227 // Redirect to the content view page and display the message as a notification.
228 window.location.href = Url.relativeUrl('contentbank/view.php', params, false);
229 } else {
230 // Fetch error notifications.
231 Notification.addNotification({
232 message: message,
233 type: 'error'
234 });
235 Notification.fetchNotifications();
236 }
237 return;
238 }).catch(Notification.exception);
239 }
240
45192e9a
SA
241 return /** @alias module:core_contentbank/actions */ {
242 // Public variables and functions.
243
244 /**
245 * Initialise the contentbank actions.
246 *
247 * @method init
248 * @return {Actions}
249 */
250 'init': function() {
251 return new Actions();
252 }
253 };
254});