Update js/ui/quiz_ui.js
Browse files- js/ui/quiz_ui.js +70 -157
js/ui/quiz_ui.js
CHANGED
|
@@ -1,177 +1,90 @@
|
|
| 1 |
class QuizUI {
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
}
|
| 6 |
|
| 7 |
-
|
| 8 |
-
// مقداردهی اولیه سیستم احراز هویت
|
| 9 |
-
AuthMiddleware.initializeApp();
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
// افزودن event listener برای دکمه خروج
|
| 24 |
-
const logoutBtn = document.getElementById('logoutBtn');
|
| 25 |
-
if (logoutBtn) {
|
| 26 |
-
logoutBtn.addEventListener('click', () => {
|
| 27 |
-
authManager.logout();
|
| 28 |
-
this.updateAuthDisplay();
|
| 29 |
-
alert('با موفقیت خارج شدید.');
|
| 30 |
-
});
|
| 31 |
-
}
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
updateAuthDisplay() {
|
| 35 |
-
const authSection = document.getElementById('authSection');
|
| 36 |
-
const user = authManager.getCurrentUser();
|
| 37 |
-
|
| 38 |
-
if (authSection) {
|
| 39 |
-
if (user) {
|
| 40 |
-
authSection.innerHTML = `
|
| 41 |
-
<div class="user-info">
|
| 42 |
-
<span>خوش آمدید, ${user.profile.fullName || user.username}!</span>
|
| 43 |
-
<button id="logoutBtn" class="btn btn-secondary">خروج</button>
|
| 44 |
-
</div>
|
| 45 |
-
`;
|
| 46 |
-
|
| 47 |
-
// اضافه کردن event listener برای دکمه جدید
|
| 48 |
-
document.getElementById('logoutBtn')?.addEventListener('click', () => {
|
| 49 |
-
authManager.logout();
|
| 50 |
-
this.updateAuthDisplay();
|
| 51 |
-
alert('با موفقیت خارج شدید.');
|
| 52 |
-
});
|
| 53 |
-
} else {
|
| 54 |
-
authSection.innerHTML = `
|
| 55 |
-
<div class="auth-buttons">
|
| 56 |
-
<button onclick="quizUI.showLoginModal()" class="btn btn-primary">ورود</button>
|
| 57 |
-
<button onclick="quizUI.showRegisterModal()" class="btn btn-secondary">ثبت نام</button>
|
| 58 |
</div>
|
| 59 |
-
`;
|
| 60 |
-
}
|
| 61 |
-
}
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
showLoginModal() {
|
| 65 |
-
// نمایش مودال ورود
|
| 66 |
-
const modalHTML = `
|
| 67 |
-
<div class="modal">
|
| 68 |
-
<div class="modal-content">
|
| 69 |
-
<h3>ورود به سیستم</h3>
|
| 70 |
-
<form id="loginForm">
|
| 71 |
-
<input type="text" name="username" placeholder="نام کاربری" required>
|
| 72 |
-
<input type="password" name="password" placeholder="رمز عبور" required>
|
| 73 |
-
<button type="submit">ورود</button>
|
| 74 |
-
</form>
|
| 75 |
-
<button onclick="this.closest('.modal').remove()">بستن</button>
|
| 76 |
</div>
|
| 77 |
-
|
| 78 |
-
`;
|
| 79 |
-
|
| 80 |
-
document.body.insertAdjacentHTML('beforeend', modalHTML);
|
| 81 |
-
|
| 82 |
-
document.getElementById('loginForm').addEventListener('submit', (e) => {
|
| 83 |
-
e.preventDefault();
|
| 84 |
-
this.handleLogin(new FormData(e.target));
|
| 85 |
});
|
| 86 |
-
}
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
const validation = ValidationMiddleware.validateLoginData(data);
|
| 95 |
-
if (!validation.isValid) {
|
| 96 |
-
alert(validation.errors.join('\n'));
|
| 97 |
-
return;
|
| 98 |
-
}
|
| 99 |
|
| 100 |
-
|
| 101 |
-
if (result.success) {
|
| 102 |
-
document.querySelector('.modal').remove();
|
| 103 |
-
this.updateAuthDisplay();
|
| 104 |
-
this.updateScoreDisplay();
|
| 105 |
-
alert('با موفقیت وارد شدید!');
|
| 106 |
-
} else {
|
| 107 |
-
alert(result.message);
|
| 108 |
-
}
|
| 109 |
}
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
return requireAuth(() => {
|
| 114 |
-
const
|
| 115 |
-
const userAnswers =
|
| 116 |
-
|
| 117 |
-
quizData.questions.forEach(question => {
|
| 118 |
-
const selectedOption = document.querySelector(
|
| 119 |
-
`input[name="question_${question.id}"]:checked`
|
| 120 |
-
);
|
| 121 |
-
userAnswers.push(selectedOption ? parseInt(selectedOption.value) : -1);
|
| 122 |
-
});
|
| 123 |
-
|
| 124 |
-
const result = this.learningLogic.check_quiz_answers(this.currentDay, userAnswers);
|
| 125 |
-
this.showQuizFeedback(result);
|
| 126 |
-
|
| 127 |
-
// بهروزرسانی امتیاز کاربر
|
| 128 |
-
if (result.reward > 0) {
|
| 129 |
-
userManager.updateUserProgress(result.reward, `quiz_day${this.currentDay}`);
|
| 130 |
-
}
|
| 131 |
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
})();
|
| 134 |
}
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
}
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
|
| 163 |
-
const scoreDisplay = document.getElementById('userScore');
|
| 164 |
-
const user = authManager.getCurrentUser();
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
} else if (scoreDisplay) {
|
| 169 |
-
scoreDisplay.textContent = '0 امتیاز - لطفاً وارد شوید';
|
| 170 |
-
}
|
| 171 |
}
|
| 172 |
-
|
| 173 |
-
// بقیه متدها مانند قبل...
|
| 174 |
-
}
|
| 175 |
-
|
| 176 |
-
// راهاندازی برنامه
|
| 177 |
-
const quizUI = new QuizUI();
|
|
|
|
| 1 |
class QuizUI {
|
| 2 |
+
static render(quizData) {
|
| 3 |
+
const quizForm = document.getElementById('quizForm');
|
| 4 |
+
if (!quizForm || !quizData) return;
|
|
|
|
| 5 |
|
| 6 |
+
let html = '<div class="quiz-questions">';
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
quizData.questions.forEach((question, index) => {
|
| 9 |
+
html += `
|
| 10 |
+
<div class="quiz-question" data-question-id="${question.id}">
|
| 11 |
+
<h4>${index + 1}. ${question.question}</h4>
|
| 12 |
+
<div class="options-container">
|
| 13 |
+
${question.options.map((option, optIndex) => `
|
| 14 |
+
<label class="option-label">
|
| 15 |
+
<input type="radio" name="question_${question.id}" value="${optIndex}">
|
| 16 |
+
<span class="option-text">${option}</span>
|
| 17 |
+
</label>
|
| 18 |
+
`).join('')}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
</div>
|
| 21 |
+
`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
});
|
|
|
|
| 23 |
|
| 24 |
+
html += `</div>
|
| 25 |
+
<button type="button" class="btn btn-primary" onclick="QuizUI.submitQuiz()">
|
| 26 |
+
ارسال پاسخها
|
| 27 |
+
</button>
|
| 28 |
+
`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
quizForm.innerHTML = html;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
+
static async submitQuiz() {
|
| 34 |
+
// استفاده از سیستم احراز هویت اصلی
|
| 35 |
return requireAuth(() => {
|
| 36 |
+
const currentDay = window.app?.currentDay || 1;
|
| 37 |
+
const userAnswers = this.collectAnswers();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
learningLogic.checkQuizAnswers(currentDay, userAnswers)
|
| 40 |
+
.then(result => {
|
| 41 |
+
this.showQuizResult(result);
|
| 42 |
+
})
|
| 43 |
+
.catch(error => {
|
| 44 |
+
console.error('Quiz submission error:', error);
|
| 45 |
+
Utils.showNotification('خطا در ارسال آزمون', 'error');
|
| 46 |
+
});
|
| 47 |
})();
|
| 48 |
}
|
| 49 |
|
| 50 |
+
static collectAnswers() {
|
| 51 |
+
// جمعآوری پاسخها از فرم
|
| 52 |
+
const answers = [];
|
| 53 |
+
const questions = document.querySelectorAll('.quiz-question');
|
| 54 |
+
|
| 55 |
+
questions.forEach(question => {
|
| 56 |
+
const selected = question.querySelector('input[type="radio"]:checked');
|
| 57 |
+
answers.push(selected ? parseInt(selected.value) : -1);
|
| 58 |
+
});
|
| 59 |
+
|
| 60 |
+
return answers;
|
| 61 |
+
}
|
| 62 |
|
| 63 |
+
static showQuizResult(result) {
|
| 64 |
+
// نمایش نتایج آزمون
|
| 65 |
+
const feedbackContainer = document.getElementById('quizFeedback');
|
| 66 |
+
if (!feedbackContainer) return;
|
|
|
|
| 67 |
|
| 68 |
+
let html = `
|
| 69 |
+
<div class="quiz-result ${result.correctCount === result.totalQuestions ? 'perfect' : 'good'}">
|
| 70 |
+
<div class="result-header">
|
| 71 |
+
<h4>${result.correctCount === result.totalQuestions ? '🎉 عالی!' : '👍 خوب بود!'}</h4>
|
| 72 |
+
<div class="score-badge">${result.correctCount}/${result.totalQuestions}</div>
|
| 73 |
+
</div>
|
| 74 |
+
<p>${result.feedback.message}</p>
|
| 75 |
+
<p class="reward-info">🎁 ${result.reward} امتیاز دریافت کردید</p>
|
| 76 |
+
|
| 77 |
+
${result.correctCount < result.totalQuestions ? `
|
| 78 |
+
<button class="btn btn-outline" onclick="QuizUI.showExplanations()">
|
| 79 |
+
📖 مشاهده توضیحات
|
| 80 |
+
</button>
|
| 81 |
+
` : ''}
|
| 82 |
+
</div>
|
| 83 |
+
`;
|
| 84 |
|
| 85 |
+
feedbackContainer.innerHTML = html;
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
// ارسال event برای بهروزرسانی UI
|
| 88 |
+
document.dispatchEvent(new CustomEvent('progressUpdated'));
|
|
|
|
|
|
|
|
|
|
| 89 |
}
|
| 90 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|