diff --git a/actions/grades.ts b/actions/grades.ts index dee4386..4d1f057 100644 --- a/actions/grades.ts +++ b/actions/grades.ts @@ -8,7 +8,6 @@ export async function getGrade(testId: number, userId: number) { grade: true, id: true, testId: true, - note: true, createdAt: true, }, where: { @@ -24,7 +23,6 @@ export async function addGrade(testId: number, userId: number, grade: number, no const newGrade = await prisma.grade.create({ data: { grade: grade, - note: note, testId: testId, userId: userId, } diff --git a/actions/mangeTest.ts b/actions/mangeTest.ts index 02a569c..ddc82db 100644 --- a/actions/mangeTest.ts +++ b/actions/mangeTest.ts @@ -16,7 +16,6 @@ export async function getActiveTest(date: Date) { select: { id: true, isActive: true, - isPassed: true, testOf: { select: { id: true, @@ -30,7 +29,6 @@ export async function getActiveTest(date: Date) { }, where: { isActive: true, - isPassed: false, testOn: new Date(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + (date.getDate().toString().length === 1 ? '0' + date.getDate() : date.getDate())), }, }); @@ -41,7 +39,6 @@ export async function getActiveTestWithGrade(date: Date, userId: number) { select: { id: true, isActive: true, - isPassed: true, testOf: { select: { id: true, @@ -54,7 +51,6 @@ export async function getActiveTestWithGrade(date: Date, userId: number) { select: { id: true, grade: true, - note: true, createdAt: true, }, take: 1, @@ -67,7 +63,6 @@ export async function getActiveTestWithGrade(date: Date, userId: number) { }, where: { isActive: true, - isPassed: false, testOn: new Date(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + (date.getDate().toString().length === 1 ? '0' + date.getDate() : date.getDate())), }, }); diff --git a/actions/results.ts b/actions/results.ts new file mode 100644 index 0000000..cd42c1d --- /dev/null +++ b/actions/results.ts @@ -0,0 +1,33 @@ +"use server"; +import prisma from "@/lib/prisma"; + +export interface Results { + name: string; + total: number; +} + +export async function getResults(testId: number) { + const grades = await prisma.grade.findMany({ where: { testId: testId, user: { isTeacher: false } } }); + + const allGrades = ['1', '1.5', '2', '2.5', '3', '3.5', '4', '4.5', '5', '5.5', '6']; + let gradeOccurences = new Array(allGrades.length).fill(0); + const gradeList = grades.map((grade) => grade.grade); + + for (let i = 0; i < gradeList.length; i++) { + gradeOccurences[allGrades.indexOf(gradeList[i].toString())]++; + } + + let data: Results[] = []; + for (let i = 0; i < gradeOccurences.length; i++) { + data.push({ + name: allGrades[i], + total: gradeOccurences[i], + }); + } + + return data; +} + +export async function getTeacherResult(testId: number) { + return await prisma.grade.findFirst({ where: { testId: testId, user: { isTeacher: true } } }); +} \ No newline at end of file diff --git a/app/dashboard/page.tsx b/app/dashboard/page.tsx index e0b56e6..3ef202e 100644 --- a/app/dashboard/page.tsx +++ b/app/dashboard/page.tsx @@ -1,18 +1,16 @@ import prisma from '@/lib/prisma'; import ActiveCard from './ActiveCard'; import { getAuthServerSession } from '@/lib/authenticate'; -import { red } from 'next/dist/lib/picocolors'; import { redirect } from 'next/navigation'; export default async function Dashboard() { const authSession = await getAuthServerSession(); console.log(authSession); - if (!authSession || !authSession.user.isTeacher) return redirect('/'); + if (!authSession) return redirect('/'); - const tests = await prisma.test.findMany({ select: { isActive: true, isPassed: true, id: true, testOf: { select: { id: true, firstName: true, lastName: true, isTeacher: true } } } }); + const tests = await prisma.test.findMany({ select: { isActive: true, id: true, testOf: { select: { id: true, firstName: true, lastName: true, isTeacher: true } } } }); const activeTests = tests.filter((test) => test.isActive); - const passedTests = tests.filter((test) => test.isPassed); return (
diff --git a/app/results/[id]/page.tsx b/app/results/[id]/page.tsx index 021e298..0920b07 100644 --- a/app/results/[id]/page.tsx +++ b/app/results/[id]/page.tsx @@ -1,43 +1,43 @@ -'use server'; +'use client'; import { Chart } from '@components/custom/chart'; import logo from '@images/logo.svg'; import Image from 'next/image'; -import Prisma from '@lib/prisma'; +import ginger from '@images/ginger.png'; +import { getResults, getTeacherResult, Results } from '@/actions/results'; +import { useEffect, useState } from 'react'; -interface data { - name: string; - total: number; -} +export default function Page({ params }: { params: { id: string } }) { + const testId = parseInt(params.id); -export default async function Page({ params }: { params: { id: string } }) { - const teacherGrade = await Prisma.grade.findFirst({ where: { testId: parseInt(params.id), user: { isTeacher: true } } }); + const [teacherGrade, setTeacherGrade] = useState(0); + const [data, setData] = useState([]); - const grades = await Prisma.grade.findMany({where: {testId: parseInt(params.id), user: {isTeacher: false}}}); + function featchResults(testId: number) { + getTeacherResult(testId) + .then((res) => setTeacherGrade(res?.grade || 0)) + .catch((err) => console.error(err)); - const allGrades = ['1', '1.5', '2', '2.5', '3', '3.5', '4', '4.5', '5', '5.5', '6']; - let gradeOccurences = new Array(allGrades.length).fill(0); - const gradeList = grades.map((grade) => grade.grade); - - - for (let i = 0; i < gradeList.length; i++) { - gradeOccurences[allGrades.indexOf(gradeList[i].toString())]++; + getResults(testId) + .then((res) => setData(res)) + .catch((err) => console.error(err)); } - let data: data[] = []; - for (let i = 0; i < gradeOccurences.length; i++) { - data.push({ - name: allGrades[i], - total: gradeOccurences[i], - }); - } + useEffect(() => { + featchResults(testId); + setTimeout(() => { + featchResults(testId); + }, 5000); + }, [testId]); return (
{'Logo'} {teacherGrade && ( -
-

Mme Tixhon :

-

{teacherGrade.grade}

+
+
+ {'Logo'} +

{teacherGrade}

+
)} diff --git a/components/custom/chart.tsx b/components/custom/chart.tsx index a271d8f..526dd0c 100644 --- a/components/custom/chart.tsx +++ b/components/custom/chart.tsx @@ -1,32 +1,32 @@ -"use client" +'use client'; -import {Santa} from "@components/custom/santa"; +import { Santa } from '@components/custom/santa'; interface ChartProps { - data: { - name: string - total: number - }[] + data: { + name: string; + total: number; + }[]; } -export function Chart({...props}: ChartProps) { - return ( -
-
-
- {props.data.map((item, index) => ( - - ))} -
-
- {props.data.map((item, index) => ( -
-
- {item.name} -
- ))} -
-
-
- ) +export function Chart({ ...props }: ChartProps) { + return ( +
+
+
+ {props.data.map((item, index) => ( + + ))} +
+
+ {props.data.map((item, index) => ( +
+
+ {item.name} +
+ ))} +
+
+
+ ); } diff --git a/components/forms/GradingForm.tsx b/components/forms/GradingForm.tsx index 5d1be4c..f1d796b 100644 --- a/components/forms/GradingForm.tsx +++ b/components/forms/GradingForm.tsx @@ -8,7 +8,7 @@ import { Session } from 'next-auth'; import { addGrade } from '@/actions/grades'; export function GradingForm({ session, testId }: { session: Session; testId: number }) { - const [grade, setGrade] = useState(0); + const [grade, setGrade] = useState(4); const [hasVoted, setHasVoted] = useState(false); async function handleSubmit(event: React.FormEvent) { @@ -39,7 +39,7 @@ export function GradingForm({ session, testId }: { session: Session; testId: num
- setGrade(Number(e.target.value) / 2)} /> + setGrade(Number(e.target.value))} />
{YourGrade} diff --git a/images/ginger.png b/images/ginger.png new file mode 100644 index 0000000..fa765e3 Binary files /dev/null and b/images/ginger.png differ diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 3e07a65..6c13d28 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -14,27 +14,25 @@ model Users { firstName String lastName String isTeacher Boolean @default(false) + isAdmin Boolean @default(false) createdAt DateTime @default(now()) grades Grade[] test Test? } model Test { - id Int @id @default(autoincrement()) - testOfId Int @unique - testOn DateTime @db.Date - createdAt DateTime @default(now()) - testOf Users @relation(fields: [testOfId], references: [id]) - grades Grade[] - isActive Boolean @default(false) - isPassed Boolean @default(false) - teacherGrade Float? + id Int @id @default(autoincrement()) + testOfId Int @unique + testOn DateTime @db.Date + createdAt DateTime @default(now()) + testOf Users @relation(fields: [testOfId], references: [id]) + grades Grade[] + isActive Boolean @default(false) } model Grade { id Int @id @default(autoincrement()) userId Int - note String? createdAt DateTime @default(now()) grade Float testId Int