first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+108
View File
@@ -0,0 +1,108 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Javascript module for deleting a database as a preset.
*
* @module mod_data/deletepreset
* @copyright 2022 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import Notification from 'core/notification';
import {prefetchStrings} from 'core/prefetch';
import {getString} from 'core/str';
import Ajax from 'core/ajax';
import Url from 'core/url';
const selectors = {
deletePresetButton: '[data-action="deletepreset"]',
};
/**
* Initialize module
*/
export const init = () => {
prefetchStrings('mod_data', [
'deleteconfirm',
'deletewarning',
]);
prefetchStrings('core', [
'delete',
]);
registerEventListeners();
};
/**
* Register events for delete preset option in action menu.
*/
const registerEventListeners = () => {
document.addEventListener('click', (event) => {
const deleteOption = event.target.closest(selectors.deletePresetButton);
if (deleteOption) {
event.preventDefault();
deletePresetConfirm(deleteOption);
}
});
};
/**
* Show the confirmation modal to delete the preset.
*
* @param {HTMLElement} deleteOption the element to delete.
*/
const deletePresetConfirm = (deleteOption) => {
const presetName = deleteOption.getAttribute('data-presetname');
const dataId = deleteOption.getAttribute('data-dataid');
Notification.deleteCancelPromise(
getString('deleteconfirm', 'mod_data', presetName),
getString('deletewarning', 'mod_data'),
).then(() => {
return deletePreset(dataId, presetName);
}).catch(() => {
return;
});
};
/**
* Delete site user preset.
*
* @param {int} dataId The id of the current database activity.
* @param {string} presetName The preset name to delete.
* @return {promise} Resolved with the result and warnings of deleting a preset.
*/
async function deletePreset(dataId, presetName) {
var request = {
methodname: 'mod_data_delete_saved_preset',
args: {
dataid: dataId,
presetnames: {presetname: presetName},
}
};
try {
await Ajax.call([request])[0];
window.location.href = Url.relativeUrl(
'mod/data/preset.php',
{
d: dataId,
},
false
);
} catch (error) {
Notification.exception(error);
}
}
+85
View File
@@ -0,0 +1,85 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Javascript module for editing a database preset.
*
* @module mod_data/editpreset
* @copyright 2022 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import ModalForm from 'core_form/modalform';
import Notification from 'core/notification';
import {getString} from 'core/str';
const selectors = {
editPresetButton: '[data-action="editpreset"]',
};
/**
* Initialize module
*/
export const init = () => {
registerEventListeners();
};
/**
* Register events for update/delete links.
*/
const registerEventListeners = () => {
document.addEventListener('click', (event) => {
const editAction = event.target.closest(selectors.editPresetButton);
if (editAction) {
event.preventDefault();
showEditPresetModal(editAction);
}
});
};
/**
* Show the edit preset modal.
*
* @param {HTMLElement} editAction the edit action element.
*/
const showEditPresetModal = (editAction) => {
const modalForm = new ModalForm({
modalConfig: {
title: getString('editpreset', 'mod_data'),
},
formClass: 'mod_data\\form\\save_as_preset',
args: {
d: editAction.getAttribute('data-dataid'),
action: editAction.getAttribute('data-action'),
presetname: editAction.getAttribute('data-presetname'),
presetdescription: editAction.getAttribute('data-presetdescription')
},
saveButtonText: getString('save'),
returnFocus: editAction,
});
modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
if (event.detail.result) {
window.location.reload();
} else {
Notification.addNotification({
type: 'error',
message: event.detail.errors.join('<br>')
});
}
});
modalForm.show();
};
+163
View File
@@ -0,0 +1,163 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Javascript module for deleting a database as a preset.
*
* @module mod_data/importmappingdialogue
* @copyright 2022 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import Notification from 'core/notification';
import Ajax from 'core/ajax';
import Url from 'core/url';
import Templates from 'core/templates';
import Modal from 'core/modal';
import {prefetchStrings} from 'core/prefetch';
import {getString} from 'core/str';
// Load global strings.
prefetchStrings('mod_data', ['mapping:dialogtitle:usepreset']);
const selectors = {
selectPreset: '[data-action="selectpreset"]',
};
/**
* Initialize module
*/
export const init = () => {
registerEventListeners();
};
/**
* Register events.
*/
const registerEventListeners = () => {
document.addEventListener('click', (event) => {
const preset = event.target.closest(selectors.selectPreset);
if (preset) {
event.preventDefault();
showMappingDialogue(preset);
}
});
};
/**
* Show the confirmation modal for uploading a preset.
*
* @param {HTMLElement} usepreset the preset to import.
*/
const showMappingDialogue = (usepreset) => {
const presetName = usepreset.dataset.presetname;
const cmId = usepreset.dataset.cmid;
getMappingInformation(cmId, presetName).then((result) => {
if (result.data && result.data.needsmapping) {
buildModal({
title: getString('mapping:dialogtitle:usepreset', 'mod_data', result.data.presetname),
body: Templates.render('mod_data/fields_mapping_body', result.data),
footer: Templates.render('mod_data/fields_mapping_footer', getMappingButtons(cmId, presetName)),
large: true,
show: true,
});
} else {
window.location.href = Url.relativeUrl(
'mod/data/field.php',
{
id: cmId,
mode: 'usepreset',
fullname: presetName,
},
false
);
}
return true;
}).catch(Notification.exception);
};
/**
* Given an object we want to build a modal ready to show
*
* @method buildModal
* @param {Object} params the modal params
* @param {Promise} params.title
* @param {Promise} params.body
* @param {Promise} params.footer
* @return {Object} The modal ready to display immediately and render body in later.
*/
const buildModal = (params) => {
return Modal.create({
...params,
}).then(modal => {
modal.showFooter();
modal.registerCloseOnCancel();
return modal;
}).catch(Notification.exception);
};
/**
* Add buttons to render on mapping modal.
*
* @param {int} cmId The id of the current database activity.
* @param {string} presetName The preset name to delete.
* @return {array} Same data with buttons.
*/
const getMappingButtons = (cmId, presetName) => {
const data = {};
data.mapfieldsbutton = Url.relativeUrl(
'mod/data/field.php',
{
id: cmId,
fullname: presetName,
mode: 'usepreset',
action: 'select',
},
false
);
data.applybutton = Url.relativeUrl(
'mod/data/field.php',
{
id: cmId,
fullname: presetName,
mode: 'usepreset',
action: 'notmapping'
},
false
);
return data;
};
/**
* Check whether we should show the mapping dialogue or not.
*
* @param {int} cmId The id of the current database activity.
* @param {string} presetName The preset name to delete.
* @return {promise} Resolved with the result and warnings of deleting a preset.
*/
const getMappingInformation = (cmId, presetName) => {
const request = {
methodname: 'mod_data_get_mapping_information',
args: {
cmid: cmId,
importedpreset: presetName,
}
};
return Ajax.call([request])[0];
};
+65
View File
@@ -0,0 +1,65 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Javascript module for importing presets.
*
* @module mod_data/importpresets
* @copyright 2022 Laurent David <laurent.david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import ModalForm from 'core_form/modalform';
import Notification from 'core/notification';
import {getString} from 'core/str';
const selectors = {
importPresetButton: '[data-action="importpresets"]',
};
/**
* Initialize module
*/
export const init = () => {
document.addEventListener('click', (event) => {
const importPresetButton = event.target.closest(selectors.importPresetButton);
if (!importPresetButton) {
return;
}
event.preventDefault();
const modalForm = new ModalForm({
modalConfig: {
title: getString('importpreset', 'mod_data'),
},
formClass: 'mod_data\\form\\import_presets',
args: {cmid: importPresetButton.dataset.dataid},
saveButtonText: getString('importandapply', 'mod_data'),
});
modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
if (event.detail.result) {
window.location.assign(event.detail.url);
} else {
Notification.addNotification({
type: 'error',
message: event.detail.errors.join('<br>')
});
}
});
modalForm.show();
});
};
+75
View File
@@ -0,0 +1,75 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Javascript module for reseting all templates.
*
* @module mod_data/resetalltemplates
* @copyright 2022 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import Notification from 'core/notification';
import {prefetchStrings} from 'core/prefetch';
import {getString} from 'core/str';
const selectors = {
resetAllTemplatesAction: '[data-action="resetalltemplates"]',
};
/**
* Initialize module
*/
export const init = () => {
prefetchStrings('mod_data', [
'resetalltemplatesconfirmtitle',
'resetalltemplatesconfirm',
]);
prefetchStrings('core', [
'reset',
]);
registerEventListeners();
};
/**
* Register events for option in action menu.
*/
const registerEventListeners = () => {
document.addEventListener('click', (event) => {
const actionLink = event.target.closest(selectors.resetAllTemplatesAction);
if (actionLink) {
event.preventDefault();
resetAllTemplatesConfirm(actionLink);
}
});
};
/**
* Show the confirmation modal to reset all the templates.
*
* @param {HTMLElement} actionLink the element that triggers the action.
*/
const resetAllTemplatesConfirm = async(actionLink) => {
try {
await Notification.saveCancelPromise(
getString('resetalltemplatesconfirmtitle', 'mod_data'),
getString('resetalltemplatesconfirm', 'mod_data'),
getString('reset', 'core'),
);
window.location = actionLink.href;
} catch (error) {
return;
}
};
+69
View File
@@ -0,0 +1,69 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Javascript module for saving a database as a preset.
*
* @module mod_data/saveaspreset
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import ModalForm from 'core_form/modalform';
import Notification from 'core/notification';
import {getString} from 'core/str';
const selectors = {
saveAsPresetButton: '[data-action="saveaspreset"]',
};
/**
* Initialize module.
*/
export const init = () => {
document.addEventListener('click', (event) => {
const saveAsPresetButton = event.target.closest(selectors.saveAsPresetButton);
if (!saveAsPresetButton) {
return;
}
event.preventDefault();
const modalForm = new ModalForm({
modalConfig: {
title: getString('savedataaspreset', 'mod_data'),
},
formClass: 'mod_data\\form\\save_as_preset',
args: {d: saveAsPresetButton.dataset.dataid},
saveButtonText: getString('save'),
returnFocus: saveAsPresetButton,
});
// Show a toast notification when the form is submitted.
modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
if (event.detail.result) {
window.location.reload();
} else {
Notification.addNotification({
type: 'error',
message: event.detail.errors.join('<br>')
});
}
});
modalForm.show();
});
};
+75
View File
@@ -0,0 +1,75 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Javascript module to control the form responsible for selecting a preset.
*
* @module mod_data/selectpreset
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
const selectors = {
presetRadioButton: 'input[name="fullname"]',
selectPresetButton: 'input[name="selectpreset"]',
selectedPresetRadioButton: 'input[name="fullname"]:checked',
};
/**
* Initialize module.
*/
export const init = () => {
const radioButton = document.querySelectorAll(selectors.presetRadioButton);
// Initialize the "Use a preset" button properly.
disableUsePresetButton();
radioButton.forEach((elem) => {
elem.addEventListener('change', function(event) {
event.preventDefault();
// Enable the "Use a preset" button when any of the radio buttons in the presets list is checked.
disableUsePresetButton();
});
});
};
/**
* Decide whether to disable or not the "Use a preset" button.
* When there is no preset selected, the button should be displayed disabled; otherwise, it will appear enabled as a primary button.
*
* @method
* @private
*/
const disableUsePresetButton = () => {
let selectPresetButton = document.querySelector(selectors.selectPresetButton);
const selectedRadioButton = document.querySelector(selectors.selectedPresetRadioButton);
if (selectedRadioButton) {
// There is one preset selected, so the button should be enabled.
selectPresetButton.removeAttribute('disabled');
selectPresetButton.classList.remove('btn-secondary');
selectPresetButton.classList.add('btn-primary');
selectPresetButton.setAttribute('data-presetname', selectedRadioButton.getAttribute('value'));
selectPresetButton.setAttribute('data-cmid', selectedRadioButton.getAttribute('data-cmid'));
} else {
// There is no any preset selected, so the button should be disabled.
selectPresetButton.setAttribute('disabled', true);
selectPresetButton.classList.remove('btn-primary');
selectPresetButton.classList.add('btn-secondary');
selectPresetButton.removeAttribute('data-presetname');
selectPresetButton.removeAttribute('data-cmid');
}
};
+143
View File
@@ -0,0 +1,143 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Javascript module to control the template editor.
*
* @module mod_data/templateseditor
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import {getString} from 'core/str';
import {prefetchStrings} from 'core/prefetch';
import {relativeUrl} from 'core/url';
import {saveCancel} from 'core/notification';
import Templates from 'core/templates';
prefetchStrings('admin', ['confirmation']);
prefetchStrings('mod_data', [
'resettemplateconfirmtitle',
'enabletemplateeditorcheck',
'editorenable'
]);
prefetchStrings('core', [
'reset',
]);
/**
* Template editor constants.
*/
const selectors = {
toggleTemplateEditor: 'input[name="useeditor"]',
resetTemplateAction: '[data-action="resettemplate"]',
resetTemplate: 'input[name="defaultform"]',
resetAllTemplates: 'input[name="resetall"]',
resetAllCheck: 'input[name="resetallcheck"]',
editForm: '#edittemplateform',
};
/**
* Register event listeners for the module.
*
* @param {Number} instanceId The database ID
* @param {string} mode The template mode
*/
const registerEventListeners = (instanceId, mode) => {
registerResetButton(mode);
registerEditorToggler(instanceId, mode);
};
const registerResetButton = (mode) => {
const editForm = document.querySelector(selectors.editForm);
const resetTemplate = document.querySelector(selectors.resetTemplate);
const resetAllTemplates = document.querySelector(selectors.resetAllTemplates);
const resetTemplateAction = document.querySelector(selectors.resetTemplateAction);
if (!resetTemplateAction || !resetTemplate || !editForm) {
return;
}
prefetchStrings('mod_data', [
mode
]);
resetTemplateAction.addEventListener('click', async(event) => {
event.preventDefault();
const params = {
resetallname: "resetallcheck",
templatename: await getString(mode, 'mod_data'),
};
saveCancel(
getString('resettemplateconfirmtitle', 'mod_data'),
Templates.render('mod_data/template_editor_resetmodal', params),
getString('reset', 'core'),
() => {
resetTemplate.value = "true";
editForm.submit();
},
null,
{triggerElement: event.target}
);
});
// The reset all checkbox is inside a modal so we need to capture at document level.
if (!resetAllTemplates) {
return;
}
document.addEventListener('change', (event) => {
if (event.target.matches(selectors.resetAllCheck)) {
resetAllTemplates.value = (event.target.checked) ? "true" : "";
}
});
};
const registerEditorToggler = (instanceId, mode) => {
const toggleTemplateEditor = document.querySelector(selectors.toggleTemplateEditor);
if (!toggleTemplateEditor) {
return;
}
toggleTemplateEditor.addEventListener('click', async(event) => {
event.preventDefault();
// Whether the event action attempts to enable or disable the template editor.
const enableTemplateEditor = event.target.checked;
if (enableTemplateEditor) {
// Display a confirmation dialog before enabling the template editor.
saveCancel(
getString('confirmation', 'admin'),
getString('enabletemplateeditorcheck', 'mod_data'),
getString('editorenable', 'mod_data'),
() => {
window.location = relativeUrl('/mod/data/templates.php', {d: instanceId, mode: mode, useeditor: true});
},
null,
{triggerElement: event.target}
);
} else {
window.location = relativeUrl('/mod/data/templates.php', {d: instanceId, mode: mode, useeditor: false});
}
});
};
/**
* Initialize the module.
*
* @param {int} instanceId The database ID
* @param {string} mode The template mode
*/
export const init = (instanceId, mode) => {
registerEventListeners(instanceId, mode);
};