276 lines
10 KiB
PHP
276 lines
10 KiB
PHP
<?php
|
|
$sub_menu = '710304';
|
|
include_once('./_common.php');
|
|
|
|
auth_check_menu($auth, $sub_menu, "r");
|
|
|
|
$sv_id = isset($_GET['sv_id']) ? (int)$_GET['sv_id'] : 0;
|
|
|
|
if (!$sv_id) {
|
|
alert('설문을 선택해주세요.', 'survey_list.php');
|
|
}
|
|
|
|
$survey = get_survey($sv_id);
|
|
if (!$survey) {
|
|
alert('존재하지 않는 설문입니다.', 'survey_list.php');
|
|
}
|
|
|
|
$questions = get_survey_questions($sv_id);
|
|
$total_responses = get_survey_response_count($sv_id, 'completed');
|
|
|
|
// 통계 업데이트
|
|
update_survey_statistics($sv_id);
|
|
|
|
$g5['title'] = '설문 통계 - ' . $survey['sv_title'];
|
|
include_once(G5_ADMIN_PATH.'/admin.head.php');
|
|
|
|
// 응답 현황 통계
|
|
$response_stats = sql_fetch("
|
|
SELECT
|
|
COUNT(CASE WHEN sr_status = 'completed' THEN 1 END) as completed,
|
|
COUNT(CASE WHEN sr_status = 'started' THEN 1 END) as started,
|
|
COUNT(CASE WHEN sr_status = 'abandoned' THEN 1 END) as abandoned,
|
|
COUNT(*) as total
|
|
FROM survey_responses
|
|
WHERE sv_id = '$sv_id'
|
|
");
|
|
|
|
// 일별 응답 통계 (최근 30일)
|
|
$daily_stats = [];
|
|
$daily_sql = "
|
|
SELECT
|
|
DATE(sr_completed_at) as date,
|
|
COUNT(*) as count
|
|
FROM survey_responses
|
|
WHERE sv_id = '$sv_id'
|
|
AND sr_status = 'completed'
|
|
AND sr_completed_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)
|
|
GROUP BY DATE(sr_completed_at)
|
|
ORDER BY date ASC
|
|
";
|
|
$daily_result = sql_query($daily_sql);
|
|
while ($row = sql_fetch_array($daily_result)) {
|
|
$daily_stats[$row['date']] = $row['count'];
|
|
}
|
|
|
|
// 시간대별 응답 통계
|
|
$hourly_stats = [];
|
|
$hourly_sql = "
|
|
SELECT
|
|
HOUR(sr_completed_at) as hour,
|
|
COUNT(*) as count
|
|
FROM survey_responses
|
|
WHERE sv_id = '$sv_id'
|
|
AND sr_status = 'completed'
|
|
GROUP BY HOUR(sr_completed_at)
|
|
ORDER BY hour ASC
|
|
";
|
|
$hourly_result = sql_query($hourly_sql);
|
|
while ($row = sql_fetch_array($hourly_result)) {
|
|
$hourly_stats[$row['hour']] = $row['count'];
|
|
}
|
|
|
|
// 완료율 계산
|
|
$completion_rate = $response_stats['total'] > 0 ?
|
|
round(($response_stats['completed'] / $response_stats['total']) * 100, 1) : 0;
|
|
|
|
// 평균 응답 시간 계산
|
|
$avg_time_sql = "
|
|
SELECT AVG(TIMESTAMPDIFF(MINUTE, sr_started_at, sr_completed_at)) as avg_minutes
|
|
FROM survey_responses
|
|
WHERE sv_id = '$sv_id'
|
|
AND sr_status = 'completed'
|
|
AND sr_completed_at IS NOT NULL
|
|
";
|
|
$avg_time_result = sql_fetch($avg_time_sql);
|
|
$avg_response_time = round($avg_time_result['avg_minutes'] ?? 0, 1);
|
|
?>
|
|
|
|
<!-- CSS는 statistics.css 파일에서 자동 로드됩니다 -->
|
|
|
|
<div class="statistics-container">
|
|
<!-- 통계 헤더 -->
|
|
<div class="stats-header">
|
|
<h1><i class="fa fa-chart-bar"></i> 설문 통계</h1>
|
|
<p><?php echo htmlspecialchars($survey['sv_title']); ?></p>
|
|
</div>
|
|
|
|
<!-- 통계 개요 -->
|
|
<div class="stats-overview">
|
|
<div class="stat-card">
|
|
<div class="stat-number"><?php echo number_format($total_responses); ?></div>
|
|
<div class="stat-label">완료된 응답</div>
|
|
</div>
|
|
|
|
<div class="stat-card">
|
|
<div class="stat-number"><?php echo $completion_rate; ?>%</div>
|
|
<div class="stat-label">완료율</div>
|
|
</div>
|
|
|
|
<div class="stat-card">
|
|
<div class="stat-number"><?php echo $avg_response_time; ?>분</div>
|
|
<div class="stat-label">평균 응답시간</div>
|
|
</div>
|
|
|
|
<div class="stat-card">
|
|
<div class="stat-number"><?php echo number_format($response_stats['started']); ?></div>
|
|
<div class="stat-label">진행중인 응답</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 응답 현황 차트 -->
|
|
<div class="chart-section">
|
|
<div class="chart-header">
|
|
<h3 class="chart-title">응답 현황</h3>
|
|
<div class="chart-controls">
|
|
<button class="chart-btn active" data-chart="daily">일별</button>
|
|
<button class="chart-btn" data-chart="hourly">시간대별</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="chart-container">
|
|
<canvas id="responseChart"></canvas>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 질문별 통계 -->
|
|
<div class="questions-stats">
|
|
<?php foreach ($questions as $index => $question): ?>
|
|
<div class="question-stat">
|
|
<div class="question-header">
|
|
<span class="question-number"><?php echo $index + 1; ?></span>
|
|
<h4 class="question-title"><?php echo htmlspecialchars($question['sq_title']); ?></h4>
|
|
<span class="question-type"><?php echo $question['sq_type']; ?></span>
|
|
</div>
|
|
|
|
<?php
|
|
// 질문별 통계 데이터 가져오기
|
|
$question_stats_sql = "
|
|
SELECT ss_option_value, ss_count, ss_percentage
|
|
FROM survey_statistics
|
|
WHERE sv_id = '$sv_id' AND sq_id = '{$question['sq_id']}'
|
|
ORDER BY ss_count DESC
|
|
";
|
|
$question_stats_result = sql_query($question_stats_sql);
|
|
$has_stats = sql_num_rows($question_stats_result) > 0;
|
|
?>
|
|
|
|
<?php if ($has_stats): ?>
|
|
<div class="question-stats-grid">
|
|
<div class="answer-stats">
|
|
<?php while ($stat = sql_fetch_array($question_stats_result)): ?>
|
|
<div class="answer-item">
|
|
<div class="answer-label"><?php echo htmlspecialchars($stat['ss_option_value']); ?></div>
|
|
<div class="answer-bar">
|
|
<div class="answer-fill" data-width="<?php echo $stat['ss_percentage']; ?>"></div>
|
|
</div>
|
|
<div class="answer-count"><?php echo number_format($stat['ss_count']); ?>명</div>
|
|
<div class="answer-percentage"><?php echo $stat['ss_percentage']; ?>%</div>
|
|
</div>
|
|
<?php endwhile; ?>
|
|
</div>
|
|
|
|
<div class="question-chart">
|
|
<canvas id="questionChart<?php echo $question['sq_id']; ?>"></canvas>
|
|
</div>
|
|
</div>
|
|
|
|
<?php elseif (in_array($question['sq_type'], ['text', 'textarea'])): ?>
|
|
<!-- 텍스트 응답 표시 -->
|
|
<div class="text-responses">
|
|
<?php
|
|
$text_responses_sql = "
|
|
SELECT sa.sa_value, sr.sr_completed_at, sr.sr_mb_id
|
|
FROM survey_answers sa
|
|
JOIN survey_responses sr ON sa.sr_id = sr.sr_id
|
|
WHERE sa.sq_id = '{$question['sq_id']}'
|
|
AND sr.sr_status = 'completed'
|
|
AND sa.sa_value != ''
|
|
ORDER BY sr.sr_completed_at DESC
|
|
LIMIT 20
|
|
";
|
|
$text_responses_result = sql_query($text_responses_sql);
|
|
|
|
if (sql_num_rows($text_responses_result) > 0):
|
|
while ($text_response = sql_fetch_array($text_responses_result)):
|
|
?>
|
|
<div class="text-response">
|
|
<div class="text-response-meta">
|
|
<?php echo $text_response['sr_mb_id'] ?: '익명'; ?> •
|
|
<?php echo date('Y-m-d H:i', strtotime($text_response['sr_completed_at'])); ?>
|
|
</div>
|
|
<div class="text-response-content">
|
|
<?php echo nl2br(htmlspecialchars($text_response['sa_value'])); ?>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
endwhile;
|
|
else:
|
|
?>
|
|
<div class="text-response">
|
|
<div class="text-response-content no-responses">
|
|
아직 응답이 없습니다.
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?php else: ?>
|
|
<div class="no-data">
|
|
<i class="fa fa-chart-bar"></i>
|
|
<p>아직 응답 데이터가 없습니다.</p>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
|
|
<!-- 내보내기 섹션 -->
|
|
<div class="export-section">
|
|
<h3><i class="fa fa-download"></i> 데이터 내보내기</h3>
|
|
<p>설문 결과를 다양한 형식으로 내보낼 수 있습니다.</p>
|
|
|
|
<div class="export-buttons">
|
|
<a href="export.php?sv_id=<?php echo $sv_id; ?>&format=excel" class="export-btn">
|
|
<i class="fa fa-file-excel"></i> 엑셀 다운로드
|
|
</a>
|
|
<a href="export.php?sv_id=<?php echo $sv_id; ?>&format=csv" class="export-btn secondary">
|
|
<i class="fa fa-file-csv"></i> CSV 다운로드
|
|
</a>
|
|
<button onclick="printStatistics()" class="export-btn secondary">
|
|
<i class="fa fa-print"></i> 인쇄하기
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Chart.js 라이브러리 -->
|
|
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
|
|
|
<!-- JavaScript는 statistics.js 파일에서 자동 로드됩니다 -->
|
|
<script>
|
|
// 페이지별 초기 데이터 설정
|
|
// 💡 [수정] JavaScript에서 사용하는 데이터 구조(survey, stats, chartData)에 맞게 객체를 재구성합니다.
|
|
// 이전에 발생했던 'daily' 또는 'title' 속성을 찾을 수 없다는 오류를 해결합니다.
|
|
window.statisticsData = {
|
|
survey: {
|
|
id: <?php echo $sv_id; ?>,
|
|
title: '<?php echo addslashes(htmlspecialchars($survey['sv_title'])); ?>'
|
|
},
|
|
stats: {
|
|
totalResponses: '<?php echo number_format($total_responses); ?>',
|
|
completionRate: '<?php echo $completion_rate; ?>',
|
|
avgResponseTime: '<?php echo $avg_response_time; ?>',
|
|
startedResponses: '<?php echo number_format($response_stats['started']); ?>'
|
|
},
|
|
chartData: {
|
|
daily: <?php echo json_encode($daily_stats); ?>,
|
|
hourly: <?php echo json_encode($hourly_stats); ?>
|
|
},
|
|
questions: <?php echo json_encode($questions); ?>
|
|
};
|
|
</script>
|
|
|
|
<?php
|
|
include_once(G5_ADMIN_PATH.'/admin.tail.php');
|
|
?>
|