// Test configurations with different questions for each test type const testConfigs = { 'depression': { title: 'Сэтгэл гутралын тест (PHQ-9)', description: 'Сэтгэл гутралын шинж тэмдгүүдийг үнэлэх олон улсын стандартын тест', questions: [ { question: "Сүүлийн 2 долоо хоногт та дараах асуудлуудаас хэр их зовж байсан бэ: Зүйлсийг хийхэд сонирхол буюу таатай байдал алдах", options: [ { text: "Огт биш", value: 0 }, { text: "Хэд хэдэн өдөр", value: 1 }, { text: "Долоо хоногийн талаас илүү", value: 2 }, { text: "Бараг өдөр бүр", value: 3 } ] }, { question: "Сэтгэл дэмийсэн, гунигтай эсвэл итгэл найдваргүй байдал", options: [ { text: "Огт биш", value: 0 }, { text: "Хэд хэдэн өдөр", value: 1 }, { text: "Долоо хоногийн талаас илүү", value: 2 }, { text: "Бараг өдөр бүр", value: 3 } ] }, { question: "Унтахад хэцүү эсвэл унтсан хойно байнга сэрэх, эсвэл хэт их унтах", options: [ { text: "Огт биш", value: 0 }, { text: "Хэд хэдэн өдөр", value: 1 }, { text: "Долоо хоногийн талаас илүү", value: 2 }, { text: "Бараг өдөр бүр", value: 3 } ] }, { question: "Ядрах эсвэл эрч хүч дутах", options: [ { text: "Огт биш", value: 0 }, { text: "Хэд хэдэн өдөр", value: 1 }, { text: "Долоо хоногийн талаас илүү", value: 2 }, { text: "Бараг өдөр бүр", value: 3 } ] }, { question: "Хоолны дуршил муу эсвэл хэт их идэх", options: [ { text: "Огт биш", value: 0 }, { text: "Хэд хэдэн өдөр", value: 1 }, { text: "Долоо хоногийн талаас илүү", value: 2 }, { text: "Бараг өдөр бүр", value: 3 } ] }, { question: "Өөртөө муу мэдрэмж төрүүлэх - өөрийгөө бүтэлгүйтэн гэж бодох эсвэл гэр бүл, найзууддаа урвуулсан гэж бодох", options: [ { text: "Огт биш", value: 0 }, { text: "Хэд хэдэн өдөр", value: 1 }, { text: "Долоо хоногийн талаас илүү", value: 2 }, { text: "Бараг өдөр бүр", value: 3 } ] }, { question: "Зүйлсэд анхаарлаа төвлөрүүлэхэд хэцүү байх, тухайлбал сонин уншиж эсвэл телевизор үзэхэд", options: [ { text: "Огт биш", value: 0 }, { text: "Хэд хэдэн өдөр", value: 1 }, { text: "Долоо хоногийн талаас илүү", value: 2 }, { text: "Бараг өдөр бүр", value: 3 } ] }, { question: "Бусад хүмүүс анзаарахаар маш удаан хөдөлдөг эсвэл ярьдаг? Эсвэл эсрэгээрээ - уур бухимдаж эсвэл тайван биш байж, ихэвчлэн хөдөлж, суудаггүйгээр байх", options: [ { text: "Огт биш", value: 0 }, { text: "Хэд хэдэн өдөр", value: 1 }, { text: "Долоо хоногийн талаас илүү", value: 2 }, { text: "Бараг өдөр бүр", value: 3 } ] }, { question: "Үхчихсөн нь дээр байх буюу ямар нэгэн байдлаар өөртөө хохирол учруулах тухай бодсон", options: [ { text: "Огт биш", value: 0 }, { text: "Хэд хэдэн өдөр", value: 1 }, { text: "Долоо хоногийн талаас илүү", value: 2 }, { text: "Бараг өдөр бүр", value: 3 } ] } ] }, 'mental-health': { title: 'Ерөнхий сэтгэл зүйн үнэлгээ', description: 'Таны ерөнхий сэтгэл зүйн эрүүл мэндийг иж бүрэн үнэлэх тест', questions: [ { question: "Та өөртөө итгэлтэй байдаг уу?", options: [ { text: "Үргэлж итгэлтэй", value: 1 }, { text: "Ихэвчлэн итгэлтэй", value: 2 }, { text: "Заримдаа", value: 3 }, { text: "Ховор", value: 4 }, { text: "Хэзээ ч итгэлгүй", value: 5 } ] }, { question: "Стресстэй хэрхэн тэмцдэг вэ?", options: [ { text: "Маш сайн даван туулдаг", value: 1 }, { text: "Сайн даван туулдаг", value: 2 }, { text: "Дундаж", value: 3 }, { text: "Хэцүү", value: 4 }, { text: "Маш хэцүү", value: 5 } ] }, { question: "Нийгмийн харилцааны талаар та хэрхэн бодож байна?", options: [ { text: "Маш сайн харилцдаг", value: 1 }, { text: "Сайн харилцдаг", value: 2 }, { text: "Дундаж", value: 3 }, { text: "Хэцүү", value: 4 }, { text: "Маш хэцүү", value: 5 } ] }, { question: "Та өөрийн амьдралын чанартайгаа сэтгэл хангалуун уу?", options: [ { text: "Маш их сэтгэл хангалуун", value: 1 }, { text: "Сэтгэл хангалуун", value: 2 }, { text: "Дундаж", value: 3 }, { text: "Сэтгэл дутуу", value: 4 }, { text: "Маш их сэтгэл дутуу", value: 5 } ] }, { question: "Бэрхшээл тулгарсан үед та хэрхэн хандаж байна?", options: [ { text: "Идэвхтэй шийддэг", value: 1 }, { text: "Ихэвчлэн шийддэг", value: 2 }, { text: "Заримдаа л", value: 3 }, { text: "Ховор шийддэг", value: 4 }, { text: "Зайлсхийдэг", value: 5 } ] } ] }, 'self-esteem': { title: 'Өөртөө итгэх итгэлийн тест (Rosenberg)', description: 'Өөртөө хандах хандлага, өөрийгөө үнэлэх байдлыг хэмжих тест', questions: [ { question: "Би өөртөө сэтгэл хангалуун хүн", options: [ { text: "Бүрэн зөвшөөрөх", value: 4 }, { text: "Зөвшөөрөх", value: 3 }, { text: "Зөвшөөрөхгүй", value: 2 }, { text: "Огт зөвшөөрөхгүй", value: 1 } ] }, { question: "Заримдаа би өөрийгөө огт хэрэггүй хүн гэж боддог", options: [ { text: "Бүрэн зөвшөөрөх", value: 1 }, { text: "Зөвшөөрөх", value: 2 }, { text: "Зөвшөөрөхгүй", value: 3 }, { text: "Огт зөвшөөрөхгүй", value: 4 } ] }, { question: "Би хэд хэдэн сайн чанартай гэж боддог", options: [ { text: "Бүрэн зөвшөөрөх", value: 4 }, { text: "Зөвшөөрөх", value: 3 }, { text: "Зөвшөөрөхгүй", value: 2 }, { text: "Огт зөвшөөрөхгүй", value: 1 } ] }, { question: "Би бусад ихэнх хүмүүсийн адил зүйлсийг хийж чаддаг", options: [ { text: "Бүрэн зөвшөөрөх", value: 4 }, { text: "Зөвшөөрөх", value: 3 }, { text: "Зөвшөөрөхгүй", value: 2 }, { text: "Огт зөвшөөрөхгүй", value: 1 } ] }, { question: "Би өөрийгөө бахархах зүйлгүй хүн гэж боддог", options: [ { text: "Бүрэн зөвшөөрөх", value: 1 }, { text: "Зөвшөөрөх", value: 2 }, { text: "Зөвшөөрөхгүй", value: 3 }, { text: "Огт зөвшөөрөхгүй", value: 4 } ] } ] }, 'relationship': { title: 'Харилцааны сэтгэл ханамжийн тест', description: 'Хосын харилцаа, интимт харилцааны сэтгэл ханамжийг үнэлэх тест', questions: [ { question: "Та өөрийн хосынхоо хамт байхдаа хэр их баяр хөөр мэдэрдэг вэ?", options: [ { text: "Маш их баярладаг", value: 5 }, { text: "Ихэвчлэн баярладаг", value: 4 }, { text: "Заримдаа", value: 3 }, { text: "Ховор", value: 2 }, { text: "Хэзээ ч баярладаггүй", value: 1 } ] }, { question: "Танай харилцаанд харилцан ойлголцол хангалттай уу?", options: [ { text: "Маш сайн ойлголцдог", value: 5 }, { text: "Сайн ойлголцдог", value: 4 }, { text: "Дундаж", value: 3 }, { text: "Ховор ойлголцдог", value: 2 }, { text: "Ойлголцдоггүй", value: 1 } ] }, { question: "Та хоёрын интимт харилцааны талаар хэр их сэтгэл хангалуун байдаг вэ?", options: [ { text: "Маш их сэтгэл хангалуун", value: 5 }, { text: "Сэтгэл хангалуун", value: 4 }, { text: "Дундаж", value: 3 }, { text: "Сэтгэл дутуу", value: 2 }, { text: "Маш их сэтгэл дутуу", value: 1 } ] }, { question: "Маргааныг хэрхэн шийддэг вэ?", options: [ { text: "Үргэлж эвээр шийддэг", value: 5 }, { text: "Ихэвчлэн эвээр", value: 4 }, { text: "Заримдаа", value: 3 }, { text: "Ховор шийддэг", value: 2 }, { text: "Хэзээ ч шийддэггүй", value: 1 } ] } ] }, 'passive-aggression': { title: 'Далд түрэмгийллийн тест', description: 'Далд түрэмгийлэл, тэвчээргүй байдлын хэв маягийг танин мэдэх тест', questions: [ { question: "Хэн нэгэнд уурласан ч шууд хэлэхгүй байдаг уу?", options: [ { text: "Үргэлж", value: 5 }, { text: "Ихэвчлэн", value: 4 }, { text: "Заримдаа", value: 3 }, { text: "Ховор", value: 2 }, { text: "Хэзээ ч", value: 1 } ] }, { question: "Хүсээгүй зүйл хийхийг хүссэн үед хэрхэн хандаж байна?", options: [ { text: "Дуугүйхэн үл тоомсорлодог", value: 5 }, { text: "Хойшлуулдаг", value: 4 }, { text: "Шууд хэлдэг", value: 1 }, { text: "Зөвшөөрөөд дараа нь гомдоддог", value: 3 }, { text: "Эсэргүүцдэг", value: 2 } ] }, { question: "Шүүмжлэлд хэрхэн хариулдаг вэ?", options: [ { text: "Хүлээн зөвшөөрдөг", value: 1 }, { text: "Тайлбар өгдөг", value: 2 }, { text: "Хамгаалдаг", value: 3 }, { text: "Гадаад байдлаар хүлээж авч дотроо эсэргүүцдэг", value: 5 }, { text: "Эсэргүүцэж хариу шүүмжлэдэг", value: 4 } ] } ] } }; let currentTest = null; let currentQuestionIndex = 0; let answers = {}; function loadTestFromURL() { const urlParams = new URLSearchParams(window.location.search); const testType = urlParams.get('type'); if (testType && testConfigs[testType]) { currentTest = testConfigs[testType]; initializeTest(); } else { // Default test if no type specified currentTest = testConfigs['mental-health']; initializeTest(); } } function initializeTest() { // Update header document.querySelector('h1').textContent = currentTest.title; document.querySelector('.text-lg.text-gray-600').textContent = currentTest.description; // Update total questions document.getElementById('total-questions').textContent = currentTest.questions.length; // Clear existing questions const container = document.getElementById('test-container'); container.innerHTML = ''; // Generate all questions currentTest.questions.forEach((questionData, index) => { const questionDiv = document.createElement('div'); questionDiv.className = `test-question ${index === 0 ? 'active' : 'hidden'}`; questionDiv.setAttribute('data-question', index + 1); const optionsHTML = questionData.options.map((option, optionIndex) => ` `).join(''); questionDiv.innerHTML = `

