Proyectos de Subversion Moodle

Rev

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