Added the ability to choose between partial and full exams
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
import {Module} from "@/interfaces";
|
||||
import {Exam, ReadingExam, ListeningExam, WritingExam, SpeakingExam, Exercise, UserSolution, LevelExam} from "@/interfaces/exam";
|
||||
import {Exam, ReadingExam, ListeningExam, WritingExam, SpeakingExam, Exercise, UserSolution, LevelExam, Variant} 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}`);
|
||||
export const getExam = async (module: Module, avoidRepeated: boolean, variant?: Variant): Promise<Exam | undefined> => {
|
||||
const url = new URLSearchParams();
|
||||
url.append("avoidRepeated", avoidRepeated.toString());
|
||||
if (variant) url.append("variant", variant);
|
||||
|
||||
const examRequest = await axios<Exam[]>(`/api/exam/${module}?${url.toString()}`);
|
||||
if (examRequest.status !== 200) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user