82 lines
2.5 KiB
TypeScript
82 lines
2.5 KiB
TypeScript
import {Module} from "@/interfaces";
|
|
import {Exam, ReadingExam, ListeningExam, WritingExam, SpeakingExam, Exercise, UserSolution, LevelExam} from "@/interfaces/exam";
|
|
import axios from "axios";
|
|
|
|
export const getExam = async (module: Module, avoidRepeated: boolean): Promise<Exam | undefined> => {
|
|
const examRequest = await axios<Exam[]>(`/api/exam/${module}?avoidRepeated=${avoidRepeated}`);
|
|
if (examRequest.status !== 200) {
|
|
return undefined;
|
|
}
|
|
|
|
const newExam = examRequest.data;
|
|
|
|
switch (module) {
|
|
case "reading":
|
|
return newExam.shift() as ReadingExam;
|
|
case "listening":
|
|
return newExam.shift() as ListeningExam;
|
|
case "writing":
|
|
return newExam.shift() as WritingExam;
|
|
case "speaking":
|
|
return newExam.shift() as SpeakingExam;
|
|
case "level":
|
|
return newExam.shift() as LevelExam;
|
|
}
|
|
};
|
|
|
|
export const getExamById = async (module: Module, id: string): Promise<Exam | undefined> => {
|
|
const examRequest = await axios<Exam>(`/api/exam/${module}/${id}`);
|
|
if (examRequest.status !== 200) {
|
|
return undefined;
|
|
}
|
|
|
|
const newExam = examRequest.data;
|
|
|
|
switch (module) {
|
|
case "reading":
|
|
return newExam as ReadingExam;
|
|
case "listening":
|
|
return newExam as ListeningExam;
|
|
case "writing":
|
|
return newExam as WritingExam;
|
|
case "speaking":
|
|
return newExam as SpeakingExam;
|
|
case "level":
|
|
return newExam as LevelExam;
|
|
}
|
|
};
|
|
|
|
export const defaultUserSolutions = (exercise: Exercise, exam: Exam): UserSolution => {
|
|
const defaultSettings = {
|
|
exam: exam.id,
|
|
exercise: exercise.id,
|
|
solutions: [],
|
|
module: exam.module,
|
|
type: exercise.type,
|
|
};
|
|
|
|
let total = 0;
|
|
switch (exercise.type) {
|
|
case "fillBlanks":
|
|
total = exercise.text.match(/({{\d+}})/g)?.length || 0;
|
|
return {...defaultSettings, score: {correct: 0, total, missing: total}};
|
|
case "matchSentences":
|
|
total = exercise.sentences.length;
|
|
return {...defaultSettings, score: {correct: 0, total, missing: total}};
|
|
case "multipleChoice":
|
|
total = exercise.questions.length;
|
|
return {...defaultSettings, score: {correct: 0, total, missing: total}};
|
|
case "writeBlanks":
|
|
total = exercise.text.match(/({{\d+}})/g)?.length || 0;
|
|
return {...defaultSettings, score: {correct: 0, total, missing: total}};
|
|
case "writing":
|
|
total = 1;
|
|
return {...defaultSettings, score: {correct: 0, total, missing: total}};
|
|
case "speaking":
|
|
total = 1;
|
|
return {...defaultSettings, score: {correct: 0, total, missing: total}};
|
|
default:
|
|
return {...defaultSettings, score: {correct: 0, total: 0, missing: 0}};
|
|
}
|
|
};
|