From deca101d5a1bc1be0f964f4fdfcb5d27e17fdd28 Mon Sep 17 00:00:00 2001 From: nicoboy Date: Sat, 3 Jan 2026 16:10:16 +0100 Subject: [PATCH] erreur sencore pour le chat bot --- assets/js/chatbot.js | 46 +++++++++++++++++++++++++++++-------------- passer_evaluation.php | 3 +-- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/assets/js/chatbot.js b/assets/js/chatbot.js index 0903f17..8fdc49d 100644 --- a/assets/js/chatbot.js +++ b/assets/js/chatbot.js @@ -67,28 +67,44 @@ class ChatbotIA { // NOUVELLE MÉTHODE : Détecter la question visible detectCurrentQuestion() { - // Méthode 1 : Chercher l'élément de question visible/actif - const activeQuestion = document.querySelector('.question.active, .question-container.active, [data-question-active="true"]'); - if (activeQuestion && activeQuestion.dataset.questionId) { - return parseInt(activeQuestion.dataset.questionId); + // Trouver la question la plus visible dans le viewport + const questionCards = document.querySelectorAll('.question-card[data-question-id]'); + + if (questionCards.length === 0) { + console.error('❌ Aucune question-card trouvée'); + return null; } - // Méthode 2 : Chercher dans les inputs visibles - const visibleInputs = document.querySelectorAll('input[name^="reponse_"]:not([style*="display: none"])'); - if (visibleInputs.length > 0) { - const match = visibleInputs[0].name.match(/reponse_(\d+)/); - if (match) { - return parseInt(match[1]); + let closestQuestion = null; + let minDistance = Infinity; + const viewportCenter = window.innerHeight / 2; + + questionCards.forEach(card => { + const rect = card.getBoundingClientRect(); + + // La question est visible dans le viewport + if (rect.top < window.innerHeight && rect.bottom > 0) { + const cardCenter = rect.top + (rect.height / 2); + const distance = Math.abs(cardCenter - viewportCenter); + + if (distance < minDistance) { + minDistance = distance; + closestQuestion = card; + } } - } + }); - // Méthode 3 : Index de question actuelle (si ton système utilise un index) - if (typeof currentQuestionIndex !== 'undefined' && questionsIds[currentQuestionIndex]) { - return questionsIds[currentQuestionIndex]; + if (closestQuestion) { + const qId = parseInt(closestQuestion.dataset.questionId); + console.log('✅ Question visible détectée:', qId); + return qId; } // Fallback : première question - return questionsIds[0]; + const firstCard = questionCards[0]; + const qId = parseInt(firstCard.dataset.questionId); + console.log('⚠️ Fallback première question:', qId); + return qId; } // NOUVELLE MÉTHODE : Charger l'aide restante via AJAX diff --git a/passer_evaluation.php b/passer_evaluation.php index eb96c91..50638f7 100644 --- a/passer_evaluation.php +++ b/passer_evaluation.php @@ -625,8 +625,7 @@ $reponses_sauvegardees = json_decode($tentative['reponses_json'] ?? '{}', true) $q): ?> -
-
+
Question /
pts