Proyectos de Subversion Moodle

Rev

Rev 1224 | Rev 1226 | Ir a la última revisión | | 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
/**
42
 * Initialise module
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");
1224 ariadna 53
  const inputs = userBulkForm.querySelectorAll("input, select"); // Capturar todos los inputs y selects
54
 
361 ariadna 55
  CustomEvents.define(actionSelect, [CustomEvents.events.accessibleChange]);
1 efrain 56
 
361 ariadna 57
  jQuery(actionSelect).on(CustomEvents.events.accessibleChange, (event) => {
58
    if (event.target.value && `${event.target.value}` !== "0") {
59
      const e = new Event("submit", { cancelable: true });
60
      userBulkForm.dispatchEvent(e);
61
      if (!e.defaultPrevented) {
62
        FormChangeChecker.markFormSubmitted(userBulkForm);
63
        userBulkForm.submit();
64
      }
1 efrain 65
    }
361 ariadna 66
  });
1 efrain 67
 
1225 ariadna 68
  // Reset Filters function
69
  const resetFilters = () => {
70
    const filterForm = document.querySelector('form#filters-form'); // Asegúrate de que el selector coincide con los filtros
71
    if (!filterForm) return;
72
 
73
    filterForm.querySelectorAll("input, select").forEach((element) => {
74
      if (element.type === "checkbox" || element.type === "radio") {
75
        element.checked = false;
76
      } else {
77
        element.value = "";
78
      }
79
    });
80
  };
81
 
1224 ariadna 82
  const resetForm = () => {
83
    inputs.forEach((input) => {
84
      if (input.type === "checkbox" || input.type === "radio") {
85
        input.checked = false;
86
      } else {
87
        input.value = "";
88
      }
89
    });
90
  };
91
 
361 ariadna 92
  const updateUserIds = () => {
93
    const selectedUsers = [
94
      ...userReport.querySelectorAll(Selectors.checkedRows),
95
    ];
96
    const selectedUserIds = selectedUsers.map((check) => parseInt(check.value));
97
    userBulkForm.querySelector('[name="userids"]').value =
98
      selectedUserIds.join(",");
1 efrain 99
 
361 ariadna 100
    actionSelect.disabled = selectedUsers.length === 0;
101
    if (actionSelect.disabled) {
102
      actionSelect.value = "0";
103
    }
1 efrain 104
 
361 ariadna 105
    const selectedUsersNames = selectedUsers.map(
106
      (check) => document.querySelector(`label[for="${check.id}"]`).textContent
107
    );
1224 ariadna 108
 
361 ariadna 109
    userBulkForm.data = {
110
      userids: selectedUserIds,
111
      usernames: selectedUsersNames,
1 efrain 112
    };
361 ariadna 113
  };
1 efrain 114
 
361 ariadna 115
  updateUserIds();
1 efrain 116
 
361 ariadna 117
  document.addEventListener("change", (event) => {
118
    if (
119
      (event.target.matches(Selectors.checkbox) ||
120
        event.target.matches(Selectors.masterCheckbox)) &&
121
      userReport.contains(event.target)
122
    ) {
123
      updateUserIds();
124
    }
125
  });
1 efrain 126
 
361 ariadna 127
  document.addEventListener(tableEvents.tableContentRefreshed, (event) => {
128
    if (userReport.contains(event.target)) {
129
      updateUserIds();
1225 ariadna 130
      resetFilters(); // Restablecer los filtros después de cada actualización
1224 ariadna 131
      resetForm(); // Resetear el formulario después de cada actualización de la tabla
361 ariadna 132
    }
133
  });
1225 ariadna 134
 
135
  jQuery(actionSelect).on(CustomEvents.events.accessibleChange, (event) => {
136
    if (event.target.value && `${event.target.value}` !== "0") {
137
      const e = new Event("submit", { cancelable: true });
138
      userBulkForm.dispatchEvent(e);
139
      if (!e.defaultPrevented) {
140
        FormChangeChecker.markFormSubmitted(userBulkForm);
141
        userBulkForm.submit();
142
        resetFilters(); // Restablecer los filtros después de cada búsqueda
143
      }
144
    }
145
  });
146
};