Added the ability for assignments to use partial exams as well
This commit is contained in:
@@ -7,7 +7,7 @@ import {sessionOptions} from "@/lib/session";
|
||||
import {uuidv4} from "@firebase/util";
|
||||
import {Module} from "@/interfaces";
|
||||
import {getExams} from "@/utils/exams.be";
|
||||
import {Exam} from "@/interfaces/exam";
|
||||
import {Exam, Variant} from "@/interfaces/exam";
|
||||
import {capitalize, flatten} from "lodash";
|
||||
import {User} from "@/interfaces/user";
|
||||
import moment from "moment";
|
||||
@@ -52,13 +52,18 @@ function getRandomIndex(arr: any[]): number {
|
||||
return randomIndex;
|
||||
}
|
||||
|
||||
const generateExams = async (generateMultiple: Boolean, selectedModules: Module[], assignees: string[]): Promise<ExamWithUser[]> => {
|
||||
const generateExams = async (
|
||||
generateMultiple: Boolean,
|
||||
selectedModules: Module[],
|
||||
assignees: string[],
|
||||
variant?: Variant,
|
||||
): Promise<ExamWithUser[]> => {
|
||||
if (generateMultiple) {
|
||||
// for optimization purposes, it would be better to create a new endpoint that returned the answers for all users at once
|
||||
const allExams = await assignees.map(async (assignee) => {
|
||||
const selectedModulePromises = await selectedModules.map(async (module: Module) => {
|
||||
try {
|
||||
const exams: Exam[] = await getExams(db, module, "true", assignee);
|
||||
const exams: Exam[] = await getExams(db, module, "true", assignee, variant);
|
||||
|
||||
const exam = exams[getRandomIndex(exams)];
|
||||
if (exam) {
|
||||
@@ -101,6 +106,7 @@ async function POST(req: NextApiRequest, res: NextApiResponse) {
|
||||
// Generate multiple true would generate an unique exam for each user
|
||||
// false would generate the same exam for all users
|
||||
generateMultiple = false,
|
||||
variant,
|
||||
...body
|
||||
} = req.body as {
|
||||
selectedModules: Module[];
|
||||
@@ -109,9 +115,10 @@ async function POST(req: NextApiRequest, res: NextApiResponse) {
|
||||
name: string;
|
||||
startDate: string;
|
||||
endDate: string;
|
||||
variant?: Variant;
|
||||
};
|
||||
|
||||
const exams: ExamWithUser[] = await generateExams(generateMultiple, selectedModules, assignees);
|
||||
const exams: ExamWithUser[] = await generateExams(generateMultiple, selectedModules, assignees, variant);
|
||||
|
||||
if (exams.length === 0) {
|
||||
res.status(400).json({ok: false, error: "No exams found for the selected modules"});
|
||||
|
||||
@@ -176,7 +176,7 @@ export default function Home({envVariables}: {envVariables: {[key: string]: stri
|
||||
{user.type === "corporate" && <CorporateDashboard user={user} />}
|
||||
{user.type === "agent" && <AgentDashboard user={user} />}
|
||||
{user.type === "admin" && <AdminDashboard user={user} />}
|
||||
{user.type === "developer" && <AdminDashboard user={user} />}
|
||||
{user.type === "developer" && <TeacherDashboard user={user} />}
|
||||
</Layout>
|
||||
)}
|
||||
</>
|
||||
|
||||
Reference in New Issue
Block a user