Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | 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
 * Various actions on modules and sections in the editing mode - hiding, duplicating, deleting, etc.
18
 *
1441 ariadna 19
 * TODO remove this module as part of MDL-83627.
20
 *
1 efrain 21
 * @module     core_course/actions
22
 * @copyright  2016 Marina Glancy
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @since      3.3
25
 */
26
define(
27
    [
28
        'jquery',
29
        'core/ajax',
30
        'core/templates',
31
        'core/notification',
32
        'core/str',
33
        'core/url',
34
        'core/yui',
35
        'core/modal_copy_to_clipboard',
36
        'core/modal_save_cancel',
37
        'core/modal_events',
38
        'core/key_codes',
39
        'core/log',
40
        'core_courseformat/courseeditor',
41
        'core/event_dispatcher',
1441 ariadna 42
        'core/local/inplace_editable/events',
1 efrain 43
        'core_course/events'
44
    ],
45
    function(
46
        $,
47
        ajax,
48
        templates,
49
        notification,
50
        str,
51
        url,
52
        Y,
53
        ModalCopyToClipboard,
54
        ModalSaveCancel,
55
        ModalEvents,
56
        KeyCodes,
57
        log,
58
        editor,
59
        EventDispatcher,
1441 ariadna 60
        InplaceEditableEvents,
1 efrain 61
        CourseEvents
62
    ) {
1441 ariadna 63
        log.debug('The course/actions module is deprecated. Please, add support_components to your course format.');
1 efrain 64
 
65
        // Eventually, core_courseformat/local/content/actions will handle all actions for
66
        // component compatible formats and the default actions.js won't be necessary anymore.
67
        // Meanwhile, we filter the migrated actions.
68
        const componentActions = [
69
            'moveSection', 'moveCm', 'addSection', 'deleteSection', 'cmDelete', 'cmDuplicate', 'sectionHide', 'sectionShow',
70
            'cmHide', 'cmShow', 'cmStealth', 'sectionHighlight', 'sectionUnhighlight', 'cmMoveRight', 'cmMoveLeft',
1441 ariadna 71
            'cmNoGroups', 'cmVisibleGroups', 'cmSeparateGroups', 'addModule', 'newModule',
1 efrain 72
        ];
73
 
74
        // The course reactive instance.
75
        const courseeditor = editor.getCurrentCourseEditor();
76
 
77
        // The current course format name (loaded on init).
78
        let formatname;
79
 
80
        var CSS = {
81
            EDITINPROGRESS: 'editinprogress',
82
            SECTIONDRAGGABLE: 'sectiondraggable',
83
            EDITINGMOVE: 'editing_move'
84
        };
85
        var SELECTOR = {
86
            ACTIVITYLI: 'li.activity',
87
            ACTIONAREA: '.actions',
88
            ACTIVITYACTION: 'a.cm-edit-action',
89
            MENU: '.moodle-actionmenu[data-enhance=moodle-core-actionmenu]',
90
            TOGGLE: '.toggle-display,.dropdown-toggle',
91
            SECTIONLI: 'li.section',
92
            SECTIONACTIONMENU: '.section_action_menu',
11 efrain 93
            SECTIONACTIONMENUTRIGGER: '.section-actions',
1 efrain 94
            SECTIONITEM: '[data-for="section_title"]',
95
            ADDSECTIONS: '.changenumsections [data-add-sections]',
96
            SECTIONBADGES: '[data-region="sectionbadges"]',
97
        };
98
 
99
        Y.use('moodle-course-coursebase', function() {
100
            var courseformatselector = M.course.format.get_section_selector();
101
            if (courseformatselector) {
102
                SELECTOR.SECTIONLI = courseformatselector;
103
            }
104
        });
105
 
106
        /**
107
         * Dispatch event wrapper.
108
         *
109
         * Old jQuery events will be replaced by native events gradually.
110
         *
111
         * @method dispatchEvent
112
         * @param {String} eventName The name of the event
113
         * @param {Object} detail Any additional details to pass into the eveent
114
         * @param {Node|HTMLElement} container The point at which to dispatch the event
115
         * @param {Object} options
116
         * @param {Boolean} options.bubbles Whether to bubble up the DOM
117
         * @param {Boolean} options.cancelable Whether preventDefault() can be called
118
         * @param {Boolean} options.composed Whether the event can bubble across the ShadowDOM boundary
119
         * @returns {CustomEvent}
120
         */
121
        const dispatchEvent = function(eventName, detail, container, options) {
122
            // Most actions still uses jQuery node instead of regular HTMLElement.
123
            if (!(container instanceof Element) && container.get !== undefined) {
124
                container = container.get(0);
125
            }
126
            return EventDispatcher.dispatchEvent(eventName, detail, container, options);
127
        };
128
 
129
        /**
130
         * Wrapper for Y.Moodle.core_course.util.cm.getId
131
         *
132
         * @param {JQuery} element
133
         * @returns {Integer}
134
         */
135
        var getModuleId = function(element) {
136
            // Check if we have a data-id first.
137
            const item = element.get(0);
138
            if (item.dataset.id) {
139
                return item.dataset.id;
140
            }
141
            // Use YUI way if data-id is not present.
142
            let id;
143
            Y.use('moodle-course-util', function(Y) {
144
                id = Y.Moodle.core_course.util.cm.getId(Y.Node(item));
145
            });
146
            return id;
147
        };
148
 
149
        /**
150
         * Wrapper for Y.Moodle.core_course.util.cm.getName
151
         *
152
         * @param {JQuery} element
153
         * @returns {String}
154
         */
155
        var getModuleName = function(element) {
156
            var name;
157
            Y.use('moodle-course-util', function(Y) {
158
                name = Y.Moodle.core_course.util.cm.getName(Y.Node(element.get(0)));
159
            });
160
            // Check if we have the name in the course state.
161
            const state = courseeditor.state;
162
            const cmid = getModuleId(element);
163
            if (!name && state && cmid) {
164
                name = state.cm.get(cmid)?.name;
165
            }
166
            return name;
167
        };
168
 
169
        /**
170
         * Wrapper for M.util.add_spinner for an activity
171
         *
172
         * @param {JQuery} activity
173
         * @returns {Node}
174
         */
175
        var addActivitySpinner = function(activity) {
176
            activity.addClass(CSS.EDITINPROGRESS);
177
            var actionarea = activity.find(SELECTOR.ACTIONAREA).get(0);
178
            if (actionarea) {
179
                var spinner = M.util.add_spinner(Y, Y.Node(actionarea));
180
                spinner.show();
181
                // Lock the activity state element.
182
                if (activity.data('id') !== undefined) {
183
                    courseeditor.dispatch('cmLock', [activity.data('id')], true);
184
                }
185
                return spinner;
186
            }
187
            return null;
188
        };
189
 
190
        /**
191
         * Wrapper for M.util.add_spinner for a section
192
         *
193
         * @param {JQuery} sectionelement
194
         * @returns {Node}
195
         */
196
        var addSectionSpinner = function(sectionelement) {
197
            sectionelement.addClass(CSS.EDITINPROGRESS);
198
            var actionarea = sectionelement.find(SELECTOR.SECTIONACTIONMENU).get(0);
199
            if (actionarea) {
200
                var spinner = M.util.add_spinner(Y, Y.Node(actionarea));
201
                spinner.show();
202
                // Lock the section state element.
203
                if (sectionelement.data('id') !== undefined) {
204
                    courseeditor.dispatch('sectionLock', [sectionelement.data('id')], true);
205
                }
206
                return spinner;
207
            }
208
            return null;
209
        };
210
 
211
        /**
212
         * Wrapper for M.util.add_lightbox
213
         *
214
         * @param {JQuery} sectionelement
215
         * @returns {Node}
216
         */
217
        var addSectionLightbox = function(sectionelement) {
218
            const item = sectionelement.get(0);
219
            var lightbox = M.util.add_lightbox(Y, Y.Node(item));
220
            if (item.dataset.for == 'section' && item.dataset.id) {
221
                courseeditor.dispatch('sectionLock', [item.dataset.id], true);
222
                lightbox.setAttribute('data-state', 'section');
223
                lightbox.setAttribute('data-state-id', item.dataset.id);
224
            }
225
            lightbox.show();
226
            return lightbox;
227
        };
228
 
229
        /**
230
         * Removes the spinner element
231
         *
232
         * @param {JQuery} element
233
         * @param {Node} spinner
234
         * @param {Number} delay
235
         */
236
        var removeSpinner = function(element, spinner, delay) {
237
            window.setTimeout(function() {
238
                element.removeClass(CSS.EDITINPROGRESS);
239
                if (spinner) {
240
                    spinner.hide();
241
                }
242
                // Unlock the state element.
243
                if (element.data('id') !== undefined) {
244
                    const mutation = (element.data('for') === 'section') ? 'sectionLock' : 'cmLock';
245
                    courseeditor.dispatch(mutation, [element.data('id')], false);
246
                }
247
            }, delay);
248
        };
249
 
250
        /**
251
         * Removes the lightbox element
252
         *
253
         * @param {Node} lightbox lighbox YUI element returned by addSectionLightbox
254
         * @param {Number} delay
255
         */
256
        var removeLightbox = function(lightbox, delay) {
257
            if (lightbox) {
258
                window.setTimeout(function() {
259
                    lightbox.hide();
260
                    // Unlock state if necessary.
261
                    if (lightbox.getAttribute('data-state')) {
262
                        courseeditor.dispatch(
263
                            `${lightbox.getAttribute('data-state')}Lock`,
264
                            [lightbox.getAttribute('data-state-id')],
265
                            false
266
                        );
267
                    }
268
                }, delay);
269
            }
270
        };
271
 
272
        /**
273
         * Initialise action menu for the element (section or module)
274
         *
275
         * @param {String} elementid CSS id attribute of the element
276
         */
277
        var initActionMenu = function(elementid) {
278
            // Initialise action menu in the new activity.
279
            Y.use('moodle-course-coursebase', function() {
280
                M.course.coursebase.invoke_function('setup_for_resource', '#' + elementid);
281
            });
282
            if (M.core.actionmenu && M.core.actionmenu.newDOMNode) {
283
                M.core.actionmenu.newDOMNode(Y.one('#' + elementid));
284
            }
285
        };
286
 
287
        /**
288
         * Returns focus to the element that was clicked or "Edit" link if element is no longer visible.
289
         *
290
         * @param {String} elementId CSS id attribute of the element
291
         * @param {String} action data-action property of the element that was clicked
292
         */
293
        var focusActionItem = function(elementId, action) {
294
            var mainelement = $('#' + elementId);
295
            var selector = '[data-action=' + action + ']';
296
            if (action === 'groupsseparate' || action === 'groupsvisible' || action === 'groupsnone') {
297
                // New element will have different data-action.
298
                selector = '[data-action=groupsseparate],[data-action=groupsvisible],[data-action=groupsnone]';
299
            }
300
            if (mainelement.find(selector).is(':visible')) {
301
                mainelement.find(selector).focus();
302
            } else {
303
                // Element not visible, focus the "Edit" link.
304
                mainelement.find(SELECTOR.MENU).find(SELECTOR.TOGGLE).focus();
305
            }
306
        };
307
 
308
        /**
309
         * Find next <a> after the element
310
         *
311
         * @param {JQuery} mainElement element that is about to be deleted
312
         * @returns {JQuery}
313
         */
314
        var findNextFocusable = function(mainElement) {
315
            var tabables = $("a:visible");
316
            var isInside = false;
317
            var foundElement = null;
318
            tabables.each(function() {
319
                if ($.contains(mainElement[0], this)) {
320
                    isInside = true;
321
                } else if (isInside) {
322
                    foundElement = this;
323
                    return false; // Returning false in .each() is equivalent to "break;" inside the loop in php.
324
                }
325
                return true;
326
            });
327
            return foundElement;
328
        };
329
 
330
        /**
331
         * Performs an action on a module (moving, deleting, duplicating, hiding, etc.)
332
         *
333
         * @param {JQuery} moduleElement activity element we perform action on
334
         * @param {Number} cmid
335
         * @param {JQuery} target the element (menu item) that was clicked
336
         */
337
        var editModule = function(moduleElement, cmid, target) {
338
            var action = target.attr('data-action');
339
            var spinner = addActivitySpinner(moduleElement);
340
            var promises = ajax.call([{
341
                methodname: 'core_course_edit_module',
342
                args: {id: cmid,
343
                    action: action,
344
                    sectionreturn: target.attr('data-sectionreturn') ? target.attr('data-sectionreturn') : null
345
                }
346
            }], true);
347
 
348
            var lightbox;
349
            if (action === 'duplicate') {
350
                lightbox = addSectionLightbox(target.closest(SELECTOR.SECTIONLI));
351
            }
352
            $.when.apply($, promises)
353
                .done(function(data) {
354
                    var elementToFocus = findNextFocusable(moduleElement);
355
                    moduleElement.replaceWith(data);
356
                    let affectedids = [];
357
                    // Initialise action menu for activity(ies) added as a result of this.
358
                    $('<div>' + data + '</div>').find(SELECTOR.ACTIVITYLI).each(function(index) {
359
                        initActionMenu($(this).attr('id'));
360
                        if (index === 0) {
361
                            focusActionItem($(this).attr('id'), action);
362
                            elementToFocus = null;
363
                        }
364
                        // Save any activity id in cmids.
365
                        affectedids.push(getModuleId($(this)));
366
                    });
367
                    // In case of activity deletion focus the next focusable element.
368
                    if (elementToFocus) {
369
                        elementToFocus.focus();
370
                    }
371
                    // Remove spinner and lightbox with a delay.
372
                    removeSpinner(moduleElement, spinner, 400);
373
                    removeLightbox(lightbox, 400);
374
                    // Trigger event that can be observed by course formats.
375
                    moduleElement.trigger($.Event('coursemoduleedited', {ajaxreturn: data, action: action}));
376
 
377
                    // Modify cm state.
378
                    courseeditor.dispatch('legacyActivityAction', action, cmid, affectedids);
379
 
380
                }).fail(function(ex) {
381
                    // Remove spinner and lightbox.
382
                    removeSpinner(moduleElement, spinner);
383
                    removeLightbox(lightbox);
384
                    // Trigger event that can be observed by course formats.
385
                    var e = $.Event('coursemoduleeditfailed', {exception: ex, action: action});
386
                    moduleElement.trigger(e);
387
                    if (!e.isDefaultPrevented()) {
388
                        notification.exception(ex);
389
                    }
390
                });
391
        };
392
 
393
        /**
394
         * Requests html for the module via WS core_course_get_module and updates the module on the course page
395
         *
396
         * Used after d&d of the module to another section
397
         *
398
         * @param {JQuery|Element} element
399
         * @param {Number} cmid
400
         * @param {Number} sectionreturn
401
         * @return {Promise} the refresh promise
402
         */
403
        var refreshModule = function(element, cmid, sectionreturn) {
404
 
405
            if (sectionreturn === undefined) {
406
                sectionreturn = courseeditor.sectionReturn;
407
            }
408
 
409
            const activityElement = $(element);
410
            var spinner = addActivitySpinner(activityElement);
411
            var promises = ajax.call([{
412
                methodname: 'core_course_get_module',
413
                args: {id: cmid, sectionreturn: sectionreturn}
414
            }], true);
415
 
416
            return new Promise((resolve, reject) => {
417
                $.when.apply($, promises)
418
                    .done(function(data) {
419
                        removeSpinner(activityElement, spinner, 400);
420
                        replaceActivityHtmlWith(data);
421
                        resolve(data);
422
                    }).fail(function() {
423
                        removeSpinner(activityElement, spinner);
424
                        reject();
425
                    });
426
            });
427
        };
428
 
429
        /**
430
         * Requests html for the section via WS core_course_edit_section and updates the section on the course page
431
         *
432
         * @param {JQuery|Element} element
433
         * @param {Number} sectionid
434
         * @param {Number} sectionreturn
435
         * @return {Promise} the refresh promise
436
         */
437
        var refreshSection = function(element, sectionid, sectionreturn) {
438
 
439
            if (sectionreturn === undefined) {
440
                sectionreturn = courseeditor.sectionReturn;
441
            }
442
 
443
            const sectionElement = $(element);
444
            const action = 'refresh';
445
            const promises = ajax.call([{
446
                methodname: 'core_course_edit_section',
447
                args: {id: sectionid, action, sectionreturn},
448
            }], true);
449
 
450
            var spinner = addSectionSpinner(sectionElement);
451
            return new Promise((resolve, reject) => {
452
                $.when.apply($, promises)
453
                    .done(dataencoded => {
454
 
455
                        removeSpinner(sectionElement, spinner);
456
                        const data = $.parseJSON(dataencoded);
457
 
458
                        const newSectionElement = $(data.content);
459
                        sectionElement.replaceWith(newSectionElement);
460
 
461
                        // Init modules menus.
462
                        $(`${SELECTOR.SECTIONLI}#${sectionid} ${SELECTOR.ACTIVITYLI}`).each(
463
                            (index, activity) => {
464
                                initActionMenu(activity.data('id'));
465
                            }
466
                        );
467
 
468
                        // Trigger event that can be observed by course formats.
469
                        const event = dispatchEvent(
470
                            CourseEvents.sectionRefreshed,
471
                            {
472
                                ajaxreturn: data,
473
                                action: action,
474
                                newSectionElement: newSectionElement.get(0),
475
                            },
476
                            newSectionElement
477
                        );
478
 
479
                        if (!event.defaultPrevented) {
480
                            defaultEditSectionHandler(
481
                                newSectionElement, $(SELECTOR.SECTIONLI + '#' + sectionid),
482
                                data,
483
                                formatname,
484
                                sectionid
485
                            );
486
                        }
487
                        resolve(data);
488
                    }).fail(ex => {
489
                        // Trigger event that can be observed by course formats.
490
                        const event = dispatchEvent(
491
                            'coursesectionrefreshfailed',
492
                            {exception: ex, action: action},
493
                            sectionElement
494
                        );
495
                        if (!event.defaultPrevented) {
496
                            notification.exception(ex);
497
                        }
498
                        reject();
499
                    });
500
            });
501
        };
502
 
503
        /**
504
         * Displays the delete confirmation to delete a module
505
         *
506
         * @param {JQuery} mainelement activity element we perform action on
507
         * @param {function} onconfirm function to execute on confirm
508
         */
509
        var confirmDeleteModule = function(mainelement, onconfirm) {
510
            var modtypename = mainelement.attr('class').match(/modtype_([^\s]*)/)[1];
511
            var modulename = getModuleName(mainelement);
512
 
513
            str.get_string('pluginname', modtypename).done(function(pluginname) {
514
                var plugindata = {
515
                    type: pluginname,
516
                    name: modulename
517
                };
518
                str.get_strings([
519
                    {key: 'confirm', component: 'core'},
520
                    {key: modulename === null ? 'deletechecktype' : 'deletechecktypename', param: plugindata},
521
                    {key: 'yes'},
522
                    {key: 'no'}
523
                ]).done(function(s) {
524
                        notification.confirm(s[0], s[1], s[2], s[3], onconfirm);
525
                    }
526
                );
527
            });
528
        };
529
 
530
        /**
531
         * Displays the delete confirmation to delete a section
532
         *
533
         * @param {String} message confirmation message
534
         * @param {function} onconfirm function to execute on confirm
535
         */
536
        var confirmEditSection = function(message, onconfirm) {
537
            str.get_strings([
538
                {key: 'confirm'}, // TODO link text
539
                {key: 'yes'},
540
                {key: 'no'}
541
            ]).done(function(s) {
542
                    notification.confirm(s[0], message, s[1], s[2], onconfirm);
543
                }
544
            );
545
        };
546
 
547
        /**
548
         * Replaces an action menu item with another one (for example Show->Hide, Set marker->Remove marker)
549
         *
550
         * @param {JQuery} actionitem
551
         * @param {String} image new image name ("i/show", "i/hide", etc.)
552
         * @param {String} stringname new string for the action menu item
553
         * @param {String} stringcomponent
554
         * @param {String} newaction new value for data-action attribute of the link
555
         * @return {Promise} promise which is resolved when the replacement has completed
556
         */
557
        var replaceActionItem = function(actionitem, image, stringname,
558
                                           stringcomponent, newaction) {
559
 
560
            var stringRequests = [{key: stringname, component: stringcomponent}];
561
            // Do not provide an icon with duplicate, different text to the menu item.
562
 
563
            return str.get_strings(stringRequests).then(function(strings) {
564
                actionitem.find('span.menu-action-text').html(strings[0]);
565
 
566
                return templates.renderPix(image, 'core');
567
            }).then(function(pixhtml) {
568
                actionitem.find('.icon').replaceWith(pixhtml);
569
                actionitem.attr('data-action', newaction);
570
                return;
571
            }).catch(notification.exception);
572
        };
573
 
574
        /**
575
         * Default post-processing for section AJAX edit actions.
576
         *
577
         * This can be overridden in course formats by listening to event coursesectionedited:
578
         *
579
         * $('body').on('coursesectionedited', 'li.section', function(e) {
580
         *     var action = e.action,
581
         *         sectionElement = $(e.target),
582
         *         data = e.ajaxreturn;
583
         *     // ... Do some processing here.
584
         *     e.preventDefault(); // Prevent default handler.
585
         * });
586
         *
587
         * @param {JQuery} sectionElement
588
         * @param {JQuery} actionItem
589
         * @param {Object} data
590
         * @param {String} courseformat
591
         * @param {Number} sectionid
592
         */
593
        var defaultEditSectionHandler = function(sectionElement, actionItem, data, courseformat, sectionid) {
594
            var action = actionItem.attr('data-action');
595
            if (action === 'hide' || action === 'show') {
596
                if (action === 'hide') {
597
                    sectionElement.addClass('hidden');
598
                    setSectionBadge(sectionElement[0], 'hiddenfromstudents', true, false);
599
                    replaceActionItem(actionItem, 'i/show',
600
                        'showfromothers', 'format_' + courseformat, 'show');
601
                } else {
602
                    setSectionBadge(sectionElement[0], 'hiddenfromstudents', false, false);
603
                    sectionElement.removeClass('hidden');
604
                    replaceActionItem(actionItem, 'i/hide',
605
                        'hidefromothers', 'format_' + courseformat, 'hide');
606
                }
607
                // Replace the modules with new html (that indicates that they are now hidden or not hidden).
608
                if (data.modules !== undefined) {
609
                    for (var i in data.modules) {
610
                        replaceActivityHtmlWith(data.modules[i]);
611
                    }
612
                }
613
                // Replace the section availability information.
614
                if (data.section_availability !== undefined) {
615
                    sectionElement.find('.section_availability').first().replaceWith(data.section_availability);
616
                }
617
                // Modify course state.
618
                const section = courseeditor.state.section.get(sectionid);
619
                if (section !== undefined) {
620
                    courseeditor.dispatch('sectionState', [sectionid]);
621
                }
622
            } else if (action === 'setmarker') {
623
                var oldmarker = $(SELECTOR.SECTIONLI + '.current'),
624
                    oldActionItem = oldmarker.find(SELECTOR.SECTIONACTIONMENU + ' ' + 'a[data-action=removemarker]');
625
                oldmarker.removeClass('current');
626
                replaceActionItem(oldActionItem, 'i/marker',
627
                    'highlight', 'core', 'setmarker');
628
                sectionElement.addClass('current');
629
                replaceActionItem(actionItem, 'i/marked',
630
                    'highlightoff', 'core', 'removemarker');
631
                courseeditor.dispatch('legacySectionAction', action, sectionid);
632
                setSectionBadge(sectionElement[0], 'iscurrent', true, true);
633
            } else if (action === 'removemarker') {
634
                sectionElement.removeClass('current');
635
                replaceActionItem(actionItem, 'i/marker',
636
                    'highlight', 'core', 'setmarker');
637
                courseeditor.dispatch('legacySectionAction', action, sectionid);
638
                setSectionBadge(sectionElement[0], 'iscurrent', false, true);
639
            }
640
        };
641
 
642
        /**
643
         * Get the focused element path in an activity if any.
644
         *
645
         * This method is used to restore focus when the activity HTML is refreshed.
646
         * Only the main course editor elements can be refocused as they are always present
647
         * even if the activity content changes.
648
         *
649
         * @param {String} id the element id the activity element
650
         * @return {String|undefined} the inner path of the focused element or undefined
651
         */
652
        const getActivityFocusedElement = function(id) {
653
            const element = document.getElementById(id);
654
            if (!element || !element.contains(document.activeElement)) {
655
                return undefined;
656
            }
657
            // Check if the actions menu toggler is focused.
658
            if (element.querySelector(SELECTOR.ACTIONAREA).contains(document.activeElement)) {
659
                return `${SELECTOR.ACTIONAREA} [tabindex="0"]`;
660
            }
661
            // Return the current element id if any.
662
            if (document.activeElement.id) {
663
                return `#${document.activeElement.id}`;
664
            }
665
            return undefined;
666
        };
667
 
668
        /**
669
         * Replaces the course module with the new html (used to update module after it was edited or its visibility was changed).
670
         *
671
         * @param {String} activityHTML
672
         */
673
        var replaceActivityHtmlWith = function(activityHTML) {
674
            $('<div>' + activityHTML + '</div>').find(SELECTOR.ACTIVITYLI).each(function() {
675
                // Extract id from the new activity html.
676
                var id = $(this).attr('id');
677
                // Check if the current focused element is inside the activity.
678
                let focusedPath = getActivityFocusedElement(id);
679
                // Find the existing element with the same id and replace its contents with new html.
680
                $(SELECTOR.ACTIVITYLI + '#' + id).replaceWith(activityHTML);
681
                // Initialise action menu.
682
                initActionMenu(id);
683
                // Re-focus the previous elements.
684
                if (focusedPath) {
685
                    const newItem = document.getElementById(id);
686
                    newItem.querySelector(focusedPath)?.focus();
687
                }
688
 
689
            });
690
        };
691
 
692
        /**
693
         * Performs an action on a module (moving, deleting, duplicating, hiding, etc.)
694
         *
695
         * @param {JQuery} sectionElement section element we perform action on
696
         * @param {Nunmber} sectionid
697
         * @param {JQuery} target the element (menu item) that was clicked
698
         * @param {String} courseformat
699
         * @return {boolean} true the action call is sent to the server or false if it is ignored.
700
         */
701
        var editSection = function(sectionElement, sectionid, target, courseformat) {
702
            var action = target.attr('data-action'),
703
                sectionreturn = target.attr('data-sectionreturn') ? target.attr('data-sectionreturn') : null;
704
 
705
            // Filter direct component handled actions.
706
            if (courseeditor.supportComponents && componentActions.includes(action)) {
707
                return false;
708
            }
709
 
710
            var spinner = addSectionSpinner(sectionElement);
711
            var promises = ajax.call([{
712
                methodname: 'core_course_edit_section',
713
                args: {id: sectionid, action: action, sectionreturn: sectionreturn}
714
            }], true);
715
 
716
            var lightbox = addSectionLightbox(sectionElement);
717
            $.when.apply($, promises)
718
                .done(function(dataencoded) {
719
                    var data = $.parseJSON(dataencoded);
720
                    removeSpinner(sectionElement, spinner);
721
                    removeLightbox(lightbox);
722
                    sectionElement.find(SELECTOR.SECTIONACTIONMENU).find(SELECTOR.TOGGLE).focus();
723
                    // Trigger event that can be observed by course formats.
724
                    var e = $.Event('coursesectionedited', {ajaxreturn: data, action: action});
725
                    sectionElement.trigger(e);
726
                    if (!e.isDefaultPrevented()) {
727
                        defaultEditSectionHandler(sectionElement, target, data, courseformat, sectionid);
728
                    }
729
                }).fail(function(ex) {
730
                    // Remove spinner and lightbox.
731
                    removeSpinner(sectionElement, spinner);
732
                    removeLightbox(lightbox);
733
                    // Trigger event that can be observed by course formats.
734
                    var e = $.Event('coursesectioneditfailed', {exception: ex, action: action});
735
                    sectionElement.trigger(e);
736
                    if (!e.isDefaultPrevented()) {
737
                        notification.exception(ex);
738
                    }
739
                });
740
            return true;
741
        };
742
 
743
        /**
744
         * Sets the section badge in the section header.
745
         *
746
         * @param {JQuery} sectionElement section element we perform action on
747
         * @param {String} badgetype the type of badge this is for
748
         * @param {bool} add true to add, false to remove
749
         * @param {boolean} removeOther in case of adding a badge, whether to remove all other.
750
         */
751
        var setSectionBadge = function(sectionElement, badgetype, add, removeOther) {
752
            const sectionbadges = sectionElement.querySelector(SELECTOR.SECTIONBADGES);
753
            if (!sectionbadges) {
754
                return;
755
            }
756
            const badge = sectionbadges.querySelector('[data-type="' + badgetype + '"]');
757
            if (!badge) {
758
                return;
759
            }
760
            if (add) {
761
                if (removeOther) {
762
                    document.querySelectorAll('[data-type="' + badgetype + '"]').forEach((b) => {
763
                        b.classList.add('d-none');
764
                    });
765
                }
766
                badge.classList.remove('d-none');
767
            } else {
768
                badge.classList.add('d-none');
769
            }
770
        };
771
 
772
        // Register a function to be executed after D&D of an activity.
773
        Y.use('moodle-course-coursebase', function() {
774
            M.course.coursebase.register_module({
775
                // Ignore camelcase eslint rule for the next line because it is an expected name of the callback.
776
                // eslint-disable-next-line camelcase
777
                set_visibility_resource_ui: function(args) {
778
                    var mainelement = $(args.element.getDOMNode());
779
                    var cmid = getModuleId(mainelement);
780
                    if (cmid) {
781
                        var sectionreturn = mainelement.find('.' + CSS.EDITINGMOVE).attr('data-sectionreturn');
782
                        refreshModule(mainelement, cmid, sectionreturn);
783
                    }
784
                },
785
                /**
786
                 * Update the course state when some cm is moved via YUI.
787
                 * @param {*} params
788
                 */
789
                updateMovedCmState: (params) => {
790
                    const state = courseeditor.state;
791
 
792
                    // Update old section.
793
                    const cm = state.cm.get(params.cmid);
794
                    if (cm !== undefined) {
795
                        courseeditor.dispatch('sectionState', [cm.sectionid]);
796
                    }
797
                    // Update cm state.
798
                    courseeditor.dispatch('cmState', [params.cmid]);
799
                },
800
                /**
801
                 * Update the course state when some section is moved via YUI.
802
                 */
803
                updateMovedSectionState: () => {
804
                    courseeditor.dispatch('courseState');
805
                },
806
            });
807
        });
808
 
809
        // From Moodle 4.0 all edit actions are being re-implemented as state mutation.
810
        // This means all method from this "actions" module will be deprecated when all the course
811
        // interface is migrated to reactive components.
812
        // Most legacy actions did not provide enough information to regenarate the course so they
813
        // use the mutations courseState, sectionState and cmState to get the updated state from
814
        // the server. However, some activity actions where we can prevent an extra webservice
815
        // call by implementing an adhoc mutation.
816
        courseeditor.addMutations({
817
            /**
818
             * Compatibility function to update Moodle 4.0 course state using legacy actions.
819
             *
820
             * This method only updates some actions which does not require to use cmState mutation
821
             * to get updated data form the server.
822
             *
823
             * @param {Object} statemanager the current state in read write mode
824
             * @param {String} action the performed action
825
             * @param {Number} cmid the affected course module id
826
             * @param {Array} affectedids all affected cm ids (for duplicate action)
827
             */
828
            legacyActivityAction: function(statemanager, action, cmid, affectedids) {
829
 
830
                const state = statemanager.state;
831
                const cm = state.cm.get(cmid);
832
                if (cm === undefined) {
833
                    return;
834
                }
835
                const section = state.section.get(cm.sectionid);
836
                if (section === undefined) {
837
                    return;
838
                }
839
 
840
                // Send the element is locked.
841
                courseeditor.dispatch('cmLock', [cm.id], true);
842
 
843
                // Now we do the real mutation.
844
                statemanager.setReadOnly(false);
845
 
846
                // This unlocked will take effect when the read only is restored.
847
                cm.locked = false;
848
 
849
                switch (action) {
850
                    case 'delete':
851
                        // Remove from section.
852
                        section.cmlist = section.cmlist.reduce(
853
                            (cmlist, current) => {
854
                                if (current != cmid) {
855
                                    cmlist.push(current);
856
                                }
857
                                return cmlist;
858
                            },
859
                            []
860
                        );
861
                        // Delete form list.
862
                        state.cm.delete(cmid);
863
                        break;
864
 
865
                    case 'hide':
866
                    case 'show':
867
                    case 'duplicate':
868
                        courseeditor.dispatch('cmState', affectedids);
869
                        break;
870
                }
871
                statemanager.setReadOnly(true);
872
            },
873
            legacySectionAction: function(statemanager, action, sectionid) {
874
 
875
                const state = statemanager.state;
876
                const section = state.section.get(sectionid);
877
                if (section === undefined) {
878
                    return;
879
                }
880
 
881
                // Send the element is locked. Reactive events are only triggered when the state
882
                // read only mode is restored. We want to notify the interface the element is
883
                // locked so we need to do a quick lock operation before performing the rest
884
                // of the mutation.
885
                statemanager.setReadOnly(false);
886
                section.locked = true;
887
                statemanager.setReadOnly(true);
888
 
889
                // Now we do the real mutation.
890
                statemanager.setReadOnly(false);
891
 
892
                // This locked will take effect when the read only is restored.
893
                section.locked = false;
894
 
895
                switch (action) {
896
                    case 'setmarker':
897
                        // Remove previous marker.
898
                        state.section.forEach((current) => {
899
                            if (current.id != sectionid) {
900
                                current.current = false;
901
                            }
902
                        });
903
                        section.current = true;
904
                        break;
905
 
906
                    case 'removemarker':
907
                        section.current = false;
908
                        break;
909
                }
910
                statemanager.setReadOnly(true);
911
            },
912
        });
913
 
914
        return /** @alias module:core_course/actions */ {
915
 
916
            /**
917
             * Initialises course page
918
             *
919
             * @method init
920
             * @param {String} courseformat name of the current course format (for fetching strings)
921
             */
922
            initCoursePage: function(courseformat) {
923
 
924
                formatname = courseformat;
925
 
926
                // Add a handler for course module actions.
927
                $('body').on('click keypress', SELECTOR.ACTIVITYLI + ' ' +
928
                        SELECTOR.ACTIVITYACTION + '[data-action]', function(e) {
929
                    if (e.type === 'keypress' && e.keyCode !== 13) {
930
                        return;
931
                    }
932
                    var actionItem = $(this),
933
                        moduleElement = actionItem.closest(SELECTOR.ACTIVITYLI),
934
                        action = actionItem.attr('data-action'),
935
                        moduleId = getModuleId(moduleElement);
936
                    switch (action) {
937
                        case 'moveleft':
938
                        case 'moveright':
939
                        case 'delete':
940
                        case 'duplicate':
941
                        case 'hide':
942
                        case 'stealth':
943
                        case 'show':
944
                        case 'groupsseparate':
945
                        case 'groupsvisible':
946
                        case 'groupsnone':
947
                            break;
948
                        default:
949
                            // Nothing to do here!
950
                            return;
951
                    }
952
                    if (!moduleId) {
953
                        return;
954
                    }
955
                    e.preventDefault();
956
                    if (action === 'delete') {
957
                        // Deleting requires confirmation.
958
                        confirmDeleteModule(moduleElement, function() {
959
                            editModule(moduleElement, moduleId, actionItem);
960
                        });
961
                    } else {
962
                        editModule(moduleElement, moduleId, actionItem);
963
                    }
964
                });
965
 
11 efrain 966
                // Add a handler for section action menu.
967
                $('body').on('click keypress',
968
                            SELECTOR.SECTIONACTIONMENUTRIGGER + '[data-sectionid] ' +
1 efrain 969
                            'a[data-action]', function(e) {
970
                    if (e.type === 'keypress' && e.keyCode !== 13) {
971
                        return;
972
                    }
973
                    var actionItem = $(this),
974
                        sectionElement = actionItem.closest(SELECTOR.SECTIONLI),
11 efrain 975
                        sectionId = actionItem.closest(SELECTOR.SECTIONACTIONMENUTRIGGER).attr('data-sectionid');
1 efrain 976
 
977
                    if (actionItem.attr('data-action') === 'permalink') {
978
                        e.preventDefault();
979
                        ModalCopyToClipboard.create({
980
                            text: actionItem.attr('href'),
981
                        }, str.get_string('sectionlink', 'course')
982
                        );
983
                        return;
984
                    }
985
 
986
                    let isExecuted = true;
987
                    if (actionItem.attr('data-confirm')) {
988
                        // Action requires confirmation.
989
                        confirmEditSection(actionItem.attr('data-confirm'), function() {
990
                            isExecuted = editSection(sectionElement, sectionId, actionItem, courseformat);
991
                        });
992
                    } else {
993
                        isExecuted = editSection(sectionElement, sectionId, actionItem, courseformat);
994
                    }
995
                    // Prevent any other module from capturing the action if it is already in execution.
996
                    if (isExecuted) {
997
                        e.preventDefault();
998
                    }
999
                });
1000
 
1001
                // The section and activity names are edited using inplace editable.
1002
                // The "update" jQuery event must be captured in order to update the course state.
1441 ariadna 1003
                $('body').on(InplaceEditableEvents.eventTypes.elementUpdated,
1004
                        `${SELECTOR.SECTIONITEM} [data-inplaceeditable]`, function(e) {
1005
                    if (e.detail.ajaxreturn.itemid) {
1 efrain 1006
                        const state = courseeditor.state;
1441 ariadna 1007
                        const section = state.section.get(e.detail.ajaxreturn.itemid);
1 efrain 1008
                        if (section !== undefined) {
1441 ariadna 1009
                            courseeditor.dispatch('sectionState', [e.detail.ajaxreturn.itemid]);
1 efrain 1010
                        }
1011
                    }
1012
                });
1441 ariadna 1013
 
1014
                $('body').on(InplaceEditableEvents.eventTypes.elementUpdated,
1015
                        `${SELECTOR.ACTIVITYLI} [data-itemtype="activityname"][data-inplaceeditable]`, function(e) {
1016
                    if (e.detail.ajaxreturn.itemid) {
1017
                        courseeditor.dispatch('cmState', [e.detail.ajaxreturn.itemid]);
1 efrain 1018
                    }
1019
                });
1020
 
1021
                // Component-based formats don't use modals to create sections.
1022
                if (courseeditor.supportComponents && componentActions.includes('addSection')) {
1023
                    return;
1024
                }
1025
 
1026
                // Add a handler for "Add sections" link to ask for a number of sections to add.
1027
                const trigger = $(SELECTOR.ADDSECTIONS);
1028
                const modalTitle = trigger.attr('data-add-sections');
1029
                const newSections = trigger.attr('data-new-sections');
1030
                str.get_string('numberweeks')
1031
                .then(function(strNumberSections) {
1032
                    var modalBody = $('<div><label for="add_section_numsections"></label> ' +
1033
                        '<input id="add_section_numsections" type="number" min="1" max="' + newSections + '" value="1"></div>');
1034
                    modalBody.find('label').html(strNumberSections);
1035
 
1036
                    return modalBody.html();
1037
                })
1038
                .then((body) => ModalSaveCancel.create({
1039
                    body,
1040
                    title: modalTitle,
1041
                }))
1042
                .then(function(modal) {
1043
                    var numSections = $(modal.getBody()).find('#add_section_numsections'),
1044
                    addSections = function() {
1045
                        // Check if value of the "Number of sections" is a valid positive integer and redirect
1046
                        // to adding a section script.
1047
                        if ('' + parseInt(numSections.val()) === numSections.val() && parseInt(numSections.val()) >= 1) {
1048
                            document.location = trigger.attr('href') + '&numsections=' + parseInt(numSections.val());
1049
                        }
1050
                    };
1051
                    modal.setSaveButtonText(modalTitle);
1052
                    modal.getRoot().on(ModalEvents.shown, function() {
1053
                        // When modal is shown focus and select the input and add a listener to keypress of "Enter".
1054
                        numSections.focus().select().on('keydown', function(e) {
1055
                            if (e.keyCode === KeyCodes.enter) {
1056
                                addSections();
1057
                            }
1058
                        });
1059
                    });
1060
                    modal.getRoot().on(ModalEvents.save, function(e) {
1061
                        // When modal "Add" button is pressed.
1062
                        e.preventDefault();
1063
                        addSections();
1064
                    });
1065
 
1066
                    trigger.on('click', (e) => {
1067
                        e.preventDefault();
1068
                        modal.show();
1069
                    });
1070
 
1071
                    return modal;
1072
                })
1073
                .catch(notification.exception);
1074
            },
1075
 
1076
            /**
1077
             * Replaces a section action menu item with another one (for example Show->Hide, Set marker->Remove marker)
1078
             *
1079
             * This method can be used by course formats in their listener to the coursesectionedited event
1080
             *
1081
             * @deprecated since Moodle 3.9
1082
             * @param {JQuery} sectionelement
1083
             * @param {String} selector CSS selector inside the section element, for example "a[data-action=show]"
1084
             * @param {String} image new image name ("i/show", "i/hide", etc.)
1085
             * @param {String} stringname new string for the action menu item
1086
             * @param {String} stringcomponent
1087
             * @param {String} newaction new value for data-action attribute of the link
1088
             */
1089
            replaceSectionActionItem: function(sectionelement, selector, image, stringname,
1090
                                                    stringcomponent, newaction) {
1091
                log.debug('replaceSectionActionItem() is deprecated and will be removed.');
1092
                var actionitem = sectionelement.find(SELECTOR.SECTIONACTIONMENU + ' ' + selector);
1093
                replaceActionItem(actionitem, image, stringname, stringcomponent, newaction);
1094
            },
1095
            // Method to refresh a module.
1096
            refreshModule,
1097
            refreshSection,
1098
        };
1099
    });