Final improvements for Groups PDF's
This commit is contained in:
@@ -32,6 +32,11 @@ import {
|
||||
streamToBuffer,
|
||||
} from "@/utils/pdf";
|
||||
|
||||
interface GroupScoreSummaryHelper {
|
||||
score: [number, number];
|
||||
label: string;
|
||||
sessions: string[];
|
||||
}
|
||||
const db = getFirestore(app);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
@@ -99,13 +104,7 @@ const getScoreAndTotal = (stats: Stat[]) => {
|
||||
);
|
||||
};
|
||||
|
||||
const getLevelScoreForUserExams = (
|
||||
correct: number,
|
||||
total: number,
|
||||
module: Module,
|
||||
focus: "academic" | "general"
|
||||
) => {
|
||||
const bandScore = calculateBandScore(correct, total, module, focus);
|
||||
const getLevelScoreForUserExams = (bandScore: number) => {
|
||||
const [level] = getLevelScore(bandScore);
|
||||
return level;
|
||||
};
|
||||
@@ -158,20 +157,45 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
[]
|
||||
) as Stat[];
|
||||
|
||||
const docsSnap = await getDocs(
|
||||
query(
|
||||
collection(db, "users"),
|
||||
where(documentId(), "in", data.assignees)
|
||||
)
|
||||
);
|
||||
const users = docsSnap.docs.map((d) => ({
|
||||
...d.data(),
|
||||
id: d.id,
|
||||
})) as User[];
|
||||
|
||||
const flattenResultsWithGrade = flattenResults.map((e) => {
|
||||
const focus = users.find((u) => u.id === e.user)?.focus || "academic";
|
||||
const bandScore = calculateBandScore(
|
||||
e.score.correct,
|
||||
e.score.total,
|
||||
e.module,
|
||||
focus
|
||||
);
|
||||
|
||||
return { ...e, bandScore };
|
||||
});
|
||||
|
||||
const moduleResults = data.exams.map(({ module }) => {
|
||||
const moduleResults = flattenResults.filter(
|
||||
const moduleResults = flattenResultsWithGrade.filter(
|
||||
(e) => e.module === module
|
||||
);
|
||||
|
||||
const bandScore =
|
||||
moduleResults.reduce((accm, curr) => accm + curr.bandScore, 0) /
|
||||
moduleResults.length;
|
||||
const { correct, total } = getScoreAndTotal(moduleResults);
|
||||
const score = calculateBandScore(correct, total, module, "academic");
|
||||
const png = getRadialProgressPNG("azul", score, total);
|
||||
const png = getRadialProgressPNG("azul", correct, total);
|
||||
|
||||
return {
|
||||
bandScore: score,
|
||||
bandScore,
|
||||
png,
|
||||
module: module[0].toUpperCase() + module.substring(1),
|
||||
score,
|
||||
score: bandScore,
|
||||
total,
|
||||
code: module,
|
||||
};
|
||||
@@ -181,12 +205,17 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
getScoreAndTotal(flattenResults);
|
||||
const overallResult = overallCorrect / overallTotal;
|
||||
|
||||
const overallPNG = getRadialProgressPNG(
|
||||
"laranja",
|
||||
overallCorrect,
|
||||
overallTotal
|
||||
);
|
||||
// generate the overall detail report
|
||||
const overallDetail = {
|
||||
module: "Overall",
|
||||
score: overallCorrect,
|
||||
total: overallTotal,
|
||||
png: getRadialProgressPNG("laranja", overallCorrect, overallTotal),
|
||||
png: overallPNG,
|
||||
} as ModuleScore;
|
||||
|
||||
const testDetails = [overallDetail, ...moduleResults];
|
||||
@@ -207,7 +236,12 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (showLevel) {
|
||||
return {
|
||||
title: "GROUP ENGLISH LEVEL TEST RESULT REPORT ",
|
||||
details: <LevelExamDetails detail={overallDetail} />,
|
||||
details: (
|
||||
<LevelExamDetails
|
||||
detail={overallDetail}
|
||||
title="Group Average CEFR"
|
||||
/>
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -222,21 +256,9 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
const numberOfStudents = data.assignees.length;
|
||||
|
||||
const getStudentsData = async (): Promise<StudentData[]> => {
|
||||
// const usersCol = collection(db, "users");
|
||||
const docsSnap = await getDocs(
|
||||
query(
|
||||
collection(db, "users"),
|
||||
where(documentId(), "in", data.assignees)
|
||||
)
|
||||
);
|
||||
const users = docsSnap.docs.map((d) => ({
|
||||
...d.data(),
|
||||
id: d.id,
|
||||
})) as User[];
|
||||
|
||||
return data.assignees.map((id) => {
|
||||
const user = users.find((u) => u.id === id);
|
||||
const exams = flattenResults.filter((e) => e.user === id);
|
||||
const exams = flattenResultsWithGrade.filter((e) => e.user === id);
|
||||
const date =
|
||||
exams.length === 0
|
||||
? "N/A"
|
||||
@@ -246,6 +268,11 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
day: "numeric",
|
||||
});
|
||||
|
||||
const bandScore =
|
||||
exams.length === 0
|
||||
? 0
|
||||
: exams.reduce((accm, curr) => accm + curr.bandScore, 0) /
|
||||
exams.length;
|
||||
const { correct, total } = getScoreAndTotal(exams);
|
||||
|
||||
const result = exams.length === 0 ? "N/A" : `${correct}/${total}`;
|
||||
@@ -258,19 +285,60 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
date,
|
||||
result,
|
||||
level: showLevel
|
||||
? getLevelScoreForUserExams(
|
||||
correct,
|
||||
total,
|
||||
baseStat.module,
|
||||
user?.focus || "academic"
|
||||
)
|
||||
: "",
|
||||
? getLevelScoreForUserExams(bandScore)
|
||||
: undefined,
|
||||
bandScore,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
const studentsData = await getStudentsData();
|
||||
|
||||
const getGroupScoreSummary = () => {
|
||||
const resultHelper = studentsData.reduce(
|
||||
(accm: GroupScoreSummaryHelper[], curr) => {
|
||||
const { bandScore, id } = curr;
|
||||
|
||||
const flooredScore = Math.floor(bandScore);
|
||||
|
||||
const hasMatch = accm.find((a) => a.score.includes(flooredScore));
|
||||
if (hasMatch) {
|
||||
return accm.map((a) => {
|
||||
if (a.score.includes(flooredScore)) {
|
||||
return {
|
||||
...a,
|
||||
sessions: [...a.sessions, id],
|
||||
};
|
||||
}
|
||||
|
||||
return a;
|
||||
});
|
||||
}
|
||||
|
||||
return [
|
||||
...accm,
|
||||
{
|
||||
score: [flooredScore, flooredScore + 0.5],
|
||||
label: `${flooredScore} - ${flooredScore + 0.5}`,
|
||||
sessions: [id],
|
||||
},
|
||||
];
|
||||
},
|
||||
[]
|
||||
) as GroupScoreSummaryHelper[];
|
||||
|
||||
const result = resultHelper.map(({ label, sessions }) => {
|
||||
return {
|
||||
label,
|
||||
percent: Math.floor((sessions.length / numberOfStudents) * 100),
|
||||
description: `No. Candidates ${sessions.length} of ${numberOfStudents}`,
|
||||
};
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
const groupScoreSummary = getGroupScoreSummary();
|
||||
|
||||
const pdfStream = await ReactPDF.renderToStream(
|
||||
<GroupTestReport
|
||||
title={title}
|
||||
@@ -288,6 +356,9 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
institution="TODO: PLACEHOLDER"
|
||||
studentsData={studentsData}
|
||||
showLevel={showLevel}
|
||||
summaryPNG={overallPNG}
|
||||
summaryScore={`${(overallResult * 100).toFixed(0)}%`}
|
||||
groupScoreSummary={groupScoreSummary}
|
||||
/>
|
||||
);
|
||||
|
||||
|
||||
@@ -284,7 +284,12 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (stat.module === "level") {
|
||||
return {
|
||||
title: "ENGLISH LEVEL TEST RESULT REPORT ",
|
||||
details: <LevelExamDetails detail={overallDetail} />,
|
||||
details: (
|
||||
<LevelExamDetails
|
||||
detail={overallDetail}
|
||||
title="Level as per CEFR Levels"
|
||||
/>
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user