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
 * Selected courses.
18
 *
19
 * @module     tool_dataprivacy/selectedcourses
20
 * @copyright  2021 The Open University
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @since      Moodle 4.3
23
 */
24
 
25
import Ajax from 'core/ajax';
26
import Notification from 'core/notification';
27
import ModalSaveCancel from 'core/modal_save_cancel';
28
import ModalEvents from 'core/modal_events';
29
import Fragment from 'core/fragment';
30
import {prefetchStrings} from 'core/prefetch';
31
import {getString} from 'core/str';
32
 
33
prefetchStrings('tool_dataprivacy', [
34
    'selectcourses',
35
    'approverequest',
36
    'errornoselectedcourse',
37
]);
38
 
39
/**
40
 * Selected Courses popup modal.
41
 *
42
 */
43
export default class SelectedCourses {
44
    /**
45
     * @var {String} contextId Context ID to load the fragment.
46
     * @private
47
     */
48
    contextId = 0;
49
 
50
    /**
51
     * @var {String} requestId ID of data export request.
52
     * @private
53
     */
54
    requestId = 0;
55
 
56
    /**
57
     * Constructor
58
     *
59
     * @param {String} contextId Context ID to load the fragment.
60
     * @param {String} requestId ID of data export request.
61
     */
62
    constructor(contextId, requestId) {
63
        this.contextId = contextId;
64
        this.requestId = requestId;
65
        // Now create the modal.
66
        ModalSaveCancel.create({
67
            title: getString('selectcourses', 'tool_dataprivacy'),
68
            body: this.getBody({requestid: requestId}),
69
            large: true,
70
            removeOnClose: true,
71
            buttons: {
72
                save: getString('approverequest', 'tool_dataprivacy'),
73
            },
74
        }).then((modal) => {
75
            this.modal = modal;
76
 
77
            return modal;
78
        }).then((modal) => {
79
            // We catch the modal save event, and use it to submit the form inside the modal.
80
            // Triggering a form submission will give JS validation scripts a chance to check for errors.
81
            modal.getRoot().on(ModalEvents.save, this.submitForm.bind(this));
82
 
83
            // We also catch the form submit event and use it to submit the form with ajax.
84
            modal.getRoot().on('submit', 'form', this.submitFormAjax.bind(this));
85
            modal.show();
86
            return modal;
87
        }).catch(Notification.exception);
88
    }
89
 
90
    /**
91
     * Get body of modal.
92
     *
93
     * @method getBody
94
     * @param {Object} formdata
95
     * @private
96
     * @return {Promise}
97
     */
98
    getBody(formdata) {
99
        const params = formdata ? {jsonformdata: JSON.stringify(formdata)} : null;
100
 
101
        // Get the content of the modal.
102
        return Fragment.loadFragment('tool_dataprivacy', 'selectcourses_form', this.contextId, params);
103
    }
104
 
105
    /**
106
     * This triggers a form submission, so that any mform elements can do final tricks before the form submission is processed.
107
     *
108
     * @method submitForm
109
     * @param {Event} e Form submission event.
110
     * @private
111
     */
112
    submitForm(e) {
113
        e.preventDefault();
114
        this.modal.getRoot().find('form').submit();
115
    }
116
 
117
    /**
118
     * Submit select courses form using ajax.
119
     *
120
     * @method submitFormAjax
121
     * @private
122
     * @param {Event} e Form submission event.
123
     */
124
    submitFormAjax(e) {
125
        e.preventDefault();
126
 
127
        // Convert all the form elements values to a serialised string.
128
        let formData = this.modal.getRoot().find('form').serialize();
129
 
130
        if (formData.indexOf('coursecontextids') === -1) {
131
            const customSelect = this.modal.getRoot().find('.custom-select');
132
            const invalidText = this.modal.getRoot().find('.invalid-feedback');
133
            customSelect.addClass('is-invalid');
134
            invalidText.attr('style', 'display: block');
135
            getString('errornoselectedcourse', 'tool_dataprivacy').then(value => {
136
                invalidText.empty().append(value);
137
                return;
138
            }).catch(Notification.exception);
139
            return;
140
        }
141
 
142
        Ajax.call([{
143
            methodname: 'tool_dataprivacy_submit_selected_courses_form',
144
            args: {requestid: this.requestId, jsonformdata: JSON.stringify(formData)},
145
        }])[0]
146
        .then((data) => {
147
            if (data.warnings.length > 0) {
148
                this.modal.setBody(this.getBody(formData));
149
            } else {
150
                this.modal.destroy();
151
                document.location.reload();
152
            }
153
            return data;
154
        })
155
        .catch((error) => Notification.exception(error));
156
    }
157
}