Fix exam mode
[qomet.git] / public / javascripts / assessment.js
index 1db80a0..1d62894 100644 (file)
@@ -1,35 +1,21 @@
+// TODO: if display == "all", les envois devraient être non définitifs (possibilité de corriger)
+// Et, blur sur une (sous-)question devrait envoyer la version courante de la sous-question
+
 let socket = null; //monitor answers in real time
 
-function checkWindowSize()
-{
-       if (assessment.mode == "secure")
-       {
-               // NOTE: temporarily accept smartphone (security hole: pretend being a smartphone on desktop browser...)
-               if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/))
-                       return true;
-               let test = () => {
-                       return window.innerWidth < screen.width || window.innerHeight < screen.height;
-               };
-               const returnVal = test;
-               while (!test)
-                       alert("Please enter fullscreen mode (F11)");
-               return returnVal;
-       }
-       return true;
-};
+if (assessment.mode == "secure" && !checkWindowSize())
+       document.location.href= "/fullscreen";
 
-function libsRefresh()
+function checkWindowSize()
 {
-       $("#statements").find("code[class^=language-]").each( (i,elem) => {
-               Prism.highlightElement(elem);
-       });
-       MathJax.Hub.Queue(["Typeset",MathJax.Hub,"statements"]);
+       // NOTE: temporarily accept smartphone (security hole: pretend being a smartphone on desktop browser...)
+       if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/))
+               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;
 };
 
