Proyectos de Subversion Moodle

Rev

Rev 1 | | 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
 * 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() {
1441 ariadna 67
        return '[data-type="bulkactions"] [data-action="move"]';
1 efrain 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
1441 ariadna 85
     * @param {boolean} showInDropdown Whether the action is displayed under a 'More' dropdown or as a separate button.
86
     * @param {number} index The index of the action.
1 efrain 87
     * @returns {Promise} The bulk move action trigger promise
88
     */
1441 ariadna 89
    async renderBulkActionTrigger(showInDropdown, index) {
90
        return Templates.render('core_grades/bulkactions/edit/tree/bulk_move_trigger', {
91
            showindropdown: showInDropdown,
92
            isfirst: index === 0,
93
        });
1 efrain 94
    }
95
 
96
    /**
97
     * Register custom event listeners.
98
     *
99
     * @method registerCustomClickListenerEvents
100
     * @param {Object} modal The modal object.
101
     * @returns {void}
102
     */
103
    async registerCustomListenerEvents(modal) {
104
        await modal.getBody();
105
        // Initialize the move options tree once the modal is shown.
106
        modal.getRoot().on(ModalEvents.shown, () => {
107
            this.moveOptionsTree = new MoveOptionsTree(() => {
108
                // Enable the 'Move' action button once something is selected.
109
                modal.setButtonDisabled('save', false);
110
            });
111
        });
112
        // Destroy the modal once it is hidden.
113
        modal.getRoot().on(ModalEvents.hidden, () => {
114
            modal.destroy();
115
        });
116
        // Define the move action event.
117
        modal.getRoot().on(ModalEvents.save, () => {
118
            // Make sure that a move option is selected.
119
            if (this.moveOptionsTree && this.moveOptionsTree.selectedMoveOption) {
120
                // Set the relevant form values.
121
                document.querySelector(Selectors.bulkMoveInput).value = 1;
122
                document.querySelector(Selectors.bulkMoveAfterInput).value = this.moveOptionsTree.selectedMoveOption.dataset.id;
123
                // Submit the form.
124
                document.querySelector(Selectors.editTreeForm).submit();
125
            }
126
        });
127
    }
128
 
129
    /**
130
     * Fetch the grade tree structure for the current course.
131
     *
132
     * @method fetchGradeTree
133
     * @returns {Promise} The grade tree promise
134
     */
135
    fetchGradeTree() {
136
        const request = {
137
            methodname: 'core_grades_get_grade_tree',
138
            args: {
139
                courseid: this.courseId,
140
            },
141
        };
142
        return Ajax.call([request])[0];
143
    }
144
 
145
    /**
146
     * Renders the bulk move modal body.
147
     *
148
     * @method renderModalBody
149
     * @returns {Promise} The modal body promise
150
     */
151
    async renderModalBody() {
152
        // We need to fetch the grade tree structure only once.
153
        if (this.gradeTree === null) {
154
            this.gradeTree = await this.fetchGradeTree();
155
        }
156
 
157
        return Templates.render('core_grades/bulkactions/edit/tree/bulk_move_grade_tree',
158
            JSON.parse(this.gradeTree));
159
    }
160
 
161
    /**
162
     * Show the bulk move modal.
163
     *
164
     * @method showModal
165
     * @returns {Promise} The modal promise
166
     */
167
     async showModal() {
168
        const modal = await ModalSaveCancel.create({
169
            title: await getString('movesitems', 'grades'),
170
            body: await this.renderModalBody(),
171
            buttons: {
172
                save: await getString('move')
173
            },
174
            large: true,
175
        });
176
        // Disable the 'Move' action button until something is selected.
177
        modal.setButtonDisabled('save', true);
178
        modal.show();
179
 
180
        return modal;
181
    }
182
}