ENCOA-315
This commit is contained in:
@@ -35,7 +35,7 @@ export default function ExamPage({ page, user, destination = "/", hideSidebar =
|
||||
const [variant, setVariant] = useState<Variant>("full");
|
||||
const [avoidRepeated, setAvoidRepeated] = useState(false);
|
||||
const [showAbandonPopup, setShowAbandonPopup] = useState(false);
|
||||
const [pendingExercises, setPendingExercises] = useState<string[]>([]);
|
||||
const [moduleLock, setModuleLock] = useState(false);
|
||||
|
||||
const {
|
||||
exam, setExam,
|
||||
@@ -58,7 +58,6 @@ export default function ExamPage({ page, user, destination = "/", hideSidebar =
|
||||
saveSession,
|
||||
setFlags,
|
||||
setShuffles,
|
||||
evaluated,
|
||||
} = useExamStore();
|
||||
|
||||
const [isFetchingExams, setIsFetchingExams] = useState(false);
|
||||
@@ -114,68 +113,77 @@ export default function ExamPage({ page, user, destination = "/", hideSidebar =
|
||||
setShowAbandonPopup(false);
|
||||
};
|
||||
|
||||
useEvaluationPolling(sessionId ? [sessionId] : [], "exam", user?.id);
|
||||
|
||||
useEffect(() => {
|
||||
if (flags.finalizeModule && !showSolutions && flags.pendingEvaluation) {
|
||||
if (exam && (exam.module === "writing" || exam.module === "speaking") && userSolutions.length > 0 && !showSolutions) {
|
||||
const exercisesToEvaluate = exam.exercises
|
||||
.map(exercise => exercise.id);
|
||||
setModuleLock(true);
|
||||
}, [flags.finalizeModule])
|
||||
|
||||
setPendingExercises(exercisesToEvaluate);
|
||||
useEffect(() => {
|
||||
if (flags.finalizeModule && !showSolutions) {
|
||||
if (exam && (exam.module === "writing" || exam.module === "speaking") && userSolutions.length > 0) {
|
||||
(async () => {
|
||||
await Promise.all(
|
||||
exam.exercises.map(async (exercise, index) => {
|
||||
if (exercise.type === "writing")
|
||||
await evaluateWritingAnswer(user.id, sessionId, exercise, index + 1, userSolutions.find((x) => x.exercise === exercise.id)!, exercise.attachment?.url);
|
||||
|
||||
if (exercise.type === "interactiveSpeaking" || exercise.type === "speaking") {
|
||||
await evaluateSpeakingAnswer(
|
||||
user.id,
|
||||
sessionId,
|
||||
exercise,
|
||||
userSolutions.find((x) => x.exercise === exercise.id)!,
|
||||
index + 1,
|
||||
);
|
||||
}
|
||||
}),
|
||||
)
|
||||
try {
|
||||
const results = await Promise.all(
|
||||
exam.exercises.map(async (exercise, index) => {
|
||||
if (exercise.type === "writing") {
|
||||
const sol = await evaluateWritingAnswer(
|
||||
user.id, sessionId, exercise, index + 1,
|
||||
userSolutions.find((x) => x.exercise === exercise.id)!,
|
||||
exercise.attachment?.url
|
||||
);
|
||||
return sol;
|
||||
}
|
||||
if (exercise.type === "interactiveSpeaking" || exercise.type === "speaking") {
|
||||
const sol = await evaluateSpeakingAnswer(
|
||||
user.id,
|
||||
sessionId,
|
||||
exercise,
|
||||
userSolutions.find((x) => x.exercise === exercise.id)!,
|
||||
index + 1,
|
||||
);
|
||||
return sol;
|
||||
}
|
||||
return null;
|
||||
})
|
||||
);
|
||||
const updatedSolutions = userSolutions.map(solution => {
|
||||
const completed = results.filter(r => r !== null).find(
|
||||
(c: any) => c.exercise === solution.exercise
|
||||
);
|
||||
return completed || solution;
|
||||
});
|
||||
setUserSolutions(updatedSolutions);
|
||||
} catch (error) {
|
||||
console.error('Error during module evaluation:', error);
|
||||
} finally {
|
||||
setModuleLock(false);
|
||||
}
|
||||
})();
|
||||
} else {
|
||||
setModuleLock(false);
|
||||
}
|
||||
}
|
||||
}, [exam, showSolutions, userSolutions, sessionId, user?.id, flags]);
|
||||
|
||||
useEvaluationPolling({ pendingExercises, setPendingExercises });
|
||||
}, [exam, showSolutions, userSolutions, sessionId, user.id, flags.finalizeModule, setUserSolutions]);
|
||||
|
||||
useEffect(() => {
|
||||
if (flags.finalizeExam && moduleIndex !== -1) {
|
||||
setModuleIndex(-1);
|
||||
|
||||
|
||||
}
|
||||
}, [flags.finalizeExam, moduleIndex, setModuleIndex]);
|
||||
|
||||
useEffect(() => {
|
||||
if (flags.finalizeExam && !flags.pendingEvaluation && pendingExercises.length === 0) {
|
||||
if (flags.finalizeExam && moduleIndex !== -1 && !moduleLock) {
|
||||
(async () => {
|
||||
if (evaluated.length !== 0) {
|
||||
setUserSolutions(
|
||||
userSolutions.map(solution => {
|
||||
const evaluatedSolution = evaluated.find(e => e.exercise === solution.exercise);
|
||||
if (evaluatedSolution) {
|
||||
return { ...solution, ...evaluatedSolution };
|
||||
}
|
||||
return solution;
|
||||
})
|
||||
);
|
||||
}
|
||||
setModuleIndex(-1);
|
||||
await saveStats();
|
||||
await axios.get("/api/stats/update");
|
||||
setShowSolutions(true);
|
||||
setFlags({ finalizeExam: false });
|
||||
dispatch({ type: "UPDATE_EXAMS" })
|
||||
})();
|
||||
})()
|
||||
}
|
||||
}, [flags.finalizeExam, moduleIndex, saveStats, setModuleIndex, userSolutions, moduleLock, flags.finalizeModule]);
|
||||
|
||||
useEffect(() => {
|
||||
if (flags.finalizeExam && !userSolutions.some(s => s.isDisabled) && !moduleLock) {
|
||||
setShowSolutions(true);
|
||||
setFlags({ finalizeExam: false });
|
||||
dispatch({ type: "UPDATE_EXAMS" });
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [saveStats, setFlags, setModuleIndex, evaluated, pendingExercises, setUserSolutions, flags]);
|
||||
}, [flags.finalizeExam]);
|
||||
|
||||
|
||||
const aggregateScoresByModule = (isPractice?: boolean): {
|
||||
@@ -276,7 +284,7 @@ export default function ExamPage({ page, user, destination = "/", hideSidebar =
|
||||
)}
|
||||
{(moduleIndex === -1 && selectedModules.length !== 0) &&
|
||||
<Finish
|
||||
isLoading={flags.pendingEvaluation}
|
||||
isLoading={userSolutions.some(s => s.isDisabled)}
|
||||
user={user!}
|
||||
modules={selectedModules}
|
||||
solutions={userSolutions}
|
||||
|
||||
@@ -4,21 +4,71 @@ import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { UserSolution } from "@/interfaces/exam";
|
||||
import { speakingReverseMarking, writingReverseMarking } from "@/utils/score";
|
||||
import { Stat } from "@/interfaces/user";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "POST") return post(req, res);
|
||||
}
|
||||
|
||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
const { sessionId, userId, userSolutions } = req.body;
|
||||
try {
|
||||
return await getSessionEvals(req, res);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ ok: false });
|
||||
}
|
||||
}
|
||||
|
||||
function formatSolutionWithEval(userSolution: UserSolution | Stat, evaluation: any) {
|
||||
if (userSolution.type === 'writing') {
|
||||
return {
|
||||
...userSolution,
|
||||
solutions: [{
|
||||
...userSolution.solutions[0],
|
||||
evaluation: evaluation.result
|
||||
}],
|
||||
score: {
|
||||
correct: writingReverseMarking[evaluation.result.overall],
|
||||
total: 100,
|
||||
missing: 0
|
||||
},
|
||||
isDisabled: false
|
||||
};
|
||||
}
|
||||
|
||||
if (userSolution.type === 'speaking' || userSolution.type === 'interactiveSpeaking') {
|
||||
return {
|
||||
...userSolution,
|
||||
solutions: [{
|
||||
...userSolution.solutions[0],
|
||||
...(
|
||||
userSolution.type === 'speaking'
|
||||
? { fullPath: evaluation.result.fullPath }
|
||||
: { answer: evaluation.result.answer }
|
||||
),
|
||||
evaluation: evaluation.result
|
||||
}],
|
||||
score: {
|
||||
correct: speakingReverseMarking[evaluation.result.overall || 0] || 0,
|
||||
total: 100,
|
||||
missing: 0
|
||||
},
|
||||
isDisabled: false
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
solution: userSolution,
|
||||
evaluation
|
||||
};
|
||||
}
|
||||
|
||||
async function getSessionEvals(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { sessionId, userId, stats } = req.body;
|
||||
const completedEvals = await db.collection("evaluation").find({
|
||||
session_id: sessionId,
|
||||
user: userId,
|
||||
@@ -29,52 +79,11 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
completedEvals.map(e => [e.exercise_id, e])
|
||||
);
|
||||
|
||||
const solutionsWithEvals = userSolutions.filter((solution: UserSolution) =>
|
||||
evalsByExercise.has(solution.exercise)
|
||||
).map((solution: any) => {
|
||||
const evaluation = evalsByExercise.get(solution.exercise)!;
|
||||
const statsWithEvals = stats
|
||||
.filter((solution: UserSolution | Stat) => evalsByExercise.has(solution.exercise))
|
||||
.map((solution: UserSolution | Stat) =>
|
||||
formatSolutionWithEval(solution, evalsByExercise.get(solution.exercise)!)
|
||||
);
|
||||
|
||||
if (solution.type === 'writing') {
|
||||
return {
|
||||
...solution,
|
||||
solutions: [{
|
||||
...solution.solutions[0],
|
||||
evaluation: evaluation.result
|
||||
}],
|
||||
score: {
|
||||
correct: writingReverseMarking[evaluation.result.overall],
|
||||
total: 100,
|
||||
missing: 0
|
||||
},
|
||||
isDisabled: false
|
||||
};
|
||||
}
|
||||
|
||||
if (solution.type === 'speaking' || solution.type === 'interactiveSpeaking') {
|
||||
return {
|
||||
...solution,
|
||||
solutions: [{
|
||||
...solution.solutions[0],
|
||||
...(
|
||||
solution.type === 'speaking'
|
||||
? { fullPath: evaluation.result.fullPath }
|
||||
: { answer: evaluation.result.answer }
|
||||
),
|
||||
evaluation: evaluation.result
|
||||
}],
|
||||
score: {
|
||||
correct: speakingReverseMarking[evaluation.result.overall || 0] || 0,
|
||||
total: 100,
|
||||
missing: 0
|
||||
},
|
||||
isDisabled: false
|
||||
};
|
||||
}
|
||||
return {
|
||||
solution,
|
||||
evaluation
|
||||
};
|
||||
});
|
||||
|
||||
res.status(200).json(solutionsWithEvals)
|
||||
res.status(200).json(statsWithEvals);
|
||||
}
|
||||
|
||||
@@ -11,19 +11,100 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "GET") return get(req, res);
|
||||
}
|
||||
|
||||
type Query = {
|
||||
op: string;
|
||||
sessionId: string;
|
||||
userId: string;
|
||||
}
|
||||
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
return res.status(401).json({ ok: false });
|
||||
}
|
||||
|
||||
const { sessionId, userId } = req.query;
|
||||
const { sessionId, userId, op } = req.query as Query;
|
||||
|
||||
switch (op) {
|
||||
case 'pending':
|
||||
return getPendingEvaluation(userId, sessionId, res);
|
||||
case 'disabled':
|
||||
return getSessionsWIthDisabledWithPending(userId, res);
|
||||
default:
|
||||
return res.status(400).json({
|
||||
ok: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function getPendingEvaluation(
|
||||
userId: string,
|
||||
sessionId: string,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
const singleEval = await db.collection("evaluation").findOne({
|
||||
session_id: sessionId,
|
||||
user: userId,
|
||||
status: "pending",
|
||||
});
|
||||
|
||||
res.status(200).json({ hasPendingEvaluation: singleEval !== null});
|
||||
return res.status(200).json({ hasPendingEvaluation: singleEval !== null });
|
||||
}
|
||||
|
||||
async function getSessionsWIthDisabledWithPending(
|
||||
userId: string,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
const sessions = await db.collection("stats")
|
||||
.aggregate([
|
||||
{
|
||||
$match: {
|
||||
user: userId,
|
||||
disabled: true
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 0,
|
||||
session: 1
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "evaluation",
|
||||
let: { sessionId: "$session" },
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$and: [
|
||||
{ $eq: ["$session", "$$sessionId"] },
|
||||
{ $eq: ["$user", userId] },
|
||||
{ $eq: ["$status", "pending"] }
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 1
|
||||
}
|
||||
}
|
||||
],
|
||||
as: "pendingEvals"
|
||||
}
|
||||
},
|
||||
{
|
||||
$match: {
|
||||
"pendingEvals.0": { $exists: true }
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
id: "$session"
|
||||
}
|
||||
}
|
||||
]).toArray();
|
||||
|
||||
return res.status(200).json({
|
||||
sessions: sessions.map(s => s.id)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,37 +3,41 @@ import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import client from "@/lib/mongodb";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { Stat } from "@/interfaces/user";
|
||||
import { requestUser } from "@/utils/api";
|
||||
import { UserSolution } from "@/interfaces/exam";
|
||||
import { WithId } from "mongodb";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "POST") return post(req, res);
|
||||
}
|
||||
|
||||
interface Body {
|
||||
solutions: UserSolution[];
|
||||
sessionID: string;
|
||||
}
|
||||
|
||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
const user = await requestUser(req, res)
|
||||
if (!user) return res.status(401).json({ ok: false });
|
||||
|
||||
const { solutions, sessionID } = req.body as Body;
|
||||
if (req.method === "POST") return post(req, res);
|
||||
}
|
||||
|
||||
const disabledStats = await db.collection("stats").find({ user: user.id, session: sessionID, disabled: true }).toArray();
|
||||
|
||||
interface Body {
|
||||
solutions: UserSolution[];
|
||||
sessionId: string;
|
||||
userId: string;
|
||||
}
|
||||
|
||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { userId, solutions, sessionId } = req.body as Body;
|
||||
const disabledStats = await db.collection("stats").find(
|
||||
{ user: userId, session: sessionId, isDisabled: true }
|
||||
).toArray();
|
||||
|
||||
await Promise.all(disabledStats.map(async (stat) => {
|
||||
const matchingSolution = solutions.find(s => s.exercise === stat.exercise);
|
||||
if (matchingSolution) {
|
||||
const { _id, ...updateFields } = matchingSolution as WithId<UserSolution>;
|
||||
await db.collection("stats").updateOne(
|
||||
{ id: stat.id },
|
||||
{ $set: { ...matchingSolution } }
|
||||
{ $set: { ...updateFields } }
|
||||
);
|
||||
}
|
||||
}));
|
||||
21
src/pages/api/stats/session/[session].ts
Normal file
21
src/pages/api/stats/session/[session].ts
Normal file
@@ -0,0 +1,21 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import client from "@/lib/mongodb";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const {session} = req.query;
|
||||
const snapshot = await db.collection("stats").find({ user: req.session.user.id, session }).toArray();
|
||||
|
||||
res.status(200).json(snapshot);
|
||||
}
|
||||
@@ -32,6 +32,8 @@ import { EntityWithRoles } from "@/interfaces/entity";
|
||||
import CardList from "@/components/High/CardList";
|
||||
import { requestUser } from "@/utils/api";
|
||||
import { useAllowedEntities } from "@/hooks/useEntityPermissions";
|
||||
import getPendingEvals from "@/utils/disabled.be";
|
||||
import useEvaluationPolling from "@/hooks/useEvaluationPolling";
|
||||
|
||||
export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => {
|
||||
const user = await requestUser(req, res)
|
||||
@@ -45,9 +47,10 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => {
|
||||
const users = await (checkAccess(user, ["admin", "developer"]) ? getUsers() : getEntitiesUsers(mapBy(entities, 'id')))
|
||||
const assignments = await (checkAccess(user, ["admin", "developer"]) ? getAssignments() : getEntitiesAssignments(mapBy(entities, 'id')))
|
||||
const gradingSystems = await getGradingSystemByEntities(mapBy(entities, 'id'))
|
||||
const pendingSessionIds = await getPendingEvals(user.id);
|
||||
|
||||
return {
|
||||
props: serialize({ user, users, assignments, entities, gradingSystems }),
|
||||
props: serialize({ user, users, assignments, entities, gradingSystems, pendingSessionIds }),
|
||||
};
|
||||
}, sessionOptions);
|
||||
|
||||
@@ -59,11 +62,12 @@ interface Props {
|
||||
assignments: Assignment[];
|
||||
entities: EntityWithRoles[]
|
||||
gradingSystems: Grading[]
|
||||
pendingSessionIds: string[];
|
||||
}
|
||||
|
||||
const MAX_TRAINING_EXAMS = 10;
|
||||
|
||||
export default function History({ user, users, assignments, entities, gradingSystems }: Props) {
|
||||
export default function History({ user, users, assignments, entities, gradingSystems, pendingSessionIds }: Props) {
|
||||
const router = useRouter();
|
||||
const [statsUserId, setStatsUserId, training, setTraining] = useRecordStore((state) => [
|
||||
state.selectedUser,
|
||||
@@ -85,9 +89,11 @@ export default function History({ user, users, assignments, entities, gradingSys
|
||||
const groupedStats = useMemo(() => groupByDate(
|
||||
stats.filter((x) => {
|
||||
if (
|
||||
(x.module === "writing" || x.module === "speaking") &&
|
||||
!x.isDisabled &&
|
||||
!x.solutions.every((y) => Object.keys(y).includes("evaluation"))
|
||||
(
|
||||
x.module === "writing" || x.module === "speaking") &&
|
||||
!x.isDisabled && Array.isArray(x.solutions) &&
|
||||
!x.solutions.every((y) => Object.keys(y).includes("evaluation")
|
||||
)
|
||||
)
|
||||
return false;
|
||||
return true;
|
||||
@@ -179,6 +185,8 @@ export default function History({ user, users, assignments, entities, gradingSys
|
||||
);
|
||||
};
|
||||
|
||||
useEvaluationPolling(pendingSessionIds ? pendingSessionIds : [], "records", user.id);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
|
||||
Reference in New Issue
Block a user