]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6184 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 28 Jul 2023 17:46:58 +0000 (19:46 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 28 Jul 2023 17:46:58 +0000 (19:46 +0200)
resources/quizv2/js/quiz.score.js

index b6ca34d93fcbc7c11ee590ab2a2fb8d9b81c9a72..ef2222d25cbabca51adfd86cff35817261d69ee1 100644 (file)
@@ -5,6 +5,7 @@ function QuizScore(quiz) {
     this.logQuestions = [];
     // Le score global du quiz
     this.score = 0;
+    this.maxScore = 0;
     this.questionStatus = [];
     this.answers = [];
     this.lastAnsweredQuestion = -1;
@@ -16,34 +17,48 @@ QuizScore.prototype = {
 
     },
 
+    /**
+     *
+     * @param questionIndex integer
+     * @param answers array
+     * @returns {{ok, status: []}}
+     */
     setAnswer: function (questionIndex, answers) {
         this.lastAnsweredQuestion = questionIndex;
         this.answers[questionIndex] = answers;
         this.updateScore();
-        return {ok: this.questionStatus[questionIndex].ok, status: this.questionStatus};
+        return {
+            ok: this.questionStatus[questionIndex].ok,
+            score: this.score,
+            maxScore: this.maxScore,
+            status: this.questionStatus
+        };
     },
 
     checkQuestion: function (questionIndex) {
-        let question = this.quiz.questions[questionIndex];
+        let question = this.quiz.data.questions[questionIndex];
 
         let a = [];
         let ok = true;
-        let count = $(this).attr('data-count') == '1';
+        let count = question.count_for_score;
         let log = {
             'count': count,
         };
 
         let answersStatus = [];
         let userAnswers = this.answers[questionIndex];
+        let min_score = 0;
+        let this_score = 0;
         if (userAnswers === undefined || userAnswers === null) {
             ok = null;
         } else {
             let $this = this;
             if (question.type === 'multiple') {
                 // Le score de la question
-                let min_score = this.getMinScore(question);
-                let this_score = 0;
-                for (const answerIndex in question.answers) {
+                min_score = this.getMinScore(question);
+                this_score = 0;
+                for (let answerIndex in question.answers) {
+                    answerIndex = parseInt(answerIndex);
                     const answer = question.answers[answerIndex];
                     let checkedByUser = userAnswers.indexOf(answerIndex) >= 0;
                     if (answer.correct === 1) {
@@ -52,7 +67,7 @@ QuizScore.prototype = {
                             this_score += answer.score;
                             answersStatus[answerIndex] = 'ok';
                         } else {
-                            // C'est une bonne réponse mais elle n'a pas été cochée par l'utilisateur
+                            // C'est une bonne réponse, mais elle n'a pas été cochée par l'utilisateur
                             answersStatus[answerIndex] = 'missed';
                         }
                     } else {
@@ -67,24 +82,29 @@ QuizScore.prototype = {
                         }
                     }
                 }
-
                 ok = this_score >= min_score;
 
                 log.answer = a;
             } else {
                 log.answer = $(this).find('input,textarea,select').val();
             }
-
-            if (!count) {
-                ok = null;
-            }
         }
 
-        this.questionStatus[questionIndex] = {'status': ok, 'answers': answersStatus};
+        this.questionStatus[questionIndex] = {
+            ok: ok,
+            answers: answersStatus,
+            // dbg_min_score: min_score,
+            // dbg_this_score: this_score
+        };
 
-        if (ok && count) {
-            this.score++;
-            log.score = 1;
+        if (count) {
+            this.maxScore++;
+            if (ok) {
+                this.score++;
+                log.score = 1;
+            } else {
+                log.score = 0;
+            }
         } else {
             log.score = 0;
         }
@@ -97,8 +117,8 @@ QuizScore.prototype = {
         if (min_score === 0) {
             min_score = 0;
             for (const answerIndex in question.answers) {
-                let answer = question.answers[answerIndex];
-                if (answer.correct) {
+                const answer = question.answers[answerIndex];
+                if (answer.correct === 1) {
                     min_score += answer.score;
                 }
             }
@@ -107,10 +127,11 @@ QuizScore.prototype = {
     },
 
     updateScore: function () {
+        this.maxScore = 0;
         this.score = 0;
         this.logQuestions = [];
 
-        for (const qn in this.quiz.questions) {
+        for (const qn in this.quiz.data.questions) {
             this.checkQuestion(qn);
         }