${questionData.question}

${optionsHTML}
`; container.appendChild(questionDiv); }); // Reset state currentQuestionIndex = 0; answers = {}; updateProgress(); updateNavigationButtons(); // Add event listeners addEventListeners(); } function addEventListeners() { // Remove existing event listeners first document.querySelectorAll('input[type="radio"]').forEach(radio => { radio.removeEventListener('change', handleAnswerChange); }); // Add new event listeners document.querySelectorAll('input[type="radio"]').forEach(radio => { radio.addEventListener('change', handleAnswerChange); }); // Navigation button listeners const nextBtn = document.getElementById('next-btn'); const prevBtn = document.getElementById('prev-btn'); const finishBtn = document.getElementById('finish-btn'); if (nextBtn) { nextBtn.removeEventListener('click', nextQuestion); nextBtn.addEventListener('click', nextQuestion); } if (prevBtn) { prevBtn.removeEventListener('click', previousQuestion); prevBtn.addEventListener('click', previousQuestion); } if (finishBtn) { finishBtn.removeEventListener('click', finishTest); finishBtn.addEventListener('click', finishTest); } } function handleAnswerChange(event) { const questionIndex = currentQuestionIndex; const selectedValue = parseInt(event.target.value); answers[`q${questionIndex}`] = selectedValue; console.log(`Question ${questionIndex + 1} answered with value: ${selectedValue}`); // Update visual state const label = event.target.closest('label'); const questionContainer = label.closest('.test-question'); const allLabels = questionContainer.querySelectorAll('label'); // Remove selected class from all labels allLabels.forEach(l => { l.classList.remove('selected', 'border-blue-500', 'bg-blue-50'); }); // Add selected class to clicked label label.classList.add('selected', 'border-blue-500', 'bg-blue-50'); } function nextQuestion() { if (currentQuestionIndex < currentTest.questions.length - 1) { currentQuestionIndex++; showCurrentQuestion(); updateProgress(); updateNavigationButtons(); } } function previousQuestion() { if (currentQuestionIndex > 0) { currentQuestionIndex--; showCurrentQuestion(); updateProgress(); updateNavigationButtons(); } } function showCurrentQuestion() { document.querySelectorAll('.test-question').forEach((q, index) => { if (index === currentQuestionIndex) { q.classList.remove('hidden'); q.classList.add('active'); } else { q.classList.add('hidden'); q.classList.remove('active'); } }); // Restore previous answer if exists const savedAnswer = answers[`q${currentQuestionIndex}`]; if (savedAnswer !== undefined) { const radio = document.querySelector(`input[name="q${currentQuestionIndex}"][value="${savedAnswer}"]`); if (radio) { radio.checked = true; const label = radio.closest('label'); const questionContainer = label.closest('.test-question'); const allLabels = questionContainer.querySelectorAll('label'); // Remove selected class from all labels allLabels.forEach(l => { l.classList.remove('selected', 'border-blue-500', 'bg-blue-50'); }); // Add selected class to saved answer label label.classList.add('selected', 'border-blue-500', 'bg-blue-50'); } } } function updateProgress() { const progress = ((currentQuestionIndex + 1) / currentTest.questions.length) * 100; document.getElementById('progress-bar').style.width = `${progress}%`; document.getElementById('current-question').textContent = currentQuestionIndex + 1; } function updateNavigationButtons() { const prevBtn = document.getElementById('prev-btn'); const nextBtn = document.getElementById('next-btn'); const finishBtn = document.getElementById('finish-btn'); // Show/hide previous button if (currentQuestionIndex > 0) { prevBtn.classList.remove('hidden'); } else { prevBtn.classList.add('hidden'); } // Show finish button on last question if (currentQuestionIndex === currentTest.questions.length - 1) { nextBtn.classList.add('hidden'); finishBtn.classList.remove('hidden'); } else { nextBtn.classList.remove('hidden'); finishBtn.classList.add('hidden'); } } function finishTest() { // Check if all questions are answered const answeredQuestions = Object.keys(answers).length; console.log(`Answered ${answeredQuestions} out of ${currentTest.questions.length} questions`); if (answeredQuestions < currentTest.questions.length) { alert('Бүх асуултанд хариулна уу.'); return; } // Calculate results and redirect to results page calculateAndRedirectToResults(); } function calculateAndRedirectToResults() { const testType = new URLSearchParams(window.location.search).get('type') || 'mental-health'; const totalScore = Object.values(answers).reduce((sum, score) => sum + score, 0); console.log('Test completed:', { testType: testType, answers: answers, totalScore: totalScore }); // Store results in localStorage const results = { testType: testType, answers: answers, totalScore: totalScore, maxScore: currentTest.questions.length * Math.max(...currentTest.questions[0].options.map(o => o.value)), timestamp: Date.now() }; localStorage.setItem('testResults', JSON.stringify(results)); // Redirect to results page window.location.href = `/test-result?type=${testType}&score=${totalScore}`; } function init() { console.log('Initializing psychological test...'); loadTestFromURL(); } function teardown() { // Remove event listeners document.querySelectorAll('input[type="radio"]').forEach(radio => { radio.removeEventListener('change', handleAnswerChange); }); const nextBtn = document.getElementById('next-btn'); const prevBtn = document.getElementById('prev-btn'); const finishBtn = document.getElementById('finish-btn'); if (nextBtn) nextBtn.removeEventListener('click', nextQuestion); if (prevBtn) prevBtn.removeEventListener('click', previousQuestion); if (finishBtn) finishBtn.removeEventListener('click', finishTest); } export { init, teardown };