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');
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");
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", () => {
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");
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
);
39 AssessmentModel
.update(req
.user
._id
, assessment
, (err
,ret
) => {
40 access
.checkRequest(res
, err
, ret
, "Assessment update failed", () => {
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 error
= validator({ _id:aid
, papers:[{number:number
}] }, "Assessment");
52 return res
.json({errmsg:error
});
53 AssessmentModel
.startSession(ObjectId(aid
), number
, (err
,ret
) => {
54 access
.checkRequest(res
,err
,ret
,"Failed session initialization", () => {
56 res
.cookie("password", ret
.password
, {
58 maxAge: params
.cookieExpire
,
60 res
.json(ret
); //contains questions+password
65 router
.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");
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", () => {
80 router
.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");
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');
96 module
.exports
= router
;