// 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("")}
` : `

עוד אין תלמידים בכיתה.

`}
${(notes[c.id]?.lines || []).map(l => `

${l}

`).join("")}
${notes[c.id]?.error ? `` : ""}

`).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 `
${pick}
השיעורים שהמבחן כולל${c.lessons.map(l => ``).join("")}

${test ? ` ` : ""}

`; } 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(", ")}

` : t.simulation.total ? `

כולם עשו את מבחן הסימולציה.

` : ""} ${t.students.length ? `
${t.students.map(s => ``).join("")}
תלמידציון צפוימוכנותמבחני תרגולסימולציות
${esc(s.email)}${s.forecast ? `${s.forecast.grade} (${s.forecast.low}–${s.forecast.high})` : "—"}${s.readiness.number} · ${esc(s.readiness.label)} ${s.readiness.practices ? `${s.readiness.practices} · הטוב ${s.readiness.practice_best}` : "—"}${s.readiness.simulations ? `${s.readiness.simulations} · הטוב ${s.readiness.simulation_best}` : "—"}
` : ""}

${open ? ` ` : ""}

`; } const showTests = () => swr("/api/teaching/tests", drawTests); async function drawTests(classes) { draw(`${tabs("tests")}

מבחנים

${classes.length ? classes.map(({ class: c, tests }) => `

${esc(c.name)}

${tests.length ? tests.map(t => testCard(c, t)).join("") : `

עוד לא נקבע מבחן לכיתה.

`}
קביעת מבחן${testForm(c)}
`).join("") : `

עוד לא שייכו אותך ככיתה. מנהל הכיתות קובע מי מלמד/ת כל כיתה.

`}
`); wireTabs(); const byId = new Map(classes.flatMap(({ class: c, tests }) => tests.map(t => [t.id, { c, t }]))); const root = document.getElementById("tests"); root.addEventListener("submit", async event => { const form = event.target.closest(".test-form"); if (!form) return; event.preventDefault(); const data = new FormData(form), error = form.querySelector(".form-error"); const body = { date: data.get("date"), lessons: data.getAll("lessons"), note: data.get("note") || "", questions: Number(data.get("questions")), minutes: Number(data.get("minutes")) }; if (!form.dataset.test) body.subject_id = form.dataset.subject; error.hidden = true; try { if (form.dataset.test) await api(`/api/tests/${form.dataset.test}`, { method: "PUT", body }); else await api(`/api/classes/${form.dataset.class}/tests`, { method: "POST", body }); await showTests(); } catch (e) { error.textContent = e.message; error.hidden = false; error.scrollIntoView({ block: "nearest" }); // the form keeps what was typed } }); root.addEventListener("change", event => { // another subject: its lessons, all ticked const pick = event.target.closest("[data-subject-pick]"); if (!pick) return; const form = pick.closest(".test-form"); form.dataset.subject = pick.value; form.querySelectorAll("[data-lesson-subject]").forEach(label => { const mine = label.dataset.lessonSubject === pick.value; label.hidden = !mine; label.querySelector("input").checked = mine; }); }); root.addEventListener("click", async event => { const edit = event.target.closest("[data-edit]")?.dataset.edit; if (edit) { const { c, t } = byId.get(Number(edit)); root.querySelector(`[data-slot="${edit}"]`).innerHTML = testForm(c, t); return; } if (event.target.closest("[data-cancel]")) { event.target.closest(".edit-slot").innerHTML = ""; return; } const remove = event.target.closest("[data-remove-test]"); if (remove) { // two steps, so a slip does not remove a test remove.outerHTML = ` `; return; } if (event.target.closest("[data-cancel-remove]")) { await showTests(); return; } const confirm = event.target.closest("[data-confirm-remove]")?.dataset.confirmRemove; if (confirm) { await api(`/api/tests/${confirm}`, { method: "DELETE" }); await showTests(); } }); } export async function show(me, view) { draw = view; await showClasses(); }