Proyectos de Subversion Moodle

Rev

Rev 1226 | | 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
 * 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
 
361 ariadna 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";
1 efrain 29
 
30
const Selectors = {
361 ariadna 31
  bulkActionsForm: "form#user-bulk-action-form",
32
  userReportWrapper: '[data-region="report-user-list-wrapper"]',
33
  checkbox:
34
    'input[type="checkbox"][data-togglegroup="report-select-all"][data-toggle="slave"]',
35
  masterCheckbox:
36
    'input[type="checkbox"][data-togglegroup="report-select-all"][data-toggle="master"]',
37
  checkedRows:
38
    '[data-togglegroup="report-select-all"][data-toggle="slave"]:checked',
1 efrain 39
};
40
 
41
/**
1227 ariadna 42
 * Initialise module
1 efrain 43
 */
44
export const init = () => {
361 ariadna 45
  const userBulkForm = document.querySelector(Selectors.bulkActionsForm);
46
  const userReport = userBulkForm
47
    ?.closest(Selectors.userReportWrapper)
48
    ?.querySelector(reportSelectors.regions.report);
49
  if (!userBulkForm || !userReport) {
50
    return;
51
  }
52
  const actionSelect = userBulkForm.querySelector("select");
53
  CustomEvents.define(actionSelect, [CustomEvents.events.accessibleChange]);
1 efrain 54
 
361 ariadna 55
  jQuery(actionSelect).on(CustomEvents.events.accessibleChange, (event) => {
56
    if (event.target.value && `${event.target.value}` !== "0") {
57
      const e = new Event("submit", { cancelable: true });
58
      userBulkForm.dispatchEvent(e);
59
      if (!e.defaultPrevented) {
60
        FormChangeChecker.markFormSubmitted(userBulkForm);
61
        userBulkForm.submit();
62
      }
1 efrain 63
    }
361 ariadna 64
  });
1 efrain 65
 
1227 ariadna 66
  // Every time the checkboxes in the report are changed, update the list of users in the form values
67
  // and enable/disable the action select.
361 ariadna 68
  const updateUserIds = () => {
69
    const selectedUsers = [
70
      ...userReport.querySelectorAll(Selectors.checkedRows),
71
    ];
72
    const selectedUserIds = selectedUsers.map((check) => parseInt(check.value));
73
    userBulkForm.querySelector('[name="userids"]').value =
74
      selectedUserIds.join(",");
1 efrain 75
 
1227 ariadna 76
    // Disable the action selector if nothing selected, and reset the current selection.
361 ariadna 77
    actionSelect.disabled = selectedUsers.length === 0;
78
    if (actionSelect.disabled) {
79
      actionSelect.value = "0";
80
    }
1 efrain 81
 
361 ariadna 82
    const selectedUsersNames = selectedUsers.map(
83
      (check) => document.querySelector(`label[for="${check.id}"]`).textContent
84
    );
1227 ariadna 85
    // Add the user ids and names to the form data attributes so they can be available from the
86
    // other JS modules that listen to the form submit event.
361 ariadna 87
    userBulkForm.data = {
88
      userids: selectedUserIds,
89
      usernames: selectedUsersNames,
1 efrain 90
    };
361 ariadna 91
  };
1 efrain 92
 
361 ariadna 93
  updateUserIds();
1 efrain 94
 
361 ariadna 95
  document.addEventListener("change", (event) => {
1227 ariadna 96
    // When checkboxes are checked next to individual users or the master toggle (Select all/none).
361 ariadna 97
    if (
98
      (event.target.matches(Selectors.checkbox) ||
99
        event.target.matches(Selectors.masterCheckbox)) &&
100
      userReport.contains(event.target)
101
    ) {
102
      updateUserIds();
103
    }
104
  });
1 efrain 105
 
361 ariadna 106
  document.addEventListener(tableEvents.tableContentRefreshed, (event) => {
1227 ariadna 107
    // When the report contents is updated (i.e. page is changed, filters applied, etc).
361 ariadna 108
    if (userReport.contains(event.target)) {
109
      updateUserIds();
110
    }
111
  });
1225 ariadna 112
};