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
 * Class that defines the bulk move action in the gradebook setup page.
18
 *
19
 * @module     core_grades/bulkactions/edit/tree/move
20
 * @copyright  2023 Mihail Geshoski <mihail@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import BulkAction from 'core/bulkactions/bulk_action';
25
import {get_string as getString} from 'core/str';
26
import ModalSaveCancel from 'core/modal_save_cancel';
27
import Templates from 'core/templates';
28
import Ajax from 'core/ajax';
29
import ModalEvents from 'core/modal_events';
30
import MoveOptionsTree from 'core_grades/bulkactions/edit/tree/move_options_tree';
31
 
32
/** @constant {Object} The object containing the relevant selectors. */
33
const Selectors = {
34
    editTreeForm: '#gradetreeform',
35
    bulkMoveInput: 'input[name="bulkmove"]',
36
    bulkMoveAfterInput: 'input[name="moveafter"]'
37
};
38
 
39
export default class GradebookEditTreeBulkMove extends BulkAction {
40
 
41
    /** @property {int|null} courseId The course ID. */
42
    courseId = null;
43
 
44
    /** @property {MoveOptionsTree|null} moveOptionsTree The move options tree object. */
45
    moveOptionsTree = null;
46
 
47
    /** @property {string|null} gradeTree The grade tree structure. */
48
    gradeTree = null;
49
 
50
    /**
51
     * The class constructor.
52
     *
53
     * @param {int} courseId The course ID.
54
     * @returns {void}
55
     */
56
    constructor(courseId) {
57
        super();
58
        this.courseId = courseId;
59
    }
60
 
61
    /**
62
     * Defines the selector of the element that triggers the bulk move action.
63
     *
64
     * @returns {string} The bulk move action trigger selector.
65
     */
66
    getBulkActionTriggerSelector() {
67
        return 'button[data-action="move"]';
68
    }
69
 
70
    /**
71
     * Defines the behavior once the bulk move action is triggered.
72
     *
73
     * @method executeBulkAction
74
     * @returns {void}
75
     */
76
    async triggerBulkAction() {
77
        const modal = await this.showModal();
78
        this.registerCustomListenerEvents(modal);
79
    }
80
 
81
    /**
82
     * Renders the bulk move action trigger element.
83
     *
84
     * @method renderBulkActionTrigger
85
     * @returns {Promise} The bulk move action trigger promise
86
     */
87
    async renderBulkActionTrigger() {
88
        return Templates.render('core_grades/bulkactions/edit/tree/bulk_move_trigger', {});
89
    }
90
 
91
    /**
92
     * Register custom event listeners.
93
     *
94
     * @method registerCustomClickListenerEvents
95
     * @param {Object} modal The modal object.
96
     * @returns {void}
97
     */
98
    async registerCustomListenerEvents(modal) {
99
        await modal.getBody();
100
        // Initialize the move options tree once the modal is shown.
101
        modal.getRoot().on(ModalEvents.shown, () => {
102
            this.moveOptionsTree = new MoveOptionsTree(() => {
103
                // Enable the 'Move' action button once something is selected.
104
                modal.setButtonDisabled('save', false);
105
            });
106
        });
107
        // Destroy the modal once it is hidden.
108
        modal.getRoot().on(ModalEvents.hidden, () => {
109
            modal.destroy();
110
        });
111
        // Define the move action event.
112
        modal.getRoot().on(ModalEvents.save, () => {
113
            // Make sure that a move option is selected.
114
            if (this.moveOptionsTree && this.moveOptionsTree.selectedMoveOption) {
115
                // Set the relevant form values.
116
                document.querySelector(Selectors.bulkMoveInput).value = 1;
117
                document.querySelector(Selectors.bulkMoveAfterInput).value = this.moveOptionsTree.selectedMoveOption.dataset.id;
118
                // Submit the form.
119
                document.querySelector(Selectors.editTreeForm).submit();
120
            }
121
        });
122
    }
123
 
124
    /**
125
     * Fetch the grade tree structure for the current course.
126
     *
127
     * @method fetchGradeTree
128
     * @returns {Promise} The grade tree promise
129
     */
130
    fetchGradeTree() {
131
        const request = {
132
            methodname: 'core_grades_get_grade_tree',
133
            args: {
134
                courseid: this.courseId,
135
            },
136
        };
137
        return Ajax.call([request])[0];
138
    }
139
 
140
    /**
141
     * Renders the bulk move modal body.
142
     *
143
     * @method renderModalBody
144
     * @returns {Promise} The modal body promise
145
     */
146
    async renderModalBody() {
147
        // We need to fetch the grade tree structure only once.
148
        if (this.gradeTree === null) {
149
            this.gradeTree = await this.fetchGradeTree();
150
        }
151
 
152
        return Templates.render('core_grades/bulkactions/edit/tree/bulk_move_grade_tree',
153
            JSON.parse(this.gradeTree));
154
    }
155
 
156
    /**
157
     * Show the bulk move modal.
158
     *
159
     * @method showModal
160
     * @returns {Promise} The modal promise
161
     */
162
     async showModal() {
163
        const modal = await ModalSaveCancel.create({
164
            title: await getString('movesitems', 'grades'),
165
            body: await this.renderModalBody(),
166
            buttons: {
167
                save: await getString('move')
168
            },
169
            large: true,
170
        });
171
        // Disable the 'Move' action button until something is selected.
172
        modal.setButtonDisabled('save', true);
173
        modal.show();
174
 
175
        return modal;
176
    }
177
}