// The teacher's home: the classes I teach, each with its students; I add (a pasted list) and remove students of my own classes.
import { api, lastAnswer, swr } from "./api.js";
import { esc, fmt } from "./math.js";
import { daysText, shortDate } from "./ui.js";
let draw; // the app's view() function
const tabs = active => `
`;
const work = () => import("./teacher_work.js");
const context = { draw: html => draw(html), tabs: active => tabs(active), wire: () => wireTabs() };
/** The number of items in each tab, from the server, filled in after the screen is drawn (and so refreshed after every action). */
async function fillCounts() {
const show = counts => document.querySelectorAll("#teacher-tabs [data-count]").forEach(el => { el.textContent = counts[el.dataset.count]; });
const known = lastAnswer("/api/teaching/counts");
if (known) show(known); // the last numbers at once, the fresh ones when they arrive
try { show(await api("/api/teaching/counts")); } catch { /* the tabs work without the numbers */ }
}
function wireTabs() {
fillCounts();
const screens = { classes: showClasses, tests: showTests, assignments: () => work().then(m => m.showAssignments(context)), inbox: () => work().then(m => m.showInbox(context)), work: () => import("./teacher_boardwork.js").then(m => m.show(context)), progress: () => import("./teacher_progress.js").then(m => m.show(context)), ai: () => import("./teacher_ai.js").then(m => m.show(context)) };
document.querySelectorAll("#teacher-tabs [data-tab]").forEach(b => b.addEventListener("click", () => screens[b.dataset.tab]()));
}
function report(result) {
const lines = [];
if (result.added.length) lines.push(`נוספו ${result.added.length} תלמידים.`);
if (result.already_here.length) lines.push(`${result.already_here.length} כבר היו בכיתה.`);
if (result.teacher.length) lines.push(`${result.teacher.map(esc).join(", ")}: מורה הכיתה לא יכול להיות גם תלמיד בה.`);
if (result.invalid.length) lines.push(`לא נראה כמו מייל: ${result.invalid.map(esc).join(", ")}`);
return lines;
}
const showClasses = (notes = {}) => swr("/api/teaching", classes => drawClasses(classes, notes));
async function drawClasses(classes, notes) {
draw(`${tabs("classes")}
הכיתות שלי
${classes.length ? `
${classes.map(c => `
${esc(c.name)}
${c.students.length} תלמידים
${c.ai_enabled ? "AI פועל" : "AI כבוי: מנהל הכיתות מפעיל"}
${c.students.length ? `
${c.students.map(s => `
${esc(s.email)}${s.signed_in ? "נכנס/ה" : "עוד לא נכנס/ה"}
`).join("")}
`
: `
עוד אין תלמידים בכיתה.
`}
`).join("")}
`
: `
עוד לא שייכו אותך ככיתה. מנהל הכיתות קובע מי מלמד/ת כל כיתה.
`}`);
wireTabs();
document.querySelectorAll("[data-progress]").forEach(b => b.addEventListener("click", () => import("./teacher_progress.js").then(m => m.show(context, Number(b.dataset.progress)))));
document.querySelectorAll(".class-card").forEach(card => {
const id = Number(card.dataset.id);
card.querySelector("form").addEventListener("submit", async event => {
event.preventDefault();
try {
const result = await api(`/api/classes/${id}/students`, { method: "POST", body: { emails: card.querySelector(".emails").value } });
await showClasses({ [id]: { lines: report(result) } });
} catch (e) { await showClasses({ [id]: { error: e.message } }); }
});
card.addEventListener("click", async event => {
const wipe = event.target.closest("[data-wipe]");
if (wipe) { // the student's answer pictures, on request: two steps (the grades stay)
wipe.outerHTML = ``;
return;
}
if (event.target.closest("[data-cancel-wipe]")) { await showClasses(); return; }
const sure = event.target.closest("[data-confirm-wipe]")?.dataset.confirmWipe;
if (sure) {
try {
const done = await api(`/api/classes/${id}/pictures/${encodeURIComponent(sure)}`, { method: "DELETE" });
await showClasses({ [id]: { lines: [`נמחקו ${done.deleted} תמונות של ${esc(sure)}. הציונים וההערות נשארו.`] } });
} catch (e) { await showClasses({ [id]: { error: e.message } }); }
return;
}
const email = event.target.closest("[data-remove]")?.dataset.remove;
if (!email) return;
try {
await api(`/api/classes/${id}/students/${encodeURIComponent(email)}`, { method: "DELETE" });
await showClasses({ [id]: { lines: [`${esc(email)} הוסר/ה מהכיתה.`] } });
} catch (e) { await showClasses({ [id]: { error: e.message } }); }
});
});
}
// ---- the tests tab: set a test for a class, change or remove it, see who took the simulation ----
const today = () => new Date().toISOString().slice(0, 10);
/** A test covers lessons of one subject: a class with several subjects picks the subject first (a set test keeps its own). */
function testForm(c, test = null) {
const id = test ? `edit-${test.id}` : `new-${c.id}`;
const subjects = c.subjects || [{ id: "math", name: "מתמטיקה" }];
const subject = test ? test.subject_id || "math" : subjects[0].id;
const chosen = new Set(test ? test.lessons.map(l => l.id) : c.lessons.filter(l => (l.subject_id || "math") === subject).map(l => l.id));
const pick = !test && subjects.length > 1 ? `` : "";
return ``;
}
function testCard(c, t) {
const open = t.days_left > 0;
return `
מבחן ב־${shortDate(t.date)} (${daysText(t.days_left)})
${t.simulation.taken} מתוך ${t.simulation.total} עשו סימולציה
${t.lessons.map(l => fmt(l.name)).join(", ")}${t.note ? ` · ${esc(t.note)}` : ""} · ${t.questions} שאלות · ${t.minutes} דקות
${t.simulation.missing.length ? `
עוד לא עשו סימולציה: ${t.simulation.missing.map(e => `${esc(e)}`).join(", ")}