Files
dnssash/theme/rb.basic/rb.custom/survey_form/module.php
T
2026-06-11 18:47:38 +09:00

274 lines
12 KiB
PHP

<?php
if (!defined('_GNUBOARD_')) exit;
// 설문 관리 라이브러리 로드
$survey_lib_path = G5_ADMIN_PATH.'/survey_manage/lib/survey.lib.php';
if (!file_exists($survey_lib_path)) {
echo '<div class="alert alert-danger">설문 라이브러리 파일이 없습니다.</div>';
return;
}
include_once($survey_lib_path);
// 모듈 설정에서 설문 ID 가져오기 (rb.custom 모듈 방식)
$sv_id = 0;
// 1. 모듈 설정에서 sv_id 확인 (rb_module 테이블의 설정값)
if (isset($row_mod['md_custom_survey_key']) && $row_mod['md_custom_survey_key']) {
$sv_id = (int)$row_mod['md_custom_survey_key'];
}
// 2. URL 파라미터에서 sv_id 확인 (우선순위 높음)
if (isset($_GET['sv_id']) && $_GET['sv_id']) {
$sv_id = (int)$_GET['sv_id'];
}
else if (isset($_POST['sv_id']) && $_POST['sv_id']) {
$sv_id = (int)$_POST['sv_id'];
}
// sv_id가 없으면 설문 선택 인터페이스 표시
if (!$sv_id && $sv_id == 0) {
echo '<div class="survey-form-module">';
echo '<div class="survey-container">';
echo '<div class="survey-header">';
echo '<div class="container">';
echo '<h3>설문을 선택해주세요</h3>';
echo '<p>모듈 설정에서 설문을 선택하거나, URL에 ?sv_id=설문번호를 추가하여 특정 설문을 표시할 수 있습니다.</p>';
// 모든 설문 목록 표시 (관리자용)
if ($is_admin) {
$all_surveys = sql_query("SELECT sv_id, sv_title, sv_status, sv_start_date, sv_end_date FROM survey_master ORDER BY sv_created_at DESC LIMIT 10");
if (sql_num_rows($all_surveys) > 0) {
echo '<div class="survey-list admin-survey-list">';
echo '<h4>설문 목록 (관리자 전용):</h4>';
echo '<div class="survey-grid">';
while ($survey_item = sql_fetch_array($all_surveys)) {
$status_class = '';
$status_text = '';
switch($survey_item['sv_status']) {
case 'active': $status_class = 'status-active'; $status_text = '진행중'; break;
case 'draft': $status_class = 'status-draft'; $status_text = '임시저장'; break;
case 'closed': $status_class = 'status-closed'; $status_text = '종료'; break;
default: $status_class = 'status-default'; $status_text = $survey_item['sv_status']; break;
}
echo '<div class="survey-card">';
echo '<div class="survey-card-header">';
echo '<h5><a href="?sv_id='.$survey_item['sv_id'].'">'.$survey_item['sv_title'].'</a></h5>';
echo '<span class="survey-status '.$status_class.'">'.$status_text.'</span>';
echo '</div>';
echo '<div class="survey-card-body">';
echo '<p>기간: '.date('Y-m-d', strtotime($survey_item['sv_start_date'])).' ~ '.date('Y-m-d', strtotime($survey_item['sv_end_date'])).'</p>';
echo '<a href="?sv_id='.$survey_item['sv_id'].'" class="btn-view-survey">설문 보기</a>';
echo '</div>';
echo '</div>';
}
echo '</div>';
echo '</div>';
}
} else {
// 일반 사용자용 - 활성화된 설문만 표시
$active_surveys = sql_query("SELECT sv_id, sv_title, sv_description FROM survey_master WHERE sv_status = 'active' AND sv_start_date <= NOW() AND sv_end_date >= NOW() ORDER BY sv_created_at DESC LIMIT 5");
if (sql_num_rows($active_surveys) > 0) {
echo '<div class="survey-list">';
echo '<h4>현재 진행 중인 설문:</h4>';
echo '<div class="survey-grid">';
while ($survey_item = sql_fetch_array($active_surveys)) {
echo '<div class="survey-card">';
echo '<div class="survey-card-header">';
echo '<h5><a href="?sv_id='.$survey_item['sv_id'].'">'.$survey_item['sv_title'].'</a></h5>';
echo '</div>';
if ($survey_item['sv_description']) {
echo '<div class="survey-card-body">';
echo '<p>'.htmlspecialchars(mb_substr($survey_item['sv_description'], 0, 100)).'...</p>';
echo '<a href="?sv_id='.$survey_item['sv_id'].'" class="btn-view-survey">참여하기</a>';
echo '</div>';
}
echo '</div>';
}
echo '</div>';
echo '</div>';
} else {
echo '<div class="no-surveys">';
echo '<i class="fa fa-clipboard" style="font-size: 3em; color: #ddd; margin-bottom: 15px;"></i>';
echo '<p>현재 진행 중인 설문이 없습니다.</p>';
echo '</div>';
}
}
echo '</div></div></div></div>';
// 기본 스타일 추가
echo '<style>
.survey-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 15px; margin-top: 15px; }
.survey-card { border: 1px solid #ddd; border-radius: 8px; padding: 15px; background: white; }
.survey-card-header { margin-bottom: 10px; }
.survey-card-header h5 { margin: 0 0 5px 0; }
.survey-card-header a { color: #333; text-decoration: none; }
.survey-card-header a:hover { color: #AA20FF; }
.survey-status { padding: 3px 8px; border-radius: 12px; font-size: 0.8em; }
.status-active { background: #d4edda; color: #155724; }
.status-draft { background: #fff3cd; color: #856404; }
.status-closed { background: #f8d7da; color: #721c24; }
.btn-view-survey { display: inline-block; padding: 8px 15px; background: #AA20FF; color: white; text-decoration: none; border-radius: 4px; font-size: 0.9em; }
.btn-view-survey:hover { background: #8A1ACC; color: white; }
.no-surveys { text-align: center; padding: 40px; color: #666; }
</style>';
return;
}
// 함수 존재 확인
if (!function_exists('validate_survey_access')) {
echo '<div class="alert alert-danger">설문 검증 함수가 정의되지 않았습니다.</div>';
return;
}
// 설문 유효성 검사
$validation = validate_survey_access($sv_id, $member['mb_id'], $_SERVER['REMOTE_ADDR']);
if (!$validation['success']) {
echo '<div class="alert alert-warning">'.$validation['message'].'</div>';
return;
}
$survey = $validation['survey'];
$questions = get_survey_questions($sv_id);
if (empty($questions)) {
echo '<div class="alert alert-info">이 설문에는 아직 질문이 등록되지 않았습니다.</div>';
return;
}
// 설문 테마 색상
$theme_color = $survey['sv_theme_color'] ?: '#AA20FF';
// 설문 데이터를 JSON으로 준비
$survey_data = array(
'survey' => $survey,
'questions' => $questions,
'theme_color' => $theme_color,
'total_responses' => get_survey_response_count($sv_id, 'completed')
);
$survey_json = json_encode($survey_data, JSON_UNESCAPED_UNICODE);
// CSS와 JS 파일의 버전을 파일 수정 시간으로 자동 갱신
$module_css_path = G5_THEME_PATH.'/rb.custom/survey_form/module.css';
$module_js_path = G5_THEME_PATH.'/rb.custom/survey_form/module.js';
$module_css_ver = file_exists($module_css_path) ? filemtime($module_css_path) : G5_CSS_VER;
$module_js_ver = file_exists($module_js_path) ? filemtime($module_js_path) : G5_JS_VER;
// 이 모듈만의 고유 ID를 생성합니다.
$module_id = 'survey_form_module_'.uniqid();
?>
<!-- 모듈의 가장 바깥 요소에 고유 ID를 부여합니다. -->
<div id="<?php echo $module_id; ?>" class="survey-form-module" data-survey='<?php echo htmlspecialchars($survey_json, ENT_QUOTES, 'UTF-8'); ?>'>
<div class="survey-container">
<!-- 설문 헤더 -->
<div class="survey-header">
<div class="container">
<h1 class="survey-title"><?php echo htmlspecialchars($survey['sv_title']); ?></h1>
<?php if ($survey['sv_description']): ?>
<p class="survey-description"><?php echo nl2br(htmlspecialchars($survey['sv_description'])); ?></p>
<?php endif; ?>
<div class="survey-info">
<div class="survey-info-item">
<i class="fa fa-clock"></i>
<span>예상 소요시간: <span class="estimated-time"><?php echo count($questions); ?>분</span></span>
</div>
<div class="survey-info-item">
<i class="fa fa-question-circle"></i>
<span>총 <span class="total-questions"><?php echo count($questions); ?></span>개 질문</span>
</div>
<div class="survey-info-item">
<i class="fa fa-users"></i>
<span><span class="total-participants"><?php echo number_format(get_survey_response_count($sv_id, 'completed')); ?></span>명 참여</span>
</div>
</div>
</div>
</div>
<!-- 진행률 바 -->
<div class="progress-container">
<div class="container">
<div class="progress-bar">
<div class="progress-fill" id="progressFill"></div>
</div>
<div class="progress-text">
<span id="progressText">0%</span> 완료 (<span id="currentQuestion">0</span> / <span class="total-questions"><?php echo count($questions); ?></span>)
</div>
</div>
</div>
<!-- 설문 폼 -->
<div class="survey-form-container">
<div class="container">
<form id="surveyForm" class="survey-form" method="post" action="<?php echo G5_THEME_URL; ?>/rb.custom/survey_form/survey_submit_page.php">
<input type="hidden" name="sv_id" value="<?php echo $sv_id; ?>">
<div class="questions-container" id="questionsContainer">
<!-- JavaScript로 질문들이 동적으로 생성됩니다 -->
</div>
<div class="submit-container">
<button type="submit" class="submit-btn" id="submitBtn" disabled>
<i class="fa fa-paper-plane"></i> 설문 제출하기
</button>
</div>
</form>
</div>
</div>
</div>
<!-- 확인 모달 -->
<div class="confirm-modal">
<div class="modal-overlay"></div>
<div class="modal-content">
<div class="modal-header">
<h3>설문 제출 확인</h3>
<button type="button" class="modal-close">&times;</button>
</div>
<div class="modal-body">
<p>설문을 제출하시겠습니까?</p>
<p class="modal-warning">제출 후에는 수정할 수 없습니다.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn-cancel">취소</button>
<button type="button" class="btn-confirm">제출하기</button>
</div>
</div>
</div>
</div>
<!-- 이 모듈에 필요한 CSS 파일을 불러옵니다. -->
<link rel="stylesheet" href="<?php echo G5_THEME_URL; ?>/rb.custom/survey_form/module.css?ver=<?php echo $module_css_ver; ?>">
<script>
(function() {
const currentModuleId = '<?php echo $module_id; ?>';
const initFunctionName = 'initSurveyFormModule';
const scriptId = 'survey-form-module-script';
if (document.getElementById(scriptId)) {
if (typeof window[initFunctionName] === 'function') {
window[initFunctionName](currentModuleId);
}
return;
}
const script = document.createElement('script');
script.id = scriptId;
script.src = '<?php echo G5_THEME_URL; ?>/rb.custom/survey_form/module.js?ver=<?php echo $module_js_ver; ?>';
script.async = true;
script.onload = () => {
if (typeof window[initFunctionName] === 'function') {
window[initFunctionName](currentModuleId);
}
};
document.head.appendChild(script);
})();
</script>