Attempt to resurrect qomet code - need some rewrite
[qomet.git] / routes / pages.js
CommitLineData
e99c53fb
BA
1let router = require("express").Router();
2const access = require("../utils/access");
43828378 3const UserModel = require("../models/user");
a3080c33 4const EvaluationModel = require("../models/evaluation");
43828378 5const CourseModel = require("../models/course");
e99c53fb
BA
6
7// Actual pages (least specific last)
8
a3080c33 9// List initials and count evaluations
e99c53fb 10router.get("/", (req,res) => {
43828378 11 UserModel.getAll( (err,userArray) => {
e99c53fb
BA
12 if (!!err)
13 return res.json(err);
14 res.render("index", {
15 title: "home",
16 userArray: userArray,
17 });
18 });
19});
20
21// Login screen
22router.get("/login", access.unlogged, (req,res) => {
23 res.render("login", {
24 title: "login",
25 });
26});
27
28// Redirection screens when possible cheating attempt detected in exam
29router.get("/enablejs", (req,res) => {
cc7c0f5e 30 res.render("enablejs", {
e99c53fb
BA
31 title: "JS disabled",
32 });
33});
34
cc7c0f5e
BA
35router.get("/fullscreen", (req,res) => {
36 res.render("fullscreen", {
37 title: "Not in fullscreen",
38 });
39});
40
41router.get("/noblur", (req,res) => {
42 res.render("noblur", {
43 title: "Lost focus",
e99c53fb
BA
44 });
45});
46
47// List courses of some user (should be [a-z]+[0-9]* but fails...)
48router.get("/:initials([a-z0-9]+)", (req,res) => {
49 let initials = req.params["initials"];
50 CourseModel.getByInitials(initials, (err,courseArray) => {
51 if (!!err)
52 return res.json(err);
53 access.getUser(req, res, (err2,user) => {
54 const isTeacher = !!user && user.initials == initials;
55 // Strip students from courses if not course admin (TODO: not required in any case)
56 if (!isTeacher)
57 {
58 courseArray.forEach( c => {
59 delete c["students"];
60 });
61 }
62 res.render("course-list", {
63 title: initials + " courses",
64 courseArray: courseArray,
65 teacher: isTeacher,
66 initials: initials,
67 });
68 });
69 });
70});
71
72// Detailed content of one course
73router.get("/:initials([a-z0-9]+)/:courseCode([a-z0-9._-]+)", (req,res) => {
74 let initials = req.params["initials"];
75 let code = req.params["courseCode"];
76 CourseModel.getByRefs(initials, code, (err,course) => {
77 access.checkRequest(res, err, course, "Course not found", () => {
a3080c33 78 EvaluationModel.getByCourse(course._id, (err2,evaluationArray) => {
e99c53fb
BA
79 if (!!err)
80 return res.json(err);
81 access.getUser(req, res, (err2,user) => {
82 const isTeacher = !!user && user.initials == initials;
83 // Strip students from course if not course admin
84 if (!isTeacher)
85 delete course["students"];
86 res.render("course", {
87 title: "course " + initials + "/" + code,
88 course: course,
a3080c33 89 evaluationArray: evaluationArray,
e99c53fb
BA
90 teacher: isTeacher,
91 initials: initials,
92 });
93 });
94 });
95 });
96 });
97});
98
43828378
BA
99// Grading students answers: --> after identification (password), always send secret with requests
100router.get("/:initials([a-z0-9]+)/:courseCode([a-z0-9._-]+)/grade", (req,res) => {
101 let initials = req.params["initials"];
102 let code = req.params["courseCode"];
103 // TODO: if (main) teacher, also send secret, saving one request
104 res.render("grade", {
105 title: "grade exams " + code + "/" + name,
106 initials: initials,
107 courseCode: code,
108 });
109});
110
a3080c33
BA
111// Display evaluation (exam or open status)
112router.get("/:initials([a-z0-9]+)/:courseCode([a-z0-9._-]+)/:evaluationName([a-z0-9._-]+)", (req,res) => {
e99c53fb
BA
113 let initials = req.params["initials"];
114 let code = req.params["courseCode"];
a3080c33
BA
115 let name = req.params["evaluationName"];
116 EvaluationModel.getByRefs(initials, code, name, (err,evaluation) => {
117 access.checkRequest(res, err, evaluation, "Evaluation not found", () => {
118 if (!evaluation.active)
119 return res.json({errmsg: "Evaluation is idle"});
120 delete evaluation["papers"]; //always remove recorded students answers
121 if (evaluation.mode == "exam")
e99c53fb
BA
122 {
123 if (!!req.headers['user-agent'].match(/(SpecialAgent|HeadlessChrome|PhantomJS)/))
124 {
125 // Basic headless browser detection
126 return res.json({errmsg: "Headless browser detected"});
127 }
db5571d6 128 // Strip questions if exam mode (stepwise process)
a3080c33 129 delete evaluation["questions"];
e99c53fb 130 }
a3080c33
BA
131 res.render("evaluation", {
132 title: "evaluation " + initials + "/" + code + "/" + name,
133 evaluation: evaluation,
e99c53fb
BA
134 });
135 });
136 });
137});
138
71d1ca9c 139// Monitor: --> after identification (password), always send secret with requests
a3080c33 140router.get("/:initials([a-z0-9]+)/:courseCode([a-z0-9._-]+)/:evaluationName([a-z0-9._-]+)/monitor", (req,res) => {
e99c53fb
BA
141 let initials = req.params["initials"];
142 let code = req.params["courseCode"];
a3080c33 143 let name = req.params["evaluationName"];
71d1ca9c 144 // TODO: if (main) teacher, also send secret, saving one request
e99c53fb 145 res.render("monitor", {
a3080c33 146 title: "monitor evaluation " + code + "/" + name,
e99c53fb 147 initials: initials,
71d1ca9c
BA
148 courseCode: code,
149 examName: name,
e99c53fb
BA
150 });
151});
152
153module.exports = router;