a1a41e20d682116478f01fea9ea1e62031655df6
[qomet.git] / models / assessment.js
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
60 startSession: function(aid, number, password, cb)
61 {
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});
77 AssessmentEntity.startSession(aid, number, password, (err2,ret) => {
78 const pwd = TokenGen.generate(12); //arbitrary number, 12 seems enough...
79 cb(err2, {
80 questions: questions,
81 password: pwd,
82 });
83 });
84 });
85 });
86 },
87
88 newAnswer: function(aid, number, password, input, cb)
89 {
90 console.log(JSON.stringify(input));
91 // Check that student hasn't already answered
92 AssessmentEntity.hasInput(aid, number, password, input.index, (err,ret) => {
93 if (!!err)
94 return cb(err,null);
95 if (!!ret)
96 return cb({errmsg:"Question already answered"},null);
97 AssessmentEntity.setInput(aid, number, password, input, (err2,ret2) => {
98 console.log(JSON.stringify(ret2));
99 if (!!err2 || !ret2)
100 return cb(err2,ret2);
101 return cb(null,ret2);
102 });
103 });
104 },
105
106 endSession: function(aid, number, password, cb)
107 {
108 AssessmentEntity.endAssessment(aid, number, password, (err,ret) => {
109 if (!!err || !ret)
110 return cb(err,ret);
111 AssessmentEntity.getConclusion(aid, (err2,conclusion) => {
112 cb(err2, {conclusion:conclusion});
113 });
114 });
115 },
116 };
117
118 module.exports = AssessmentModel;