troodle-expcontent/global.js
Kumi 48fcfbe3f2
fix: correct indentation error in event listener callback
Adjusted the indentation of the event listener's callback function to ensure proper closing of the block. This prevents potential syntax errors and improves code readability.
2024-11-14 16:54:32 +01:00

196 lines
7 KiB
JavaScript

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) {
const url = document.location.href;
const urlobj = new URL(url);
return urlobj.searchParams.get(name);
}
// Function to wait until a global variable is defined
function waitForGlobalVariable(variableName) {
return new Promise((resolve) => {
const checkVariable = setInterval(() => {
if (window[variableName]) {
clearInterval(checkVariable);
resolve();
}
}, 100);
});
}
// Check if the 'embed' parameter is set to '1'
if (getUrlParameter("embed") === "1") {
// Apply custom CSS to hide Moodle interface elements
const 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);
waitForGlobalVariable("bootstrap")
.then(() => {
console.log("Bootstrap loaded successfully");
$(document).ready(function () {
$(".btn-secondary:contains('Back')").hide();
const embedded = getUrlParameter("embed");
const inModal = getUrlParameter("modal");
const page = getUrlParameter("page") ? getUrlParameter("page") : 0;
// Set required attribute on inputs associated with labels containing <i title="Required field">
$('label:has(i[title="Required field"])').each(function () {
const inputId = $(this).attr("for");
$("#" + inputId).attr("required", "required");
});
// Hide the modal if the 'cancel' button is clicked
$("[name=cancel]").click(function () {
hideContent();
});
// Hide any button that has a text starting with "Check" or "Previous page"
$("button").each(function () {
if ($(this).text().startsWith("Check")) {
$(this).hide();
}
});
$("input").each(function () {
if ($(this).val().startsWith("Previous page")) {
$(this).hide();
}
});
$("form").each(function () {
const form = $(this);
// Check if the form has a button where the text contains "Finish attempt"
let isLast = false;
const finishButton = form.find('input[type="submit"][value*="Finish attempt"]');
if (finishButton.length) {
isLast = true;
}
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
let formData = form.serialize();
// Add the submit button's data to formData
const submitButton = form.find(
'input[type="submit"][name="savevalues"]'
);
if (submitButton.length) {
formData +=
"&" +
encodeURIComponent(submitButton.attr("name")) +
"=" +
encodeURIComponent(submitButton.val());
}
const sesskey = form.find('input[name=sesskey]').val();
const attempt = form.find('input[name=attempt]').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, status, xhr) {
if (form.attr("action").includes("/quiz/")) {
// Check if the quiz has a next page by calling the attempt.php page
let nextUrl = form.attr("action").replace("processattempt", "attempt");
nextUrl += "&page=" + (parseInt(page) + 1) + "&attempt=" + attempt + "&sesskey=" + sesskey;
if (isLast) {
nextUrl = "https://invalid.invalid";
}
$.get(nextUrl, function (response) {
console.log(nextUrl + " seems to be a valid URL, redirecting...");
nextUrl = nextUrl + "&embed=1";
if (inModal === "1") {
nextUrl += "&modal=1";
}
document.location.href = nextUrl;
return;
})
.fail(function () {
console.log("No next page found at " + nextUrl + ", finalizing attempt...");
const finalize = "/mod/quiz/processattempt.php";
const data = {
attempt: attempt,
finishattempt: 1,
sesskey: sesskey,
};
$.post(finalize, data, function (response) {
console.log(response);
// Close the modal if `modal=1` is set
if (inModal === "1") {
window.top.closeQuizModal();
} else {
// Else call the nextContent function
window.top.nextContent();
}
});
});
} else {
// Close the modal if `modal=1` is set
if (inModal === "1") {
window.top.closeQuizModal();
} else {
// Else call the nextContent function
window.top.nextContent();
}
}
},
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.message);
});
}
});