|
|
|
@@ -1,430 +1,434 @@
|
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import type {NextApiRequest, NextApiResponse} from "next";
|
|
|
|
import { app, storage } from "@/firebase";
|
|
|
|
import {app, storage} from "@/firebase";
|
|
|
|
import {
|
|
|
|
import {getFirestore, doc, getDoc, updateDoc, getDocs, query, collection, where, documentId} from "firebase/firestore";
|
|
|
|
getFirestore,
|
|
|
|
import {withIronSessionApiRoute} from "iron-session/next";
|
|
|
|
doc,
|
|
|
|
import {sessionOptions} from "@/lib/session";
|
|
|
|
getDoc,
|
|
|
|
|
|
|
|
updateDoc,
|
|
|
|
|
|
|
|
getDocs,
|
|
|
|
|
|
|
|
query,
|
|
|
|
|
|
|
|
collection,
|
|
|
|
|
|
|
|
where,
|
|
|
|
|
|
|
|
documentId,
|
|
|
|
|
|
|
|
} from "firebase/firestore";
|
|
|
|
|
|
|
|
import { withIronSessionApiRoute } from "iron-session/next";
|
|
|
|
|
|
|
|
import { sessionOptions } from "@/lib/session";
|
|
|
|
|
|
|
|
import ReactPDF from "@react-pdf/renderer";
|
|
|
|
import ReactPDF from "@react-pdf/renderer";
|
|
|
|
import GroupTestReport from "@/exams/pdf/group.test.report";
|
|
|
|
import GroupTestReport from "@/exams/pdf/group.test.report";
|
|
|
|
import { ref, uploadBytes, getDownloadURL } from "firebase/storage";
|
|
|
|
import {ref, uploadBytes, getDownloadURL} from "firebase/storage";
|
|
|
|
import { Stat } from "@/interfaces/user";
|
|
|
|
import {Stat, CorporateUser} from "@/interfaces/user";
|
|
|
|
import { User } from "@/interfaces/user";
|
|
|
|
import {User, DemographicInformation} from "@/interfaces/user";
|
|
|
|
import { Module } from "@/interfaces";
|
|
|
|
import {Module} from "@/interfaces";
|
|
|
|
import { ModuleScore, StudentData } from "@/interfaces/module.scores";
|
|
|
|
import {ModuleScore, StudentData} from "@/interfaces/module.scores";
|
|
|
|
import { SkillExamDetails } from "@/exams/pdf/details/skill.exam";
|
|
|
|
import {SkillExamDetails} from "@/exams/pdf/details/skill.exam";
|
|
|
|
import { LevelExamDetails } from "@/exams/pdf/details/level.exam";
|
|
|
|
import {LevelExamDetails} from "@/exams/pdf/details/level.exam";
|
|
|
|
import { calculateBandScore, getLevelScore } from "@/utils/score";
|
|
|
|
import {calculateBandScore, getLevelScore} from "@/utils/score";
|
|
|
|
import {
|
|
|
|
import {generateQRCode, getRadialProgressPNG, streamToBuffer} from "@/utils/pdf";
|
|
|
|
generateQRCode,
|
|
|
|
import {Group} from "@/interfaces/user";
|
|
|
|
getRadialProgressPNG,
|
|
|
|
|
|
|
|
streamToBuffer,
|
|
|
|
|
|
|
|
} from "@/utils/pdf";
|
|
|
|
|
|
|
|
import moment from "moment-timezone";
|
|
|
|
import moment from "moment-timezone";
|
|
|
|
|
|
|
|
|
|
|
|
interface GroupScoreSummaryHelper {
|
|
|
|
interface GroupScoreSummaryHelper {
|
|
|
|
score: [number, number];
|
|
|
|
score: [number, number];
|
|
|
|
label: string;
|
|
|
|
label: string;
|
|
|
|
sessions: string[];
|
|
|
|
sessions: string[];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const db = getFirestore(app);
|
|
|
|
const db = getFirestore(app);
|
|
|
|
|
|
|
|
|
|
|
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
|
|
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
|
|
|
|
|
|
|
|
|
|
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
if (req.method === "GET") return get(req, res);
|
|
|
|
if (req.method === "GET") return get(req, res);
|
|
|
|
if (req.method === "POST") return post(req, res);
|
|
|
|
if (req.method === "POST") return post(req, res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const getExamSummary = (score: number) => {
|
|
|
|
const getExamSummary = (score: number) => {
|
|
|
|
if (score > 0.8) {
|
|
|
|
if (score > 0.8) {
|
|
|
|
return "Scoring between 81% and 100% on the English exam collectively demonstrates an outstanding level of proficiency in writing, speaking, listening, and reading for this group of students. Mastery of key concepts is evident across all language domains, showcasing not only a high level of skill but also a dedication to excellence. The group is encouraged to continue challenging themselves with advanced material in writing, speaking, listening, and reading to further refine their impressive command of the English language.";
|
|
|
|
return "Scoring between 81% and 100% on the English exam collectively demonstrates an outstanding level of proficiency in writing, speaking, listening, and reading for this group of students. Mastery of key concepts is evident across all language domains, showcasing not only a high level of skill but also a dedication to excellence. The group is encouraged to continue challenging themselves with advanced material in writing, speaking, listening, and reading to further refine their impressive command of the English language.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (score > 0.6) {
|
|
|
|
if (score > 0.6) {
|
|
|
|
return "The group's average scores between 61% and 80% on the English exam, encompassing writing, speaking, listening, and reading, reflect a commendable level of proficiency. There's evidence of a solid grasp of key concepts collectively, and effective application of skills. Room for refinement and deeper exploration in writing, speaking, listening, and reading remains, presenting an opportunity for the entire group to further their mastery.";
|
|
|
|
return "The group's average scores between 61% and 80% on the English exam, encompassing writing, speaking, listening, and reading, reflect a commendable level of proficiency. There's evidence of a solid grasp of key concepts collectively, and effective application of skills. Room for refinement and deeper exploration in writing, speaking, listening, and reading remains, presenting an opportunity for the entire group to further their mastery.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (score > 0.4) {
|
|
|
|
if (score > 0.4) {
|
|
|
|
return "Scoring between 41% and 60% on the English exam across writing, speaking, listening, and reading indicates a moderate level of understanding for the group. While there's a commendable grasp of key concepts collectively, refining fundamental skills in writing, speaking, listening, and reading can lead to notable improvement. The group is encouraged to work together with consistent effort and targeted focus on weaker areas.";
|
|
|
|
return "Scoring between 41% and 60% on the English exam across writing, speaking, listening, and reading indicates a moderate level of understanding for the group. While there's a commendable grasp of key concepts collectively, refining fundamental skills in writing, speaking, listening, and reading can lead to notable improvement. The group is encouraged to work together with consistent effort and targeted focus on weaker areas.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (score > 0.2) {
|
|
|
|
if (score > 0.2) {
|
|
|
|
return "The group's average scores between 21% and 40% on the English exam, encompassing writing, speaking, listening, and reading, show some understanding of key concepts in each domain. However, there's room for improvement in fundamental skills for the entire group. Strengthening writing, speaking, listening, and reading abilities collectively through consistent effort and focused group study will contribute to overall proficiency.";
|
|
|
|
return "The group's average scores between 21% and 40% on the English exam, encompassing writing, speaking, listening, and reading, show some understanding of key concepts in each domain. However, there's room for improvement in fundamental skills for the entire group. Strengthening writing, speaking, listening, and reading abilities collectively through consistent effort and focused group study will contribute to overall proficiency.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return "The average performance of this group of students in English, covering writing, speaking, listening, and reading, indicates a collective need for improvement, with scores falling between 0% and 20%. Across all language domains, there's a significant gap in understanding key concepts. Strengthening fundamental skills in writing, speaking, listening, and reading is crucial for the entire group. Implementing a shared, consistent study routine and seeking group support in each area can contribute to substantial progress.";
|
|
|
|
return "The average performance of this group of students in English, covering writing, speaking, listening, and reading, indicates a collective need for improvement, with scores falling between 0% and 20%. Across all language domains, there's a significant gap in understanding key concepts. Strengthening fundamental skills in writing, speaking, listening, and reading is crucial for the entire group. Implementing a shared, consistent study routine and seeking group support in each area can contribute to substantial progress.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const getLevelSummary = (score: number) => {
|
|
|
|
const getLevelSummary = (score: number) => {
|
|
|
|
if (score > 0.8) {
|
|
|
|
if (score > 0.8) {
|
|
|
|
return "Scoring between 81% and 100% on the English exam collectively demonstrates an outstanding level of proficiency for this group of students, showcasing a mastery of key concepts related to vocabulary and grammar. There's evidence of not only a high level of skill but also a dedication to excellence. The group is encouraged to continue challenging themselves with advanced material in vocabulary and grammar to further refine their impressive command of the English language.";
|
|
|
|
return "Scoring between 81% and 100% on the English exam collectively demonstrates an outstanding level of proficiency for this group of students, showcasing a mastery of key concepts related to vocabulary and grammar. There's evidence of not only a high level of skill but also a dedication to excellence. The group is encouraged to continue challenging themselves with advanced material in vocabulary and grammar to further refine their impressive command of the English language.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (score > 0.6) {
|
|
|
|
if (score > 0.6) {
|
|
|
|
return "The group's average scores between 61% and 80% on the English exam reflect a commendable level of proficiency with solid grasp of key concepts related to vocabulary and grammar. Room for refinement and deeper exploration in these language skills remains, presenting an opportunity for the entire group to further their mastery. Consistent effort in honing nuanced aspects of vocabulary and grammar will contribute to even greater proficiency.";
|
|
|
|
return "The group's average scores between 61% and 80% on the English exam reflect a commendable level of proficiency with solid grasp of key concepts related to vocabulary and grammar. Room for refinement and deeper exploration in these language skills remains, presenting an opportunity for the entire group to further their mastery. Consistent effort in honing nuanced aspects of vocabulary and grammar will contribute to even greater proficiency.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (score > 0.4) {
|
|
|
|
if (score > 0.4) {
|
|
|
|
return "Scoring between 41% and 60% on the English exam indicates a moderate level of understanding for the group, with commendable grasp of key concepts related to vocabulary and grammar. Refining these fundamental language skills can lead to notable improvement. The group is encouraged to work together with consistent effort and targeted focus on enhancing their vocabulary and grammar.";
|
|
|
|
return "Scoring between 41% and 60% on the English exam indicates a moderate level of understanding for the group, with commendable grasp of key concepts related to vocabulary and grammar. Refining these fundamental language skills can lead to notable improvement. The group is encouraged to work together with consistent effort and targeted focus on enhancing their vocabulary and grammar.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (score > 0.2) {
|
|
|
|
if (score > 0.2) {
|
|
|
|
return "The group's average scores between 21% and 40% on the English exam show some understanding of key concepts in vocabulary and grammar. However, there's room for improvement in these fundamental language skills for the entire group. Strengthening vocabulary and grammar collectively through consistent effort and focused group study will contribute to overall proficiency.";
|
|
|
|
return "The group's average scores between 21% and 40% on the English exam show some understanding of key concepts in vocabulary and grammar. However, there's room for improvement in these fundamental language skills for the entire group. Strengthening vocabulary and grammar collectively through consistent effort and focused group study will contribute to overall proficiency.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return "The average performance of this group of students in English suggests a collective need for improvement, with scores falling between 0% and 20%. There's a significant gap in understanding key concepts related to vocabulary and grammar. Strengthening fundamental language skills, such as vocabulary and grammar, is crucial for the entire group. Implementing a shared, consistent study routine and seeking group support in these areas can contribute to substantial progress.";
|
|
|
|
return "The average performance of this group of students in English suggests a collective need for improvement, with scores falling between 0% and 20%. There's a significant gap in understanding key concepts related to vocabulary and grammar. Strengthening fundamental language skills, such as vocabulary and grammar, is crucial for the entire group. Implementing a shared, consistent study routine and seeking group support in these areas can contribute to substantial progress.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const getPerformanceSummary = (module: Module, score: number) => {
|
|
|
|
const getPerformanceSummary = (module: Module, score: number) => {
|
|
|
|
if (module === "level") return getLevelSummary(score);
|
|
|
|
if (module === "level") return getLevelSummary(score);
|
|
|
|
return getExamSummary(score);
|
|
|
|
return getExamSummary(score);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const getScoreAndTotal = (stats: Stat[]) => {
|
|
|
|
const getScoreAndTotal = (stats: Stat[]) => {
|
|
|
|
return stats.reduce(
|
|
|
|
return stats.reduce(
|
|
|
|
(acc, { score }) => {
|
|
|
|
(acc, {score}) => {
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
...acc,
|
|
|
|
correct: acc.correct + score.correct,
|
|
|
|
correct: acc.correct + score.correct,
|
|
|
|
total: acc.total + score.total,
|
|
|
|
total: acc.total + score.total,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ correct: 0, total: 0 }
|
|
|
|
{correct: 0, total: 0},
|
|
|
|
);
|
|
|
|
);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const getLevelScoreForUserExams = (bandScore: number) => {
|
|
|
|
const getLevelScoreForUserExams = (bandScore: number) => {
|
|
|
|
const [level] = getLevelScore(bandScore);
|
|
|
|
const [level] = getLevelScore(bandScore);
|
|
|
|
return level;
|
|
|
|
return level;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
// verify if it's a logged user that is trying to export
|
|
|
|
// verify if it's a logged user that is trying to export
|
|
|
|
if (req.session.user) {
|
|
|
|
if (req.session.user) {
|
|
|
|
const { id } = req.query as { id: string };
|
|
|
|
const {id} = req.query as {id: string};
|
|
|
|
|
|
|
|
|
|
|
|
const docSnap = await getDoc(doc(db, "assignments", id));
|
|
|
|
const docSnap = await getDoc(doc(db, "assignments", id));
|
|
|
|
const data = docSnap.data() as {
|
|
|
|
const data = docSnap.data() as {
|
|
|
|
assigner: string;
|
|
|
|
assigner: string;
|
|
|
|
assignees: string[];
|
|
|
|
assignees: string[];
|
|
|
|
results: any;
|
|
|
|
results: any;
|
|
|
|
exams: { module: Module }[];
|
|
|
|
exams: {module: Module}[];
|
|
|
|
startDate: string;
|
|
|
|
startDate: string;
|
|
|
|
pdf?: string;
|
|
|
|
pdf?: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
if (!data) {
|
|
|
|
if (!data) {
|
|
|
|
res.status(400).end();
|
|
|
|
res.status(400).end();
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (data.assigner !== req.session.user.id) {
|
|
|
|
if (data.assigner !== req.session.user.id) {
|
|
|
|
res.status(401).json({ ok: false });
|
|
|
|
res.status(401).json({ok: false});
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (data.pdf) {
|
|
|
|
if (data.pdf) {
|
|
|
|
// if it does, return the pdf url
|
|
|
|
// if it does, return the pdf url
|
|
|
|
const fileRef = ref(storage, data.pdf);
|
|
|
|
const fileRef = ref(storage, data.pdf);
|
|
|
|
const url = await getDownloadURL(fileRef);
|
|
|
|
const url = await getDownloadURL(fileRef);
|
|
|
|
|
|
|
|
|
|
|
|
res.status(200).end(url);
|
|
|
|
res.status(200).end(url);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const docUser = await getDoc(doc(db, "users", req.session.user.id));
|
|
|
|
const docUser = await getDoc(doc(db, "users", req.session.user.id));
|
|
|
|
if (docUser.exists()) {
|
|
|
|
if (docUser.exists()) {
|
|
|
|
// we'll need the user in order to get the user data (name, email, focus, etc);
|
|
|
|
// we'll need the user in order to get the user data (name, email, focus, etc);
|
|
|
|
const user = docUser.data() as User;
|
|
|
|
const user = docUser.data() as User;
|
|
|
|
|
|
|
|
|
|
|
|
// generate the QR code for the report
|
|
|
|
// generate the QR code for the report
|
|
|
|
const qrcode = await generateQRCode(
|
|
|
|
const qrcode = await generateQRCode((req.headers.origin || "") + req.url);
|
|
|
|
(req.headers.origin || "") + req.url
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!qrcode) {
|
|
|
|
if (!qrcode) {
|
|
|
|
res.status(500).json({ ok: false });
|
|
|
|
res.status(500).json({ok: false});
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const flattenResults = data.results.reduce(
|
|
|
|
const flattenResults = data.results.reduce((accm: Stat[], entry: any) => {
|
|
|
|
(accm: Stat[], entry: any) => {
|
|
|
|
const stats = entry.stats as Stat[];
|
|
|
|
const stats = entry.stats as Stat[];
|
|
|
|
return [...accm, ...stats];
|
|
|
|
return [...accm, ...stats];
|
|
|
|
}, []) as Stat[];
|
|
|
|
},
|
|
|
|
|
|
|
|
[]
|
|
|
|
|
|
|
|
) as Stat[];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const docsSnap = await getDocs(
|
|
|
|
const docsSnap = await getDocs(query(collection(db, "users"), where(documentId(), "in", data.assignees)));
|
|
|
|
query(
|
|
|
|
const users = docsSnap.docs.map((d) => ({
|
|
|
|
collection(db, "users"),
|
|
|
|
...d.data(),
|
|
|
|
where(documentId(), "in", data.assignees)
|
|
|
|
id: d.id,
|
|
|
|
)
|
|
|
|
})) as User[];
|
|
|
|
);
|
|
|
|
|
|
|
|
const users = docsSnap.docs.map((d) => ({
|
|
|
|
|
|
|
|
...d.data(),
|
|
|
|
|
|
|
|
id: d.id,
|
|
|
|
|
|
|
|
})) as User[];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const flattenResultsWithGrade = flattenResults.map((e) => {
|
|
|
|
const flattenResultsWithGrade = flattenResults.map((e) => {
|
|
|
|
const focus = users.find((u) => u.id === e.user)?.focus || "academic";
|
|
|
|
const focus = users.find((u) => u.id === e.user)?.focus || "academic";
|
|
|
|
const bandScore = calculateBandScore(
|
|
|
|
const bandScore = calculateBandScore(e.score.correct, e.score.total, e.module, focus);
|
|
|
|
e.score.correct,
|
|
|
|
|
|
|
|
e.score.total,
|
|
|
|
|
|
|
|
e.module,
|
|
|
|
|
|
|
|
focus
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { ...e, bandScore };
|
|
|
|
return {...e, bandScore};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const moduleResults = data.exams.map(({ module }) => {
|
|
|
|
const moduleResults = data.exams.map(({module}) => {
|
|
|
|
const moduleResults = flattenResultsWithGrade.filter(
|
|
|
|
const moduleResults = flattenResultsWithGrade.filter((e) => e.module === module);
|
|
|
|
(e) => e.module === module
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const baseBandScore =
|
|
|
|
const baseBandScore = moduleResults.reduce((accm, curr) => accm + curr.bandScore, 0) / moduleResults.length;
|
|
|
|
moduleResults.reduce((accm, curr) => accm + curr.bandScore, 0) /
|
|
|
|
const bandScore = isNaN(baseBandScore) ? 0 : baseBandScore;
|
|
|
|
moduleResults.length;
|
|
|
|
const {correct, total} = getScoreAndTotal(moduleResults);
|
|
|
|
const bandScore = isNaN(baseBandScore) ? 0 : baseBandScore;
|
|
|
|
const png = getRadialProgressPNG("azul", correct, total);
|
|
|
|
const { correct, total } = getScoreAndTotal(moduleResults);
|
|
|
|
|
|
|
|
const png = getRadialProgressPNG("azul", correct, total);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
bandScore,
|
|
|
|
bandScore,
|
|
|
|
png,
|
|
|
|
png,
|
|
|
|
module: module[0].toUpperCase() + module.substring(1),
|
|
|
|
module: module[0].toUpperCase() + module.substring(1),
|
|
|
|
score: bandScore,
|
|
|
|
score: bandScore,
|
|
|
|
total,
|
|
|
|
total,
|
|
|
|
code: module,
|
|
|
|
code: module,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}) as ModuleScore[];
|
|
|
|
}) as ModuleScore[];
|
|
|
|
|
|
|
|
|
|
|
|
const { correct: overallCorrect, total: overallTotal } =
|
|
|
|
const {correct: overallCorrect, total: overallTotal} = getScoreAndTotal(flattenResults);
|
|
|
|
getScoreAndTotal(flattenResults);
|
|
|
|
const baseOverallResult = overallCorrect / overallTotal;
|
|
|
|
const baseOverallResult = overallCorrect / overallTotal;
|
|
|
|
const overallResult = isNaN(baseOverallResult) ? 0 : baseOverallResult;
|
|
|
|
const overallResult = isNaN(baseOverallResult) ? 0 : baseOverallResult;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const overallPNG = getRadialProgressPNG(
|
|
|
|
const overallPNG = getRadialProgressPNG("laranja", overallCorrect, overallTotal);
|
|
|
|
"laranja",
|
|
|
|
// generate the overall detail report
|
|
|
|
overallCorrect,
|
|
|
|
const overallDetail = {
|
|
|
|
overallTotal
|
|
|
|
module: "Overall",
|
|
|
|
);
|
|
|
|
score: overallCorrect,
|
|
|
|
// generate the overall detail report
|
|
|
|
total: overallTotal,
|
|
|
|
const overallDetail = {
|
|
|
|
png: overallPNG,
|
|
|
|
module: "Overall",
|
|
|
|
} as ModuleScore;
|
|
|
|
score: overallCorrect,
|
|
|
|
|
|
|
|
total: overallTotal,
|
|
|
|
|
|
|
|
png: overallPNG,
|
|
|
|
|
|
|
|
} as ModuleScore;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const testDetails = [overallDetail, ...moduleResults];
|
|
|
|
const testDetails = [overallDetail, ...moduleResults];
|
|
|
|
// generate the performance summary based on the overall result
|
|
|
|
// generate the performance summary based on the overall result
|
|
|
|
const baseStat = data.exams[0];
|
|
|
|
const baseStat = data.exams[0];
|
|
|
|
const performanceSummary = getPerformanceSummary(
|
|
|
|
const performanceSummary = getPerformanceSummary(
|
|
|
|
// from what I noticed, exams is either an array with the level module
|
|
|
|
// from what I noticed, exams is either an array with the level module
|
|
|
|
// or X modules, either way
|
|
|
|
// or X modules, either way
|
|
|
|
// as long as I verify the first entry I should be fine
|
|
|
|
// as long as I verify the first entry I should be fine
|
|
|
|
baseStat.module,
|
|
|
|
baseStat.module,
|
|
|
|
overallResult
|
|
|
|
overallResult,
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const showLevel = baseStat.module === "level";
|
|
|
|
const showLevel = baseStat.module === "level";
|
|
|
|
|
|
|
|
|
|
|
|
// level exams have a different report structure than the skill exams
|
|
|
|
// level exams have a different report structure than the skill exams
|
|
|
|
const getCustomData = () => {
|
|
|
|
const getCustomData = () => {
|
|
|
|
if (showLevel) {
|
|
|
|
if (showLevel) {
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
title: "GROUP ENGLISH LEVEL TEST RESULT REPORT ",
|
|
|
|
title: "GROUP ENGLISH LEVEL TEST RESULT REPORT ",
|
|
|
|
details: (
|
|
|
|
details: <LevelExamDetails detail={overallDetail} title="Group Average CEFR" />,
|
|
|
|
<LevelExamDetails
|
|
|
|
};
|
|
|
|
detail={overallDetail}
|
|
|
|
}
|
|
|
|
title="Group Average CEFR"
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
title: "GROUP ENGLISH SKILLS TEST RESULT REPORT",
|
|
|
|
title: "GROUP ENGLISH SKILLS TEST RESULT REPORT",
|
|
|
|
details: <SkillExamDetails testDetails={testDetails} />,
|
|
|
|
details: <SkillExamDetails testDetails={testDetails} />,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const { title, details } = getCustomData();
|
|
|
|
const {title, details} = getCustomData();
|
|
|
|
|
|
|
|
|
|
|
|
const numberOfStudents = data.assignees.length;
|
|
|
|
const numberOfStudents = data.assignees.length;
|
|
|
|
|
|
|
|
|
|
|
|
const getStudentsData = async (): Promise<StudentData[]> => {
|
|
|
|
const getStudentsData = async (): Promise<StudentData[]> => {
|
|
|
|
return data.assignees.map((id) => {
|
|
|
|
return data.assignees.map((id) => {
|
|
|
|
const user = users.find((u) => u.id === id);
|
|
|
|
const user = users.find((u) => u.id === id);
|
|
|
|
const exams = flattenResultsWithGrade.filter((e) => e.user === id);
|
|
|
|
const exams = flattenResultsWithGrade.filter((e) => e.user === id);
|
|
|
|
const date =
|
|
|
|
const date =
|
|
|
|
exams.length === 0
|
|
|
|
exams.length === 0
|
|
|
|
? "N/A"
|
|
|
|
? "N/A"
|
|
|
|
: new Date(exams[0].date).toLocaleDateString(undefined, {
|
|
|
|
: new Date(exams[0].date).toLocaleDateString(undefined, {
|
|
|
|
year: "numeric",
|
|
|
|
year: "numeric",
|
|
|
|
month: "numeric",
|
|
|
|
month: "numeric",
|
|
|
|
day: "numeric",
|
|
|
|
day: "numeric",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const bandScore =
|
|
|
|
const bandScore = exams.length === 0 ? 0 : exams.reduce((accm, curr) => accm + curr.bandScore, 0) / exams.length;
|
|
|
|
exams.length === 0
|
|
|
|
const {correct, total} = getScoreAndTotal(exams);
|
|
|
|
? 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}`;
|
|
|
|
const result = exams.length === 0 ? "N/A" : `${correct}/${total}`;
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
id,
|
|
|
|
name: user?.name || "N/A",
|
|
|
|
name: user?.name || "N/A",
|
|
|
|
email: user?.email || "N/A",
|
|
|
|
email: user?.email || "N/A",
|
|
|
|
gender: user?.demographicInformation?.gender || "N/A",
|
|
|
|
gender: user?.demographicInformation?.gender || "N/A",
|
|
|
|
date,
|
|
|
|
date,
|
|
|
|
result,
|
|
|
|
result,
|
|
|
|
level: showLevel
|
|
|
|
level: showLevel ? getLevelScoreForUserExams(bandScore) : undefined,
|
|
|
|
? getLevelScoreForUserExams(bandScore)
|
|
|
|
bandScore,
|
|
|
|
: undefined,
|
|
|
|
};
|
|
|
|
bandScore,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const studentsData = await getStudentsData();
|
|
|
|
const studentsData = await getStudentsData();
|
|
|
|
|
|
|
|
|
|
|
|
const getGroupScoreSummary = () => {
|
|
|
|
const getGroupScoreSummary = () => {
|
|
|
|
const resultHelper = studentsData.reduce(
|
|
|
|
const resultHelper = studentsData.reduce((accm: GroupScoreSummaryHelper[], curr) => {
|
|
|
|
(accm: GroupScoreSummaryHelper[], curr) => {
|
|
|
|
const {bandScore, id} = curr;
|
|
|
|
const { bandScore, id } = curr;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const flooredScore = Math.floor(bandScore);
|
|
|
|
const flooredScore = Math.floor(bandScore);
|
|
|
|
|
|
|
|
|
|
|
|
const hasMatch = accm.find((a) => a.score.includes(flooredScore));
|
|
|
|
const hasMatch = accm.find((a) => a.score.includes(flooredScore));
|
|
|
|
if (hasMatch) {
|
|
|
|
if (hasMatch) {
|
|
|
|
return accm.map((a) => {
|
|
|
|
return accm.map((a) => {
|
|
|
|
if (a.score.includes(flooredScore)) {
|
|
|
|
if (a.score.includes(flooredScore)) {
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
...a,
|
|
|
|
...a,
|
|
|
|
sessions: [...a.sessions, id],
|
|
|
|
sessions: [...a.sessions, id],
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return a;
|
|
|
|
return a;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
return [
|
|
|
|
...accm,
|
|
|
|
...accm,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
score: [flooredScore, flooredScore + 0.5],
|
|
|
|
score: [flooredScore, flooredScore + 0.5],
|
|
|
|
label: `${flooredScore} - ${flooredScore + 0.5}`,
|
|
|
|
label: `${flooredScore} - ${flooredScore + 0.5}`,
|
|
|
|
sessions: [id],
|
|
|
|
sessions: [id],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
];
|
|
|
|
},
|
|
|
|
}, []) as GroupScoreSummaryHelper[];
|
|
|
|
[]
|
|
|
|
|
|
|
|
) as GroupScoreSummaryHelper[];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const result = resultHelper.map(({ score, label, sessions }) => {
|
|
|
|
const result = resultHelper.map(({score, label, sessions}) => {
|
|
|
|
const finalLabel = showLevel ? getLevelScore(score[0])[1] : label;
|
|
|
|
const finalLabel = showLevel ? getLevelScore(score[0])[1] : label;
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
label: finalLabel,
|
|
|
|
label: finalLabel,
|
|
|
|
percent: Math.floor((sessions.length / numberOfStudents) * 100),
|
|
|
|
percent: Math.floor((sessions.length / numberOfStudents) * 100),
|
|
|
|
description: `No. Candidates ${sessions.length} of ${numberOfStudents}`,
|
|
|
|
description: `No. Candidates ${sessions.length} of ${numberOfStudents}`,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const groupScoreSummary = getGroupScoreSummary();
|
|
|
|
const getInstitution = async () => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// due to database inconsistencies, I'll be overprotective here
|
|
|
|
|
|
|
|
const assignerUserSnap = await getDoc(doc(db, "users", data.assigner));
|
|
|
|
|
|
|
|
if (assignerUserSnap.exists()) {
|
|
|
|
|
|
|
|
// we'll need the user in order to get the user data (name, email, focus, etc);
|
|
|
|
|
|
|
|
const assignerUser = assignerUserSnap.data() as User;
|
|
|
|
|
|
|
|
|
|
|
|
const pdfStream = await ReactPDF.renderToStream(
|
|
|
|
if (assignerUser.type === "teacher") {
|
|
|
|
<GroupTestReport
|
|
|
|
// also search for groups where this user belongs
|
|
|
|
title={title}
|
|
|
|
const queryGroups = query(collection(db, "groups"), where("participants", "array-contains", assignerUser.id));
|
|
|
|
date={moment(data.startDate).tz(user.demographicInformation?.timezone || 'UTC').format('ll HH:mm:ss')}
|
|
|
|
const groupSnapshot = await getDocs(queryGroups);
|
|
|
|
name={user.name}
|
|
|
|
|
|
|
|
email={user.email}
|
|
|
|
|
|
|
|
id={user.id}
|
|
|
|
|
|
|
|
gender={user.demographicInformation?.gender}
|
|
|
|
|
|
|
|
summary={performanceSummary}
|
|
|
|
|
|
|
|
renderDetails={details}
|
|
|
|
|
|
|
|
logo={"public/logo_title.png"}
|
|
|
|
|
|
|
|
qrcode={qrcode}
|
|
|
|
|
|
|
|
numberOfStudents={numberOfStudents}
|
|
|
|
|
|
|
|
institution="TODO: PLACEHOLDER"
|
|
|
|
|
|
|
|
studentsData={studentsData}
|
|
|
|
|
|
|
|
showLevel={showLevel}
|
|
|
|
|
|
|
|
summaryPNG={overallPNG}
|
|
|
|
|
|
|
|
summaryScore={`${(overallResult * 100).toFixed(0)}%`}
|
|
|
|
|
|
|
|
groupScoreSummary={groupScoreSummary}
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// generate the file ref for storage
|
|
|
|
const groups = groupSnapshot.docs.map((doc) => ({
|
|
|
|
const fileName = `${Date.now().toString()}.pdf`;
|
|
|
|
id: doc.id,
|
|
|
|
const refName = `assignment_report/${fileName}`;
|
|
|
|
...doc.data(),
|
|
|
|
const fileRef = ref(storage, refName);
|
|
|
|
})) as Group[];
|
|
|
|
|
|
|
|
|
|
|
|
// upload the pdf to storage
|
|
|
|
if (groups.length > 0) {
|
|
|
|
const pdfBuffer = await streamToBuffer(pdfStream);
|
|
|
|
const adminQuery = query(
|
|
|
|
const snapshot = await uploadBytes(fileRef, pdfBuffer, {
|
|
|
|
collection(db, "users"),
|
|
|
|
contentType: "application/pdf",
|
|
|
|
where(
|
|
|
|
});
|
|
|
|
documentId(),
|
|
|
|
|
|
|
|
"in",
|
|
|
|
|
|
|
|
groups.map((g) => g.admin),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
const adminUsersSnap = await getDocs(adminQuery);
|
|
|
|
|
|
|
|
|
|
|
|
// update the stats entries with the pdf url to prevent duplication
|
|
|
|
const admins = adminUsersSnap.docs.map((doc) => ({
|
|
|
|
await updateDoc(docSnap.ref, {
|
|
|
|
id: doc.id,
|
|
|
|
pdf: refName,
|
|
|
|
...doc.data(),
|
|
|
|
});
|
|
|
|
})) as CorporateUser[];
|
|
|
|
const url = await getDownloadURL(fileRef);
|
|
|
|
|
|
|
|
res.status(200).end(url);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.status(401).json({ ok: false });
|
|
|
|
const adminData = admins.find((a) => a.corporateInformation?.companyInformation?.name);
|
|
|
|
return;
|
|
|
|
if (adminData) {
|
|
|
|
} catch (err) {
|
|
|
|
return adminData.corporateInformation.companyInformation.name;
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
res.status(500).json({ ok: false });
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (assignerUser.type === "corporate" && assignerUser.corporateInformation?.companyInformation?.name) {
|
|
|
|
|
|
|
|
return assignerUser.corporateInformation.companyInformation.name;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const institution = await getInstitution();
|
|
|
|
|
|
|
|
const groupScoreSummary = getGroupScoreSummary();
|
|
|
|
|
|
|
|
const demographicInformation = user.demographicInformation as DemographicInformation;
|
|
|
|
|
|
|
|
const pdfStream = await ReactPDF.renderToStream(
|
|
|
|
|
|
|
|
<GroupTestReport
|
|
|
|
|
|
|
|
title={title}
|
|
|
|
|
|
|
|
date={moment(data.startDate)
|
|
|
|
|
|
|
|
.tz(user.demographicInformation?.timezone || "UTC")
|
|
|
|
|
|
|
|
.format("ll HH:mm:ss")}
|
|
|
|
|
|
|
|
name={user.name}
|
|
|
|
|
|
|
|
email={user.email}
|
|
|
|
|
|
|
|
id={user.id}
|
|
|
|
|
|
|
|
gender={demographicInformation?.gender}
|
|
|
|
|
|
|
|
summary={performanceSummary}
|
|
|
|
|
|
|
|
renderDetails={details}
|
|
|
|
|
|
|
|
logo={"public/logo_title.png"}
|
|
|
|
|
|
|
|
qrcode={qrcode}
|
|
|
|
|
|
|
|
numberOfStudents={numberOfStudents}
|
|
|
|
|
|
|
|
institution={institution}
|
|
|
|
|
|
|
|
studentsData={studentsData}
|
|
|
|
|
|
|
|
showLevel={showLevel}
|
|
|
|
|
|
|
|
summaryPNG={overallPNG}
|
|
|
|
|
|
|
|
summaryScore={`${(overallResult * 100).toFixed(0)}%`}
|
|
|
|
|
|
|
|
groupScoreSummary={groupScoreSummary}
|
|
|
|
|
|
|
|
passportId={demographicInformation?.passport_id || ""}
|
|
|
|
|
|
|
|
/>,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// generate the file ref for storage
|
|
|
|
|
|
|
|
const fileName = `${Date.now().toString()}.pdf`;
|
|
|
|
|
|
|
|
const refName = `assignment_report/${fileName}`;
|
|
|
|
|
|
|
|
const fileRef = ref(storage, refName);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// upload the pdf to storage
|
|
|
|
|
|
|
|
const pdfBuffer = await streamToBuffer(pdfStream);
|
|
|
|
|
|
|
|
const snapshot = await uploadBytes(fileRef, pdfBuffer, {
|
|
|
|
|
|
|
|
contentType: "application/pdf",
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// update the stats entries with the pdf url to prevent duplication
|
|
|
|
|
|
|
|
await updateDoc(docSnap.ref, {
|
|
|
|
|
|
|
|
pdf: refName,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = await getDownloadURL(fileRef);
|
|
|
|
|
|
|
|
res.status(200).end(url);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.status(401).json({ok: false});
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
res.status(500).json({ok: false});
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async function get(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
async function get(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
if (req.session.user) {
|
|
|
|
if (req.session.user) {
|
|
|
|
const { id } = req.query as { id: string };
|
|
|
|
const {id} = req.query as {id: string};
|
|
|
|
|
|
|
|
|
|
|
|
const docSnap = await getDoc(doc(db, "assignments", id));
|
|
|
|
const docSnap = await getDoc(doc(db, "assignments", id));
|
|
|
|
const data = docSnap.data();
|
|
|
|
const data = docSnap.data();
|
|
|
|
if (!data) {
|
|
|
|
if (!data) {
|
|
|
|
res.status(400).end();
|
|
|
|
res.status(400).end();
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (data.assigner !== req.session.user.id) {
|
|
|
|
if (data.assigner !== req.session.user.id) {
|
|
|
|
res.status(401).json({ ok: false });
|
|
|
|
res.status(401).json({ok: false});
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (data.pdf) {
|
|
|
|
if (data.pdf) {
|
|
|
|
const fileRef = ref(storage, data.pdf);
|
|
|
|
const fileRef = ref(storage, data.pdf);
|
|
|
|
const url = await getDownloadURL(fileRef);
|
|
|
|
const url = await getDownloadURL(fileRef);
|
|
|
|
return res.redirect(url);
|
|
|
|
return res.redirect(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res.status(404).end();
|
|
|
|
res.status(404).end();
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res.status(401).json({ ok: false });
|
|
|
|
res.status(401).json({ok: false});
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|