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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class QuizUI {
|
| 2 |
+
constructor() {
|
| 3 |
+
this.currentDay = 1;
|
| 4 |
+
this.initializeApp();
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
async initializeApp() {
|
| 8 |
+
// مقداردهی اولیه سیستم احراز هویت
|
| 9 |
+
AuthMiddleware.initializeApp();
|
| 10 |
+
|
| 11 |
+
await this.loadProgress();
|
| 12 |
+
this.renderLesson();
|
| 13 |
+
this.renderQuiz();
|
| 14 |
+
this.renderExercise();
|
| 15 |
+
this.updateScoreDisplay();
|
| 16 |
+
this.setupAuthUI();
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
setupAuthUI() {
|
| 20 |
+
// نمایش وضعیت کاربر در رابط کاربری
|
| 21 |
+
this.updateAuthDisplay();
|
| 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 |
+
</div>
|
| 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 |
+
handleLogin(formData) {
|
| 89 |
+
const data = {
|
| 90 |
+
username: formData.get('username'),
|
| 91 |
+
password: formData.get('password')
|
| 92 |
+
};
|
| 93 |
+
|
| 94 |
+
const validation = ValidationMiddleware.validateLoginData(data);
|
| 95 |
+
if (!validation.isValid) {
|
| 96 |
+
alert(validation.errors.join('\n'));
|
| 97 |
+
return;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
const result = authManager.loginUser(data.username, data.password);
|
| 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 |
+
submitQuiz() {
|
| 113 |
+
return requireAuth(() => {
|
| 114 |
+
const quizData = this.learningLogic.load_quiz(this.currentDay);
|
| 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 |
+
this.updateScoreDisplay();
|
| 133 |
+
})();
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
submitExercise() {
|
| 137 |
+
return requireAuth(() => {
|
| 138 |
+
const answer = document.getElementById('exerciseAnswer').value.trim();
|
| 139 |
+
if (!answer) {
|
| 140 |
+
alert('لطفاً پاسخ خود را وارد کنید');
|
| 141 |
+
return;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
const validation = ValidationMiddleware.validateExerciseAnswer(answer);
|
| 145 |
+
if (!validation.isValid) {
|
| 146 |
+
alert(validation.errors.join('\n'));
|
| 147 |
+
return;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
const result = this.learningLogic.check_exercise_answer(this.currentDay, answer);
|
| 151 |
+
this.showExerciseFeedback(result);
|
| 152 |
+
|
| 153 |
+
// بهروزرسانی امتیاز کاربر
|
| 154 |
+
if (result.reward > 0) {
|
| 155 |
+
userManager.updateUserProgress(result.reward, `exercise_day${this.currentDay}`);
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
this.updateScoreDisplay();
|
| 159 |
+
})();
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
updateScoreDisplay() {
|
| 163 |
+
const scoreDisplay = document.getElementById('userScore');
|
| 164 |
+
const user = authManager.getCurrentUser();
|
| 165 |
+
|
| 166 |
+
if (scoreDisplay && user) {
|
| 167 |
+
scoreDisplay.textContent = `${user.progress.totalScore || 0} امتیاز`;
|
| 168 |
+
} else if (scoreDisplay) {
|
| 169 |
+
scoreDisplay.textContent = '0 امتیاز - لطفاً وارد شوید';
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
// بقیه متدها مانند قبل...
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
// راهاندازی برنامه
|
| 177 |
+
const quizUI = new QuizUI();
|