137 lines
5.7 KiB
PHP
137 lines
5.7 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
// Ensure admin is logged in
|
|
if (!isset($_SESSION['user_id']) || !in_array('Admin', $_SESSION['user_roles'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
// Check for Survey ID
|
|
if (!isset($_GET['survey_id'])) {
|
|
header('Location: surveys.php');
|
|
exit;
|
|
}
|
|
$survey_id = $_GET['survey_id'];
|
|
|
|
// Fetch survey details
|
|
$survey_stmt = db()->prepare("SELECT * FROM surveys WHERE id = ?");
|
|
$survey_stmt->execute([$survey_id]);
|
|
$survey = $survey_stmt->fetch();
|
|
if (!$survey) {
|
|
header('Location: surveys.php');
|
|
exit;
|
|
}
|
|
|
|
// Handle form submission for new question
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['question_text'])) {
|
|
$question_text = trim($_POST['question_text']);
|
|
$question_type = trim($_POST['question_type']);
|
|
$options = ($question_type === 'multiple-choice') ? trim($_POST['options']) : null;
|
|
|
|
if (!empty($question_text) && !empty($question_type)) {
|
|
$stmt = db()->prepare("INSERT INTO survey_questions (survey_id, question_text, question_type, options) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$survey_id, $question_text, $question_type, $options]);
|
|
header("Location: manage_questions.php?survey_id=" . $survey_id);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Handle question deletion
|
|
if (isset($_GET['delete_question'])) {
|
|
$question_id = $_GET['delete_question'];
|
|
$stmt = db()->prepare("DELETE FROM survey_questions WHERE id = ? AND survey_id = ?");
|
|
$stmt->execute([$question_id, $survey_id]);
|
|
header("Location: manage_questions.php?survey_id=" . $survey_id);
|
|
exit;
|
|
}
|
|
|
|
// Fetch all questions for the survey
|
|
$questions_stmt = db()->prepare("SELECT * FROM survey_questions WHERE survey_id = ? ORDER BY created_at ASC");
|
|
$questions_stmt->execute([$survey_id]);
|
|
$questions = $questions_stmt->fetchAll();
|
|
|
|
$pageTitle = "Manage Questions for " . htmlspecialchars($survey['title']);
|
|
require_once 'templates/header.php';
|
|
?>
|
|
|
|
<main>
|
|
<section class="survey-section">
|
|
<div class="container">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1>Manage Questions for "<?= htmlspecialchars($survey['title']) ?>"</h1>
|
|
<a href="surveys.php" class="btn btn-secondary">Back to Surveys</a>
|
|
</div>
|
|
|
|
<!-- Add New Question Form -->
|
|
<div class="form-container mb-5">
|
|
<h2>Add New Question</h2>
|
|
<form method="POST">
|
|
<div class="form-group">
|
|
<label for="question_text" class="form-label">Question Text</label>
|
|
<input type="text" id="question_text" name="question_text" class="form-control" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="question_type" class="form-label">Question Type</label>
|
|
<select id="question_type" name="question_type" class="form-control" required onchange="toggleOptions(this.value)">
|
|
<option value="text">Text (Single Line)</option>
|
|
<option value="textarea">Textarea (Multi-line)</option>
|
|
<option value="rating">Rating (1-5)</option>
|
|
<option value="multiple-choice">Multiple Choice</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group" id="options-container" style="display: none;">
|
|
<label for="options" class="form-label">Options (comma-separated)</label>
|
|
<input type="text" id="options" name="options" class="form-control">
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Add Question</button>
|
|
</form>
|
|
</div>
|
|
|
|
<!-- List of Existing Questions -->
|
|
<h2>Existing Questions</h2>
|
|
<div class="table-responsive">
|
|
<table class="table table-striped table-bordered">
|
|
<thead class="thead-dark">
|
|
<tr>
|
|
<th>Question Text</th>
|
|
<th>Type</th>
|
|
<th>Options</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($questions)): ?>
|
|
<tr>
|
|
<td colspan="4">No questions added to this survey yet.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($questions as $question): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($question['question_text']) ?></td>
|
|
<td><?= htmlspecialchars($question['question_type']) ?></td>
|
|
<td><?= htmlspecialchars($question['options'] ?? '') ?></td>
|
|
<td>
|
|
<a href="?survey_id=<?= $survey_id ?>&delete_question=<?= $question['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this question?')">Delete</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
</main>
|
|
|
|
<script>
|
|
function toggleOptions(type) {
|
|
document.getElementById('options-container').style.display = (type === 'multiple-choice') ? 'block' : 'none';
|
|
}
|
|
</script>
|
|
|
|
<?php
|
|
require_once 'templates/footer.php';
|
|
?>
|