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

index db77ee2cf8687d9f4eefad77a73d4de61e35751d..4a3586634a32daf7ba87989fd6c31faae7deb768 100644 (file)
@@ -2,81 +2,83 @@ const $ = require("cash-dom");
 
 function QuizScore(quiz) {
     this.quiz = quiz;
-    this.logQuestions=[];
-    this.score=0;
-    this.questionStatus=[];
+    this.logQuestions = [];
+    this.score = 0;
+    this.questionStatus = [];
+    this.answers = [];
+    this.lastAnsweredQuestion = -1;
     this.init();
 }
 
 QuizScore.prototype = {
-
     init: function () {
 
     },
 
-    updateScore: function () {
-        this.score = 0;
-        var answers = [];
-        var qn = 1;
-        this.logQuestions = [];
+    setAnswer: function (questionIndex, answers) {
+        this.lastAnsweredQuestion = questionIndex;
+        this.answers[questionIndex] = answers;
+        this.updateScore();
+        return this.questionStatus
+    },
+
+    checkQuestion: function (questionIndex) {
+        let question = this.quiz.questions[questionIndex];
 
-        let $this=this;
-
-        $('.question').each(function () {
-            var a = [];
-            var ok = true;
-            var count = $(this).attr('data-count') == '1';
-            var log = {
-                'count': count,
-            };
-
-            if ($(this).data('type') === 'multiple') {
-                var min_score = parseInt($(this).data('min-score'));
-                if (min_score === 0) {
-                    min_score = $(this).find('.answer[data-correct="1"]').length;
-                }
-                var this_score = 0;
-                $(this).find('.answer').each(function () {
-                    if ($(this).hasClass('active')) {
-                        var correct = $(this).attr('data-correct') == '1';
-                        this_score += correct ? 1 : -1;
-                        a.push($(this).data('a'))
+        var a = [];
+        var ok = true;
+        var count = $(this).attr('data-count') == '1';
+        var log = {
+            'count': count,
+        };
+
+        if (question.multiple) {
+            // Le score minimal est le score que l'utilisateur doit atteindre sur une question pour que sa réponse soit considérée
+            var min_score = parseInt(question.min_score);
+            if (min_score === 0) {
+                min_score = 0;
+                $.each(question.answers, function (answerIndex, answer) {
+                    if (answer.correct) {
+                        min_score += answer.score;
                     }
                 });
-                ok = this_score >= min_score;
-
-                answers.push(a);
-                log.answer = a;
-            } else {
-                log.answer = $(this).find('input,textarea,select').val();
             }
+            var this_score = 0;
 
-            if (!count) {
-                ok = null;
-            }
+            ok = this_score >= min_score;
 
-            $this.questionStatus[qn] = ok;
+            log.answer = a;
+        } else {
+            log.answer = $(this).find('input,textarea,select').val();
+        }
 
-            var b = $("#correction .correction[data-i='" + qn + "'] .badge");
-            $(b).removeClass('correct').removeClass('incorrect');
-            if (ok === true) {
-                $(b).addClass('correct');
-            } else if (ok === false) {
-                $(b).addClass('incorrect');
-            }
-            if (ok && count) {
-                $this.score++;
-                log.score = 1;
-            } else {
-                log.score = 0;
-            }
-            $this.logQuestions[qn] = log;
-            qn++;
-        });
+        if (!count) {
+            ok = null;
+        }
+
+        this.questionStatus[questionIndex] = ok;
+
+        if (ok && count) {
+            this.score++;
+            log.score = 1;
+        } else {
+            log.score = 0;
+        }
+        this.logQuestions[questionIndex] = log;
+    },
 
+    updateScore: function () {
+        this.score = 0;
+        this.logQuestions = [];
+
+        let $this = this;
+
+        $.each(this.quiz.questions, function (qn, question) {
+            $this.checkQuestion(qn);
+        });
 
         var state = {
-            q: $(".answer.active").last().closest('.question').data('q') + 1, a: answers
+            q: this.lastAnsweredQuestion + 1, a: this.answers
         };
 
         if (cubeSCORM.SCORM) {