/* teach - leerling-omgeving: toont enkel de widgets die de leerkracht heeft klaargezet (via een toewijzing aan de klas of de leerling zelf), in speelmodus (readonly) en zonder de sleep/vergroot/sluit-chrome van het bord. Geen momentopname: /my/assignment zoekt live in het bord van de leerkracht, dus deze pagina pollt periodiek zodat nieuwe woorden vanzelf doorkomen. */ const PUPIL_WIDGET_CATS = ["taal", "rekenen"]; const pupilView = document.getElementById("pupilView"); let pupilPollTimer = null; let pupilLastSig = null; function pupilAllowedDefs(){ return REGISTRY.filter(d => PUPIL_WIDGET_CATS.includes(d.cat)); } function renderPupilWidgets(widgets){ pupilView.innerHTML = ""; const defs = pupilAllowedDefs(); const usable = widgets .map(w => ({ w, def: defs.find(d => d.id === w.id) })) .filter(x => x.def); if(!usable.length){ const hint = document.createElement("div"); hint.className = "pupil-empty"; hint.textContent = T("pupilEmpty"); pupilView.appendChild(hint); return; } usable.forEach(({ w, def }, idx)=>{ const card = document.createElement("div"); card.className = "widget pupil-card"; card.style.width = def.w + "px"; card.style.minHeight = def.h + "px"; const body = document.createElement("div"); body.className = "widget-body"; card.appendChild(body); pupilView.appendChild(card); /* geen stabiel widget-instantie-id in het bord (alleen het widget-type in w.id) - positie in de lijst is de fijnste identiteit die beschikbaar is, stabiel zolang de leerkracht de widgets op het bord niet herschikt */ const widgetId = def.id + ":" + idx; def.mount(body, w.state || null, { readonly:true, onProgress: (p)=>{ api("/my/progress", { method:"POST", body: { widgetId, widgetType: def.id, ...p } }).catch(()=>{}); }}); }); } async function pupilPoll(){ try{ const res = await api("/my/assignment"); const sig = JSON.stringify(res.widgets || []); if(sig !== pupilLastSig){ pupilLastSig = sig; renderPupilWidgets(res.widgets || []); } }catch(e){ /* stille mislukking - volgende poll probeert opnieuw */ } } function startPupilView(){ document.body.classList.add("pupil-mode"); pupilLastSig = null; pupilPoll(); clearInterval(pupilPollTimer); pupilPollTimer = setInterval(pupilPoll, 20000); } function stopPupilView(){ document.body.classList.remove("pupil-mode"); clearInterval(pupilPollTimer); pupilPollTimer = null; pupilView.innerHTML = ""; }