Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 manage content bank actions, such as delete or rename.
18
 *
19
 * @module     core_contentbank/actions
20
 * @copyright  2020 Sara Arjona <sara@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define([
24
    'jquery',
25
    'core/ajax',
26
    'core/notification',
27
    'core/str',
28
    'core/templates',
29
    'core/url',
30
    'core/modal_save_cancel',
31
    'core/modal_events'],
32
function($, Ajax, Notification, Str, Templates, Url, ModalSaveCancel, ModalEvents) {
33
 
34
    /**
35
     * List of action selectors.
36
     *
37
     * @type {{DELETE_CONTENT: string}}
38
     */
39
    var ACTIONS = {
40
        DELETE_CONTENT: '[data-action="deletecontent"]',
41
        RENAME_CONTENT: '[data-action="renamecontent"]',
42
        SET_CONTENT_VISIBILITY: '[data-action="setcontentvisibility"]',
43
        COPY_CONTENT: '[data-action="copycontent"]',
44
    };
45
 
46
    /**
47
     * Actions class.
48
     */
49
    var Actions = function() {
50
        this.registerEvents();
51
    };
52
 
53
    /**
54
     * Register event listeners.
55
     */
56
    Actions.prototype.registerEvents = function() {
57
        $(ACTIONS.DELETE_CONTENT).click(function(e) {
58
            e.preventDefault();
59
 
60
            var contentname = $(this).data('contentname');
61
            var contentuses = $(this).data('uses');
62
            var contentid = $(this).data('contentid');
63
            var contextid = $(this).data('contextid');
64
 
65
            var strings = [
66
                {
67
                    key: 'deletecontent',
68
                    component: 'core_contentbank'
69
                },
70
                {
71
                    key: 'deletecontentconfirm',
72
                    component: 'core_contentbank',
73
                    param: {
74
                        name: contentname,
75
                    }
76
                },
77
                {
78
                    key: 'deletecontentconfirmlinked',
79
                    component: 'core_contentbank',
80
                },
81
                {
82
                    key: 'delete',
83
                    component: 'core'
84
                },
85
            ];
86
 
87
            var deleteButtonText = '';
88
            Str.get_strings(strings).then(function(langStrings) {
89
                var modalTitle = langStrings[0];
90
                var modalContent = langStrings[1];
91
                if (contentuses > 0) {
92
                    modalContent += ' ' + langStrings[2];
93
                }
94
                deleteButtonText = langStrings[3];
95
 
96
                return ModalSaveCancel.create({
97
                    title: modalTitle,
98
                    body: modalContent,
99
                    large: true,
100
                    removeOnClose: true,
101
                    show: true,
102
                    buttons: {
103
                        save: deleteButtonText,
104
                    },
105
                });
106
            }).then(function(modal) {
107
                modal.getRoot().on(ModalEvents.save, function() {
108
                    // The action is now confirmed, sending an action for it.
109
                    return deleteContent(contentid, contextid);
110
                });
111
 
112
                return;
113
            }).catch(Notification.exception);
114
        });
115
 
116
        $(ACTIONS.RENAME_CONTENT).click(function(e) {
117
            e.preventDefault();
118
 
119
            var contentname = $(this).data('contentname');
120
            var contentid = $(this).data('contentid');
121
 
122
            var strings = [
123
                {
124
                    key: 'renamecontent',
125
                    component: 'core_contentbank'
126
                },
127
                {
128
                    key: 'rename',
129
                    component: 'core_contentbank'
130
                },
131
            ];
132
 
133
            var saveButtonText = '';
134
            Str.get_strings(strings).then(function(langStrings) {
135
                var modalTitle = langStrings[0];
136
                saveButtonText = langStrings[1];
137
 
138
                return ModalSaveCancel.create({
139
                    title: modalTitle,
140
                    body: Templates.render('core_contentbank/renamecontent', {'contentid': contentid, 'name': contentname}),
141
                    removeOnClose: true,
142
                    show: true,
143
                    buttons: {
144
                        save: saveButtonText,
145
                    },
146
                });
147
            }).then(function(modal) {
148
                modal.getRoot().on(ModalEvents.save, function(e) {
149
                    // The action is now confirmed, sending an action for it.
150
                    var newname = $("#newname").val().trim();
151
                    if (newname) {
152
                        renameContent(contentid, newname);
153
                    } else {
154
                        var errorStrings = [
155
                            {
156
                                key: 'error',
157
                            },
158
                            {
159
                                key: 'emptynamenotallowed',
160
                                component: 'core_contentbank',
161
                            },
162
                        ];
163
                        Str.get_strings(errorStrings).then(function(langStrings) {
164
                            Notification.alert(langStrings[0], langStrings[1]);
165
                        }).catch(Notification.exception);
166
                        e.preventDefault();
167
                    }
168
                });
169
 
170
                return;
171
            }).catch(Notification.exception);
172
        });
173
 
174
        $(ACTIONS.COPY_CONTENT).click(function(e) {
175
            e.preventDefault();
176
 
177
            var contentname = $(this).data('contentname');
178
            var contentid = $(this).data('contentid');
179
 
180
            var strings = [
181
                {
182
                    key: 'copycontent',
183
                    component: 'core_contentbank'
184
                },
185
                {
186
                    key: 'error',
187
                },
188
                {
189
                    key: 'emptynamenotallowed',
190
                    component: 'core_contentbank',
191
                },
192
            ];
193
 
194
            let errorTitle, errorMessage;
195
            Str.get_strings(strings).then(function(langStrings) {
196
                var modalTitle = langStrings[0];
197
                errorTitle = langStrings[1];
198
                errorMessage = langStrings[2];
199
 
200
                return ModalSaveCancel.create({
201
                    title: modalTitle,
202
                    body: Templates.render('core_contentbank/copycontent', {'contentid': contentid, 'name': contentname}),
203
                    removeOnClose: true,
204
                    show: true,
205
                });
206
            }).then(function(modal) {
207
                modal.getRoot().on(ModalEvents.save, function() {
208
                    // The action is now confirmed, sending an action for it.
209
                    var newname = $("#newname").val().trim();
210
                    if (newname) {
211
                        copyContent(contentid, newname);
212
                    } else {
213
                        Notification.alert(errorTitle, errorMessage);
214
                        return false;
215
                    }
216
                });
217
                return;
218
            }).catch(Notification.exception);
219
        });
220
 
221
        $(ACTIONS.SET_CONTENT_VISIBILITY).click(function(e) {
222
            e.preventDefault();
223
 
224
            var contentid = $(this).data('contentid');
225
            var visibility = $(this).data('visibility');
226
 
227
            setContentVisibility(contentid, visibility);
228
        });
229
    };
230
 
231
    /**
232
     * Delete content from the content bank.
233
     *
234
     * @param {int} contentid The content to delete.
235
     * @param {int} contextid The contextid where the content belongs.
236
     */
237
    function deleteContent(contentid, contextid) {
238
        var request = {
239
            methodname: 'core_contentbank_delete_content',
240
            args: {
241
                contentids: {contentid}
242
            }
243
        };
244
 
245
        var requestType = 'success';
246
        Ajax.call([request])[0].then(function(data) {
247
            if (data.result) {
248
                return 'contentdeleted';
249
            }
250
            requestType = 'error';
251
            return 'contentnotdeleted';
252
 
253
        }).done(function(message) {
254
            var params = {
255
                contextid: contextid
256
            };
257
            if (requestType == 'success') {
258
                params.statusmsg = message;
259
            } else {
260
                params.errormsg = message;
261
            }
262
            // Redirect to the main content bank page and display the message as a notification.
263
            window.location.href = Url.relativeUrl('contentbank/index.php', params, false);
264
        }).fail(Notification.exception);
265
    }
266
 
267
    /**
268
     * Rename content in the content bank.
269
     *
270
     * @param {int} contentid The content to rename.
271
     * @param {string} name The new name for the content.
272
     */
273
    function renameContent(contentid, name) {
274
        var request = {
275
            methodname: 'core_contentbank_rename_content',
276
            args: {
277
                contentid: contentid,
278
                name: name
279
            }
280
        };
281
        var requestType = 'success';
282
        Ajax.call([request])[0].then(function(data) {
283
            if (data.result) {
284
                return 'contentrenamed';
285
            }
286
            requestType = 'error';
287
            return data.warnings[0].message;
288
 
289
        }).then(function(message) {
290
            var params = null;
291
            if (requestType == 'success') {
292
                params = {
293
                    id: contentid,
294
                    statusmsg: message
295
                };
296
                // Redirect to the content view page and display the message as a notification.
297
                window.location.href = Url.relativeUrl('contentbank/view.php', params, false);
298
            } else {
299
                // Fetch error notifications.
300
                Notification.addNotification({
301
                    message: message,
302
                    type: 'error'
303
                });
304
                Notification.fetchNotifications();
305
            }
306
            return;
307
        }).catch(Notification.exception);
308
    }
309
 
310
    /**
311
     * Copy content in the content bank.
312
     *
313
     * @param {int} contentid The content to copy.
314
     * @param {string} name The name for the new content.
315
     */
316
    function copyContent(contentid, name) {
317
        var request = {
318
            methodname: 'core_contentbank_copy_content',
319
            args: {
320
                contentid: contentid,
321
                name: name
322
            }
323
        };
324
        Ajax.call([request])[0].then(function(data) {
325
            if (data.id == 0) {
326
                // Fetch error notifications.
327
                Notification.addNotification({
328
                    message: data.warnings[0].message,
329
                    type: 'error'
330
                });
331
                Notification.fetchNotifications();
332
                return data.warnings[0].message;
333
            } else {
334
                let params = {
335
                    id: data.id,
336
                    statusmsg: 'contentcopied'
337
                };
338
                // Redirect to the content view page and display the message as a notification.
339
                window.location.href = Url.relativeUrl('contentbank/view.php', params, false);
340
            }
341
            return '';
342
        }).catch(Notification.exception);
343
    }
344
 
345
    /**
346
     * Set content visibility in the content bank.
347
     *
348
     * @param {int} contentid The content to modify
349
     * @param {int} visibility The new visibility value
350
     */
351
    function setContentVisibility(contentid, visibility) {
352
        var request = {
353
            methodname: 'core_contentbank_set_content_visibility',
354
            args: {
355
                contentid: contentid,
356
                visibility: visibility
357
            }
358
        };
359
        var requestType = 'success';
360
        Ajax.call([request])[0].then(function(data) {
361
            if (data.result) {
362
                return 'contentvisibilitychanged';
363
            }
364
            requestType = 'error';
365
            return data.warnings[0].message;
366
 
367
        }).then(function(message) {
368
            var params = null;
369
            if (requestType == 'success') {
370
                params = {
371
                    id: contentid,
372
                    statusmsg: message
373
                };
374
                // Redirect to the content view page and display the message as a notification.
375
                window.location.href = Url.relativeUrl('contentbank/view.php', params, false);
376
            } else {
377
                // Fetch error notifications.
378
                Notification.addNotification({
379
                    message: message,
380
                    type: 'error'
381
                });
382
                Notification.fetchNotifications();
383
            }
384
            return;
385
        }).catch(Notification.exception);
386
    }
387
 
388
    return /** @alias module:core_contentbank/actions */ {
389
        // Public variables and functions.
390
 
391
        /**
392
         * Initialise the contentbank actions.
393
         *
394
         * @method init
395
         * @return {Actions}
396
         */
397
        'init': function() {
398
            return new Actions();
399
        }
400
    };
401
});