Proyectos de Subversion Moodle

Rev

Rev 361 | 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
 * Add bulk actions to the users list report
18
 *
19
 * @module     core_admin/bulk_user_actions
20
 * @copyright  2024 Marina Glancy
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import * as reportSelectors from 'core_reportbuilder/local/selectors';
25
import * as tableEvents from 'core_table/local/dynamic/events';
26
import * as FormChangeChecker from 'core_form/changechecker';
27
import * as CustomEvents from 'core/custom_interaction_events';
28
import jQuery from 'jquery';
29
 
30
const Selectors = {
31
    bulkActionsForm: 'form#user-bulk-action-form',
32
    userReportWrapper: '[data-region="report-user-list-wrapper"]',
33
    checkbox: 'input[type="checkbox"][data-togglegroup="report-select-all"][data-toggle="slave"]',
34
    masterCheckbox: 'input[type="checkbox"][data-togglegroup="report-select-all"][data-toggle="master"]',
35
    checkedRows: '[data-togglegroup="report-select-all"][data-toggle="slave"]:checked',
36
};
37
 
38
/**
39
 * Initialise module
40
 */
41
export const init = () => {
42
 
43
    const userBulkForm = document.querySelector(Selectors.bulkActionsForm);
44
    const userReport = userBulkForm?.closest(Selectors.userReportWrapper)?.querySelector(reportSelectors.regions.report);
45
    if (!userBulkForm || !userReport) {
46
        return;
47
    }
48
    const actionSelect = userBulkForm.querySelector('select');
49
    CustomEvents.define(actionSelect, [CustomEvents.events.accessibleChange]);
50
 
51
    jQuery(actionSelect).on(CustomEvents.events.accessibleChange, event => {
52
        if (event.target.value && `${event.target.value}` !== "0") {
53
            const e = new Event('submit', {cancelable: true});
54
            userBulkForm.dispatchEvent(e);
55
            if (!e.defaultPrevented) {
56
                FormChangeChecker.markFormSubmitted(userBulkForm);
57
                userBulkForm.submit();
58
            }
59
        }
60
    });
61
 
62
    // Every time the checkboxes in the report are changed, update the list of users in the form values
63
    // and enable/disable the action select.
64
    const updateUserIds = () => {
65
        const selectedUsers = [...userReport.querySelectorAll(Selectors.checkedRows)];
66
        const selectedUserIds = selectedUsers.map(check => parseInt(check.value));
67
        userBulkForm.querySelector('[name="userids"]').value = selectedUserIds.join(',');
68
 
69
        // Disable the action selector if nothing selected, and reset the current selection.
70
        actionSelect.disabled = selectedUsers.length === 0;
71
        if (actionSelect.disabled) {
72
            actionSelect.value = "0";
73
        }
74
 
75
        const selectedUsersNames = selectedUsers.map(check => document.querySelector(`label[for="${check.id}"]`).textContent);
76
        // Add the user ids and names to the form data attributes so they can be available from the
77
        // other JS modules that listen to the form submit event.
78
        userBulkForm.data = {userids: selectedUserIds, usernames: selectedUsersNames};
79
    };
80
 
81
    updateUserIds();
82
 
83
    document.addEventListener('change', event => {
84
        // When checkboxes are checked next to individual users or the master toggle (Select all/none).
85
        if ((event.target.matches(Selectors.checkbox) || event.target.matches(Selectors.masterCheckbox))
86
                && userReport.contains(event.target)) {
87
            updateUserIds();
88
        }
89
    });
90
 
91
    document.addEventListener(tableEvents.tableContentRefreshed, event => {
92
        // When the report contents is updated (i.e. page is changed, filters applied, etc).
93
        if (userReport.contains(event.target)) {
94
            updateUserIds();
95
        }
96
    });
97
};