|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Quiz Progress — Teacher Dashboard</title> |
| 6 | + <style> |
| 7 | + body { font-family: sans-serif; max-width: 1100px; margin: 2rem auto; padding: 0 1rem; } |
| 8 | + table { border-collapse: collapse; width: 100%; font-size: 0.9rem; } |
| 9 | + th, td { border: 1px solid #ddd; padding: 8px 12px; text-align: left; } |
| 10 | + th { background: #f4f4f4; } |
| 11 | + tr:nth-child(even) { background: #fafafa; } |
| 12 | + select, input { padding: 6px; margin: 0 8px 1rem 0; } |
| 13 | + #auth-bar { margin-bottom: 1.5rem; } |
| 14 | + #message { color: #666; font-style: italic; } |
| 15 | + </style> |
| 16 | +</head> |
| 17 | +<body> |
| 18 | + <h1>Quiz Progress Dashboard</h1> |
| 19 | + |
| 20 | + <div id="auth-bar"> |
| 21 | + <button id="sign-in-btn" onclick="signIn()" style="display:none">Sign in with GitHub</button> |
| 22 | + <span id="user-label"></span> |
| 23 | + </div> |
| 24 | + |
| 25 | + <div id="controls" style="display:none"> |
| 26 | + <label>Filter by student: |
| 27 | + <input id="filter-student" type="text" placeholder="github login"> |
| 28 | + </label> |
| 29 | + <label>Filter by quiz: |
| 30 | + <input id="filter-quiz" type="text" placeholder="quiz id"> |
| 31 | + </label> |
| 32 | + <button onclick="loadResults()">Refresh</button> |
| 33 | + </div> |
| 34 | + |
| 35 | + <p id="message"></p> |
| 36 | + |
| 37 | + <table id="results-table" style="display:none"> |
| 38 | + <thead> |
| 39 | + <tr> |
| 40 | + <th>Student</th> |
| 41 | + <th>GitHub Login</th> |
| 42 | + <th>Quiz ID</th> |
| 43 | + <th>Page</th> |
| 44 | + <th>Score</th> |
| 45 | + <th>Pct</th> |
| 46 | + <th>Submitted</th> |
| 47 | + </tr> |
| 48 | + </thead> |
| 49 | + <tbody id="results-body"></tbody> |
| 50 | + </table> |
| 51 | + |
| 52 | + <script> |
| 53 | + const SUPABASE_URL = 'https://wltmawdleuvcjxqtzkmj.supabase.co'; |
| 54 | + const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6IndsdG1hd2RsZXV2Y2p4cXR6a21qIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzY4MDI5MDEsImV4cCI6MjA5MjM3ODkwMX0.MGZavTM0z6n1RjOJaxR1jj1Emr0zsKCPt38L4k_CO5U'; |
| 55 | + |
| 56 | + const projectId = new URL(SUPABASE_URL).hostname.split('.')[0]; |
| 57 | + const SESSION_KEY = `sb-${projectId}-auth-token`; |
| 58 | + |
| 59 | + function handleOAuthCallback() { |
| 60 | + const hash = window.location.hash; |
| 61 | + if (!hash.includes('access_token=')) return; |
| 62 | + |
| 63 | + const params = new URLSearchParams(hash.slice(1)); |
| 64 | + const accessToken = params.get('access_token'); |
| 65 | + const refreshToken = params.get('refresh_token'); |
| 66 | + const expiresIn = parseInt(params.get('expires_in') || '3600', 10); |
| 67 | + |
| 68 | + if (!accessToken) return; |
| 69 | + |
| 70 | + fetch(`${SUPABASE_URL}/auth/v1/user`, { |
| 71 | + headers: { |
| 72 | + 'apikey': SUPABASE_ANON_KEY, |
| 73 | + 'Authorization': `Bearer ${accessToken}`, |
| 74 | + }, |
| 75 | + }) |
| 76 | + .then(r => r.json()) |
| 77 | + .then(user => { |
| 78 | + const session = { |
| 79 | + access_token: accessToken, |
| 80 | + refresh_token: refreshToken, |
| 81 | + expires_at: Math.floor(Date.now() / 1000) + expiresIn, |
| 82 | + user, |
| 83 | + }; |
| 84 | + localStorage.setItem(SESSION_KEY, JSON.stringify(session)); |
| 85 | + history.replaceState(null, '', window.location.pathname + window.location.search); |
| 86 | + init(); // re-run now that session is stored |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + function getSession() { |
| 91 | + try { |
| 92 | + return JSON.parse(localStorage.getItem(SESSION_KEY)); |
| 93 | + } catch (_) { return null; } |
| 94 | + } |
| 95 | + |
| 96 | + function isSessionValid(session) { |
| 97 | + if (!session?.access_token) return false; |
| 98 | + if (!session.expires_at) return false; |
| 99 | + const nowInSeconds = Math.floor(Date.now() / 1000); |
| 100 | + return session.expires_at > nowInSeconds; |
| 101 | + } |
| 102 | + |
| 103 | + async function refreshSession(session) { |
| 104 | + if (!session?.refresh_token) return null; |
| 105 | + |
| 106 | + try { |
| 107 | + const res = await fetch(`${SUPABASE_URL}/auth/v1/token?grant_type=refresh_token`, { |
| 108 | + method: 'POST', |
| 109 | + headers: { |
| 110 | + 'Content-Type': 'application/json', |
| 111 | + 'apikey': SUPABASE_ANON_KEY, |
| 112 | + }, |
| 113 | + body: JSON.stringify({ |
| 114 | + refresh_token: session.refresh_token, |
| 115 | + }), |
| 116 | + }); |
| 117 | + |
| 118 | + if (!res.ok) return null; |
| 119 | + |
| 120 | + const newSession = await res.json(); |
| 121 | + const updated = { |
| 122 | + access_token: newSession.access_token, |
| 123 | + refresh_token: newSession.refresh_token || session.refresh_token, |
| 124 | + expires_at: Math.floor(Date.now() / 1000) + (newSession.expires_in || 3600), |
| 125 | + user: session.user, |
| 126 | + }; |
| 127 | + localStorage.setItem(SESSION_KEY, JSON.stringify(updated)); |
| 128 | + return updated; |
| 129 | + } catch (_) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + function signIn() { |
| 135 | + const redirectTo = window.location.origin + window.location.pathname + window.location.search; |
| 136 | + const url = `${SUPABASE_URL}/auth/v1/authorize?provider=github&redirect_to=${encodeURIComponent(redirectTo)}`; |
| 137 | + window.location.href = url; |
| 138 | + } |
| 139 | + |
| 140 | + function signOut() { |
| 141 | + localStorage.removeItem(SESSION_KEY); |
| 142 | + init(); |
| 143 | + } |
| 144 | + |
| 145 | + async function loadResults() { |
| 146 | + const session = getSession(); |
| 147 | + if (!session?.access_token) return; |
| 148 | + |
| 149 | + const student = document.getElementById('filter-student').value.trim(); |
| 150 | + const quiz = document.getElementById('filter-quiz').value.trim(); |
| 151 | + |
| 152 | + let url = `${SUPABASE_URL}/rest/v1/quiz_results?order=submitted_at.desc&limit=500`; |
| 153 | + if (student) url += `&github_login=eq.${encodeURIComponent(student)}`; |
| 154 | + if (quiz) url += `&quiz_id=eq.${encodeURIComponent(quiz)}`; |
| 155 | + |
| 156 | + const res = await fetch(url, { |
| 157 | + headers: { |
| 158 | + 'apikey': SUPABASE_ANON_KEY, |
| 159 | + 'Authorization': `Bearer ${session.access_token}`, |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + // If token expired during the API call, handle it |
| 164 | + if (res.status === 401) { |
| 165 | + localStorage.removeItem(SESSION_KEY); |
| 166 | + document.getElementById('message').textContent = 'Session expired. Please sign in again.'; |
| 167 | + document.getElementById('sign-in-btn').style.display = ''; |
| 168 | + document.getElementById('user-label').innerHTML = '<button onclick="signIn()" style="padding:4px 8px;cursor:pointer;">Sign in</button>'; |
| 169 | + document.getElementById('controls').style.display = 'none'; |
| 170 | + document.getElementById('results-table').style.display = 'none'; |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + const rows = await res.json(); |
| 175 | + const msg = document.getElementById('message'); |
| 176 | + |
| 177 | + if (!Array.isArray(rows) || rows.length === 0) { |
| 178 | + msg.textContent = rows.length === 0 |
| 179 | + ? 'No results found.' |
| 180 | + : 'No results returned — your GitHub account may not be in the teachers table.'; |
| 181 | + document.getElementById('results-table').style.display = 'none'; |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + msg.textContent = ''; |
| 186 | + const table = document.getElementById('results-table'); |
| 187 | + table.style.display = ''; |
| 188 | + document.getElementById('results-body').innerHTML = rows.map(r => ` |
| 189 | + <tr> |
| 190 | + <td>${r.student_name}</td> |
| 191 | + <td><a href="https://github.com/${r.github_login}" target="_blank">${r.github_login}</a></td> |
| 192 | + <td>${r.quiz_id}</td> |
| 193 | + <td>${r.page_url}</td> |
| 194 | + <td>${r.score} / ${r.total}</td> |
| 195 | + <td>${r.pct}%</td> |
| 196 | + <td>${new Date(r.submitted_at).toLocaleString()}</td> |
| 197 | + </tr> |
| 198 | + `).join(''); |
| 199 | + } |
| 200 | + |
| 201 | + async function init() { |
| 202 | + let session = getSession(); |
| 203 | + const msg = document.getElementById('message'); |
| 204 | + |
| 205 | + // Check if session exists and is valid |
| 206 | + if (session && !isSessionValid(session)) { |
| 207 | + // Try to refresh the token |
| 208 | + session = await refreshSession(session); |
| 209 | + if (!session) { |
| 210 | + localStorage.removeItem(SESSION_KEY); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + if (!session?.access_token) { |
| 215 | + document.getElementById('sign-in-btn').style.display = ''; |
| 216 | + document.getElementById('user-label').innerHTML = '<button onclick="signIn()" style="padding:4px 8px;cursor:pointer;">Sign in</button>'; |
| 217 | + msg.textContent = 'Sign in with your GitHub account to view results.'; |
| 218 | + document.getElementById('controls').style.display = 'none'; |
| 219 | + document.getElementById('results-table').style.display = 'none'; |
| 220 | + return; |
| 221 | + } |
| 222 | + |
| 223 | + const login = session.user?.user_metadata?.user_name || session.user?.email; |
| 224 | + document.getElementById('user-label').innerHTML = `Signed in as <strong>${login}</strong> <button onclick="signOut()" style="padding:4px 8px;margin-left:1rem;cursor:pointer;">Sign out</button>`; |
| 225 | + document.getElementById('controls').style.display = ''; |
| 226 | + await loadResults(); |
| 227 | + } |
| 228 | + |
| 229 | + if (window.location.hash.includes('access_token=')) { |
| 230 | + handleOAuthCallback(); // calls init() when the session is stored |
| 231 | + } else { |
| 232 | + init(); |
| 233 | + } |
| 234 | + </script> |
| 235 | +</body> |
| 236 | +</html> |
0 commit comments