/* The Mathly path. The skill graph comes from /api/math/skills (app/math_skills.py). Every skill is open; the path walks them in order, and the first one not done yet is where the student goes on. A skill has one sample question or a list of questions. It is done once every one of its questions has been answered correctly and the last few answers (Sky.masterNeed) were right at the first try; after each answer the student moves to the next unsolved question, so a missed question comes back after the others. The choices are shuffled each time a question is shown, and a wrong answer does not reveal the right one, so a question that comes back is solved again, not remembered. Finished skills come back in a mixed review (see "spaced review" below). Progress is kept on the device per account (mathly.progress.v1:), and for a signed-in student also on the server so it follows them to other devices (see "progress across devices"): `done` per skill, and `solved` (indexes answered correctly), `at` (the question to show) and `set` (the round's questions, see round()) for skills with a list. Every tagged wrong answer is also kept in `slips` (see "misconception patterns" and math-sky.js), so the mistakes that keep coming back across skills can be shown with what to practise. */ (()=>{ const css=document.createElement("link"); css.rel="stylesheet"; css.href=document.currentScript.src.replace("math.js","math.css"); const cssReady=new Promise(done=>{ css.onload=css.onerror=done; }); // until then #mathView is not yet the scrolling box document.head.append(css); const CFG=window.MATHLY||{}, AUTH_URL=CFG.supabaseUrl||""; let ME_EMAIL="", MY_CLASSES=[], MY_CLASS=null, client=null, PREVIEW=false; // PREVIEW: a teacher looking at the app as a student const can=permission=>(CFG.permissions||[]).includes("*")||(CFG.permissions||[]).includes(permission); const api=(url,options={})=>fetch(url,{...options,headers:{...(options.headers||{}),...(CFG.token?{Authorization:`Bearer ${CFG.token}`}:{})}}); const esc=s=>String(s??"").replace(/[&<>"']/g,c=>({"&":"&","<":"<",">":">",'"':""","'":"'"}[c])); const fmt=s=>esc(s).replace(/\$([^$]+)\$/g,'$1'); // $…$ is math: set left to right const plain=s=>String(s).replace(/\$/g,""); // the model's text: as fmt, and a signed number it left outside $…$ is set left to right too, or its minus lands after it const fmtLoose=s=>String(s??"").split("$").map((part,i)=>i%2?`${esc(part)}` :esc(part).replace(/(^|[\s(־])([−-]\d+(?:[.,]\d+)?)/g,'$1$2')).join(""); const key=()=>"mathly.progress.v1:"+(ME_EMAIL||"guest"); const load=()=>{ try{ return JSON.parse(localStorage.getItem(key())||"{}"); }catch{ return {}; } }; const save=()=>{ P.updated=Date.now(); try{ localStorage.setItem(key(),JSON.stringify(P)); }catch{} queuePush(); }; /* Progress across devices: a signed-in student's progress is also kept on the server (app/progress.py). On sign-in the server's copy is merged into this device's; after each save the merged copy is sent back, and coming back to the tab merges again. mergeProgress keeps the work of both copies: finished skills, gold stars, solved questions, comets, mistakes and exams are joined; counts keep the higher one; where a student is now (the round, the question, a sitting, the run of first tries, spaced review) comes from the copy saved last (`updated`). */ const isObj=v=>!!v&&typeof v==="object"&&!Array.isArray(v); function mergeProgress(a,b){ a=isObj(a)?a:{}; b=isObj(b)?b:{}; const [old,neu]=(+a.updated||0)>(+b.updated||0)?[b,a]:[a,b], out={...old,...neu}; const each=(k,pick)=>{ const o=isObj(old[k])?old[k]:{}, n=isObj(neu[k])?neu[k]:{}, r={...o,...n}; if(pick) for(const id in o) if(id in n) r[id]=pick(o[id],n[id]); out[k]=r; }; const earliest=(x,y)=>String(x)String(x)>String(y)?x:y, most=(x,y)=>Math.max(+x||0,+y||0); const list=k=>[...(Array.isArray(old[k])?old[k]:[]),...(Array.isArray(neu[k])?neu[k]:[])]; const joined=(k,id)=>{ const by=new Map(); for(const x of list(k)) by.set(id(x),x); return [...by.values()]; }; for(const k of new Set([...Object.keys(old),...Object.keys(neu)])) if(isObj(old[k])&&isObj(neu[k])) each(k); // newer per key each("done",earliest); each("gold",earliest); each("seen",latest); each("tries",most); each("wrongs",most); each("days",most); each("examples",(x,y)=>({n:most(x?.n,y?.n),last:latest(x?.last||"",y?.last||"")})); // worked examples opened, per skill each("solved",(x,y)=>[...new Set([...(Array.isArray(x)?x:[]),...(Array.isArray(y)?y:[])])].sort((p,q)=>p-q)); out.comets=[...new Set(list("comets"))]; out.slips=joined("slips",s=>JSON.stringify([s?.at,s?.id,s?.q,s?.m])).sort((x,y)=>String(x?.at).localeCompare(String(y?.at))).slice(-500); if(old.quizzes||neu.quizzes) out.quizzes=joined("quizzes",q=>String(q?.at)).sort((x,y)=>String(x?.at).localeCompare(String(y?.at))).slice(-60); // QUIZ_KEEP if(old.cometDay||neu.cometDay) out.cometDay=latest(old.cometDay||"",neu.cometDay||""); out.updated=Math.max(+a.updated||0,+b.updated||0); return out; } const synced=()=>!!(AUTH_URL&&CFG.token); let pushTimer=null; async function pullProgress(){ if(!synced()) return null; try{ const r=await api("/api/math/progress"); return r.ok?(await r.json()).progress||{}:null; }catch{ return null; } } async function pushProgress(){ clearTimeout(pushTimer); pushTimer=null; const remote=await pullProgress(); if(remote===null) return; try{ await api("/api/math/progress",{method:"PUT",keepalive:true,headers:{"Content-Type":"application/json"}, body:JSON.stringify({progress:mergeProgress(remote,P)})}); }catch{} } function queuePush(){ if(!synced()) return; clearTimeout(pushTimer); pushTimer=setTimeout(pushProgress,1500); } document.addEventListener("visibilitychange",async()=>{ if(!synced()||!G) return; if(document.visibilityState==="hidden"){ if(pushTimer) pushProgress(); return; } if(cur) return; // a question or exam is open: take the other device's work next time loadComments(); // the teacher may have reviewed an answer meanwhile loadAssignments(); loadFinals(); const remote=await pullProgress(); if(!remote||cur) return; P=Sky.ensure(mergeProgress(P,remote)); try{ localStorage.setItem(key(),JSON.stringify(P)); }catch{} if(ACTIVE_SUBJECT==="מתמטיקה"&&!root?.hidden) draw(); }); let G=null, BY={}, POST={}, ORDER=[], P={done:{},tries:{},solved:{},at:{}}, root=null, curQ=0, path=null, sheet=null, cur=null, back=null, ACTIVE_SUBJECT=""; const ROUND_WRONG={}; // wrong answers per skill in its current round: from the second the worked example is offered let nav=null, FIRST=true, COMBO=0, REV=null, QZ=null, UQ=null; // UQ: the unit quiz on the sheet (see unit quiz below) // QZ: the practice exam in progress (see quiz below) // the tab bar; whether the shown question is still at its first try; first-try answers in a row let picker=null, PH=null, resPicker=null, RES=null; // RES: the open lesson-resources sheet (see openResources()) // the photo file picker, and the photographed problem on the sheet (see photo()) let advanceTimer=null; function stopPhone(){ window.MathlyAnswers?.closeQuestion(); } function cancelAdvance(){ clearTimeout(advanceTimer); advanceTimer=null; } function index(graph){ G=graph; BY=Object.fromEntries(G.skills.map(s=>[s.id,s])); POST=Object.fromEntries(G.skills.map(s=>[s.id,[]])); G.skills.forEach(s=>s.prereqs.forEach(p=>POST[p].push(s.id))); // the path's order: unit by unit, and in a unit the order of the file (the teaching schedule's), // except that a skill waits until the skills it needs in its unit are placed ORDER=G.units.flatMap(u=>{ const left=G.skills.filter(s=>s.unit===u.id), placed=new Set(), out=[]; while(left.length){ const i=left.findIndex(s=>s.prereqs.every(p=>placed.has(p)||BY[p].unit!==u.id)); const [s]=left.splice(i,1); placed.add(s.id); out.push(s.id); } return out; }); } // the units the student's class studies (its "lessons"); a teacher, or an account in no class, has every unit. // A skill of a unit the class does not study counts as known: it does not lock the skills that need it. const shownUnits=()=>{ const mine=typeof MY_CLASSES==="undefined"?[]:MY_CLASS?[MY_CLASS]:MY_CLASSES, ids=new Set(mine.flatMap(c=>c.lessons||[])); const units=G.units.filter(u=>ids.has(u.id)); return units.length?units:G.units; }; let SHOWN=new Set(); // a skill the server locked (an open-ended question while AI grading is off) is "off": shown, never asked, never blocking const status=id=>P.done[id]?"done":BY[id].locked?"off":"open"; const next=()=>ORDER.find(id=>SHOWN.has(BY[id].unit)&&status(id)==="open")||null; const QS=s=>s.questions||[s.sample]; const solvedOf=id=>new Set(P.solved[id]||[]); const shuffled=a=>{ const out=a.slice(); for(let i=out.length-1;i>0;i--){ const j=Math.floor(Math.random()*(i+1)); [out[i],out[j]]=[out[j],out[i]]; } return out; }; // a multiple-choice question's buttons, in a new order each time it is shown const choiceButtons=q=>`
${shuffled(q.choices||[]).map((c,i)=>``).join("")}
`; const figures=q=>`${q.graph?plot(q.graph):""}${q.diagram?geometryDiagram(q.diagram):""}${q.figure?figure(q.figure):""}`; /* A round: a skill with more than ROUND questions asks a random ROUND of them (indexes into its list), the ones not solved yet first. A round in progress keeps its questions (P.set); a finished skill gets a new one each time it is opened. Progress from before rounds existed was on the first ROUND questions, so it stays there. */ const ROUND=10; function round(id){ const n=QS(BY[id]).length; if(n<=ROUND) return [...Array(n).keys()]; let r=P.set[id]; if(!Array.isArray(r)||r.length!==ROUND||r.some(i=>!(Number.isInteger(i)&&i>=0&&iNumber.isInteger(i)&&i>=0&&i!got.has(i)),...all.filter(i=>got.has(i))].slice(0,ROUND); } P.set[id]=r; } return r; } const solvedIn=id=>{ const got=solvedOf(id); return round(id).filter(i=>got.has(i)).length; }; // the round's question after `from` that is not solved yet; once all are solved, simply the next one const nextQ=(id,from)=>{ const r=round(id), got=solvedOf(id), at=r.indexOf(from), n=r.length; for(let k=1;k<=n;k++){ const j=r[(at+k)%n]; if(!got.has(j)) return j; } return r[(at+1)%n]; }; async function show(){ document.documentElement.classList.add("math-on"); if(!root){ root=document.createElement("section"); root.id="mathView"; root.setAttribute("aria-label","מסלול המתמטיקה"); path=document.createElement("div"); path.className="mx-wrap"; sheet=document.createElement("div"); sheet.className="mx-layer"; sheet.hidden=true; // no capture attribute: the phone offers the camera and the gallery both picker=document.createElement("input"); picker.type="file"; picker.accept="image/*"; picker.hidden=true; picker.onchange=()=>{ const f=picker.files[0]; picker.value=""; if(f) photo(f); }; resPicker=document.createElement("input"); resPicker.type="file"; resPicker.accept="image/*"; resPicker.hidden=true; resPicker.setAttribute("capture","environment"); // a lesson resource is photographed: the phone opens its camera resPicker.onchange=()=>{ const f=resPicker.files[0]; resPicker.value=""; if(f) resourcePhoto(f); }; nav=document.createElement("nav"); nav.className="mx-nav"; nav.setAttribute("aria-label","ניווט"); nav.hidden=true; root.append(path,sheet,picker,resPicker,nav); document.body.append(root); window.MathlySky.mount(root); root.addEventListener("click",onClick); root.addEventListener("submit",e=>{ if(e.target.matches?.("[data-review-form]")){ e.preventDefault(); reviewSave(e.target); } else if(e.target.matches?.("[data-focus-form]")){ e.preventDefault(); focusSave(e.target); } else if(e.target.matches?.("[data-ws-return]")){ e.preventDefault(); wsReturn(e.target); } else if(e.target.matches?.("[data-exam-form]")){ e.preventDefault(); examSave(e.target); } else if(e.target.matches?.("[data-exam-add]")){ e.preventDefault(); examAdd(e.target); } else if(e.target.matches?.("[data-final-open],[data-final-grant]")){ e.preventDefault(); finalForm(e.target); } else if(e.target.matches?.("[data-budget-form]")){ e.preventDefault(); const v=e.target.elements.limit.value; aqSetting(e.target.dataset.budgetForm,{ai_daily_checks:v===""?null:+v},e.target.closest("section").querySelector("[role=status]")); } else if(e.target.matches?.("[data-asg-form]")){ e.preventDefault(); asgSave(e.target); } else if(e.target.matches?.("[data-quiz-note]")){ e.preventDefault(); quizNoteSave(e.target); } else if(e.target.matches?.("[data-ws-check]")){ e.preventDefault(); wsCheck(e.target); } }); root.addEventListener("change",e=>{ if(e.target.matches?.("[data-report-sort],[data-report-unit]")) reportControl(e.target); else if(e.target.matches?.("[data-ws-file]")) wsAddPhotos(e.target); else if(e.target.matches?.("[data-inbox-filter]")){ (REPORT.inboxFilter||={})[e.target.dataset.inboxFilter]=e.target.value; drawReport(); sheet.querySelector(`[data-inbox-filter="${e.target.dataset.inboxFilter}"]`)?.focus(); } else if(e.target.matches?.("[data-auto-send]")) aqSetting(e.target.dataset.autoSend,{auto_send_practice:e.target.checked},e.target.closest("section").querySelector("[role=status]")); }); root.addEventListener("input",e=>{ if(WS&&e.target.matches?.("[data-ws-note]")) WS.note=e.target.value; }); // kept across redraws document.addEventListener("keydown",e=>{ if(e.key==="Escape"&&helpOpen()) return helpClose(); if(e.key==="Escape"&&cur) QZ?.items?quizQuit():closeSheet(); }); // an exam asks before it is left } root.hidden=false; if(!G){ path.innerHTML='

טוענים את המסלול…

'; try{ const r=await fetch("/api/math/skills"); if(!r.ok) throw new Error(`השרת ענה ${r.status}`); index(await r.json()); } catch(e){ path.innerHTML=`

לא הצלחנו לטעון את המסלול: ${esc(e.message)}. רעננו את העמוד ונסו שוב.

`; return; } } const remote=await pullProgress(); P=window.MathlySky.ensure(Object.assign({done:{},tries:{},solved:{},at:{},set:{}},remote?mergeProgress(load(),remote):load())); if(remote){ try{ localStorage.setItem(key(),JSON.stringify(P)); }catch{} queuePush(); } // this device's own work goes up too if(!P.set||typeof P.set!=="object") P.set={}; SHOWN=new Set(shownUnits().map(u=>u.id)); hasExams()?drawSubjectPicker():selectSubject("מתמטיקה"); // no exams yet: straight to math, science is a tap away if(CFG.aiReview) aqCount(); if(teachClasses().length) inboxCount(); loadComments(); loadAssignments(); loadFinals(); window.MathlyAnswers?.configure({api,account:CFG.userId,enabled:CFG.answerScans,root,queued:CFG.aiDirect===false,onGraded:({questionId,grading})=>{ if(grading.result.verdict!=="correct"||P.done[questionId])return; const unitWas=unitDone(unitOf(questionId)); P.done[questionId]=grading.finished_at||new Date().toISOString(); Sky.mastered(P,questionId); P.tries[questionId]=(P.tries[questionId]||0)+1; const r=Sky.correct(P,questionId,{first:true,n:QS(BY[questionId]).length,finished:true}); save();draw();rewards(questionId,{r,lit:true,unitWas}); }}); } function hide(){ cancelAdvance(); if(root) root.hidden=true; document.documentElement.classList.remove("math-on"); } // subjects with a path of their own: always offered, even before the class has an exam in them const PATH_SUBJECTS=["מתמטיקה","מדעים"]; // the classes whose exams a screen shows: a student's own. A teacher sees no exam schedule at all (it is edited under ניהול) const examClasses=()=>!PREVIEW&&(can("students.view")||teachClasses().length)?[]:MY_CLASS?[MY_CLASS]:MY_CLASSES; const hasExams=()=>examClasses().some(row=>(row.exams||[]).some(exam=>exam?.subject&&exam?.date)); function subjectOptions(){ const bySubject=new Map(PATH_SUBJECTS.map(name=>[name,{name,dates:[],classes:new Set()}])); examClasses().forEach(row=>(row.exams||[]).forEach(exam=>{ if(!exam?.subject||!exam?.date)return; const item=bySubject.get(exam.subject)||{name:exam.subject,dates:[],classes:new Set()}; item.dates.push(exam.date);item.classes.add(row.name);bySubject.set(exam.subject,item); })); const today=new Date();today.setHours(0,0,0,0); // first the subjects with an exam ahead (nearest first), then those with no exams yet, then those whose exams are over return [...bySubject.values()].map(item=>{ const dates=item.dates.sort(), left=dates.filter(date=>examDate(date)>=today); return {...item,dates,left,rank:left.length?0:dates.length?2:1}; }).sort((a,b)=>a.rank-b.rank||String(a.left[0]||"").localeCompare(String(b.left[0]||""))); } // the stars lit in a subject's units out of all its skills; null when the subject has no practice path here function subjectSkills(name){ const ids=mineUnits().filter(u=>u.subject===name).flatMap(u=>unitIds(u).filter(live)); return ids.length?{lit:ids.filter(id=>P.done[id]).length,total:ids.length}:null; } function subjectCard(item,today){ const [icon,tone]=examLooks[item.name]||["●","other"], count=item.dates.length, left=item.left.length, over=count>0&&!left; const days=left?Math.round((examDate(item.left[0])-today)/86400000):null, skills=subjectSkills(item.name); const dots=count&&count<=12?``).join("")}`:""; const exams=count?(over?"כל המבחנים הסתיימו":`נשארו ${left} מתוך ${count} מבחנים`):item.name==="מדעים"?"חידות בהצבעה כיתתית":"מסלול תרגול"; const bar=skills?`${skills.lit} מתוך ${skills.total} מיומנויות`:""; const when=days===null?"":`${days===0?"היום":days===1?"מחר":`בעוד${days}ימים`}`; return ``; } function drawSubjectPicker(){ ACTIVE_SUBJECT=""; nav.hidden=true; const manage=can("management.access"), subjects=subjectOptions(), today=new Date();today.setHours(0,0,0,0); path.innerHTML=`${previewBar()}

המקצועות שלי

בחרו מקצוע כדי לפתוח את המסלול שלו

${window.MathlyGuide?.button()||""}${teachButton()}${manage?'':""}${window.MathlyAnswers?.pushButton?.()||""}${AUTH_URL?'':""}
${classesCard()}
${subjects.map(item=>subjectCard(item,today)).join("")}
`; } function selectSubject(subject){ ACTIVE_SUBJECT=subject; if(subject==="מתמטיקה")return setView(VIEW); nav.hidden=true; if(subject==="מדעים"&&window.MathlyScience)return window.MathlyScience.show(path); const [icon,tone]=examLooks[subject]||["●","other"]; path.innerHTML=`

${esc(subject)}

המקצועות שלי

המסלול של ${esc(subject)} עדיין לא זמין

לוח המבחנים של המקצוע כבר מחובר לכיתה. כשהמסלול יתווסף, הוא ייפתח מכאן.

`; } /* The math screens: home (what you gathered and your streak on top, the path below it) and the collection. The rules of the stars, the streak and the comets, and their pictures, are in math-sky.js. */ const Sky=window.MathlySky; let VIEW="home"; // the tab shown: home (what has a date or needs action), path (self-paced learning), sky (the collection) const SOUND_ON=''; const SOUND_OFF=''; const TABS=[["home","בית",''], ["path","מסלול",''], ["grades","ציונים",''], ["sky","האוסף",``]]; // the names of tests, one place. A unit (lesson) has two quizzes: בוחן חיזוק (practice: retaken, its best grade // shown, not the unit's grade) and בוחן מסכם (final: once, the unit's grade). Practice over several units is a // מבחן תרגול; a מבחן is the school's exam on the class's exam table. const TERMS={practice:"בוחן חיזוק",final:"בוחן מסכם",mixed:"מבחן תרגול",exam:"מבחן",unitQuizzes:"בחני היחידה"}; const STATE_NAME={gold:"כוכב זהב",lit:"הושלמה",fading:"הושלמה, הכוכב דועך",next:"הכוכב הבא",open:"פתוחה",off:"סגורה כרגע"}; const mineUnits=()=>G.units.filter(u=>SHOWN.has(u.id)); const unitOf=id=>G.units.find(u=>u.id===BY[id].unit); const unitIds=u=>ORDER.filter(id=>BY[id].unit===u.id); const live=id=>!BY[id].locked; const seenAt=id=>Date.parse(P.seen?.[id]||P.done[id]||0)||0; const goldNeed=id=>Sky.goldNeed(Math.min(ROUND,QS(BY[id]).length)); const inDays=d=>d===0?"היום":d===1?"מחר":`בעוד ${d} ימים`; function unitDone(u){ const ids=unitIds(u).filter(live); return ids.length>0&&ids.every(id=>P.done[id]); } function skyStats(){ let lit=0, gold=0, total=0, cons=0; const units=mineUnits(); for(const u of units){ const ids=unitIds(u).filter(live); total+=ids.length; for(const id of ids) if(P.done[id]){ lit++; if(P.gold[id]) gold++; } if(unitDone(u)) cons++; } const fading=ORDER.filter(id=>SHOWN.has(BY[id].unit)&&Sky.fading(P,id)).sort((a,b)=>seenAt(a)-seenAt(b)); return {lit,gold,total,cons,units:units.length,fading}; } // the class's next exam in a subject of its units function nextExam(){ const today=new Date(); today.setHours(0,0,0,0); const exams=examClasses().flatMap(row=>{ const subjects=classSubjects(row); return (row.exams||[]).filter(e=>e?.date&&subjects.includes(e.subject)); }) .filter(e=>examDate(e.date)>=today).sort((a,b)=>a.date.localeCompare(b.date)); return exams.length?{...exams[0],days:Math.round((examDate(exams[0].date)-today)/86400000)}:null; } function topBar(){ const st=skyStats(), hot=Sky.today(P)>=Sky.GOAL, manage=can("management.access"), on=Sky.soundOn(), streak=Sky.streak(P); return `
${Sky.spark()}מתמטיקה
${window.MathlyGuide?.button()||""}${teachButton()}${manage?'':""}${window.MathlyAnswers?.pushButton?.()||""}${AUTH_URL?'':""}
${Sky.spark()}${st.lit}כוכבים ${Sky.spark()}${st.gold}כוכבי זהב ${P.comets.length}שביטים
`; } function counts(st){ return `
${st.lit}מתוך ${st.total} כוכבים
${st.gold}כוכבי זהב
${st.cons}קבוצות כוכבים
${P.comets.length}שביטים
`; } /* a unit's constellation as one picture: its outline (whole once the unit is done) and its stars */ function conSvg(u,animate=false){ const ids=unitIds(u), shp=Sky.shape(u.id,ids.length,u.name), nxt=next(), full=unitDone(u); const lines=[...shp.lines,...(full?shp.inner:[])].map(([a,b],k)=>``).join(""); const stars=ids.map((id,i)=>{ const st=Sky.star(P,id,status(id),id===nxt), [x,y]=shp.stars[i], r=st==="gold"?18:["lit","fading","next"].includes(st)?13:7; return ``; }).join(""); return ``; } const pips=(u,fresh)=>``; // today's goal: right answers so far; opens the streak calendar function daily(){ const n=Sky.today(P), goal=Sky.GOAL, done=n>=goal, streak=Sky.streak(P); return ``; } /* The streak calendar, a month at a time: days whose goal was met are joined in one band, a forgiven day sits in the band in ice, practice short of the goal is a small dot. */ let CAL=null; // the month shown: {y,m} const MONTH=new Intl.DateTimeFormat("he-IL",{month:"long",year:"numeric"}); function showStreak(step=0){ const now=new Date(); if(!step||!CAL) CAL={y:now.getFullYear(),m:now.getMonth()}; else{ const d=new Date(CAL.y,CAL.m+step,1); CAL={y:d.getFullYear(),m:d.getMonth()}; } const keys=Object.keys(P.days||{}).sort(), first=keys[0]?keys[0].slice(0,7):Sky.dayKey(now).slice(0,7); const ym=`${CAL.y}-${String(CAL.m+1).padStart(2,"0")}`, atStart=ym<=first, atNow=ym>=Sky.dayKey(now).slice(0,7); const streak=Sky.streak(P), best=Math.max(Sky.best(P),streak), n=Sky.today(P), goal=Sky.GOAL; const cells=Sky.month(P,CAL.y,CAL.m,now), on=c=>c&&(c.state==="met"||c.state==="saved"); const metHere=cells.filter(c=>c?.state==="met").length; const grid=cells.map((c,i)=>{ if(!c) return ''; const col=i%7, prev=col>0&&on(cells[i-1]), nxt=col<6&&on(cells[i+1]); const cls=["mx-cday",c.state&&`s-${c.state}`,on(c)&&prev&&"jp",on(c)&&nxt&&"jn",c.today&&"today",c.future&&"future"].filter(Boolean).join(" "); const say=c.state==="met"?"היעד הושלם":c.state==="saved"?"יום שנסלח":c.state==="some"?"תרגול בלי יעד":""; return `${c.day}${say?`${say}`:""}`; }).join(""); if(!cur) back=document.activeElement; cur="streak"; sheet.innerHTML=`
`; sheet.hidden=false; sheet.querySelector(step>0?"[data-act=cal-next]":step<0?"[data-act=cal-prev]":".mx-x").focus({preventScroll:true}); } /* The path: unit after unit, top to bottom. Each unit is a hub in the middle with its skills orbiting it in order: skill 1 sits at the top and the rest follow clockwise, each numbered, with arrows on the ring between them. A stretch of the ring is lit once the skill it leaves from is done, so the lit ring leads to where to go next. The unit quiz follows the orbit. */ const ORBIT_R=34; // the ring's radius, in % of the orbit's width const isOn=st=>st==="lit"||st==="gold"||st==="fading"; function orbitRing(sts){ const n=sts.length, a=i=>(-90+360*i/n)*Math.PI/180, pt=(t,r=ORBIT_R)=>[50+r*Math.cos(t),50+r*Math.sin(t)], f=v=>v.toFixed(2); const gap=n>1?Math.min(.32,Math.PI/n*.55):0; // room left around each node (radians) const arcs=sts.slice(0,-1).map((st,k)=>{ const t0=a(k)+gap, t1=a(k+1)-gap, [x0,y0]=pt(t0), [x1,y1]=pt(t1), tm=(a(k)+a(k+1))/2, [mx,my]=pt(tm), deg=tm*180/Math.PI+90; return ` `; }).join(""); // the closing stretch, from the last skill back to the first, stays faint: the orbit has a start and an end const back=n>2?(()=>{ const [x0,y0]=pt(a(n-1)+gap), [x1,y1]=pt(a(n)-gap); return ``; })():""; return ``; } /* ---------- home ---------- Only what has a date or needs the student: the next exam, the quizzes, work that is due and new teacher comments. It always ends with a button that continues the path, so it is never empty. */ function homeView(){ if(teacherHome()) return `${topBar()}${classesCard()}`; const exam=examItem(), quizzes=quizCards(), due=tasksSummary()+assignmentsCard(), comments=commentsCard(); const owed=CFG.userId&&(ASG||FIN)?myTasks().some(x=>!["handed","waiting","checked"].includes(x.state)):false; const empty=!exam&&!quizzes&&!owed&&!asgView()?.open.length&&!comments; const nxt=next(); return `${previewBar()}${topBar()}
${exam}${examLessons()}${quizzes}${due}${comments} ${empty?'

אין כרגע משהו להגיש או מבחן קרוב.

':""}
`; } // the lessons (units) of the next exam: the ones still to finish stand out, and each opens on the path function examLessons(){ const exam=nextExam(); if(!exam) return ""; const prep=prepExams().find(e=>e.date===exam.date&&e.subject===exam.subject); if(!prep) return ""; const units=G.units.filter(u=>prep.units.includes(u.id)).map(u=>{ const ids=unitIds(u).filter(live), d=ids.filter(id=>P.done?.[id]).length; return {u,d,n:ids.length,full:ids.length>0&&d===ids.length}; }); const left=units.filter(x=>!x.full); return `

${left.length?`להשלים לפני המבחן: ${left.length===1?"שיעור אחד":`${left.length} שיעורים`}`:"כל השיעורים של המבחן הושלמו ✓"}

    ${[...left,...units.filter(x=>x.full)].map(({u,d,n,full})=>`
  • `).join("")}
`; } // the next exam on the class's exam table: subject, date and days left; it opens its preparation when it names units function examItem(){ const exam=nextExam(); if(!exam) return ""; const prep=prepExams().find(e=>e.date===exam.date&&e.subject===exam.subject); const when=`${examDate(exam.date).toLocaleDateString("he-IL",{day:"numeric",month:"numeric"})} · ${exam.days?`עוד ${exam.days===1?"יום אחד":`${exam.days} ימים`}`:"היום"}`; return prep?`` :`
מבחן ב${esc(exam.subject)} · ${when}
`; } // a class's teacher (not in student preview) gets the classes, not a learning path const teacherHome=()=>!PREVIEW&&teachClasses().length>0; function pathView(){ if(teacherHome()) return homeView(); const nxt=next(), cls=MY_CLASS?` · כיתת ${esc(MY_CLASS.name)}`:"", teach=can("students.view")&&!PREVIEW, marks=asgMarks(); const fix=new Set(myPatterns().recurring.map(x=>x.practise)); // skills that a recurring mistake sends back to const units=mineUnits().map(u=>{ const ids=unitIds(u), act=ids.filter(live), lit=act.filter(id=>P.done[id]).length, n=ids.length, full=unitDone(u); const sts=ids.map(id=>Sky.star(P,id,status(id),id===nxt)); const steps=ids.map((id,i)=>{ const st=sts[i], tag=st==="next"?"ממשיכים כאן":fix.has(id)?"לחזק":st==="fading"?"דועך":"", given=marks.get(id); const t=(-90+360*i/n)*Math.PI/180, dx=Math.cos(t), dy=Math.sin(t), up=dy<-.3; return `
${fmt(BY[id].short)}${tag?`${tag}`:""}
`; }).join(""); const res=teach?` data-act="resources" data-unit="${u.id}"`:""; // a teacher opens the lesson's resources from its title const pct=act.length?Math.round(100*lit/act.length):0; return `
${orbitRing(sts)}

${esc(u.name)}

${lit}/${act.length}${u.when?`${esc(u.when)}`:""}
${steps}
${teach?``:""}
${quizStep(u)}`; }).join(""); return `${previewBar()}${topBar()}${daily()}${reviewCard()}${patternsCard()}

המסלול שלי

אלגברה וגאומטריה כיתה ט׳ · 5 יח״ל${cls}

${Sky.owl(40)}התחלה
${units}

כל השיעורים פתוחים. בכל יחידה המיומנויות מסודרות סביבה לפי הסדר, מ־1 למעלה ועם כיוון השעון. כוכב נדלק כשעונים נכון על כל השאלות של המיומנות, והופך לזהב אחרי ${3} תשובות נכונות ברצף (או כל השאלות, כשיש פחות). כוכב שלא תרגלו ${Sky.FADE_DAYS} יום דועך, ותשובה נכונה אחת מחזירה לו את האור. ההתקדמות נשמרת במכשיר הזה.

`; } function collectionView(){ const st=skyStats(); return `${topBar()}

האוסף שלי

כל מיומנות היא כוכב. כל יחידה שמסיימים מציירת קבוצת כוכבים.

${counts(st)}

קבוצות כוכבים

${st.cons} מתוך ${st.units}
${mineUnits().map(u=>{ const ids=unitIds(u).filter(live), lit=ids.filter(id=>P.done[id]).length, full=unitDone(u); return ``; }).join("")}

שביטים נדירים

${P.comets.length} מתוך ${Sky.COMETS.length}

שביטים חולפים לפעמים על המסך בזמן תרגול. מי שלוחץ עליהם בזמן תופס אותם.

${Sky.COMETS.map(c=>{ const have=P.comets.includes(c.id); return ``; }).join("")}
${examClasses().filter(row=>(row.exams||[]).length).map(row=>examBoard(row,true)).join("")}`; } function draw(){ Sky.ensure(P); if(VIEW==="path"&&teacherHome()) VIEW="home"; if(VIEW==="grades"&&(teacherHome()||!CFG.userId)) VIEW="home"; // grades: a signed-in student's path.innerHTML=VIEW==="sky"?collectionView():VIEW==="home"?homeView():VIEW==="grades"?gradesView():pathView(); nav.hidden=false; const tabs=TABS.filter(([v])=>!(v==="path"&&teacherHome())&&!(v==="grades"&&(teacherHome()||!CFG.userId))); nav.innerHTML=`
${tabs.map(([v,label,icon])=>``).join("")}
`; decorateUnits(); } function setView(view,unit){ if(view==="grades") GRADES=null; // the tab reads the server's grades again each time it is opened VIEW=view; draw(); root.scrollTop=0; const target=view!=="path"?null:unit?root.querySelector(`#mx-unit-${CSS.escape(unit)}`):root.querySelector(".mx-star.st-next")||root.querySelector(".mx-star.st-open"); if(!target) return; target.scrollIntoView({block:unit?"start":"center"}); cssReady.then(()=>{ if(VIEW===view&&target.isConnected) target.scrollIntoView({block:unit?"start":"center"}); }); } function showComet(cid){ const c=Sky.COMETS.find(x=>x.id===cid), have=P.comets.includes(cid); if(!c) return; if(!cur) back=document.activeElement; cur="comet"; sheet.innerHTML=`
`; sheet.hidden=false; sheet.querySelector(".mx-x").focus({preventScroll:true}); } function launchComet(){ Sky.comet(()=>{ cancelAdvance(); const c=Sky.catchComet(P); save(); draw(); if(c) Sky.celebrate({kind:"gold",title:"תפסתם שביט נדיר!",sub:esc(c.fact),art:Sky.cometCard(c)}); }); } /* After a correct answer in `id`: the celebrations it earned, one after another, then a comet perhaps. `r`: what Sky.correct changed; `lit`: the skill was finished just now; `unitWas`: its unit was complete before. Resolves when the student has seen them all (at once when there was nothing to celebrate). */ function rewards(id,{r,lit,unitWas}){ const u=unitOf(id), steps=[], name=Sky.shape(u.id,1,u.name).name; if(r.gold) steps.push(()=>Sky.celebrate({kind:"gold",title:"כוכב זהב!",sub:`${fmt(BY[id].short)} · ${goldNeed(id)===1?"נכון בניסיון הראשון":`${goldNeed(id)} נכונות ברצף`}`,extra:pips(u,id)})); else if(lit) steps.push(()=>Sky.celebrate({kind:"lit",title:"כוכב חדש!",sub:fmt(BY[id].short),extra:pips(u,id)})); if(unitDone(u)&&!unitWas) steps.push(()=>Sky.celebrate({kind:"con",title:`קבוצת ${name} הושלמה!`,sub:`כל הכוכבים של ${esc(u.name)} דולקים.`,art:conSvg(u,true)})); if(r.revived&&!steps.length) Sky.toast(`${fmt(BY[id].short)} זורח שוב`); if(r.goal) steps.push(async()=>{ Sky.sound.goal(); Sky.toast(`יעד יומי הושלם · ${Sky.streak(P)} ימים ברצף`); }); if(Sky.cometDue(P,{gold:r.gold})){ P.cometDay=Sky.dayKey(); save(); steps.push(()=>new Promise(res=>setTimeout(()=>{ launchComet(); res(); },600))); } return steps.reduce((p,step)=>p.then(step),Promise.resolve()).then(()=>{ if(steps.length) draw(); }); } function chips(ids){ return ids.length?ids.map(id=>``).join(""):'אין'; } /* ---------- the teacher's assignments (app/assignments.py) ---------- GET /api/math/assignments gives the student's classes' assignments and when they handed in each unit quiz. Whether an item is done is worked out on the page (Sky.assignmentState) from the progress and those sittings, so the card and the teacher's report always agree. A card on the home screen lists the open ones, and the done ones for two weeks. */ let ASG=null; async function loadAssignments(){ if(!CFG.userId||!MY_CLASSES.length) return; try{ const r=await api("/api/math/assignments",{cache:"no-store"}); ASG=r.ok?await r.json():null; }catch{ ASG=null; } if(ACTIVE_SUBJECT==="מתמטיקה"&&root&&!root.hidden&&!cur) draw(); // not over the subject picker } function asgView(){ if(!ASG?.assignments?.length) return null; const now=new Date(), cutoff=Sky.jerusalemDay(now-14*864e5); const rows=ASG.assignments.map(a=>({a,s:Sky.assignmentState(a,P,{sittings:ASG.sittings,submissions:ASG.submissions},now)})); return {open:rows.filter(x=>!x.s.finished).sort((p,q)=>p.a.due_on.localeCompare(q.a.due_on)), done:rows.filter(x=>x.s.finished&&x.a.due_on>=cutoff).sort((p,q)=>q.a.due_on.localeCompare(p.a.due_on))}; } const asgDate=due=>new Date(due+"T12:00:00").toLocaleDateString("he-IL",{day:"numeric",month:"numeric"}); const asgUnit=id=>esc(G.units.find(u=>u.id===id)?.name||id); function asgItemLabel(item){ if(item.kind==="skill") return BY[item.id]?fmt(BY[item.id].name):esc(item.id); if(item.kind==="unit_quiz") return `${TERMS.practice}: ${asgUnit(item.unit)}`; if(item.kind==="worksheet") return `שיעורי בית: ${esc(wsTitle(item.id))}`; return `${TERMS.mixed}: ${(item.units||[]).map(asgUnit).join(", ")}`; } // the skills of unfinished assignments, with the titles, for the marker on the path function asgMarks(){ const marks=new Map(); for(const {a} of asgView()?.open||[]) for(const item of a.items) if(item.kind==="skill") marks.set(item.id,[...(marks.get(item.id)||[]),a.title]); return marks; } // the time left to hand in, by Jerusalem's days; the last day counts the hours to midnight function asgLeft(due,now=new Date()){ const day=Sky.jerusalemDay(now), d=Math.round((Date.parse(due+"T00:00:00Z")-Date.parse(day+"T00:00:00Z"))/864e5); if(d<0) return {late:true,text:d===-1?"באיחור של יום":`באיחור של ${-d} ימים`}; if(d===0){ const hour=+new Intl.DateTimeFormat("en-GB",{timeZone:"Asia/Jerusalem",hour:"numeric",hourCycle:"h23"}).format(now), left=24-hour; return {late:false,soon:true,text:left<=1?"היום, נשארה פחות משעה":`היום, נשארו כ־${left} שעות`}; } return {late:false,soon:d<=1,text:d===1?"מחר, נשאר יום אחד":d>60?"":`נשארו ${d} ימים`}; // far away: just the date } function assignmentsCard(){ const v=asgView(); if(!v||(!v.open.length&&!v.done.length)) return ""; const several=new Set(ASG.assignments.map(a=>a.class_id)).size>1, left=a=>asgLeft(a.due_on); const open=v.open.map(({a,s})=>`
${esc(a.title)}${a.personal?' 🎯 אישי':""}${s.overdue?"באיחור · ":""}${left(a).text?`⏳ ${left(a).text} · `:""}עד ${asgDate(a.due_on)}
${several?`${esc(a.class_name)}`:""}${a.note?`

${esc(a.note)}

`:""}

${s.doneCount} מתוך ${s.total} הושלמו

    ${s.items.map(x=>`
  • ${asgItemLabel(x.item)}${x.done?' (הושלם)':""}${x.known?"":" (לא ניתן לבדוק כרגע)"}${x.item.kind==="worksheet"?asgSheetHtml(x):""}
  • `).join("")}
`).join(""); const done=v.done.length?`
הושלמו (${v.done.length})
    ${v.done.map(({a,s})=> `
  • ✓ ${esc(a.title)} עד ${asgDate(a.due_on)}${s.late?" · הוגשה באיחור":""}${s.items.filter(x=>x.item.kind==="worksheet").map(x=>`
    ${asgItemLabel(x.item)}${asgSheetHtml(x)}
    `).join("")}
  • `).join("")}
`:""; return `

משימות מהמורה

${open}${done}
`; } // a worksheet item on the card: handed in or checked (with the teacher's score and comment), and the button that opens it function asgSheetHtml(x){ const sub=x.submission, checked=sub?.checked_at&&sub.state!=="returned"; if(sub?.state==="returned") return ` ↩ הוחזר לתיקון ${sub.return_comment?`

המורה: ${esc(sub.return_comment)}

`:""}`; const badge=checked?` נבדק${sub.score!=null?` · ${sub.score}`:""}`:x.done?' הוגש':""; return `${badge} ${checked&&sub.comment?`

המורה: ${esc(sub.comment)}

`:""}`; } /* ---------- the student's task list (GET /api/math/my-tasks) ---------- One list of what the student owes: open assignments (the class's and focus practice given to them), final quizzes open to them, and work the teacher returned for a fix. Each has one state (Sky.taskState), nearest due first. */ function myTasks(){ const out=[], today=Sky.jerusalemDay(new Date()), server={sittings:ASG?.sittings,submissions:ASG?.submissions}; for(const a of ASG?.assignments||[]){ const t=Sky.assignmentTask(a,P,server); // done and nothing left to hear back about: off the list once its date has passed if(t.st.finished&&!["waiting","returned"].includes(t.state)&&a.due_on!x.done&&x.due>d?x.due:d,a.due_on); const back=t.st.items.find(x=>x.returned); out.push({key:`a:${a.id}`,title:a.title,kind:a.personal?"🎯 תרגול אישי":"משימה",due,state:t.state,label:t.label, sub:back?`↩ ${esc(back.submission.return_comment||"")}`:`${t.st.doneCount} מתוך ${t.st.total} הושלמו`, go:back?`data-ws-open="${esc(back.item.id)}"`:`data-asg-go="${esc(a.id)}"`}); } for(const o of FIN?.offers||[]){ const t=Sky.finalTask(o), unit=G.units.find(u=>u.id===o.window.unit_id); out.push({key:`f:${o.window.id}:${o.grant?.id||""}`,title:`${TERMS.final}: ${unit?.name||o.window.unit_id}`,kind:`📌 ${TERMS.final}`, due:Sky.jerusalemDay(Date.parse(o.until)),state:t.state,label:t.label,sub:`פתוח עד ${new Date(Date.parse(o.until)).toLocaleString("he-IL",{dateStyle:"short",timeStyle:"short"})}`, go:`data-uquiz="${esc(o.window.unit_id)}"`}); } return out.sort((p,q)=>(p.due||"9999").localeCompare(q.due||"9999")); } function tasksSummary(){ if(!CFG.userId||!MY_CLASSES.length) return ""; const grades=""; // the grades have their own tab if(!ASG&&!FIN) return grades; const list=myTasks(), open=list.filter(x=>!["handed","waiting","checked"].includes(x.state)), next=open[0]; return `${grades}`; } let TASKS=null; async function openTasks(){ if(!cur) back=document.activeElement; closeSheetQuietly(); cur="tasks"; TASKS={loading:true}; drawTasks(); try{ const r=await api("/api/math/my-tasks",{cache:"no-store"}); const d=r.ok?await r.json():null; if(cur!=="tasks") return; if(d){ if(d.assignments) ASG=d.assignments; if(d.finals){ FIN={...(FIN||{skew:0}),offers:d.finals}; } } TASKS={error:!d}; }catch{ if(cur==="tasks") TASKS={error:true}; } if(cur==="tasks") drawTasks(); } function drawTasks(){ const list=TASKS?.loading?null:myTasks(); const rows=list?.map(x=>`
  • ${esc(x.title)} ${x.kind}

    ${x.sub}${x.due?` · ${(asgLeft(x.due).text||`עד ${asgDate(x.due)}`)}`:""}

    ${x.label}
  • `).join(""); sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-x").focus({preventScroll:true}); } /* ---------- the student's grades (GET /api/math/my-grades) ---------- Every grade the server holds, by unit: the official grade of the final quiz (once the teacher released it), practice quizzes (best and latest, not counting), and checked worksheets; what still waits for a check; and the teacher's comments not opened yet, first. Each grade opens its result. */ let GRADES=null; // the grades tab (and every link to the grades) shows the page; it loads when it has nothing function openGrades(){ closeSheet(); setView("grades"); } async function loadGrades(){ GRADES={loading:true}; try{ const r=await api("/api/math/my-grades",{cache:"no-store"}); GRADES=r.ok?await r.json():{error:true}; }catch{ GRADES={error:true}; } if(VIEW==="grades"&&ACTIVE_SUBJECT==="מתמטיקה"&&root&&!root.hidden) draw(); } function gradesView(){ if(!GRADES) loadGrades(); return `${previewBar()}${topBar()}

    הציונים שלי

    כל הציונים כאן מהשרת ·

    ${gradesBody(GRADES||{loading:true})}
    `; } const gradeOpen=v=>v.open===1?"ממתין לבדיקה · שאלה אחת":`ממתין לבדיקה · ${v.open} שאלות`; const gradeDay=t=>{ const d=new Date(t); return isNaN(d)?"":d.toLocaleDateString("he-IL",{day:"numeric",month:"numeric"}); }; /* The grades as one list: what is still to hand in first (nearest date first), then everything handed in, newest first. Each row: when, what, the result, and what can be done now (hand in, hand in again, open). */ function gradeRows(g){ const rows=[], unit=id=>G.units.find(u=>u.id===id)?.name||id; for(const o of FIN?.offers||[]) if(!o.start?.sitting_id) rows.push({todo:true,at:o.until,kind:TERMS.final,what:unit(o.window.unit_id),result:o.start?"בתהליך":"פתוח להגשה", act:``}); const handed=new Set((g.worksheets||[]).map(w=>w.worksheet_id)); for(const a of ASG?.assignments||[]) for(const item of a.items) if(item.kind==="worksheet"&&!handed.has(item.id)&&!(ASG.submissions||[]).some(x=>x.worksheet_id===item.id)) rows.push({todo:true,at:a.due_on+"T12:00:00",kind:"שיעורי בית",what:wsTitle(item.id),result:`עד ${asgDate(a.due_on)}`, act:``}); for(const u of g.units||[]) for(const v of u.sittings||[]){ const final=v.mode==="final", mark=final&&v.id===u.official?.id?' ★ ציון היחידה':!final&&v.id===u.practice_best?.id&&v.grade!=null?' הכי טוב':""; const result=final&&v.released===false?"ממתין לפרסום":v.grade==null?gradeOpen(v):`${v.grade}/100`; const note=(g.comments||[]).some(c=>v.attempts?.includes(c.attempt_id))?' 💬':""; rows.push({at:v.submitted_at,kind:final?TERMS.final:TERMS.practice,what:u.name, result:`${mark}${note}`, act:final?"":``}); } for(const w of g.worksheets||[]){ const back=w.state==="returned"; rows.push({at:w.submitted_at||w.checked_at||"",kind:"שיעורי בית",what:w.title||w.assignment, result:back?"↩ הוחזר לתיקון":w.checked_at?`${w.score??"נבדק"}${w.score!=null?"/100":""}${w.comment?' 💬':""}`:"ממתין לבדיקה", act:``}); } const todo=rows.filter(r=>r.todo).sort((a,b)=>String(a.at).localeCompare(String(b.at))); return [...todo,...rows.filter(r=>!r.todo).sort((a,b)=>(!a.at-!b.at)||String(b.at||"").localeCompare(String(a.at||"")))]; // no date: last } function gradesBody(g){ if(g?.loading) return '

    טוענים…

    '; if(g?.error) return '

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

    '; const rows=gradeRows(g), official=(g.units||[]).filter(u=>u.official&&u.official.released!==false&&u.official.grade!=null); const comments=g.comments||[]; return `${official.length?`

    ציוני היחידות: ${official.map(u=>`${esc(u.name)} ${u.official.grade}`).join(" · ")}

    `:""} ${comments.length?`

    💬 ${comments.length===1?"הערה חדשה מהמורה":`${comments.length} הערות חדשות מהמורה`}: ${comments.map(c=>``).join(" · ")}

    `:""} ${rows.length?`
      ${rows.map(r=>`
    • ${esc(r.kind)}${esc(r.what)}${r.result}${r.act}
    • `).join("")}
    ` :'

    עוד אין ציונים. כשתגישו בוחן או שיעורי בית, הם יופיעו כאן.

    '} ${g.quizzes_known===false?'

    חלק מהבחנים לא נטענו עכשיו.

    ':""} ${g.worksheets===null?'

    שיעורי הבית לא נטענו עכשיו.

    ':""}`; } // a sitting from the grades page opens its result screen; one from another device joins this device's list first function gradeSitting(key){ const [uid,sid]=key.split(":"), v=(GRADES?.units||[]).flatMap(u=>u.sittings).find(x=>x.id===sid); if(!v) return; const st=uqState(uid); let h=st.history.findIndex(x=>x.sid===sid); if(h<0){ st.history.push({at:v.submitted_at,attempts:v.attempts.slice(),score:null,timeUp:v.time_up,ck:crypto.randomUUID(),sid,mode:v.mode}); st.history.sort((p,q)=>String(p.at).localeCompare(String(q.at))); st.history=st.history.slice(-10); h=st.history.findIndex(x=>x.sid===sid); save(); } if(h<0) return; cur=null; uqResult(uid,h); } /* ---------- preparing for an exam (the class's exam board, with the units each exam covers) ---------- For each upcoming exam that names units: the days left, the units, the teacher's note, how ready the student is (each skill's state, the mistakes that keep coming back in those units, the latest quiz grades), and a plan of steps. The plan is fixed when the student first opens it (P.prep[key]) and each step is checked off from progress made after that. */ function prepExams(){ const today=new Date(); today.setHours(0,0,0,0); return examClasses().flatMap(row=>(row.exams||[]).filter(e=>e?.date&&Array.isArray(e.units)&&e.units.length&&examDate(e.date)>=today) .map(e=>({...e,classId:row.id,key:`${row.id}|${e.subject}|${e.date}`,days:Math.round((examDate(e.date)-today)/86400000), units:e.units.filter(id=>G.units.some(u=>u.id===id))}))) .filter(e=>e.units.length).sort((a,b)=>a.date.localeCompare(b.date)); } const prepSkills=e=>G.units.filter(u=>e.units.includes(u.id)).flatMap(u=>unitIds(u).filter(live)); function prepSummary(e){ const ids=prepSkills(e); return `${ids.filter(id=>P.done?.[id]).length} מתוך ${ids.length} מיומנויות`; } function prepRecurring(e){ const ids=new Set(prepSkills(e)); return Sky.patterns(P,G.mistakes).recurring.filter(x=>G.mistakes[x.m]&&(x.skills||[]).some(id=>ids.has(id))); } // the plan's steps, in order: not finished, fading, to strengthen, the units' quizzes, then the three quizzes before the exam function prepSteps(e){ const ids=prepSkills(e), steps=[], seen=new Set(); for(const id of ids) if(["new","practising"].includes(skillState(id))){ steps.push({k:"skill",id}); seen.add(id); } for(const id of ids) if(skillState(id)==="fading"){ steps.push({k:"review",id}); seen.add(id); } for(const x of prepRecurring(e)) if(BY[x.practise]&&!seen.has(x.practise)){ steps.push({k:"strengthen",id:x.practise}); seen.add(x.practise); } for(const u of G.units.filter(u=>e.units.includes(u.id)&&u.quiz)) steps.push({k:"uquiz",unit:u.id}); for(const h of HQ) steps.push({k:"quiz",kind:h.kind}); return steps; } function prepPlan(e){ const prep=(P.prep||={}), mine=prep[e.key]; if(mine?.steps?.some(x=>x.k==="exam")){ // a plan from before the three quizzes: its practice exam becomes them mine.steps=mine.steps.flatMap(x=>x.k==="exam"?HQ.map(h=>({k:"quiz",kind:h.kind})):[x]); save(); } if(mine?.steps) return mine; prep[e.key]={opened:new Date().toISOString(),steps:prepSteps(e)}; save(); return prep[e.key]; } function prepDone(step,e,since){ if(step.k==="skill") return !!P.done?.[step.id]; if(step.k==="review") return String(P.seen?.[step.id]||"")>since&&!Sky.fading(P,step.id); if(step.k==="strengthen") return String(P.seen?.[step.id]||"")>since; if(step.k==="uquiz") return uqState(step.unit).history.some(h=>String(h.at)>since&&h.mode!=="final"); if(step.k==="quiz") return (P.quizzes||[]).some(q=>q.kind===step.kind&&q.exam===e.key&&String(q.at)>since); return (P.quizzes||[]).some(q=>String(q.at)>since&&e.units.every(u=>(q.units||[]).includes(u))); } function prepStepHtml(step,e,done,today){ const unit=id=>G.units.find(u=>u.id===id); const [label,go]=step.k==="skill"?[`ללמוד ולסיים: ${fmt(BY[step.id].short||BY[step.id].name)}`,`data-id="${esc(step.id)}"`] :step.k==="review"?[`לחזור על מיומנות שדועכת: ${fmt(BY[step.id].short||BY[step.id].name)}`,`data-id="${esc(step.id)}"`] :step.k==="strengthen"?[`לחזק (טעות שחוזרת): ${fmt(BY[step.id].short||BY[step.id].name)}`,`data-id="${esc(step.id)}"`] :step.k==="uquiz"?[`${TERMS.practice}: ${esc(unit(step.unit)?.name||step.unit)}`,`data-uquiz="${esc(step.unit)}"`] :step.k==="quiz"?[esc(HQ.find(h=>h.kind===step.kind)?.title||""),`data-hq="${esc(step.kind)}" data-hq-exam="${esc(e.key)}"`] :[`${TERMS.mixed} על החומר`,`data-prep-exam="${esc(e.key)}"`]; return `
  • ${label}${done?' (בוצע)':""}${today&&!done?' היום':""}
  • `; } let PREP=null; async function openPrep(key){ const e=prepExams().find(x=>x.key===key); if(!e) return; if(!cur) back=document.activeElement; closeSheetQuietly(); cur="prep"; PREP={key,grades:null}; prepPlan(e); drawPrep(); try{ const r=await api("/api/math/my-grades",{cache:"no-store"}); if(cur==="prep"&&PREP?.key===key&&r.ok){ PREP.grades=await r.json(); drawPrep(); } }catch{} } function drawPrep(){ const e=prepExams().find(x=>x.key===PREP?.key); if(!e) return; const ids=prepSkills(e), states=ids.map(id=>({id,st:skillState(id)})), finished=states.filter(x=>["gold","lit","fading"].includes(x.st)).length; const fadingN=states.filter(x=>x.st==="fading").length, plan=prepPlan(e); const steps=plan.steps.filter(x=>["exam","uquiz","quiz"].includes(x.k)?true:BY[x.id]), done=steps.map(x=>prepDone(x,e,plan.opened)); const left=done.filter(d=>!d).length, perDay=e.days>=2&&left?Math.ceil(left/e.days):0; const todayIdx=new Set(); if(perDay){ for(let i=0,n=0;i(todayIdx.has(b)-todayIdx.has(a))||a-b):[...steps.keys()]; const units=G.units.filter(u=>e.units.includes(u.id)), recurring=prepRecurring(e); const grade=uid=>{ const u=(PREP.grades?.units||[]).find(x=>x.unit_id===uid); if(!u) return ""; const f=u.official, p=u.practice_best||u.practice_latest; return `${TERMS.final}: ${f?(f.released===false?"ממתין לפרסום":f.grade==null?"ממתין לבדיקה":`${f.grade}/100`):"–"} · ${TERMS.practice}, הכי טוב: ${p?(p.grade==null?"ממתין לבדיקה":`${p.grade}/100`):"–"}`; }; sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-x").focus({preventScroll:true}); } // opens the first item of the assignment that is not done function asgGo(id){ const a=ASG?.assignments.find(x=>x.id===id); if(!a) return; const s=Sky.assignmentState(a,P,{sittings:ASG.sittings,submissions:ASG.submissions}), item=s.items[s.next]?.item; if(!item) return; if(item.kind==="skill") return BY[item.id]&&open(item.id); if(item.kind==="worksheet") return wsOpen(item.id); if(item.kind==="unit_quiz") return uqIntro(item.unit); stopQuizClock(); QZ=null; return quizSetup(item.units); } /* Answers the teacher has reviewed that the student has not opened since (GET /api/math/teacher-comments): a card on the home screen opens the first one. Opening an answer tells the server it was seen, and drops it from the list. */ let TC=[]; async function loadComments(){ if(!CFG.userId||!CFG.answerScans) return; try{ const r=await api("/api/math/teacher-comments",{cache:"no-store"}); TC=r.ok?(await r.json()).comments||[]:[]; }catch{ TC=[]; } if(ACTIVE_SUBJECT==="מתמטיקה"&&root&&!root.hidden&&!cur) draw(); // not over the subject picker } const commentSeen=pred=>{ const n=TC.length; TC=TC.filter(c=>!pred(c)); return TC.length!==n; }; function commentsCard(){ if(!TC.length) return ""; const c=TC[0], more=TC.length-1; return ``; } function openComment(attemptId){ const c=TC.find(x=>x.attempt_id===attemptId); if(!c) return; const [kind,uid]=String(c.question_id).split(":"); if(kind==="quiz"){ const h=(uqState(uid).history||[]).findIndex(e=>(e.attempts||[]).includes(attemptId)); return h>=0?uqResult(uid,h):uqIntro(uid); // this device does not have that sitting: the quiz page lists the past ones } if(BY[c.question_id]) open(c.question_id); } /* ---------- help while practising a skill ---------- The "?" in a skill's sheet opens a panel on top of the question: the skill's description, its worked example (a question of its own, solved one step at a time with the reason for each) and the teacher's explanations of it. The question underneath is not touched, so closing the panel returns to it as it was. The worked example is also offered after the second mistake in a round. Opening the example is kept (P.examples) for the teacher's report; it changes nothing about finishing or gold, which still need clean first-try answers. */ let helpBack=null; const helpOpen=()=>!!sheet?.querySelector(".mx-help-wrap"); function helpClose(){ const el=sheet?.querySelector(".mx-help-wrap"); if(!el) return; el.remove(); helpBack?.focus?.({preventScroll:true}); helpBack=null; } function exampleCard(ex){ return `

    דוגמה פתורה

    ${fmt(ex.question)}

      ${ex.steps.map((st,i)=>`${fmt(st.do)}${fmt(st.why)}`).join("")}
    `; } // one more step, then the answer function exampleNext(btn){ const box=btn.closest(".mx-example"), next=box.querySelector(".mx-example-steps li[hidden]"); if(next){ next.hidden=false; if(!box.querySelector(".mx-example-steps li[hidden]")) btn.textContent="הצגת התשובה"; next.scrollIntoView({block:"nearest"}); return; } box.querySelector(".mx-example-answer").hidden=false; btn.hidden=true; } function showHelp(id){ const s=BY[id]; if(!s||!sheet||sheet.hidden) return; if(s.example){ const e=((P.examples||={})[id]||={n:0,last:""}); e.n++; e.last=new Date().toISOString(); save(); } if(!helpOpen()) helpBack=document.activeElement; sheet.querySelector(".mx-help-wrap")?.remove(); const el=document.createElement("div"); el.className="mx-help-wrap"; el.innerHTML=`
    `; sheet.append(el); el.querySelector(".mx-x").focus({preventScroll:true}); fillExplanations(el.querySelector("[data-help-resources]"),s.unit,r=>r.skill_id===id); } function open(id){ commentSeen(c=>c.question_id===id); stopPhone(); cancelAdvance(); const s=BY[id], st=status(id), unit=G.units.find(u=>u.id===s.unit), all=QS(s); if(P.done[id]&&cur!==id) delete P.set[id]; // a finished skill opened again: a new round const r=round(id), n=r.length; curQ=r.includes(P.at[id])?P.at[id]:r[0]; const q=all[curQ], openEnded=q.type==="open-ended", got=solvedOf(id), gotN=r.filter(i=>got.has(i)).length; if(!cur) back=document.activeElement; cur=id; FIRST=true; REV=null; stopQuizClock(); QZ=null; stopUnitQuiz(); sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-sheet").scrollTop=0; window.MathlyBoard?.mount(sheet,`${id}:${curQ}`,{question:()=>sheet.querySelector(".mx-q")}); // the sketch board beside the exercise, when the student has it on sheet.querySelector(".mx-x").focus({preventScroll:true}); if(openEnded&&st!=="off")window.MathlyAnswers?.mountQuestion(sheet.querySelector(".mx-answer"),id,s.question_version); fillExplanations(sheet.querySelector("[data-skill-res]"),s.unit,r=>r.skill_id===id); } function closeSheet(){ stopPhone(); dropResources(); dropWorksheet(); dropQueue(); dropReview(); cancelAdvance(); REV=null; stopQuizClock(); QZ=null; stopUnitQuiz(); if(sheet)sheet.hidden=true; cur=null; back?.focus?.({preventScroll:true}); back=null; } // counts down, then opens `target` (a skill id); stops if the student moved on in the meantime function autoAdvance(id,target){ let seconds=3; const countdown=sheet.querySelector(".mx-countdown"); const tick=()=>{ if(cur!==id||sheet.hidden||root.hidden) return cancelAdvance(); if(seconds===0) return open(target); countdown.textContent=target===id?`השאלה הבאה בעוד ${seconds}…`:`התרגיל הבא בעוד ${seconds}…`; seconds--; advanceTimer=setTimeout(tick,1000); }; tick(); } function answer(btn){ if(btn.disabled) return; if(QZ) return quizPick(btn); if(REV) return reviewAnswer(btn); const id=cur, m=btn.dataset.m, fb=sheet.querySelector(".mx-fb"), n=round(id).length, qi=curQ; const buttons=sheet.querySelectorAll("[data-choice]"); buttons.forEach(b=>b.classList.remove("right","wrong")); btn.classList.add(m?"wrong":"right"); P.tries[id]=(P.tries[id]||0)+1; if(n>1) P.at[id]=nextQ(id,qi); if(!m){ const all=solvedOf(id); all.add(qi); P.solved[id]=[...all].sort((a,b)=>a-b); const gotN=solvedIn(id); // solved in this round const missed=new Set(P.missed[id]||[]), recovered=missed.delete(qi); P.missed[id]=[...missed]; // done once every question is solved and the last answers were right at the first try const run=FIRST?(P.run[id]||0)+1:0, need=Sky.masterNeed(n); const first=!P.done[id]&&gotN>=n&&run>=need, unitWas=unitDone(unitOf(id)); if(first){ P.done[id]=new Date().toISOString(); Sky.mastered(P,id); delete ROUND_WRONG[id]; } COMBO=FIRST?COMBO+1:0; const persisted=!FIRST; const r=Sky.correct(P,id,{first:FIRST,n,finished:!!P.done[id]}); FIRST=false; save(); draw(); buttons.forEach(b=>b.disabled=true); COMBO>=2?Sky.sound.combo(COMBO):Sky.sound.ok(); Sky.buzz(25); Sky.burstAt(btn,14); const ok=`${Sky.praise({recovered,persisted,combo:COMBO})}${COMBO>=2?`${Sky.FLAME}${COMBO} ברצף`:""}`; const after=rewards(id,{r,lit:first,unitWas}), then=go=>after.then(()=>{ if(cur===id&&!sheet.hidden&&fb.isConnected) go(); }); // not once another question is shown if(!P.done[id]){ const left=need-run, say=gotN${ok}${say} `; return n===1?then(()=>{}):then(()=>autoAdvance(id,id)); } const nxt=next(); fb.innerHTML=`
    ${ok}${first?`${n>1?`פתרתם את כל ${n} השאלות. `:""}המיומנות סומנה כהושלמה.`:""} ${nxt&&nxt!==id?`` :nxt?"":"סיימתם את כל המסלול. כל הכבוד!"} ${n>1?``:""}
    `; if(nxt&&nxt!==id) then(()=>autoAdvance(id,nxt)); return; } // a mistake is quiet: no sound, no vibration, and words about the mistake, not about the student Sky.wrong(P,id); Sky.slip(P,id,qi,m,"practice"); FIRST=false; COMBO=0; const missed=new Set(P.missed[id]||[]); missed.add(qi); P.missed[id]=[...missed]; save(); const M=G.mistakes[m], to=M.back&&M.back!==id?M.back:null; // with a list of questions, a missed question comes back later, shuffled; the right answer is not shown, // so solving it then shows understanding, not memory if(n>1) buttons.forEach(b=>b.disabled=true); else btn.disabled=true; // from the second mistake in the round the worked example is offered (the first is often a slip, and the tip covers it) ROUND_WRONG[id]=(ROUND_WRONG[id]||0)+1; const offer=ROUND_WRONG[id]>=2&&BY[id].example?``:""; fb.innerHTML=`
    טעות נפוצה, וטוב שהיא עולה עכשיו${fmt(M.name)}${fmt(M.tip)} ${offer} ${n>1?`השאלה תחזור אחרי שאר השאלות, ותוכלו לנסות אותה שוב.`:""} ${to?``:n>1?"":"אפשר לבחור תשובה אחרת."} ${patternNote(m,[id,to])}
    `; } /* ---------- spaced review ---------- Finished skills come back after growing gaps (Sky.mastered/reviewed/due). A review is a few questions from different units, mixed, and the skill's name is shown only after the answer: choosing the method is part of the question. */ const REVIEW_SIZE=3; const choiceSkill=id=>QS(BY[id]).some(q=>q.choices); const dueReviews=()=>ORDER.filter(id=>SHOWN.has(BY[id].unit)&&live(id)&&choiceSkill(id)&&Sky.due(P,id)) .sort((a,b)=>(P.review[a]?.due||"").localeCompare(P.review[b]?.due||"")||seenAt(a)-seenAt(b)); function reviewCard(){ const n=dueReviews().length; if(!n) return ""; return ``; } /* ---------- misconception patterns ---------- The mistakes that keep coming back, across skills (Sky.patterns over P.slips): a card on home, and a sheet with what to practise for each. The words are about the mistake, never about the student; a pattern that stopped is a win. */ function myPatterns(){ const known=x=>G.mistakes[x.m]&&BY[x.practise], {recurring,stopped}=Sky.patterns(P,G.mistakes); return {recurring:recurring.filter(known),stopped:stopped.filter(known)}; } // after a mistake: a line when this mistake is now a recurring one, with its skill to strengthen (unless offered already) function patternNote(m,offered=[]){ const x=myPatterns().recurring.find(x=>x.m===m); if(!x) return ""; return `הטעות הזו חזרה ${timesWord(x.n)} ${inSkills(x.skills.length)} בחודש האחרון. כדאי לחזק את הבסיס שלה, והיא תיעלם גם בשאר הנושאים. ${offered.includes(x.practise)?"":``}`; } const inSkills=k=>k===1?"במיומנות אחת":`ב־${k} מיומנויות`; const timesWord=n=>n===1?"פעם אחת":`${n} פעמים`; const daysAgo=iso=>{ const d=Math.round((Date.parse(Sky.dayKey())-Date.parse(Sky.dayKey(new Date(iso))))/86400000); return d<=0?"היום":d===1?"אתמול":`לפני ${d} ימים`; }; function patternsCard(){ const {recurring,stopped}=myPatterns(), top=recurring[0]; if(!top&&!stopped.length) return ""; const say=top?`${fmt(G.mistakes[top.m].name)}: חזר ${timesWord(top.n)} ${inSkills(top.skills.length)} לאחרונה. כדאי לחזק.` :`${fmt(G.mistakes[stopped[0].m].name)}: הדפוס הזה כבר לא חוזר. התיקון עבד!`; return ``; } function showPatterns(){ const {recurring,stopped}=myPatterns(); const row=x=>{ const M=G.mistakes[x.m]; return `
  • ${fmt(M.name)}${fmt(M.tip)} ${timesWord(x.n)} ${inSkills(x.skills.length)} · לאחרונה ${daysAgo(x.last)}
  • `; }; if(!cur) back=document.activeElement; cur="patterns"; sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-sheet").scrollTop=0; sheet.querySelector(".mx-x").focus({preventScroll:true}); } // the oldest due skills, one per unit first, in a mixed order; a random multiple-choice question of each function startReview(){ const due=dueReviews(), pick=[], units=new Set(); for(const id of due) if(pick.length{ const qs=QS(BY[id]).map((q,i)=>i).filter(i=>QS(BY[id])[i].choices); return {id,qi:qs[Math.floor(Math.random()*qs.length)]}; }); if(!cur) back=document.activeElement; stopQuizClock(); QZ=null; REV={items,i:0,right:0}; openReview(); } function openReview(){ cancelAdvance(); const {items,i}=REV, it=items[i], q=QS(BY[it.id])[it.qi]; cur="review"; FIRST=true; sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-sheet").scrollTop=0; sheet.querySelector(".mx-x").focus({preventScroll:true}); } const inDaysWord=d=>d===1?"מחר":`בעוד ${d} ימים`; function reviewAnswer(btn){ const it=REV.items[REV.i], id=it.id, m=btn.dataset.m, ok=!m, fb=sheet.querySelector(".mx-fb"), s=BY[id]; sheet.querySelectorAll("[data-choice]").forEach(b=>b.disabled=true); btn.classList.add(ok?"right":"wrong"); P.tries[id]=(P.tries[id]||0)+1; Sky.reviewed(P,id,ok); const gap=Math.round((Date.parse(P.review[id].due)-Date.parse(Sky.dayKey()))/86400000); const last=REV.i+1>=REV.items.length, go=``; if(ok){ REV.right++; const r=Sky.correct(P,id,{first:true,n:QS(s).length,finished:true}); save(); draw(); Sky.sound.ok(); Sky.burstAt(btn,14); fb.innerHTML=`
    ${Sky.praise()}הנושא: ${fmt(s.name)}. החזרה הבאה עליו ${inDaysWord(gap)}.${go}
    `; rewards(id,{r,lit:false,unitWas:true}); return; } Sky.wrong(P,id); Sky.slip(P,id,it.qi,m,"review"); save(); draw(); const M=G.mistakes[m]; fb.innerHTML=`
    הנושא: ${fmt(s.name)}${fmt(M.name)}${fmt(M.tip)} שכחה היא חלק מהלמידה. הנושא יחזור מחר.${go} ${patternNote(m,[id])}
    `; } function reviewNext(){ if(REV.i+1החזרה הסתיימה ${right} מתוך ${items.length} נכונות. ${right===items.length?"הכול נשמר בזיכרון.":"מה שלא הצליח יחזור מחר, וזה בדיוק מה שמחזק אותו."} ${more?``:""}`; REV=null; } /* ---------- practice exam ---------- Like the real thing: questions from several units, mixed, the skill's name hidden, no feedback until it is handed in. It covers what the schedule taught before the class's next exam (or before this month, without one); the student can change the units. The report counts the answers per unit, names the three mistakes to work on with their skills, and brings the finished skills that went wrong back to today's review. Nothing on the path is locked by it. The results are kept in P.quizzes (newest last). */ const QUIZ_SIZE=12, QUIZ_MIN_PER_Q=3.5; // a relaxed pace: minutes per question, shown, never enforced const SCHOOL_MONTHS=["ספטמבר","אוקטובר","נובמבר","דצמבר","ינואר","פברואר","מרץ","אפריל","מאי","יוני","יולי","אוגוסט"]; const schoolMonth=d=>(d.getMonth()+4)%12; // September is 0 const skillMonth=id=>{ const m=String(BY[id].month||"").split("–")[0].trim(); const i=SCHOOL_MONTHS.indexOf(m); return i<0?99:i; }; // the exam the practice prepares for: the class's next one within a month, if any const quizExam=()=>{ const e=typeof nextExam==="function"?nextExam():null; return e&&e.days<=30?e:null; }; function quizUnits(){ const exam=quizExam(), until=exam?schoolMonth(examDate(exam.date)):schoolMonth(new Date()); const units=mineUnits().map(u=>({u,ids:unitIds(u).filter(id=>live(id)&&choiceSkill(id))})).filter(x=>x.ids.length); let on=units.filter(x=>x.ids.some(id=>skillMonth(id)x.ids.some(id=>skillMonth(id)<=until)); // the first month of the year const offered=units.filter(x=>x.ids.some(id=>skillMonth(id)<=until)); // taught by then; the exam's own month may be added return {exam,offered,on:new Set(on.map(x=>x.u.id))}; } function quizSetup(preset){ let {exam,offered,on}=quizUnits(); if(Array.isArray(preset)){ // an assignment asks for these units: they are offered whatever the schedule says, and chosen const all=mineUnits().map(u=>({u,ids:unitIds(u).filter(id=>live(id)&&choiceSkill(id))})).filter(x=>x.ids.length); offered=[...offered,...all.filter(x=>preset.includes(x.u.id)&&!offered.some(o=>o.u.id===x.u.id))]; on=new Set(preset.filter(id=>offered.some(o=>o.u.id===id))); } if(!cur) back=document.activeElement; cur="quiz"; QZ={setup:true,on,offered}; sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-x").focus({preventScroll:true}); } function quizToggle(uid){ if(!QZ?.setup) return; QZ.on.has(uid)?QZ.on.delete(uid):QZ.on.add(uid); sheet.querySelector(`[data-quiz-unit="${CSS.escape(uid)}"]`).setAttribute("aria-pressed",QZ.on.has(uid)); sheet.querySelector("[data-act=quiz-start]").disabled=!QZ.on.size; } // questions from the chosen units in proportion to their skills: the units take turns, each turn a skill not used yet function quizStart(){ const units=QZ.offered.filter(x=>QZ.on.has(x.u.id)).map(x=>({u:x.u,ids:shuffled(x.ids)})); const total=units.reduce((n,x)=>n+x.ids.length,0), items=[], used=new Set(); const share=units.map(x=>({...x,want:Math.max(1,Math.round(QUIZ_SIZE*x.ids.length/total)),k:0})); for(let turn=0;items.length=QUIZ_SIZE||x.k>=x.want) continue; const id=x.ids[x.k%x.ids.length], qs=QS(BY[id]).map((q,i)=>i).filter(i=>QS(BY[id])[i].choices&&!used.has(`${id}:${i}`)); if(!qs.length){ x.k++; continue; } const qi=qs[Math.floor(Math.random()*qs.length)]; used.add(`${id}:${qi}`); items.push({id,qi,pick:null}); x.k++; } if(share.every(x=>x.k>=x.want)) share.forEach(x=>x.want++); // rounding left room: one more for each, in turn } QZ={items:shuffled(items),i:0,started:Date.now(),units:units.map(x=>x.u.id)}; quizShow(); QZ.clock=setInterval(quizTick,1000); } function stopQuizClock(){ if(QZ?.clock) clearInterval(QZ.clock); } const mmss=ms=>{ const s=Math.floor(ms/1000); return `${Math.floor(s/60)}:${String(s%60).padStart(2,"0")}`; }; function quizTick(){ const el=sheet.querySelector(".mx-quiz-clock"); if(el&&QZ?.started) el.textContent=mmss(Date.now()-QZ.started); } function quizShow(){ const {items,i}=QZ, it=items[i], q=QS(BY[it.id])[it.qi], last=i===items.length-1; if(!it.order) it.order=shuffled(q.choices.map((_,k)=>k)); // the same order when coming back to a question sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-sheet").scrollTop=0; if(QZ.feedback&&it.pick!=null) quizFeedback(it,q); // coming back to an answered question: its feedback again window.MathlyBoard?.mount(sheet,`quiz:${QZ.started}:${i}`,{question:()=>sheet.querySelector(".mx-q")}); // a sketch per exam question } // a choice is only marked; it can be changed until the exam is handed in function quizPick(btn){ if(!QZ?.items) return; if(QZ.feedback){ // a strengthening quiz: the first pick is the answer, and the feedback comes right away const it=QZ.items[QZ.i]; if(it.pick!=null) return; it.pick=+btn.dataset.choice; sheet.querySelector(`[data-quiz-go="${QZ.i}"]`).classList.add("done"); const ok=btn.dataset.right!=null; ok?Sky.sound.ok():Sky.sound.tap?.(); return quizFeedback(it,QS(BY[it.id])[it.qi]); } const it=QZ.items[QZ.i]; it.pick=+btn.dataset.choice; sheet.querySelectorAll("[data-choice]").forEach(b=>b.setAttribute("aria-pressed",b===btn)); sheet.querySelector(`[data-quiz-go="${QZ.i}"]`).classList.add("done"); Sky.sound.tap(); } function quizGo(k){ if(!QZ?.items) return; QZ.i=Math.max(0,Math.min(QZ.items.length-1,k)); quizShow(); } function quizSubmit(){ const left=QZ.items.filter(x=>x.pick==null).length; if(left&&!QZ.sure){ QZ.sure=true; const nav=sheet.querySelector(".mx-quiz-nav"); nav.insertAdjacentHTML("beforebegin",`

    ${left===1?"שאלה אחת לא נענתה":`${left} שאלות לא נענו`}. אפשר לחזור אליהן במספרים למעלה, או ללחוץ שוב על הגשה.

    `); return; } stopQuizClock(); const items=QZ.items, ms=Date.now()-QZ.started, now=new Date().toISOString(); const res=items.map(x=>{ const q=QS(BY[x.id])[x.qi], c=x.pick==null?null:q.choices[x.pick]; return {...x,q,ok:!!c?.correct,mistake:c&&!c.correct?c.mistake:null}; }); const right=res.filter(r=>r.ok).length; // answering counts like practice: right answers go to today's goal, and a finished skill that went wrong is reviewed today for(const r of res){ if(r.ok){ Sky.ensure(P); const k=Sky.dayKey(); P.days[k]=(P.days[k]||0)+1; } else if(P.done[r.id]){ Sky.ensure(P); P.review[r.id]={box:0,due:Sky.dayKey()}; } if(r.mistake) Sky.slip(P,r.id,r.qi,r.mistake,"exam",new Date(now)); if(r.pick!=null){ (P.tries||={})[r.id]=(P.tries[r.id]||0)+1; if(!r.ok) (P.wrongs||={})[r.id]=(P.wrongs[r.id]||0)+1; } // like practice, for the chance of a right answer } const mistakes={}; res.filter(r=>r.mistake).forEach(r=>{ (mistakes[r.mistake]??={n:0,ids:new Set()}).n++; mistakes[r.mistake].ids.add(r.id); }); const skipped=res.filter(r=>r.pick==null).map(r=>r.id); const top=Object.entries(mistakes).sort((a,b)=>b[1].n-a[1].n).slice(0,3); const weak=[...new Set([...top.flatMap(([,m])=>[...m.ids]),...skipped])]; const def=QZ.def||null, title=QZ.title||TERMS.mixed, before=def?quizAttempts(def):[]; const entry={at:now,n:items.length,right,ms,units:QZ.units,weak}; if(def) Object.assign(entry,{kind:def.kind,exam:def.exam,qs:items.map(x=>`${x.id}:${x.qi}`)}); if(QZ.conf) entry.sure=items.map(x=>x.sure??null); (P.quizzes=P.quizzes||[]).push(entry); P.quizzes=P.quizzes.slice(-QUIZ_KEEP); save(); draw(); const perUnit=QZ.units.map(uid=>{ const u=G.units.find(x=>x.id===uid), rs=res.filter(r=>BY[r.id].unit===uid); return {u,n:rs.length,ok:rs.filter(r=>r.ok).length}; }).filter(x=>x.n); const plan=top.length?top.map(([k,m])=>{ const M=G.mistakes[k]; return `
  • ${fmt(M.name)}${m.n>1?` (${m.n} פעמים)`:""}${fmt(M.tip)} ${[...m.ids].map(id=>``).join("")}
  • `; }).join(""):""; QZ={done:true}; sheet.innerHTML=`
    `; sheet.querySelector(".mx-x").focus({preventScroll:true}); if(right/items.length>=.8) Sky.sound.goal(); } function quizQuit(){ if(!QZ?.items) return closeSheet(); if(QZ.items.every(x=>x.pick==null)||QZ.quitting) return closeSheet(); QZ.quitting=true; sheet.querySelector(".mx-quiz-nav").insertAdjacentHTML("beforebegin",'

    לצאת? התשובות במבחן הזה לא יישמרו. לחצו שוב על ✕ כדי לצאת.

    '); } /* ---------- quizzes chosen for each student (openspec student-home-and-exam-quizzes) ---------- Before an exam that names units (7 days or less): two strengthening quizzes, aiming at an expected score of about 70% and 80% (Sky.pickQuiz), with feedback after each answer and the worked example after a wrong one, and a quiz like the exam: its units only, mixed, topic names hidden, feedback at the end. Without such an exam: the two strengthening quizzes over the units taught so far. Each can be retaken with new questions; the best grade counts. Attempts are kept in P.quizzes with their kind, exam and questions. */ const QUIZ_KEEP=60, QUIZ_MIN=6, QUIZ_NOTE_MAX=140; const HQ=[ {kind:"s1",title:"מבחן תרגול 1",target:.7,band:[4,7],say:"בעיקר נושאים שכמעט סגרתם, עם משוב אחרי כל שאלה."}, {kind:"s2",title:"מבחן תרגול 2",target:.8,band:[3,5],say:"יותר נושאים שכבר יודעים, עם משוב אחרי כל שאלה."}, {kind:"exam",title:"מבחן תרגול כמו במבחן",target:.75,band:[2,3],say:"מכל החומר, מעורבב ובלי שמות נושאים. המשוב בסוף."}]; function homeQuizzes(){ if(teacherHome()) return []; const e=prepExams()[0]; if(e&&e.days<=7) return quizzesFor(e); const units=[...quizUnits().on]; return units.length?HQ.filter(h=>h.kind!=="exam").map(h=>({...h,exam:null,units,days:null})):[]; } const quizzesFor=e=>HQ.map(h=>({...h,exam:e.key,units:e.units,days:e.days})); const quizAttempts=def=>(P.quizzes||[]).filter(q=>q.kind===def.kind&&(q.exam||null)===(def.exam||null)); // the skills of the units with their multiple-choice questions; rank: taught later is higher const quizPool=units=>units.flatMap(uid=>{ const u=G.units.find(x=>x.id===uid); return u?unitIds(u):[]; }) .filter(id=>live(id)&&choiceSkill(id)) .map(id=>({id,unit:BY[id].unit,rank:skillMonth(id)*1000+ORDER.indexOf(id),qs:QS(BY[id]).map((q,i)=>q.choices?i:-1).filter(i=>i>=0)})); function buildQuiz(def){ const pool=quizPool(def.units), seen={}; for(const a of quizAttempts(def)) for(const k of a.qs||[]) if(!(String(seen[k]||"")>a.at)) seen[k]=a.at; const toLearn=pool.filter(t=>!Sky.practised(P,t.id)).map(t=>t.id); if(def.kind!=="exam") return {items:Sky.pickQuiz(P,pool,{target:def.target,n:QUIZ_SIZE,seen}).items,toLearn}; // like the exam: every unit, in proportion to the topics practised in it const by=def.units.map(uid=>pool.filter(t=>t.unit===uid&&Sky.practised(P,t.id))).filter(x=>x.length); const total=by.reduce((n,x)=>n+x.length,0), items=[]; for(const ts of by) items.push(...Sky.pickQuiz(P,ts,{target:def.target,n:Math.max(1,Math.round(QUIZ_SIZE*ts.length/total)),seen}).items); items.splice(QUIZ_SIZE); if(items.length`${x.id}:${x.qi}`, taken=new Set(items.map(key)), more={...seen}; for(const k of taken) more[k]="9999"; items.push(...Sky.pickQuiz(P,pool,{target:def.target,n:QUIZ_SIZE,seen:more}).items.filter(x=>!taken.has(key(x))).slice(0,QUIZ_SIZE-items.length)); } return {items,toLearn}; } const bestOf=list=>list.reduce((b,q)=>!b||q.right/q.n>b.right/b.n?q:b,null); function quizDelta(last,prev){ if(!last||!prev) return ""; const d=last.right-prev.right; return d>0?`+${d} מהפעם הקודמת`:d<0?`${d} מהפעם הקודמת`:"כמו בפעם הקודמת"; } function quizCards(){ const defs=homeQuizzes(); if(!defs.length) return ""; return `

    ${defs[0].exam?"בחנים לקראת המבחן":"בחנים"}

    ${defs.map(quizCardHtml).join("")}
    `; } function quizCardHtml(def){ const tries=quizAttempts(def), best=bestOf(tries), b=buildQuiz(def), today=def.days!=null&&def.days>=def.band[0]&&def.days<=def.band[1]; const head=`${esc(def.title)}${today?' מומלץ להיום':""}`; const days=def.days!=null?`מומלץ ${def.band[1]}–${def.band[0]} ימים לפני המבחן`:""; const mine=best?`הכי טוב: ${best.right}/${best.n}${tries.length>1?` · ${quizDelta(tries.at(-1),tries.at(-2))}`:""}`:""; if(b.items.length${head} אפשר להתחיל אחרי שתתרגלו קודם את הנושאים:${b.toLearn.slice(0,5).map(id=>``).join("")}${days}`; return ``; } function startHomeQuiz(kind,examKey){ const e=examKey?prepExams().find(x=>x.key===examKey):null; const def=(e?quizzesFor(e):homeQuizzes()).find(d=>d.kind===kind); if(!def) return; const b=buildQuiz(def); if(b.items.length({...x,pick:null}))),i:0,started:Date.now(),units:def.units,def,title:def.title,feedback:def.kind!=="exam",conf:def.kind==="exam"}; quizShow(); QZ.clock=setInterval(quizTick,1000); } // a strengthening quiz's answer: right or wrong, the mistake's tip, and the worked example after a wrong one function quizFeedback(it,q){ const box=sheet.querySelector(".mx-quiz-fb"); if(!box) return; const c=q.choices[it.pick], ok=!!c?.correct, M=!ok&&c?.mistake?G.mistakes[c.mistake]:null, ex=!ok?BY[it.id].example:null; sheet.querySelectorAll("[data-choice]").forEach(btn=>{ btn.disabled=true; if(btn.dataset.right!=null) btn.classList.add("right"); else if(+btn.dataset.choice===it.pick) btn.classList.add("wrong"); }); box.innerHTML=ok?`
    ${Sky.praise()}
    ` :`
    ${M?fmt(M.name):"לא בדיוק"}${M?`${fmt(M.tip)}`:""}התשובה הנכונה מסומנת. ${ex?`
    לראות דוגמה פתורה${exampleCard(ex)}
    `:""}
    `; } // how sure the student is, in the quiz like the exam (not required) const SURE=["לא בטוח/ה","די בטוח/ה","בטוח/ה"]; const quizSureHtml=it=>`
    כמה בטוחים? (לא חובה) ${SURE.map((l,k)=>``).join("")}
    `; function quizSure(k){ const it=QZ?.items?.[QZ.i]; if(!it) return; it.sure=it.sure===k?null:k; sheet.querySelectorAll("[data-quiz-sure]").forEach(b=>b.setAttribute("aria-pressed",+b.dataset.quizSure===it.sure)); } function quizSureWrongHtml(res,sure){ const bad=res.map((r,i)=>({r,s:sure[i]})).filter(x=>x.s===2&&!x.r.ok); return bad.length?`

    הייתי בטוח/ה וטעיתי

      ${bad.map(x=>`
    1. ${fmt(x.r.q.question)}
    2. `).join("")}

    טעויות שבטוחים בהן קשה לראות לבד. כדאי לעבור עליהן עם הדוגמה הפתורה או עם המורה.

    `:""; } function quizCompareHtml(right,n,before){ const best=bestOf([...before,{right,n}]), prev=before.at(-1); return `

    הכי טוב: ${best.right}/${best.n}${prev?` · ${quizDelta({right},prev)}`:""}

    `; } function quizNoteSave(form){ const q=(P.quizzes||[]).find(x=>x.at===form.dataset.quizNote), text=form.elements.note.value.trim().slice(0,QUIZ_NOTE_MAX); if(!q) return; if(text) q.note=text; else delete q.note; save(); form.querySelector("[role=status]").textContent=text?"נשמר.":""; } /* ---------- unit quiz ---------- A unit can end with a summarizing quiz (unit.quiz, app/math_skills.py): open-ended questions with points, and a time limit. It is shown like a paper quiz: all the questions at once, a clock counting down, nothing checked until it is handed in. The student writes on paper and photographs each answer (on this device, or from the phone on the same account); when the time is up there are UQ_PHOTO_MIN more minutes to photograph, then it is handed in by itself. After handing in, each answer is checked (the server does not check quiz answers on upload), and the report shows the points per question with feedback and a solution. The sitting is kept in P.uq[unit]: {sitting:{started, attempts, submitted}, history:[{at, score, graded}]} so a reload continues it with the clock still running. The time limit is kept by this page; the server keeps the answers and their checks. */ const UQ_PHOTO_MIN=10, UQ_BASE="/api/math/answer-attempts"; const uqState=uid=>((P.uq||={})[uid]||={history:[]}); const uqReady=u=>!!(u.quiz&&!u.quiz.locked&&CFG.answerScans&&CFG.userId); const uqEnds=(u,sit)=>sit.deadline?sit.deadline-UQ_PHOTO_MIN*60000:sit.started+u.quiz.minutes*60000; // a final sitting: the server's deadline function quizStep(u){ if(!u.quiz) return ""; const st=uqState(u.id), last=st.history.at(-1), going=st.sitting&&!st.sitting.submitted; const tag=going?"בתהליך":finOffer(u.id)?`${TERMS.final} פתוח`:last?.score!=null?`${last.score}/100`:last?"הוגש":""; return `
    ${TERMS.unitQuizzes}${tag?`${tag}`:""}
    `; } async function uqJson(url,options={}){ const r=await api(url,{cache:"no-store",...options}), d=await r.json().catch(()=>({})); if(!r.ok){ const e=Error(UQ_ERRORS[d.detail]||"החיבור נקטע. נסו שוב."); e.code=d.detail; e.status=r.status; throw e; } return d; } const uqPost=(url,body={})=>uqJson(url,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(body)}); const UQ_ERRORS={quiz_sittings_unavailable:"שמירת ההגשה אינה זמינה כרגע. נסו שוב מאוחר יותר.",invalid_sitting:"ההגשה לא תואמת לתשובות השמורות. פתחו את הבוחן מחדש.",question_changed:"הבוחן עודכן. רעננו את העמוד והתחילו מחדש.",image_too_large:"התמונה גדולה מדי. צלמו שוב.",invalid_image:"לא ניתן לקרוא את התמונה. צלמו שוב.", unsupported_image:"בחרו תמונת JPEG, PNG או WebP.",answer_storage_unavailable:"שמירת תשובות אינה זמינה כרגע.",grading_unavailable:"הבדיקה האוטומטית אינה זמינה כרגע.", grading_failed:"הבדיקה נכשלה. התמונה נשמרה; אפשר לנסות שוב.",grading_limit:"הגעתם למכסת הבדיקות היומית. נסו שוב מחר.",grading_busy:"כבר מתבצעות בדיקות. נסו שוב בעוד רגע.", upload_in_progress:"העלאה כבר מתבצעת. רגע…", quiz_time_over:"הזמן של המבחן נגמר. המבחן הוגש עם התשובות שנשמרו.",final_open:"המבחן של היחידה פתוח לכם. אחרי שתגישו אותו אפשר לתרגל שוב.", final_not_open:"המבחן לא פתוח עכשיו.",final_already_handed_in:"כבר הגשתם את המבחן של היחידה.",quiz_modes_unavailable:"מבחנים אינם זמינים כרגע.",open_ended_locked:"בוחנים פתוחים סגורים כרגע באתר.",sign_in_required:"יש להיכנס לחשבון."}; function stopUnitQuiz(){ if(UQ){ clearInterval(UQ.clock); clearTimeout(UQ.poll); Object.values(UQ.images||{}).forEach(URL.revokeObjectURL); } UQ=null; } // the intro: what the quiz is and how it works, the last results, and start / continue function uqIntro(uid){ const u=G.units.find(x=>x.id===uid), q=u.quiz, st=uqState(uid), offer=finOffer(uid), start=offer?.start; // a final sitting started on another device (or before this device lost its copy): the server's start continues it if(start&&!start.sitting_id&&start.attempt_ids?.length===q.questions.length&&st.sitting?.startId!==start.id&&!(st.sitting&&!st.sitting.submitted)) st.sitting=finSitting(start); const sit=st.sitting, going=sit&&!sit.submitted; if(going) return uqOpen(uid); if(!cur) back=document.activeElement; stopUnitQuiz(); cur="uquiz"; UQ={uid}; const ready=uqReady(u), n=q.questions.length; const until=offer?new Date(Date.parse(offer.until)-(FIN?.skew||0)).toLocaleString("he-IL",{dateStyle:"short",timeStyle:"short"}):""; const final=offer?`

    📌 ${TERMS.final}${offer.grant?" (ניסיון נוסף מהמורה)":""}

    המורה פתח/ה את הבוחן המסכם של היחידה עד ${esc(until)}. ${offer.grant?.reason?`(${esc(offer.grant.reason)})`:""}

    • ${TERMS.final} נעשה פעם אחת, והציון שלו הוא הציון של היחידה.
    • השעון נשמר בשרת: גם אם סוגרים או מרעננים, הזמן ממשיך.
    • הציון והפתרונות יופיעו אחרי שהמורה יפרסם את התוצאות.
    ${ready?`
    `:""}
    `:""; const label=h=>h.mode==="final"?(h.score==null?"מסכם · הוגש":`מסכם · ${h.score}/100`):(h.score==null?"חיזוק · ממתין לבדיקה":`חיזוק · ${h.score}/100`); sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-x").focus({preventScroll:true}); } // starting the final asks once more: it is given once function uqFinalAsk(){ const box=sheet.querySelector("[data-uq-final-box]"); if(!box) return; box.innerHTML=` `; box.querySelector("[data-act=uq-final-go]").focus(); } /* ---------- final unit quizzes (app/quiz_modes.py) ---------- GET /api/math/quiz-windows: the final sittings open to the student now (a window of their class, or a grant from the teacher), each with its start when started, and the windows of their final sittings (whether results are released). A final sitting's clock is the server's: `skew` turns the server's times into this device's. */ let FIN=null; async function loadFinals(){ if(!CFG.userId||!MY_CLASSES.length) return; try{ const r=await api("/api/math/quiz-windows",{cache:"no-store"}); FIN=r.ok?await r.json():null; if(FIN) FIN.skew=Date.parse(FIN.now)-Date.now(); }catch{ FIN=null; } if(ACTIVE_SUBJECT==="מתמטיקה"&&root&&!root.hidden&&!cur) draw(); } const finOffer=uid=>(FIN?.offers||[]).find(o=>o.window.unit_id===uid&&!o.start?.sitting_id)||null; const finSitting=(start,skew=FIN?.skew||0)=>({started:Date.parse(start.started_at)-skew,deadline:Date.parse(start.deadline)-skew, attempts:start.attempt_ids.slice(),submitted:null,startId:start.id,mode:"final"}); // a new sitting: one fresh answer attempt per question async function uqStart(final=false){ const uid=UQ.uid, u=G.units.find(x=>x.id===uid), btn=sheet.querySelector(final?"[data-act=uq-final-go]":"[data-act=uq-start]"); const label=btn.textContent; btn.disabled=true; btn.textContent="מכינים את הבוחן…"; try{ let start=null, skew=0; if(final){ const d=await uqPost("/api/math/quiz-sittings/start",{unit_id:uid}); start=d.start; skew=Date.parse(d.now)-Date.now(); } const n=u.quiz.questions.length, have=start?.attempt_ids?.length===n?start.attempt_ids:[], attempts=[]; for(const [index,q] of u.quiz.questions.entries()) attempts.push(have[index]||(await uqPost("/api/math/quiz-attempts",{unit:uid,index,question_version:q.question_version,new:true, ...(start?{start_id:start.id}:{first:index===0})})).attempt.id); uqState(uid).sitting=start?finSitting({...start,attempt_ids:attempts},skew):{started:Date.now(),attempts,submitted:null,mode:"practice"}; save(); draw(); if(final) loadFinals(); uqOpen(uid); }catch(e){ btn.disabled=false; btn.textContent=label; btn.insertAdjacentHTML("afterend",`

    ${esc(e.message)}

    `); if(final) loadFinals(); } } // the quiz paper function uqOpen(uid){ const u=G.units.find(x=>x.id===uid), sit=uqState(uid).sitting; if(!cur) back=document.activeElement; stopUnitQuiz(); cur="uquiz"; UQ={uid,res:{},images:{},busy:{},msg:{}}; sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-sheet").scrollTop=0; // all the questions on one paper: the board has a page per question, and hands in that page as the question's answer window.MathlyBoard?.mount(sheet,`uq:${uid}:${sit.started}`,{pages:u.quiz.questions.length,question:i=>sheet.querySelector(`#uq-q${i}`), submit:async(i,picture)=>{ if(UQ?.uid!==uid||uqState(uid).sitting?.submitted) return {error:"הבוחן כבר הוגש"}; await uqUpload(i,picture); return UQ?.msg[i]?{error:UQ.msg[i]}:{}; }, turned:i=>sheet.querySelector(`#uq-q${i}`)?.scrollIntoView({block:"start",behavior:"smooth"})}); sheet.querySelector("[data-uq-file]").onchange=e=>{ const f=e.target.files[0]; e.target.value=""; if(f&&UQ?.pick!=null) uqUpload(UQ.pick,f); }; uqTick(); UQ.clock=setInterval(uqTick,1000); sit.attempts.forEach((_,i)=>uqRefresh(i)); } function uqTick(){ if(!UQ?.uid) return; const u=G.units.find(x=>x.id===UQ.uid), sit=uqState(UQ.uid).sitting; if(!sit||sit.submitted) return; const left=uqEnds(u,sit)-Date.now(), photo=left+UQ_PHOTO_MIN*60000; const clock=sheet.querySelector(".mx-uq-clock"), banner=sheet.querySelector(".mx-uq-banner"); if(!clock) return; if(photo<=0) return uqSubmit(true); clock.textContent=left>0?mmss(left):mmss(photo); clock.classList.toggle("low",left>0&&left<5*60000); clock.classList.toggle("over",left<=0); const say=left>0?(left<5*60000?"נשארו פחות מ־5 דקות. זה זמן טוב לבדוק ולצלם.":""):`הזמן לכתיבה נגמר. נשארו ${mmss(photo)} לצילום ולהגשה, בלי להוסיף לפתרונות.`; if(banner.textContent!==say) banner.textContent=say; } // one question's answer box: the photo if there is one, and the ways to add or replace it async function uqRefresh(i){ const sit=uqState(UQ.uid).sitting, aid=sit.attempts[i], uid=UQ.uid; try{ const r=await uqJson(`${UQ_BASE}/${aid}`); if(UQ?.uid!==uid) return; UQ.res[i]=r; if(r.attachment&&!UQ.images[i]){ const b=await api(`${UQ_BASE}/${aid}/image`,{cache:"no-store"}); if(b.ok&&UQ?.uid===uid) UQ.images[i]=URL.createObjectURL(await b.blob()); } }catch(e){ if(UQ?.uid===uid) UQ.msg[i]=e.message; } if(UQ?.uid===uid) uqDrawAnswer(i); // waiting for the phone: look again soon if(UQ?.uid===uid&&!UQ.res[i]?.attachment&&["waiting","confirmed","uploading","failed"].includes(UQ.res[i]?.scan?.state)){ clearTimeout(UQ.poll); UQ.poll=setTimeout(()=>sit.attempts.forEach((_,k)=>uqRefresh(k)),2000); } } // a photo is on its way (this device's, or the phone's): handing in now would leave that answer out const uqUploading=()=>Object.values(UQ?.busy||{}).some(Boolean)||Object.values(UQ?.res||{}).some(r=>r&&!r.attachment&&r.scan?.state==="uploading"); function uqPaintSubmit(){ const b=sheet.querySelector('[data-act="uq-submit"]'); if(!b) return; const up=uqUploading(); b.disabled=up; b.textContent=up?"מעלים תמונה…":"הגשת הבוחן"; } function uqDrawAnswer(i){ const box=sheet.querySelector(`[data-uq-i="${i}"]`); if(!box) return; const r=UQ.res[i], has=!!r?.attachment, busy=UQ.busy[i], st=r?.scan?.state, phone=["waiting","confirmed","uploading","failed"].includes(st)&&!has; const touch=matchMedia("(max-width: 700px)").matches||navigator.maxTouchPoints>0; // on a computer the phone comes first: it is how most answers on paper get here. While the phone has the request, // the button is the request's box (its code and progress), not a window over the quiz. const phoneBtn=touch?"":window.MathlyAnswers.phoneLive(has?null:r?.scan,`data-uq-phone="${i}"`,`data-uq-cancel="${i}"`,has?"צילום מחדש בטלפון":"צילום בטלפון",!!busy); box.innerHTML=`${has?`התשובה המצולמת לשאלה ${i+1}`:""}

    ${busy?esc(busy):UQ.msg[i]?esc(UQ.msg[i]):has?"✓ התשובה נשמרה":phone?"":st==="declined"?"הבקשה נדחתה בטלפון":st==="expired"?"בקשת הצילום פגה":"עוד לא צולמה תשובה"}

    ${window.MathlyBoard?``:""}${phoneBtn}`; const done=Object.values(UQ.res).filter(x=>x?.attachment).length, n=uqState(UQ.uid).sitting.attempts.length; const c=sheet.querySelector("[data-uq-count]"); if(c) c.textContent=`צולמו ${done} מתוך ${n} תשובות`; uqPaintSubmit(); } // a photo replaces an answer by a fresh attempt for that question (an attempt keeps the one photo it received) async function uqFresh(i){ const u=G.units.find(x=>x.id===UQ.uid), q=u.quiz.questions[i], sit=uqState(UQ.uid).sitting; if(!UQ.res[i]?.attachment) return sit.attempts[i]; const r=await uqPost("/api/math/quiz-attempts",{unit:UQ.uid,index:i,question_version:q.question_version,new:true,...(sit.startId?{start_id:sit.startId}:{})}); sit.attempts[i]=r.attempt.id; save(); if(UQ.images[i]){ URL.revokeObjectURL(UQ.images[i]); delete UQ.images[i]; } UQ.res[i]=r; return r.attempt.id; } // this device: request, confirm, lease and upload in a row (the same steps the phone takes) async function uqUpload(i,file){ const uid=UQ.uid; UQ.busy[i]="מעלים את התמונה…"; UQ.msg[i]=""; uqDrawAnswer(i); try{ const photo=await window.MathlyPhoto.prepare(file); if(photo.size>5000000) throw Error(UQ_ERRORS.image_too_large); const aid=await uqFresh(i); const scan=(await uqPost(`${UQ_BASE}/${aid}/scans`,{renew:true,direct:true})).scan; // this device's own upload: no phone is asked await uqPost(`/api/math/answer-scans/${scan.id}/confirm`); const lease=await uqPost(`/api/math/answer-scans/${scan.id}/uploads`,{key:crypto.randomUUID()}); if(!lease.attachment){ const form=new FormData(); form.append("photo",photo,"answer.jpg"); await uqJson(`/api/math/answer-scans/${scan.id}/uploads/${lease.lease_id}`,{method:"POST",body:form}); } }catch(e){ if(UQ?.uid===uid) UQ.msg[i]=e.message; if(e.code==="quiz_time_over"&&UQ?.uid===uid){ UQ.busy[i]=""; return uqSubmit(true); } } if(UQ?.uid!==uid) return; UQ.busy[i]=""; uqRefresh(i); } // the phone: a request with a 4-digit code, shown in the answer's own box until the phone sends the photo or declines async function uqPhone(i){ const uid=UQ.uid; UQ.busy[i]="יוצרים בקשת צילום…"; UQ.msg[i]=""; uqDrawAnswer(i); try{ const aid=await uqFresh(i); UQ.res[i]=await uqPost(`${UQ_BASE}/${aid}/scans`,{renew:true}); } catch(e){ if(UQ?.uid===uid) UQ.msg[i]=e.message; } if(UQ?.uid!==uid) return; UQ.busy[i]=""; uqRefresh(i); } async function uqCancel(i){ // the computer withdraws the request const uid=UQ.uid, sid=UQ.res[i]?.scan?.id; if(!sid) return; try{ UQ.res[i]=await uqPost(`/api/math/answer-scans/${sid}/decline`); }catch(e){ if(UQ?.uid===uid) UQ.msg[i]=e.message; } if(UQ?.uid===uid) uqRefresh(i); } function uqSubmit(timeUp=false){ const uid=UQ.uid, sit=uqState(uid).sitting; if(!sit||sit.submitted) return; if(!timeUp&&uqUploading()) return; // the button is disabled too; this covers a key press const missing=sit.attempts.filter((_,i)=>!UQ.res[i]?.attachment).length; if(!timeUp&&missing&&!UQ.sure){ UQ.sure=true; sheet.querySelector(".mx-quiz-nav").insertAdjacentHTML("beforebegin",`

    ${missing===1?"לשאלה אחת":`ל־${missing} שאלות`} אין תשובה מצולמת, והן יקבלו 0 נקודות. לחצו שוב על הגשה כדי להגיש בכל זאת.

    `); return; } sit.submitted=Date.now(); const st=uqState(uid); st.history.push({at:new Date().toISOString(),attempts:sit.attempts.slice(),score:null,timeUp,ck:crypto.randomUUID(),mode:sit.mode||"practice",...(sit.startId?{startId:sit.startId}:{})}); st.history=st.history.slice(-10); save(); draw(); uqResult(uid,st.history.length-1,true); } /* The grade of a sitting is worked out by the server from the stored checks (app/quiz_grades.py); the score kept in this device's progress is only a copy of it. A sitting is handed in on the server once (`sid`); the same key makes a retry after a lost response the same sitting. */ async function uqSend(uid,entry){ if(entry.sid) return entry.sid; if(!entry.ck){ entry.ck=crypto.randomUUID(); save(); } const d=await uqPost("/api/math/quiz-sittings",{unit_id:uid,attempts:entry.attempts,time_up:!!entry.timeUp,client_key:entry.ck,...(entry.startId?{start_id:entry.startId}:{})}); if(entry.startId) loadFinals(); entry.sid=d.id; if(d.code) entry.code=d.code; save(); return entry.sid; } async function uqSitting(uid,sid){ return (await uqJson(`/api/math/quiz-sittings?unit_id=${encodeURIComponent(uid)}`)).sittings.find(x=>x.id===sid)||null; } // the report: each answer is checked now (once), then points per question, feedback and the solution async function uqResult(uid,h,check=false){ const u=G.units.find(x=>x.id===uid), st=uqState(uid), entry=st.history[h]; if(!cur) back=document.activeElement; stopUnitQuiz(); cur="uquiz"; UQ={uid,res:{},images:{}}; const n=entry.attempts.length; sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-x").focus({preventScroll:true}); let sendError=""; commentSeen(c=>entry.attempts.includes(c.attempt_id)); try{ await uqSend(uid,entry); }catch(e){ sendError=e.message; } if(UQ?.uid!==uid) return; const codeTag=()=>entry.code?` · מספר הבוחן: ${esc(entry.code)}`:""; if(entry.code) sheet.querySelector(".mx-src").innerHTML=(CFG.aiDirect===false?"שולחים את התשובות לבדיקת המורה":"בודקים את התשובות")+codeTag(); const results=[]; for(let i=0;ir?.ai_request?.code).find(Boolean)||""; // a hand-in from before, or another device sheet.querySelector(".mx-src").innerHTML=(CFG.aiDirect===false?"שולחים את התשובות לבדיקת המורה":"")+codeTag(); // points: the server's, per question (a checked answer earns its share; a missing one 0; one not checked yet leaves the total open) let sitting=null; if(entry.sid){ try{ sitting=await uqSitting(uid,entry.sid); }catch(e){ sendError||=e.message; } } if(UQ?.uid!==uid) return; if(sitting?.mode==="final"&&!sitting.released) return uqUnreleased(uid,entry,results); const rows=u.quiz.questions.map((q,i)=>{ const r=results[i], g=r?.grading, a=g?.state==="completed"?g.result:null, pts=sitting?sitting.questions[i].earned:null; let say=""; if(!r?.attachment) say="לא צולמה תשובה"; else if(sitting?.questions[i]?.state==="teacher") say="נבדק על ידי המורה"; else if(a&&a.score!=null) say={correct:"נכון ומלא",partial:"נכון בחלקו",incorrect:"לא נכון"}[a.verdict]||""; else { const req=a?null:r.ai_request; say=req&&["pending","failed"].includes(req.state)?"ממתין לאישור המורה לבדיקה":req?.state==="sending"?"בבדיקה עכשיו":req?.state==="declined"?`המורה לא אישר את הבדיקה${req.note?`: ${req.note}`:""}`:a?.verdict==="unreadable"?"לא הצלחנו לקרוא את התמונה":a?.verdict==="needs_review"?"ממתין לבדיקה של המורה":r.error||(!r.grading_available?"הבדיקה האוטומטית אינה זמינה כרגע":"עוד לא נבדקה"); } return `
  • שאלה ${i+1}${pts==null?"–":pts}/${q.points} נק׳

    ${fmt(q.question).replace(/\n/g,"
    ")}

    ${UQ.images[i]?`התשובה שלכם לשאלה ${i+1}`:""}

    ${esc(say)}

    ${window.MathlyAnswers?.teacherNote?.(r||{})||""} ${a?.feedback?`

    ${r.teacher_review?"בדיקה אוטומטית: ":""}${fmtLoose(a.feedback)}

    `:""}${a?.next_step?`

    הצעד הבא: ${fmtLoose(a.next_step)}

    `:""} ${a?.solution?`
    הפתרון המלא

    ${fmtLoose(a.solution)}

    `:""} ${r?.attachment?window.MathlyAnswers?.reviewBox?.(entry.attempts[i],r)||"":""} ${r?.attachment&&!a&&r.grading_available&&!["pending","sending","failed"].includes(r.ai_request?.state)?``:""}
  • `; }); const earned=sitting?sitting.questions.reduce((sum,x)=>sum+(x.earned||0),0):0, open=sitting?sitting.open:n; entry.score=sitting?.grade??null; save(); draw(); const total=!sitting?"ההגשה לא נשמרה בשרת":open?`${earned} נקודות עד עכשיו · ${open===1?"שאלה אחת לא נבדקה":`${open} שאלות לא נבדקו`}`:`${earned}/100`; sheet.querySelector("#mxTitle").textContent=`${sitting?.mode==="final"||entry.mode==="final"?TERMS.final:TERMS.practice}: ${total}`; sheet.querySelector(".mx-src").innerHTML=esc(`הוגש ${new Date(entry.at).toLocaleString("he-IL",{dateStyle:"short",timeStyle:"short"})}${entry.timeUp?" · הוגש כשהזמן נגמר":""}`)+codeTag(); const final=sitting?.mode==="final", retake=final?"":uqRetakeHtml(sitting); sheet.querySelector(".mx-uq-report").innerHTML=`
      ${rows.join("")}
    ${sendError&&!sitting?`

    ${esc(sendError)}

    `:""} ${retake}

    הבדיקה אוטומטית באמצעות AI: בדקו שהיא תואמת למה שכתבתם, ואם משהו נראה לא נכון, הראו למורה.

    `; } // the skills behind the points a practice sitting lost (each quiz question names its skills), to practise before a retake const SKILL_STATE={gold:"זהב ⭐",lit:"הושלם",fading:"דועך – כדאי לחזור",practising:"בתרגול",new:"עוד לא התחיל"}; function skillState(id){ if(P.done?.[id]) return P.gold?.[id]?"gold":Sky.fading(P,id)?"fading":"lit"; return (P.missed?.[id]||[]).length||P.tries?.[id]||P.seen?.[id]?"practising":"new"; } function uqLostSkills(sitting){ if(!sitting) return []; return [...new Set(sitting.questions.filter(x=>x.earned!=null&&x.earnedx.skills||[]))].filter(id=>BY[id]); } function uqRetakeHtml(sitting){ const lost=uqLostSkills(sitting); if(!lost.length) return ""; return `

    לפני ניסיון נוסף: כדאי לתרגל

    אלה המיומנויות של השאלות שבהן הפסדתם נקודות. אפשר לתרגל אותן קודם, או לנסות שוב מיד.

      ${lost.map(id=>`
    • ${SKILL_STATE[skillState(id)]}
    • `).join("")}
    `; } // a final sitting before the teacher releases the results: handed in, and the photos the student sent function uqUnreleased(uid,entry,results){ const u=G.units.find(x=>x.id===uid); entry.score=null; save(); draw(); sheet.querySelector("#mxTitle").textContent=`${TERMS.final}: הוגש`; sheet.querySelector(".mx-src").textContent=`הוגש ${new Date(entry.at).toLocaleString("he-IL",{dateStyle:"short",timeStyle:"short"})}${entry.timeUp?" · הוגש כשהזמן נגמר":""}`; sheet.querySelector(".mx-uq-report").innerHTML=`

    המבחן הוגש. התוצאות יפורסמו על ידי המורה: הציון, הבדיקה והפתרונות יופיעו כאן ובדף הציונים.

      ${u.quiz.questions.map((q,i)=>`
    1. שאלה ${i+1}${q.points} נק׳
      ${UQ.images[i]?`התשובה שלכם לשאלה ${i+1}`:`

      ${results[i]?.attachment?"":"לא צולמה תשובה"}

      `}
    2. `).join("")}
    `; } function onClick(e){ const t=e.target.closest("button,[data-act]"); if(!t||!root.contains(t)) return; if(t.dataset.choice!=null) return answer(t); if(t.dataset.boardToggle!=null) return window.MathlyBoard?.toggle(); if(t.dataset.quizUnit) return quizToggle(t.dataset.quizUnit); if(t.dataset.quizGo!=null) return quizGo(+t.dataset.quizGo); if(t.dataset.quizSure!=null) return quizSure(+t.dataset.quizSure); if(t.dataset.hq) return startHomeQuiz(t.dataset.hq,t.dataset.hqExam); if(t.dataset.uquiz) return uqIntro(t.dataset.uquiz); // a sitting in progress opens straight on its paper if(t.dataset.sres) return t.closest(".mx-sres-item").classList.toggle("zoom"); if(t.dataset.help) return showHelp(t.dataset.help); if(t.dataset.example) return showHelp(t.dataset.example); if(t.dataset.exampleNext!=null) return exampleNext(t); if(t.dataset.aqTab) return aqTab(t.dataset.aqTab); if(t.dataset.reviewAnswer) return trOpenAnswer(t.dataset.reviewAnswer,t.dataset.reviewFrom); if(t.dataset.reviewBack!=null) return TR?.back?.(); if(t.dataset.answers) return reviewSitting(...t.dataset.answers.split(":")); if(t.dataset.askReview!=null) return window.MathlyAnswers.askReview(t); if(t.dataset.comment) return openComment(t.dataset.comment); if(t.dataset.asgGo) return asgGo(t.dataset.asgGo); if(t.dataset.gradeSitting) return gradeSitting(t.dataset.gradeSitting); if(t.dataset.prep) return openPrep(t.dataset.prep); if(t.dataset.prepExam){ const e=prepExams().find(x=>x.key===t.dataset.prepExam); if(e){ stopQuizClock(); QZ=null; cur=null; return quizSetup(e.units); } } if(t.dataset.asgNew!=null){ REPORT.form={id:null}; drawReport(); return sheet.querySelector(".mx-asg-form input[name=title]")?.focus(); } if(t.dataset.asgEdit){ REPORT.form={id:t.dataset.asgEdit,focus:t.dataset.asgFocus!=null}; drawReport(); return sheet.querySelector(".mx-asg-form input[name=title]")?.focus(); } if(t.dataset.asgFocusNew!=null) return focusOpen(); if(t.dataset.finalClose||t.dataset.finalRelease) return finalAct(t); if(t.dataset.batch) return aqBatch(t); if(t.dataset.examDel) return examDelete(t.dataset.examDel); if(t.dataset.inboxOpen) return inboxOpen(t.dataset.inboxOpen); if(t.dataset.wsAiFill){ const s=REPORT?.data.students.flatMap(x=>x.worksheet_submissions||[]).find(x=>x.id===t.dataset.wsAiFill), f=sheet.querySelector("[data-ws-check]"); if(s?.ai&&f){ if(s.ai.score!=null) f.elements.score.value=s.ai.score; f.elements.comment.value=s.ai.comment||""; f.elements.comment.focus(); } return; } if(t.dataset.focusGive) return focusOpen([t.dataset.focusGive],t.dataset.focusTo.split(",").filter(Boolean)); if(t.dataset.asgCancel!=null){ wsCancel(); REPORT.form=null; return drawReport(); } if(t.dataset.asgDel) return asgDelete(t.dataset.asgDel); if(t.dataset.wsOpen) return wsOpen(t.dataset.wsOpen); if(t.dataset.wsUpload!=null) return wsUpload(t); if(t.dataset.wsRemove) return wsRemove(t.dataset.wsRemove); if(t.dataset.wsSub) return wsSubOpen(t.dataset.wsSub); if(t.dataset.wsSubBack!=null){ wsSubClose(); if(REPORT) REPORT.inbox=null; drawReport(); return sheet.querySelector(`[data-report-tab="${REPORT.tab}"]`)?.focus(); } if(t.dataset.wsAi!=null) return wsAiCheck(t); if(t.dataset.wsPick!=null) return sheet.querySelector("[data-ws-file]")?.click(); if(t.dataset.wsSend!=null) return wsSend(); if(t.dataset.wsBoard!=null) return wsBoard(); if(t.dataset.wsPhone!=null) return wsPhone(); if(t.dataset.wsCancel!=null) return wsPhoneCancel(); if(t.dataset.wsUp) return wsMove(+t.dataset.wsUp,false); if(t.dataset.wsDrop) return wsMove(+t.dataset.wsDrop,true); if(t.dataset.wsDownload!=null) return wsDownload(t); if(t.dataset.uqResult){ const [uid,h,check]=t.dataset.uqResult.split(":"); return uqResult(uid,+h,check==="check"); } if(t.dataset.uqPhoto!=null){ UQ.pick=+t.dataset.uqPhoto; return sheet.querySelector("[data-uq-file]").click(); } if(t.dataset.uqPhone!=null) return uqPhone(+t.dataset.uqPhone); if(t.dataset.uqCancel!=null) return uqCancel(+t.dataset.uqCancel); if(t.dataset.uqBoard!=null) return window.MathlyBoard?.show(+t.dataset.uqBoard); if(t.dataset.subject) return selectSubject(t.dataset.subject); if(t.dataset.view){ closeSheet(); return setView(t.dataset.view,t.dataset.unit); } if(t.dataset.comet) return showComet(t.dataset.comet); if(t.dataset.id) return open(t.dataset.id); if(t.dataset.manageMode) return showManageMode(t.dataset.manageMode); if(t.dataset.prompterPlay) return window.MathlyPrompter?.open(PR.texts.find(x=>x.id===t.dataset.prompterPlay)?.body); if(t.dataset.prompterOutline) return prOutline(t.dataset.prompterOutline,t); if(t.dataset.prompterOpen) return prEdit(t.dataset.prompterOpen); if(t.dataset.prompterDel) return prDelete(t.dataset.prompterDel); if(t.dataset.classAction) return manageClassAction(t.dataset.classAction,t.dataset.classId); if(t.dataset.reportClass!=null) return classReport(t.dataset.reportClass); if(t.dataset.reportStudent!=null) return reportOpenStudent(t.dataset.reportStudent); if(t.dataset.reportTab||t.dataset.heatUnit||t.dataset.heatGroup||t.dataset.heatAll) return reportControl(t); if(t.dataset.aqSend) return aqSend(t.dataset.aqSend); if(t.dataset.aqDecline) return aqDecline(t.dataset.aqDecline); switch(t.dataset.act){ case "help-close": return helpClose(); case "manage": return manageClasses(); case "class-report": return classReport(t.dataset.classId); case "ai-queue": return aiQueue(); case "aq-all": return aqAll(); case "photo-cancel": return photoCancel(); case "manage-refresh": return loadManagedClasses(true); case "manage-lab": return showManageMode("lab"); case "manage-prompter": return showManageMode("prompter"); case "prompter-go": return window.MathlyPrompter?.open(sheet.querySelector("#mxPrompterText")?.value); case "prompter-outline-go": return window.MathlyPrompter?.open(sheet.querySelector("#mxPrompterOutline")?.value); case "prompter-save": return prSave(false); case "prompter-save-new": return prSave(true); case "prompter-new": return prNew(); case "manage-practice": return closeSheet(); case "preview-on": return setPreview(true); case "preview-off": return setPreview(false); case "subjects": return drawSubjectPicker(); case "streak": return showStreak(); case "review": return startReview(); case "patterns": return showPatterns(); case "quiz": stopQuizClock(); QZ=null; return quizSetup(); case "quiz-start": return quizStart(); case "quiz-next": return quizGo(QZ.i+1); case "quiz-prev": return quizGo(QZ.i-1); case "quiz-submit": return quizSubmit(); case "quiz-quit": return quizQuit(); case "tasks": return openTasks(); case "grades": return openGrades(); case "uq-start": return uqStart(); case "uq-final": return uqFinalAsk(); case "uq-final-go": return uqStart(true); case "uq-submit": return uqSubmit(); case "review-next": return REV&&reviewNext(); case "cal-prev": return showStreak(-1); case "cal-next": return showStreak(1); case "push": return window.MathlyAnswers?.togglePush?.(); case "sound": Sky.setSound(!Sky.soundOn()); Sky.sound.tap(); return draw(); case "resources": return openResources(t.dataset.unit); case "res-take": case "res-retake": return resPicker.click(); case "res-cancel": RES.photo=null; RES.msg=""; return drawResources(); case "res-visible": return setResourceVisible(t.dataset.res,t.dataset.visible==="1"); case "unit-explanations": return openExplanations(t.dataset.unit); case "res-delete": return deleteResource(t.dataset.res); case "photo": case "retake": return picker.click(); case "hint": PH.shown++; return drawPhoto(); case "work": PH.works.add(+t.dataset.i); return drawPhoto(); case "all": PH.all=true; return drawPhoto(); case "close": return closeSheet(); case "out": return Promise.resolve(window.MathlyAnswers?.forgetPush?.()).finally(()=>{window.MathlyAnswers?.reset();closeSheet();hide();return client?.auth.signOut().then(()=>location.reload());}); } } let MANAGED_CLASSES=[], MANAGE_MODE="classes", MANAGE_BUSY=false; const manageTabs=[ ["classes","כיתות","פתיחה, עריכה ומחיקה"], ["students","תלמידים","רשימה ומסנן כיתות"], ["content","תוכן","מקצועות, יחידות ותת־נושאים"], ["flags","ניטור","מצב המערכת ופעילות"], ["access","הרשאות","תפקידים וגישה"] ]; const examLooks={ "גיאוגרפיה":["🌍","geo"],"אזרחות":["🏛️","civics"],"מבוא מדעי":["🧪","intro"], "מתמטיקה":["∑","math"],"לשון":["✍️","language"],"חינוך פיננסי":["₪","finance"], "מדעים":["🔬","science"],"אנגלית":["A","english"],"ספרות":["📚","literature"], "היסטוריה":["⌛","history"],"תנ״ך":["📜","bible"] }; const examDate=value=>{const [y,m,d]=String(value).split("-").map(Number);return new Date(y,m-1,d)}; const examDay=value=>{const date=examDate(value);return `${String(date.getDate()).padStart(2,"0")}.${String(date.getMonth()+1).padStart(2,"0")}.${date.getFullYear()}`}; const examFullDate=value=>new Intl.DateTimeFormat("he-IL",{weekday:"long",day:"numeric",month:"long",year:"numeric"}).format(examDate(value)); const examMonth=value=>new Intl.DateTimeFormat("he-IL",{month:"long",year:"numeric"}).format(examDate(value)); const classSubjects=row=>[...new Set(G.units.filter(unit=>(row.lessons||[]).includes(unit.id)).map(unit=>unit.subject).filter(Boolean))]; function examCards(exams,nextDate){ return exams.map(exam=>{ const [icon,tone]=examLooks[exam.subject]||["●","other"], next=exam.date===nextDate; return `
    ${esc(exam.subject)}${examDay(exam.date)}${next?" · המבחן הבא":""}
    `; }).join(""); } function examBoard(row,compact=false){ const all=[...(row.exams||[])].filter(exam=>exam?.date&&exam?.subject).sort((a,b)=>a.date.localeCompare(b.date)); const subjects=classSubjects(row), exams=compact?all.filter(exam=>subjects.includes(exam.subject)):all; if(!exams.length)return ""; const today=new Date();today.setHours(0,0,0,0); const upcoming=exams.filter(exam=>examDate(exam.date)>=today), nextDate=upcoming[0]?.date||""; const groups=[];exams.forEach(exam=>{const month=examMonth(exam.date),last=groups[groups.length-1];if(last?.month===month)last.exams.push(exam);else groups.push({month,exams:[exam]});}); const full=`
    ${groups.map(group=>`
    ${esc(group.month)}
    ${examCards(group.exams,nextDate)}
    `).join("")}
    `; const title=esc(compact?`מבחני ${subjects.join(" ו")}`:row.exam_title||`לוח מבחנים – ${row.name}`),note=esc(row.exam_note||""); if(!compact)return `

    ${title}

    ${exams.length} מבחנים
    ${full}${note?`

    ${note}

    `:""}
    `; const next=upcoming[0], days=next?Math.max(0,Math.ceil((examDate(next.date)-today)/86400000)):null; const countdown=next?`
    ${days===0?"היום":days===1?"מחר":days}${days>1?"ימים למבחן":""}
    המבחן הקרוב${esc(next.subject)}
    `:""; return `

    ${title}

    ${upcoming.length?`${upcoming.length} מבחנים בהמשך השנה`:"לוח המבחנים הסתיים"}
    ${countdown}${upcoming.length?`
    ${examCards(upcoming,nextDate)}
    `:""}${note?`

    ${note}

    `:""}
    `; } async function manageClasses(){ if(!can("management.access")) return; closeSheet(); back=document.activeElement; cur="management"; sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-x").focus(); MANAGE_MODE="classes"; await loadManagedClasses(); } async function loadManagedClasses(refresh=false){ if(MANAGE_BUSY||cur!=="management") return; const state=sheet.querySelector(".mx-manage-state"); MANAGE_BUSY=true; state.textContent=refresh?"מרעננים את נתוני הניהול…":"טוענים את הכיתות…"; try{ const response=await api("/api/classes"); if(!response.ok) throw new Error("לא הצלחנו לטעון את הכיתות. נסו שוב."); const {classes}=await response.json(); if(cur!=="management") return; MANAGED_CLASSES=classes||[]; state.textContent=refresh?"הנתונים עודכנו.":""; showManageMode(MANAGE_MODE); }catch(error){state.textContent=error.message;sheet.querySelector(".mx-management").innerHTML=`

    ${esc(error.message)}

    `;} finally{MANAGE_BUSY=false;} } function showManageMode(mode){ if(cur!=="management") return; MANAGE_MODE=mode; sheet.querySelectorAll("[data-manage-mode]").forEach(button=>button.setAttribute("aria-selected",String(button.dataset.manageMode===mode))); const box=sheet.querySelector(".mx-management"); if(mode==="classes") return drawManagedClasses(box); if(mode==="students"){ const students=new Map(); MANAGED_CLASSES.forEach(row=>(row.emails||[]).forEach(email=>students.set(email,[...(students.get(email)||[]),row.name]))); box.innerHTML=`

    תלמידים

    ${students.size} תלמידים · ${MANAGED_CLASSES.length} כיתות
    ${students.size?`
    ${[...students].map(([email,classes])=>`
    ${esc(email)}${esc(classes.join(" · "))}
    `).join("")}
    `:'

    עדיין לא נוספו תלמידים לכיתות.

    '}`; return; } if(mode==="content") return drawContent(box); if(mode==="flags") return drawMonitoring(box); if(mode==="access"){ const roles=CFG.roles||[], permissions=CFG.permissions||[]; box.innerHTML=`

    הרשאות

    תפקידים וגישה
    התפקידים שלך
    ${roles.length?roles.map(role=>`${esc(role)}`).join(""):'מנהל/ת'}
    הרשאות פעילות
    ${permissions.map(permission=>`${esc(permission)}`).join("")}
    `; return; } if(mode==="prompter") return drawPrompter(box); box.innerHTML='

    מעבדה

    כלים לבדיקה ופיתוח

    כלי המעבדה למתמטיקה יתווספו כאן. אפשר לחזור לכל אזור ניהול מהכרטיסיות למעלה.

    '; } /* Prompter (פרומפטר): a developer writes or picks a text and shows it full screen (static/prompter.js). Saved texts are kept with the account (app/prompter.py), so they are on every device; the text being edited is also kept in this browser, so nothing is lost before it is saved. ראשי פרקים: for talking from reminders rather than reading, Claude writes a saved text's outline once (2–4 short topics per slide, app/prompter.py); it is kept with the text and can be edited here. Saving a changed text without touching its outline drops the outline, since it was written from the old text. */ let PR={texts:[],id:null,loaded:false,outlineEdited:false}; function prDraft(){ try{return JSON.parse(localStorage.getItem("mathly.prompterDraft")||"null")||{title:"",body:localStorage.getItem("mathly.prompterText")||"",id:null};}catch{return {title:"",body:"",id:null};} } function prKeep(){ const title=sheet.querySelector("#mxPrompterTitle"), body=sheet.querySelector("#mxPrompterText"), outline=sheet.querySelector("#mxPrompterOutline"); if(!title||!body) return; try{localStorage.setItem("mathly.prompterDraft",JSON.stringify({title:title.value,body:body.value,outline:outline?.value||"",outlineEdited:PR.outlineEdited,id:PR.id}));}catch{} } function prShowOutline(text){ const box=sheet.querySelector(".mx-prompter-outline"), area=sheet.querySelector("#mxPrompterOutline"); if(!box||!area) return; area.value=text||""; box.open=!!text; PR.outlineEdited=false; } function prSay(text){ const el=sheet.querySelector(".mx-prompter-state"); if(el) el.textContent=text; } function prDate(at){ try{return new Date(at).toLocaleString("he-IL",{day:"numeric",month:"numeric",year:"2-digit",hour:"2-digit",minute:"2-digit"});}catch{return "";} } function drawPrompter(box){ const draft=prDraft(); PR.id=draft.id||null; PR.outlineEdited=!!draft.outlineEdited; box.innerHTML=`

    פרומפטר

    טקסט לקריאה מול המצלמה
    ראשי פרקים

    תזכורות קצרות לכל שקף, במקום לקרוא את הטקסט. לחצו „ראשי פרקים” ליד טקסט שמור ו־Claude יכין אותן; אפשר לערוך כאן ולשמור. כותרת שקף בשורה משלה, ואחריה שורות שמתחילות ב־•

    רווח: הפעלה/עצירה · ↑/↓: מהירות · +/−: גודל · M: מראה · F: מסך מלא · Esc: יציאה

    עכבר: לחיצה – הפעלה/עצירה · גלגלת בזמן ריצה – מהירות · לחצן ימני – כמה שורות אחורה · לחצן אמצעי או "אחורה" – להתחלה. שלט מצגות (Page Down/Up) עובד גם הוא.

    טקסטים שמורים

    טוענים…

    `; box.querySelectorAll("#mxPrompterTitle,#mxPrompterText").forEach(el=>el.addEventListener("input",prKeep)); box.querySelector("#mxPrompterOutline").addEventListener("input",()=>{PR.outlineEdited=true; prKeep();}); prMark(); box.querySelector("#mxPrompterText").focus(); prLoad(); } function prMark(){ prSay(PR.id?`עורכים את „${PR.texts.find(t=>t.id===PR.id)?.title||"הטקסט השמור"}”. „שמירה” מעדכנת אותו.`:""); } function prList(){ const list=sheet.querySelector(".mx-prompter-list"); if(!list) return; sheet.querySelector(".mx-prompter-count").textContent=PR.texts.length?`${PR.texts.length} טקסטים`:""; list.innerHTML=PR.texts.length?PR.texts.map(t=>`${esc(t.title)}${esc(prDate(t.updated_at))} · ${t.body.length.toLocaleString("he-IL")} תווים `).join("") :'

    עדיין אין טקסטים שמורים. כתבו טקסט ולחצו „שמירה”.

    '; } async function prLoad(){ try{ const r=await api("/api/prompter/texts"); if(!r.ok) throw new Error(); PR.texts=(await r.json()).texts||[]; PR.loaded=true; if(PR.id&&!PR.texts.some(t=>t.id===PR.id)) PR.id=null; prList(); prMark(); }catch{ const list=sheet.querySelector(".mx-prompter-list"); if(list) list.innerHTML='

    לא הצלחנו לטעון את הטקסטים השמורים. הטקסט שכתבתם נשמר בדפדפן הזה.

    '; } } async function prSave(asNew){ const title=sheet.querySelector("#mxPrompterTitle").value, body=sheet.querySelector("#mxPrompterText").value; if(!body.trim()) return prSay("אין טקסט לשמור."); const id=asNew?null:PR.id; // the outline goes along only when it was edited here; otherwise the server keeps it, or drops it if the text changed const send={title,body}; if(id&&PR.outlineEdited) send.outline=sheet.querySelector("#mxPrompterOutline").value; prSay("שומרים…"); try{ const r=await api(id?`/api/prompter/texts/${id}`:"/api/prompter/texts",{method:id?"PUT":"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(send)}); if(r.status===409) return prSay("יש כבר 200 טקסטים שמורים. מחקו אחד כדי לשמור עוד."); if(r.status===404&&id){ PR.id=null; return prSave(true); } // deleted on another device: keep it as a new one if(!r.ok) throw new Error(); const {text}=await r.json(); PR.texts=[text,...PR.texts.filter(t=>t.id!==text.id)]; PR.id=text.id; sheet.querySelector("#mxPrompterTitle").value=text.title; prShowOutline(text.outline); prKeep(); prList(); prSay(`נשמר: „${text.title}”.`); }catch{ prSay("השמירה נכשלה. הטקסט עדיין כאן ובדפדפן הזה; נסו שוב."); } } function prEdit(id){ const t=PR.texts.find(x=>x.id===id); if(!t) return; PR.id=id; sheet.querySelector("#mxPrompterTitle").value=t.title; sheet.querySelector("#mxPrompterText").value=t.body; prShowOutline(t.outline); prKeep(); prList(); prMark(); sheet.querySelector("#mxPrompterText").focus(); } function prNew(){ PR.id=null; sheet.querySelector("#mxPrompterTitle").value=""; sheet.querySelector("#mxPrompterText").value=""; prShowOutline(""); prKeep(); prList(); prSay(""); sheet.querySelector("#mxPrompterTitle").focus(); } // ראשי פרקים of a saved text: shown at once if it has them, otherwise Claude writes them first (about half a minute) async function prOutline(id,btn){ const t=PR.texts.find(x=>x.id===id); if(!t) return; if(t.outline) return window.MathlyPrompter?.open(t.outline); btn.disabled=true; btn.textContent="מכינים…"; prSay(`Claude מכין ראשי פרקים ל„${t.title}”. זה לוקח כחצי דקה.`); try{ const r=await api(`/api/prompter/texts/${id}/outline`,{method:"POST"}); if(!r.ok) throw new Error(String(r.status)); const {text}=await r.json(); PR.texts=PR.texts.map(x=>x.id===text.id?text:x); if(PR.id===text.id){ prShowOutline(text.outline); prKeep(); } prList(); prSay(`ראשי הפרקים של „${text.title}” מוכנים ונשמרו. אפשר לערוך אותם ב„עריכה”.`); window.MathlyPrompter?.open(text.outline); }catch{ prList(); prSay("לא הצלחנו להכין ראשי פרקים כרגע. נסו שוב בעוד דקה."); } } async function prDelete(id){ const t=PR.texts.find(x=>x.id===id); if(!t||!confirm(`למחוק את „${t.title}”?`)) return; try{ const r=await api(`/api/prompter/texts/${id}`,{method:"DELETE"}); if(!r.ok) throw new Error(); PR.texts=PR.texts.filter(x=>x.id!==id); if(PR.id===id){PR.id=null; prKeep();} prList(); prSay(`„${t.title}” נמחק.`); }catch{ prSay("המחיקה נכשלה. נסו שוב."); } } /* Monitoring (ניטור): a snapshot of the system, open-answer activity, and the server's latest warnings. Each source loads and fails on its own, so a slow Supabase does not hold up the rest. */ const MON_RANGES={day:["יום","24 השעות האחרונות","15 דקות"],week:["שבוע","7 הימים האחרונים","שעה"],month:["חודש","30 הימים האחרונים","יום"]}; const MON_ACTIONS={visit:["👋","נכנס/ה לאפליקציה"],photo:["📷","צילם/ה תרגיל לעזרה"],answer_upload:["📝","שלח/ה תשובה פתוחה"],answer_graded:["✔️","תשובה נבדקה"],class_saved:["🏫","שמר/ה כיתה"],class_deleted:["🗑️","מחק/ה כיתה"],resource_added:["🖼️","הוסיף/ה תמונה למשאבי שיעור"], worksheet_added:["📄","העלה/תה שיעורי בית"],worksheet_submitted:["📬","הגיש/ה שיעורי בית"],worksheet_checked:["✅","בדק/ה שיעורי בית"],worksheet_ai_checked:["🤖","בדק/ה שיעורי בית עם AI"], ai_request:["🙋","ביקש/ה בדיקת AI מהמורה"],ai_sent:["🤖","שלח/ה בקשה של תלמיד ל-AI"],ai_failed:["⚠️","שליחה ל-AI נכשלה"],ai_declined:["✋","דחה/תה בקשת AI"]}; const VERDICTS={correct:"נכונות",partial:"נכונות חלקית",incorrect:"שגויות",unreadable:"לא קריאות",needs_review:"דורשות בדיקה",grading:"בבדיקה",failed:"הבדיקה נכשלה"}; let MON={}, MON_RANGE=(()=>{try{return localStorage.getItem("mathly.monRange")}catch{return null}})(); if(!MON_RANGES[MON_RANGE]) MON_RANGE="week"; const monRel=new Intl.RelativeTimeFormat("he",{numeric:"auto"}); function monAgo(stamp){ const when=new Date(stamp), seconds=Math.max(0,(Date.now()-when.getTime())/1000); if(!stamp||Number.isNaN(when.getTime())) return ""; const [value,unit]=seconds<60?[Math.max(1,Math.floor(seconds)),"second"]:seconds<3600?[Math.floor(seconds/60),"minute"]:seconds<86400?[Math.floor(seconds/3600),"hour"]:seconds<2592000?[Math.floor(seconds/86400),"day"]:[Math.floor(seconds/2592000),"month"]; return ``; } async function monGet(url){ try{ const res=await api(url), data=await res.json().catch(()=>({})); if(!res.ok) throw new Error(data.detail||res.status); return data; } catch(err){ return {error:err.message||"לא זמין"}; } } function drawMonitoring(box){ box.innerHTML=`

    ניטור

    מצב המערכת ופעילות · מתעדכן פעם בדקה
    `; drawMonSummary(); drawMonActivity(); loadMonitoring(); } function loadMonitoring(){ const redraw=draw=>{ if(cur==="management"&&MANAGE_MODE==="flags") draw(); }; monGet("/api/monitor/summary").then(data=>{ MON.summary=data; redraw(drawMonSummary); }); monGet("/api/monitor/supabase").then(data=>{ MON.supabase=data; redraw(drawMonSummary); }); monGet("/api/monitor/ai-usage").then(data=>{ MON.ai=data; redraw(drawMonSummary); }); loadMonActivity().then(()=>{ redraw(drawMonActivity); redraw(drawMonSummary); }); } async function loadMonActivity(){ const range=MON_RANGE, data=await monGet(`/api/monitor/activity?period=${range}`); if(range===MON_RANGE) MON.activity=data; } setInterval(()=>{ if(cur==="management"&&MANAGE_MODE==="flags"&&!document.hidden) loadMonitoring(); },60000); function drawMonSummary(){ const box=sheet.querySelector(".mx-mon-summary"); if(!box) return; const sum=MON.summary, sb=MON.supabase, loading=!sum, err=sum?.error; const tile=(value,label,sub,tone="",cls="")=>`
    ${value}${esc(label)}${sub?`${sub}`:""}
    `; const tiles=[]; { const seen=sum?.seen, n=seen?.last_5m; tiles.push(tile(loading?"…":err?"–":n?`${n} ${n===1?"משתמש/ת":"משתמשים"}`:"אין משתמשים","נכנסו ב־5 הדקות האחרונות", loading?"טוען…":err?esc(err):`${seen.last_hour} בשעה האחרונה · ${seen.last_24h} ב־24 שעות · ${n?"כדאי לחכות עם פריסה":"אפשר לפרוס גרסה חדשה"}`, loading||err?"":n?"busy":"ready","mx-mon-live")); } { const b=sum?.build||{}; tiles.push(tile(loading?"…":monAgo(b.updated_at)||"–","עדכון אחרון",err?esc(err):esc([b.version,b.commit_message].filter(Boolean).join(" · ")))); } { const ph=sum?.photos; tiles.push(tile(loading||err?"–":String(ph.today),"תרגילים מצולמים היום",loading?"":err?esc(err):!sum.photo_help?"עזרה בצילום אינה מוגדרת": `${ph.students} תלמידים · ${ph.at_limit} הגיעו למכסה (${ph.daily_limit} ליום)`,!loading&&!err&&ph.at_limit?"warn":"")); } { const e=sum?.errors||{}, tone=loading||err?"":e.last_hour?"bad":e.last_24h?"warn":"ready"; tiles.push(tile(loading||err?"–":String(e.last_24h??0),"שגיאות בלוג ב־24 השעות האחרונות",loading?"":err?esc(err): `${e.since_start??0} מאז הפעלת השרת${e.last_at?` · אחרונה ${monAgo(new Date(e.last_at*1000).toISOString())}`:""}`,tone)); } { const parts=sb&&!sb.error?[["התחברות",sb.auth||{}],["מסד נתונים",sb.db||{}]]:[]; const down=parts.some(([,x])=>!x.ok), slow=parts.some(([,x])=>x.ok&&x.ms>2000); const sub=!sb?"בודק…":sb.error?esc(sb.error):parts.map(([name,x])=>x.ok?`${name} ${(x.ms/1000).toFixed(1)} ש׳`:`${name}: ${esc(x.error||"לא עונה")}`).join(" · "); tiles.push(tile(!sb?"…":sb.error?"–":down?"לא זמין":slow?"איטי":"תקין","Supabase",sub,!sb||sb.error?"":down?"bad":slow?"warn":"ready")); } { const ai=MON.ai, t=ai?.total, f=ai?.by_feature||{}; const usd=x=>`$${(x||0).toFixed(x>=10?0:2)}`, tok=n=>n>=1e6?`${(n/1e6).toFixed(1)}M`:n>=1e3?`${Math.round(n/1e3)}K`:String(n||0); tiles.push(tile(!ai?"…":t?usd(t.cost_usd):"–",`עלות Claude ב־${ai?.days||30} יום (הערכה)`, !ai?"טוען…":ai.error?esc(ai.error):ai.unavailable?esc(ai.unavailable): `${t.calls} קריאות · ${tok(t.input_tokens)} אסימוני קלט · ${tok(t.output_tokens)} פלט · היום ${usd(ai.today.cost_usd)}`+ ` · צילום ${usd(f.photo?.cost_usd)} · בדיקה ${usd(f.grading?.cost_usd)}`+(f.prompter?` · פרומפטר ${usd(f.prompter.cost_usd)}`:""))); } // open answers: what is switched on, and how this period's photo checks came out const ans=sum?.answers, act=MON.activity, verdicts=act?.summary?.verdicts||{}; let answers; if(loading) answers='

    טוען…

    '; else if(err) answers=`

    לא ניתן לטעון: ${esc(err)}

    `; else{ const flag=(on,yes,no)=>`
  • ${on?yes:no}
  • `; answers=`
      ${flag(ans.storage,"שמירת תשובות מצולמות פעילה","שמירת תשובות מצולמות כבויה")}${flag(!ans.open_ended_locked,"שאלות פתוחות פתוחות לתלמידים","שאלות פתוחות נעולות")}${flag(ans.grading,"בדיקה אוטומטית של תשובות פעילה","בדיקה אוטומטית כבויה")}
    `; const total=Object.values(verdicts).reduce((a,b)=>a+b,0); if(act&&!act.error&&!act.unavailable) answers+=total?`

    ${total} בדיקות ב־${MON_RANGES[MON_RANGE][1]}:

      ${Object.entries(verdicts).sort((a,b)=>b[1]-a[1]).map(([k,n])=>`
    • ${esc(VERDICTS[k]||k)}${n}
    • `).join("")}
    `:`

    אין בדיקות תשובות ב־${MON_RANGES[MON_RANGE][1]}.

    `; } // the last WARNING-or-worse log lines of this server process const lines=sum?.errors?.recent; const logs=loading?'

    טוען…

    ':err?`

    לא ניתן לטעון: ${esc(err)}

    `:!lines?.length?'

    אין אזהרות או שגיאות מאז הפעלת השרת.

    ': lines.map(line=>`
    ${esc(line.level)}${esc(line.logger)}${monAgo(new Date(line.at*1000).toISOString())}
    ${esc(line.message)}
    `).join(""); // the latest actions: what, who, when (kept in server memory since its last start) const acts=sum?.actions; const actions=loading?'

    טוען…

    ':err?`

    לא ניתן לטעון: ${esc(err)}

    `:!acts?.length?'

    עדיין לא נרשמו פעולות מאז הפעלת השרת.

    ': acts.slice(0,10).map(a=>{ const [icon,what]=MON_ACTIONS[a.kind]||["•",a.kind]; return `
    ${esc(what)}${esc(a.email||"משתמש לא ידוע")}${a.label?` · ${esc(a.label)}`:""}
    ${monAgo(new Date(a.at*1000).toISOString())}
    `; }).join(""); box.innerHTML=`
    תמונת מצב
    ${tiles.join("")}
    פעולות אחרונות
    ${actions}
    תשובות פתוחות
    ${answers}
    5 שורות הלוג האחרונות (אזהרה ומעלה)
    ${logs}
    `; } function drawMonActivity(){ const box=sheet.querySelector(".mx-mon-activity"); if(!box) return; const [,title,bucket]=MON_RANGES[MON_RANGE], data=MON.activity; const ranges=Object.entries(MON_RANGES).map(([key,[label]])=>``).join(""); let body; if(!data) body='

    טוען את נתוני הפעילות…

    '; else if(data.error) body=`

    גרף הפעילות אינו זמין: ${esc(data.error)}

    `; else if(data.unavailable) body=`

    גרף הפעילות אינו זמין (${esc(data.unavailable)}).

    `; else{ const rows=data.buckets||[], s=data.summary||{}, avg=s.active_students?Math.round(s.attempts/s.active_students*10)/10:"–"; const stats=[[s.active_students,"תלמידים פעילים"],[s.attempts,"תשובות פתוחות"],[avg,"תשובות לתלמיד/ה"],[s.returning_students,"חזרו ביום נוסף"],[s.active_days,"ימים עם פעילות"]] .map(([n,label])=>`
    ${n??"–"}${label}
    `).join(""); const W=720,H=190,left=32,top=10,width=678,height=145, max=Math.max(1,...rows.map(r=>r.attempts)), maxA=Math.max(1,...rows.map(r=>r.active_students)), bw=width/Math.max(1,rows.length); const days=MON_RANGE==="month", when=t=>new Date(t).toLocaleString("he-IL",days?{day:"numeric",month:"numeric",timeZone:"Asia/Jerusalem"}:MON_RANGE==="week"?{weekday:"short",hour:"2-digit",minute:"2-digit",timeZone:"Asia/Jerusalem"}:{hour:"2-digit",minute:"2-digit",timeZone:"Asia/Jerusalem"}); const tick=(m,f,attrs)=>Number.isInteger(m*f)?`${m*f}`:""; // whole counts only const grid=[0,.5,1].map(f=>{ const y=top+height*(1-f); return `${tick(max,f,`x="0" y="${y+4}"`)}${tick(maxA,f,`class="active-axis" x="${W-2}" y="${y+4}" text-anchor="end"`)}`; }).join(""); const points=[], bars=rows.map((r,i)=>{ const x=left+i*bw, h=r.attempts/max*height; points.push(`${x+bw/2},${top+height-r.active_students/maxA*height}`); return r.attempts?`${esc(when(r.bucket))}: ${r.attempts} תשובות, ${r.active_students} תלמידים`:""; }).join(""); const labels=rows.length?[[0,"start"],[Math.floor((rows.length-1)/2),"middle"],[rows.length-1,"end"]].map(([i,a])=>`${esc(when(rows[i].bucket))}`).join(""):""; body=`
    ${stats}
    תשובות פתוחותתלמידים פעילים
    ${grid}${bars}${labels} ${s.attempts?"":'

    עוד לא נשלחו תשובות פתוחות בטווח הזה.

    '}`; } box.innerHTML=`
    פעילות ב־${title}

    תשובות פתוחות שנשלחו · כל עמודה מייצגת ${bucket}. התרגול עצמו נשמר במכשיר של כל תלמיד/ה ולא נספר כאן.

    ${ranges}
    ${body}`; box.querySelectorAll("[data-mon-range]").forEach(button=>button.onclick=async()=>{ MON_RANGE=button.dataset.monRange; try{localStorage.setItem("mathly.monRange",MON_RANGE)}catch{} MON.activity=null; drawMonActivity(); await loadMonActivity(); drawMonActivity(); drawMonSummary(); }); } // every subject a class meets: those with units, then those that so far only have exams const contentSubjects=()=>[...new Set([...G.units.map(unit=>unit.subject), ...MANAGED_CLASSES.flatMap(row=>(row.exams||[]).map(exam=>exam.subject))].filter(Boolean))]; const unitSkills=id=>ORDER.filter(sid=>BY[sid].unit===id).map(sid=>BY[sid]); const questionCount=skills=>skills.reduce((sum,skill)=>sum+QS(skill).length,0); function drawContent(box){ const subjects=contentSubjects(); box.innerHTML=`

    תוכן לימודי

    ${subjects.length} מקצועות · ${G.units.length} יחידות · ${G.skills.length} תת־נושאים · ${questionCount(G.skills)} שאלות
    ${subjects.map(subject=>{ const units=G.units.filter(unit=>unit.subject===subject), [icon,tone]=examLooks[subject]||["●","other"]; return `
    ${esc(subject)}${units.length?`${units.length} יחידות`:"עוד אין יחידות במסלול"}
    ${units.map(unit=>{ const skills=unitSkills(unit.id), classes=MANAGED_CLASSES.filter(row=>(row.lessons||[]).includes(unit.id)).length; return `
    ${esc(unit.name)} ${unit.when?`${esc(unit.when)} · `:""}${skills.length} תת־נושאים · ${questionCount(skills)} שאלות · פעיל ב־${classes} כיתות
      ${skills.map(skill=>`
    1. ${fmt(skill.name)}${esc(skill.subject||subject)} › ${esc(unit.name)}${skill.month?` · ${esc(skill.month)}`:""} · ${QS(skill).length===1?"שאלה אחת":`${QS(skill).length} שאלות`}
    2. `).join("")}
    `; }).join("")}
    `; }).join("")}`; } function drawManagedClasses(box){ const classes=MANAGED_CLASSES; box.innerHTML=`

    כיתות

    פתיחה, עריכה ומחיקה
    ${classes.length} כיתות
    ${classes.length?`
    ${classes.map(row=>{ const units=G.units.filter(unit=>(row.lessons||[]).includes(unit.id)); const subjects=classSubjects(row); const unitLine=units.length===G.units.length?`כל היחידות (${G.units.length})`:units.map(unit=>unit.name).join(" · ")||"לא נבחרו"; const students=row.emails||[], teachers=row.teachers||[]; return `
    ${esc(row.name)}
    יחידות
    ${esc(unitLine)}
    מקצועות היחידות
    ${esc(subjects.join(" · ")||"לא הוגדרו")}
    תלמידים
    ${students.length?`${students.length}: ${esc(students.join(" · "))}`:"עוד אין"}
    מורה הכיתה
    ${teachers.length?esc(teachers.join(" · ")):"עוד לא נבחר"}
    ${examBoard(row)}
    `; }).join("")}
    `:'
    עוד אין כיתות

    פתחו כיתה ראשונה, בחרו את יחידות הלימוד והוסיפו את כתובות התלמידים.

    '}`; } function manageClassAction(action,id){ if(action==="new") return openClassEditor(null); if(action==="cancel") return drawManagedClasses(sheet.querySelector(".mx-management")); const row=MANAGED_CLASSES.find(item=>String(item.id)===String(id)); if(!row) return; if(action==="edit") return openClassEditor(row); if(action==="delete") return deleteManagedClass(row); if(action==="report") return classReport(row.id); } function openClassEditor(row){ const box=sheet.querySelector(".mx-management"), creating=!row; const selected=new Set(row?.lessons||[]); // a new class starts with no subject chosen const subjects=[...new Set(G.units.map(unit=>unit.subject))], subjectUnits=subject=>G.units.filter(unit=>unit.subject===subject); box.innerHTML=`

    ${creating?"כיתה חדשה":`עריכת ${esc(row.name)}`}

    מקצועותבחרו את המקצועות שהכיתה לומדת. בחירת מקצוע מסמנת את כל יחידות הלימוד שלו, ואפשר לבטל יחידות שהכיתה לא לומדת.
    ${subjects.map(subject=>{const [icon,tone]=examLooks[subject]||["●","other"], units=subjectUnits(subject); return ``}).join("")}
    יחידות הלימודהתלמידים יראו במסלול רק את המיומנויות של היחידות שסומנו. מקצוע היחידה קובע אילו מבחנים יוצגו במסלול.

    בחרו מקצוע כדי לראות את יחידות הלימוד שלו.

    ${subjects.map(subject=>`
    ${esc(subject)}
    ${subjectUnits(subject).map(unit=>{const skills=unitSkills(unit.id);return ``}).join("")}
    `).join("")}
    `; const form=box.querySelector("form"), message=form.querySelector('[role="status"]'); const unitBoxes=subject=>[...form.querySelectorAll('input[name="unit"]')].filter(input=>input.dataset.subject===subject); // a subject is chosen while any of its units is; a subject's units stay in view from choosing it until it is cleared const open=new Set(subjects.filter(subject=>subjectUnits(subject).some(unit=>selected.has(unit.id)))); const syncSubjects=()=>{ form.querySelectorAll('input[name="subject"]').forEach(input=>{ const boxes=unitBoxes(input.value), on=boxes.filter(box=>box.checked).length; input.checked=on>0; input.indeterminate=on>0&&on0; }; form.addEventListener("change",event=>{ const input=event.target; if(input.name==="subject"){ unitBoxes(input.value).forEach(box=>box.checked=input.checked); if(input.checked) open.add(input.value); else open.delete(input.value); } if(input.name==="subject"||input.name==="unit") syncSubjects(); }); syncSubjects(); const lines=name=>form.elements[name].value.split(/[\n,;]+/).map(value=>value.trim().toLowerCase()).filter(Boolean); const pickTeacher=form.elements.teacher; form.querySelector('input[name="name"]').focus(); form.onsubmit=async event=>{ event.preventDefault(); const lessons=[...form.querySelectorAll('input[name="unit"]:checked')].map(input=>input.value); if(!lessons.length){message.textContent="בחרו לפחות מקצוע אחד.";return;} const teacher=pickTeacher.value.trim().toLowerCase(), students=lines("emails"); if(teacher&&!teacher.includes("@")){message.textContent="כתבו כתובת דוא״ל תקינה למורה.";return;} if(students.includes(teacher)){message.textContent="מורה לא יכול להיות גם תלמיד בכיתה. הסירו אותו מרשימת התלמידים.";return;} const body={name:form.elements.name.value.trim(),emails:students,teachers:teacher?[teacher]:[],lessons}; const buttons=form.querySelectorAll("button"); buttons.forEach(button=>button.disabled=true); message.textContent=creating?"פותחים את הכיתה…":"שומרים…"; try{ const response=await api(creating?"/api/classes":`/api/classes/${encodeURIComponent(row.id)}`,{method:creating?"POST":"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(body)}); if(!response.ok){const detail=(await response.json().catch(()=>({}))).detail;throw new Error(detail||"לא הצלחנו לשמור את הכיתה. נסו שוב.");} if(!creating){ Object.assign(row,body); MY_CLASSES.forEach(item=>{if(String(item.id)===String(row.id))Object.assign(item,body,{teacher:body.teachers.includes(ME_EMAIL.toLowerCase())});}); if(String(MY_CLASS?.id)===String(row.id))Object.assign(MY_CLASS,body); SHOWN=new Set(shownUnits().map(unit=>unit.id)); draw(); } await loadManagedClasses(true); }catch(error){message.textContent=error.message;buttons.forEach(button=>button.disabled=false);} }; } async function deleteManagedClass(row){ if(!confirm(`למחוק את הכיתה ${row.name}? התלמידים שלה לא יוכלו להיכנס עוד.`)) return; const state=sheet.querySelector(".mx-manage-state"); state.textContent=`מוחקים את ${row.name}…`; try{ const response=await api(`/api/classes/${encodeURIComponent(row.id)}`,{method:"DELETE"}); if(!response.ok){const detail=(await response.json().catch(()=>({}))).detail;throw new Error(detail||"לא הצלחנו למחוק את הכיתה.");} MANAGED_CLASSES=MANAGED_CLASSES.filter(item=>String(item.id)!==String(row.id)); MY_CLASSES=MY_CLASSES.filter(item=>String(item.id)!==String(row.id)); if(String(MY_CLASS?.id)===String(row.id)) MY_CLASS=MY_CLASSES.length===1?MY_CLASSES[0]:null; SHOWN=new Set(shownUnits().map(unit=>unit.id)); draw(); state.textContent="הכיתה נמחקה."; drawManagedClasses(sheet.querySelector(".mx-management")); }catch(error){state.textContent=error.message;} } /* ---------- the class teacher's report ---------- A class's teacher is one of its students, chosen in the class editor. That account (and a manager, from the class's card) opens the class's report (/api/classes/{id}/report, app/class_report.py): each student's saved progress, read the same way the student's own page reads it. It shows, per student, the skills finished in the class's units, the mistakes that keep coming back (Sky.patterns), the summarizing quizzes' grades and the practice exams' results, and above them the mistakes most common in the class. */ const teachClasses=()=>MY_CLASSES.filter(row=>row.teacher); // "view as student": the teacher sees the home screen the way a student of the class does, until they switch back const previewBar=()=>PREVIEW?`
    מצב תצוגה כתלמיד
    `:""; function setPreview(on){ PREVIEW=on; closeSheet(); if(hasExams()) drawSubjectPicker(); else selectSubject("מתמטיקה"); window.scrollTo?.(0,0); } const teachButton=()=>CFG.aiReview?``:""; // the teacher's classes, at the top of the home screen: each opens its report (students, progress, assignments) function classesCard(){ const mine=PREVIEW?[]:teachClasses(); if(!mine.length) return ""; return `

    הכיתות שלי

    ${mine.map(row=>``).join("")}
    ${CFG.aiLeft!=null?`

    ${CFG.aiLeft?`נשארו לכם היום ${CFG.aiLeft===1?"בקשת AI אחת":`${CFG.aiLeft} בקשות AI`} מתוך 3.`:"השתמשתם היום בכל 3 בקשות ה-AI. אפשר שוב מחר."}

    `:""}
    `; } const aiUsed=()=>{ if(CFG.aiLeft!=null) CFG.aiLeft=Math.max(0,CFG.aiLeft-1); }; let REPORT=null; const shortDate=at=>{ const d=new Date(at); return isNaN(d)?"":d.toLocaleDateString("he-IL",{day:"numeric",month:"numeric"}); }; const pct=(a,b)=>b?Math.round(100*a/b):0; // one student's numbers, from their saved progress (null: nothing saved yet) function reportOf(student,units){ const p=student.progress; if(!p) return null; const perUnit=units.map(u=>{ const ids=unitIds(u).filter(live); return {u,n:ids.length,done:ids.filter(id=>p.done?.[id]).length, gold:ids.filter(id=>p.gold?.[id]).length,stuck:ids.filter(id=>!p.done?.[id]&&(p.missed?.[id]||[]).length).map(id=>BY[id])}; }); const n=perUnit.reduce((s,x)=>s+x.n,0), done=perUnit.reduce((s,x)=>s+x.done,0); const {recurring}=Sky.patterns(p,G.mistakes); // the unit quizzes' grades are the server's (student.quiz_sittings, oldest first; null: they could not be read); the // device's own history only says which submissions the server has not seen ("unverified") const sittings=student.quiz_sittings; const quizzes=units.filter(u=>u.quiz).map(u=>{ const st=p.uq?.[u.id]||{history:[]}, mine=(sittings||[]).filter(x=>x.unit_id===u.id), known=new Set(mine.map(x=>x.id)); // the official grade is the latest final sitting's (app/quiz_modes.py); practice sittings never make it const official=mine.filter(x=>x.mode==="final").at(-1)||null, practice=mine.filter(x=>x.mode!=="final").at(-1)||null; return {u,st,last:mine.at(-1)||null,official,practice,count:mine.length,unverified:st.history.filter(h=>!h.sid||!known.has(h.sid)).length}; }); const officials=quizzes.map(x=>x.official?.grade).filter(g=>g!=null); const scores=officials.length?officials:quizzes.map(x=>x.last?.grade).filter(g=>g!=null); const exams=(p.quizzes||[]).slice().reverse(); const days=Object.keys(p.days||{}).sort(), last=[p.updated?new Date(+p.updated):null,days.length?new Date(days.at(-1)):null].filter(d=>d&&!isNaN(d)).sort((a,b)=>b-a)[0]; // worked examples the student opened in the last two weeks, in the class's skills (the device reports them) const classSkills=new Set(units.flatMap(u=>unitIds(u).filter(live))), since=Date.now()-14*864e5; const helped=Object.entries(p.examples||{}).filter(([id,e])=>classSkills.has(id)&&BY[id]&&Date.parse(e?.last)>=since) .map(([id,e])=>({id,n:+e.n||1,last:e.last})).sort((a,b)=>b.last.localeCompare(a.last)); return {perUnit,n,done,helped,week:Sky.weekly(p),recurring:recurring.filter(x=>G.mistakes[x.m]),quizzes,quizzesKnown:sittings!=null,quizAvg:scores.length?Math.round(scores.reduce((a,b)=>a+b,0)/scores.length):null,quizAvgOfficial:officials.length>0,exams,last}; } async function classReport(id){ const mine=teachClasses(), classId=id??REPORT?.id??mine[0]?.id; if(classId==null) return; if(!cur) back=document.activeElement; closeSheetQuietly(); cur="report"; REPORT={id:classId,data:null,form:null,tab:repGet("tab","list"),sort:repGet("sort","attention"),unit:"overview",open:repGet("mapMonths",null)}; const tabs=mine.length>1?`
    ${mine.map(row=>``).join("")}
    `:""; sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-x").focus({preventScroll:true}); try{ const r=await api(`/api/classes/${encodeURIComponent(classId)}/report`,{cache:"no-store"}); if(!r.ok) throw Error(r.status===403?"רק מורה הכיתה יכול לראות את הנתונים שלה.":"לא הצלחנו לטעון את נתוני הכיתה. נסו שוב."); const data=await r.json(); if(cur!=="report"||REPORT?.id!==classId) return; REPORT.data=data; drawReport(); }catch(e){ if(cur==="report") sheet.querySelector(".mx-manage-state").textContent=e.message; } } // a student named in the attention list: their card opens and scrolls into view function reportOpenStudent(i){ const card=sheet.querySelector(`#rs-${+i}`); if(!card) return; card.open=true; card.scrollIntoView({block:"nearest"}); card.querySelector("summary").focus({preventScroll:true}); } function closeSheetQuietly(){ const b=back; closeSheet(); back=b; } // the teacher's choices in the report (which tab, the order) are kept on this device const repGet=(k,def)=>{ try{ return localStorage.getItem("mathly.report."+k)||def; }catch{ return def; } }; const repSet=(k,v)=>{ try{ localStorage.setItem("mathly.report."+k,v); }catch{} }; const REPORT_SORTS={attention:"דורשים תשומת לב קודם",name:"שם",progress:"התקדמות",activity:"פעילות אחרונה (הישנה קודם)"}; // how a skill stands for a student, as on their own path: gold, lit, fading (finished), trying (practised, not finished), none function cellState(p,id){ if(!p) return "none"; if(p.done?.[id]) return Sky.star(p,id,"done",false); return p.tries?.[id]>0||(p.missed?.[id]||[]).length?"trying":"none"; } const CELL_TEXT={gold:"כוכב זהב",lit:"הושלמה",fading:"הושלמה, הכוכב דועך",trying:"מתרגל, עוד לא סיים",none:"לא התחיל"}; const HARD_SHARE=1/3; // a skill is a class-wide difficulty when this share of the students are on it without finishing // a unit's share of finished skills as a heat level: 0 not started, 1 under a third, 2 under two thirds, 3 under all, 4 all const heatLevel=(done,n,started)=>!n||(!done&&!started)?0:done>=n?4:done*3REPORT.open==null?new Set([new Date().toLocaleDateString("he-IL",{month:"long",timeZone:"Asia/Jerusalem"})]) :new Set(REPORT.open.split("|").filter(Boolean)); function drawReport(){ const {class:row,students}=REPORT.data, ids=new Set(row.lessons||[]); const units=G.units.filter(u=>ids.has(u.id)&&unitIds(u).some(live)); const skillNames={}; units.forEach(u=>unitIds(u).filter(live).forEach(id=>{ skillNames[id]=BY[id].name; })); const ctx={skills:skillNames,units:units.filter(u=>u.quiz).map(u=>({id:u.id,name:u.name})),assignments:REPORT.data.assignments||[]}; const all=students.map(s=>({s,r:reportOf(s,units),att:Sky.attention(s,ctx)})); sheet.querySelector("#mxTitle").textContent=`הכיתה שלי: ${row.name}`; sheet.querySelector(".mx-manage-state").textContent=`${students.length} תלמידים · ${all.filter(x=>x.r).length} התחילו לתרגל`; // the mistakes that keep coming back for the most students const common={}; all.forEach(({s,r})=>r?.recurring.forEach(x=>(common[x.m]||=[]).push(s.email))); const top=Object.entries(common).sort((a,b)=>b[1].length-a[1].length).slice(0,5); // a student is shown by the class's nickname, else their Google name, else the start of their email const name=email=>students.find(x=>x.email===email)?.name||email.split("@")[0]; const quizUnits=units.filter(u=>u.quiz); const DEVICE_NOTE=' · נתוני תרגול מהמכשיר'; const grade=score=>score==null?"–":`${score}`; const tone=score=>score==null?"":score>=80?" good":score>=55?" mid":" low"; // a student's state at a glance: stuck on something, active this week, or quiet; and the last 14 days as small bars const dayKey=d=>d.toLocaleDateString("en-CA",{timeZone:"Asia/Jerusalem"}); const status=r=>{ const week=Date.now()-7*864e5; return r.perUnit.some(x=>x.stuck.length)?['low',"תקוע","יש לתלמיד לפחות מיומנות אחת שלא הושלמה ושיש בה שאלות שנענו לא נכון ועדיין לא נפתרו. זה לא ציון: אין סף, ומספיק שאלה אחת. נתוני המכשיר של התלמיד, והשרת לא בודק אותם."]:r.last&&r.last.getTime()>=week?['good',"פעיל"]:['mid',"לא פעיל"]; }; const spark=s=>{ const days=s.progress?.days||{}, now=Date.now(); const bars=Array.from({length:14},(_,k)=>days[dayKey(new Date(now-(13-k)*864e5))]||0), top=Math.max(5,...bars); return `${bars.map(n=>``).join("")}`; }; const card=({s,r,att},i)=>{ if(!r) return `
    ${esc(name(s.email))}${s.signed_in?"עוד לא התחיל לתרגל":"עוד לא נכנס לחשבון"}

    ${esc(s.email)}

    ${focusOf(s,units).length?focusCardHtml(s,{recurring:[]},units):""}
    `; const exam=r.exams[0]; const [stTone,stText,stHint]=status(r); return `
    ${esc(name(s.email))}${stText} ${r.done}/${r.n} מיומנויות${spark(s)} ${r.quizAvgOfficial?"מבחן":"בוחן"} ${grade(r.quizAvg)} מבחן תרגול ${exam?`${exam.right}/${exam.n}`:"–"} ${r.recurring.length?`${r.recurring.length} קשיים`:""} ${(s.review_requests||[]).length?`ביקש בדיקה חוזרת`:""}

    ${esc(s.email)}${r.last?` · פעילות אחרונה ${r.last.toLocaleDateString("he-IL")}`:""}

    ${focusCardHtml(s,r,units)} ${(s.review_requests||[]).length?`
    ביקש שתסתכלו שוב
    ${s.review_requests.map(q=>`

    ${fmt(q.question||"")}${q.message?`: ${esc(q.message)}`:""}

    `).join("")}
    `:""} ${r.helped.length?`
    דוגמאות פתורות שנפתחו בשבועיים האחרונים${DEVICE_NOTE}
      ${r.helped.map(x=> `
    • ${fmt(BY[x.id].name)} ${x.n===1?"פעם אחת":`${x.n} פעמים`}, לאחרונה ${shortDate(x.last)}
    • `).join("")}
    `:""}

    ${r.week.thisWeek} תשובות נכונות השבוע · ${r.week.lastWeek} בשבוע שעבר

    ${focusOfHtml(s)}
    התקדמות ביחידות${DEVICE_NOTE}
    ${r.perUnit.filter(x=>x.done||x.gold||x.stuck.length).map(x=>`
    ${esc(x.u.name)}${x.gold?` · ${x.gold} זהב`:""}${x.done}/${x.n}
    `).join("")||'

    עוד אין פעילות ביחידות.

    '}
    ${r.perUnit.some(x=>!x.done&&!x.gold&&!x.stuck.length)?`

    עוד ${r.perUnit.filter(x=>!x.done&&!x.gold&&!x.stuck.length).length} יחידות בלי פעילות

    `:""}
    קשיים${DEVICE_NOTE}
    ${r.recurring.length?`
    ${r.recurring.map(x=>`${fmt(G.mistakes[x.m].name)} ×${x.n}`).join("")}

    ${r.recurring.map(x=>`${fmt(G.mistakes[x.m].name)}: ${x.n} פעמים ב־${x.skills.length} מיומנויות, לאחרונה ${shortDate(x.last)}${BY[x.practise]?`. לחזק: ${fmt(BY[x.practise].name)}`:""}`).join("
    ")}

    `:'

    אין טעויות שחוזרות לאחרונה.

    '} ${r.perUnit.some(x=>x.stuck.length)?`

    מיומנויות עם שאלות שעוד לא נפתרו: ${r.perUnit.flatMap(x=>x.stuck).slice(0,8).map(sk=>fmt(sk.name)).join(" · ")}

    `:""}
    ${TERMS.unitQuizzes}
    ${quizUnits.length?`${r.quizzes.map(({u,st,last,official,practice,count,unverified})=>{ const g=x=>x?(x.grade==null?"ממתין לבדיקה":`${x.grade}/100`):"–"; const cell=!r.quizzesKnown?"לא זמין":g(official); const prac=st.going?"בתהליך":!r.quizzesKnown?"לא זמין":practice?g(practice):unverified?"לא מאומת":"–"; const subs=`${count||(unverified?"":"–")}${unverified&&r.quizzesKnown?`${count?" ":""}(${unverified} לא מאומתות)`:""}`; return ``; }).join("")}
    יחידהציון היחידה (בוחן מסכם)בוחן אחרוןהגשותתאריךתשובות
    ${esc(u.name)}${cell}${prac}${subs}${last?shortDate(last.submitted_at):"–"}${last?``:""}
    `:'

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

    '}
    מבחני תרגול${DEVICE_NOTE}
    ${r.exams.length?`${r.exams.slice(0,10).map(q=>``).join("")}
    תאריךציוןתשובות נכונותזמן
    ${shortDate(q.at)}${pct(q.right,q.n)}${q.right}/${q.n}${q.ms?`${Math.round(q.ms/60000)} דק׳`:"–"}
    `:'

    עוד לא נעשו מבחני תרגול.

    '}
    `; }; // the order the teacher chose; ties keep the class list's order. `i` is the student's place in the class list (their card's id) const rank={attention:(a,b)=>b.att.length-a.att.length, name:(a,b)=>name(a.s.email).localeCompare(name(b.s.email),"he"), progress:(a,b)=>(b.r?b.r.done:-1)-(a.r?a.r.done:-1), activity:(a,b)=>(a.r?.last?.getTime()??0)-(b.r?.last?.getTime()??0)}; const sort=rank[REPORT.sort]?REPORT.sort:"attention"; const order=all.map((x,i)=>({...x,i})).sort(rank[sort]); const needy=order.filter(x=>x.att.length); const attn=`

    דורשים תשומת לב

    ${needy.length?`
      ${needy.map(({s,att,i})=> `
    • ${att.map(x=>fmt(x.text)).join(" · ")}
    • `).join("")}
    ` :'

    אף תלמיד לא דורש תשומת לב כרגע.

    '}
    `; const list=`${attn} ${top.length?`

    קשיים נפוצים בכיתה

      ${top.map(([m,who])=>`
    • ${fmt(G.mistakes[m].name)} ${who.length} תלמידים: ${who.map(e=>esc(name(e))).join(", ")}
    • `).join("")}
    `:""}

    תלמידים

    ${students.length?order.map(x=>card(x,x.i)).join(""):'

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

    '}`; // the map: a row per student; a column per unit, or (one unit chosen) per skill. The colour says how far the student is, // the red mark says they are stuck on something there. It is the device's data, as the rest of the progress const mapUnit=units.find(u=>u.id===REPORT.unit); const started=all.filter(x=>x.r).length; let map; if(!students.length) map='

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

    '; else if(!mapUnit){ // two levels: the units are grouped by the month the syllabus teaches them in; a month's column opens into its units const monthOf=u=>(u.when||"ללא מועד").split(/[–-]/)[0].trim(); const groups=[]; units.forEach((u,k)=>{ const key=monthOf(u); let g=groups.find(x=>x.key===key); if(!g) groups.push(g={key,ks:[]}); g.ks.push(k); }); const openG=heatOpen(), isOpen=g=>openG.has(g.key), rows=groups.some(isOpen)?2:1; const stuckIn=ks=>order.filter(({r})=>r&&ks.some(k=>r.perUnit[k].stuck.length)).length; const hard=ks=>stuckIn(ks)>0&&stuckIn(ks)*3>=Math.max(started,1); const cell=(s,r,ks,title)=>{ if(!r) return ``; const done=ks.reduce((t,k)=>t+r.perUnit[k].done,0), n=ks.reduce((t,k)=>t+r.perUnit[k].n,0), stuck=ks.reduce((t,k)=>t+r.perUnit[k].stuck.length,0); const label=`${title}: ${done} מתוך ${n} מיומנויות${stuck?`, תקוע ב־${stuck}`:""}`; return `${done?`${pct(done,n)}%`:""}`; }; const head1=groups.map(g=>isOpen(g) ?`` :``).join(""); const head2=rows===2?`${groups.filter(isOpen).flatMap(g=>g.ks).map(k=> ``).join("")}`:""; map=`${head1}${head2}${ order.map(({s,r})=>`${groups.map(g=>isOpen(g)?g.ks.map(k=>cell(s,r,[k],units[k].name)).join(""):cell(s,r,g.ks,g.key)).join("")}`).join("")}
    תלמיד
    ${esc(name(s.email))}

    לחיצה על חודש פותחת אותו ליחידות שלו, ולחיצה על שם יחידה פותחת אותה לפי מיומנויות. נקודה אדומה: התלמיד תקוע בלפחות מיומנות אחת.

    `; }else{ const skills=unitIds(mapUnit).filter(live); const count=skills.map(id=>{ const st=all.map(x=>cellState(x.s.progress,id)); const trying=st.filter(t=>t==="trying").length; return {id,done:st.filter(t=>["gold","lit","fading"].includes(t)).length,trying,hard:trying>0&&trying>=started*HARD_SHARE}; }); map=`${count.map(c=> ``).join("")}${ order.map(({s})=>`${skills.map(id=>{ const st=cellState(s.progress,id); return ``; }).join("")}`).join("")}${count.map(c=>``).join("")}${count.map(c=>``).join("")}
    תלמיד${c.hard?"⚠ ":""}${fmt(BY[c.id].short||BY[c.id].name)}
    ${esc(name(s.email))}
    סיימו${c.done}/${started}
    מתקשים${c.trying||""}

    ⚠ מיומנות שלפחות שליש מהתלמידים מתרגלים ועוד לא סיימו.

    `; } const mapLegend=mapUnit?"":``; const view=["map","tasks","subs","exams","focus","finals","inbox"].includes(REPORT.tab)?REPORT.tab:"list"; const bar=`
    ${view==="map"&&!mapUnit?``:""} ${view==="map"?``:""}
    `; sheet.querySelector(".mx-report").innerHTML=bar+(view==="map"?`${mapLegend}
    ${map}
    `:view==="tasks"||view==="subs"?(REPORT.sub?wsSubHtml(name):view==="subs"?subsTabHtml(order,name):REPORT.form?(REPORT.form.focus?focusFormHtml(units,name):asgFormHtml(units)):asgTasksHtml(order,name)) :view==="inbox"?(REPORT.sub?wsSubHtml(name):inboxHtml(name)) :view==="exams"?examsTabHtml(units,all,name):view==="focus"?classFocusHtml(units,all,name):view==="finals"?finalsTabHtml(units,name):list); if((view==="tasks"||view==="subs"||view==="inbox")&&REPORT.sub) wsSubImages(); if(REPORT.flash) sheet.querySelector(".mx-report").insertAdjacentHTML("afterbegin",`

    ${esc(REPORT.flash)}

    `); // until the next tab } /* ---------- the teacher's inbox (GET /api/teacher/inbox) ---------- Everything of this class that waits for the teacher, oldest first: worksheets to check, quiz answers that need the teacher, requests to look again. Filters by kind, state and assignment or quiz; saving a check opens the next one. */ let INBOX_COUNT=null; async function inboxCount(){ try{ const r=await api("/api/teacher/inbox",{cache:"no-store"}); if(!r.ok) return; const d=await r.json(); INBOX_COUNT={}; for(const e of d.entries) INBOX_COUNT[e.class_id]=(INBOX_COUNT[e.class_id]||0)+1; }catch{ return; } if(ACTIVE_SUBJECT==="מתמטיקה"&&root&&!root.hidden&&!cur) draw(); } const INBOX_KIND={worksheet:"שיעורי בית",quiz:"תשובה בבוחן",request:"בקשה לבדיקה חוזרת"}; const INBOX_STATE={submitted:"ממתין לבדיקה",waiting:"עוד לא נבדק",unreadable:"ה־AI לא הצליח לקרוא",needs_review:"ה־AI ביקש מורה",ai_to_confirm:"ציון AI לאישור",request:"התלמיד/ה ביקש/ה"}; const inboxKey=e=>`${e.kind}:${e.submission_id||e.attempt_id}`; const inboxItem=e=>e.assignment_id?`a:${e.assignment_id}`:e.window_id?`w:${e.window_id}`:e.unit_id?`u:${e.unit_id}`:""; async function inboxLoad(){ const id=REPORT.id; try{ const r=await api(`/api/teacher/inbox?class_id=${encodeURIComponent(id)}`,{cache:"no-store"}); const d=await r.json().catch(()=>({})); if(REPORT?.id!==id) return; REPORT.inboxData=r.ok?d:{error:true,entries:[]}; (INBOX_COUNT||={})[id]=r.ok?d.entries.length:0; } catch{ if(REPORT?.id===id) REPORT.inboxData={error:true,entries:[]}; } if(cur==="report"&&REPORT.tab==="inbox") drawReport(); } function inboxList(){ const f=REPORT.inboxFilter||={}, done=REPORT.inboxDone||=new Set(); return (REPORT.inboxData?.entries||[]).filter(e=>!done.has(inboxKey(e))&&(!f.kind||e.kind===f.kind)&&(!f.state||e.state===f.state)&&(!f.item||inboxItem(e)===f.item)); } function inboxHtml(name){ if(!REPORT.inboxData){ inboxLoad(); return '

    טוענים…

    '; } if(REPORT.inboxData.error) return '

    לא הצלחנו לטעון את מה שממתין לבדיקה.

    '; const all=REPORT.inboxData.entries, list=inboxList(), f=REPORT.inboxFilter; const items=new Map(all.filter(inboxItem).map(e=>[inboxItem(e),e.assignment||e.title.replace(/\s*·\s*שאלה\s*\d+$/,"")])); const opt=(key,value,label)=>``; return `
    ${REPORT.inboxData.unavailable?.length?'

    חלק מהרשימה לא נטען עכשיו.

    ':""} ${list.length?`

    ${list.length} ממתינים לכם, הישן ביותר קודם. שמירה עוברת לבא בתור.

      ${list.map(e=>`
    1. ${esc(name(e.student_email))} · ${esc(INBOX_KIND[e.kind])} ${fmt(e.title||"")}${e.assignment?` (${esc(e.assignment)})`:""}${e.version>1?" · מתוקן":""}

      ${esc(shortDate(e.submitted_at))}${e.late?" · באיחור":""} · ${esc(INBOX_STATE[e.state]||e.state)}${e.note?` · "${esc(e.note)}"`:""}${e.message?` · "${esc(e.message)}"`:""}

    2. `).join("")}
    ` :`

    ${all.length?"אין מה לבדוק בסינון הזה.":"אין מה לבדוק. 🎉"}

    `}`; } function inboxOpen(key){ const e=(REPORT.inboxData?.entries||[]).find(x=>inboxKey(x)===key); if(!e) return; REPORT.inbox={key}; if(e.kind==="worksheet"){ REPORT.sub={wid:e.worksheet_id,email:e.student_email,urls:[]}; drawReport(); return sheet.querySelector(".mx-ws-review [data-ws-sub-back]")?.focus(); } const id=REPORT.id; trOpenAnswer(e.attempt_id,"inbox"); if(TR) TR.inbox={key,classId:id}; } // the report's frame again, around the data it already has (back from a sheet opened over it) function drawReportShell(){ const mine=teachClasses(), classId=REPORT.id; const tabs=mine.length>1?`
    ${mine.map(row=>``).join("")}
    `:""; cur="report"; sheet.innerHTML=`
    `; sheet.hidden=false; } // back from a quiz answer to the class's inbox (the report, kept as it was); `saved`: on to the next one function inboxBack(saved=false){ const kept=REPORT; dropReview(); if(!kept) return closeSheet(); REPORT=kept; REPORT.tab="inbox"; cur=null; drawReportShell(); if(saved) return inboxAfterSave(); REPORT.inbox=null; drawReport(); } // after a save: this one is done, and the next one of the same filter opens (or the list, when none is left) function inboxAfterSave(){ const key=REPORT.inbox?.key; if(key) REPORT.inboxDone.add(key); const next=inboxList()[0]; wsSubClose(); REPORT.inbox=null; if(INBOX_COUNT&&REPORT.id in INBOX_COUNT) INBOX_COUNT[REPORT.id]=Math.max(0,INBOX_COUNT[REPORT.id]-1); if(next) return inboxOpen(inboxKey(next)); drawReport(); sheet.querySelector(".mx-report").insertAdjacentHTML("afterbegin",'

    סיימתם את כל מה שממתין בסינון הזה. 🎉

    '); } /* ---------- final unit quizzes, in the report (app/quiz_modes.py) ---------- The class teacher opens a final quiz of a unit for a time, closes it early, gives one student another sitting, and releases the results. How far each window is comes from the students' sittings in the report (their window_id). */ const localInput=d=>{ const z=new Date(d.getTime()-d.getTimezoneOffset()*60000); return z.toISOString().slice(0,16); }; const whenShort=t=>new Date(t).toLocaleString("he-IL",{day:"numeric",month:"numeric",hour:"2-digit",minute:"2-digit"}); async function finalsLoad(){ const id=REPORT.id; try{ const r=await api(`/api/classes/${encodeURIComponent(id)}/quiz-windows`,{cache:"no-store"}); const d=await r.json().catch(()=>({})); if(REPORT?.id!==id) return; REPORT.finals=r.ok?d:{error:d.detail||"error"}; } catch{ if(REPORT?.id===id) REPORT.finals={error:"error"}; } if(cur==="report"&&REPORT.tab==="finals") drawReport(); } function finalStats(w){ const sittings=REPORT.data.students.flatMap(s=>(s.quiz_sittings||[]).filter(x=>x.window_id===w.id).map(x=>({s,x}))); const started=(REPORT.finals?.starts||[]).filter(x=>x.window_id===w.id).length; return {started,handed:sittings.length,checked:sittings.filter(({x})=>x.grade!=null).length,sittings}; } function finalsTabHtml(units,name){ if(!REPORT.finals){ finalsLoad(); return '

    טוענים…

    '; } if(REPORT.finals.error) return `

    ${REPORT.finals.error==="quiz_modes_unavailable"?"מבחנים אינם זמינים עדיין באתר.":"לא הצלחנו לטעון את המבחנים."}

    `; const quizUnits=units.filter(u=>u.quiz), now=Date.now(), start=new Date(Math.ceil(now/300000)*300000); const windows=(REPORT.finals.windows||[]).slice().sort((a,b)=>b.opens_at.localeCompare(a.opens_at)); const students=REPORT.data.students; const card=w=>{ const u=G.units.find(x=>x.id===w.unit_id), st=finalStats(w), opens=Date.parse(w.opens_at), closes=Date.parse(w.closes_at); const state=nowg.window_id===w.id); return `

    ${esc(u?.name||w.unit_id)} · ${state}

    ${esc(whenShort(w.opens_at))} עד ${esc(whenShort(w.closes_at))} · התחילו ${st.started} · הגישו ${st.handed} מתוך ${students.length} · נבדקו במלואם ${st.checked} ${w.released_at?` · התוצאות פורסמו ${esc(whenShort(w.released_at))}`:""}

    ${state!=="נסגר"?``:""} ${w.released_at?"":``} ${st.handed?``:""}

    ${grants.length?`

    ניסיון נוסף: ${grants.map(g=>`${esc(name(g.student_email))} עד ${esc(whenShort(g.until))}${g.reason?` (${esc(g.reason)})`:""}`).join(" · ")}

    `:""}
    ניסיון נוסף לתלמיד/ה
    `; }; return `

    בוחן מסכם נעשה פעם אחת, והציון שלו הוא הציון של היחידה. בחני חיזוק לא משנים אותו.

    ${quizUnits.length?`

    פתיחת בוחן מסכם

    `:'

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

    '} ${windows.map(card).join("")||'

    עוד לא נפתחו מבחנים.

    '}`; } const FINAL_ERR={window_already_open:"כבר יש בוחן מסכם פתוח ליחידה הזאת.",quiz_modes_unavailable:"בחנים מסכמים אינם זמינים עדיין באתר."}; async function finalPost(path,body,status){ const r=await api(`/api/classes/${encodeURIComponent(REPORT.id)}/quiz-windows${path}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(body||{})}); const d=await r.json().catch(()=>({})); if(!r.ok) throw Error(FINAL_ERR[d.detail]||(typeof d.detail==="string"&&r.status===400?"בדקו את הפרטים: "+d.detail:"הפעולה נכשלה. נסו שוב.")); return d; } async function finalAct(t){ try{ if(t.dataset.finalClose) await finalPost(`/${encodeURIComponent(t.dataset.finalClose)}/close`); else if(t.dataset.finalRelease){ const w=(REPORT.finals.windows||[]).find(x=>x.id===t.dataset.finalRelease), st=finalStats(w), open=st.handed-st.checked; if(open&&!confirm(`${open===1?"בוחן אחד עוד לא נבדק":`${open} בחנים עוד לא נבדקו`} במלואו. לפרסם בכל זאת? התלמידים האלה יראו את הציון כשהבדיקה תסתיים.`)) return; await finalPost(`/${encodeURIComponent(w.id)}/release`); } REPORT.finals=null; drawReport(); }catch(e){ alert(e.message); } } async function finalForm(form){ const status=form.querySelector("[role=status]"), button=form.querySelector("[type=submit]"); const iso=v=>v?new Date(v).toISOString():null; button.disabled=true; status.textContent="שומרים…"; try{ if(form.dataset.finalGrant) await finalPost(`/${encodeURIComponent(form.dataset.finalGrant)}/grants`, {student_email:form.elements.student.value,until:iso(form.elements.until.value),reason:form.elements.reason.value}); else await finalPost("",{unit_id:form.elements.unit.value,opens_at:iso(form.elements.opens.value),closes_at:iso(form.elements.closes.value)}); REPORT.finals=null; drawReport(); }catch(e){ button.disabled=false; status.textContent=e.message; } } /* ---------- the class's exams, in the report: what each covers, and how ready the class is ---------- The class teacher names an exam's units and a note (PUT /api/classes/{id}/exams/topics); the students' preparation view is built on them. For an exam with units: how many students finished each of its skills, and who is furthest. */ function examsTabHtml(units,all,name){ const exams=(REPORT.data.class.exams||[]).filter(e=>e?.date&&e?.subject).slice().sort((a,b)=>a.date.localeCompare(b.date)); const today=new Date(); today.setHours(0,0,0,0); const upcoming=exams.filter(e=>examDate(e.date)>=today), past=exams.filter(e=>examDate(e.date)!!s.progress?.done?.[id]; const subjects=[...new Set(["מתמטיקה",...exams.map(e=>e.subject)])]; const add=`

    הוספת מבחן ללוח המבחנים

    ${subjects.map(x=>`
    החומר למבחן (לא חובה)
    ${units.map(u=>``).join("")}
    `; const card=e=>{ const chosen=new Set(e.units||[]), ids=units.filter(u=>chosen.has(u.id)).flatMap(u=>unitIds(u).filter(live)); const days=Math.round((examDate(e.date)-today)/864e5); const ready=ids.length?`
    כמה הכיתה מוכנה${ids.map(id=>{ const k=all.filter(x=>done(x.s,id)).length; return ``; }).join("")}
    מיומנותסיימו
    ${fmt(BY[id].short||BY[id].name)}${k}/${n}

    הכי רחוקים מהחומר: ${all.map(x=>({s:x.s,k:ids.filter(id=>done(x.s,id)).length})).sort((a,b)=>a.k-b.k).slice(0,5) .map(x=>`${esc(name(x.s.email))} (${x.k}/${ids.length})`).join(" · ")}

    ` :'

    בחרו את היחידות של המבחן כדי לראות כמה הכיתה מוכנה, וכדי שהתלמידים יקבלו תוכנית הכנה.

    '; return `

    ${esc(e.subject)} · ${esc(examFullDate(e.date))} ${days===0?"היום":days===1?"מחר":`בעוד ${days} ימים`}

    החומר למבחן
    ${units.map(u=>``).join("")}
    ${ready}
    `; }; return `

    לוח המבחנים של הכיתה: מה שהתלמידים רואים במסך המקצועות. מבחן הוא המבחן בבית הספר; לפניו התלמידים מקבלים תוכנית הכנה ומבחני תרגול.

    ${upcoming.map(card).join("")||'

    אין מבחנים קרובים בלוח.

    '} ${add} ${past.length?`
    מבחנים שעברו (${past.length})
      ${past.slice().reverse().map(e=>`
    • ${esc(e.subject)} · ${esc(examDay(e.date))}${(e.units||[]).length?` (${e.units.map(id=>esc(G.units.find(u=>u.id===id)?.name||id)).join(", ")})`:""}
    • `).join("")}
    `:""}`; } async function examAdd(form){ const status=form.querySelector("[role=status]"), button=form.querySelector("[type=submit]"); const body={subject:form.elements.subject.value,date:form.elements.date.value,units:[...form.querySelectorAll("[name=unit]:checked")].map(c=>c.value),note:form.elements.note.value}; if(!body.subject.trim()){ status.textContent="כתבו מקצוע."; return form.elements.subject.focus(); } if(!body.date){ status.textContent="בחרו תאריך."; return form.elements.date.focus(); } button.disabled=true; status.textContent="שומרים…"; try{ const r=await api(`/api/classes/${encodeURIComponent(REPORT.id)}/exams`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(body)}); const d=await r.json().catch(()=>({})); if(!r.ok) throw Error(r.status===409?"המבחן הזה כבר בלוח.":r.status===403?"רק מורה הכיתה יכול להוסיף מבחנים.":"ההוספה נכשלה. בדקו את הפרטים ונסו שוב."); REPORT.data.class.exams=[...(REPORT.data.class.exams||[]),d]; REPORT.flash=`המבחן ב${d.subject} נוסף ללוח. התלמידים רואים אותו.`; drawReport(); }catch(e){ button.disabled=false; status.textContent=e.message; } } async function examDelete(key){ const [subject,date]=key.split("|"); if(!confirm(`למחוק את המבחן ב${subject} (${examDay(date)}) מלוח המבחנים?`)) return; try{ const r=await api(`/api/classes/${encodeURIComponent(REPORT.id)}/exams?${new URLSearchParams({subject,date})}`,{method:"DELETE"}); if(!r.ok) throw Error("המחיקה נכשלה."); REPORT.data.class.exams=(REPORT.data.class.exams||[]).filter(e=>!(e.subject===subject&&e.date===date)); REPORT.flash="המבחן נמחק מהלוח."; drawReport(); }catch(e){ alert(e.message); } } async function examSave(form){ const [subject,date]=form.dataset.examForm.split("|"), status=form.querySelector("[role=status]"), button=form.querySelector("[type=submit]"); const body={subject,date,units:[...form.querySelectorAll("[name=unit]:checked")].map(c=>c.value),note:form.elements.note.value}; button.disabled=true; status.textContent="שומרים…"; try{ const r=await api(`/api/classes/${encodeURIComponent(REPORT.id)}/exams/topics`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(body)}); const d=await r.json().catch(()=>({})); if(!r.ok) throw Error(r.status===403?"רק מורה הכיתה יכול לקבוע את החומר.":"השמירה נכשלה. נסו שוב."); REPORT.data.class.exams=(REPORT.data.class.exams||[]).map(e=>e.subject===subject&&e.date===date?d:e); drawReport(); sheet.querySelector(".mx-report").insertAdjacentHTML("afterbegin",'

    החומר למבחן נשמר. התלמידים רואים תוכנית הכנה.

    '); }catch(e){ button.disabled=false; status.textContent=e.message; } } /* ---------- the class's assignments, in the report (app/assignments.py) ---------- The tab lists the class's assignments, how many students finished each and, for each student, which items are done (worked out by Sky.assignmentState from the progress and the server's quiz sittings, as on the student's own screen), and gives the teacher the form to make, change or delete one. */ const ASG_ERR={400:"אחד הפריטים אינו שייך ליחידות של הכיתה, או שהתאריך אינו תקין.",403:"רק מורה הכיתה יכול לתת לה משימות.",404:"הכיתה או המשימה לא נמצאו.", 503:"המשימות אינן זמינות כרגע. נסו שוב מאוחר יותר."}; // focus practice counts only its chosen students (app/assignments.py); a class-wide assignment counts everyone const asgFor=(a,s)=>!a.student_emails?.length||a.student_emails.includes(s.email); const asgStates=(a,order)=>order.filter(({s})=>asgFor(a,s)).map(({s})=>({s,st:Sky.assignmentState(a,s.progress,wsServer(s))})); function asgTasksHtml(order,name){ const list=REPORT.data.assignments; if(list===null) return '

    המשימות אינן זמינות כרגע באתר.

    '; const today=Sky.jerusalemDay(new Date()); const cards=list.slice().sort((p,q)=>q.due_on.localeCompare(p.due_on)).map(a=>{ const rows=asgStates(a,order), finished=rows.filter(x=>x.st.finished).length, past=today>a.due_on; const focus=a.student_emails?.length?`

    אישי תרגול ממוקד ל: ${a.student_emails.map(e=>`${esc(name(e))}`).join(", ")}

    `:""; return `
    ${esc(a.title)}${past?"המועד עבר · ":""}עד ${asgDate(a.due_on)}
    ${focus}${a.note?`

    ${esc(a.note)}

    `:""}

    ${finished} מתוך ${rows.length} תלמידים סיימו

      ${a.items.map(i=>`
    • ${asgItemLabel(i)}
    • `).join("")}
    ${wsSummaryHtml(a,order)} ${rows.length?`
    st.items.some(x=>wsWaiting(x.submission)))?" open":""}>מי עשה מה
    ${a.items.map(i=>``).join("")} ${rows.map(({s,st})=>`${st.items.map(x=>wsCellHtml(x,a,s)|| ``).join("")}`).join("")}
    תלמידמצב${asgItemLabel(i)}
    ${esc(name(s.email))}${Sky.assignmentTask(a,s.progress,wsServer(s)).label}${x.done?(x.late?"✓ באיחור":"✓"):x.known?"–":"?"}
    `:""}
    `; }).join(""); return `
    ${cards||`

    ${asgFull()?"עוד לא נתתם משימות לכיתה. ":"עוד לא נתתם שיעורי בית לכיתה. "}${asgFull()?"משימה היא מיומנויות לסיים, בוחן מסכם להגיש, מבחן תרגול או שיעורי בית, עד תאריך.":"שיעורי בית הם PDF או תמונות שהתלמידים פותרים ומגישים עד תאריך יעד."}

    `}`; } // a teacher gives worksheets only; the full picker (skills, unit quizzes, practice exams) is for developers const asgFull=()=>can("management.access"); function asgFormHtml(units){ const old=REPORT.data.assignments.find(x=>x.id===REPORT.form.id); const a=old||{title:"",note:"",due_on:new Date(Date.now()+7*864e5).toLocaleDateString("en-CA",{timeZone:"Asia/Jerusalem"}),items:[]}; const skills=new Set(a.items.filter(i=>i.kind==="skill").map(i=>i.id)), quizzes=new Set(a.items.filter(i=>i.kind==="unit_quiz").map(i=>i.unit)); const exam=new Set(a.items.find(i=>i.kind==="exam")?.units||[]); const box=(name,value,on,label)=>``; return `

    ${asgFull()?(old?"עריכת משימה":"משימה חדשה"):(old?"עריכת שיעורי בית":"שיעורי בית חדשים")}

    ${asgFull()?`
    מיומנויות לסיים${units.map(u=>{ const ids=unitIds(u).filter(live); return `skills.has(id))?" open":""}>${esc(u.name)}
    ${ids.map(id=>box("skill",id,skills.has(id),fmt(BY[id].short||BY[id].name))).join("")}
    `; }).join("")}
    בוחן חיזוק להגשה
    ${units.filter(u=>u.quiz).map(u=>box("quiz",u.id,quizzes.has(u.id),esc(u.name))).join("")||"ליחידות של הכיתה אין בוחן מסכם."}
    ${TERMS.mixed} על היחידות
    ${units.map(u=>box("exam",u.id,exam.has(u.id),esc(u.name))).join("")}
    מי שיסמן כמה יחידות יבקש ${TERMS.mixed} שכולל את כולן.
    `:""} ${wsFieldHtml(old)||(asgFull()?"":'

    שיעורי הבית אינם זמינים כרגע, ולכן אי אפשר לשמור אותם.

    ')}
    `; } /* Focus practice (app/assignments.py): a class teacher gives 1 to 5 skills to chosen students. Opened empty, or with skills and students already chosen (from a student's or the class's focus areas). */ const FOCUS_MAX=5; /* ---------- focus areas (Sky.focus): where each student, and the class, most need work ---------- Each skill comes with its evidence in words, and each piece of evidence says where it comes from: unit-quiz points are checked by the server; practice answers, misconceptions and fading are what the student's device reports. */ const DEVICE_TAG='· מהמכשיר'; const classSkillIds=units=>units.flatMap(u=>unitIds(u).filter(live)); // quiz points lost are the server's evidence: they count even for a student whose device has reported nothing yet const focusOf=(s,units)=>Sky.focus(s.progress||{},s.quiz_sittings,classSkillIds(units)).slice(0,Sky.FOCUS.TOP); function focusEvidence(f){ const quiz=f.lost?[`איבד ${Math.round(f.lost)} נק׳ בבחנים`]:[]; const device=[f.wrong?`${f.wrong} טעויות ב־14 ימים`:"",f.unfinished?"לא הושלם":"",f.fading?"דועך":""].filter(Boolean); return [quiz.join(""),device.length?`${device.join(" · ")} ${DEVICE_TAG}`:""].filter(Boolean).join(" · "); } function focusCardHtml(s,r,units){ const top=focusOf(s,units), rec=r.recurring||[]; if(!top.length&&!rec.length) return `
    🎯 על מה לעבוד

    אין כרגע מיומנות שצריך לחזק.

    `; return `
    🎯 על מה לעבוד
    ${top.length?`
      ${top.map(f=>`
    1. ${fmt(BY[f.id].short||BY[f.id].name)} ${focusEvidence(f)}
    2. `).join("")}
    `:""} ${rec.length?`

    טעויות שחוזרות ${DEVICE_TAG}

      ${rec.map(x=>`
    • ${fmt(G.mistakes[x.m].name)} · ${x.n} פעמים ב־${x.skills.length} מיומנויות${BY[x.practise]?` · לחזק: ${fmt(BY[x.practise].short||BY[x.practise].name)} `:""}
    • `).join("")}
    `:""}
    `; } // the class: the skills most students list among their focus areas, and the misconceptions recurring in most students function classFocusHtml(units,all,name){ const bySkill={}, byMistake={}; for(const {s,r} of all){ for(const f of focusOf(s,units)) (bySkill[f.id]||=[]).push(s.email); for(const x of r?.recurring||[]) (byMistake[x.m]||=[]).push(s.email); } const skills=Object.entries(bySkill).sort((a,b)=>b[1].length-a[1].length).slice(0,5); const mistakes=Object.entries(byMistake).filter(([m])=>G.mistakes[m]).sort((a,b)=>b[1].length-a[1].length).slice(0,3); const who=list=>`
    ${list.length===1?"תלמיד/ה אחד/ת":`${list.length} תלמידים`}

    ${list.map(e=>`${esc(name(e))}`).join(" · ")}

    `; if(!skills.length&&!mistakes.length) return '

    אין כרגע מיומנות או טעות שחוזרת אצל כמה תלמידים.

    '; return `

    מה הכיתה צריכה לחזק: המיומנויות שהכי הרבה תלמידים צריכים לעבוד עליהן (לפי ניקוד הבחנים מהשרת ונתוני התרגול מהמכשיר), והטעויות שחוזרות אצל הכי הרבה תלמידים.

    ${skills.length?`
    מיומנויות לחזק
      ${skills.map(([id,list])=>`
    1. ${fmt(BY[id].short||BY[id].name)} · ${list.length} תלמידים ${who(list)}
    2. `).join("")}
    `:""} ${mistakes.length?`
    טעויות שחוזרות ${DEVICE_TAG}
      ${mistakes.map(([m,list])=>`
    1. ${fmt(G.mistakes[m].name)} · ${list.length} תלמידים ${BY[G.mistakes[m].back]?``:""}${who(list)}
    2. `).join("")}
    `:""}`; } // on a student's card: the focus practice given to them, and how far they are function focusOfHtml(s){ const mine=(REPORT.data.assignments||[]).filter(a=>a.student_emails?.includes(s.email)); if(!mine.length) return ""; return `
    🎯 תרגול ממוקד
      ${mine.map(a=>{ const st=Sky.assignmentState(a,s.progress,wsServer(s)); return `
    • ${esc(a.title)} עד ${asgDate(a.due_on)} · ${st.finished?(st.late?"הושלם באיחור":"הושלם"):st.overdue?"באיחור":`${st.doneCount} מתוך ${st.total}`}
    • `; }).join("")}
    `; } function focusOpen(skills=[],emails=[]){ REPORT.tab="tasks"; REPORT.sub=null; REPORT.form={id:null,focus:true,skills,emails}; drawReport(); sheet.querySelector(".mx-asg-form input[name=title]")?.focus(); } function focusFormHtml(units,name){ const old=(REPORT.data.assignments||[]).find(x=>x.id===REPORT.form.id); const skills=new Set(old?old.items.filter(i=>i.kind==="skill").map(i=>i.id):REPORT.form.skills||[]); const chosen=new Set(old?old.student_emails||[]:REPORT.form.emails||[]); const due=old?.due_on||new Date(Date.now()+7*864e5).toLocaleDateString("en-CA",{timeZone:"Asia/Jerusalem"}); const title=old?.title??(skills.size===1?`חיזוק: ${BY[[...skills][0]]?.short||BY[[...skills][0]]?.name||""}`:"תרגול ממוקד"); return `

    ${old?"עריכת תרגול ממוקד":"תרגול ממוקד לתלמידים"}

    רק התלמידים שתבחרו יראו את המשימה, והדוח סופר רק אותם. אפשר לבחור עד ${FOCUS_MAX} מיומנויות.

    תלמידים
    ${REPORT.data.students.map(s=>``).join("")}
    מיומנויות לתרגל${units.map(u=>{ const ids=unitIds(u).filter(live); return `skills.has(id))?" open":""}>${esc(u.name)}
    ${ids.map(id=>``).join("")}
    `; }).join("")}
    `; } async function focusSave(form){ const id=form.dataset.focusForm, status=form.querySelector(":scope>.mx-row>[role=status]"), button=form.querySelector("[type=submit]"), classId=REPORT.id; const picked=n=>[...form.querySelectorAll(`[name=${n}]:checked`)].map(c=>c.value); const body={title:form.elements.title.value,note:form.elements.note.value,due_on:form.elements.due_on.value, items:picked("skill").map(v=>({kind:"skill",id:v})),student_emails:picked("student")}; if(!body.title.trim()){ status.textContent="כתבו כותרת."; return form.elements.title.focus(); } if(!body.student_emails.length){ status.textContent="בחרו לפחות תלמיד אחד."; return; } if(!body.items.length||body.items.length>FOCUS_MAX){ status.textContent=`בחרו בין מיומנות אחת ל־${FOCUS_MAX} מיומנויות.`; return; } if(!body.due_on){ status.textContent="בחרו תאריך יעד."; return; } button.disabled=true; status.textContent="שומרים…"; try{ const r=await api(`/api/classes/${encodeURIComponent(classId)}/assignments${id?`/${encodeURIComponent(id)}`:""}`, {method:id?"PUT":"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(body)}); const d=await r.json().catch(()=>({})); if(!r.ok) throw Error(d.detail==="audience_unavailable"?"תרגול ממוקד אינו זמין עדיין באתר.":typeof d.detail==="string"&&r.status===400?"לא הצלחנו לשמור: בדקו את התלמידים והמיומנויות.":ASG_ERR[r.status]||"השמירה נכשלה. נסו שוב."); if(cur!=="report"||REPORT?.id!==classId) return; const list=REPORT.data.assignments||[]; REPORT.data.assignments=id?list.map(x=>x.id===id?d:x):[d,...list]; REPORT.form=null; drawReport(); sheet.querySelector(".mx-report").insertAdjacentHTML("afterbegin",`

    התרגול הממוקד נשמר. ${d.student_emails.length===1?"התלמיד/ה רואה":"התלמידים שבחרתם רואים"} אותו בבית.

    `); }catch(e){ if(cur==="report"){ button.disabled=false; status.textContent=e.message; } } } async function asgSave(form){ const id=form.dataset.asgForm, status=form.querySelector(":scope>.mx-row>[role=status]"), button=form.querySelector("[type=submit]"), classId=REPORT.id; // the save's own line, not the worksheet upload's const picked=name=>[...form.querySelectorAll(`[name=${name}]:checked`)].map(c=>c.value); const items=[...picked("skill").map(v=>({kind:"skill",id:v})),...picked("quiz").map(v=>({kind:"unit_quiz",unit:v}))]; if(picked("exam").length) items.push({kind:"exam",units:picked("exam")}); if(!asgFull()) items.push(...(REPORT.data.assignments.find(x=>x.id===id)?.items||[]).filter(i=>i.kind!=="worksheet")); // kept as they were const before=(REPORT.data.assignments.find(x=>x.id===id)?.items||[]).filter(i=>i.kind==="worksheet").map(i=>i.id); // without the worksheet list (not available), the assignment keeps the worksheets it had for(const w of REPORT.form?.sheets||before.map(wid=>({id:wid}))) items.push({kind:"worksheet",id:w.id}); const body={title:form.elements.title.value,note:form.elements.note.value,due_on:form.elements.due_on.value,items}; if(!body.title.trim()){ status.textContent="כתבו כותרת למשימה."; form.elements.title.focus(); return; } if(!body.due_on){ status.textContent="בחרו תאריך להגשה."; return; } if(!items.length){ status.textContent=asgFull()?"בחרו לפחות פריט אחד: מיומנות, בוחן חיזוק, ${TERMS.mixed} או שיעורי בית.":"העלו שיעורי בית אחד לפחות."; return; } button.disabled=true; status.textContent="שומרים…"; try{ const r=await api(`/api/classes/${encodeURIComponent(classId)}/assignments${id?`/${encodeURIComponent(id)}`:""}`, {method:id?"PUT":"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(body)}); const d=await r.json().catch(()=>({})); if(!r.ok) throw Error(d.detail==="teachers_give_worksheets_only"?"מורה נותן שיעורי בית בלבד.":ASG_ERR[r.status]||"השמירה נכשלה. נסו שוב."); if(cur!=="report"||REPORT?.id!==classId) return; const list=REPORT.data.assignments||[]; REPORT.data.assignments=id?list.map(x=>x.id===id?d:x):[d,...list]; wsAfterSave(before,d.items.filter(i=>i.kind==="worksheet").map(i=>i.id)); REPORT.form=null; drawReport(); sheet.querySelector(".mx-report").insertAdjacentHTML("afterbegin",`

    ${id?"המשימה עודכנה.":"המשימה נשמרה. התלמידים רואים אותה בבית."}

    `); }catch(e){ if(cur==="report"){ button.disabled=false; status.textContent=e.message; } } } async function asgDelete(id){ const a=REPORT.data.assignments.find(x=>x.id===id); if(!a) return; const started=REPORT.data.students.filter(s=>Sky.assignmentState(a,s.progress,wsServer(s)).doneCount>0).length; const sheets=a.items.filter(i=>i.kind==="worksheet").map(i=>i.id), handed=REPORT.data.students.filter(s=>(s.worksheet_submissions||[]).some(x=>sheets.includes(x.worksheet_id))).length; if(!confirm(`למחוק את המשימה "${a.title}"? התלמידים לא יראו אותה יותר.${started?` ${started} תלמידים כבר השלימו לפחות פריט אחד ממנה.`:""}${handed?` ${handed} תלמידים הגישו דפי עבודה, וההגשות יימחקו.`:""}`)) return; const classId=REPORT.id; try{ const r=await api(`/api/classes/${encodeURIComponent(classId)}/assignments/${encodeURIComponent(id)}`,{method:"DELETE"}); if(!r.ok) throw Error(ASG_ERR[r.status]||"המחיקה נכשלה. נסו שוב."); if(cur!=="report"||REPORT?.id!==classId) return; REPORT.data.assignments=REPORT.data.assignments.filter(x=>x.id!==id); wsAfterSave(sheets,[]); drawReport(); }catch(e){ if(cur==="report") sheet.querySelector(".mx-manage-state").textContent=e.message; } } /* ---------- worksheets on assignments (app/worksheets.py) ---------- The teacher uploads a PDF or photos from the assignment form; it becomes a {kind:"worksheet", id} item when the assignment is saved. Students open its pages (rendered on the server) from the card, and hand in photos of their solution, which they can replace until the teacher checks. The teacher checks each submission from the report's "משימות" tab: a score and a comment, both optional. */ const WS_ERR={too_many_pages:"אפשר עד 10 עמודים.",pdf_too_large:"הקובץ גדול מדי (עד 10MB).",file_too_large:"הקובץ גדול מדי.", image_too_large:"תמונה גדולה מדי (עד 5MB).",not_a_pdf:"הקובץ אינו PDF תקין.",unreadable_pdf:"לא הצלחנו לקרוא את ה־PDF. אם הוא מוגן בסיסמה, שמרו עותק בלי סיסמה.", pdf_too_slow:"ה־PDF כבד מדי לעיבוד. נסו לשמור אותו מחדש, או להעלות צילומים.",unsupported_image:"אפשר להעלות PDF או תמונות (JPEG, PNG, WebP).", empty_image:"הקובץ ריק.",one_pdf_or_photos:"העלו PDF אחד, או תמונות, לא את שניהם יחד.",no_files:"בחרו קובץ.", worksheets_unavailable:"שיעורי הבית אינם זמינים כרגע. נסו שוב מאוחר יותר.",already_checked:"המורה כבר בדק/ה את ההגשה, ולכן אי אפשר להחליף אותה.", worksheet_in_use:"שיעורי הבית כבר נשמרו במשימה.", open_ended_locked:"בדיקת AI נעולה כרגע.",grading_unavailable:"בדיקה אוטומטית אינה זמינה כרגע באתר.", teacher_ai_limit:"הגעתם למכסה של 3 בקשות AI ביום. אפשר לבדוק ידנית, או לנסות שוב מחר."}; const wsError=(r,fallback)=>WS_ERR[typeof r.data?.detail==="string"?r.data.detail:""]||(r.status===0?"אין חיבור לשרת. בדקו את החיבור ונסו שוב.":r.status===403?"אין לכם הרשאה לפעולה הזו.":r.status===404?"שיעורי הבית לא נמצאו.":fallback); // a form sent with upload progress (fetch has none) function sendForm(url,method,body,onProgress){ return new Promise(resolve=>{ const x=new XMLHttpRequest(); x.open(method,url); if(CFG.token) x.setRequestHeader("Authorization",`Bearer ${CFG.token}`); x.upload.onprogress=e=>{ if(e.lengthComputable) onProgress?.(e.loaded/e.total); }; x.onload=()=>{ let data={}; try{ data=JSON.parse(x.responseText); }catch{} resolve({ok:x.status>=200&&x.status<300,status:x.status,data}); }; x.onerror=x.ontimeout=()=>resolve({ok:false,status:0,data:{}}); x.send(body); }); } // a photo made smaller on the phone before it is sent; the file itself when that fails and it is an image we take async function wsPrepare(file){ let blob=null; try{ blob=await window.MathlyPhoto.prepare(file); }catch{} if(!blob&&["image/jpeg","image/png","image/webp"].includes(file.type)) blob=file; return blob&&blob.size<=5000000?blob:null; } const wsAll=()=>[...(REPORT?.data?.worksheets||[]),...(REPORT?.form?.sheets||[]),...(ASG?.worksheets||[])]; const wsInfo=id=>wsAll().find(w=>w.id===id)||null; const wsTitle=id=>wsInfo(id)?.title||"שיעורי בית"; const wsWhen=t=>new Date(t).toLocaleString("he-IL",{day:"numeric",month:"numeric",hour:"2-digit",minute:"2-digit",timeZone:"Asia/Jerusalem"}); // a submission waits for the teacher while it is handed in and neither checked nor returned const wsWaiting=x=>!!x&&(x.state||(x.checked_at?"checked":"submitted"))==="submitted"; const wsDue=(sub,a)=>{ const back=sub?.state==="returned"?sub.return_due_on:(sub?.earlier||[]).find(e=>e.return_due_on)?.return_due_on; return back&&back>a.due_on?back:a.due_on; }; const wsLate=(sub,a)=>!!sub&&sub.state!=="returned"&&Sky.jerusalemDay(sub.submitted_at)>wsDue(sub,a); const wsWhenShort=t=>new Date(t).toLocaleString("he-IL",{day:"numeric",month:"numeric",hour:"2-digit",minute:"2-digit"}); // earlier versions of a hand-in (returned for a fix): what was handed in and what the teacher wrote function wsEarlierHtml(sub){ const list=sub?.earlier||[]; if(!list.length) return ""; return `
    הגשות קודמות (${list.length})
      ${list.map(e=>`
    • גרסה ${e.version} · הוגש ${esc(wsWhenShort(e.submitted_at))} · ${e.pages} עמ׳ ${e.note?`
      ההערה שלך: ${esc(e.note)}`:""}${e.return_comment?`
      הוחזר לתיקון: ${esc(e.return_comment)}`:""}
    • `).join("")}
    `; } const wsServer=s=>({sittings:s.quiz_sittings,submissions:s.worksheet_submissions}); const wsHanded=id=>(REPORT?.data?.students||[]).filter(s=>(s.worksheet_submissions||[]).some(x=>x.worksheet_id===id)).length; async function wsBlob(url){ try{ const r=await api(url); return r.ok?URL.createObjectURL(await r.blob()):null; }catch{ return null; } } // ---- the teacher: in the assignment form function wsListHtml(){ const list=REPORT.form?.sheets||[]; return list.map(w=>`
  • 📄 ${esc(w.title)} ${w.pages} עמ׳ · ${w.source==="pdf"?"PDF":"תמונות"}
  • `).join(""); } function wsFieldHtml(old){ if(!Array.isArray(REPORT.data.worksheets)) return ""; // the worksheet tables are not there: no worksheets if(!REPORT.form.sheets) REPORT.form.sheets=(old?.items||[]).filter(i=>i.kind==="worksheet").map(i=>wsInfo(i.id)||{id:i.id,title:"שיעורי בית",pages:"?",source:""}); return `
    שיעורי בית
      ${wsListHtml()}
    PDF אחד של עד 10 עמודים, או עד 10 תמונות. אפשר להוסיף כמה שיעורי בית שרוצים. התלמידים פותחים אותם, מצלמים את הפתרון ומגישים עד תאריך היעד, ואתם בודקים בלשונית שיעורי בית או הגשות.
    `; } async function wsUpload(button){ const form=button.closest("form"), status=form.querySelector("[data-ws-status]"), bar=form.querySelector("[data-ws-progress]"); const title=form.elements.ws_title.value.trim(), files=[...form.elements.ws_files.files], classId=REPORT.id, mine=REPORT.form; const pdfs=files.filter(f=>f.type==="application/pdf"||/\.pdf$/i.test(f.name)); if(!title){ status.textContent="כתבו שם לשיעורי הבית."; return form.elements.ws_title.focus(); } if(!files.length){ status.textContent="בחרו PDF או תמונות."; return form.elements.ws_files.focus(); } if(pdfs.length&&files.length>1){ status.textContent=WS_ERR.one_pdf_or_photos; return; } if(files.length>10){ status.textContent="אפשר עד 10 תמונות."; return; } button.disabled=true; status.textContent="מכינים…"; const body=new FormData(); body.append("title",title); if(pdfs.length) body.append("files",files[0],files[0].name||"worksheet.pdf"); else for(const [i,f] of files.entries()){ const blob=await wsPrepare(f); if(!blob){ button.disabled=false; status.textContent=`לא הצלחנו להכין את התמונה ${i+1}. נסו תמונה אחרת.`; return; } body.append("files",blob,`page${i+1}.jpg`); } status.textContent="מעלים…"; bar.hidden=false; bar.value=0; const r=await sendForm(`/api/classes/${encodeURIComponent(classId)}/worksheets`,"POST",body,p=>bar.value=p); if(cur!=="report"||REPORT?.form!==mine) return; button.disabled=false; bar.hidden=true; if(!r.ok){ status.textContent=wsError(r,"ההעלאה נכשלה. נסו שוב."); return; } mine.sheets=[...(mine.sheets||[]),r.data]; form.querySelector("[data-ws-list]").innerHTML=wsListHtml(); form.elements.ws_title.value=""; form.elements.ws_files.value=""; status.textContent=`"${r.data.title}" הועלו (${r.data.pages} עמ׳). התלמידים יראו אותם כשתשמרו.`; } function wsRemove(id){ const mine=REPORT.form, old=REPORT.data.assignments.find(x=>x.id===mine.id), saved=(old?.items||[]).some(i=>i.kind==="worksheet"&&i.id===id); const n=saved?wsHanded(id):0; if(n&&!confirm(`להסיר את "${wsTitle(id)}"? ${n} תלמידים כבר הגישו אותו, וההגשות שלהם יימחקו כשתשמרו את המשימה.`)) return; mine.sheets=(mine.sheets||[]).filter(w=>w.id!==id); const list=sheet.querySelector("[data-ws-list]"); if(list) list.innerHTML=wsListHtml(); if(!saved) api(`/api/classes/${encodeURIComponent(REPORT.id)}/worksheets/${encodeURIComponent(id)}`,{method:"DELETE"}).catch(()=>{}); // not saved yet: gone now } // the form closed without saving: worksheets uploaded in it, and not in the assignment, go now (else the hourly cleanup) function wsCancel(){ const old=REPORT.data.assignments.find(x=>x.id===REPORT.form?.id), kept=new Set((old?.items||[]).filter(i=>i.kind==="worksheet").map(i=>i.id)); for(const w of REPORT.form?.sheets||[]) if(!kept.has(w.id)) api(`/api/classes/${encodeURIComponent(REPORT.id)}/worksheets/${encodeURIComponent(w.id)}`,{method:"DELETE"}).catch(()=>{}); } // after a save: the report knows the assignment's worksheets, and forgets those it lost function wsAfterSave(before,after){ const gone=new Set(before.filter(id=>!after.includes(id))); const known=(REPORT.data.worksheets||[]).filter(w=>!gone.has(w.id)); for(const w of REPORT.form?.sheets||[]) if(after.includes(w.id)&&!known.some(k=>k.id===w.id)) known.push(w); if(Array.isArray(REPORT.data.worksheets)) REPORT.data.worksheets=known; for(const s of REPORT.data.students) if(s.worksheet_submissions) s.worksheet_submissions=s.worksheet_submissions.filter(x=>!gone.has(x.worksheet_id)); } // ---- the teacher: in the report's assignments tab function wsSummaryHtml(a,order){ return a.items.filter(i=>i.kind==="worksheet").map(i=>{ if(order.some(({s})=>s.worksheet_submissions==null)) return `

    📄 ${esc(wsTitle(i.id))}: ההגשות אינן זמינות כרגע.

    `; const subs=order.map(({s})=>s.worksheet_submissions.find(x=>x.worksheet_id===i.id)).filter(Boolean); const late=subs.filter(x=>wsLate(x,a)).length, waiting=subs.filter(wsWaiting).length, back=subs.filter(x=>x.state==="returned").length; return `

    📄 ${esc(wsTitle(i.id))}: הגישו ${subs.length} מתוך ${order.length}${late?` (${late} באיחור)`:""} · ${order.length-subs.length} לא הגישו · ${waiting} ממתינים לבדיקה${back?` · ${back} הוחזרו לתיקון`:""}

    `; }).join(""); } function wsCellHtml(x,a,s){ const sub=x.submission; if(!sub) return null; const label=sub.state==="returned"?"↩ הוחזר":sub.checked_at?(sub.score!=null?`✔ ${sub.score}`:"✔ נבדק"):sub.version>1?"לבדיקה (מתוקן)":"לבדיקה"; return ``; } function wsSubOpen(key){ const [wid,email]=key.split("|"); REPORT.sub={wid,email,urls:[]}; drawReport(); sheet.querySelector(".mx-ws-review [data-ws-sub-back]")?.focus(); } function wsSubClose(){ for(const u of REPORT?.sub?.urls||[]) if(u) URL.revokeObjectURL(u); if(REPORT) REPORT.sub=null; } function wsSubHtml(name){ const {wid,email}=REPORT.sub, s=REPORT.data.students.find(x=>x.email===email); const sub=(s?.worksheet_submissions||[]).find(x=>x.worksheet_id===wid); const a=REPORT.data.assignments.find(x=>x.items.some(i=>i.kind==="worksheet"&&i.id===wid)); if(!s||!sub) return `

    ההגשה לא נמצאה.

    `; return `

    ${esc(wsTitle(wid))} · ${esc(name(email))}

    הוגש ${esc(wsWhen(sub.submitted_at))}${a&&wsLate(sub,a)?' · באיחור':""} · ${sub.pages} עמ׳${sub.version>1?` · גרסה ${sub.version} (אחרי תיקון)`:""}${sub.checked_at&&sub.state!=="returned"?` · נבדק ${esc(wsWhen(sub.checked_at))}`:""}${sub.state==="returned"?" · הוחזר לתיקון":""}

    ${sub.note?`

    הערה מהתלמיד/ה: ${esc(sub.note)}

    `:""} ${wsEarlierHtml(sub)}
    ${Array.from({length:sub.pages},(_,i)=>`עמוד ${i+1} של ההגשה`).join("")}
    ${CFG.aiLeft!=null?`נשארו לכם היום ${CFG.aiLeft} מתוך 3 בקשות AI. `:""}ה־AI מציע ציון והערה, ואתם מאשרים או מתקנים.
    ${sub.ai?`

    🤖 הצעת ה־AI${sub.ai.readable===false?" (לא הצליח לקרוא)":""}: ${sub.ai.score!=null?`${sub.ai.score} · `:""}${esc(sub.ai.comment||"")} (התלמיד/ה לא רואה אותה עד שתשמרו)

    `:""}
    ${sub.state==="returned"?`

    הוחזר לתיקון${sub.return_comment?`: ${esc(sub.return_comment)}`:""}. כשהתלמיד/ה יגיש/תגיש שוב, ההגשה תחזור לבדיקה.

    ` :`
    ↩ החזרה לתיקון
    `}
    `; } async function wsSubImages(){ const mine=REPORT?.sub; if(!mine) return; const s=REPORT.data.students.find(x=>x.email===mine.email), sub=(s?.worksheet_submissions||[]).find(x=>x.worksheet_id===mine.wid); if(!sub) return; for(let i=0;i=0&&score<=100)){ status.textContent="הציון הוא מספר שלם בין 0 ל־100, או ריק."; return form.elements.score.focus(); } button.disabled=true; status.textContent="שומרים…"; try{ const r=await api(`/api/classes/${encodeURIComponent(REPORT.id)}/worksheets/${encodeURIComponent(mine.wid)}/submissions/${encodeURIComponent(form.dataset.wsCheck)}`, {method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({score,comment})}); const d=await r.json().catch(()=>({})); if(!r.ok) throw Error(wsError({status:r.status,data:d},"השמירה נכשלה. נסו שוב.")); if(REPORT?.sub!==mine) return; const s=REPORT.data.students.find(x=>x.email===mine.email); s.worksheet_submissions=s.worksheet_submissions.map(x=>x.id===d.id?{...x,...d,earlier:x.earlier}:x); if(REPORT.inbox) return inboxAfterSave(); wsSubClose(); drawReport(); sheet.querySelector(".mx-report").insertAdjacentHTML("afterbegin",'

    הבדיקה נשמרה. התלמיד/ה רואה אותה בכרטיס המשימה.

    '); }catch(e){ if(REPORT?.sub===mine){ button.disabled=false; status.textContent=e.message; } } } // back to the student for a fix: a comment (required) and maybe a new due date; the next hand-in is a new version async function wsReturn(form){ const mine=REPORT.sub, status=form.querySelector("[role=status]"), button=form.querySelector("[type=submit]"); const comment=form.elements.comment.value.trim(), due_on=form.elements.due_on.value||null; if(!comment){ status.textContent="כתבו מה לתקן."; return form.elements.comment.focus(); } button.disabled=true; status.textContent="מחזירים…"; try{ const r=await api(`/api/classes/${encodeURIComponent(REPORT.id)}/worksheets/${encodeURIComponent(mine.wid)}/submissions/${encodeURIComponent(form.dataset.wsReturn)}/return`, {method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({comment,due_on})}); const d=await r.json().catch(()=>({})); if(!r.ok) throw Error(d.detail==="returns_unavailable"?"החזרה לתיקון אינה זמינה עדיין באתר.":d.detail==="not_the_current_version"?"התלמיד/ה כבר הגיש/ה גרסה חדשה. רעננו את הדוח.":wsError({status:r.status,data:d},"ההחזרה נכשלה. נסו שוב.")); if(REPORT?.sub!==mine) return; const s=REPORT.data.students.find(x=>x.email===mine.email); s.worksheet_submissions=s.worksheet_submissions.map(x=>x.id===d.id?{...x,...d,earlier:x.earlier}:x); if(REPORT.inbox) return inboxAfterSave(); wsSubClose(); drawReport(); sheet.querySelector(".mx-report").insertAdjacentHTML("afterbegin",'

    ההגשה הוחזרה לתיקון. התלמיד/ה רואה את ההערה בכרטיס המשימה.

    '); }catch(e){ if(REPORT?.sub===mine){ button.disabled=false; status.textContent=e.message; } } } // the AI's suggestion for this submission fills the form; the teacher decides what is saved async function wsAiCheck(button){ const mine=REPORT?.sub, status=sheet.querySelector("[data-ws-ai-status]"), form=sheet.querySelector("[data-ws-check]"); if(!mine||!form) return; button.disabled=true; status.textContent="ה־AI בודק… זה לוקח כחצי דקה."; try{ const r=await api(`/api/classes/${encodeURIComponent(REPORT.id)}/worksheets/${encodeURIComponent(mine.wid)}/submissions/${encodeURIComponent(form.dataset.wsCheck)}/ai-check`,{method:"POST"}); const d=await r.json().catch(()=>({})); if(REPORT?.sub!==mine) return; if(!r.ok){ if(d.detail==="teacher_ai_limit") CFG.aiLeft=0; throw Error(String(d.detail||"").startsWith("ai_check_failed")?"הבדיקה של ה־AI נכשלה. אפשר לנסות שוב או לבדוק ידנית.":wsError({status:r.status,data:d},"הבדיקה של ה־AI נכשלה. אפשר לבדוק ידנית.")); } if(typeof d.left==="number"&&!d.class_budget) CFG.aiLeft=d.left; if(d.score!=null) form.elements.score.value=d.score; form.elements.comment.value=d.comment||""; status.textContent=d.readable?"הצעת ה־AI הוכנסה לטופס. בדקו, תקנו אם צריך, ולחצו על הכפתור כדי לשמור.":"ה־AI לא הצליח לקרוא את הפתרון (לא נספר מהמכסה). ההערה שלו בטופס, ואפשר לבדוק ידנית."; form.elements.comment.focus({preventScroll:true}); }catch(e){ if(REPORT?.sub===mine) status.textContent=e.message; } finally{ button.disabled=false; } } // ---- the report's "הגשות" tab: everything the students handed in, by student and by kind function subsTabHtml(order,name){ const students=REPORT.data.students, noteDevice=' · מהמכשיר'; const hwRows=s=>(s.worksheet_submissions||[]).slice().sort((p,q)=>String(q.submitted_at).localeCompare(String(p.submitted_at))); const cards=order.map(({s,i})=>{ const exams=(s.progress?.quizzes||[]).slice().reverse(); const sit=s.quiz_sittings, latest=new Map((sit||[]).map(x=>[x.unit_id,x.id])); const quizzes=(sit||[]).slice().reverse(), homework=hwRows(s); const waiting=homework.filter(wsWaiting).length+quizzes.filter(x=>x.grade==null).length; const total=exams.length+quizzes.length+homework.length; const group=(title,rows,empty)=>`
    ${title}
    ${rows.length?`
      ${rows.join("")}
    `:`

    ${empty}

    `}
    `; const examRows=exams.map(q=>`
  • ${(q.units||[]).map(asgUnit).join(", ")||TERMS.mixed} ${esc(shortDate(q.at))} · ${q.right}/${q.n} נכונות (${pct(q.right,q.n)}%)
  • `); const quizRows=sit==null?[]:quizzes.map(x=>`
  • ${asgUnit(x.unit_id)} ${x.mode==="final"?"מסכם":"חיזוק"} ${esc(shortDate(x.submitted_at))} · ${x.grade==null?'ממתין לבדיקה':`ציון ${x.grade}/100`} ${latest.get(x.unit_id)===x.id?``:""}
  • `); const hwList=homework.map(x=>{ const a=REPORT.data.assignments?.find(y=>y.items.some(it=>it.kind==="worksheet"&&it.id===x.worksheet_id)), late=a&&wsLate(x,a); return `
  • ${esc(wsTitle(x.worksheet_id))} ${esc(wsWhen(x.submitted_at))}${late?" · באיחור":""} · ${x.state==="returned"?"הוחזר לתיקון":x.checked_at?(x.score!=null?`נבדק, ציון ${x.score}`:"נבדק"):`ממתין לבדיקה${x.version>1?" (מתוקן)":""}`}
  • `; }); return `
    ${esc(name(s.email))} ${total} הגשות${waiting?`${waiting} ממתינות לבדיקה`:""}
    ${group(`שיעורי בית`,hwList,"עוד לא הוגשו שיעורי בית.")} ${group(`בחנים`,quizRows,sit==null?"הבחנים אינם זמינים כרגע.":"עוד לא הוגש בוחן.")} ${group(`מבחני תרגול${noteDevice}`,examRows,"עוד לא נעשה מבחן תרגול.")}
    `; }).join(""); const waitingAll=order.reduce((n,{s})=>n+hwRows(s).filter(wsWaiting).length+(s.quiz_sittings||[]).filter(x=>x.grade==null).length,0); return `

    ${waitingAll?`${waitingAll} הגשות ממתינות לבדיקה. `:"אין הגשות שממתינות לבדיקה. "}שיעורי בית אפשר לבדוק ידנית או עם AI בפתיחת ההגשה. בחנים מסכמים נבדקים בפתיחה, וה־AI בודק אותם דרך תור ה־AI.${CFG.aiReview?' ':""}

    ${cards||'

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

    '}`; } // ---- the student: the worksheet and the hand-in let WS=null; // {id, urls (pages), files (chosen photos, prepared), previews, msg, busy, progress} function dropWorksheet(){ if(WS){ clearTimeout(WS.timer); const sid=WS.phone?.res?.scan?.id; if(sid) uqPost(`/api/math/answer-scans/${sid}/decline`).catch(()=>{}); // a request for the phone that nobody waits for window.MathlyBoard?.closeFull(); for(const u of [...WS.urls,...WS.previews]) if(u) URL.revokeObjectURL(u); WS=null; } wsSubClose(); } const wsAssignment=id=>ASG?.assignments.find(a=>a.items.some(i=>i.kind==="worksheet"&&i.id===id)); const wsMine=id=>(ASG?.submissions||[]).find(x=>x.worksheet_id===id)||null; function wsOpen(id){ if(!cur) back=document.activeElement; closeSheetQuietly(); cur="worksheet"; WS={id,urls:[],files:[],previews:[],msg:"",busy:false,progress:0,fresh:true}; drawWorksheet(); wsPages(WS); } function wsHandHtml(w,a){ const sub=wsMine(w.id); if(ASG.submissions===null) return '

    אי אפשר לבדוק כרגע אם הגשתם. נסו שוב מאוחר יותר.

    '; const returned=sub?.state==="returned"; if(sub?.checked_at&&!returned) return `

    ✔ המורה בדק/ה${sub.score!=null?` · ציון ${sub.score}`:""}

    ${sub.comment?`

    ${esc(sub.comment)}

    `:""}

    הוגש ${esc(wsWhen(sub.submitted_at))}${wsLate(sub,a)?" · באיחור":""} · ${sub.pages} עמ׳${sub.version>1?` · גרסה ${sub.version}`:""}

    ${wsEarlierHtml(sub)}`; const chosen=WS.files.length?`
      ${WS.files.map((_,i)=>`
    1. תמונה ${i+1} עמוד ${i+1}${i?``:""}
    2. `).join("")}
    `:""; const due=wsDue(sub,a); return `${returned?`
    ↩ הוחזר לתיקון${sub.return_comment?`

    המורה: ${esc(sub.return_comment)}

    `:""}

    צלמו את הפתרון המתוקן והגישו שוב${sub.return_due_on?` עד ${asgDate(sub.return_due_on)}`:""}. ההגשה הקודמת נשמרת.

    ` :sub?`

    ✓ הוגש ${esc(wsWhen(sub.submitted_at))}${wsLate(sub,a)?" · באיחור":""} · ${sub.pages} עמ׳${sub.version>1?` · גרסה ${sub.version}`:""} · ממתין לבדיקה. אפשר להחליף את ההגשה עד שהמורה יבדוק.

    ${sub.note?`

    ההערה שלך למורה: ${esc(sub.note)}

    `:""}` :`

    פתרתם? צלמו את הדפים של הפתרון (עד 10) והגישו למורה.${Sky.jerusalemDay(new Date())>due?" תאריך היעד עבר, ואפשר עדיין להגיש. ההגשה תסומן באיחור.":""}

    `} ${wsEarlierHtml(sub)} ${chosen} ${WS.files.length?``:""}
    ${window.MathlyBoard?``:""} ${wsPhoneHtml()} ${WS.files.length?``:""}
    ${WS.busy?``:""}`; } // on a computer the phone is asked to photograph a page: its code and progress show in the box (like an exercise's answer) function wsPhoneHtml(){ const touch=matchMedia("(max-width: 700px)").matches||navigator.maxTouchPoints>0; if(touch||!window.MathlyAnswers||!CFG.answerScans||!CFG.userId) return ""; return window.MathlyAnswers.phoneLive(WS.phone?.res?.scan,"data-ws-phone","data-ws-cancel","צילום בטלפון",WS.busy||WS.files.length>=10||!!WS.phone?.starting); } function drawWorksheet(){ const top=sheet.querySelector(".mx-sheet")?.scrollTop||0; const w=ASG?.worksheets?.find(x=>x.id===WS.id), a=wsAssignment(WS.id); const body=!w||!a?'

    שיעורי הבית אינם זמינים כרגע.

    ':`
    ${w.source==="pdf"?'':'אפשר לפתוח כל עמוד בגודל מלא ולשמור אותו.'}
    ${Array.from({length:w.pages},(_,i)=>`עמוד ${i+1} מתוך ${w.pages}`).join("")}

    ההגשה שלי

    ${wsHandHtml(w,a)}

    ${esc(WS.msg)}

    `; sheet.innerHTML=`
    `; sheet.hidden=false; if(WS.fresh){ WS.fresh=false; sheet.querySelector(".mx-x").focus({preventScroll:true}); } else sheet.querySelector(".mx-sheet").scrollTop=top; // a computer: the board beside the worksheet when the student turned it on (as in quizzes) if(w&&a&&window.MathlyBoard?.desk()) window.MathlyBoard.mount(sheet,`ws:${WS.id}`,{sendLabel:"הוספה להגשה", question:()=>sheet.querySelector(".mx-ws-pages"),submit:wsBoardSubmit(WS,false)}); } // a page written on the board joins the hand-in like a photo // On a computer the board opens beside the worksheet, so its pages stay in view; on a phone, full screen function wsBoard(){ const mine=WS; if(!mine||mine.busy||!window.MathlyBoard) return; if(window.MathlyBoard.desk()) return window.MathlyBoard.show(0); window.MathlyBoard.openFull({host:document.body,key:`ws:${mine.id}`,title:"פתרון בלוח",sendLabel:"הוספה להגשה", submit:wsBoardSubmit(mine,true),done:()=>WS===mine&&mine.files.length&&wsShowHandIn()}); } // a page from the board joins the photos of the hand-in; it is sent to the teacher with the hand-in's own button function wsBoardSubmit(mine,close){ return async(_,picture)=>{ if(WS!==mine) return {error:"החלון נסגר"}; if(mine.files.length>=10) return {error:"אפשר עד 10 עמודים."}; const blob=await wsPrepare(picture); if(WS!==mine) return {error:"החלון נסגר"}; if(!blob) return {error:"לא ניתן להכין את התמונה"}; mine.files.push(blob); mine.previews.push(URL.createObjectURL(blob)); mine.msg=`העמוד מהלוח נוסף (${mine.files.length} עמ׳). כדי לשלוח למורה לחצו על "הגשה".`; drawWorksheet(); if(!close) wsShowHandIn(); return close?{close:true}:{}; }; } // the hand-in after a page was added: in view, its send button marked and focused function wsShowHandIn(){ const hand=sheet.querySelector(".mx-ws-hand"), send=sheet.querySelector("[data-ws-send]"); if(!hand) return; hand.scrollIntoView({block:"center",behavior:"smooth"}); if(send){ send.classList.add("mx-attn"); send.focus({preventScroll:true}); } } async function wsPhone(){ const mine=WS; if(!mine||mine.busy||mine.phone) return; mine.phone={starting:true}; mine.msg=""; drawWorksheet(); try{ const r=await uqPost("/api/math/worksheet-attempts",{worksheet_id:mine.id,new:true}); const res=await uqPost(`${UQ_BASE}/${r.attempt.id}/scans`,{renew:true}); if(WS!==mine) return; mine.phone={aid:r.attempt.id,res,state:res.scan?.state}; }catch(e){ if(WS!==mine) return; mine.phone=null; mine.msg=e.message; } drawWorksheet(); wsPhoneWatch(mine); } async function wsPhoneCancel(){ const mine=WS, sid=mine?.phone?.res?.scan?.id; if(!sid) return; clearTimeout(mine.timer); mine.phone=null; try{ await uqPost(`/api/math/answer-scans/${sid}/decline`); }catch{} if(WS===mine){ mine.msg="הבקשה בוטלה."; drawWorksheet(); } } // looks every 2 seconds until the phone has sent the photo (it joins the hand-in) or the request ends function wsPhoneWatch(mine){ clearTimeout(mine.timer); const p=mine.phone; if(!p?.aid) return; mine.timer=setTimeout(async()=>{ if(WS!==mine||mine.phone!==p) return; try{ const r=await uqJson(`${UQ_BASE}/${p.aid}`); if(WS!==mine||mine.phone!==p) return; p.res=r; if(r.attachment){ const b=await api(`${UQ_BASE}/${p.aid}/image`,{cache:"no-store"}); if(WS!==mine||mine.phone!==p) return; if(!b.ok) throw Error("לא הצלחנו לקבל את התמונה מהטלפון. נסו שוב."); const blob=await b.blob(); if(WS!==mine||mine.phone!==p) return; mine.phone=null; if(mine.files.length>=10) mine.msg="אפשר עד 10 עמודים."; else{ mine.files.push(blob); mine.previews.push(URL.createObjectURL(blob)); mine.msg="התמונה מהטלפון נוספה להגשה."; } return drawWorksheet(); } const st=r.scan?.state; if(["waiting","confirmed","uploading","failed"].includes(st)){ if(st!==p.state){ p.state=st; drawWorksheet(); } // redraw only when the phone moved on return wsPhoneWatch(mine); } mine.phone=null; mine.msg=st==="declined"?"הבקשה נדחתה בטלפון.":st==="expired"?"בקשת הצילום פגה. אפשר לבקש שוב.":"הבקשה הסתיימה."; }catch(e){ if(WS!==mine||mine.phone!==p) return; mine.phone=null; mine.msg=e.message; } drawWorksheet(); },2000); } async function wsPages(mine){ const w=ASG?.worksheets?.find(x=>x.id===mine.id); if(!w) return; for(let i=0;ix.id===mine.id); if(!w) return; button.disabled=true; const url=await wsBlob(`/api/math/worksheets/${encodeURIComponent(w.id)}/original`); if(WS!==mine) return url&&URL.revokeObjectURL(url); button.disabled=false; if(!url){ mine.msg="ההורדה נכשלה. נסו שוב."; return drawWorksheet(); } const link=document.createElement("a"); link.href=url; link.download=`${w.title}.pdf`; document.body.append(link); link.click(); link.remove(); setTimeout(()=>URL.revokeObjectURL(url),60000); } async function wsAddPhotos(input){ const mine=WS, files=[...input.files]; input.value=""; if(!mine||!files.length) return; mine.msg="מכינים את התמונות…"; drawWorksheet(); let failed=0; for(const f of files){ if(mine.files.length>=10){ mine.msg="אפשר עד 10 עמודים."; break; } const blob=await wsPrepare(f); if(WS!==mine) return; if(!blob){ failed++; continue; } mine.files.push(blob); mine.previews.push(URL.createObjectURL(blob)); } if(mine.files.length<10||failed) mine.msg=failed?`לא הצלחנו להכין ${failed===1?"תמונה אחת":`${failed} תמונות`}. צלמו שוב.`:""; drawWorksheet(); } function wsMove(i,drop){ if(!WS||WS.busy) return; if(drop){ URL.revokeObjectURL(WS.previews[i]); WS.files.splice(i,1); WS.previews.splice(i,1); } else if(i>0){ for(const k of ["files","previews"]) [WS[k][i-1],WS[k][i]]=[WS[k][i],WS[k][i-1]]; } WS.msg=""; drawWorksheet(); } async function wsSend(){ const mine=WS; if(!mine||mine.busy||!mine.files.length) return; mine.busy=true; mine.progress=0; mine.msg="שולחים…"; drawWorksheet(); const body=new FormData(); mine.files.forEach((f,i)=>body.append("files",f,`page${i+1}.jpg`)); if((mine.note||"").trim()) body.append("note",mine.note.trim()); const r=await sendForm(`/api/math/worksheets/${encodeURIComponent(mine.id)}/submission`,"PUT",body,p=>{ mine.progress=p; const bar=WS===mine&&sheet.querySelector(".mx-ws-hand progress"); if(bar) bar.value=p; }); if(WS!==mine) return; mine.busy=false; if(!r.ok){ mine.msg=wsError(r,"השליחה נכשלה. התמונות נשמרו כאן: נסו לשלוח שוב."); // the chosen photos stay if(r.data?.detail==="already_checked") loadAssignments(); return drawWorksheet(); } ASG.submissions=[...(ASG.submissions||[]).filter(x=>x.worksheet_id!==mine.id),r.data]; for(const u of mine.previews) URL.revokeObjectURL(u); mine.files=[]; mine.previews=[]; mine.note=""; mine.msg=`ההגשה נשלחה למורה. 🎉 קבלה: ${r.data.pages} עמ׳, ${wsWhenShort(r.data.submitted_at)}${wsLate(r.data,wsAssignment(mine.id)||{due_on:"9999-12-31"})?", באיחור":", בזמן"}.`; drawWorksheet(); draw(); // the card on the home screen behind the sheet } // the report's own controls: the view and the order function reportControl(t){ if(t.dataset.reportTab){ wsSubClose(); REPORT.flash=null; REPORT.tab=t.dataset.reportTab; repSet("tab",REPORT.tab); drawReport(); sheet.querySelector(`[data-report-tab="${REPORT.tab}"]`)?.focus(); } else if(t.matches("[data-report-unit]")){ REPORT.unit=t.value; drawReport(); sheet.querySelector("[data-report-unit]")?.focus(); } else if(t.dataset.heatGroup||t.dataset.heatAll){ const open=heatOpen(), key=t.dataset.heatGroup; if(key) open.has(key)?open.delete(key):open.add(key); else if(t.dataset.heatAll==="close") open.clear(); else [...sheet.querySelectorAll("[data-heat-group]")].forEach(b=>open.add(b.dataset.heatGroup)); REPORT.open="|"+[...open].join("|"); repSet("mapMonths",REPORT.open); drawReport(); sheet.querySelector(key?`[data-heat-group="${CSS.escape(key)}"]`:`[data-heat-all="${t.dataset.heatAll}"]`)?.focus(); } else if(t.dataset.heatUnit){ REPORT.unit=t.dataset.heatUnit; drawReport(); sheet.querySelector("[data-report-unit]")?.focus(); } else if(t.matches("[data-report-sort]")){ REPORT.sort=t.value; repSet("sort",t.value); drawReport(); sheet.querySelector("[data-report-sort]")?.focus(); } } /* ---------- the teacher's review of a student's answer (app/teacher_review.py) ---------- The class teacher opens an open answer of one of their students (from a unit-quiz row of the report, or a handled request of the AI queue): the question, the photo, the automatic check, and a form for a score of their own and a comment. The teacher's score counts instead of the AI's wherever the answer is graded. */ let TR=null; // the answer on the sheet: {attemptId, back (how to return), data, url} const TR_ERR={invalid_score:"הציון חייב להיות מספר שלם בין 0 ל־100.",comment_too_long:"ההערה ארוכה מדי (עד 1000 תווים).",empty_review:"כתבו ציון או הערה.", invalid_comment:"ההערה אינה תקינה.",forbidden:"אין לכם הרשאה לתשובה הזו.",teacher_required:"אין לכם הרשאה לתשובה הזו.",not_found:"התשובה לא נמצאה.", answer_required:"לתשובה הזו אין תמונה שמורה.",review_unavailable:"הבדיקה אינה זמינה כרגע. נסו שוב מאוחר יותר.",answer_storage_unavailable:"שמירת תשובות אינה זמינה כרגע."}; function dropReview(){ if(TR?.url) URL.revokeObjectURL(TR.url); TR=null; } const trName=email=>String(email||"").split("@")[0]; function trShell(title,backLabel){ if(cur!=="review"){ if(!cur) back=document.activeElement; closeSheetQuietly(); cur="review"; } sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-x").focus({preventScroll:true}); } // the answers of one unit-quiz sitting of a student, to open one of them function reviewSitting(index,unitId){ const student=REPORT?.data?.students[index], unit=G.units.find(u=>u.id===unitId); const sitting=(student?.quiz_sittings||[]).filter(x=>x.unit_id===unitId).at(-1); if(!sitting) return; trShell(`תשובות בבוחן: ${unit.name}`,"חזרה לדוח"); // opening the sheet closes the one before it, and its answer dropReview(); TR={back:()=>classReport(REPORT.id)}; const STATE={missing:"לא צולמה תשובה",waiting:"ממתינה לבדיקה",unreadable:"לא קריאה",needs_review:"דורשת בדיקה שלכם",teacher:"נבדקה על ידכם",checked:"נבדקה"}; sheet.querySelector(".mx-manage-state").textContent=`${trName(student.email)} · ${sitting.grade==null?"עוד אין ציון":`ציון ${sitting.grade}/100`}`; sheet.querySelector(".mx-tr").innerHTML=`
      ${sitting.questions.map((q,k)=>`
    1. שאלה ${k+1} ${q.earned==null?"–":q.earned}/${q.points} נק׳

      ${STATE[q.state]||esc(q.state)}

      ${q.state==="missing"?"":``}
    2. `).join("")}
    `; } async function trOpenAnswer(attemptId,from){ const [kind,a,b]=String(from||"").split(":"); const backTo=kind==="sitting"?()=>reviewSitting(+a,b):kind==="queue"?()=>aiQueue():kind==="report"?()=>classReport(REPORT?.id) :kind==="inbox"?()=>inboxBack():()=>closeSheet(); trShell("תשובה של תלמיד",kind==="queue"?"חזרה לתור":"חזרה"); dropReview(); const mine=TR={attemptId,back:backTo,data:null,url:null}; sheet.querySelector(".mx-manage-state").textContent="טוענים את התשובה…"; try{ const r=await api(`/api/teacher/answers/${encodeURIComponent(attemptId)}`,{cache:"no-store"}); const d=await r.json().catch(()=>({})); if(!r.ok) throw Error(TR_ERR[d.detail]||"לא הצלחנו לטעון את התשובה. נסו שוב."); if(TR!==mine) return; mine.data=d; drawReview(); const img=await api(`/api/teacher/answers/${encodeURIComponent(attemptId)}/image`,{cache:"no-store"}); if(img.ok){ const url=URL.createObjectURL(await img.blob()); if(TR!==mine) return URL.revokeObjectURL(url); mine.url=url; const el=sheet.querySelector(".mx-tr-img"); if(el){ el.src=url; el.hidden=false; } } }catch(e){ if(TR===mine) sheet.querySelector(".mx-manage-state").textContent=e.message; } } function drawReview(note=""){ const d=TR.data, a=d.grading?.state==="completed"?d.grading.result:null, rv=d.review, rq=d.request; const isQuiz=String(d.attempt.question_id).startsWith("quiz:"), when=at=>new Date(at).toLocaleString("he-IL",{dateStyle:"short",timeStyle:"short"}); sheet.querySelector(".mx-manage-state").textContent=`${trName(d.student.email)} · ${d.student.email}`; sheet.querySelector(".mx-tr").innerHTML=`

    ${fmt(d.attempt.snapshot.name||"")}

    ${fmtLoose(d.attempt.snapshot.question||"").replace(/\n/g,"
    ")}

    התשובה שצילם התלמיד ${rq?.state==="open"?`
    התלמיד ביקש שתסתכלו שוב${rq.message?`

    ${esc(rq.message)}

    `:""}${when(rq.created_at)}
    `:""}
    בדיקה אוטומטית (AI)
    ${a?`

    ${esc(AQ_VERDICT[a.verdict]||"")}${a.score==null?"":` · ${esc(a.score)}/100`}

    ${a.feedback?`

    ${fmtLoose(a.feedback)}

    `:""} ${a.transcription?`
    מה ה-AI קרא בתמונה

    ${fmtLoose(a.transcription)}

    `:""} ${a.solution?`
    הפתרון המלא

    ${fmtLoose(a.solution)}

    `:""}`:'

    התשובה עוד לא נבדקה בבדיקה אוטומטית.

    '}
    ${rv?`
    הבדיקה שלכם

    ${rv.score==null?"בלי ציון":`${esc(rv.score)}/100`} · ${when(rv.created_at)}

    ${rv.comment?`

    ${esc(rv.comment).replace(/\n/g,"
    ")}

    `:""}
    `:""}
    ${rv?"עדכון הבדיקה שלכם":"הבדיקה שלכם"}

    ${isQuiz?"הציון שלכם מחליף את הציון האוטומטי בחישוב ציון הבוחן.":"הציון שלכם מחליף את הציון האוטומטי. ציון 100 מסמן את המיומנות כהושלמה אצל התלמיד."}

    ${esc(note)}
    `; } async function reviewSave(form){ const mine=TR, status=form.querySelector("[role=status]"), button=form.querySelector("[type=submit]"); const raw=form.elements.score.value.trim(), body={comment:form.elements.comment.value}; if(raw!=="") body.score=Number(raw); button.disabled=true; status.textContent="שומרים…"; try{ const r=await api(`/api/teacher/answers/${encodeURIComponent(mine.attemptId)}/review`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(body)}); const d=await r.json().catch(()=>({})); if(!r.ok) throw Error(TR_ERR[d.detail]||"השמירה נכשלה. נסו שוב."); if(TR!==mine) return; mine.data.review=d.review; mine.data.request=d.request; mine.data.effective_score=d.review?.score??mine.data.effective_score; mine.changed=true; if(mine.inbox) return inboxBack(true); drawReview("נשמר. התלמיד יראה את הבדיקה שלכם."); }catch(e){ if(TR===mine){ button.disabled=false; status.textContent=e.message; } } } /* ---------- the teacher's AI queue (app/ai_queue.py) ---------- A student does not call the AI. Their answer to check (or a photographed problem) waits here for their class's teacher, who looks at the photo and sends it to the AI or declines it with a note. The student's page shows the result. A manager sees every class's requests. */ let AQ=null, AQ_OPEN=null, RQ_OPEN=0, AQ_TAB="queue"; // AQ: the queue on the sheet; AQ_OPEN: how many requests wait (for the header button) const AQ_KIND={photo:"📷 תרגיל לעזרה",grading:"📝 בדיקת תשובה"}; const AQ_STATE={pending:"ממתינה",sending:"נשלחת…",done:"נשלחה ל-AI",failed:"השליחה נכשלה",declined:"נדחתה",cancelled:"בוטלה"}; const AQ_ERR={grading_failed:"הבדיקה נכשלה. אפשר לנסות שוב.",grading_unavailable:"בדיקה אוטומטית אינה זמינה כרגע באתר.",open_ended_locked:"שאלות פתוחות נעולות כרגע.", grading_limit:"התלמיד הגיע למכסת הבדיקות היומית.",grading_busy:"כבר מתבצעות בדיקות לתלמיד הזה. נסו שוב בעוד רגע.",grading_interrupted:"הבדיקה הופסקה. אפשר לנסות שוב.", request_taken:"הבקשה כבר טופלה (אולי ממכשיר אחר). רעננו את התור.",queue_unavailable:"התור אינו זמין כרגע.", answer_storage_unavailable:"לא הצלחנו לקרוא את התמונה השמורה.",photo_failed:"פתרון התמונה נכשל. אפשר לנסות שוב.", teacher_ai_limit:"הגעתם למכסה של 3 בקשות AI ביום. אפשר לשלוח שוב מחר."}; // a failed send's error is "code|reason|technical detail" (app/ai_queue.py) function aqError(error){ const [code,reason="",...rest]=String(error||"").split("|"), detail=rest.join("|"); return `

    ${esc(AQ_ERR[code]||code||"השליחה נכשלה.")}

    ${window.MathlyAnswers?.errorDetails?.(reason,detail)||""}`; } const AQ_VERDICT={correct:"נכונה",partial:"נכונה חלקית",incorrect:"שגויה",unreadable:"לא קריאה",needs_review:"דורשת בדיקה שלכם"}; const aqCode=list=>{ const c=list.map(r=>r.code).find(Boolean); return c?`#${esc(c)}`:""; }; const aqWaiting=r=>["pending","sending","failed"].includes(r.state); const aqLabel=()=>`תור AI${(AQ_OPEN||0)+RQ_OPEN?` ${(AQ_OPEN||0)+RQ_OPEN}`:""}`; // waiting requests, and students asking to look again function aqPaintBadge(){ document.querySelectorAll('[data-act="ai-queue"]').forEach(b=>{ if(!b.closest(".mx-sheet")) b.innerHTML=aqLabel(); }); } function aqBadge(rows){ AQ_OPEN=rows.filter(r=>r.state==="pending").length; aqPaintBadge(); } function rqBadge(rows){ RQ_OPEN=rows.length; aqPaintBadge(); } async function aqCount(){ try{ const r=await api("/api/ai-queue",{cache:"no-store"}); if(r.ok) aqBadge((await r.json()).requests||[]); }catch{} try{ const r=await api("/api/teacher/review-requests",{cache:"no-store"}); if(r.ok) rqBadge((await r.json()).requests||[]); }catch{} } function dropQueue(){ if(AQ) Object.values(AQ.urls).forEach(u=>u&&URL.revokeObjectURL(u)); AQ=null; } async function aiQueue(){ if(cur!=="aiqueue"){ if(!cur) back=document.activeElement; closeSheetQuietly(); cur="aiqueue"; AQ={rows:null,reqs:null,tab:AQ_TAB,urls:{},busy:new Set(),openGroups:new Set()}; } const mine=AQ; sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-x").focus({preventScroll:true}); try{ const r=await api("/api/ai-queue",{cache:"no-store"}); if(!r.ok) throw Error(r.status===403?"רק מורה של כיתה יכול לראות את התור.":"לא הצלחנו לטעון את התור. נסו שוב."); const d=await r.json(); if(AQ!==mine) return; mine.rows=d.requests||[]; drawQueue(); }catch(e){ if(AQ===mine) sheet.querySelector(".mx-manage-state").textContent=e.message; } try{ // the students asking the teacher to look again at a checked answer const r=await api("/api/teacher/review-requests",{cache:"no-store"}); mine.reqs=r.ok?(await r.json()).requests||[]:[]; }catch{ mine.reqs=[]; } if(AQ===mine){ rqBadge(mine.reqs); drawQueue(); } await aqBudgetLoad(mine); } /* ---------- the class's daily AI budget and batches (app/ai_batches.py) ---------- Checks of students' work come from each class's daily budget, not the teacher's own 3 requests. A batch sends every pending check of one scope (a final quiz, a unit's practice quizzes, a worksheet) in one action, after showing how many it holds and how many the class has left today. */ async function aqBudgetLoad(mine=AQ){ try{ const r=await api("/api/ai-queue/budget",{cache:"no-store"}); mine.budget=r.ok?await r.json():null; }catch{ mine.budget=null; } if(AQ===mine) drawQueue(); } function aqBudgetHtml(){ const b=AQ?.budget; if(!b?.classes?.length) return ""; if(b.available===false) return '

    תקציב ה־AI הכיתתי עוד לא מוגדר באתר: כל שליחה נספרת מ־3 הבקשות היומיות שלכם.

    '; return b.classes.map(c=>`

    ${esc(c.name)}: בדיקות AI

    נוצלו ${c.used_today} מתוך ${c.limit} היום · ב־7 הימים האחרונים: ${c.used_7d}

    ${b.manager?`
    `:""} ${c.scopes.length?`
      ${c.scopes.map(x=>`
    • ${esc(x.label)}: ${x.pending} ממתינות
    • `).join("")}
    `:'

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

    '}

    `).join(""); } // a batch: first how many, then the teacher confirms async function aqBatch(button){ const [cid,scope]=button.dataset.batch.split("|"), status=button.closest("section,article")?.querySelector("[role=status]"); const post=confirm=>api("/api/ai-queue/batches",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({class_id:+cid,scope,confirm})}).then(async r=>{ const d=await r.json().catch(()=>({})); if(!r.ok) throw Error(d.detail==="budget_unavailable"?"תקציב ה־AI הכיתתי עוד לא מוגדר באתר.":"השליחה נכשלה. נסו שוב."); return d; }); button.disabled=true; try{ const p=await post(false); if(!p.total){ if(status) status.textContent="אין מה לשלוח."; return; } if(!confirm(`${p.label||"שליחה לבדיקה"}: ${p.total} בדיקות · נשארו ${p.left} היום.${p.total>p.left?` יישלחו ${p.left} עכשיו, והשאר מחר.`:""} לשלוח?`)) return; const d=await post(true), say=`נשלחו ${d.sent} בדיקות.${d.later?` ${d.later} יישלחו מחר.`:""}`; if(status) status.textContent=say; if(cur==="aiqueue") setTimeout(()=>aiQueue(),400); else if(cur==="report"){ REPORT.finals=null; REPORT.inboxData=null; REPORT.flash=say; drawReport(); } }catch(e){ if(status) status.textContent=e.message; } finally{ button.disabled=false; } } async function aqSetting(cid,body,status){ try{ const r=await api(`/api/classes/${encodeURIComponent(cid)}/ai-settings`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(body)}); if(!r.ok) throw Error(r.status===403?"רק מנהל יכול לשנות את התקציב.":"השמירה נכשלה."); if(status) status.textContent="נשמר."; aqBudgetLoad(); }catch(e){ if(status) status.textContent=e.message; } } function aqTab(tab){ if(!AQ) return; AQ.tab=AQ_TAB=tab; drawQueue(); sheet.querySelector(`[data-aq-tab="${tab}"]`)?.focus(); } function aqResult(r){ const x=r.result||{}; if(r.kind==="photo") return x.readable===false?"ה-AI לא הצליח לקרוא את התרגיל.":`התשובה שה-AI מצא: ${fmtLoose(x.answer||"")}`; return `תוצאה: ${esc(AQ_VERDICT[x.verdict]||"")}${x.score==null?"":` · ${esc(x.score)}/100`}`; } function drawQueue(){ const box=sheet.querySelector(".mx-aq"); if(!box) return; const tabs=sheet.querySelector("[data-aq-tabs]"), asks=AQ?.reqs; if(tabs) tabs.innerHTML=` `; if(AQ?.tab==="requests") return drawRequests(); sheet.querySelector('[data-act="aq-all"]').hidden=true; sheet.querySelector("[data-aq-help]").hidden=false; if(!AQ?.rows) return; const open=AQ.rows.filter(aqWaiting), rest=AQ.rows.filter(r=>!aqWaiting(r)); aqBadge(AQ.rows); const done=AQ.tab==="done"; sheet.querySelector(".mx-manage-state").textContent=(done?(rest.length?`${rest.length} בקשות שטופלו ביממה האחרונה`:"אין בקשות שטופלו ביממה האחרונה."):open.length?(open.length===1?"בקשה אחת ממתינה":`${open.length} בקשות ממתינות`):"אין בקשות ממתינות.")+(CFG.aiLeft!=null?` · נשארו לכם היום ${CFG.aiLeft} מתוך 3 בקשות AI`:""); sheet.querySelector('[data-act="aq-all"]').hidden=done||open.filter(r=>r.state==="pending").length<2; const when=at=>new Date(at).toLocaleString("he-IL",{dateStyle:"short",timeStyle:"short"}); const card=r=>{ const busy=AQ.busy.has(r.id), act=r.state==="pending"||r.state==="failed"; return `
    ${AQ_KIND[r.kind]||""}${aqCode([r])} ${when(r.created_at)}${busy?"שולחים ל-AI…":AQ_STATE[r.state]||esc(r.state)}
    ${r.label?`

    ${fmt(r.label)}

    `:""}${r.question?`

    ${fmtLoose(r.question).replace(/\n/g,"
    ")}

    `:""} ${aqWaiting(r)?``:""} ${r.state==="failed"&&r.error?aqError(r.error):""} ${r.state==="declined"&&r.note?`

    ההערה שלכם: ${esc(r.note)}

    `:""} ${r.state==="done"?`

    ${aqResult(r)}

    `:""} ${r.kind==="grading"&&r.attempt_id&&!aqWaiting(r)?``:""} ${act?`
    `:""}
    `; }; // by student, then by subject (the same quiz: its label without the " · שאלה N"); newest first within each const subjectOf=r=>String(r.label||"").replace(/\s*·\s*שאלה\s*\d+\s*$/,"").trim()||(r.kind==="photo"?"תרגילים מצולמים":"ללא נושא"); const grouped=rows=>{ const students=new Map(), last=new Map(); // a request before the codes existed has none: the requests of one hand-in were made within moments of each other const clusterKey=r=>{ const id=`${r.email}|${subjectOf(r)}`, t=Date.parse(r.created_at), prev=last.get(id); if(!prev||Math.abs(prev.t-t)>120000) last.set(id,{t,n:(prev?.n||0)+1}); else prev.t=t; return `${subjectOf(r)}@${last.get(id).n}`; }; rows.forEach(r=>{ const s=students.get(r.email)||students.set(r.email,new Map()).get(r.email), k=r.code?`${subjectOf(r)}#${r.code}`:/·\s*שאלה\s*\d+\s*$/.test(r.label||"")?clusterKey(r):subjectOf(r); (s.get(k)||s.set(k,[]).get(k)).push(r); }); // one hand-in (its code) is one group: two sittings of the same quiz stay apart return [...students].map(([email,subjects])=>`

    ${esc(String(email).split("@")[0])}

    ${[...subjects].map(([key0,list])=>{ const name=subjectOf(list[0]); if(list.length===1) return `
    ${card(list[0])}
    `; // one request: a plain card (its card shows the hand-in number) // a collection (a quiz): one request that opens into a card per question, each handled on its own const key=email+"|"+key0, waiting=list.filter(aqWaiting).length; return `
    ${fmtLoose(name)} ${list.length}${aqCode(list)}${waiting?`${waiting} ממתינות`:"טופלו"}${list.map(card).join("")}
    `; }).join("")}
    `).join(""); }; box.innerHTML=(done?"":aqBudgetHtml())+grouped(done?rest:open); box.querySelectorAll("[data-aq-group]").forEach(d=>d.addEventListener("toggle",()=>{ const k=d.dataset.aqGroup; d.open?AQ.openGroups.add(k):AQ.openGroups.delete(k); })); (done?[]:open).forEach(r=>aqImage(r.id)); } // students who asked the teacher to look again at a checked answer function drawRequests(){ const box=sheet.querySelector(".mx-aq"), rows=AQ.reqs; sheet.querySelector('[data-act="aq-all"]').hidden=true; sheet.querySelector("[data-aq-help]").hidden=true; const state=sheet.querySelector(".mx-manage-state"); if(!rows){ state.textContent="טוענים את הבקשות…"; box.innerHTML=""; return; } state.textContent=rows.length?(rows.length===1?"בקשה אחת לבדיקה חוזרת":`${rows.length} בקשות לבדיקה חוזרת`):"אין בקשות לבדיקה חוזרת."; const when=at=>new Date(at).toLocaleString("he-IL",{dateStyle:"short",timeStyle:"short"}); box.innerHTML=rows.map(r=>`
    ${esc(String(r.student).split("@")[0])}🔁 בקשה לבדיקה חוזרת${when(r.created_at)}
    ${r.question?`

    ${fmt(r.question)}

    `:""} ${r.message?`

    ${esc(r.message)}

    `:'

    בלי הודעה.

    '}
    `).join(""); } async function aqImage(id){ const mine=AQ, show=url=>{ const img=sheet.querySelector(`[data-aq-img="${CSS.escape(id)}"]`); if(img){ img.src=url; img.hidden=false; } }; if(mine.urls[id]) return show(mine.urls[id]); if(id in mine.urls) return; // loading mine.urls[id]=null; try{ const r=await api(`/api/ai-queue/${encodeURIComponent(id)}/image`,{cache:"no-store"}); if(!r.ok) throw Error(); const url=URL.createObjectURL(await r.blob()); if(AQ!==mine) return URL.revokeObjectURL(url); mine.urls[id]=url; show(url); }catch{ if(AQ===mine) delete mine.urls[id]; } } async function aqPost(id,what,body){ const mine=AQ; if(!mine||mine.busy.has(id)) return; mine.busy.add(id); drawQueue(); let row=null, err=""; try{ const r=await api(`/api/ai-queue/${encodeURIComponent(id)}/${what}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(body||{})}); const d=await r.json().catch(()=>({})); if(d.detail==="teacher_ai_limit") CFG.aiLeft=0; if(!r.ok) throw Error(AQ_ERR[d.detail]||"הפעולה נכשלה. נסו שוב."); row=d; if(what==="send"&&d.state==="done") aiUsed(); }catch(e){ err=e.message; } mine.busy.delete(id); if(AQ!==mine) return; if(row){ const i=mine.rows.findIndex(x=>x.id===id); if(i>=0) mine.rows[i]=row; } drawQueue(); if(err) sheet.querySelector(".mx-manage-state").textContent=err; } const aqSend=id=>aqPost(id,"send"); function aqDecline(id){ const note=prompt("הערה לתלמיד (לא חובה), למשל: צלמו שוב, התמונה לא ברורה"); if(note!==null) return aqPost(id,"decline",{note}); } async function aqAll(){ for(const r of (AQ?.rows||[]).filter(x=>x.state==="pending")){ if(!AQ||CFG.aiLeft===0) return; await aqSend(r.id); } } /* A photographed problem (/api/math/photo, app/math_photo.py): hints first. The sheet shows the problem as it was read (a wrong reading is the student's cue to photograph again), then one hint; each further hint, each step and the whole solution with its answer come only when the student asks for them. */ async function shrink(file){ return window.MathlyPhoto.prepare(file); } async function photo(file){ stopPhone(); cancelAdvance(); let blob; try{ blob=await shrink(file); }catch{ blob=file; } // a format the browser cannot draw goes as it is if(PH?.url) URL.revokeObjectURL(PH.url); if(cur!=="photo"){ if(!cur) back=document.activeElement; cur="photo"; } const mine=PH={url:URL.createObjectURL(blob),data:null,error:"",busy:true,shown:1,works:new Set(),all:false,fresh:true}; drawPhoto(); try{ const form=new FormData(); form.append("photo",blob,"problem.jpg"); const res=await api("/api/math/photo",{method:"POST",body:form}), data=await res.json().catch(()=>({})); if(!res.ok) throw new Error(typeof data.detail==="string"?data.detail:`השרת ענה ${res.status}`); if(data.queued){ mine.req=data.request; mine.left=data.left; } else { mine.data=data; if(data.readable) aiUsed(); } }catch(e){ mine.error=e.message; } mine.busy=false; if(PH===mine&&cur==="photo") drawPhoto(); // a newer photo, or a closed sheet, keeps its own if(mine.req) photoWait(mine); } // a student's photo waits for their teacher (app/ai_queue.py): the sheet looks for the answer while it is open const photoWaiting=req=>!!req&&["pending","sending","failed"].includes(req.state); async function photoWait(mine){ while(PH===mine&&cur==="photo"&&photoWaiting(mine.req)){ await new Promise(done=>setTimeout(done,8000)); if(PH!==mine||cur!=="photo") return; try{ const r=await api("/api/math/ai-requests",{cache:"no-store"}); if(!r.ok) continue; const row=((await r.json()).requests||[]).find(x=>x.id===mine.req.id); if(!row) continue; mine.req=row; if(row.state==="done"&&row.result) mine.data={...row.result,left:mine.left}; }catch{ continue; } if(!photoWaiting(mine.req)&&PH===mine&&cur==="photo") drawPhoto(); } } async function photoCancel(){ const mine=PH; if(!mine?.req) return; try{ const r=await api(`/api/math/ai-requests/${encodeURIComponent(mine.req.id)}/cancel`,{method:"POST"}); if(r.ok) mine.req=await r.json(); }catch{} if(PH===mine&&cur==="photo") drawPhoto(); } function drawPhoto(){ const d=PH.data; let body; if(PH.busy) body='
    קוראים את התרגיל ופותרים…זה לוקח כמה שניות.
    '; else if(PH.error) body=`
    לא הצלחנו${esc(PH.error)}
    `; else if(!d&&photoWaiting(PH.req)) body=`
    התמונה נשלחה למורהאחרי שהמורה יאשר, הרמזים והפתרון יופיעו כאן.
    `; else if(!d) body=`
    ${PH.req?.state==="cancelled"?"הבקשה בוטלה":"המורה לא אישר את הבקשה"}${PH.req?.note?`הערת המורה: ${esc(PH.req.note)}`:""}
    `; else if(!d.readable) body=`
    לא הצלחנו לקרוא את התרגיל${fmtLoose(d.note||"צלמו שוב, קרוב יותר ובאור טוב.")}
    `; else{ const n=d.steps.length, shown=PH.all?n:Math.min(PH.shown,n); body=`
    זה התרגיל שקראנו:

    ${fmtLoose(d.problem)}

    ${d.note?`

    ${fmtLoose(d.note)}

    `:""}
      ${d.steps.slice(0,shown).map((s,i)=>`
    1. ${fmtLoose(s.hint)} ${PH.all||PH.works.has(i)?`
      ${fmtLoose(s.work)}
      `:``}
    2. `).join("")}
    ${PH.all?`
    התשובה${fmtLoose(d.answer)}
    `:`
    ${shownרמז הבא':""}
    `} ${d.skill&&BY[d.skill]?`

    המיומנות שהתרגיל מתרגל

    ${chips([d.skill])}
    `:""} ${d.left!=null?`

    נשארו לכם היום ${d.left} צילומים.

    `:""}`; } sheet.innerHTML=`
    `; sheet.hidden=false; if(PH.fresh){ PH.fresh=false; sheet.querySelector(".mx-x").focus({preventScroll:true}); } } /* The teacher's explanations, for students (GET /api/math/units/.../student-resources): the resources a teacher marked visible. Those tagged with a skill appear in that skill's sheet and in its help panel; the rest of the unit's are on a list opened from the unit on the path. Images come with the student's sign-in, so each is a blob URL. */ const SRES={}; // unit id → {rows, p (the request), at, urls} const SRES_TTL=300000; function sresLoad(unitId){ const c=SRES[unitId]||=({rows:null,p:null,at:0,urls:{}}); if(!c.p||Date.now()-c.at>SRES_TTL){ c.at=Date.now(); c.p=(async()=>{ try{ const r=await api(`/api/math/units/${encodeURIComponent(unitId)}/student-resources`,{cache:"no-store"}); c.rows=r.ok?(await r.json()).resources||[]:[]; }catch{ c.rows=c.rows||[]; } return c.rows; })(); } return c.p; } async function sresImage(unitId,id){ const c=SRES[unitId]; if(c.urls[id]) return c.urls[id]; try{ const r=await api(`/api/math/student-resources/${encodeURIComponent(id)}/image`); if(r.ok) return c.urls[id]=URL.createObjectURL(await r.blob()); }catch{} return ""; } const sresList=rows=>`
    ${rows.map(r=>`
    ${esc(RES_KINDS[r.kind]||"")}${r.note?` · ${esc(r.note)}`:""}
    `).join("")}
    `; function sresPaint(node,unitId,rows){ node.insertAdjacentHTML("beforeend",sresList(rows)); for(const r of rows) sresImage(unitId,r.id).then(url=>{ const img=node.querySelector(`[data-sres-img="${CSS.escape(r.id)}"]`); if(img&&url) img.src=url; }); } // fills a box with the unit's explanations that `pick` chooses (a skill's own); the box stays hidden when there are none async function fillExplanations(node,unitId,pick){ if(!node||!CFG.userId) return; const rows=(await sresLoad(unitId)).filter(pick); if(!rows.length||!node.isConnected) return; node.innerHTML="

    הסברים מהמורה

    "; node.hidden=false; sresPaint(node,unitId,rows); } // the unit's explanations that belong to no skill: a button under the unit, once they are known to exist function decorateUnits(){ if(!CFG.userId||can("students.view")||VIEW!=="path"||!path) return; for(const u of mineUnits()){ const c=SRES[u.id]; if(!c?.rows){ if(!c?.p) sresLoad(u.id).then(()=>{ if(root&&!root.hidden&&!cur) decorateUnits(); }); continue; } const general=c.rows.filter(r=>!r.skill_id), section=path.querySelector(`#mx-unit-${u.id}`); if(general.length&§ion&&!section.querySelector(".mx-sres-open")) section.insertAdjacentHTML("beforeend",``); } } async function openExplanations(unitId){ const unit=G.units.find(u=>u.id===unitId); if(!unit) return; stopPhone(); cancelAdvance(); if(!cur) back=document.activeElement; closeSheetQuietly(); cur="explanations"; sheet.innerHTML=`
    `; sheet.hidden=false; sheet.querySelector(".mx-x").focus({preventScroll:true}); const rows=(await sresLoad(unitId)).filter(r=>!r.skill_id), node=sheet.querySelector("[data-unit-res]"); if(!node||cur!=="explanations") return; node.innerHTML=rows.length?"":'

    אין כרגע הסברים לשיעור הזה.

    '; if(rows.length) sresPaint(node,unitId,rows); } /* Lesson resources (app/unit_resources.py). A teacher taps a unit's title on the path, photographs an exercise or an explanation, says what it shows (a new exercise, a theory explanation, or other words) and, if they like, which of the unit's topics it belongs to. Images come through the API with the teacher's sign-in, so each is a blob URL. */ const RES_KINDS={exercise:"תרגיל חדש",theory:"הסבר תיאורטי",other:"אחר"}; const resError=(status,fallback)=>({401:"צריך להיכנס מחדש.",403:"רק מורים יכולים לראות ולהוסיף משאבים.",404:"השיעור או התמונה לא נמצאו.", 413:"התמונה גדולה מדי. נסו לצלם שוב.",415:"לא הצלחנו לקרוא את התמונה. צלמו שוב או בחרו JPEG, PNG או WebP.", 503:"שמירת משאבים עדיין לא זמינה בשרת. נסו שוב מאוחר יותר."}[status]||fallback); function dropResources(){ if(!RES) return; Object.values(RES.urls).forEach(url=>URL.revokeObjectURL(url)); if(RES.preview) URL.revokeObjectURL(RES.preview); RES=null; } function openResources(unitId){ const unit=G.units.find(u=>u.id===unitId); if(!unit||!can("students.view")) return; stopPhone(); cancelAdvance(); dropResources(); if(!cur) back=document.activeElement; cur="resources"; RES={unit,items:null,error:"",urls:{},photo:null,preview:null,busy:false,msg:"",fresh:true}; drawResources(); loadResources(RES); } async function loadResources(mine){ try{ const res=await api(`/api/math/units/${encodeURIComponent(mine.unit.id)}/resources`,{cache:"no-store"}), data=await res.json().catch(()=>({})); if(!res.ok) throw new Error(resError(res.status,"לא הצלחנו לטעון את המשאבים.")); mine.items=data.resources||[]; mine.error=""; }catch(e){ mine.items=mine.items||[]; mine.error=e.message; } if(RES===mine&&cur==="resources") drawResources(); } function resourceForm(){ const skills=unitSkills(RES.unit.id); return `
    התמונה שצילמתם
    מה בתמונה?
    ${Object.entries(RES_KINDS).map(([k,label])=>``).join("")}
    הסבר תיאורטי מוצג לתלמידים כברירת מחדל. תרגיל והערה נשארים מוסתרים עד שתסמנו.
    `; } function resourceItem(r){ const skill=r.skill_id&&BY[r.skill_id], mine=r.created_by===CFG.userId||can("management.access"); const when=new Date(r.created_at), date=Number.isNaN(when.getTime())?"":when.toLocaleDateString("he-IL",{day:"numeric",month:"short",year:"numeric"}); return `
    ${esc(r.kind===
    ${esc(RES_KINDS[r.kind]||r.kind)}${r.kind==="other"&&r.note?`

    ${esc(r.note)}

    `:""} ${skill?`${fmt(skill.short||skill.name)} · `:""}${esc(date)}${r.created_email?` · ${esc(r.created_email)}`:""} ${r.visible?"👁 גלוי לתלמידים":"🔒 מוסתר מהתלמידים"}
    ${mine?`
    `:""}
    `; } function drawResources(){ const items=RES.items, was=sheet.querySelector(".mx-res-form"); // a redraw keeps what was filled in const filled=was&&{kind:was.elements.kind.value,note:was.elements.note.value,skill:was.elements.skill.value, visible:was.elements.visible.checked,touched:!!was.elements.visible.dataset.touched}; const list=items===null?'

    טוענים את המשאבים…

    ':items.length?`
    ${items.map(resourceItem).join("")}
    ` :'

    עוד אין משאבים לשיעור הזה. צלמו תרגיל או הסבר כדי להוסיף.

    '; sheet.innerHTML=`
    `; sheet.hidden=false; if(RES.fresh){ RES.fresh=false; sheet.querySelector(".mx-x").focus({preventScroll:true}); } const form=sheet.querySelector(".mx-res-form"); if(form){ const note=form.elements.note; const visible=form.elements.visible; // shown to students: a theory explanation by default, until the teacher decides if(filled){ form.querySelectorAll('[name="kind"]').forEach(r=>r.checked=r.value===filled.kind); note.value=filled.note; form.elements.skill.value=filled.skill; note.hidden=filled.kind!=="other"; visible.checked=filled.visible; if(filled.touched) visible.dataset.touched="1"; } form.addEventListener("change",event=>{ if(event.target===visible){ visible.dataset.touched="1"; return; } note.hidden=form.elements.kind.value!=="other"; if(!note.hidden) note.focus(); if(!visible.dataset.touched) visible.checked=form.elements.kind.value==="theory"; }); form.onsubmit=event=>{ event.preventDefault(); saveResource(form); }; } (items||[]).forEach(r=>{ if(r.id in RES.urls) showResourceImage(r.id); else loadResourceImage(RES,r.id); }); } function showResourceImage(id){ const url=RES.urls[id], img=sheet.querySelector(`[data-res-img="${CSS.escape(id)}"]`), link=sheet.querySelector(`[data-res-link="${CSS.escape(id)}"]`); if(!url) return; if(img) img.src=url; if(link) link.href=url; } async function loadResourceImage(mine,id){ mine.urls[id]=null; // asked for once try{ const res=await api(`/api/math/resources/${encodeURIComponent(id)}/image`); if(!res.ok) return; const url=URL.createObjectURL(await res.blob()); if(RES!==mine){ URL.revokeObjectURL(url); return; } mine.urls[id]=url; showResourceImage(id); }catch{} } async function resourcePhoto(file){ if(!RES||cur!=="resources") return; const mine=RES; mine.msg="מכינים את התמונה…"; drawResources(); let blob=null; try{ blob=await window.MathlyPhoto.prepare(file); }catch{} if(!blob&&["image/jpeg","image/png","image/webp"].includes(file.type)) blob=file; if(RES!==mine) return; if(!blob||blob.size>5000000){ mine.msg="לא הצלחנו להכין את התמונה. צלמו שוב."; return drawResources(); } if(mine.preview) URL.revokeObjectURL(mine.preview); mine.photo=blob; mine.preview=URL.createObjectURL(blob); mine.msg="בחרו מה יש בתמונה, ואם תרצו גם את הנושא."; drawResources(); sheet.querySelector(".mx-res-form")?.scrollIntoView({block:"start"}); } async function saveResource(form){ const mine=RES, kind=form.elements.kind.value, note=form.elements.note.value.trim(), skill=form.elements.skill.value; const status=sheet.querySelector(".mx-res-msg"); if(!kind){ status.textContent="בחרו מה יש בתמונה: תרגיל חדש, הסבר תיאורטי או אחר."; return; } if(kind==="other"&&!note){ status.textContent="כתבו בכמה מילים מה יש בתמונה."; form.elements.note.focus(); return; } mine.busy=true; form.querySelectorAll("button").forEach(b=>b.disabled=true); status.textContent="שומרים…"; try{ const body=new FormData(); body.append("photo",mine.photo,"resource.jpg"); body.append("kind",kind); body.append("note",kind==="other"?note:""); body.append("skill",skill); body.append("visible",form.elements.visible.checked?"1":"0"); const res=await api(`/api/math/units/${encodeURIComponent(mine.unit.id)}/resources`,{method:"POST",body}), data=await res.json().catch(()=>({})); if(!res.ok) throw new Error(resError(res.status,"לא הצלחנו לשמור. נסו שוב.")); mine.urls[data.id]=URL.createObjectURL(mine.photo); // the saved photo, without downloading it again mine.items=[data,...(mine.items||[])]; URL.revokeObjectURL(mine.preview); mine.photo=mine.preview=null; mine.msg="התמונה נשמרה במשאבי השיעור."; }catch(e){ mine.msg=e.message; } mine.busy=false; if(RES===mine&&cur==="resources") drawResources(); } async function setResourceVisible(id,visible){ const mine=RES; if(!mine) return; try{ const res=await api(`/api/math/resources/${encodeURIComponent(id)}`,{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify({visible})}), data=await res.json().catch(()=>({})); if(!res.ok) throw new Error(resError(res.status,"לא הצלחנו לשנות. נסו שוב.")); const i=mine.items.findIndex(r=>r.id===id); if(i>=0) mine.items[i]={...mine.items[i],visible:data.visible}; mine.msg=data.visible?"התמונה גלויה עכשיו לתלמידים.":"התמונה מוסתרת מהתלמידים."; }catch(e){ mine.msg=e.message; } if(RES===mine&&cur==="resources") drawResources(); } async function deleteResource(id){ const mine=RES; if(!mine||!confirm("למחוק את התמונה ממשאבי השיעור?")) return; try{ const res=await api(`/api/math/resources/${encodeURIComponent(id)}`,{method:"DELETE"}); if(!res.ok) throw new Error(resError(res.status,"לא הצלחנו למחוק. נסו שוב.")); mine.items=mine.items.filter(r=>r.id!==id); if(mine.urls[id]) URL.revokeObjectURL(mine.urls[id]); delete mine.urls[id]; mine.msg="התמונה נמחקה."; }catch(e){ mine.msg=e.message; } if(RES===mine&&cur==="resources") drawResources(); } function geometryDiagram(diagram){ const triangle=diagram.type==="triangle"; return `
    ${triangle?'':""} ${esc(diagram.base)} ${esc(diagram.height)} ${triangle?`${esc(diagram.side||"")}`:""} השרטוט להמחשה ואינו בקנה מידה
    `; } /* A geometry question's figure (`figure` in app/math_algebra9.json, checked by app/math_skills.py), one statement per ";": A=x,y[@dir] a point (y up; "_P" is not named, @n/s/e/w/ne/nw/se/sw places its name) · poly A B C · line A B · dash A B dot A · len A B text (lenx: on the other side) · ang P V Q text (ang2/ang3: two or three arcs) · right P V Q tick A B n (equal sides) · par A B n (parallel arrows) · text x,y words · note words (a line under the figure) grid (a coordinate plane) */ function figure(src){ const P={}, ops=[]; let grid=false; for(const st of src.split(";").map(s=>s.trim()).filter(Boolean)){ const m=st.match(/^([A-Za-z_][\w']*)=(-?[\d.]+),(-?[\d.]+)(?:@(\w+))?$/); if(m){ P[m[1]]={x:+m[2],y:+m[3],dir:m[4]}; continue; } if(st==="grid"){ grid=true; continue; } const [op,...a]=st.split(/\s+/); ops.push([op,a]); } const under=ops.filter(([op])=>op==="note").map(([,a])=>`

    ${esc(a.join(" "))}

    `).join(""); const notes=ops.filter(([op])=>op==="text").map(([,a])=>{const [x,y]=a[0].split(",").map(Number);return {x,y,words:a.slice(1).join(" ")}}); const all=[...Object.values(P),...notes], xs=all.map(p=>p.x), ys=all.map(p=>p.y); let x0=Math.min(...xs), x1=Math.max(...xs), y0=Math.min(...ys), y1=Math.max(...ys); if(grid){ x0=Math.floor(Math.min(x0,0))-1; x1=Math.ceil(Math.max(x1,0))+1; y0=Math.floor(Math.min(y0,0))-1; y1=Math.ceil(Math.max(y1,0))+1; } const W=320, PAD=grid?14:30, dx=Math.max(x1-x0,1e-6), dy=Math.max(y1-y0,1e-6), s=Math.min((W-2*PAD)/dx,(250-2*PAD)/dy); const H=Math.round(dy*s+2*PAD), ox=(W-dx*s)/2; const X=x=>ox+(x-x0)*s, Y=y=>H-PAD-(y-y0)*s, at=n=>[X(P[n].x),Y(P[n].y)]; const f=v=>v.toFixed(1), unit=([x,y])=>{const l=Math.hypot(x,y)||1;return [x/l,y/l]}; const named=Object.keys(P).filter(n=>!n.startsWith("_")); const pts=Object.keys(P), cx=pts.reduce((t,n)=>t+at(n)[0],0)/pts.length, cy=pts.reduce((t,n)=>t+at(n)[1],0)/pts.length; const txt=(x,y,words,cls="",anchor="")=>`${esc(words)}`; // a label beside a point: one beside a side grows away from it, so a long label never covers the side const beside=(x,y,[nx,ny],gap,words,cls)=>Math.abs(nx)>0.8?txt(x+nx*(gap-6),y+ny*gap,words,cls,nx>0?"start":"end"):txt(x+nx*gap,y+ny*gap,words,cls); let back="", shapes="", marks="", words=""; if(grid){ for(let x=x0;x<=x1;x++){ back+=``; if(x&&x>x0&&x`; if(y&&y>y0&&y`; } const side=(a,b,flip)=>{const [ax,ay]=at(a),[bx,by]=at(b),u=unit([bx-ax,by-ay]),mx=(ax+bx)/2,my=(ay+by)/2; let n=[-u[1],u[0]]; if((n[0]*(mx-cx)+n[1]*(my-cy)<0)!==!!flip) n=[-n[0],-n[1]]; return {u,n,mx,my};}; for(const [op,a] of ops){ if(op==="poly") shapes+=``; else if(op==="line"||op==="dash"){ const [p,q]=[at(a[0]),at(a[1])]; shapes+=``; } else if(op==="len"||op==="lenx"){ const {n,mx,my}=side(a[0],a[1],op==="lenx"); words+=beside(mx,my,n,14,a.slice(2).join(" "),"q"); } else if(/^ang[23]?$/.test(op)){ const v=at(a[1]), u1=unit([at(a[0])[0]-v[0],at(a[0])[1]-v[1]]), u2=unit([at(a[2])[0]-v[0],at(a[2])[1]-v[1]]); let d=Math.atan2(u2[1],u2[0])-Math.atan2(u1[1],u1[0]); while(d>Math.PI)d-=2*Math.PI; while(d<=-Math.PI)d+=2*Math.PI; const arcs=+(op[3]||1), deg=Math.abs(d)*180/Math.PI; for(let i=0;i`; } const b=unit([u1[0]+u2[0],u1[1]+u2[1]]), r=16+(arcs-1)*4+13+Math.max(0,(45-deg)*0.45); if(a.length>3) words+=txt(v[0]+b[0]*r,v[1]+b[1]*r,a.slice(3).join(" "),"q"); } else if(op==="right"){ const v=at(a[1]), u1=unit([at(a[0])[0]-v[0],at(a[0])[1]-v[1]]), u2=unit([at(a[2])[0]-v[0],at(a[2])[1]-v[1]]), k=10; marks+=``; } else if(op==="tick"||op==="par"){ const {u,n,mx,my}=side(a[0],a[1]), k=+(a[2]||1); // arrows step aside from the equal-side marks of the same side const shift=op==="par"&&ops.some(([o,b])=>o==="tick"&&[b[0],b[1]].sort().join()===[a[0],a[1]].sort().join())?14:0; for(let i=0;i` :``; } } else if(op==="dot") a.forEach(n=>{const [x,y]=at(n);shapes+=``;}); } const dirs={n:[0,-1],s:[0,1],e:[1,0],w:[-1,0],ne:[1,-1],nw:[-1,-1],se:[1,1],sw:[-1,1]}; for(const n of named){ const [x,y]=at(n), d=unit(dirs[P[n].dir]||(Math.hypot(x-cx,y-cy)<1?[1,-1]:[x-cx,y-cy])); words+=txt(x+d[0]*14,y+d[1]*14,n,"v"); } notes.forEach(t=>{words+=txt(X(t.x),Y(t.y),t.words,"t");}); return `
    ${back}${shapes}${marks}${words}${under}השרטוט להמחשה ואינו בקנה מידה
    `; } // a function's graph through the given points: monotone cubic (Fritsch-Carlson), so it never passes the marked extrema function plot(g){ const W=320, H=210, PAD=24, [x0,x1]=g.x, [y0,y1]=g.y; const sx=x=>PAD+(x-x0)/(x1-x0)*(W-2*PAD), sy=y=>H-PAD-(y-y0)/(y1-y0)*(H-2*PAD); const xs=g.points.map(p=>p[0]), ys=g.points.map(p=>p[1]), k=xs.length; const d=xs.slice(0,-1).map((x,i)=>(ys[i+1]-ys[i])/(xs[i+1]-x)); const m=xs.map((_,i)=>i===0?d[0]:i===k-1?d[k-2]:d[i-1]*d[i]<=0?0:(d[i-1]+d[i])/2); d.forEach((di,i)=>{ if(di===0){ m[i]=m[i+1]=0; return; } const a=m[i]/di, b=m[i+1]/di, s=a*a+b*b; if(s>9){ const t=3/Math.sqrt(s); m[i]=t*a*di; m[i+1]=t*b*di; } }); let line=""; for(let i=0;i`; if(x) ticks+=`${x}`; } for(let y=Math.ceil(y0);y<=y1;y++){ grid+=``; if(y) ticks+=`${y}`; } const dots=g.points.map(([x,y])=>``).join(""); return `
    ${grid} ${ticks}${dots}
    `; } async function boot(){ const state=document.getElementById("bootState"), login=document.getElementById("loginBtn"); if(AUTH_URL){ if(!window.supabase){ state.className="error"; state.textContent="לא ניתן לטעון את שירות הכניסה."; return; } client=window.supabase.createClient(AUTH_URL,CFG.supabaseKey); client.auth.onAuthStateChange((event,session)=>{ CFG.token=session?.access_token||""; if(event==="SIGNED_OUT"||(CFG.userId&&session?.user?.id&&session.user.id!==CFG.userId)){ window.MathlyAnswers?.reset();closeSheet();hide();location.reload(); } }); const {data}=await client.auth.getSession(), session=data.session; if(!session){ state.textContent="יש להיכנס כדי לפתוח את מסלול המתמטיקה."; login.hidden=false; login.onclick=()=>client.auth.signInWithOAuth({provider:"google",options:{redirectTo:location.origin+location.pathname}}); return; } CFG.token=session.access_token; const meta=session.user?.user_metadata||{}; CFG.name=meta.given_name||String(meta.full_name||meta.name||"").trim().split(/\s+/)[0]||""; const response=await api("/api/me"); if(!response.ok){ state.className="error"; state.textContent=response.status===403?"החשבון אינו משויך לכיתת מתמטיקה.":"לא ניתן להשלים את הכניסה."; return; } const me=await response.json();CFG.userId=me.user_id;CFG.answerScans=me.answer_scans; ME_EMAIL=me.email||""; MY_CLASSES=me.classes||[]; MY_CLASS=MY_CLASSES.length===1?MY_CLASSES[0]:null; CFG.permissions=me.permissions||[]; CFG.roles=me.roles||[]; CFG.aiDirect=me.ai_direct!==false; CFG.aiReview=!!me.ai_review; CFG.aiLeft=typeof me.ai_left==="number"?me.ai_left:null; // a teacher's AI requests left today (app/teacher_ai.py) } document.getElementById("boot").hidden=true; show(); } boot().catch(error=>{ const state=document.getElementById("bootState"); state.className="error"; state.textContent=`לא הצלחנו לפתוח את המסלול: ${error.message}`; }); })();