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
 * 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';
11 efrain 28
import PendingPromise from 'core/pending';
1 efrain 29
 
30
const Selectors = {
31
    advancedFormLink: 'a.showadvancedform'
32
};
33
 
11 efrain 34
const getDetailsFromEvent = (event) => {
35
    if (event.target.closest('[data-trigger="add-item-form"]')) {
36
        const trigger = event.target.closest('[data-trigger="add-item-form"]');
37
 
38
        return {
39
            trigger,
40
            formClass: 'core_grades\\form\\add_item',
41
            titleKey: trigger.getAttribute('data-itemid') === '-1' ? 'newitem' : 'itemsedit',
42
            args: {
43
                itemid: trigger.getAttribute('data-itemid'),
44
            },
45
        };
46
    } else if (event.target.closest('[data-trigger="add-category-form"]')) {
47
        const trigger = event.target.closest('[data-trigger="add-category-form"]');
48
        return {
49
            trigger,
50
            formClass: 'core_grades\\form\\add_category',
51
            titleKey: trigger.getAttribute('data-category') === '-1' ? 'newcategory' : 'categoryedit',
52
            args: {
53
                category: trigger.getAttribute('data-category'),
54
            },
55
        };
56
    } else if (event.target.closest('[data-trigger="add-outcome-form"]')) {
57
        const trigger = event.target.closest('[data-trigger="add-outcome-form"]');
58
        return {
59
            trigger,
60
            formClass: 'core_grades\\form\\add_outcome',
61
            titleKey: trigger.getAttribute('data-itemid') === '-1' ? 'newoutcomeitem' : 'outcomeitemsedit',
62
            args: {
63
                itemid: trigger.getAttribute('data-itemid'),
64
            },
65
        };
66
    }
67
 
68
    return null;
69
};
70
 
1 efrain 71
/**
72
 * Initialize module
73
 */
74
export const init = () => {
75
    // Sometimes the trigger does not exist, so lets conditionally add it.
76
    document.addEventListener('click', event => {
11 efrain 77
        const triggerData = getDetailsFromEvent(event);
1 efrain 78
 
11 efrain 79
        if (triggerData) {
1 efrain 80
            event.preventDefault();
11 efrain 81
            const pendingPromise = new PendingPromise(`core_grades:add_item:${triggerData.args.itemid}`);
1 efrain 82
 
11 efrain 83
            const {trigger, formClass, titleKey, args} = triggerData;
1 efrain 84
            args.courseid = trigger.getAttribute('data-courseid');
85
            args.gpr_plugin = trigger.getAttribute('data-gprplugin');
86
 
87
            const modalForm = new ModalForm({
88
                modalConfig: {
11 efrain 89
                    title: getString(titleKey, 'core_grades'),
1 efrain 90
                },
91
                formClass: formClass,
92
                args: args,
93
                saveButtonText: getString('save', 'core'),
94
                returnFocus: trigger,
95
            });
96
 
97
            // Show a toast notification when the form is submitted.
98
            modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
99
                if (event.detail.result) {
11 efrain 100
                    new PendingPromise('core_grades:form_submitted');
1 efrain 101
                    window.location.assign(event.detail.url);
102
                } else {
103
                    Notification.addNotification({
104
                        type: 'error',
105
                        message: getString('saving_failed', 'core_grades')
106
                    });
107
                }
108
            });
109
 
110
            modalForm.show();
11 efrain 111
            pendingPromise.resolve();
1 efrain 112
        }
113
 
114
        const showAdvancedForm = event.target.closest(Selectors.advancedFormLink);
11 efrain 115
        if (showAdvancedForm) {
116
            // Navigate to the advanced form page and cary over any entered data.
1 efrain 117
            event.preventDefault();
11 efrain 118
 
119
            // Do not resolve this pendingPromise - it will be cleared when the page changes.
120
            new PendingPromise('core_grades:show_advanced_form');
1 efrain 121
            const form = event.target.closest('form');
122
            form.action = showAdvancedForm.href;
123
            // Disable the form change checker as we are going to carry over the data to the advanced form.
124
            FormChangeChecker.disableAllChecks();
125
            form.submit();
126
        }
127
    });
128
};