2024-07-02 14:20:55 +00:00
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
|
|
// Function to load a script into the DOM
|
|
|
|
function loadScript(url, integrity, crossorigin) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const script = document.createElement("script");
|
|
|
|
script.src = url;
|
|
|
|
script.integrity = integrity;
|
|
|
|
script.crossOrigin = crossorigin;
|
|
|
|
script.onload = () => resolve(script);
|
|
|
|
script.onerror = () => reject(new Error(`Failed to load script: ${url}`));
|
|
|
|
document.head.appendChild(script);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function to get URL parameters
|
|
|
|
function getUrlParameter(name) {
|
2024-07-02 15:31:28 +00:00
|
|
|
url = document.location.href;
|
2024-07-02 15:43:06 +00:00
|
|
|
urlobj = new URL(url);
|
2024-07-02 15:31:28 +00:00
|
|
|
return urlobj.searchParams.get(name);
|
2024-07-02 14:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the 'embed' parameter is set to '1'
|
|
|
|
if (getUrlParameter("embed") === "1") {
|
|
|
|
// Apply custom CSS to hide Moodle interface elements
|
|
|
|
var style = document.createElement("style");
|
|
|
|
style.innerHTML = `
|
|
|
|
body.path-mod {
|
|
|
|
#header, #footer, .breadcrumb, .navbar, .side-pre, .side-post, .drawer, .drawer-toggler, .footer-popover, .navigation {
|
|
|
|
display: none !important;
|
|
|
|
}
|
|
|
|
#region-main {
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
document.head.appendChild(style);
|
|
|
|
|
|
|
|
loadScript(
|
|
|
|
"https://code.jquery.com/jquery-3.7.1.min.js",
|
|
|
|
"sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=",
|
|
|
|
"anonymous"
|
|
|
|
)
|
|
|
|
.then(() => {
|
|
|
|
console.log("jQuery loaded successfully");
|
|
|
|
// Load Bootstrap after jQuery is loaded
|
|
|
|
return loadScript(
|
|
|
|
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js",
|
|
|
|
"sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz",
|
|
|
|
"anonymous"
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
console.log("Bootstrap loaded successfully");
|
|
|
|
$(document).ready(function () {
|
2024-07-02 15:43:06 +00:00
|
|
|
var embedded = getUrlParameter("embed");
|
2024-07-02 15:58:00 +00:00
|
|
|
var inModal = getUrlParameter("modal");
|
2024-07-02 15:43:06 +00:00
|
|
|
|
2024-07-02 14:20:55 +00:00
|
|
|
// Set required attribute on inputs associated with labels containing <i title="Required field">
|
|
|
|
$('label:has(i[title="Required field"])').each(function () {
|
|
|
|
var inputId = $(this).attr("for");
|
|
|
|
$("#" + inputId).attr("required", "required");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Hide the modal if the 'cancel' button is clicked
|
|
|
|
$("[name=cancel]").click(function () {
|
|
|
|
hideContent();
|
|
|
|
});
|
|
|
|
|
|
|
|
$("form").each(function () {
|
|
|
|
var form = $(this);
|
|
|
|
|
|
|
|
form.submit(function (event) {
|
|
|
|
event.preventDefault(); // Prevent the default form submission
|
|
|
|
|
|
|
|
// Check if the form is valid
|
|
|
|
if (!form[0].checkValidity()) {
|
|
|
|
form[0].reportValidity();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize the form data
|
|
|
|
var formData = form.serialize();
|
|
|
|
|
|
|
|
// Add the submit button's data to formData
|
|
|
|
var submitButton = form.find(
|
|
|
|
'input[type="submit"][name="savevalues"]'
|
|
|
|
);
|
|
|
|
if (submitButton.length) {
|
|
|
|
formData +=
|
|
|
|
"&" +
|
|
|
|
encodeURIComponent(submitButton.attr("name")) +
|
|
|
|
"=" +
|
|
|
|
encodeURIComponent(submitButton.val());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the AJAX request
|
|
|
|
$.ajax({
|
|
|
|
url: form.attr("action"), // Use the form's action attribute
|
|
|
|
type: form.attr("method"), // Use the form's method attribute
|
|
|
|
data: formData,
|
|
|
|
success: function (response) {
|
2024-07-02 15:43:06 +00:00
|
|
|
// Close the modal if `modal=1` is set
|
2024-07-02 15:58:00 +00:00
|
|
|
if (inModal === "1") {
|
2024-07-02 15:43:06 +00:00
|
|
|
window.top.closeQuizModal();
|
|
|
|
} else {
|
|
|
|
// Else call the nextContent function
|
|
|
|
window.top.nextContent();
|
|
|
|
}
|
2024-07-02 14:20:55 +00:00
|
|
|
},
|
|
|
|
error: function (xhr, status, error) {
|
|
|
|
// Handle any errors that occur during the AJAX request
|
|
|
|
console.error(
|
|
|
|
"Form submission failed: " + status + " - " + error
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|