diff --git a/src/interfaces/exam.ts b/src/interfaces/exam.ts index 6374d9ed..af431f65 100644 --- a/src/interfaces/exam.ts +++ b/src/interfaces/exam.ts @@ -3,6 +3,7 @@ import {Module} from "."; export type Exam = ReadingExam | ListeningExam | WritingExam | SpeakingExam | LevelExam; export type Variant = "full" | "partial"; export type InstructorGender = "male" | "female" | "varied"; +export type Difficulty = "easy" | "medium" | "hard"; export interface ReadingExam { parts: ReadingPart[]; @@ -12,6 +13,7 @@ export interface ReadingExam { type: "academic" | "general"; isDiagnostic: boolean; variant?: Variant; + difficulty?: Difficulty; } export interface ReadingPart { @@ -29,6 +31,7 @@ export interface LevelExam { minTimer: number; isDiagnostic: boolean; variant?: Variant; + difficulty?: Difficulty; } export interface ListeningExam { @@ -38,6 +41,7 @@ export interface ListeningExam { minTimer: number; isDiagnostic: boolean; variant?: Variant; + difficulty?: Difficulty; } export interface ListeningPart { @@ -69,6 +73,7 @@ export interface WritingExam { minTimer: number; isDiagnostic: boolean; variant?: Variant; + difficulty?: Difficulty; } interface WordCounter { @@ -84,6 +89,7 @@ export interface SpeakingExam { isDiagnostic: boolean; variant?: Variant; instructorGender: InstructorGender; + difficulty?: Difficulty; } export type Exercise = diff --git a/src/pages/api/exam/[module]/index.ts b/src/pages/api/exam/[module]/index.ts index 46fa98f8..e7ff0699 100644 --- a/src/pages/api/exam/[module]/index.ts +++ b/src/pages/api/exam/[module]/index.ts @@ -6,6 +6,7 @@ import {withIronSessionApiRoute} from "iron-session/next"; import {sessionOptions} from "@/lib/session"; import {Exam, InstructorGender, Variant} from "@/interfaces/exam"; import {getExams} from "@/utils/exams.be"; +import {Module} from "@/interfaces"; const db = getFirestore(app); export default withIronSessionApiRoute(handler, sessionOptions); @@ -24,7 +25,7 @@ async function GET(req: NextApiRequest, res: NextApiResponse) { } const {module, avoidRepeated, variant, instructorGender} = req.query as { - module: string; + module: Module; avoidRepeated: string; variant?: Variant; instructorGender?: InstructorGender; diff --git a/src/utils/exams.be.ts b/src/utils/exams.be.ts index 76ee4d1e..01b67116 100644 --- a/src/utils/exams.be.ts +++ b/src/utils/exams.be.ts @@ -1,11 +1,12 @@ -import {collection, getDocs, query, where, setDoc, doc, Firestore} from "firebase/firestore"; +import {collection, getDocs, query, where, setDoc, doc, Firestore, getDoc} from "firebase/firestore"; import {shuffle} from "lodash"; -import {Exam, InstructorGender, Variant} from "@/interfaces/exam"; -import {Stat} from "@/interfaces/user"; +import {Difficulty, Exam, InstructorGender, Variant} from "@/interfaces/exam"; +import {Stat, User} from "@/interfaces/user"; +import {Module} from "@/interfaces"; export const getExams = async ( db: Firestore, - module: string, + module: Module, avoidRepeated: string, // added userId as due to assignments being set from the teacher to the student // we need to make sure we are serving exams not executed by the user and not @@ -29,6 +30,7 @@ export const getExams = async ( const variantExams: Exam[] = filterByVariant(allExams, variant); const genderedExams: Exam[] = filterByInstructorGender(variantExams, instructorGender); + const difficultyExams: Exam[] = await filterByDifficulty(db, genderedExams, module, userId); if (avoidRepeated === "true") { const statsQ = query(collection(db, "stats"), where("user", "==", userId)); @@ -38,12 +40,12 @@ export const getExams = async ( id: doc.id, ...doc.data(), })) as unknown as Stat[]; - const filteredExams = genderedExams.filter((x) => !stats.map((s) => s.exam).includes(x.id)); + const filteredExams = difficultyExams.filter((x) => !stats.map((s) => s.exam).includes(x.id)); - return filteredExams.length > 0 ? filteredExams : genderedExams; + return filteredExams.length > 0 ? filteredExams : difficultyExams; } - return genderedExams; + return difficultyExams; }; const filterByInstructorGender = (exams: Exam[], instructorGender?: InstructorGender) => { @@ -56,3 +58,15 @@ const filterByVariant = (exams: Exam[], variant?: Variant) => { const filtered = variant && variant === "partial" ? exams.filter((x) => x.variant === "partial") : exams.filter((x) => x.variant !== "partial"); return filtered.length > 0 ? filtered : exams; }; + +const filterByDifficulty = async (db: Firestore, exams: Exam[], module: Module, userID?: string) => { + if (!userID) return exams; + const userRef = await getDoc(doc(db, "users", userID)); + if (!userRef.exists()) return exams; + + const user = {...userRef.data(), id: userRef.id} as User; + const difficulty = user.levels[module] <= 3 ? "easy" : user.levels[module] <= 6 ? "medium" : "hard"; + + const filteredExams = exams.filter((exam) => exam.difficulty === difficulty); + return filteredExams.length === 0 ? exams : filteredExams; +};