document.addEventListener('DOMContentLoaded', function() { const startExamBtn = document.getElementById('startExamBtn'); const pageLoader = document.getElementById('pageLoader'); const loaderMessage = document.getElementById('loaderMessage'); const examSelectionModal = new bootstrap.Modal(document.getElementById('examSelectionModal')); // Form elements const examCategorySelect = document.getElementById('examCategory'); const examNameSelect = document.getElementById('examName'); const examDescription = document.getElementById('examDescription'); const startSelectedExamBtn = document.getElementById('startSelectedExam'); const viewPastResultsBtn = document.getElementById('viewPastResultsBtn'); // Hide View Results button by default - only show when current exam is EVALUATING or EVALUATED if (viewPastResultsBtn) { viewPastResultsBtn.closest('li').style.display = 'none'; } let labs = []; // Will store all labs fetched from the API let selectedLab = null; // Will store the currently selected lab // Check for current exam status on page load checkCurrentExamStatus(); console.log('Loading labs on page load...'); // Load labs data when the page loads fetchLabs(false); // Function to check current exam status function checkCurrentExamStatus() { fetch('/facilitator/api/v1/exams/current') .then(response => { if (!response.ok) { if (response.status !== 404) { console.error('Error checking current exam status:', response.status); } return null; } return response.json(); }) .then(data => { if (data && data.id) { // Store current exam data in localStorage for the View Results functionality localStorage.setItem('currentExamData', JSON.stringify(data)); // Show View Results button only if status is EVALUATING or EVALUATED if (data.status === 'EVALUATING' || data.status === 'EVALUATED') { if (viewPastResultsBtn) { viewPastResultsBtn.closest('li').style.display = 'block'; } } // If exam is in PREPARING state, show loading overlay and start polling if (data.status === 'PREPARING') { console.log('Exam is in PREPARING state, showing loading overlay'); showLoadingOverlay(); updateLoadingMessage('Preparing lab environment...'); updateExamInfo(data.info?.name || 'Unknown Exam'); // Start polling for status pollExamStatus(data.id).then(statusData => { if (statusData.status === 'READY') { // Redirect to exam page when ready window.location.href = `/exam.html?id=${data.id}`; } }); } } }) .catch(error => { console.error('Error checking current exam status:', error); }); } // Event listener for the "Start Exam" button startExamBtn.addEventListener('click', function(e) { e.preventDefault(); console.log('Checking for active exam sessions...'); // First check if there's any active exam fetch('/facilitator/api/v1/exams/current') .then(response => { if (response.status === 404) { console.log('No active exam found, proceeding with new exam'); // No active exam, proceed as normal if (labs.length > 0) { console.log('Using pre-loaded labs data'); examSelectionModal.show(); } else { console.log('No pre-loaded labs data available, fetching now...'); fetchLabs(true); } return null; } if (!response.ok) { console.error('Error checking current exam status:', response.status); return null; } return response.json(); }) .then(data => { if (data && data.id) { console.log('Active exam found:', data.id, 'Status:', data.status); // Active exam found, show warning modal showActiveExamWarningModal(data); } }) .catch(error => { console.error('Error checking for active exam:', error); // Proceed anyway in case of error if (labs.length > 0) { examSelectionModal.show(); } else { fetchLabs(true); } }); }); // Function to show warning modal for active exam function showActiveExamWarningModal(examData) { // Create modal HTML const modalHTML = `
`; // Add modal to DOM if it doesn't exist if (!document.getElementById('activeExamWarningModal')) { document.body.insertAdjacentHTML('beforeend', modalHTML); } // Get modal element and create Bootstrap modal const modalElement = document.getElementById('activeExamWarningModal'); const warningModal = new bootstrap.Modal(modalElement); // Show the modal warningModal.show(); // Remove any existing event listeners by cloning and replacing the buttons const oldTerminateBtn = document.getElementById('terminateAndProceedBtn'); const newTerminateBtn = oldTerminateBtn.cloneNode(true); oldTerminateBtn.parentNode.replaceChild(newTerminateBtn, oldTerminateBtn); const oldContinueBtn = document.getElementById('continueSessionBtn'); const newContinueBtn = oldContinueBtn.cloneNode(true); oldContinueBtn.parentNode.replaceChild(newContinueBtn, oldContinueBtn); // Add event listener for continue session button document.getElementById('continueSessionBtn').addEventListener('click', function() { // Redirect to the current exam window.location.href = `/exam.html?id=${examData.id}`; }); // Add event listener for terminate and proceed button document.getElementById('terminateAndProceedBtn').addEventListener('click', function() { // Update button to show progress const terminateBtn = document.getElementById('terminateAndProceedBtn'); terminateBtn.disabled = true; terminateBtn.innerHTML = '${lab.description || 'No description available.'}