Proyectos de Subversion Moodle

Rev

| 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
 * Javascript module for bulk actions.
18
 *
19
 * @module      gradereport_singleview/bulkactions
20
 * @copyright   2022 Ilya Tregubov <ilya@moodle.com>
21
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import Pending from 'core/pending';
25
import CustomEvents from "core/custom_interaction_events";
26
import ModalSaveCancel from 'core/modal_save_cancel';
27
import Templates from 'core/templates';
28
import ModalEvents from 'core/modal_events';
29
import * as Str from 'core/str';
30
import Notification from 'core/notification';
31
import selectors from 'gradereport_singleview/selectors';
32
 
33
/**
34
 * Initialize module.
35
 */
36
export const init = () => {
37
    const pendingPromise = new Pending();
38
    registerListenerEvents();
39
    pendingPromise.resolve();
40
};
41
 
42
/**
43
 * Register bulk actions related event listeners.
44
 *
45
 * @method registerListenerEvents
46
 */
47
const registerListenerEvents = () => {
48
    const events = [
49
        'click',
50
        CustomEvents.events.activate,
51
        CustomEvents.events.keyboardActivate
52
    ];
53
    CustomEvents.define(document, events);
54
 
55
    // Register events.
56
    events.forEach((event) => {
57
        document.addEventListener(event, async(e) => {
58
            const trigger = e.target.closest(selectors.actions.bulkaction);
59
 
60
            if (!trigger) {
61
                return;
62
            }
63
            if ((trigger.dataset.action === 'overrideallgrades') || (trigger.dataset.action === 'overridenonegrades')) {
64
                const override = document.querySelectorAll(selectors.elements.override);
65
 
66
                if (trigger.dataset.action === 'overridenonegrades') {
67
                    // Alert for removing all grade overrides on page.
68
                    Str.get_strings([
69
                        {key: 'removeoverride', component: 'gradereport_singleview'},
70
                        {key: 'overridenoneconfirm', component: 'gradereport_singleview'},
71
                        {key: 'removeoverridesave', component: 'gradereport_singleview'},
72
                        {key: 'cancel', component: 'moodle'},
73
                    ])
74
                    .then((strings) => {
75
                        return Notification.confirm(
76
                            strings[0],
77
                            strings[1],
78
                            strings[2],
79
                            strings[3],
80
                            () => {
81
                                // Uncheck each override checkbox - this will make grade and feedback input fields disabled.
82
                                override.forEach((el) => {
83
                                    if (el.checked) {
84
                                        el.click();
85
                                    }
86
                                });
87
                            });
88
                    })
89
                    .catch(Notification.exception);
90
 
91
                } else {
92
                    // Check each override checkbox - this will make grade and feedback input fields enabled.
93
                    override.forEach((el) => {
94
                        if (!el.checked) {
95
                            el.click();
96
                        }
97
                    });
98
                }
99
            } else if ((trigger.dataset.action === 'excludeallgrades') || (trigger.dataset.action === 'excludenonegrades')) {
100
                const exclude = document.querySelectorAll(selectors.elements.exclude);
101
                const checked = (trigger.dataset.action === 'excludeallgrades');
102
                // Uncheck or check each exclude checkbox.
103
                exclude.forEach((el) => {
104
                    el.checked = checked;
105
                });
106
            } else if (trigger.dataset.action === 'bulklegend') {
107
                // Modal for bulk insert grades.
108
                Str.get_strings([
109
                    {key: 'bulklegend', component: 'gradereport_singleview'},
110
                    {key: 'save', component: 'moodle'},
111
                ])
112
                .then((strings) => {
113
                    return ModalSaveCancel.create({
114
                        body: Templates.render('gradereport_singleview/bulkinsert', {
115
                            id: 'bulkinsertmodal',
116
                            name: 'bulkinsertmodal'
117
                        }),
118
                        title: strings[0],
119
                        buttons: {
120
                            save: strings[1],
121
                        },
122
                        removeOnClose: true,
123
                        show: true,
124
                    });
125
                })
126
                .then((modal) => {
127
                    modal.getFooter().find(selectors.elements.modalsave).attr('disabled', true);
128
 
129
                    // We need to acknowledge that we understand risks of loosing data.
130
                    // Only when acknowledge checkbox is checked we allow selecting insert options.
131
                    modal.getRoot().on('change', selectors.elements.warningcheckbox,
132
                        (e) => {
133
                            e.preventDefault();
134
                            if (e.target.checked) {
135
                                modal.getRoot().find(selectors.elements.modalformdata).removeClass('dimmed_text');
136
                                modal.getRoot().find(selectors.elements.modalradio).removeAttr('disabled');
137
                                modal.getRoot().find(selectors.elements.modalinput).removeAttr('disabled');
138
 
139
                                const formRadioData = modal.getRoot().find(selectors.elements.modalradiochecked).val();
140
                                // We allow saving grades only when all needed data present on form.
141
                                if (formRadioData) {
142
                                    modal.getFooter().find(selectors.elements.modalsave).removeAttr('disabled');
143
                                }
144
                            } else {
145
                                modal.getRoot().find(selectors.elements.modalformdata).addClass('dimmed_text');
146
                                modal.getRoot().find(selectors.elements.modalradio).attr('disabled', true);
147
                                modal.getRoot().find(selectors.elements.modalinput).attr('disabled', true);
148
                                modal.getFooter().find(selectors.elements.modalsave).attr('disabled', true);
149
                            }
150
                        });
151
 
152
                    // We allow saving grades only when all needed data present on form.
153
                    modal.getRoot().on('change', selectors.elements.modalradio, (e) => {
154
                            e.preventDefault();
155
                            modal.getFooter().find(selectors.elements.modalsave).removeAttr('disabled');
156
                        });
157
 
158
                    modal.getRoot().on(ModalEvents.save, () => {
159
                        // When save button is clicked in modal form we insert data from modal
160
                        // into preexisted hidden bulk insert form and Save button for table form.
161
                        document.querySelector(selectors.elements.enablebulkinsert).checked = true;
162
                        const formRadioData = modal.getRoot().find(selectors.elements.modalradiochecked).val();
163
                        const $select = document.querySelector(selectors.elements.formradio);
164
                        $select.value = formRadioData;
165
 
166
                        const formData = modal.getRoot().find(selectors.elements.modalgrade).val();
167
                        document.querySelector(selectors.elements.formgrade).value = formData;
168
                        document.querySelector(selectors.elements.formsave).click();
169
                    });
170
 
171
                    return modal;
172
                })
173
                .catch(Notification.exception);
174
            }
175
        });
176
    });
177
};