From 5a043c7ce8efa309c4dac14fb8a768d73de4fd2e Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 28 Jul 2023 19:46:58 +0200 Subject: [PATCH] wip #6184 @0.5 --- resources/quizv2/js/quiz.score.js | 59 +++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/resources/quizv2/js/quiz.score.js b/resources/quizv2/js/quiz.score.js index b6ca34d93..ef2222d25 100644 --- a/resources/quizv2/js/quiz.score.js +++ b/resources/quizv2/js/quiz.score.js @@ -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); } -- 2.39.5