Commit | Line | Data |
---|---|---|
e99c53fb BA |
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"); | |
6 | ||
7 | const AssessmentModel = | |
8 | { | |
9 | getByRefs: function(initials, code, name, cb) | |
10 | { | |
11 | UserEntity.getByInitials(initials, (err,user) => { | |
12 | if (!!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"}); | |
20 | cb(null,assessment); | |
21 | }); | |
22 | }); | |
23 | }); | |
24 | }, | |
25 | ||
26 | add: function(uid, cid, name, cb) | |
27 | { | |
28 | // 1) Check that course is owned by user of ID uid | |
29 | CourseEntity.getById(cid, (err,course) => { | |
30 | if (!!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); | |
36 | }); | |
37 | }, | |
38 | ||
39 | update: function(uid, assessment, cb) | |
40 | { | |
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); | |
55 | }); | |
56 | }); | |
57 | }, | |
58 | ||
59 | // Set password in responses collection | |
f03a2ad9 | 60 | startSession: function(aid, number, password, cb) |
e99c53fb | 61 | { |
f03a2ad9 BA |
62 | AssessmentEntity.getPaperByNumber(aid, number, (err,paper) => { |
63 | if (!!err) | |
64 | return cb(err,null); | |
65 | if (!!paper) | |
66 | { | |
67 | if (!password) | |
68 | return cb({errmsg:"Missing password"}); | |
69 | if (paper.password != password) | |
70 | return cb({errmsg:"Wrong password"}); | |
71 | } | |
72 | AssessmentEntity.getQuestions(aid, (err,questions) => { | |
73 | if (!!err) | |
74 | return cb(err,null); | |
75 | if (!!paper) | |
76 | return cb(null,{paper:paper,questions:questions}); | |
2c545c26 BA |
77 | const pwd = TokenGen.generate(12); //arbitrary number, 12 seems enough... |
78 | AssessmentEntity.startSession(aid, number, pwd, (err2,ret) => { | |
f03a2ad9 BA |
79 | cb(err2, { |
80 | questions: questions, | |
81 | password: pwd, | |
82 | }); | |
e99c53fb BA |
83 | }); |
84 | }); | |
85 | }); | |
86 | }, | |
87 | ||
f03a2ad9 BA |
88 | newAnswer: function(aid, number, password, input, cb) |
89 | { | |
f03a2ad9 BA |
90 | // Check that student hasn't already answered |
91 | AssessmentEntity.hasInput(aid, number, password, input.index, (err,ret) => { | |
92 | if (!!err) | |
93 | return cb(err,null); | |
94 | if (!!ret) | |
95 | return cb({errmsg:"Question already answered"},null); | |
96 | AssessmentEntity.setInput(aid, number, password, input, (err2,ret2) => { | |
f03a2ad9 BA |
97 | if (!!err2 || !ret2) |
98 | return cb(err2,ret2); | |
99 | return cb(null,ret2); | |
100 | }); | |
101 | }); | |
102 | }, | |
103 | ||
e99c53fb BA |
104 | endSession: function(aid, number, password, cb) |
105 | { | |
106 | AssessmentEntity.endAssessment(aid, number, password, (err,ret) => { | |
107 | if (!!err || !ret) | |
108 | return cb(err,ret); | |
109 | AssessmentEntity.getConclusion(aid, (err2,conclusion) => { | |
110 | cb(err2, {conclusion:conclusion}); | |
111 | }); | |
112 | }); | |
113 | }, | |
114 | }; | |
115 | ||
116 | module.exports = AssessmentModel; |