Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
 * Cohorts actions.
18
 *
19
 * @module     core_cohort/actions
20
 * @copyright  2024 David Woloszyn <david.woloszyn@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import {dispatchEvent} from 'core/event_dispatcher';
25
import Notification from 'core/notification';
26
import Pending from 'core/pending';
27
import {prefetchStrings} from 'core/prefetch';
28
import {getString} from 'core/str';
29
import {add as addToast} from 'core/toast';
30
import {deleteCohort, deleteCohorts} from 'core_cohort/repository';
31
import * as reportEvents from 'core_reportbuilder/local/events';
32
import * as reportSelectors from 'core_reportbuilder/local/selectors';
33
import {eventTypes} from 'core/local/inplace_editable/events';
34
 
35
const SELECTORS = {
36
    CHECKBOXES: '[data-togglegroup="report-select-all"][data-toggle="slave"]:checked',
37
    DELETE: '[data-action="cohort-delete"]',
38
    DELETEBUTTON: '[data-action="cohort-delete-selected"]',
39
    EDITNAME: '[data-itemtype="cohortname"]',
40
};
41
 
42
/**
43
 * Initialise module.
44
 */
45
export const init = () => {
46
 
47
    prefetchStrings('core_cohort', [
48
        'delcohortsconfirm',
49
        'delcohortssuccess',
50
        'delconfirm',
51
        'delsuccess',
52
    ]);
53
 
54
    prefetchStrings('core', [
55
        'delete',
56
        'deleteselected',
57
        'selectitem',
58
    ]);
59
 
60
    registerEventListeners();
61
};
62
 
63
/**
64
 * Register event listeners.
65
 */
66
export const registerEventListeners = () => {
67
 
68
    // Edit cohort name inplace.
69
    document.addEventListener(eventTypes.elementUpdated, async(event) => {
70
        const editCohortName = event.target.closest(SELECTORS.EDITNAME);
71
        if (editCohortName) {
72
            const cohortId = event.target.dataset.itemid;
73
            const checkbox = document.querySelector(`input[value="${cohortId}"][type="checkbox"]`);
74
            const label = document.querySelector(`label[for="${checkbox.id}"]`);
75
            if (label) {
76
                label.innerHTML = await getString('selectitem', 'core', event.target.dataset.value);
77
            }
78
        }
79
    });
80
 
81
    document.addEventListener('click', event => {
82
 
83
        // Delete single cohort.
84
        const cohortDeleteSingle = event.target.closest(SELECTORS.DELETE);
85
        if (cohortDeleteSingle) {
86
            event.preventDefault();
87
 
88
            const {cohortId, cohortName} = cohortDeleteSingle.dataset;
89
 
90
            Notification.saveCancelPromise(
91
                getString('deleteselected', 'core'),
92
                getString('delconfirm', 'core_cohort', cohortName),
93
                getString('delete', 'core'),
94
                {triggerElement: cohortDeleteSingle}
95
            ).then(() => {
96
                const pendingPromise = new Pending('core_cohort/cohort:delete');
97
                const reportElement = event.target.closest(reportSelectors.regions.report);
98
 
99
                // eslint-disable-next-line promise/no-nesting
100
                return deleteCohort(cohortId)
101
                    .then(() => addToast(getString('delsuccess', 'core_cohort')))
102
                    .then(() => {
103
                        dispatchEvent(reportEvents.tableReload, {preservePagination: true}, reportElement);
104
                        return pendingPromise.resolve();
105
                    })
106
                    .catch(Notification.exception);
107
            }).catch(() => {
108
                return;
109
            });
110
        }
111
 
112
        // Delete multiple cohorts.
113
        const cohortDeleteMultiple = event.target.closest(SELECTORS.DELETEBUTTON);
114
        if (cohortDeleteMultiple) {
115
            event.preventDefault();
116
 
117
            const reportElement = document.querySelector(reportSelectors.regions.report);
118
            const cohortDeleteChecked = reportElement.querySelectorAll(SELECTORS.CHECKBOXES);
119
            if (cohortDeleteChecked.length === 0) {
120
                return;
121
            }
122
 
123
            Notification.saveCancelPromise(
124
                getString('deleteselected', 'core'),
125
                getString('delcohortsconfirm', 'core_cohort'),
126
                getString('delete', 'core'),
127
                {triggerElement: cohortDeleteMultiple}
128
            ).then(() => {
129
                const pendingPromise = new Pending('core_cohort/cohorts:delete');
130
                const deleteCohortIds = [...cohortDeleteChecked].map(check => check.value);
131
 
132
                // eslint-disable-next-line promise/no-nesting
133
                return deleteCohorts(deleteCohortIds)
134
                    .then(() => addToast(getString('delcohortssuccess', 'core_cohort')))
135
                    .then(() => {
136
                        dispatchEvent(reportEvents.tableReload, {preservePagination: true}, reportElement);
137
                        return pendingPromise.resolve();
138
                    })
139
                    .catch(Notification.exception);
140
            }).catch(() => {
141
                return;
142
            });
143
        }
144
    });
145
};