Upload admin.js
Browse files- js/admin.js +142 -1
js/admin.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import { getDB, normCode } from './db.js';
|
| 2 |
-
import { $, $$, bind, withLoading, toast, openModal, fmtDateTime, fmt, esc } from './util.js';
|
| 3 |
import { loadBankIndex } from './quiz.js';
|
|
|
|
| 4 |
|
| 5 |
let db, user, currentClassCode = null;
|
| 6 |
let classes = [], students = [], banks = [];
|
|
@@ -68,6 +69,52 @@ async function init() {
|
|
| 68 |
bind($('#btnRefreshStudents'), 'click', () => withLoading($('#btnRefreshStudents'), renderStudents));
|
| 69 |
bind($('#btnRefreshAttempts'), 'click', () => withLoading($('#btnRefreshAttempts'), renderAttempts));
|
| 70 |
bind($('#btnRefreshGifts'), 'click', () => withLoading($('#btnRefreshGifts'), renderGifts));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
function switchTab(tab) {
|
|
@@ -81,6 +128,7 @@ function refreshCurrentTab(tab) {
|
|
| 81 |
const current = tab || $$('.nav-btn.active')[0]?.dataset.tab || 'students';
|
| 82 |
if (current === 'classes') renderClasses();
|
| 83 |
else if (current === 'students') renderStudents();
|
|
|
|
| 84 |
else if (current === 'attempts') renderAttempts();
|
| 85 |
else if (current === 'gifts') renderGifts();
|
| 86 |
}
|
|
@@ -166,6 +214,18 @@ async function renderStudents() {
|
|
| 166 |
students = await db.listStudents(currentClassCode);
|
| 167 |
$('#studentCount').textContent = `${students.length} 名學生`;
|
| 168 |
$('#cbAllStudents').checked = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
const bankOpts = banks.map(b => `<option value="${b.id}">${b.name}</option>`).join('');
|
| 171 |
|
|
@@ -343,4 +403,85 @@ async function renderGifts() {
|
|
| 343 |
</tr>`).join('') || '<tr><td colspan="4" class="center mute">尚無發送紀錄</td></tr>';
|
| 344 |
}
|
| 345 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
init();
|
|
|
|
| 1 |
import { getDB, normCode } from './db.js';
|
| 2 |
+
import { $, $$, bind, withLoading, toast, openModal, fmtDateTime, fmt, esc, todayStr, addDays } from './util.js';
|
| 3 |
import { loadBankIndex } from './quiz.js';
|
| 4 |
+
import { SCHEDULE, TIERS, OPEN_OFFSET_DAYS } from './config.js';
|
| 5 |
|
| 6 |
let db, user, currentClassCode = null;
|
| 7 |
let classes = [], students = [], banks = [];
|
|
|
|
| 69 |
bind($('#btnRefreshStudents'), 'click', () => withLoading($('#btnRefreshStudents'), renderStudents));
|
| 70 |
bind($('#btnRefreshAttempts'), 'click', () => withLoading($('#btnRefreshAttempts'), renderAttempts));
|
| 71 |
bind($('#btnRefreshGifts'), 'click', () => withLoading($('#btnRefreshGifts'), renderGifts));
|
| 72 |
+
|
| 73 |
+
// 測驗派送控制綁定
|
| 74 |
+
bind($('#btnGoQuizSettings'), 'click', () => switchTab('quizzes'));
|
| 75 |
+
bind($('#btnToolbarQuiz'), 'click', () => switchTab('quizzes'));
|
| 76 |
+
bind($('#btnSaveQuizSettings'), 'click', saveQuizSettings);
|
| 77 |
+
bind($('#chkOpenAll'), 'change', (e) => {
|
| 78 |
+
$$('.cb-quiz').forEach(cb => cb.disabled = e.target.checked);
|
| 79 |
+
});
|
| 80 |
+
bind($('#cbAllQuizzes'), 'change', (e) => {
|
| 81 |
+
if (!$('#chkOpenAll').checked) {
|
| 82 |
+
$$('.cb-quiz').forEach(cb => cb.checked = e.target.checked);
|
| 83 |
+
updateSelectedCount();
|
| 84 |
+
}
|
| 85 |
+
});
|
| 86 |
+
bind($('#btnQuickSelectToday'), 'click', () => {
|
| 87 |
+
if ($('#chkOpenAll').checked) return toast('已開啟全開模式,不需個別勾選', 'info');
|
| 88 |
+
const today = todayStr();
|
| 89 |
+
$$('.cb-quiz').forEach(cb => {
|
| 90 |
+
const r = SCHEDULE.find(x => x.id === cb.value);
|
| 91 |
+
if (r) {
|
| 92 |
+
const opDate = addDays(r.date, OPEN_OFFSET_DAYS);
|
| 93 |
+
if (today >= opDate) cb.checked = true;
|
| 94 |
+
}
|
| 95 |
+
});
|
| 96 |
+
updateSelectedCount();
|
| 97 |
+
toast('已自動勾選今日排程到達之測驗', 'info');
|
| 98 |
+
});
|
| 99 |
+
bind($('#btnQuickSelectAllUnits'), 'click', () => {
|
| 100 |
+
if ($('#chkOpenAll').checked) return toast('已開啟全開模式,不需個別勾選', 'info');
|
| 101 |
+
$$('.cb-quiz').forEach(cb => {
|
| 102 |
+
const r = SCHEDULE.find(x => x.id === cb.value);
|
| 103 |
+
if (r && r.tier === 1) cb.checked = true;
|
| 104 |
+
});
|
| 105 |
+
updateSelectedCount();
|
| 106 |
+
toast('已勾選所有 25 題單元測驗', 'info');
|
| 107 |
+
});
|
| 108 |
+
bind($('#btnQuickSelectAll'), 'click', () => {
|
| 109 |
+
if ($('#chkOpenAll').checked) return toast('已開啟全開模式,不需個別勾選', 'info');
|
| 110 |
+
$$('.cb-quiz').forEach(cb => cb.checked = true);
|
| 111 |
+
updateSelectedCount();
|
| 112 |
+
});
|
| 113 |
+
bind($('#btnQuickDeselectAll'), 'click', () => {
|
| 114 |
+
if ($('#chkOpenAll').checked) return toast('已開啟全開模式,請先取消全開模式', 'info');
|
| 115 |
+
$$('.cb-quiz').forEach(cb => cb.checked = false);
|
| 116 |
+
updateSelectedCount();
|
| 117 |
+
});
|
| 118 |
}
|
| 119 |
|
| 120 |
function switchTab(tab) {
|
|
|
|
| 128 |
const current = tab || $$('.nav-btn.active')[0]?.dataset.tab || 'students';
|
| 129 |
if (current === 'classes') renderClasses();
|
| 130 |
else if (current === 'students') renderStudents();
|
| 131 |
+
else if (current === 'quizzes') renderQuizzes();
|
| 132 |
else if (current === 'attempts') renderAttempts();
|
| 133 |
else if (current === 'gifts') renderGifts();
|
| 134 |
}
|
|
|
|
| 214 |
students = await db.listStudents(currentClassCode);
|
| 215 |
$('#studentCount').textContent = `${students.length} 名學生`;
|
| 216 |
$('#cbAllStudents').checked = false;
|
| 217 |
+
|
| 218 |
+
// 更新快捷測驗狀態橫幅
|
| 219 |
+
const classInfo = await db.getClass(currentClassCode) || {};
|
| 220 |
+
const statusEl = $('#lblQuickQuizStatus');
|
| 221 |
+
if (statusEl) {
|
| 222 |
+
if (classInfo.openAll) {
|
| 223 |
+
statusEl.innerHTML = '🎯 測驗開放狀態:<b style="color:var(--mint)">🔓 全開模式 (自由練習,所有單元皆開放)</b>';
|
| 224 |
+
} else {
|
| 225 |
+
const openCount = Array.isArray(classInfo.openRanges) ? classInfo.openRanges.length : 0;
|
| 226 |
+
statusEl.innerHTML = `🎯 測驗開放狀態:<b style="color:var(--gold)">已手動開放 ${openCount} 項單元</b> <span class="mute">(其餘依每日排程)</span>`;
|
| 227 |
+
}
|
| 228 |
+
}
|
| 229 |
|
| 230 |
const bankOpts = banks.map(b => `<option value="${b.id}">${b.name}</option>`).join('');
|
| 231 |
|
|
|
|
| 403 |
</tr>`).join('') || '<tr><td colspan="4" class="center mute">尚無發送紀錄</td></tr>';
|
| 404 |
}
|
| 405 |
|
| 406 |
+
// -------------------------------------------------------------
|
| 407 |
+
// 測驗派送與進度開放管理
|
| 408 |
+
// -------------------------------------------------------------
|
| 409 |
+
async function renderQuizzes() {
|
| 410 |
+
if (!currentClassCode) {
|
| 411 |
+
$('#lblQuizClass').textContent = '未選擇班級';
|
| 412 |
+
$('#tbQuizzes tbody').innerHTML = '<tr><td colspan="7" class="center mute" style="padding:40px;">⚠️ 請先在上方下拉選單選擇或建立班級,即可派送測驗!</td></tr>';
|
| 413 |
+
return;
|
| 414 |
+
}
|
| 415 |
+
const c = classes.find(x => x.code === currentClassCode) || { name: currentClassCode };
|
| 416 |
+
$('#lblQuizClass').textContent = `${c.name} (${currentClassCode})`;
|
| 417 |
+
|
| 418 |
+
const classInfo = await db.getClass(currentClassCode) || {};
|
| 419 |
+
const openAll = !!classInfo.openAll;
|
| 420 |
+
const openRanges = Array.isArray(classInfo.openRanges) ? classInfo.openRanges : [];
|
| 421 |
+
|
| 422 |
+
$('#chkOpenAll').checked = openAll;
|
| 423 |
+
|
| 424 |
+
const today = todayStr();
|
| 425 |
+
const tb = $('#tbQuizzes tbody');
|
| 426 |
+
|
| 427 |
+
tb.innerHTML = SCHEDULE.map((r) => {
|
| 428 |
+
const tr = TIERS[r.tier] || { name: String(r.tier) };
|
| 429 |
+
const opDate = addDays(r.date, OPEN_OFFSET_DAYS);
|
| 430 |
+
const isScheduleOpen = today >= opDate;
|
| 431 |
+
const isManuallyOpen = openRanges.includes(r.id);
|
| 432 |
+
|
| 433 |
+
let statusBadge = '';
|
| 434 |
+
if (openAll) {
|
| 435 |
+
statusBadge = '<span class="badge badge-mint">全開模式 (已開放)</span>';
|
| 436 |
+
} else if (isManuallyOpen) {
|
| 437 |
+
statusBadge = '<span class="badge" style="background:var(--gold); color:#000; font-weight:bold;">已手動開放</span>';
|
| 438 |
+
} else if (isScheduleOpen) {
|
| 439 |
+
statusBadge = '<span class="badge badge-mint">排程已到 (已開放)</span>';
|
| 440 |
+
} else {
|
| 441 |
+
statusBadge = '<span class="mute">🔒 鎖定中 (等待排程或派送)</span>';
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
const rangeName = r.bank === 'labor' ? '勞權及SDGs' : `${tr.name} ${r.s}`;
|
| 445 |
+
const rangeDesc = `${r.from} ~ ${r.to} 題`;
|
| 446 |
+
|
| 447 |
+
return `<tr>
|
| 448 |
+
<td class="cb-cell">
|
| 449 |
+
<input type="checkbox" class="cb-quiz" value="${r.id}" ${isManuallyOpen ? 'checked' : ''} ${openAll ? 'disabled' : ''}>
|
| 450 |
+
</td>
|
| 451 |
+
<td><span class="badge">${tr.name}</span></td>
|
| 452 |
+
<td><b>${esc(r.id)}</b></td>
|
| 453 |
+
<td>${esc(rangeName)}</td>
|
| 454 |
+
<td class="num">${rangeDesc}</td>
|
| 455 |
+
<td class="mute">${r.date} <small>(提前1天開放: ${opDate})</small></td>
|
| 456 |
+
<td>${statusBadge}</td>
|
| 457 |
+
</tr>`;
|
| 458 |
+
}).join('');
|
| 459 |
+
|
| 460 |
+
updateSelectedCount();
|
| 461 |
+
|
| 462 |
+
$$('.cb-quiz', tb).forEach(cb => bind(cb, 'change', updateSelectedCount));
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
function updateSelectedCount() {
|
| 466 |
+
const checked = $$('.cb-quiz:checked').length;
|
| 467 |
+
$('#lblSelectedCount').textContent = `已手動選取 ${checked} / ${SCHEDULE.length} 項`;
|
| 468 |
+
$('#cbAllQuizzes').checked = checked > 0 && checked === SCHEDULE.length;
|
| 469 |
+
}
|
| 470 |
+
|
| 471 |
+
async function saveQuizSettings() {
|
| 472 |
+
if (!currentClassCode) return toast('請先選擇班級', 'error');
|
| 473 |
+
const btn = $('#btnSaveQuizSettings');
|
| 474 |
+
withLoading(btn, async () => {
|
| 475 |
+
try {
|
| 476 |
+
const openAll = $('#chkOpenAll').checked;
|
| 477 |
+
const openRanges = $$('.cb-quiz:checked').map(cb => cb.value);
|
| 478 |
+
await db.updateClass(currentClassCode, { openAll, openRanges });
|
| 479 |
+
toast('✅ 測驗派送設定已儲存!學生端即刻生效', 'success');
|
| 480 |
+
await renderQuizzes();
|
| 481 |
+
} catch(e) {
|
| 482 |
+
toast(e.message, 'error');
|
| 483 |
+
}
|
| 484 |
+
});
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
init();
|