129 lines
5.8 KiB
PHP
129 lines
5.8 KiB
PHP
<?php
|
|
require_once 'includes/header.php';
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'client') {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$client_id = $_SESSION['user_id'];
|
|
|
|
if (!isset($_GET['id'])) {
|
|
header('Location: surveys.php');
|
|
exit;
|
|
}
|
|
|
|
$client_survey_id = $_GET['id'];
|
|
|
|
// Fetch client survey details
|
|
$stmt = db()->prepare('SELECT cs.*, s.title, s.description FROM client_surveys cs JOIN surveys s ON cs.survey_id = s.id WHERE cs.id = ? AND cs.client_id = ?');
|
|
$stmt->execute([$client_survey_id, $client_id]);
|
|
$client_survey = $stmt->fetch();
|
|
|
|
if (!$client_survey) {
|
|
header('Location: surveys.php');
|
|
exit;
|
|
}
|
|
|
|
// Fetch survey questions
|
|
$questions_stmt = db()->prepare('SELECT * FROM survey_questions WHERE survey_id = ? ORDER BY id');
|
|
$questions_stmt->execute([$client_survey['survey_id']]);
|
|
$questions = $questions_stmt->fetchAll();
|
|
|
|
// Handle submission
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $client_survey['status'] === 'pending') {
|
|
foreach ($questions as $question) {
|
|
$response_value = $_POST['responses'][$question['id']] ?? '';
|
|
if (is_array($response_value)) {
|
|
$response_value = implode(', ', $response_value);
|
|
}
|
|
|
|
$stmt = db()->prepare('INSERT INTO survey_responses (client_survey_id, question_id, response) VALUES (?, ?, ?)');
|
|
$stmt->execute([$client_survey_id, $question['id'], $response_value]);
|
|
}
|
|
|
|
// Mark as completed
|
|
$stmt = db()->prepare('UPDATE client_surveys SET status = \'completed\', completed_at = NOW() WHERE id = ?');
|
|
$stmt->execute([$client_survey_id]);
|
|
|
|
header('Location: surveys.php');
|
|
exit;
|
|
}
|
|
|
|
// If already completed, fetch responses
|
|
$responses = [];
|
|
if ($client_survey['status'] === 'completed') {
|
|
$responses_stmt = db()->prepare('SELECT question_id, response FROM survey_responses WHERE client_survey_id = ?');
|
|
$responses_stmt->execute([$client_survey_id]);
|
|
$responses_data = $responses_stmt->fetchAll();
|
|
foreach ($responses_data as $response) {
|
|
$responses[$response['question_id']] = $response['response'];
|
|
}
|
|
}
|
|
?>
|
|
|
|
<div class="container mt-5">
|
|
<h3><?php echo htmlspecialchars($client_survey['title']); ?></h3>
|
|
<p><?php echo htmlspecialchars($client_survey['description']); ?></p>
|
|
|
|
<?php if ($client_survey['status'] === 'completed'): ?>
|
|
<div class="alert alert-success">You have already completed this survey.</div>
|
|
<?php endif; ?>
|
|
|
|
<form method="POST">
|
|
<?php foreach ($questions as $index => $question):
|
|
?>
|
|
<div class="card my-3">
|
|
<div class="card-body">
|
|
<h5 class="card-title"><?php echo ($index + 1) . '. ' . htmlspecialchars($question['question']); ?></h5>
|
|
|
|
<?php
|
|
$response_value = $responses[$question['id']] ?? '';
|
|
switch ($question['type']) {
|
|
case 'text':
|
|
echo '<input type="text" class="form-control" name="responses['.$question['id'].']" value="'.$response_value.'" '.($client_survey['status'] === 'completed' ? 'disabled' : '').'>';
|
|
break;
|
|
case 'textarea':
|
|
echo '<textarea class="form-control" name="responses['.$question['id'].']" rows="3" '.($client_survey['status'] === 'completed' ? 'disabled' : '').'>'.$response_value.'</textarea>';
|
|
break;
|
|
case 'select':
|
|
case 'radio':
|
|
case 'checkbox':
|
|
$options_stmt = db()->prepare('SELECT * FROM survey_question_options WHERE question_id = ?');
|
|
$options_stmt->execute([$question['id']]);
|
|
$options = $options_stmt->fetchAll();
|
|
|
|
if ($question['type'] === 'select') {
|
|
echo '<select class="form-select" name="responses['.$question['id'].']" '.($client_survey['status'] === 'completed' ? 'disabled' : '').'>';
|
|
foreach ($options as $option) {
|
|
$selected = ($response_value == $option['option_text']) ? 'selected' : '';
|
|
echo '<option value="'.htmlspecialchars($option['option_text']).'" '.$selected.'>'.htmlspecialchars($option['option_text']).'</option>';
|
|
}
|
|
echo '</select>';
|
|
} else {
|
|
$response_array = explode(', ', $response_value);
|
|
foreach ($options as $option) {
|
|
$checked = in_array($option['option_text'], $response_array) ? 'checked' : '';
|
|
echo '<div class="form-check">';
|
|
echo '<input class="form-check-input" type="'.$question['type'].'" name="responses['.$question['id'].'][]" value="'.htmlspecialchars($option['option_text']).'" id="option_'.$option['id'].'" '.$checked.' '.($client_survey['status'] === 'completed' ? 'disabled' : '').'>';
|
|
echo '<label class="form-check-label" for="option_'.$option['id'].'">'.htmlspecialchars($option['option_text']).'</label>';
|
|
echo '</div>';
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
?>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
|
|
<?php if ($client_survey['status'] === 'pending'): ?>
|
|
<button type="submit" class="btn btn-primary">Submit Survey</button>
|
|
<?php endif; ?>
|
|
<a href="surveys.php" class="btn btn-secondary">Back to Surveys</a>
|
|
</form>
|
|
</div>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|