Skip to content

Commit

Permalink
Ready for exam information
Browse files Browse the repository at this point in the history
  • Loading branch information
chamhayden committed Nov 26, 2024
1 parent dd88487 commit 953ecad
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 19 deletions.
55 changes: 46 additions & 9 deletions backend/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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({});
Expand Down Expand Up @@ -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;

Expand Down
39 changes: 31 additions & 8 deletions frontend/src/page/Assessments/AssessmentsExam.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Exam22T1 />
Component = <Exam22T1 />
} else if (getters.term === '22T3') {
return <Exam22T3 />
Component = <Exam22T3 />
} else if (getters.term === '23T1') {
return <Exam23T1 />
Component = <Exam23T1 />
} else if (getters.term === '23T3') {
return <Exam23T3 />
Component = <Exam23T3 />
} else if (getters.term === '24T1') {
return <Exam24T1 />
Component = <Exam24T1 />
} else if (getters.term === '24T3') {
return <Exam24T3 />
} else {
return <>This is a sample exam page!</>;
Component = <Exam24T3 />
}

return <>
{examInfo && <div style={{ margin: '20px', padding: '5px 20px', border: '1px solid #333' }}>
<h3>Your personal exam info</h3>
<ul>
<li>Room: {examInfo.room}</li>
<li>Date: {examInfo.date}</li>
<li>Start time: {examInfo.start}</li>
<li>End time: {examInfo.end}</li>
</ul>
</div>}
{Component}
</>
}

export default makePage(AssessmentsExam, {
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/util/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 953ecad

Please sign in to comment.