'update'
[qomet.git] / public / javascripts / assessment.js
index 9858643..93200cc 100644 (file)
@@ -10,9 +10,9 @@ function checkWindowSize()
                return true;
        // 3 is arbitrary, but a small tolerance is required (e.g. in Firefox)
        return window.innerWidth >= screen.width-3 && window.innerHeight >= screen.height-3;
-};
+}
 
-let V = new Vue({
+new Vue({
        el: "#assessment",
        data: {
                assessment: assessment,
@@ -39,25 +39,42 @@ let V = new Vue({
        },
        mounted: function() {
                $(".modal").modal();
-               if (assessment.mode != "secure")
+               if (["exam","open"].includes(assessment.mode))
                        return;
-               window.addEventListener("keydown", e => {
-                       // Ignore F12 (avoid accidental window resize due to devtools)
-                       // NOTE: in Chromium at least, fullscreen mode exit with F11 cannot be prevented.
-                       // Workaround: disable key at higher level. Possible xbindkey config:
-                       // "false"
-                       //   m:0x10 + c:95
-                       //   Mod2 + F11
-                       if (e.keyCode == 123)
-                               e.preventDefault();
-               }, false);
                window.addEventListener("blur", () => {
-                       this.trySendCurrentAnswer();
-                       document.location.href= "/noblur";
+                       if (this.stage != 2)
+                               return;
+                       if (assessment.mode == "secure")
+                       {
+                               this.trySendCurrentAnswer();
+                               document.location.href= "/noblur";
+                       }
+                       else //"watch" mode
+                               socket.emit(message.studentBlur, {number:this.student.number});
                }, false);
+               if (assessment.mode == "watch")
+               {
+                       window.addEventListener("focus", () => {
+                               if (this.stage != 2)
+                                       return;
+                               socket.emit(message.studentFocus, {number:this.student.number});
+                       }, false);
+               }
                window.addEventListener("resize", e => {
-                       this.trySendCurrentAnswer();
-                       document.location.href= "/fullscreen";
+                       if (this.stage != 2)
+                               return;
+                       if (assessment.mode == "secure")
+                       {
+                               this.trySendCurrentAnswer();
+                               document.location.href = "/fullscreen";
+                       }
+                       else //"watch" mode
+                       {
+                               if (checkWindowSize())
+                                       socket.emit(message.studentFullscreen, {number:this.student.number});
+                               else
+                                       socket.emit(message.studentResize, {number:this.student.number});
+                       }
                }, false);
        },
        methods: {
@@ -105,6 +122,9 @@ let V = new Vue({
                                $("#leftButton, #rightButton").hide();
                                if (assessment.time > 0)
                                {
+
+// TODO: distinguish total exam time AND question time
+
                                        const deltaTime = !!paper ? Date.now() - paper.startTime : 0;
                                        this.remainingTime = assessment.time * 60 - Math.round(deltaTime / 1000);
                                        this.runTimer();
@@ -157,7 +177,7 @@ let V = new Vue({
                                                // Got password: students answers locked to this page until potential teacher
                                                // action (power failure, computer down, ...)
                                        }
-                                       socket = io.connect("/" + assessment.name, {
+                                       socket = io.connect("/", {
                                                query: "aid=" + assessment._id + "&number=" + this.student.number + "&password=" + this.student.password
                                        });
                                        socket.on(message.allAnswers, this.setAnswers);
@@ -165,8 +185,10 @@ let V = new Vue({
                                },
                        });
                },
+
+
                // stage 2
-               runTimer: function() {
+               runGlobalTimer: function() {
                        if (assessment.time <= 0)
                                return;
                        let self = this;
@@ -180,8 +202,25 @@ let V = new Vue({
                                }
                        }, 1000);
                },
+               runQuestionTimer: function(idx) {
+                       if (assessment.questions[idx].time <= 0)
+                               return;
+                       let self = this; //TODO: question remaining time
+                       setInterval( function() {
+                               self.remainingTime--;
+                               if (self.remainingTime <= 0)
+                               {
+                                       if (self.stage == 2)
+                                               self.endAssessment();
+                                       clearInterval(this);
+                               }
+                       }, 1000);
+               },
+
+//TODO: get question after sending answer
+
                // stage 2
-               sendAnswer: function() {
+               sendOneAnswer: function() {
                        const realIndex = this.answers.indices[this.answers.index];
                        let gotoNext = () => {
                                if (this.answers.index == assessment.questions.length - 1)
@@ -216,6 +255,13 @@ let V = new Vue({
                                },
                        });
                },
+               // TODO: I don't like that + sending should not be definitive in exam mode with display = all
+               sendAnswer: function() {
+                       if (assessment.display == "one")
+                               this.sendOneAnswer();
+                       else
+                               assessment.questions.forEach(this.sendOneAnswer);
+               },
                // stage 2 --> 3 (or 4)
                // from a message by statements component, or time over
                endAssessment: function() {
@@ -225,6 +271,7 @@ let V = new Vue({
                        {
                                this.stage = 4;
                                this.answers.showSolution = true;
+                               this.answers.displayAll = true;
                                return;
                        }
                        $.ajax("/end/assessment", {
@@ -238,20 +285,21 @@ let V = new Vue({
                                success: ret => {
                                        if (!!ret.errmsg)
                                                return this.showWarning(ret.errmsg);
-                                       assessment.conclusion = ret.conclusion;
                                        this.stage = 3;
                                        delete this.student["password"]; //unable to send new answers now
-                                       socket.disconnect();
-                                       socket = null;
                                },
                        });
                },
                // stage 3 --> 4 (on socket message "feedback")
                setAnswers: function(m) {
-                       for (let i=0; i<m.answers.length; i++)
-                               assessment.questions[i].answer = m.answers[i];
+                       const answers = JSON.parse(m.answers);
+                       for (let i=0; i<answers.length; i++)
+                               assessment.questions[i].answer = answers[i];
                        this.answers.showSolution = true;
+                       this.answers.displayAll = true;
                        this.stage = 4;
+                       socket.disconnect();
+                       socket = null;
                },
        },
 });