9269aeedcf8e7ec4d003e2974e90df2b3a3ec5a4
1 const AssessmentEntity
= require("../entities/assessment");
2 const CourseEntity
= require("../entities/course");
3 const ObjectId
= require("bson-objectid");
4 const UserEntity
= require("../entities/user");
5 const TokenGen
= require("../utils/tokenGenerator");
7 const AssessmentModel
=
9 getByRefs: function(initials
, code
, name
, cb
)
11 UserEntity
.getByInitials(initials
, (err
,user
) => {
13 return cb(err
|| {errmsg: "User not found"});
14 CourseEntity
.getByPath(user
._id
, code
, (err2
,course
) => {
15 if (!!err2
|| !course
)
16 return cb(err2
|| {errmsg: "Course not found"});
17 AssessmentEntity
.getByPath(course
._id
, name
, (err3
,assessment
) => {
18 if (!!err3
|| !assessment
)
19 return cb(err3
|| {errmsg: "Assessment not found"});
26 add: function(uid
, cid
, name
, cb
)
28 // 1) Check that course is owned by user of ID uid
29 CourseEntity
.getById(cid
, (err
,course
) => {
31 return cb({errmsg: "Course retrieval failure"});
32 if (!course
.uid
.equals(uid
))
33 return cb({errmsg:"Not your course"},undefined);
34 // 2) Insert new blank assessment
35 AssessmentEntity
.insert(cid
, name
, cb
);
39 update: function(uid
, assessment
, cb
)
41 const qid
= ObjectId(assessment
._id
);
42 // 1) Check that assessment is owned by user of ID uid
43 AssessmentEntity
.getById(qid
, (err
,assessmentOld
) => {
44 if (!!err
|| !assessmentOld
)
45 return cb({errmsg: "Assessment retrieval failure"});
46 CourseEntity
.getById(ObjectId(assessmentOld
.cid
), (err2
,course
) => {
47 if (!!err2
|| !course
)
48 return cb({errmsg: "Course retrieval failure"});
49 if (!course
.uid
.equals(uid
))
50 return cb({errmsg:"Not your course"},undefined);
51 // 2) Replace assessment
52 delete assessment
["_id"];
53 assessment
.cid
= ObjectId(assessment
.cid
);
54 AssessmentEntity
.replace(qid
, assessment
, cb
);
59 // Set password in responses collection
60 startSession: function(aid
, number
, password
, cb
)
62 AssessmentEntity
.getPaperByNumber(aid
, number
, (err
,paper
) => {
68 return cb({errmsg:"Missing password"});
69 if (paper
.password
!= password
)
70 return cb({errmsg:"Wrong password"});
72 AssessmentEntity
.getQuestions(aid
, (err
,questions
) => {
76 return cb(null,{paper:paper
,questions:questions
});
77 const pwd
= TokenGen
.generate(12); //arbitrary number, 12 seems enough...
78 AssessmentEntity
.startSession(aid
, number
, pwd
, (err2
,ret
) => {
88 newAnswer: function(aid
, number
, password
, input
, cb
)
90 // Check that student hasn't already answered
91 AssessmentEntity
.hasInput(aid
, number
, password
, input
.index
, (err
,ret
) => {
95 return cb({errmsg:"Question already answered"},null);
96 AssessmentEntity
.setInput(aid
, number
, password
, input
, (err2
,ret2
) => {
104 endSession: function(aid
, number
, password
, cb
)
106 AssessmentEntity
.endAssessment(aid
, number
, password
, (err
,ret
) => {
109 AssessmentEntity
.getConclusion(aid
, (err2
,conclusion
) => {
110 cb(err2
, {conclusion:conclusion
});
116 module
.exports
= AssessmentModel
;