124 lines
5.0 KiB
PHP
124 lines
5.0 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="card mb-5">
|
|
<div class="card-body">
|
|
<h2 class="card-title">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>
|
|
</div>
|
|
|
|
<!-- List of Existing Surveys -->
|
|
<h2>Existing Surveys</h2>
|
|
<div class="row">
|
|
<?php if (empty($surveys)):
|
|
?>
|
|
<div class="col">
|
|
<p>No surveys created yet.</p>
|
|
</div>
|
|
<?php else:
|
|
?>
|
|
<?php foreach ($surveys as $survey):
|
|
?>
|
|
<div class="col-md-6 mb-4">
|
|
<div class="card h-100">
|
|
<div class="card-body">
|
|
<h5 class="card-title"><?= htmlspecialchars($survey['title']) ?></h5>
|
|
<p class="card-text"><?= htmlspecialchars($survey['description']) ?></p>
|
|
</div>
|
|
<div class="card-footer">
|
|
<a href="manage_questions.php?survey_id=<?= $survey['id'] ?>" class="btn btn-primary">Manage Questions</a>
|
|
<a href="edit_survey.php?id=<?= $survey['id'] ?>" class="btn btn-secondary">Edit</a>
|
|
<small class="text-muted float-right">Created: <?= date('M j, Y', strtotime($survey['created_at'])) ?></small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach;
|
|
?>
|
|
<?php endif;
|
|
?>
|
|
</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';
|
|
?>
|