82 lines
2.8 KiB
React
82 lines
2.8 KiB
React
import { getExamAssignmentById } from "@/app/lib/actions/examActions";
|
|
import MainSection from "@/app/(protected)/components/shared/Main";
|
|
import ExamResults from "@/app/(protected)/components/teacher/ExamResults";
|
|
import { auth } from "@/app/lib/utils/auth";
|
|
import { redirect } from "next/navigation";
|
|
import NotAuthorized from "@/app/auth/components/NotAuthorized";
|
|
import { getClassModel } from "@/app/models/Class";
|
|
import { isValidObjectId } from "@/app/lib/helpers/validObjectId";
|
|
|
|
export default async function AdminAssignmentResultsPage({ params }) {
|
|
const { id: assignmentId } = await params || {};
|
|
|
|
if (!isValidObjectId(assignmentId)) {
|
|
return (
|
|
<MainSection>
|
|
<div className="flex flex-col items-center justify-center py-20">
|
|
<p className="text-lg text-neutral-500 dark:text-neutral-400">Registro não encontrado.</p>
|
|
</div>
|
|
</MainSection>
|
|
);
|
|
}
|
|
|
|
const session = await auth();
|
|
|
|
if (!session?.user?.id) {
|
|
redirect("/auth/login");
|
|
}
|
|
|
|
const userRoles = Array.isArray(session.user.roles) ? session.user.roles : [];
|
|
const isAdmin =
|
|
userRoles.includes("admin") ||
|
|
userRoles.includes("superadmin") ||
|
|
session.user.role === "admin" ||
|
|
session.user.role === "superadmin";
|
|
if (!isAdmin) {
|
|
return <NotAuthorized />;
|
|
}
|
|
|
|
const result = await getExamAssignmentById(assignmentId);
|
|
if (!result.success) {
|
|
return (
|
|
<MainSection>
|
|
<div className="max-w-6xl mx-auto p-6">
|
|
<h1 className="text-2xl font-semibold text-neutral-900 dark:text-neutral-100">
|
|
Resultados da prova
|
|
</h1>
|
|
<p className="text-sm text-red-600 dark:text-red-400 mt-2">
|
|
{result.error || "Não foi possível carregar os resultados desta prova."}
|
|
</p>
|
|
</div>
|
|
</MainSection>
|
|
);
|
|
}
|
|
const assignment = result.success ? result.data : null;
|
|
|
|
// Check if current user is a teacher of the class
|
|
let canGrade = false;
|
|
if (session?.user?.id && assignment?.classId) {
|
|
const Class = await getClassModel();
|
|
const classRef = assignment.classId?._id || assignment.classId;
|
|
const classData = await Class.findById(classRef).lean();
|
|
const teacherIds = classData?.teachers?.map((t) => t.toString()) || [];
|
|
canGrade = teacherIds.includes(session.user.id);
|
|
}
|
|
|
|
return (
|
|
<MainSection>
|
|
<div className="max-w-6xl mx-auto p-6">
|
|
<div className="mb-6">
|
|
<h1 className="text-2xl font-semibold text-neutral-900 dark:text-neutral-100">
|
|
{assignment?.title || "Prova"}
|
|
</h1>
|
|
<p className="text-sm text-neutral-600 dark:text-neutral-400 mt-1">
|
|
Resultados e correção das provas
|
|
</p>
|
|
</div>
|
|
<ExamResults assignmentId={assignmentId} assignmentTitle={assignment?.title} canGrade={canGrade} />
|
|
</div>
|
|
</MainSection>
|
|
);
|
|
}
|