early draft of sockets logic for monitoring
[qomet.git] / public / javascripts / assessment.js
index 5fe24cd..c014b5a 100644 (file)
@@ -15,6 +15,15 @@ function checkWindowSize()
        return window.innerWidth >= screen.width-3 && window.innerHeight >= screen.height-3;
 };
 
+function libsRefresh()
+{
+       // Run Prism + MathJax on questions text
+       $("#statements").find("code[class^=language-]").each( (i,elem) => {
+               Prism.highlightElement(elem);
+       });
+       MathJax.Hub.Queue(["Typeset",MathJax.Hub,"statements"]);
+}
+
 new Vue({
        el: "#assessment",
        data: {
@@ -34,8 +43,6 @@ new Vue({
                "statements": {
                        props: ['assessment','inputs','student','stage'],
                        // TODO: general render function for nested exercises
-                       // TODO: with answer if stage==4 : class "wrong" if ticked AND stage==4 AND received answers
-                       // class "right" if stage == 4 AND received answers (background-color: red / green)
                        // There should be a questions navigator below, or next (visible if display=='all')
                        // Full questions tree is rendered, but some parts hidden depending on display settings
                        render(h) {
@@ -165,13 +172,13 @@ new Vue({
                                if (assessment.mode != "secure")
                                        return;
                                window.addEventListener("keydown", e => {
-                                       // (Try to) Ignore F11 + F12 (avoid accidental window resize)
-                                       // NOTE: in Chromium at least, exiting fullscreen mode with F11 cannot be prevented.
+                                       // 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 ([122,123].includes(e.keyCode))
+                                       if (e.keyCode == 123)
                                                e.preventDefault();
                                }, false);
                                window.addEventListener("blur", () => {
@@ -183,6 +190,9 @@ new Vue({
                                        document.location.href= "/fullscreen";
                                }, false);
                        },
+                       updated: function() {
+                               libsRefresh(); //TODO: shouldn't be required: "MathJax" strings on start and assign them to assessment.questions. ...
+                       },
                        methods: {
                                inputId: function(i,j) {
                                        return "q" + i + "_" + "input" + j;
@@ -193,14 +203,15 @@ new Vue({
                                },
                                // stage 2
                                sendAnswer: function(realIndex) {
-                                       console.log(realIndex);
-                                       if (assessment.index == assessment.questions.length - 1)
-                                               this.$emit("gameover");
-                                       else
-                                               assessment.index++;
-                                       this.$forceUpdate(); //TODO: shouldn't be required
+                                       let gotoNext = () => {
+                                               if (assessment.index == assessment.questions.length - 1)
+                                                       this.$emit("gameover");
+                                               else
+                                                       assessment.index++;
+                                               this.$forceUpdate(); //TODO: shouldn't be required
+                                       };
                                        if (assessment.mode == "open")
-                                               return; //only local
+                                               return gotoNext(); //only local
                                        let answerData = {
                                                aid: assessment._id,
                                                answer: JSON.stringify({
@@ -220,7 +231,9 @@ new Vue({
                                                success: ret => {
                                                        if (!!ret.errmsg)
                                                                return this.$emit("warning", ret.errmsg);
-                                                       //socket.emit(message.newAnswer, answer);
+                                                       else
+                                                               gotoNext();
+                                                       socket.emit(message.newAnswer, answerData);
                                                },
                                        });
                                },
@@ -278,7 +291,8 @@ new Vue({
                                $("#leftButton, #rightButton").hide();
                                if (assessment.time > 0)
                                {
-                                       this.remainingTime = assessment.time * 60 - (!!paper ? paper.startTime/1000 : 0);
+                                       const deltaTime = !!paper ? Date.now() - paper.startTime : 0;
+                                       this.remainingTime = assessment.time * 60 - Math.round(deltaTime / 1000);
                                        this.runTimer();
                                }
                                // Initialize structured answer(s) based on questions type and nesting (TODO: more general)
@@ -296,18 +310,12 @@ new Vue({
                                {
                                        // Resuming
                                        let indices = paper.inputs.map( input => { return input.index; });
-                                       let remainingIndices = _.difference(_.range(assessment.questions.length), indices);
+                                       let remainingIndices = _.difference( _.range(assessment.questions.length).map(String), indices );
                                        assessment.indices = indices.concat( _.shuffle(remainingIndices) );
                                }
                                assessment.index = !!paper ? paper.inputs.length : 0;
+                               Vue.nextTick(libsRefresh);
                                this.stage = 2;
-                               Vue.nextTick( () => {
-                                       // Run Prism + MathJax on questions text
-                                       $("#statements").find("code[class^=language-]").each( (i,elem) => {
-                                               Prism.highlightElement(elem);
-                                       });
-                                       MathJax.Hub.Queue(["Typeset",MathJax.Hub,"statements"]);
-                               });
                        };
                        if (assessment.mode == "open")
                                return initializeStage2();
@@ -333,12 +341,10 @@ new Vue({
                                                // Got password: students answers locked to this page until potential teacher
                                                // action (power failure, computer down, ...)
                                        }
-                                       // TODO: password also exchanged by sockets to check identity
-                                       //socket = io.connect("/" + assessment.name, {
-                                       //      query: "number=" + this.student.number + "&password=" + this.password
-                                       //});
-                                       //socket.on(message.allAnswers, this.setAnswers);
-                                       //socket.on("disconnect", () => { }); //TODO: notify monitor (highlight red), redirect
+                                       socket = io.connect("/" + assessment.name, {
+                                               query: "number=" + this.student.number + "&password=" + this.password
+                                       });
+                                       socket.on(message.allAnswers, this.setAnswers);
                                        initializeStage2(s.questions, s.paper);
                                },
                        });
@@ -379,8 +385,8 @@ new Vue({
                                        assessment.conclusion = ret.conclusion;
                                        this.stage = 3;
                                        delete this.student["password"]; //unable to send new answers now
-                                       //socket.disconnect();
-                                       //socket = null;
+                                       socket.disconnect();
+                                       socket = null;
                                },
                        });
                },