Fix sign of deltaTime for disconnections
[qomet.git] / models / assessment.js
index 3f89cc9..65d594b 100644 (file)
@@ -23,6 +23,18 @@ const AssessmentModel =
                });
        },
 
+       checkPassword: function(aid, number, password, cb)
+       {
+               AssessmentEntity.getById(aid, (err,assessment) => {
+                       if (!!err || !assessment)
+                               return cb(err, assessment);
+                       const paperIdx = assessment.papers.findIndex( item => { return item.number == number; });
+                       if (paperIdx === -1)
+                               return cb({errmsg: "Paper not found"}, false);
+                       cb(null, assessment.papers[paperIdx].password == password);
+               });
+       },
+
        add: function(uid, cid, name, cb)
        {
                // 1) Check that course is owned by user of ID uid
@@ -38,9 +50,9 @@ const AssessmentModel =
 
        update: function(uid, assessment, cb)
        {
-               const qid = ObjectId(assessment._id);
+               const aid = ObjectId(assessment._id);
                // 1) Check that assessment is owned by user of ID uid
-               AssessmentEntity.getById(qid, (err,assessmentOld) => {
+               AssessmentEntity.getById(aid, (err,assessmentOld) => {
                        if (!!err || !assessmentOld)
                                return cb({errmsg: "Assessment retrieval failure"});
                        CourseEntity.getById(ObjectId(assessmentOld.cid), (err2,course) => {
@@ -51,25 +63,70 @@ const AssessmentModel =
                                // 2) Replace assessment
                                delete assessment["_id"];
                                assessment.cid = ObjectId(assessment.cid);
-                               AssessmentEntity.replace(qid, assessment, cb);
+                               AssessmentEntity.replace(aid, assessment, cb);
                        });
                });
        },
 
        // Set password in responses collection
-       startSession: function(aid, number, cb)
+       startSession: function(aid, number, password, cb)
        {
-               const password = TokenGen.generate(12); //arbitrary number, 12 seems enough...
-               AssessmentEntity.getQuestions(aid, (err,questions) => {
-                       AssessmentEntity.startSession(aid, number, password, (err2,ret) => {
-                               cb(err, {
-                                       questions: questions,
-                                       password: password,
+               AssessmentEntity.getPaperByNumber(aid, number, (err,paper) => {
+                       if (!!err)
+                               return cb(err,null);
+                       if (!paper && !!password)
+                               return cb({errmsg: "Cannot start a new exam before finishing current"},null);
+                       if (!!paper)
+                       {
+                               if (!password)
+                                       return cb({errmsg: "Missing password"});
+                               if (paper.password != password)
+                                       return cb({errmsg: "Wrong password"});
+                       }
+                       AssessmentEntity.getQuestions(aid, (err,questions) => {
+                               if (!!err)
+                                       return cb(err,null);
+                               if (!!paper)
+                                       return cb(null,{paper:paper,questions:questions});
+                               const pwd = TokenGen.generate(12); //arbitrary number, 12 seems enough...
+                               AssessmentEntity.startSession(aid, number, pwd, (err2,ret) => {
+                                       cb(err2, {
+                                               questions: questions,
+                                               password: pwd,
+                                       });
                                });
                        });
                });
        },
 
+       newAnswer: function(aid, number, password, input, cb)
+       {
+               // Check that student hasn't already answered
+               AssessmentEntity.hasInput(aid, number, password, input.index, (err,ret) => {
+                       if (!!err)
+                               return cb(err,null);
+                       if (!!ret)
+                               return cb({errmsg:"Question already answered"},null);
+                       AssessmentEntity.setInput(aid, number, password, input, (err2,ret2) => {
+                               if (!!err2 || !ret2)
+                                       return cb(err2,ret2);
+                               return cb(null,ret2);
+                       });
+               });
+       },
+
+       // NOTE: no callbacks for 2 next functions, failures are not so important
+       // (because monitored: teachers can see what's going on)
+
+       newConnection: function(aid, number)
+       {
+               //increment discoCount, reset discoTime to NULL, update totalDisco
+               AssessmentEntity.getDiscoTime(aid, number, (err,discoTime) => {
+                       if (!!discoTime)
+                               AssessmentEntity.addDisco(aid, number, Date.now() - discoTime);
+               });
+       },
+
        endSession: function(aid, number, password, cb)
        {
                AssessmentEntity.endAssessment(aid, number, password, (err,ret) => {