9ab92ba448b071b1b1182d03de2ea958429073c5
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 checkPassword: function(aid
, number
, password
, cb
)
28 AssessmentEntity
.getById(aid
, (err
,assessment
) => {
29 if (!!err
|| !assessment
)
30 return cb(err
, assessment
);
31 const paperIdx
= assessment
.papers
.findIndex( item
=> { return item
.number
== number
; });
33 return cb({errmsg: "Paper not found"}, false);
34 cb(null, assessment
.papers
[paperIdx
].password
== password
);
38 add: function(uid
, cid
, name
, cb
)
40 // 1) Check that course is owned by user of ID uid
41 CourseEntity
.getById(cid
, (err
,course
) => {
43 return cb({errmsg: "Course retrieval failure"});
44 if (!course
.uid
.equals(uid
))
45 return cb({errmsg:"Not your course"},undefined);
46 // 2) Insert new blank assessment
47 AssessmentEntity
.insert(cid
, name
, cb
);
51 update: function(uid
, assessment
, cb
)
53 const aid
= ObjectId(assessment
._id
);
54 // 1) Check that assessment is owned by user of ID uid
55 AssessmentEntity
.getById(aid
, (err
,assessmentOld
) => {
56 if (!!err
|| !assessmentOld
)
57 return cb({errmsg: "Assessment retrieval failure"});
58 CourseEntity
.getById(ObjectId(assessmentOld
.cid
), (err2
,course
) => {
59 if (!!err2
|| !course
)
60 return cb({errmsg: "Course retrieval failure"});
61 if (!course
.uid
.equals(uid
))
62 return cb({errmsg:"Not your course"},undefined);
63 // 2) Replace assessment
64 delete assessment
["_id"];
65 assessment
.cid
= ObjectId(assessment
.cid
);
66 AssessmentEntity
.replace(aid
, assessment
, cb
);
71 // Set password in responses collection
72 startSession: function(aid
, number
, password
, cb
)
74 AssessmentEntity
.getPaperByNumber(aid
, number
, (err
,paper
) => {
77 if (!paper
&& !!password
)
78 return cb({errmsg: "Cannot start a new exam before finishing current"},null);
82 return cb({errmsg: "Missing password"});
83 if (paper
.password
!= password
)
84 return cb({errmsg: "Wrong password"});
86 AssessmentEntity
.getQuestions(aid
, (err
,questions
) => {
90 return cb(null,{paper:paper
,questions:questions
});
91 const pwd
= TokenGen
.generate(12); //arbitrary number, 12 seems enough...
92 AssessmentEntity
.startSession(aid
, number
, pwd
, (err2
,ret
) => {
102 newAnswer: function(aid
, number
, password
, input
, cb
)
104 // Check that student hasn't already answered
105 AssessmentEntity
.hasInput(aid
, number
, password
, input
.index
, (err
,ret
) => {
109 return cb({errmsg:"Question already answered"},null);
110 AssessmentEntity
.setInput(aid
, number
, password
, input
, (err2
,ret2
) => {
112 return cb(err2
,ret2
);
113 return cb(null,ret2
);
118 endSession: function(aid
, number
, password
, cb
)
120 AssessmentEntity
.endAssessment(aid
, number
, password
, (err
,ret
) => {
123 AssessmentEntity
.getConclusion(aid
, (err2
,conclusion
) => {
124 cb(err2
, {conclusion:conclusion
});
130 module
.exports
= AssessmentModel
;