lms/public/build/assets/attempt-BqUyEpvb.js
2025-12-15 12:26:23 +01:00

2 lines
5.9 KiB
JavaScript

import{j as e,r as o,H as q,ba as Q,a3 as L,cZ as O,e as b}from"./vendor-BKmmeuxg.js";import{c as l}from"./utils-DxSsnXxD.js";import{B as m}from"./button-BpNwv2aE.js";import{I as z}from"./index-CupQom6K.js";import{M as B}from"./main-_V386djS.js";import M from"./attempt-navbar-CpeYyj_5.js";import R from"./question-navigator-suD72PVv.js";import $ from"./question-renderer-DE_1b1iJ.js";import F from"./timer-component-CSkSxcXX.js";import"./index-WW_1X1qx.js";import"./app-logo-CmnEIwal.js";import"./index-D9-QKOod.js";import"./use-screen-DU5n3V8C.js";import"./badge-AQfLLMpR.js";import"./card-D9_SJYq6.js";import"./question-type-badge-C65yTFrV.js";import"./fill-blank-question-hyvZaPr5.js";import"./input-DM0goJrn.js";import"./label-HmIcv5Yx.js";import"./listening-question-CfpGLH_r.js";import"./radio-group-C0C2vfl-.js";import"./slider-CUN53q6N.js";import"./matching-question-lExZP3k0.js";import"./select-CD9o3HN1.js";import"./mcq-question-D11ARDWZ.js";import"./multiple-select-question-DtEnPS2r.js";import"./checkbox-BBZKtHnN.js";import"./ordering-question-BhvGRbtu.js";import"./short-answer-question-cDvWN2cF.js";import"./textarea-CyndvowL.js";const H=({open:s,onOpenChange:t,children:n})=>s?e.jsxs("div",{className:"fixed inset-0 z-50 flex items-center justify-center",children:[e.jsx("div",{className:"fixed inset-0 bg-black/80",onClick:()=>t(!1)}),e.jsx("div",{className:"relative z-50",children:n})]}):null,P=({className:s,children:t,...n})=>e.jsx("div",{className:l("w-full max-w-lg bg-white border rounded-lg shadow-lg p-6 space-y-4",s),...n,children:t}),J=({className:s,children:t,...n})=>e.jsx("div",{className:l("space-y-2",s),...n,children:t}),Y=({className:s,children:t,...n})=>e.jsx("div",{className:l("flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",s),...n,children:t}),K=({className:s,children:t,...n})=>e.jsx("h2",{className:l("text-lg font-semibold",s),...n,children:t}),U=({className:s,children:t,...n})=>e.jsx("p",{className:l("text-sm text-gray-600",s),...n,children:t}),V=({className:s,children:t,...n})=>e.jsx(m,{className:l("",s),...n,children:t}),W=({className:s,children:t,...n})=>e.jsx(m,{variant:"outline",className:l("mt-2 sm:mt-0",s),...n,children:t}),De=({attempt:s})=>{const[t,n]=o.useState(0),[d,p]=o.useState({}),[u,y]=o.useState(new Set),[A,x]=o.useState(!1),[w,j]=o.useState(!1),v=((s.exam.duration_hours||0)*60+(s.exam.duration_minutes||0))*60,k=s.start_time?new Date(s.start_time).getTime():Date.now(),D=v>0?v:3600,C=s.end_time?s.end_time:new Date(k+D*1e3).toISOString(),a=s.exam.questions||[],c=a[t],h=new Set(Object.keys(d).map(Number));o.useEffect(()=>{const i=localStorage.getItem(`exam-attempt-${s.id}`);if(i)try{const r=JSON.parse(i);p(r.answers||{}),y(new Set(r.marked||[]))}catch(r){console.error("Failed to load saved answers:",r)}},[s.id]),o.useEffect(()=>{const i=setInterval(()=>{f()},3e4);return()=>clearInterval(i)},[d,u]),o.useEffect(()=>{const i=r=>{r.preventDefault(),r.returnValue=""};return window.addEventListener("beforeunload",i),()=>window.removeEventListener("beforeunload",i)},[]);const f=()=>{localStorage.setItem(`exam-attempt-${s.id}`,JSON.stringify({answers:d,marked:Array.from(u),lastSaved:new Date().toISOString()}))},E=async(i,r)=>{await b.post(route("exam-attempts.answer",s.id),{question_id:i,answer_data:r},{preserveScroll:!0,preserveState:!0})},I=i=>{c&&(p(r=>({...r,[c.id]:i})),E(c.id,i),f())},N=()=>{t>0&&n(t-1)},S=()=>{t<a.length-1&&n(t+1)},_=async()=>{j(!0),f();const i=Object.entries(d).map(([r,T])=>({exam_question_id:Number(r),answer_data:T}));b.post(route("exam-attempts.submit",s.id),{exam_attempt_id:s.id,answers:i},{onError:r=>{console.log(r)},onSuccess:()=>{localStorage.removeItem(`exam-attempt-${s.id}`)},onFinish:()=>{j(!1)}})};o.useEffect(()=>{const i=r=>{r.key==="ArrowRight"&&t<a.length-1?S():r.key==="ArrowLeft"&&t>0&&N()};return window.addEventListener("keydown",i),()=>window.removeEventListener("keydown",i)},[t,a.length]);const g=a.length-h.size;return e.jsxs(B,{children:[e.jsx(q,{title:`Taking: ${s.exam.title}`}),e.jsxs("main",{className:"flex min-h-screen flex-col justify-between overflow-x-hidden",children:[e.jsx(M,{attempt:s,questionIndex:t}),e.jsxs("div",{className:"container py-12",children:[e.jsx("div",{className:"space-y-4",children:e.jsxs("div",{className:"grid grid-cols-1 gap-4 lg:grid-cols-4",children:[e.jsxs("div",{className:"space-y-4 lg:col-span-3",children:[e.jsx(F,{attempt:s,endTime:C,questionIndex:t}),c&&e.jsx($,{question:c,questionNumber:t+1,answer:d[c.id],onAnswerChange:I}),e.jsxs("div",{className:"flex items-center justify-between rounded-lg bg-white p-4 shadow",children:[e.jsxs(m,{onClick:N,disabled:t===0,variant:"outline",children:[e.jsx(Q,{className:"mr-2 h-4 w-4"}),"Previous"]}),t<a.length-1?e.jsxs(m,{onClick:S,children:["Next",e.jsx(L,{className:"ml-2 h-4 w-4"})]}):e.jsx(m,{onClick:()=>x(!0),variant:"default",children:"Submit Exam"})]})]}),e.jsx("div",{className:"lg:col-span-1",children:e.jsx(R,{questions:a,currentQuestionIndex:t,answeredQuestions:h,markedQuestions:u,onNavigate:n})})]})}),e.jsx(H,{open:A,onOpenChange:x,children:e.jsxs(P,{children:[e.jsxs(J,{children:[e.jsxs(K,{className:"flex items-center gap-2",children:[e.jsx(O,{className:"h-5 w-5 text-yellow-600"}),"Submit Exam?"]}),e.jsxs(U,{className:"space-y-3",children:[e.jsx("p",{children:"Are you sure you want to submit your exam? This action cannot be undone."}),g>0&&e.jsx("div",{className:"rounded-lg bg-yellow-50 p-3",children:e.jsxs("p",{className:"text-sm font-semibold text-yellow-800",children:["Warning: You have ",g," unanswered question",g>1?"s":"","!"]})}),e.jsxs("div",{className:"text-sm",children:[e.jsxs("p",{children:[e.jsx("strong",{children:"Answered:"})," ",h.size," / ",a.length]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Marked for review:"})," ",u.size]})]})]})]}),e.jsxs(Y,{children:[e.jsx(W,{onClick:()=>x(!1),children:"Cancel"}),e.jsx(V,{onClick:_,disabled:w,children:w?"Submitting...":"Yes, Submit Exam"})]})]})})]}),e.jsx(z,{})]})]})};export{De as default};