First commit
[qomet.git] / routes / assessments.js
... / ...
CommitLineData
1let router = require("express").Router();
2const access = require("../utils/access");
3const UserModel = require("../models/user");
4const AssessmentModel = require("../models/assessment");
5const AssessmentEntity = require("../entities/assessment");
6const CourseModel = require("../models/course");
7const params = require("../config/parameters");
8const validator = require("../public/javascripts/utils/validation");
9const ObjectId = require("bson-objectid");
10const sanitizeHtml = require('sanitize-html');
11
12router.get("/add/assessment", access.ajax, access.logged, (req,res) => {
13 const name = req.query["name"];
14 const cid = req.query["cid"];
15 let error = validator({cid:cid, name:name}, "Assessment");
16 if (error.length > 0)
17 return res.json({errmsg:error});
18 AssessmentModel.add(req.user._id, ObjectId(cid), name, (err,assessment) => {
19 access.checkRequest(res, err, assessment, "Assessment addition failed", () => {
20 res.json(assessment);
21 });
22 });
23});
24
25router.post("/update/assessment", access.ajax, access.logged, (req,res) => {
26 const assessment = JSON.parse(req.body["assessment"]);
27 let error = validator(assessment, "Assessment");
28 if (error.length > 0)
29 return res.json({errmsg:error});
30 const sanitizeOpts = {allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ]) };
31 assessment.introduction = sanitizeHtml(assessment.introduction, sanitizeOpts);
32 assessment.conclusion = sanitizeHtml(assessment.conclusion, sanitizeOpts);
33 assessment.questions.forEach( q => {
34 q.wording = sanitizeHtml(q.wording, sanitizeOpts);
35 //q.answer = sanitizeHtml(q.answer); //if text (TODO: it's an array in this case?!)
36 for (let i=0; i<q.options.length; i++) //if QCM
37 q.options[i] = sanitizeHtml(q.options[i], sanitizeOpts);
38 });
39 AssessmentModel.update(req.user._id, assessment, (err,ret) => {
40 access.checkRequest(res, err, ret, "Assessment update failed", () => {
41 res.json({});
42 });
43 });
44});
45
46// Generate and set student password, return it
47router.get("/start/assessment", access.ajax, (req,res) => {
48 let number = req.query["number"];
49 let aid = req.query["aid"];
50 let error = validator({ _id:aid, papers:[{number:number}] }, "Assessment");
51 if (error.length > 0)
52 return res.json({errmsg:error});
53 AssessmentModel.startSession(ObjectId(aid), number, (err,ret) => {
54 access.checkRequest(res,err,ret,"Failed session initialization", () => {
55 // Set password
56 res.cookie("password", ret.password, {
57 httpOnly: true,
58 maxAge: params.cookieExpire,
59 });
60 res.json(ret); //contains questions+password
61 });
62 });
63});
64
65router.get("/send/answer", access.ajax, (req,res) => {
66 let aid = req.query["aid"];
67 let number = req.query["number"];
68 let password = req.query["password"];
69 let input = JSON.parse(req.query["answer"]);
70 let error = validator({ _id:aid, papers:[{number:number,password:password,inputs:[input]}] }, "Assessment");
71 if (error.length > 0)
72 return res.json({errmsg:error});
73 AssessmentEntity.setInput(ObjectId(aid), number, password, input, (err,ret) => {
74 access.checkRequest(res,err,ret,"Cannot send answer", () => {
75 res.json({});
76 });
77 });
78});
79
80router.get("/end/assessment", access.ajax, (req,res) => {
81 let aid = req.query["aid"];
82 let number = req.query["number"];
83 let password = req.query["password"];
84 let error = validator({ _id:aid, papers:[{number:number,password:password}] }, "Assessment");
85 if (error.length > 0)
86 return res.json({errmsg:error});
87 // Destroy pwd, set endTime, return conclusion
88 AssessmentModel.endSession(ObjectId(aid), number, password, (err,conclusion) => {
89 access.checkRequest(res,err,conclusion,"Cannot end assessment", () => {
90 res.clearCookie('password');
91 res.json(conclusion);
92 });
93 });
94});
95
96module.exports = router;