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 |
* Javascript module for deleting a database as a preset.
|
|
|
18 |
*
|
|
|
19 |
* @module mod_data/importmappingdialogue
|
|
|
20 |
* @copyright 2022 Amaia Anabitarte <amaia@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
import Notification from 'core/notification';
|
|
|
25 |
import Ajax from 'core/ajax';
|
|
|
26 |
import Url from 'core/url';
|
|
|
27 |
import Templates from 'core/templates';
|
|
|
28 |
import Modal from 'core/modal';
|
|
|
29 |
import {prefetchStrings} from 'core/prefetch';
|
|
|
30 |
import {getString} from 'core/str';
|
|
|
31 |
|
|
|
32 |
// Load global strings.
|
|
|
33 |
prefetchStrings('mod_data', ['mapping:dialogtitle:usepreset']);
|
|
|
34 |
|
|
|
35 |
const selectors = {
|
|
|
36 |
selectPreset: '[data-action="selectpreset"]',
|
|
|
37 |
};
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Initialize module
|
|
|
41 |
*/
|
|
|
42 |
export const init = () => {
|
|
|
43 |
registerEventListeners();
|
|
|
44 |
};
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Register events.
|
|
|
48 |
*/
|
|
|
49 |
const registerEventListeners = () => {
|
|
|
50 |
document.addEventListener('click', (event) => {
|
|
|
51 |
const preset = event.target.closest(selectors.selectPreset);
|
|
|
52 |
if (preset) {
|
|
|
53 |
event.preventDefault();
|
|
|
54 |
showMappingDialogue(preset);
|
|
|
55 |
}
|
|
|
56 |
});
|
|
|
57 |
};
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Show the confirmation modal for uploading a preset.
|
|
|
61 |
*
|
|
|
62 |
* @param {HTMLElement} usepreset the preset to import.
|
|
|
63 |
*/
|
|
|
64 |
const showMappingDialogue = (usepreset) => {
|
|
|
65 |
const presetName = usepreset.dataset.presetname;
|
|
|
66 |
const cmId = usepreset.dataset.cmid;
|
|
|
67 |
|
|
|
68 |
getMappingInformation(cmId, presetName).then((result) => {
|
|
|
69 |
if (result.data && result.data.needsmapping) {
|
|
|
70 |
buildModal({
|
|
|
71 |
title: getString('mapping:dialogtitle:usepreset', 'mod_data', result.data.presetname),
|
|
|
72 |
body: Templates.render('mod_data/fields_mapping_body', result.data),
|
|
|
73 |
footer: Templates.render('mod_data/fields_mapping_footer', getMappingButtons(cmId, presetName)),
|
|
|
74 |
large: true,
|
|
|
75 |
show: true,
|
|
|
76 |
});
|
|
|
77 |
} else {
|
|
|
78 |
window.location.href = Url.relativeUrl(
|
|
|
79 |
'mod/data/field.php',
|
|
|
80 |
{
|
|
|
81 |
id: cmId,
|
|
|
82 |
mode: 'usepreset',
|
|
|
83 |
fullname: presetName,
|
|
|
84 |
},
|
|
|
85 |
false
|
|
|
86 |
);
|
|
|
87 |
}
|
|
|
88 |
return true;
|
|
|
89 |
}).catch(Notification.exception);
|
|
|
90 |
};
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Given an object we want to build a modal ready to show
|
|
|
94 |
*
|
|
|
95 |
* @method buildModal
|
|
|
96 |
* @param {Object} params the modal params
|
|
|
97 |
* @param {Promise} params.title
|
|
|
98 |
* @param {Promise} params.body
|
|
|
99 |
* @param {Promise} params.footer
|
|
|
100 |
* @return {Object} The modal ready to display immediately and render body in later.
|
|
|
101 |
*/
|
|
|
102 |
const buildModal = (params) => {
|
|
|
103 |
return Modal.create({
|
|
|
104 |
...params,
|
|
|
105 |
}).then(modal => {
|
|
|
106 |
modal.showFooter();
|
|
|
107 |
modal.registerCloseOnCancel();
|
|
|
108 |
return modal;
|
|
|
109 |
}).catch(Notification.exception);
|
|
|
110 |
};
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Add buttons to render on mapping modal.
|
|
|
114 |
*
|
|
|
115 |
* @param {int} cmId The id of the current database activity.
|
|
|
116 |
* @param {string} presetName The preset name to delete.
|
|
|
117 |
* @return {array} Same data with buttons.
|
|
|
118 |
*/
|
|
|
119 |
const getMappingButtons = (cmId, presetName) => {
|
|
|
120 |
const data = {};
|
|
|
121 |
|
|
|
122 |
data.mapfieldsbutton = Url.relativeUrl(
|
|
|
123 |
'mod/data/field.php',
|
|
|
124 |
{
|
|
|
125 |
id: cmId,
|
|
|
126 |
fullname: presetName,
|
|
|
127 |
mode: 'usepreset',
|
|
|
128 |
action: 'select',
|
|
|
129 |
},
|
|
|
130 |
false
|
|
|
131 |
);
|
|
|
132 |
|
|
|
133 |
data.applybutton = Url.relativeUrl(
|
|
|
134 |
'mod/data/field.php',
|
|
|
135 |
{
|
|
|
136 |
id: cmId,
|
|
|
137 |
fullname: presetName,
|
|
|
138 |
mode: 'usepreset',
|
|
|
139 |
action: 'notmapping'
|
|
|
140 |
},
|
|
|
141 |
false
|
|
|
142 |
);
|
|
|
143 |
|
|
|
144 |
return data;
|
|
|
145 |
};
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Check whether we should show the mapping dialogue or not.
|
|
|
149 |
*
|
|
|
150 |
* @param {int} cmId The id of the current database activity.
|
|
|
151 |
* @param {string} presetName The preset name to delete.
|
|
|
152 |
* @return {promise} Resolved with the result and warnings of deleting a preset.
|
|
|
153 |
*/
|
|
|
154 |
const getMappingInformation = (cmId, presetName) => {
|
|
|
155 |
const request = {
|
|
|
156 |
methodname: 'mod_data_get_mapping_information',
|
|
|
157 |
args: {
|
|
|
158 |
cmid: cmId,
|
|
|
159 |
importedpreset: presetName,
|
|
|
160 |
}
|
|
|
161 |
};
|
|
|
162 |
return Ajax.call([request])[0];
|
|
|
163 |
};
|