feat(exam): S4-11 marking flow, results table, CSV, ResultsWidget
Some checks failed
app-ci-deploy / test-build-deploy (push) Has been cancelled
Some checks failed
app-ci-deploy / test-build-deploy (push) Has been cancelled
- /exam-marker/:batchId/mark — ExamMarkingPage: student queue, per-part mark entry, upsert marks - /exam-marker/:batchId/results — ExamResultsPage: results table with absent rows, CSV download - ResultsWidget on ClassDetailPage: last batch summary, class average, absent count, batch creation - New types: MarkingBatch, StudentSubmission, BatchQueueResponse, BatchResultsResponse, MarkUpsertPayload - New repo methods: createBatch, listBatches, getBatchQueue, getBatchResults, getBatchCsv, upsertMark Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
ab35193be1
commit
3389fdcb5b
@ -35,6 +35,9 @@ vi.mock('./pages/exam', () => ({
|
||||
ExamDashboardPage: () => <div>Exam Marker</div>,
|
||||
ExamTemplateSetupPage: () => <div>Exam Template Setup</div>,
|
||||
MarkSchemePage: () => <div>Mark Scheme editor</div>,
|
||||
ExamMarkingPage: () => <div>Exam Marking</div>,
|
||||
ExamResultsPage: () => <div>Exam Results</div>,
|
||||
ResultsWidget: () => <div>Results Widget</div>,
|
||||
}));
|
||||
vi.mock('./pages/user/calendarPage', () => ({ default: () => <div>Calendar</div> }));
|
||||
vi.mock('./pages/user/settingsPage', () => ({ default: () => <div>Settings</div> }));
|
||||
|
||||
@ -7,7 +7,7 @@ import LoginPage from './pages/auth/loginPage';
|
||||
import SignupPage from './pages/auth/signupPage';
|
||||
import SinglePlayerPage from './pages/tldraw/singlePlayerPage';
|
||||
import MultiplayerUser from './pages/tldraw/multiplayerUser';
|
||||
import { ExamDashboardPage, ExamTemplateSetupPage, MarkSchemePage } from './pages/exam';
|
||||
import { ExamDashboardPage, ExamMarkingPage, ExamResultsPage, ExamTemplateSetupPage, MarkSchemePage } from './pages/exam';
|
||||
import { ErrorBoundary } from './components/ErrorBoundary';
|
||||
import CalendarPage from './pages/user/calendarPage';
|
||||
import SettingsPage from './pages/user/settingsPage';
|
||||
@ -185,6 +185,8 @@ const AppRoutes: React.FC = () => {
|
||||
<Route path="/exam-marker" element={<ErrorBoundary><ExamDashboardPage /></ErrorBoundary>} />
|
||||
<Route path="/exam-marker/:templateId/setup" element={<ErrorBoundary><ExamTemplateSetupPage /></ErrorBoundary>} />
|
||||
<Route path="/exam-marker/:templateId/marks" element={<ErrorBoundary><MarkSchemePage /></ErrorBoundary>} />
|
||||
<Route path="/exam-marker/:batchId/mark" element={<ErrorBoundary><ExamMarkingPage /></ErrorBoundary>} />
|
||||
<Route path="/exam-marker/:batchId/results" element={<ErrorBoundary><ExamResultsPage /></ErrorBoundary>} />
|
||||
<Route path="/doc-intelligence/:fileId" element={<CCDocumentIntelligence />} />
|
||||
<Route path="/morphic" element={<MorphicPage />} />
|
||||
<Route path="/tldraw-dev" element={<TLDrawDevPage />} />
|
||||
|
||||
233
src/pages/exam/ExamMarkingPage.tsx
Normal file
233
src/pages/exam/ExamMarkingPage.tsx
Normal file
@ -0,0 +1,233 @@
|
||||
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { useNavigate, useParams } from 'react-router-dom';
|
||||
import { v5 as uuidv5 } from 'uuid';
|
||||
import {
|
||||
Alert,
|
||||
Box,
|
||||
Button,
|
||||
Card,
|
||||
CardContent,
|
||||
Chip,
|
||||
CircularProgress,
|
||||
Container,
|
||||
Divider,
|
||||
List,
|
||||
ListItemButton,
|
||||
ListItemText,
|
||||
Stack,
|
||||
TextField,
|
||||
Typography,
|
||||
} from '@mui/material';
|
||||
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
|
||||
import SaveIcon from '@mui/icons-material/Save';
|
||||
import TableChartIcon from '@mui/icons-material/TableChart';
|
||||
|
||||
import { examRepository } from '../../services/exam/examRepository';
|
||||
import type { BatchQueueResponse, ExamQuestion, ExamTemplateDetail, StudentSubmission } from '../../types/exam.types';
|
||||
|
||||
const MARK_NAMESPACE = '3f2dbbeb-9b15-4f99-9b71-8c535f8dc3d0';
|
||||
|
||||
function stableMarkId(batchId: string, submissionId: string, questionId: string) {
|
||||
return uuidv5(`${batchId}:${submissionId}:${questionId}`, MARK_NAMESPACE);
|
||||
}
|
||||
|
||||
const ExamMarkingPage: React.FC = () => {
|
||||
const { batchId } = useParams<{ batchId: string }>();
|
||||
const navigate = useNavigate();
|
||||
const [queue, setQueue] = useState<BatchQueueResponse | null>(null);
|
||||
const [template, setTemplate] = useState<ExamTemplateDetail | null>(null);
|
||||
const [selectedId, setSelectedId] = useState<string | null>(null);
|
||||
const [marks, setMarks] = useState<Record<string, string>>({});
|
||||
const [comments, setComments] = useState<Record<string, string>>({});
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [message, setMessage] = useState<string | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
if (!batchId) return;
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const nextQueue = await examRepository.getBatchQueue(batchId);
|
||||
setQueue(nextQueue);
|
||||
setTemplate(await examRepository.getTemplate(nextQueue.batch.template_id));
|
||||
setSelectedId((current) => current ?? nextQueue.submissions[0]?.id ?? null);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : String(e));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [batchId]);
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
}, [load]);
|
||||
|
||||
const markableQuestions = useMemo(
|
||||
() => (template?.questions ?? []).filter((q) => !q.is_container).sort((a, b) => a.order - b.order),
|
||||
[template],
|
||||
);
|
||||
const selected = queue?.submissions.find((s) => s.id === selectedId) ?? null;
|
||||
|
||||
const saveSelected = async () => {
|
||||
if (!batchId || !selected) return;
|
||||
setSaving(true);
|
||||
setError(null);
|
||||
setMessage(null);
|
||||
try {
|
||||
const writes = markableQuestions
|
||||
.map((q) => ({ q, raw: marks[q.id], comment: comments[q.id] }))
|
||||
.filter(({ raw, comment }) => raw !== undefined && raw !== '' || !!comment?.trim());
|
||||
for (const { q, raw, comment } of writes) {
|
||||
const awarded = raw === undefined || raw === '' ? 0 : Number(raw);
|
||||
if (Number.isNaN(awarded) || awarded < 0 || awarded > (q.max_marks ?? Number.MAX_SAFE_INTEGER)) {
|
||||
throw new Error(`Invalid mark for ${q.label}`);
|
||||
}
|
||||
await examRepository.upsertMark(stableMarkId(batchId, selected.id, q.id), {
|
||||
submission_id: selected.id,
|
||||
question_id: q.id,
|
||||
awarded_marks: awarded,
|
||||
comment: comment?.trim() || undefined,
|
||||
confirmed: true,
|
||||
});
|
||||
}
|
||||
setMessage(`Saved ${writes.length} mark${writes.length === 1 ? '' : 's'} for ${selected.student_name || selected.student_id || 'student'}.`);
|
||||
setMarks({});
|
||||
setComments({});
|
||||
await load();
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : String(e));
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const chooseStudent = (submission: StudentSubmission) => {
|
||||
setSelectedId(submission.id);
|
||||
setMarks({});
|
||||
setComments({});
|
||||
setMessage(null);
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return <Box sx={{ display: 'flex', justifyContent: 'center', py: 8 }}><CircularProgress /></Box>;
|
||||
}
|
||||
|
||||
if (error && !queue) {
|
||||
return (
|
||||
<Container maxWidth="lg" sx={{ py: 4 }}>
|
||||
<Alert severity="error">{error}</Alert>
|
||||
<Button sx={{ mt: 2 }} startIcon={<ArrowBackIcon />} onClick={() => navigate('/exam-marker')}>
|
||||
Back to Exam Marker
|
||||
</Button>
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container maxWidth="xl" sx={{ py: 3 }}>
|
||||
<Stack spacing={2}>
|
||||
<Box sx={{ display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between', gap: 2, flexWrap: 'wrap' }}>
|
||||
<Box>
|
||||
<Button size="small" startIcon={<ArrowBackIcon />} onClick={() => navigate('/exam-marker')} sx={{ mb: 1 }}>
|
||||
Exam Marker
|
||||
</Button>
|
||||
<Typography variant="h4" component="h1" fontWeight={700}>{queue?.batch.title || 'Mark exam'}</Typography>
|
||||
<Typography variant="body2" color="text.secondary">
|
||||
{template?.title || queue?.batch.template_id} · {queue?.progress.total ?? 0} students in queue
|
||||
</Typography>
|
||||
</Box>
|
||||
<Button variant="outlined" startIcon={<TableChartIcon />} onClick={() => batchId && navigate(`/exam-marker/${batchId}/results`)}>
|
||||
Results
|
||||
</Button>
|
||||
</Box>
|
||||
|
||||
{error && <Alert severity="error" onClose={() => setError(null)}>{error}</Alert>}
|
||||
{message && <Alert severity="success" onClose={() => setMessage(null)}>{message}</Alert>}
|
||||
{markableQuestions.length === 0 && (
|
||||
<Alert severity="warning">
|
||||
This template has no markable parts yet. Add parts in template setup before entering marks.
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<Stack direction={{ xs: 'column', md: 'row' }} spacing={2} alignItems="stretch">
|
||||
<Card variant="outlined" sx={{ width: { xs: '100%', md: 340 }, flexShrink: 0 }}>
|
||||
<CardContent>
|
||||
<Typography variant="h6" gutterBottom>Marking queue</Typography>
|
||||
<Stack direction="row" spacing={1} sx={{ mb: 1 }} flexWrap="wrap" useFlexGap>
|
||||
<Chip size="small" label={`${queue?.progress.total ?? 0} total`} />
|
||||
<Chip size="small" label={`${queue?.progress.absent ?? 0} absent`} color="warning" variant="outlined" />
|
||||
<Chip size="small" label={`${queue?.progress.complete ?? 0} complete`} color="success" variant="outlined" />
|
||||
</Stack>
|
||||
<List dense disablePadding>
|
||||
{(queue?.submissions ?? []).map((submission) => (
|
||||
<ListItemButton
|
||||
key={submission.id}
|
||||
selected={submission.id === selectedId}
|
||||
onClick={() => chooseStudent(submission)}
|
||||
sx={{ borderRadius: 1, mb: 0.5 }}
|
||||
>
|
||||
<ListItemText
|
||||
primary={submission.student_name || submission.student_id || 'Unknown student'}
|
||||
secondary={`${submission.status} · ${submission.mark_entry_count ?? 0} marks`}
|
||||
/>
|
||||
<Chip size="small" label={submission.status} color={submission.status === 'absent' ? 'warning' : 'default'} variant="outlined" />
|
||||
</ListItemButton>
|
||||
))}
|
||||
</List>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card variant="outlined" sx={{ flex: 1 }}>
|
||||
<CardContent>
|
||||
{selected ? (
|
||||
<Stack spacing={2}>
|
||||
<Box>
|
||||
<Typography variant="h6">{selected.student_name || selected.student_id || 'Unknown student'}</Typography>
|
||||
<Typography variant="body2" color="text.secondary">Status: {selected.status}</Typography>
|
||||
</Box>
|
||||
<Divider />
|
||||
{markableQuestions.map((q: ExamQuestion) => (
|
||||
<Stack key={q.id} direction={{ xs: 'column', sm: 'row' }} spacing={1.5} alignItems={{ xs: 'stretch', sm: 'center' }}>
|
||||
<Box sx={{ minWidth: 150 }}>
|
||||
<Typography variant="body2" fontWeight={700}>{q.label}</Typography>
|
||||
<Typography variant="caption" color="text.secondary">/ {q.max_marks} marks</Typography>
|
||||
</Box>
|
||||
<TextField
|
||||
label="Mark"
|
||||
type="number"
|
||||
size="small"
|
||||
inputProps={{ min: 0, max: q.max_marks, step: 0.5 }}
|
||||
value={marks[q.id] ?? ''}
|
||||
onChange={(e) => setMarks((prev) => ({ ...prev, [q.id]: e.target.value }))}
|
||||
sx={{ width: { xs: '100%', sm: 120 } }}
|
||||
/>
|
||||
<TextField
|
||||
label="Comment"
|
||||
size="small"
|
||||
value={comments[q.id] ?? ''}
|
||||
onChange={(e) => setComments((prev) => ({ ...prev, [q.id]: e.target.value }))}
|
||||
sx={{ flex: 1 }}
|
||||
/>
|
||||
</Stack>
|
||||
))}
|
||||
<Box sx={{ display: 'flex', justifyContent: 'flex-end', gap: 1 }}>
|
||||
<Button onClick={() => navigate(`/exam-marker/${batchId}/results`)}>Skip to results</Button>
|
||||
<Button variant="contained" startIcon={<SaveIcon />} onClick={saveSelected} disabled={saving || markableQuestions.length === 0}>
|
||||
{saving ? 'Saving…' : 'Save marks'}
|
||||
</Button>
|
||||
</Box>
|
||||
</Stack>
|
||||
) : (
|
||||
<Typography color="text.secondary" sx={{ py: 4, textAlign: 'center' }}>No submissions in this batch.</Typography>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</Stack>
|
||||
</Stack>
|
||||
</Container>
|
||||
);
|
||||
};
|
||||
|
||||
export default ExamMarkingPage;
|
||||
177
src/pages/exam/ExamResultsPage.tsx
Normal file
177
src/pages/exam/ExamResultsPage.tsx
Normal file
@ -0,0 +1,177 @@
|
||||
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { useNavigate, useParams } from 'react-router-dom';
|
||||
import {
|
||||
Alert,
|
||||
Box,
|
||||
Button,
|
||||
Chip,
|
||||
CircularProgress,
|
||||
Container,
|
||||
Paper,
|
||||
Stack,
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableContainer,
|
||||
TableHead,
|
||||
TableRow,
|
||||
Typography,
|
||||
} from '@mui/material';
|
||||
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
|
||||
import DownloadIcon from '@mui/icons-material/Download';
|
||||
import EditIcon from '@mui/icons-material/Edit';
|
||||
|
||||
import { examRepository } from '../../services/exam/examRepository';
|
||||
import type { BatchResultsResponse } from '../../types/exam.types';
|
||||
|
||||
function formatMark(value: number | null | undefined) {
|
||||
return value === null || value === undefined ? '' : String(value);
|
||||
}
|
||||
|
||||
const ExamResultsPage: React.FC = () => {
|
||||
const { batchId } = useParams<{ batchId: string }>();
|
||||
const navigate = useNavigate();
|
||||
const [data, setData] = useState<BatchResultsResponse | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [downloading, setDownloading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
if (!batchId) return;
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
setData(await examRepository.getBatchResults(batchId));
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : String(e));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [batchId]);
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
}, [load]);
|
||||
|
||||
const summary = useMemo(() => {
|
||||
const rows = data?.results ?? [];
|
||||
const presentTotals = rows
|
||||
.map((r) => r.total)
|
||||
.filter((v): v is number => typeof v === 'number');
|
||||
const average = presentTotals.length
|
||||
? presentTotals.reduce((sum, v) => sum + v, 0) / presentTotals.length
|
||||
: null;
|
||||
return {
|
||||
total: rows.length,
|
||||
absent: rows.filter((r) => r.status === 'absent' && r.total === null).length,
|
||||
marked: rows.filter((r) => r.total !== null).length,
|
||||
average,
|
||||
};
|
||||
}, [data]);
|
||||
|
||||
const downloadCsv = async () => {
|
||||
if (!batchId) return;
|
||||
setDownloading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const csv = await examRepository.getBatchCsv(batchId);
|
||||
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `batch-${batchId}.csv`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
URL.revokeObjectURL(url);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : String(e));
|
||||
} finally {
|
||||
setDownloading(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return <Box sx={{ display: 'flex', justifyContent: 'center', py: 8 }}><CircularProgress /></Box>;
|
||||
}
|
||||
|
||||
if (error || !data) {
|
||||
return (
|
||||
<Container maxWidth="lg" sx={{ py: 4 }}>
|
||||
<Alert severity="error">{error || 'Results not found'}</Alert>
|
||||
<Button sx={{ mt: 2 }} startIcon={<ArrowBackIcon />} onClick={() => navigate('/exam-marker')}>
|
||||
Back to Exam Marker
|
||||
</Button>
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container maxWidth="xl" sx={{ py: 4 }}>
|
||||
<Stack spacing={3}>
|
||||
<Box sx={{ display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between', gap: 2, flexWrap: 'wrap' }}>
|
||||
<Box>
|
||||
<Button size="small" startIcon={<ArrowBackIcon />} onClick={() => navigate('/exam-marker')} sx={{ mb: 1 }}>
|
||||
Exam Marker
|
||||
</Button>
|
||||
<Typography variant="h4" component="h1" fontWeight={700}>
|
||||
{data.batch.title || 'Exam results'}
|
||||
</Typography>
|
||||
<Typography variant="body2" color="text.secondary">
|
||||
Batch {data.batch.id} · created {new Date(data.batch.created_at).toLocaleDateString('en-GB')}
|
||||
</Typography>
|
||||
</Box>
|
||||
<Stack direction="row" spacing={1}>
|
||||
<Button variant="outlined" startIcon={<EditIcon />} onClick={() => navigate(`/exam-marker/${data.batch.id}/mark`)}>
|
||||
Mark
|
||||
</Button>
|
||||
<Button variant="contained" startIcon={<DownloadIcon />} onClick={downloadCsv} disabled={downloading}>
|
||||
{downloading ? 'Preparing…' : 'Download CSV'}
|
||||
</Button>
|
||||
</Stack>
|
||||
</Box>
|
||||
|
||||
<Stack direction="row" spacing={1} flexWrap="wrap" useFlexGap>
|
||||
<Chip label={`${summary.total} students`} />
|
||||
<Chip label={`${summary.marked} with marks`} color="success" variant="outlined" />
|
||||
<Chip label={`${summary.absent} absent/no scan`} color="warning" variant="outlined" />
|
||||
<Chip label={`Class average ${summary.average === null ? '—' : summary.average.toFixed(1)}`} color="primary" />
|
||||
</Stack>
|
||||
|
||||
<TableContainer component={Paper} variant="outlined">
|
||||
<Table size="small" stickyHeader>
|
||||
<TableHead>
|
||||
<TableRow>
|
||||
<TableCell>Student</TableCell>
|
||||
<TableCell>Status</TableCell>
|
||||
{data.questions.map((q) => (
|
||||
<TableCell key={q.id} align="right">{q.label} / {q.max_marks}</TableCell>
|
||||
))}
|
||||
<TableCell align="right">Total</TableCell>
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{data.results.map((row) => (
|
||||
<TableRow key={row.submission_id} sx={row.status === 'absent' && row.total === null ? { opacity: 0.72 } : undefined}>
|
||||
<TableCell>
|
||||
<Typography variant="body2" fontWeight={600}>{row.student_name || row.student_id || 'Unknown student'}</Typography>
|
||||
{row.student_id && <Typography variant="caption" color="text.secondary">{row.student_id}</Typography>}
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Chip size="small" label={row.status || 'unknown'} color={row.status === 'absent' ? 'warning' : 'default'} variant="outlined" />
|
||||
</TableCell>
|
||||
{data.questions.map((q) => (
|
||||
<TableCell key={q.id} align="right">{formatMark(row.marks[q.id])}</TableCell>
|
||||
))}
|
||||
<TableCell align="right"><strong>{formatMark(row.total)}</strong></TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</TableContainer>
|
||||
</Stack>
|
||||
</Container>
|
||||
);
|
||||
};
|
||||
|
||||
export default ExamResultsPage;
|
||||
169
src/pages/exam/ResultsWidget.tsx
Normal file
169
src/pages/exam/ResultsWidget.tsx
Normal file
@ -0,0 +1,169 @@
|
||||
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import {
|
||||
Alert,
|
||||
Box,
|
||||
Button,
|
||||
Card,
|
||||
CardContent,
|
||||
Chip,
|
||||
CircularProgress,
|
||||
MenuItem,
|
||||
Stack,
|
||||
TextField,
|
||||
Typography,
|
||||
} from '@mui/material';
|
||||
import AssessmentIcon from '@mui/icons-material/Assessment';
|
||||
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
|
||||
import TableChartIcon from '@mui/icons-material/TableChart';
|
||||
|
||||
import { examRepository } from '../../services/exam/examRepository';
|
||||
import type { BatchResultsResponse, ExamTemplate, MarkingBatch } from '../../types/exam.types';
|
||||
|
||||
interface ResultsWidgetProps {
|
||||
classId: string;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
function averageFromResults(results: BatchResultsResponse | null) {
|
||||
const totals = (results?.results ?? [])
|
||||
.map((row) => row.total)
|
||||
.filter((value): value is number => typeof value === 'number');
|
||||
if (!totals.length) return null;
|
||||
return totals.reduce((sum, value) => sum + value, 0) / totals.length;
|
||||
}
|
||||
|
||||
const ResultsWidget: React.FC<ResultsWidgetProps> = ({ classId, className }) => {
|
||||
const navigate = useNavigate();
|
||||
const [templates, setTemplates] = useState<ExamTemplate[]>([]);
|
||||
const [batches, setBatches] = useState<MarkingBatch[]>([]);
|
||||
const [latestResults, setLatestResults] = useState<BatchResultsResponse | null>(null);
|
||||
const [selectedTemplateId, setSelectedTemplateId] = useState('');
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [creating, setCreating] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const [nextTemplates, nextBatches] = await Promise.all([
|
||||
examRepository.listTemplates(),
|
||||
examRepository.listBatches(),
|
||||
]);
|
||||
const classBatches = nextBatches
|
||||
.filter((batch) => batch.class_id === classId)
|
||||
.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());
|
||||
setTemplates(nextTemplates);
|
||||
setSelectedTemplateId((current) => current || nextTemplates[0]?.id || '');
|
||||
setBatches(classBatches);
|
||||
setLatestResults(classBatches[0] ? await examRepository.getBatchResults(classBatches[0].id) : null);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : String(e));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [classId]);
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
}, [load]);
|
||||
|
||||
const latestBatch = batches[0] ?? null;
|
||||
const average = useMemo(() => averageFromResults(latestResults), [latestResults]);
|
||||
const absent = latestResults?.results.filter((row) => row.status === 'absent' && row.total === null).length ?? 0;
|
||||
|
||||
const createBatch = async () => {
|
||||
if (!selectedTemplateId) return;
|
||||
setCreating(true);
|
||||
setError(null);
|
||||
try {
|
||||
const template = templates.find((item) => item.id === selectedTemplateId);
|
||||
const created = await examRepository.createBatch({
|
||||
template_id: selectedTemplateId,
|
||||
class_id: classId,
|
||||
title: `${className || 'Class'} · ${template?.title || 'Exam'}`,
|
||||
});
|
||||
navigate(`/exam-marker/${created.id}/mark`);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : String(e));
|
||||
} finally {
|
||||
setCreating(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Card variant="outlined" sx={{ mb: 2 }}>
|
||||
<CardContent>
|
||||
<Stack spacing={2}>
|
||||
<Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 1, flexWrap: 'wrap' }}>
|
||||
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
|
||||
<AssessmentIcon color="primary" />
|
||||
<Box>
|
||||
<Typography variant="h6">Assessment results</Typography>
|
||||
<Typography variant="caption" color="text.secondary">Last exam summary for this class</Typography>
|
||||
</Box>
|
||||
</Box>
|
||||
{loading && <CircularProgress size={22} />}
|
||||
</Box>
|
||||
|
||||
{error && <Alert severity="warning" onClose={() => setError(null)}>{error}</Alert>}
|
||||
|
||||
{latestBatch ? (
|
||||
<Stack spacing={1.5}>
|
||||
<Box>
|
||||
<Typography variant="body2" fontWeight={700}>{latestBatch.title || 'Exam batch'}</Typography>
|
||||
<Typography variant="caption" color="text.secondary">
|
||||
{new Date(latestBatch.created_at).toLocaleDateString('en-GB')}
|
||||
</Typography>
|
||||
</Box>
|
||||
<Stack direction="row" spacing={1} flexWrap="wrap" useFlexGap>
|
||||
<Chip size="small" color="primary" label={`Average ${average === null ? '—' : average.toFixed(1)}`} />
|
||||
<Chip size="small" variant="outlined" label={`${latestResults?.results.length ?? 0} students`} />
|
||||
<Chip size="small" color="warning" variant="outlined" label={`${absent} absent`} />
|
||||
</Stack>
|
||||
<Stack direction="row" spacing={1}>
|
||||
<Button size="small" variant="contained" startIcon={<TableChartIcon />} onClick={() => navigate(`/exam-marker/${latestBatch.id}/results`)}>
|
||||
View results
|
||||
</Button>
|
||||
<Button size="small" variant="outlined" onClick={() => navigate(`/exam-marker/${latestBatch.id}/mark`)}>
|
||||
Continue marking
|
||||
</Button>
|
||||
</Stack>
|
||||
</Stack>
|
||||
) : !loading ? (
|
||||
<Typography variant="body2" color="text.secondary">
|
||||
No exam batches have been created for this class yet.
|
||||
</Typography>
|
||||
) : null}
|
||||
|
||||
<Stack direction={{ xs: 'column', sm: 'row' }} spacing={1} alignItems={{ xs: 'stretch', sm: 'center' }}>
|
||||
<TextField
|
||||
select
|
||||
size="small"
|
||||
label="Template"
|
||||
value={selectedTemplateId}
|
||||
onChange={(e) => setSelectedTemplateId(e.target.value)}
|
||||
disabled={!templates.length || creating}
|
||||
sx={{ minWidth: 260 }}
|
||||
>
|
||||
{templates.map((template) => (
|
||||
<MenuItem key={template.id} value={template.id}>{template.title}</MenuItem>
|
||||
))}
|
||||
</TextField>
|
||||
<Button
|
||||
variant="outlined"
|
||||
startIcon={<PlayArrowIcon />}
|
||||
onClick={createBatch}
|
||||
disabled={!selectedTemplateId || creating}
|
||||
>
|
||||
{creating ? 'Creating…' : 'Create marking batch'}
|
||||
</Button>
|
||||
</Stack>
|
||||
</Stack>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
|
||||
export default ResultsWidget;
|
||||
@ -1,3 +1,6 @@
|
||||
export { default as ExamDashboardPage } from './ExamDashboardPage';
|
||||
export { default as ExamTemplateSetupPage } from './setup/ExamTemplateSetupPage';
|
||||
export { default as MarkSchemePage } from './MarkSchemePage';
|
||||
export { default as ExamMarkingPage } from './ExamMarkingPage';
|
||||
export { default as ExamResultsPage } from './ExamResultsPage';
|
||||
export { default as ResultsWidget } from './ResultsWidget';
|
||||
|
||||
@ -9,6 +9,7 @@ import {
|
||||
ArrowBack, PersonAdd, PersonRemove, CheckCircle, Cancel, School,
|
||||
} from '@mui/icons-material';
|
||||
import { useAuth } from '../../contexts/AuthContext';
|
||||
import { ResultsWidget } from '../exam';
|
||||
|
||||
const API_BASE = import.meta.env.VITE_API_BASE || import.meta.env.VITE_API_URL || '/api';
|
||||
|
||||
@ -254,6 +255,8 @@ const ClassDetailPage: React.FC = () => {
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<ResultsWidget classId={cls.id} className={cls.name} />
|
||||
|
||||
{/* Tabs */}
|
||||
<Tabs value={tab} onChange={(_, v) => setTab(v)} sx={{ borderBottom: 1, borderColor: 'divider', mb: 2 }}>
|
||||
<Tab label={`Students (${cls.student_count})`} />
|
||||
|
||||
@ -11,12 +11,17 @@ import { API_BASE } from '../../config/apiConfig';
|
||||
import { logger } from '../../debugConfig';
|
||||
import { supabase } from '../../supabaseClient';
|
||||
import type {
|
||||
BatchQueueResponse,
|
||||
BatchResultsResponse,
|
||||
CreateBatchPayload,
|
||||
CreateTemplatePayload,
|
||||
ExamBoundary,
|
||||
ExamQuestion,
|
||||
ExamResponseArea,
|
||||
ExamTemplate,
|
||||
ExamTemplateDetail,
|
||||
MarkingBatch,
|
||||
MarkUpsertPayload,
|
||||
Neo4jSyncResult,
|
||||
PatchQuestionPayload,
|
||||
SpecPoint,
|
||||
@ -209,6 +214,48 @@ export const examRepository = {
|
||||
const res = await axios.post<Neo4jSyncResult>(`${EXAM_BASE}/templates/${templateId}/neo4j-sync`, {}, { headers });
|
||||
return res.data;
|
||||
},
|
||||
|
||||
async createBatch(payload: CreateBatchPayload): Promise<MarkingBatch> {
|
||||
const headers = await authHeaders();
|
||||
const res = await axios.post<MarkingBatch>(`${EXAM_BASE}/batches`, payload, { headers });
|
||||
return res.data;
|
||||
},
|
||||
|
||||
async listBatches(params: { includeArchived?: boolean; templateId?: string } = {}): Promise<MarkingBatch[]> {
|
||||
const headers = await authHeaders();
|
||||
const res = await axios.get<{ batches: MarkingBatch[] }>(`${EXAM_BASE}/batches`, {
|
||||
headers,
|
||||
params: {
|
||||
include_archived: params.includeArchived ?? false,
|
||||
template_id: params.templateId,
|
||||
},
|
||||
});
|
||||
return res.data.batches ?? [];
|
||||
},
|
||||
|
||||
async getBatchQueue(batchId: string): Promise<BatchQueueResponse> {
|
||||
const headers = await authHeaders();
|
||||
const res = await axios.get<BatchQueueResponse>(`${EXAM_BASE}/batches/${batchId}/queue`, { headers });
|
||||
return res.data;
|
||||
},
|
||||
|
||||
async getBatchResults(batchId: string): Promise<BatchResultsResponse> {
|
||||
const headers = await authHeaders();
|
||||
const res = await axios.get<BatchResultsResponse>(`${EXAM_BASE}/batches/${batchId}/results`, { headers });
|
||||
return res.data;
|
||||
},
|
||||
|
||||
async getBatchCsv(batchId: string): Promise<string> {
|
||||
const headers = await authHeaders();
|
||||
const res = await axios.get<string>(`${EXAM_BASE}/batches/${batchId}/csv`, { headers, responseType: 'text' });
|
||||
return res.data;
|
||||
},
|
||||
|
||||
async upsertMark(markId: string, payload: MarkUpsertPayload): Promise<unknown> {
|
||||
const headers = await authHeaders();
|
||||
const res = await axios.put(`${EXAM_BASE}/marks/${markId}`, payload, { headers });
|
||||
return res.data;
|
||||
},
|
||||
};
|
||||
|
||||
export default examRepository;
|
||||
|
||||
@ -201,3 +201,68 @@ export interface Neo4jSyncResult {
|
||||
status: string;
|
||||
projection?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface MarkingBatch {
|
||||
id: string;
|
||||
template_id: string;
|
||||
class_id: string | null;
|
||||
institute_id: string;
|
||||
teacher_id: string;
|
||||
title: string | null;
|
||||
status: 'open' | 'closed' | 'archived' | string;
|
||||
created_at: string;
|
||||
updated_at?: string;
|
||||
submission_count?: number;
|
||||
}
|
||||
|
||||
export interface StudentSubmission {
|
||||
id: string;
|
||||
batch_id: string;
|
||||
student_id: string | null;
|
||||
student_name: string | null;
|
||||
status: 'absent' | 'unmatched' | 'matched' | 'marking' | 'complete' | string;
|
||||
storage_path?: string | null;
|
||||
mark_entry_count?: number;
|
||||
}
|
||||
|
||||
export interface BatchQueueResponse {
|
||||
batch: MarkingBatch;
|
||||
submissions: StudentSubmission[];
|
||||
progress: {
|
||||
total: number;
|
||||
absent: number;
|
||||
complete: number;
|
||||
in_progress: number;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ExamResultRow {
|
||||
submission_id: string;
|
||||
student_id: string | null;
|
||||
student_name: string | null;
|
||||
status: string | null;
|
||||
marks: Record<string, number | null | undefined>;
|
||||
total: number | null;
|
||||
}
|
||||
|
||||
export interface BatchResultsResponse {
|
||||
batch: MarkingBatch;
|
||||
questions: Array<Pick<ExamQuestion, 'id' | 'label' | 'max_marks' | 'order'>>;
|
||||
results: ExamResultRow[];
|
||||
}
|
||||
|
||||
export interface CreateBatchPayload {
|
||||
template_id: string;
|
||||
class_id?: string;
|
||||
title?: string;
|
||||
}
|
||||
|
||||
export interface MarkUpsertPayload {
|
||||
submission_id: string;
|
||||
question_id: string;
|
||||
awarded_marks: number;
|
||||
mark_scheme_detail?: Record<string, unknown>;
|
||||
annotation_shape_ids?: unknown;
|
||||
comment?: string;
|
||||
confirmed?: boolean;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user