diff --git a/app/admin/page.tsx b/app/admin/page.tsx index c5d17e7..e60f0e1 100644 --- a/app/admin/page.tsx +++ b/app/admin/page.tsx @@ -6,7 +6,7 @@ import { collection, doc, getDocs, updateDoc } from 'firebase/firestore'; import { useRouter } from 'next/navigation'; import * as XLSX from 'xlsx'; import QuizDetails from '../components/QuizDetails'; -import {formatDate} from '../Date'; +import { formatDate } from '../Date'; import toast from 'react-hot-toast'; const AdminPage = () => { @@ -14,6 +14,7 @@ const AdminPage = () => { const router = useRouter(); const [userData, setUserData] = useState([]); const [quizData, setQuizData] = useState([]); + const [lockStatus, setLockStatus] = useState>({}); useEffect(() => { fetchQuizData(); @@ -44,7 +45,6 @@ const AdminPage = () => { userDataArray.push(data); } }); - // Sort userDataArray by USN userDataArray.sort((a: any, b: any) => a.USN.localeCompare(b.USN)); setUserData(userDataArray); } catch (error) { @@ -68,19 +68,38 @@ const AdminPage = () => { try { const querySnapshot = await getDocs(collection(db, 'quizzes')); const quizDoc = querySnapshot.docs.find(doc => doc.data().data.quizName === quizName); + if (quizDoc) { await updateDoc(doc(db, 'quizzes', quizDoc.id), { 'data.isDeleted': true, }); fetchQuizData(); - toast.success(`Quiz ${quizName} marked as deleted.`); + toast.success(`Quiz "${quizName}" marked as deleted.`); } else { console.error('Quiz not found:', quizName); - toast.error(`Quiz ${quizName} not found.`); + toast.error(`Quiz "${quizName}" not found.`); } } catch (error) { console.error('Error deleting quiz:', error); - toast.error(`Error marking quiz ${quizName} as deleted.`); + toast.error(`Error marking quiz "${quizName}" as deleted.`); + } + }; + + async function handleLockUnlockQuiz(quizName: any) { + const querySnapshot = await getDocs(collection(db, 'quizzes')); + const quizDoc = querySnapshot.docs.find(doc => doc.data().data.quizName === quizName); + + if (quizDoc) { + const currentLockStatus = quizDoc.data().data.isLocked; + await updateDoc(doc(db, 'quizzes', quizDoc.id), { + 'data.isLocked': !currentLockStatus, + }); + setLockStatus(prevStatus => ({ ...prevStatus, [quizName]: !currentLockStatus })); + fetchQuizData(); + toast.success(`Quiz "${quizName}" ${currentLockStatus ? 'unlocked' : 'locked'} successfully.`); + } else { + console.error('Quiz not found:', quizName); + toast.error(`Quiz "${quizName}" not found.`); } }; @@ -99,11 +118,8 @@ const AdminPage = () => { const handleDownloadExcel = () => { var d = new Date(); var n = formatDate(d); - - // Create a sorted copy of userData based on USN + const sortedUserData = [...userData].sort((a: any, b: any) => a.USN.localeCompare(b.USN)); - - // Flatten the sortedUserData array and include details for each quiz const flatData = sortedUserData.map(user => { const userFlat = { 'USN': user.USN, @@ -111,7 +127,7 @@ const AdminPage = () => { 'Student Name': user.displayName, 'Total Score': calculateTotalScore(user.quizData), // Flatten quizData array - ...user.quizData.reduce((acc : any, quiz : any, index : any) => ({ + ...user.quizData.reduce((acc: any, quiz: any, index: any) => ({ ...acc, [` ${quiz.quizName} `]: `${quiz.score} / ${quiz.totalQuestions}`, [`${quiz.quizName} Time`]: quiz.time, @@ -122,18 +138,17 @@ const AdminPage = () => { }; return userFlat; }); - + // Create Excel sheet const ws = XLSX.utils.json_to_sheet(flatData); const wb = XLSX.utils.book_new(); - XLSX.utils.book_append_sheet(wb, ws, `Student Data Sheet ${n}` ); + XLSX.utils.book_append_sheet(wb, ws, `Student Data Sheet ${n}`); const excelBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' }); saveAsExcelFile(excelBuffer, `Student Data Sheet ${n} .xlsx`); }; // Function to handle downloading individual quiz data - const handleDownloadQuiz = (quizName : any) => { - // Filter userData to get data for the specific quizName + const handleDownloadQuiz = (quizName: any) => { const quizData = userData.map(user => { const userQuiz = user.quizData.find((quiz: any) => quiz.quizName === quizName); return { @@ -147,7 +162,6 @@ const AdminPage = () => { }; }).filter(user => user['Quiz Name'] !== ''); - // Create Excel sheet for the specific quiz const ws = XLSX.utils.json_to_sheet(quizData); const wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, `${quizName} Data`); @@ -178,13 +192,13 @@ const AdminPage = () => {
{console.log(quizData)} {quizData - .map((quiz:any, index: any) => ( + .map((quiz: any, index: any) => (
+
))}
)} Create Quiz diff --git a/app/components/Fileinput.tsx b/app/components/Fileinput.tsx index 3edab7c..a570e9d 100644 --- a/app/components/Fileinput.tsx +++ b/app/components/Fileinput.tsx @@ -2,7 +2,8 @@ import React, { useState } from "react"; import * as XLSX from "xlsx"; import { useDropzone } from "react-dropzone"; import { v4 as uuidv4 } from 'uuid'; -import {useRouter} from "next/navigation"; +import { useRouter } from "next/navigation"; + interface FileInputProps { onFileUpload: (data: any) => void; } @@ -13,6 +14,7 @@ interface QuizInfo { courseCode: string; id: string; isDeleted: boolean; + isLcoked: boolean; } interface QuizData { @@ -31,6 +33,7 @@ function FileInput({ onFileUpload }: FileInputProps) { courseCode: "", id: uuidv4(), isDeleted: false, + isLcoked: false, } as QuizInfo); const [quizGenerated, setQuizGenerated] = useState(false); const [viewQuizData, setViewQuizData] = useState(false); @@ -47,7 +50,7 @@ function FileInput({ onFileUpload }: FileInputProps) { const excelData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); const quizData = condenseExcelData(excelData); const fileName = file.name; - + setExcelData(quizData as any); setFileName(fileName); }; diff --git a/app/student/page.tsx b/app/student/page.tsx index 572efc0..485d48c 100644 --- a/app/student/page.tsx +++ b/app/student/page.tsx @@ -7,26 +7,25 @@ import { db } from '../firebase'; import { onAuthStateChanged } from 'firebase/auth'; import { useRouter } from 'next/navigation'; -// Define a function to fetch quiz data async function fetchQuizData() { - const quizCollection = collection(db, 'quizzes'); - const querySnapshot = await getDocs(quizCollection); - const quizData:any = []; + const querySnapshot = await getDocs(collection(db, 'quizzes')); + const quizData: any = []; querySnapshot.forEach((doc) => { - quizData.push(doc.data()); + if (!doc.data().data.isDeleted && !doc.data().data.isLocked) { + quizData.push(doc.data()); + } }); - // console.log(quizData); return quizData; } async function fetchUserData() { const userCollection = collection(db, 'users'); const querySnapshot = await getDocs(userCollection); - const userData:any = []; + const userData: any = []; querySnapshot.forEach((doc) => { userData.push(doc.data()); }); - const currentUserData = userData.filter((entry:any) => { + const currentUserData = userData.filter((entry: any) => { return ( entry.uid == auth.currentUser?.uid ); @@ -34,25 +33,25 @@ async function fetchUserData() { console.log(currentUserData); return userData; } + // StudentPage component const StudentPage = () => { - const router = useRouter(); + const router = useRouter(); - const [user, setUser] = useState(auth.currentUser); - const [userData, setUserData] = useState([]); + const [user, setUser] = useState(auth.currentUser); + const [userData, setUserData] = useState([]); - useEffect(() => { - // Check the user's authentication state - onAuthStateChanged(auth, (user) => { - if (user) { - - setUser(user as any); - } else { - // Redirect unauthenticated users to the login page - router.push('/login'); - } - }); - }, []); + useEffect(() => { + // Check the user's authentication state + onAuthStateChanged(auth, (user) => { + if (user) { + setUser(user as any); + } else { + // Redirect unauthenticated users to the login page + router.push('/login'); + } + }); + }, []); const [quizzes, setQuizzes] = useState([]); @@ -66,60 +65,51 @@ const StudentPage = () => { } fetchData(); }, []); - // console.log("quizzes are " + quizzes) return (
{/* Quiz Cards Section */}
-

Join the quiz challenge and unlock your potential!

-
- {quizzes.map((quiz: any) => { - const userAttempts: any = userData.find( - (data: any) => data.uid === auth.currentUser?.uid - ); - - const isQuizAttempted = - userAttempts && - userAttempts.quizData.some((attempt: any) => attempt.quizId === quiz.data.id); - - return ( -
- {isQuizAttempted ? ( -
-

Quiz Name: {quiz.data.quizName}

-

Course: {quiz.data.course}

-

Course Code: {quiz.data.courseCode}

-

This quiz has been attempted.

+

Join the quiz challenge and unlock your potential!

+
+ {quizzes.map((quiz: any) => { + const userAttempts: any = userData.find( + (data: any) => data.uid === auth.currentUser?.uid + ); + + const isQuizAttempted = + userAttempts && + userAttempts.quizData.some((attempt: any) => attempt.quizId === quiz.data.id); + + return ( +
+ {isQuizAttempted ? ( +
+

Quiz Name: {quiz.data.quizName}

+

Course: {quiz.data.course}

+

Course Code: {quiz.data.courseCode}

+

This quiz has been attempted.

+
+ ) : ( + +
+

Quiz Name: {quiz.data.quizName}

+

Course: {quiz.data.course}

+

Course Code: {quiz.data.courseCode}

+
+ + )} +
+ ); + })}
- ) : ( - -
-

Quiz Name: {quiz.data.quizName}

-

Course: {quiz.data.course}

-

Course Code: {quiz.data.courseCode}

-
- - )} -
- ); -})} - - - - - - -
-
- +
); };