'update'
[qomet.git] / public / javascripts / assessment.js
index 5019840..93200cc 100644 (file)
@@ -10,14 +10,14 @@ 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;
-};
+}
 
 new Vue({
        el: "#assessment",
        data: {
                assessment: assessment,
-               inputs: [ ], //student's answers
-               student: { }, //filled later
+               answers: { }, //filled later with answering parameters
+               student: { }, //filled later (name, password)
                // Stage 0: unauthenticated (number),
                //       1: authenticated (got a name, unvalidated)
                //       2: locked: password set, exam started
@@ -33,38 +33,53 @@ new Vue({
                        let minutes = Math.floor(this.remainingTime / 60);
                        return this.padWithZero(minutes) + ":" + this.padWithZero(seconds);
                },
+               showAnswers: function() {
+                       return this.stage == 4;
+               },
        },
        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);
-       },
-               trySendCurrentAnswer: function() {
-                       if (this.stage == 2)
-                               this.sendAnswer(assessment.indices[assessment.index]);
-               },
        },
        methods: {
                // In case of AJAX errors
-               warning: function(message) {
+               showWarning: function(message) {
                        this.warnMsg = message;
                        $("#warning").modal("open");
                },
@@ -73,6 +88,10 @@ new Vue({
                                return "0" + x;
                        return x;
                },
+               trySendCurrentAnswer: function() {
+                       if (this.stage == 2)
+                               this.sendAnswer();
+               },
                // stage 0 --> 1
                getStudent: function(cb) {
                        $.ajax("/get/student", {
@@ -84,7 +103,7 @@ new Vue({
                                dataType: "json",
                                success: s => {
                                        if (!!s.errmsg)
-                                               return this.warning(s.errmsg);
+                                               return this.showWarning(s.errmsg);
                                        this.stage = 1;
                                        this.student = s.student;
                                        Vue.nextTick( () => { Materialize.updateTextFields(); });
@@ -103,6 +122,9 @@ 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();
@@ -110,11 +132,12 @@ new Vue({
                                // Initialize structured answer(s) based on questions type and nesting (TODO: more general)
                                if (!!questions)
                                        assessment.questions = questions;
+                               this.answers.inputs = [ ];
                                for (let q of assessment.questions)
-                                       this.inputs.push( _(q.options.length).times( _.constant(false) ) );
+                                       this.answers.inputs.push( _(q.options.length).times( _.constant(false) ) );
                                if (!paper)
                                {
-                                       assessment.indices = assessment.fixed
+                                       this.answers.indices = assessment.fixed
                                                ? _.range(assessment.questions.length)
                                                : _.shuffle( _.range(assessment.questions.length) );
                                }
@@ -123,10 +146,11 @@ new Vue({
                                        // Resuming
                                        let indices = paper.inputs.map( input => { return input.index; });
                                        let remainingIndices = _.difference( _.range(assessment.questions.length).map(String), indices );
-                                       assessment.indices = indices.concat( _.shuffle(remainingIndices) );
+                                       this.answers.indices = indices.concat( _.shuffle(remainingIndices) );
                                }
-                               assessment.index = !!paper ? paper.inputs.length : 0;
-                               Vue.nextTick(libsRefresh);
+                               this.answers.index = !!paper ? paper.inputs.length : 0;
+                               this.answers.displayAll = assessment.display == "all";
+                               this.answers.showSolution = false;
                                this.stage = 2;
                        };
                        if (assessment.mode == "open")
@@ -140,12 +164,12 @@ new Vue({
                                dataType: "json",
                                success: s => {
                                        if (!!s.errmsg)
-                                               return this.warning(s.errmsg);
+                                               return this.showWarning(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; });
+                                               this.answers.inputs = s.paper.inputs.map( inp => { return inp.input; });
                                        }
                                        else
                                        {
@@ -153,45 +177,65 @@ new Vue({
                                                // Got password: students answers locked to this page until potential teacher
                                                // action (power failure, computer down, ...)
                                        }
-                                       socket = io.connect("/" + assessment.name, {
-                                               query: "number=" + this.student.number + "&password=" + this.password
+                                       socket = io.connect("/", {
+                                               query: "aid=" + assessment._id + "&number=" + this.student.number + "&password=" + this.student.password
                                        });
                                        socket.on(message.allAnswers, this.setAnswers);
                                        initializeStage2(s.questions, s.paper);
                                },
                        });
                },
+
+
                // stage 2
-               runTimer: function() {
+               runGlobalTimer: function() {
                        if (assessment.time <= 0)
                                return;
                        let self = this;
                        setInterval( function() {
                                self.remainingTime--;
-                               if (self.remainingTime <= 0 || self.stage >= 4)
-                                       self.endAssessment();
+                               if (self.remainingTime <= 0)
+                               {
+                                       if (self.stage == 2)
+                                               self.endAssessment();
+                                       clearInterval(this);
+                               }
+                       }, 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
-               // TODO: currentIndex ? click: () => this.sendAnswer(assessment.indices[assessment.index]),
-               // De même, cette condition sur le display d'une question doit remonter (résumée dans 'index' property) :
-               // à faire par ici : "hide": this.stage == 2 && assessment.display == 'one' && assessment.indices[assessment.index] != i,
-               sendAnswer: function(realIndex) {
+               sendOneAnswer: function() {
+                       const realIndex = this.answers.indices[this.answers.index];
                        let gotoNext = () => {
-                               if (assessment.index == assessment.questions.length - 1)
-                                       this.$emit("gameover");
+                               if (this.answers.index == assessment.questions.length - 1)
+                                       this.endAssessment();
                                else
-                                       assessment.index++;
-                               this.$forceUpdate(); //TODO: shouldn't be required
+                                       this.answers.index++;
+                               this.$children[0].$forceUpdate(); //TODO: bad HACK, and shouldn't be required...
                        };
                        if (assessment.mode == "open")
                                return gotoNext(); //only local
                        let answerData = {
                                aid: assessment._id,
                                answer: JSON.stringify({
-                                       index:realIndex.toString(),
-                                       input:this.inputs[realIndex]
+                                       index: realIndex.toString(),
+                                       input: this.answers.inputs[realIndex]
                                                .map( (tf,i) => { return {val:tf,idx:i}; } )
                                                .filter( item => { return item.val; })
                                                .map( item => { return item.idx; })
@@ -205,13 +249,19 @@ new Vue({
                                dataType: "json",
                                success: ret => {
                                        if (!!ret.errmsg)
-                                               return this.$emit("warning", ret.errmsg);
-                                       else
-                                               gotoNext();
+                                               return this.showWarning(ret.errmsg);
+                                       gotoNext();
                                        socket.emit(message.newAnswer, answerData);
                                },
                        });
                },
+               // 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() {
@@ -220,6 +270,8 @@ new Vue({
                        if (assessment.mode == "open")
                        {
                                this.stage = 4;
+                               this.answers.showSolution = true;
+                               this.answers.displayAll = true;
                                return;
                        }
                        $.ajax("/end/assessment", {
@@ -232,20 +284,22 @@ new Vue({
                                dataType: "json",
                                success: ret => {
                                        if (!!ret.errmsg)
-                                               return this.warning(ret.errmsg);
-                                       assessment.conclusion = ret.conclusion;
+                                               return this.showWarning(ret.errmsg);
                                        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) {
+               setAnswers: function(m) {
+                       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;
                },
        },
 });