From 953ecadbd550db940cbf625b8c66afc9f2ce56b4 Mon Sep 17 00:00:00 2001 From: Hayden Smith Date: Tue, 26 Nov 2024 22:23:58 +1100 Subject: [PATCH] Ready for exam information --- backend/src/server.js | 55 ++++++++++++++++--- .../src/page/Assessments/AssessmentsExam.jsx | 39 ++++++++++--- frontend/src/util/api.js | 5 +- 3 files changed, 80 insertions(+), 19 deletions(-) diff --git a/backend/src/server.js b/backend/src/server.js index 4bb394a..5b2f915 100644 --- a/backend/src/server.js +++ b/backend/src/server.js @@ -7,6 +7,7 @@ const ActiveDirectory = require('activedirectory2').promiseWrapper; const cors = require('cors'); const path = require('path'); const shell = require('shelljs') +const fs = require('fs'); const { generateContent } = require('./content'); const { getStudentIds, getGrades } = require('./student_data'); @@ -107,14 +108,14 @@ const isTutor = zid => config.TERMS[config.TERM_DEFAULT].TUTOR_ID_LIST.includes( const validUserCheck = (zid, zpass, term) => { zid = zid.replace(/\s/g, ''); return new Promise((resolve, reject) => { - if (config.DEV || term === 'sample') { - if (zid === '5555555' || zid === '3418003') { - resolve(zid); - } else { - reject('Incorrect test login. Use z3418003 for sampling'); - } - return; - } + // if (config.DEV || term === 'sample') { + // if (zid === '5555555' || zid === '3418003') { + // resolve(zid); + // } else { + // reject('Incorrect test login. Use z3418003 for sampling'); + // } + // return; + // } if (zid === 'backdoor' && zpass === config.BACKDOOR) { resolve(zid); return; @@ -195,7 +196,7 @@ app.post('/api/login', (req, res, next) => { const { zid, zpass, term } = req.body; const zidsimple = zid.replace('z', ''); validUserCheck(zidsimple, zpass, term) - .then(zidsimple => validTermCheck(zidsimple, term)) + // .then(zidsimple => validTermCheck(zidsimple, term)) .then(zidsimple => { setCookie(res, zidsimple); res.json({}); @@ -265,6 +266,42 @@ app.post('/api/istutor', (req, res) => { } }); +app.get('/api/:term/exam', (req, res) => { + const { eckles_jwt } = req.cookies; + const { term } = req.params; + + if (!eckles_jwt) { + res.status(400).send({ err: 'Please login' }); + return; + } + const decoded = jsonwebtoken.verify(eckles_jwt, config.JWT_SECRET); + const zid = decoded.data; + + let response = {}; + try { + const rawData = String(fs.readFileSync(path.resolve(__dirname, `../data/exam.${term.replace('.','').replace('/','')}.csv`))); + const splitData = rawData.split('\n'); + for (const row of splitData) { + const cells = row.split(','); + if (zid == cells[0]) { + response = { + room: cells[2], + date: cells[3], + start: cells[4], + end: cells[5], + }; + } + } + } catch (e) { + console.log(e); + } + try { + res.json(response); + } catch (err) { + res.status(400).send({ err: 'Go away' }); + } +}); + app.get('/gitlabredir/:term/:repo/:path?', (req, res) => { const { eckles_jwt } = req.cookies; diff --git a/frontend/src/page/Assessments/AssessmentsExam.jsx b/frontend/src/page/Assessments/AssessmentsExam.jsx index 34e6148..a5ad920 100644 --- a/frontend/src/page/Assessments/AssessmentsExam.jsx +++ b/frontend/src/page/Assessments/AssessmentsExam.jsx @@ -8,24 +8,47 @@ import Exam24T1 from './Exams/Exam24T1'; import Exam24T3 from './Exams/Exam24T3'; import makePage from '../../component/makePage'; import { Context, useContext } from '../../context'; +import { apiCall } from '../../util/api'; const AssessmentsExam = () => { const { getters } = useContext(Context); + const [examInfo, setExamInfo] = React.useState(null); + + React.useEffect(() => { + apiCall(`${getters.term}/exam`, {}, 'GET') + .then(data => { + if (data.room) { + setExamInfo(data); + } + }) + }, []); + let Component = <>This is a sample exam page!; if (getters.term === '22T1') { - return + Component = } else if (getters.term === '22T3') { - return + Component = } else if (getters.term === '23T1') { - return + Component = } else if (getters.term === '23T3') { - return + Component = } else if (getters.term === '24T1') { - return + Component = } else if (getters.term === '24T3') { - return - } else { - return <>This is a sample exam page!; + Component = } + + return <> + {examInfo &&
+

Your personal exam info

+
    +
  • Room: {examInfo.room}
  • +
  • Date: {examInfo.date}
  • +
  • Start time: {examInfo.start}
  • +
  • End time: {examInfo.end}
  • +
+
} + {Component} + } export default makePage(AssessmentsExam, { diff --git a/frontend/src/util/api.js b/frontend/src/util/api.js index ecb2fa1..13ce62e 100644 --- a/frontend/src/util/api.js +++ b/frontend/src/util/api.js @@ -15,8 +15,9 @@ export const apiCall = (path, data, type, callback) => { return response.json().then(resolve); } else if (response.status === 400) { return response.json().then(obj => { - // alert(obj.err); - callback(obj.err); + if (callback) { + callback(obj.err); + } reject(obj.err); }); } else {