X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fassessment.js;h=501984010af4f564d9513364b863a5ddf9472ace;hb=435371c7ba4b60790953115b9ebed68a047bb0a3;hp=5fe24cd24946414f20842e9e911914d0577b20ef;hpb=f03a2ad9e0b2fa36051def18d4c19c2f293cac1d;p=qomet.git diff --git a/public/javascripts/assessment.js b/public/javascripts/assessment.js index 5fe24cd..5019840 100644 --- a/public/javascripts/assessment.js +++ b/public/javascripts/assessment.js @@ -1,6 +1,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 socket = null; //monitor answers in real time if (assessment.mode == "secure" && !checkWindowSize()) @@ -30,203 +27,6 @@ new Vue({ remainingTime: 0, //global, in seconds warnMsg: "", }, - components: { - "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) { - let self = this; - let questions = (assessment.questions || [ ]).map( (q,i) => { - let questionContent = [ ]; - questionContent.push( - h( - "div", - { - "class": { - wording: true, - }, - domProps: { - innerHTML: q.wording, - }, - } - ) - ); - let optionsOrder = _.range(q.options.length); - if (!q.fixed) - optionsOrder = _.shuffle(optionsOrder); - let optionList = [ ]; - optionsOrder.forEach( idx => { - let option = [ ]; - option.push( - h( - "input", - { - domProps: { - checked: this.inputs.length > 0 && this.inputs[i][idx], - }, - attrs: { - id: this.inputId(i,idx), - type: "checkbox", - }, - on: { - change: e => { this.inputs[i][idx] = e.target.checked; }, - }, - }, - ) - ); - option.push( - h( - "label", - { - domProps: { - innerHTML: q.options[idx], - }, - attrs: { - "for": this.inputId(i,idx), - }, - } - ) - ); - optionList.push( - h( - "div", - { - "class": { - option: true, - choiceCorrect: this.stage == 4 && assessment.questions[i].answer.includes(idx), - choiceWrong: this.stage == 4 && this.inputs[i][idx] && !assessment.questions[i].answer.includes(idx), - }, - }, - option - ) - ); - }); - questionContent.push( - h( - "div", - { - "class": { - optionList: true, - }, - }, - optionList - ) - ); - return h( - "div", - { - "class": { - "question": true, - "hide": this.stage == 2 && assessment.display == 'one' && assessment.indices[assessment.index] != i, - }, - }, - questionContent - ); - }); - if (this.stage == 2) - { - questions.unshift( - h( - "button", - { - "class": { - "waves-effect": true, - "waves-light": true, - "btn": true, - }, - style: { - "display": "block", - "margin-left": "auto", - "margin-right": "auto", - }, - on: { - click: () => this.sendAnswer(assessment.indices[assessment.index]), - }, - }, - "Send" - ) - ); - } - return h( - "div", - { - attrs: { - id: "statements", - }, - }, - 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: { - inputId: function(i,j) { - return "q" + i + "_" + "input" + j; - }, - trySendCurrentAnswer: function() { - if (this.stage == 2) - this.sendAnswer(assessment.indices[assessment.index]); - }, - // 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 - if (assessment.mode == "open") - return; //only local - let answerData = { - aid: assessment._id, - answer: JSON.stringify({ - index:realIndex.toString(), - input:this.inputs[realIndex] - .map( (tf,i) => { return {val:tf,idx:i}; } ) - .filter( item => { return item.val; }) - .map( item => { return item.idx; }) - }), - number: this.student.number, - password: this.student.password, - }; - $.ajax("/send/answer", { - method: "GET", - data: answerData, - dataType: "json", - success: ret => { - if (!!ret.errmsg) - return this.$emit("warning", ret.errmsg); - //socket.emit(message.newAnswer, answer); - }, - }); - }, - }, - }, - }, computed: { countdown: function() { let seconds = this.remainingTime % 60; @@ -236,6 +36,31 @@ new Vue({ }, mounted: function() { $(".modal").modal(); + if (assessment.mode != "secure") + 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"; + }, false); + window.addEventListener("resize", e => { + this.trySendCurrentAnswer(); + document.location.href= "/fullscreen"; + }, false); + }, + trySendCurrentAnswer: function() { + if (this.stage == 2) + this.sendAnswer(assessment.indices[assessment.index]); + }, }, methods: { // In case of AJAX errors @@ -278,7 +103,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 +122,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 +153,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); }, }); @@ -355,6 +173,45 @@ new Vue({ clearInterval(this); }, 1000); }, + // 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) { + 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 gotoNext(); //only local + let answerData = { + aid: assessment._id, + answer: JSON.stringify({ + index:realIndex.toString(), + input:this.inputs[realIndex] + .map( (tf,i) => { return {val:tf,idx:i}; } ) + .filter( item => { return item.val; }) + .map( item => { return item.idx; }) + }), + number: this.student.number, + password: this.student.password, + }; + $.ajax("/send/answer", { + method: "GET", + data: answerData, + dataType: "json", + success: ret => { + if (!!ret.errmsg) + return this.$emit("warning", ret.errmsg); + else + gotoNext(); + socket.emit(message.newAnswer, answerData); + }, + }); + }, // stage 2 --> 3 (or 4) // from a message by statements component, or time over endAssessment: function() { @@ -379,8 +236,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; }, }); },