/** * TPU Survey - Standalone Statistics Logic */ function renderStatsQuestions(questions) { let html = ''; const ratingQuestions = questions.filter(q => q.question_type === 'radio' && q.mean !== undefined); const categoricalQuestions = questions.filter(q => (q.question_type === 'dropdown' || q.question_type === 'checkbox' || (q.question_type === 'radio' && q.mean === undefined)) && q.frequencies && q.frequencies.length > 0 ); if (categoricalQuestions.length > 0) { categoricalQuestions.forEach(q => { const cleanText = q.question_text.replace(/^\s*[\d๑-๙]+[\.\)\s-:]*\s*/, ''); html += `

${escapeHtml(cleanText)}

`; q.frequencies.forEach(f => { html += ` `; }); html += `
ตัวเลือก จำนวน ร้อยละ
${escapeHtml(String(f.value))} ${f.count} ${f.percentage}%
`; }); } if (ratingQuestions.length > 0) { html += `
`; html += ``; html += ``; let totalMean = 0, totalSD = 0, count = 0; ratingQuestions.forEach(q => { const cleanText = q.question_text.replace(/^\s*[\d๑-๙]+[\.\)\s-:]*\s*/, ''); html += ``; html += ``; for (let i = 4; i >= 0; i--) { const freq = q.frequencies?.[i] || { count: 0 }; html += ``; } html += ``; html += ``; html += ``; html += ``; totalMean += q.mean || 0; totalSD += q.sd || 0; count++; }); if (count > 0) { const avgMean = totalMean / count; const avgSD = totalSD / count; let avgInterp = ''; if (avgMean >= 4.51) avgInterp = 'มากที่สุด'; else if (avgMean >= 3.51) avgInterp = 'มาก'; else if (avgMean >= 2.51) avgInterp = 'ปานกลาง'; else if (avgMean >= 1.51) avgInterp = 'น้อย'; else avgInterp = 'น้อยที่สุด'; html += ``; html += ``; html += ``; html += ``; } html += `
รายการประเมิน54321X̄S.D.แปลผล
${escapeHtml(cleanText)}${freq.count}${q.mean?.toFixed(2) || '-'}${q.sd?.toFixed(2) || '-'}${q.interpretation || '-'}
รวมเฉลี่ย${avgMean.toFixed(2)}${avgSD.toFixed(2)}${avgInterp}
`; } // Text responses questions.filter(q => q.question_type === 'text').forEach(q => { const cleanText = q.question_text.replace(/^\s*[\d๑-๙]+[\.\)\s-:]*\s*/, ''); html += `
`; html += `

${escapeHtml(cleanText)}

`; if (q.text_responses && q.text_responses.length > 0) { q.text_responses.forEach((t, i) => { html += `
${i+1}. ${escapeHtml(t)}
`; }); } else { html += `

(ไม่มีข้อคิดเห็น)

`; } html += `
`; }); return html; } async function initStats() { const urlParams = new URLSearchParams(window.location.search); const surveyId = urlParams.get('survey_id'); if (!surveyId) { alert('ไม่ระบุ ID แบบสอบถาม'); window.close(); return; } const statsEl = document.getElementById('statsMainContent'); const actionsEl = document.getElementById('statsActions'); const titleEl = document.getElementById('statsSurveyTitle'); try { const [summary, detail, resListData] = await Promise.all([ API.get(`statistics.php?action=summary&survey_id=${surveyId}`), API.get(`statistics.php?action=detail&survey_id=${surveyId}`), API.get(`responses.php?action=list&survey_id=${surveyId}`) ]); titleEl.textContent = `ผลสถิติ: ${summary.survey.title}`; document.title = `ผลสถิติ: ${summary.survey.title} - TPU Survey`; actionsEl.innerHTML = ` `; let html = `
📊

ค่าเฉลี่ยรวม (X̄)

${summary.overall_mean.toFixed(2)}
📉

ส่วนเบี่ยงเบนมาตรฐาน (S.D.)

${summary.overall_sd.toFixed(2)}
👥

จำนวนผู้ตอบ

${summary.total_responses}
`; (detail.sections || []).forEach((section, index) => { html += `
`; html += `

${escapeHtml(section.title)}

`; if (index === 0) { html += `
📊 จำนวนยอดการตอบแบบสอบถามทั้งหมด: ${detail.total_responses} ชุด
`; } html += `
`; html += `
`; // Collect all questions for this section (from headings + loose) const allQuestions = []; // Render headings (section.headings || []).forEach(heading => { if (heading.questions && heading.questions.length > 0) { // Add heading sub-header html += `

📑 ${escapeHtml(heading.title)}

${heading.description ? `

${escapeHtml(heading.description)}

` : ''}
`; html += renderStatsQuestions(heading.questions); heading.questions.forEach(q => allQuestions.push(q)); } }); // Render loose questions const looseQuestions = section.questions || []; if (looseQuestions.length > 0) { if ((section.headings || []).length > 0) { html += `
📋 คำถามทั่วไป
`; } html += renderStatsQuestions(looseQuestions); looseQuestions.forEach(q => allQuestions.push(q)); } html += `
`; }); // --- ส่วนที่ 4: รายการคำตอบทั้งหมด (Individual Responses) --- const responses = resListData.responses || []; html += `

📝 รายการคำตอบทั้งหมด (${responses.length} ชุด)

`; if (responses.length === 0) { html += ``; } else { responses.forEach((r, idx) => { html += ` `; }); } html += `
# วัน-เวลาที่ส่ง IP Address จัดการ
ยังไม่มีผู้ตอบแบบสอบถาม
${responses.length - idx} ${new Date(r.submitted_at).toLocaleString('th-TH')} ${r.ip_address || '-'}
`; statsEl.innerHTML = html; } catch (err) { statsEl.innerHTML = `
❌

ไม่สามารถโหลดข้อมูลได้

${err.message}

`; } } async function viewResponseDetail(responseId) { try { const data = await API.get(`responses.php?action=get&id=${responseId}`); const r = data.response; let html = `

วันเวลาที่ส่ง: ${new Date(r.submitted_at).toLocaleString('th-TH')}

IP Address: ${r.ip_address}

`; r.answers.forEach((ans, idx) => { const cleanText = ans.question_text.replace(/^\s*[\d๑-๙]+[\.\)\s-:]*\s*/, ''); html += `
${escapeHtml(cleanText)}
คำตอบ: ${escapeHtml(ans.answer_value)}
`; }); html += `
`; Modal.create(`รายละเอียดคำตอบ #${r.id}`, html, '', 'modal-lg'); } catch (err) { alert('ไม่สามารถโหลดรายละเอียดได้: ' + err.message); } } document.addEventListener('DOMContentLoaded', initStats);