Proyectos de Subversion Moodle

Rev

Rev 361 | Rev 1225 | 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
 
1224 ariadna 68
  const resetForm = () => {
69
    inputs.forEach((input) => {
70
      if (input.type === "checkbox" || input.type === "radio") {
71
        input.checked = false;
72
      } else {
73
        input.value = "";
74
      }
75
    });
76
  };
77
 
361 ariadna 78
  const updateUserIds = () => {
79
    const selectedUsers = [
80
      ...userReport.querySelectorAll(Selectors.checkedRows),
81
    ];
82
    const selectedUserIds = selectedUsers.map((check) => parseInt(check.value));
83
    userBulkForm.querySelector('[name="userids"]').value =
84
      selectedUserIds.join(",");
1 efrain 85
 
361 ariadna 86
    actionSelect.disabled = selectedUsers.length === 0;
87
    if (actionSelect.disabled) {
88
      actionSelect.value = "0";
89
    }
1 efrain 90
 
361 ariadna 91
    const selectedUsersNames = selectedUsers.map(
92
      (check) => document.querySelector(`label[for="${check.id}"]`).textContent
93
    );
1224 ariadna 94
 
361 ariadna 95
    userBulkForm.data = {
96
      userids: selectedUserIds,
97
      usernames: selectedUsersNames,
1 efrain 98
    };
361 ariadna 99
  };
1 efrain 100
 
361 ariadna 101
  updateUserIds();
1 efrain 102
 
361 ariadna 103
  document.addEventListener("change", (event) => {
104
    if (
105
      (event.target.matches(Selectors.checkbox) ||
106
        event.target.matches(Selectors.masterCheckbox)) &&
107
      userReport.contains(event.target)
108
    ) {
109
      updateUserIds();
110
    }
111
  });
1 efrain 112
 
361 ariadna 113
  document.addEventListener(tableEvents.tableContentRefreshed, (event) => {
114
    if (userReport.contains(event.target)) {
115
      updateUserIds();
1224 ariadna 116
      resetForm(); // Resetear el formulario después de cada actualización de la tabla
361 ariadna 117
    }
118
  });
1224 ariadna 119
};