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
 * This module provides the course copy modal from the course and
18
 * category management screen.
19
 *
20
 * @module     core_course/copy_modal
21
 * @copyright  2020 onward The Moodle Users Association <https://moodleassociation.org/>
22
 * @author     Matt Porritt <mattp@catalyst-au.net>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @since      3.9
25
 */
26
 
27
import {get_string as getString} from 'core/str';
28
import Modal from 'core/modal';
29
import * as ajax from 'core/ajax';
30
import * as Fragment from 'core/fragment';
31
import Notification from 'core/notification';
32
import * as Config from 'core/config';
33
 
34
export default class CopyModal {
35
    static init(context) {
36
        return new CopyModal(context);
37
    }
38
 
39
    constructor(context) {
40
        this.contextid = context;
41
 
42
        this.registerEventListeners();
43
    }
44
 
45
    registerEventListeners() {
46
        document.addEventListener('click', (e) => {
47
            const copyAction = e.target.closest('.action-copy');
48
            if (!copyAction) {
49
                return;
50
            }
51
            e.preventDefault(); // Stop. Hammer time.
52
 
53
            const url = new URL(copyAction.href);
54
            const params = new URLSearchParams(url.search);
55
 
56
            this.fetchCourseData(params.get('id'))
57
            .then(([course]) => this.createModal(course))
58
            .catch((error) => Notification.exception(error));
59
        });
60
    }
61
 
62
    fetchCourseData(courseid) {
63
        return ajax.call([{
64
            methodname: 'core_course_get_courses',
65
            args: {
66
                options: {
67
                    ids: [courseid],
68
                },
69
            },
70
        }])[0];
71
    }
72
 
73
    submitBackupRequest(jsonformdata) {
74
        return ajax.call([{
75
            methodname: 'core_backup_submit_copy_form',
76
            args: {
77
                jsonformdata,
78
            },
79
        }])[0];
80
    }
81
 
82
    createModal(
83
        course,
84
        formdata = {},
85
    ) {
86
        const params = {
87
            jsonformdata: JSON.stringify(formdata),
88
            courseid: course.id,
89
        };
90
 
91
        // Create the Modal.
92
        return Modal.create({
93
            title: getString('copycoursetitle', 'backup', course.shortname),
94
            body: Fragment.loadFragment('course', 'new_base_form', this.contextid, params),
95
            large: true,
96
            show: true,
97
            removeOnClose: true,
98
        })
99
        .then((modal) => {
100
            // Explicitly handle form click events.
101
            modal.getRoot().on('click', '#id_submitreturn', (e) => {
102
                this.processModalForm(course, modal, e);
103
            });
104
            modal.getRoot().on('click', '#id_cancel', (e) => {
105
                e.preventDefault();
106
                modal.destroy();
107
            });
108
            modal.getRoot().on('click', '#id_submitdisplay', (e) => {
109
                e.formredirect = true;
110
                this.processModalForm(course, modal, e);
111
 
112
            });
113
 
114
            return modal;
115
        });
116
    }
117
 
118
    processModalForm(course, modal, e) {
119
        e.preventDefault(); // Stop modal from closing.
120
 
121
        // Form data.
122
        const copyform = modal.getRoot().find('form').serialize();
123
        const formjson = JSON.stringify(copyform);
124
 
125
        // Handle invalid form fields for better UX.
126
        const invalid = modal.getRoot()[0].querySelectorAll('[aria-invalid="true"], .error');
127
        if (invalid.length) {
128
            invalid[0].focus();
129
            return;
130
        }
131
 
132
        modal.destroy();
133
 
134
        // Submit form via ajax.
135
        this.submitBackupRequest(formjson)
136
        .then(() => {
137
            if (e.formredirect == true) {
138
                // We are redirecting to copy progress display.
139
                const redirect = `${Config.wwwroot}/backup/copyprogress.php?id=${course.id}`;
140
                window.location.assign(redirect);
141
            }
142
 
143
            return;
144
        })
145
        .catch(() => {
146
            // Form submission failed server side, redisplay with errors.
147
            this.createModal(course, copyform);
148
        });
149
    }
150
}