Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | 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
 * Prints the add item gradebook form
18
 *
19
 * @module core_grades
20
 * @copyright 2023 Mathew May <mathew.solutions>
21
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22
 */
23
 
24
import ModalForm from 'core_form/modalform';
25
import {getString} from 'core/str';
26
import Notification from 'core/notification';
27
import * as FormChangeChecker from 'core_form/changechecker';
28
 
29
const Selectors = {
30
    advancedFormLink: 'a.showadvancedform'
31
};
32
 
33
/**
34
 * Initialize module
35
 */
36
export const init = () => {
37
    // Sometimes the trigger does not exist, so lets conditionally add it.
38
    document.addEventListener('click', event => {
39
        const args = {};
40
 
41
        let formClass = null;
42
        let title = null;
43
        let trigger = null;
44
        if (event.target.closest('[data-trigger="add-item-form"]')) {
45
            event.preventDefault();
46
            trigger = event.target.closest('[data-trigger="add-item-form"]');
47
            formClass = 'core_grades\\form\\add_item';
48
            title = trigger.getAttribute('data-itemid') === '-1' ?
49
                getString('newitem', 'core_grades') : getString('itemsedit', 'core_grades');
50
            args.itemid = trigger.getAttribute('data-itemid');
51
        } else if (event.target.closest('[data-trigger="add-category-form"]')) {
52
            event.preventDefault();
53
            trigger = event.target.closest('[data-trigger="add-category-form"]');
54
            formClass = 'core_grades\\form\\add_category';
55
            title = trigger.getAttribute('data-category') === '-1' ?
56
                getString('newcategory', 'core_grades') : getString('categoryedit', 'core_grades');
57
            args.category = trigger.getAttribute('data-category');
58
        } else if (event.target.closest('[data-trigger="add-outcome-form"]')) {
59
            event.preventDefault();
60
            trigger = event.target.closest('[data-trigger="add-outcome-form"]');
61
            formClass = 'core_grades\\form\\add_outcome';
62
            title = trigger.getAttribute('data-itemid') === '-1' ?
63
                getString('newoutcomeitem', 'core_grades') : getString('outcomeitemsedit', 'core_grades');
64
            args.itemid = trigger.getAttribute('data-itemid');
65
        }
66
 
67
        if (trigger) {
68
            args.courseid = trigger.getAttribute('data-courseid');
69
            args.gpr_plugin = trigger.getAttribute('data-gprplugin');
70
 
71
            const modalForm = new ModalForm({
72
                modalConfig: {
73
                    title: title,
74
                },
75
                formClass: formClass,
76
                args: args,
77
                saveButtonText: getString('save', 'core'),
78
                returnFocus: trigger,
79
            });
80
 
81
            // Show a toast notification when the form is submitted.
82
            modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
83
                if (event.detail.result) {
84
                    window.location.assign(event.detail.url);
85
                } else {
86
                    Notification.addNotification({
87
                        type: 'error',
88
                        message: getString('saving_failed', 'core_grades')
89
                    });
90
                }
91
            });
92
 
93
            modalForm.show();
94
        }
95
 
96
        const showAdvancedForm = event.target.closest(Selectors.advancedFormLink);
97
        if (showAdvancedForm) { // Navigate to the advanced form page and cary over any entered data.
98
            event.preventDefault();
99
            const form = event.target.closest('form');
100
            form.action = showAdvancedForm.href;
101
            // Disable the form change checker as we are going to carry over the data to the advanced form.
102
            FormChangeChecker.disableAllChecks();
103
            form.submit();
104
        }
105
    });
106
};