// Enhanced User Sessions handling // Function to load user sessions with export functionality window.loadUserSessions = async function(username) { if (!username) { document.getElementById('userSessionList').innerHTML = ''; return; } try { const res = await fetch('/getResults'); const results = await res.json(); const userSessions = results[username]; const sessionList = document.getElementById('userSessionList'); sessionList.innerHTML = ''; if (!userSessions || !Array.isArray(userSessions) || userSessions.length === 0) { sessionList.innerHTML = '
  • No sessions found.
  • '; return; } userSessions.forEach((session, index) => { const date = new Date(session.timestamp || Date.now()).toLocaleString(); const score = session.score || 0; const total = (session.answers && session.answers.length) || 0; const percent = total > 0 ? Math.round((score / total) * 100) : 0; const li = document.createElement('li'); li.style = 'padding: 1rem; border-bottom: 1px solid var(--border);'; li.innerHTML = `
    ${date}
    Score: ${score}/${total} (${percent}%)
    `; sessionList.appendChild(li); }); // Store the sessions in a global variable for export functionality window.currentUserSessions = { username, sessions: userSessions }; } catch (error) { console.error("Error loading sessions:", error); document.getElementById('userSessionList').innerHTML = '
  • Error loading sessions.
  • '; } }; // Toggle session details view window.toggleSessionDetails = function(sessionIndex) { const detailsDiv = document.getElementById(`session${sessionIndex}`); detailsDiv.classList.toggle('hidden'); }; // Export all sessions for current user as CSV window.exportAllSessionsCSV = function() { if (!window.currentUserSessions || !window.currentUserSessions.sessions) { showToast('Please select a student first'); return; } const { username, sessions } = window.currentUserSessions; // Create CSV content let csvContent = 'Date,Score,Total Words,Percentage,Words Correct,Words Incorrect\n'; sessions.forEach(session => { const date = new Date(session.timestamp || Date.now()).toLocaleDateString(); const score = session.score || 0; const total = (session.answers && session.answers.length) || 0; const percent = total > 0 ? Math.round((score / total) * 100) : 0; // Count correct and incorrect words const correctWords = session.answers ? session.answers.filter(a => a.correct).length : 0; const incorrectWords = total - correctWords; csvContent += `${date},${score},${total},${percent}%,${correctWords},${incorrectWords}\n`; }); // Create and download the CSV file const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.setAttribute('href', url); link.setAttribute('download', `${username}_all_sessions.csv`); link.style.visibility = 'hidden'; document.body.appendChild(link); link.click(); document.body.removeChild(link); showToast('All sessions exported to CSV'); };