34760-vm/surveys.php
Flatlogic Bot d47e350516 v1
2025-10-07 16:17:37 +00:00

125 lines
5.1 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;
}
// Handle form submission for new survey
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['title'])) {
$title = trim($_POST['title']);
$description = trim($_POST['description']);
if (!empty($title)) {
$stmt = db()->prepare("INSERT INTO surveys (title, description) VALUES (?, ?)");
$stmt->execute([$title, $description]);
header("Location: surveys.php"); // Redirect to avoid form resubmission
exit;
}
}
// Pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$records_per_page = 10;
$offset = ($page - 1) * $records_per_page;
// Get total number of surveys
$total_stmt = db()->query("SELECT COUNT(*) FROM surveys");
$total_records = $total_stmt->fetchColumn();
$total_pages = ceil($total_records / $records_per_page);
// Fetch surveys for the current page
$surveys_stmt = db()->prepare("SELECT * FROM surveys ORDER BY created_at DESC LIMIT :limit OFFSET :offset");
$surveys_stmt->bindValue(':limit', $records_per_page, PDO::PARAM_INT);
$surveys_stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$surveys_stmt->execute();
$surveys = $surveys_stmt->fetchAll();
$pageTitle = "Manage Surveys";
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 Surveys</h1>
<a href="admin.php" class="btn btn-secondary">Back to Submissions</a>
</div>
<!-- Create New Survey Form -->
<div class="form-container mb-5">
<h2>Create New Survey</h2>
<form method="POST">
<div class="form-group">
<label for="title" class="form-label">Survey Title</label>
<input type="text" id="title" name="title" class="form-control" required>
</div>
<div class="form-group">
<label for="description" class="form-label">Description (Optional)</label>
<textarea id="description" name="description" rows="3" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">Create Survey</button>
</form>
</div>
<!-- List of Existing Surveys -->
<h2>Existing Surveys</h2>
<div class="table-responsive">
<table class="table table-striped table-bordered">
<thead class="thead-dark">
<tr>
<th>Title</th>
<th>Description</th>
<th>Created At</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($surveys)): ?>
<tr>
<td colspan="4">No surveys created yet.</td>
</tr>
<?php else: ?>
<?php foreach ($surveys as $survey): ?>
<tr>
<td><?= htmlspecialchars($survey['title']) ?></td>
<td><?= htmlspecialchars($survey['description']) ?></td>
<td><?= $survey['created_at'] ?></td>
<td>
<a href="manage_questions.php?survey_id=<?= $survey['id'] ?>" class="btn btn-sm btn-primary">Manage Questions</a>
<a href="edit_survey.php?id=<?= $survey['id'] ?>" class="btn btn-sm btn-secondary">Edit</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<!-- Pagination Links -->
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
<?php if ($page > 1): ?>
<li class="page-item"><a class="page-link" href="?page=<?= $page - 1 ?>">Previous</a></li>
<?php endif; ?>
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
<li class="page-item <?= ($i == $page) ? 'active' : '' ?>"><a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a></li>
<?php endfor; ?>
<?php if ($page < $total_pages): ?>
<li class="page-item"><a class="page-link" href="?page=<?= $page + 1 ?>">Next</a></li>
<?php endif; ?>
</ul>
</nav>
</div>
</section>
</main>
<?php
require_once 'templates/footer.php';
?>