Added the ability to choose between partial and full exams

This commit is contained in:
Tiago Ribeiro
2024-01-23 10:11:04 +00:00
parent 67c2e06575
commit 74d3f30c93
7 changed files with 98 additions and 71 deletions

View File

@@ -1,52 +1,51 @@
import {
collection,
getDocs,
query,
where,
setDoc,
doc,
Firestore,
} from "firebase/firestore";
import { shuffle } from "lodash";
import { Exam } from "@/interfaces/exam";
import { Stat } from "@/interfaces/user";
import {collection, getDocs, query, where, setDoc, doc, Firestore} from "firebase/firestore";
import {shuffle} from "lodash";
import {Exam, Variant} from "@/interfaces/exam";
import {Stat} from "@/interfaces/user";
export const getExams = async (
db: Firestore,
module: string,
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
// by the teacher that performed the request
userId: string | undefined
db: Firestore,
module: string,
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
// by the teacher that performed the request
userId: string | undefined,
variant?: Variant,
): Promise<Exam[]> => {
const moduleRef = collection(db, module);
const moduleRef = collection(db, module);
const q = query(moduleRef, where("isDiagnostic", "==", false));
const snapshot = await getDocs(q);
const q = query(moduleRef, where("isDiagnostic", "==", false));
const snapshot = await getDocs(q);
const exams: Exam[] = shuffle(
snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
module,
}))
) as Exam[];
const exams: Exam[] = filterByVariant(
shuffle(
snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
module,
})),
) as Exam[],
variant,
);
if (avoidRepeated === "true") {
const statsQ = query(collection(db, "stats"), where("user", "==", userId));
const statsSnapshot = await getDocs(statsQ);
if (avoidRepeated === "true") {
const statsQ = query(collection(db, "stats"), where("user", "==", userId));
const statsSnapshot = await getDocs(statsQ);
const stats: Stat[] = statsSnapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
})) as unknown as Stat[];
const filteredExams = exams.filter(
(x) => !stats.map((s) => s.exam).includes(x.id)
);
const stats: Stat[] = statsSnapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
})) as unknown as Stat[];
const filteredExams = exams.filter((x) => !stats.map((s) => s.exam).includes(x.id));
return filteredExams.length > 0 ? filteredExams : exams;
}
return filteredExams.length > 0 ? filteredExams : exams;
}
return exams;
return exams;
};
const filterByVariant = (exams: Exam[], variant?: Variant) => {
const filtered = variant && variant === "partial" ? exams.filter((x) => x.variant === "partial") : exams;
return filtered.length > 0 ? filtered : exams;
};