OpenMAIC-React / src /lib /quiz /grading.ts
muthuk1's picture
Convert OpenMAIC from Next.js to React (Vite)
f56a29b verified
import type { QuizQuestion } from '@/lib/types/stage';
export interface QuestionResult {
questionId: string;
correct: boolean | null;
status: 'correct' | 'incorrect';
earned: number;
aiComment?: string;
}
export function arraysEqual(a: string[], b: string[]): boolean {
if (a.length !== b.length) return false;
const sa = [...a].sort();
const sb = [...b].sort();
return sa.every((v, i) => v === sb[i]);
}
export function toArray(v: string | string[] | undefined): string[] {
if (!v) return [];
return Array.isArray(v) ? v : [v];
}
export function isShortAnswer(q: QuizQuestion): boolean {
return q.type === 'short_answer' || (!q.hasAnswer && (!q.answer || q.answer.length === 0));
}
/** Grade choice questions locally. Returns results only for non-short-answer questions. */
export function gradeChoiceQuestions(
questions: QuizQuestion[],
answers: Record<string, string | string[]>,
): QuestionResult[] {
return questions
.filter((q) => !isShortAnswer(q))
.map((q) => {
const pts = q.points ?? 1;
const userAnswer = toArray(answers[q.id]);
const correctAnswer = toArray(q.answer);
const correct = arraysEqual(userAnswer, correctAnswer);
return {
questionId: q.id,
correct,
status: correct ? ('correct' as const) : ('incorrect' as const),
earned: correct ? pts : 0,
};
});
}