| 1 | let router = require("express").Router(); |
| 2 | const access = require("../utils/access"); |
| 3 | const UserModel = require("../models/user"); |
| 4 | const AssessmentModel = require("../models/assessment"); |
| 5 | const AssessmentEntity = require("../entities/assessment"); |
| 6 | const CourseModel = require("../models/course"); |
| 7 | const params = require("../config/parameters"); |
| 8 | const validator = require("../public/javascripts/utils/validation"); |
| 9 | const ObjectId = require("bson-objectid"); |
| 10 | const sanitizeHtml = require('sanitize-html'); |
| 11 | |
| 12 | router.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 | |
| 25 | router.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 |
| 47 | router.get("/start/assessment", access.ajax, (req,res) => { |
| 48 | let number = req.query["number"]; |
| 49 | let aid = req.query["aid"]; |
| 50 | let password = req.cookies["password"]; //potentially from cookies, resuming |
| 51 | let error = validator({ _id:aid, papers:[{number:number,password:password || "samplePwd"}] }, "Assessment"); |
| 52 | if (error.length > 0) |
| 53 | return res.json({errmsg:error}); |
| 54 | AssessmentModel.startSession(ObjectId(aid), number, password, (err,ret) => { |
| 55 | access.checkRequest(res,err,ret,"Failed session initialization", () => { |
| 56 | if (!password) |
| 57 | { |
| 58 | // Set password |
| 59 | res.cookie("password", ret.password, { |
| 60 | httpOnly: true, |
| 61 | maxAge: params.cookieExpire, |
| 62 | }); |
| 63 | } |
| 64 | res.json(ret); //contains questions+password(or paper if resuming) |
| 65 | }); |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | router.get("/send/answer", access.ajax, (req,res) => { |
| 70 | let aid = req.query["aid"]; |
| 71 | let number = req.query["number"]; |
| 72 | let password = req.query["password"]; |
| 73 | let input = JSON.parse(req.query["answer"]); |
| 74 | let error = validator({ _id:aid, papers:[{number:number,password:password,inputs:[input]}] }, "Assessment"); |
| 75 | if (error.length > 0) |
| 76 | return res.json({errmsg:error}); |
| 77 | AssessmentModel.newAnswer(ObjectId(aid), number, password, input, (err,ret) => { |
| 78 | access.checkRequest(res,err,ret,"Cannot send answer", () => { |
| 79 | res.json({}); |
| 80 | }); |
| 81 | }); |
| 82 | }); |
| 83 | |
| 84 | router.get("/end/assessment", access.ajax, (req,res) => { |
| 85 | let aid = req.query["aid"]; |
| 86 | let number = req.query["number"]; |
| 87 | let password = req.query["password"]; |
| 88 | let error = validator({ _id:aid, papers:[{number:number,password:password}] }, "Assessment"); |
| 89 | if (error.length > 0) |
| 90 | return res.json({errmsg:error}); |
| 91 | // Destroy pwd, set endTime, return conclusion |
| 92 | AssessmentModel.endSession(ObjectId(aid), number, password, (err,conclusion) => { |
| 93 | access.checkRequest(res,err,conclusion,"Cannot end assessment", () => { |
| 94 | res.clearCookie('password'); |
| 95 | res.json(conclusion); |
| 96 | }); |
| 97 | }); |
| 98 | }); |
| 99 | |
| 100 | module.exports = router; |