Updated users to get exams related to their level

This commit is contained in:
Tiago Ribeiro
2024-02-10 09:10:52 +00:00
parent 1895b9e183
commit 2e894622d0
3 changed files with 29 additions and 8 deletions

View File

@@ -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 =

View File

@@ -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;

View File

@@ -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;
};