-// TODO: if display == "all", les envois devraient être non définitifs (possibilité de corriger)
-// Et, blur sur une (sous-)question devrait envoyer la version courante de la sous-question
-
-let V = new Vue({
+new Vue({
        el: "#assessment",
        data: {
                assessment: assessment,
@@ -42,37 +28,11 @@ let V = new Vue({
                //       4: show answers
                stage: assessment.mode != "open" ? 0 : 1,
                remainingTime: 0, //global, in seconds
+               warnMsg: "",
        },
        components: {
                "statements": {
                        props: ['assessment','inputs','student','stage'],
-                       data: function() {
-                               return {
-                                       index: 0, //current question index in assessment.indices
-                               };
-                       },
-                       mounted: function() {
-                               if (assessment.mode != "secure")
-                                       return;
-                               $("#warning").modal({
-                                       complete: () => {
-                                               this.stage = 2;
-                                               this.resumeAssessment();
-                                       },
-                               });
-                               window.addEventListener("blur", () => {
-                                       if (this.stage == 2)
-                                               this.showWarning();
-                               }, false);
-                               window.addEventListener("resize", e => {
-                                       if (this.stage == 2 && !checkWindowSize())
-                                               this.showWarning();
-                               }, false);
-                               //socket.on("disconnect", () => { }); //TODO: notify monitor (highlight red)
-                       },
-                       updated: function() {
-                               libsRefresh();
-                       },
                        // 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)
@@ -80,14 +40,14 @@ let V = new Vue({
                        // Full questions tree is rendered, but some parts hidden depending on display settings
                        render(h) {
                                let self = this;
-                               let questions = assessment.questions.map( (q,i) => {
+                               let questions = (assessment.questions || [ ]).map( (q,i) => {
                                        let questionContent = [ ];
                                        questionContent.push(
                                                h(
                                                        "div",
                                                        {
                                                                "class": {
-                                                                       "wording": true,
+                                                                       wording: true,
                                                                },
                                                                domProps: {
                                                                        innerHTML: q.wording,
@@ -106,7 +66,7 @@ let V = new Vue({
                                                                "input",
                                                                {
                                                                        domProps: {
-                                                                               checked: this.inputs[i][idx],
+                                                                               checked: this.inputs.length > 0 && this.inputs[i][idx],
                                                                        },
                                                                        attrs: {
                                                                                id: this.inputId(i,idx),
@@ -161,7 +121,7 @@ let V = new Vue({
                                                {
                                                        "class": {
                                                                "question": true,
-                                                               "hide": this.stage == 2 && assessment.display == 'one' && assessment.indices[this.index] != i,
+                                                               "hide": this.stage == 2 && assessment.display == 'one' && assessment.indices[assessment.index] != i,
                                                        },
                                                },
                                                questionContent
@@ -169,7 +129,6 @@ let V = new Vue({
                                });
                                if (this.stage == 2)
                                {
-                                       // TODO: one button per question
                                        questions.unshift(
                                                h(
                                                        "button",
@@ -179,8 +138,13 @@ let V = new Vue({
                                                                        "waves-light": true,
                                                                        "btn": true,
                                                                },
+                                                               style: {
+                                                                       "display": "block",
+                                                                       "margin-left": "auto",
+                                                                       "margin-right": "auto",
+                                                               },
                                                                on: {
-                                                                       click: () => this.sendAnswer(assessment.indices[this.index]),
+                                                                       click: () => this.sendAnswer(assessment.indices[assessment.index]),
                                                                },
                                                        },
                                                        "Send"
@@ -197,24 +161,47 @@ let V = new Vue({
                                        questions
                                );
                        },
+                       mounted: function() {
+                               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.
+                                       // Workaround: disable key at higher level. Possible xbindkey config:
+                                       // "false"
+                                       //   m:0x10 + c:95
+                                       //   Mod2 + F11
+                                       if ([122,123].includes(e.keyCode))
+                                               e.preventDefault();
+                               }, false);
+                               window.addEventListener("blur", () => {
+                                       this.trySendCurrentAnswer();
+                                       document.location.href= "/noblur";
+                               }, false);
+                               window.addEventListener("resize", e => {
+                                       this.trySendCurrentAnswer();
+                                       document.location.href= "/fullscreen";
+                               }, false);
+                       },
                        methods: {
-                               // HELPERS:
                                inputId: function(i,j) {
                                        return "q" + i + "_" + "input" + j;
                                },
-                               showWarning: function(action) {
-                                       this.sendAnswer(assessment.indices[this.index]);
-                                       this.stage = 32; //fictive stage to hide all elements
-                                       $("#warning").modal('open');
+                               trySendCurrentAnswer: function() {
+                                       if (this.stage == 2)
+                                               this.sendAnswer(assessment.indices[assessment.index]);
                                },
                                // stage 2
                                sendAnswer: function(realIndex) {
-                                       if (this.index == assessment.questions.length - 1)
-                                               this.$emit("gameover");
-                                       else
-                                               this.index++;
+                                       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({
@@ -233,48 +220,16 @@ let V = new Vue({
                                                dataType: "json",
                                                success: ret => {
                                                        if (!!ret.errmsg)
-                                                               return alert(ret.errmsg);
+                                                               return this.$emit("warning", ret.errmsg);
+                                                       else
+                                                               gotoNext();
                                                        //socket.emit(message.newAnswer, answer);
                                                },
                                        });
                                },
-                               // stage 2 after blur or resize
-                               resumeAssessment: function() {
-                                       checkWindowSize();
-                               },
                        },
                },
        },
-       mounted: function() {
-               window.addEventListener("keydown", e => {
-                       // If F12 or ctrl+shift (ways to access devtools)
-                       if (e.keyCode == 123 || (e.ctrlKey && e.shiftKey))
-                               e.preventDefault();
-               }, false);
-               // Devtools detect based on https://jsfiddle.net/ebhjxfwv/4/
-               let div = document.createElement('div');
-               let devtoolsLoop = setInterval(
-                       () => {
-                               if (assessment.mode != "open")
-                               {
-                                       console.log(div);
-                                       console.clear();
-                               }
-                       },
-                       1000
-               );
-               Object.defineProperty(div, "id", {
-                       get: () => {
-                               clearInterval(devtoolsLoop);
-                               if (assessment.mode != "open")
-                               {
-                                       if (this.stage == 2)
-                                               this.endAssessment();
-                                       document.location.href = "/nodevtools";
-                               }
-                       }
-               });
-       },
        computed: {
                countdown: function() {
                        let seconds = this.remainingTime % 60;
@@ -282,8 +237,15 @@ let V = new Vue({
                        return this.padWithZero(minutes) + ":" + this.padWithZero(seconds);
                },
        },
+       mounted: function() {
+               $(".modal").modal();
+       },
        methods: {
-               // HELPERS:
+               // In case of AJAX errors
+               warning: function(message) {
+                       this.warnMsg = message;
+                       $("#warning").modal("open");
+               },
                padWithZero: function(x) {
                        if (x < 10)
                                return "0" + x;
@@ -300,7 +262,7 @@ let V = new Vue({
                                dataType: "json",
                                success: s => {
                                        if (!!s.errmsg)
-                                               return alert(s.errmsg);
+                                               return this.warning(s.errmsg);
                                        this.stage = 1;
                                        this.student = s.student;
                                        Vue.nextTick( () => { Materialize.updateTextFields(); });
@@ -315,12 +277,11 @@ let V = new Vue({
                },
                // stage 1 --> 2 (get all questions, set password)
                startAssessment: function() {
-                       checkWindowSize();
-                       let initializeStage2 = questions => {
+                       let initializeStage2 = (questions,paper) => {
                                $("#leftButton, #rightButton").hide();
                                if (assessment.time > 0)
                                {
-                                       this.remainingTime = assessment.time * 60;
+                                       this.remainingTime = assessment.time * 60 - (!!paper ? paper.startTime/1000 : 0);
                                        this.runTimer();
                                }
                                // Initialize structured answer(s) based on questions type and nesting (TODO: more general)
@@ -328,11 +289,28 @@ let V = new Vue({
                                        assessment.questions = questions;
                                for (let q of assessment.questions)
                                        this.inputs.push( _(q.options.length).times( _.constant(false) ) );
-                               assessment.indices = assessment.fixed
-                                       ? _.range(assessment.questions.length)
-                                       : _.shuffle( _.range(assessment.questions.length) );
+                               if (!paper)
+                               {
+                                       assessment.indices = assessment.fixed
+                                               ? _.range(assessment.questions.length)
+                                               : _.shuffle( _.range(assessment.questions.length) );
+                               }
+                               else
+                               {
+                                       // Resuming
+                                       let indices = paper.inputs.map( input => { return input.index; });
+                                       let remainingIndices = _.difference(_.range(assessment.questions.length), indices);
+                                       assessment.indices = indices.concat( _.shuffle(remainingIndices) );
+                               }
+                               assessment.index = !!paper ? paper.inputs.length : 0;
                                this.stage = 2;
-                               Vue.nextTick( () => { libsRefresh(); });
+                               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();
@@ -345,16 +323,26 @@ let V = new Vue({
                                dataType: "json",
                                success: s => {
                                        if (!!s.errmsg)
-                                               return alert(s.errmsg);
-                                       this.student.password = s.password;
-                                       // Got password: students answers locked to this page until potential teacher
-                                       // action (power failure, computer down, ...)
+                                               return this.warning(s.errmsg);
+                                       if (!!s.paper)
+                                       {
+                                               // Resuming: receive stored answers + startTime
+                                               this.student.password = s.paper.password;
+                                               this.inputs = s.paper.inputs.map( inp => { return inp.input; });
+                                       }
+                                       else
+                                       {
+                                               this.student.password = s.password;
+                                               // 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);
-                                       initializeStage2(s.questions);
+                                       //socket.on("disconnect", () => { }); //TODO: notify monitor (highlight red), redirect
+                                       initializeStage2(s.questions, s.paper);
                                },
                        });
                },
@@ -370,40 +358,34 @@ let V = new Vue({
                                        clearInterval(this);
                        }, 1000);
                },
-               // stage 2 after disconnect (socket)
-               resumeAssessment: function() {
-                       // UNIMPLEMENTED
-                       // TODO: get stored answers (papers[number cookie]), inject (inputs), set index+indices
-               },
                // stage 2 --> 3 (or 4)
-               // from a message by statements component
+               // from a message by statements component, or time over
                endAssessment: function() {
-                       // If time over or cheating: set endTime, destroy password
+                       // Set endTime, destroy password
                        $("#leftButton, #rightButton").show();
-                       //this.sendAnswer(...); //TODO: for each non-answered (and non-empty!) index (yet)
-                       if (assessment.mode != "open")
+                       if (assessment.mode == "open")
                        {
-                               $.ajax("/end/assessment", {
-                                       method: "GET",
-                                       data: {
-                                               aid: assessment._id,
-                                               number: this.student.number,
-                                               password: this.student.password,
-                                       },
-                                       dataType: "json",
-                                       success: ret => {
-                                               if (!!ret.errmsg)
-                                                       return alert(ret.errmsg);
-                                               assessment.conclusion = ret.conclusion;
-                                               this.stage = 3;
-                                               delete this.student["password"]; //unable to send new answers now
-                                               //socket.disconnect();
-                                               //socket = null;
-                                       },
-                               });
-                       }
-                       else
                                this.stage = 4;
+                               return;
+                       }
+                       $.ajax("/end/assessment", {
+                               method: "GET",
+                               data: {
+                                       aid: assessment._id,
+                                       number: this.student.number,
+                                       password: this.student.password,
+                               },
+                               dataType: "json",
+                               success: ret => {
+                                       if (!!ret.errmsg)
+                                               return this.warning(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(answers) {