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
 * Course LTI External tools list management.
18
 *
19
 * @module      mod_lti/course_tools_list
20
 * @copyright   2023 Jake Dallimore <jrhdallimore@gmail.com>
21
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
"use strict";
25
 
26
import Notification from 'core/notification';
27
import Pending from 'core/pending';
28
import Ajax from 'core/ajax';
29
import {add as addToast} from 'core/toast';
30
import {getString, getStrings} from 'core/str';
31
import {refreshTableContent} from 'core_table/dynamic';
32
import * as Selectors from 'core_table/local/dynamic/selectors';
33
import {toggleShowInActivityChooser} from "./repository";
34
 
35
/**
36
 * Initialise module.
37
 */
38
export const init = () => {
39
    document.addEventListener('click', event => {
40
 
41
        const courseToolDelete = event.target.closest('[data-action="course-tool-delete"]');
42
        if (courseToolDelete) {
43
            event.preventDefault();
44
 
45
            // A different message is used in the modal if the tool has usages within the course.
46
            const usage = courseToolDelete.dataset.courseToolUsage;
47
            const deleteBodyStringId = usage > 0 ? 'deletecoursetoolwithusageconfirm' : 'deletecoursetoolconfirm';
48
            const requiredStrings = [
49
                {key: 'deletecoursetool', component: 'mod_lti', param: courseToolDelete.dataset.courseToolName},
50
                {key: deleteBodyStringId, component: 'mod_lti', param: courseToolDelete.dataset.courseToolName},
51
                {key: 'delete', component: 'core', param: courseToolDelete.dataset.courseToolName},
52
                {key: 'coursetooldeleted', component: 'mod_lti', param: courseToolDelete.dataset.courseToolName}
53
            ];
54
            // Use triggerElement to return focus to the action menu toggle.
55
            const triggerElement = courseToolDelete.closest('.dropdown').querySelector('.dropdown-toggle');
56
 
57
            getStrings(requiredStrings).then(([modalTitle, modalBody, deleteLabel]) => {
58
                return Notification.deleteCancelPromise(
59
                    modalTitle,
60
                    modalBody,
61
                    deleteLabel,
62
                    {triggerElement});
63
            }).then(() => {
64
                const pendingPromise = new Pending('mod_lti/course_tools:delete');
65
 
66
                const request = {
67
                    methodname: 'mod_lti_delete_course_tool_type',
68
                    args: {tooltypeid: courseToolDelete.dataset.courseToolId}
69
                };
70
                return Ajax.call([request])[0]
71
                    .then(addToast(getString('coursetooldeleted', 'mod_lti', courseToolDelete.dataset.courseToolName)))
72
                    .then(() => {
73
                        const tableRoot = triggerElement.closest(Selectors.main.region);
74
                        return refreshTableContent(tableRoot);
75
                    })
76
                    .then(pendingPromise.resolve)
77
                    .catch(Notification.exception);
78
            }).catch(() => {
79
                return;
80
            });
81
        }
82
 
83
        const courseShowInActivityChooser = event.target.closest('[data-action="showinactivitychooser-toggle"]');
84
        if (courseShowInActivityChooser) {
85
            const showInActivityChooserStateToggle = courseShowInActivityChooser.dataset.state === "0" ? 1 : 0;
86
            return toggleShowInActivityChooser(
87
                courseShowInActivityChooser.dataset.id,
88
                courseShowInActivityChooser.dataset.courseid,
89
                showInActivityChooserStateToggle,
90
            );
91
        }
92
    });
93
};