130 lines
5.9 KiB
PHP
130 lines
5.9 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
// Check if user is logged in and is an Admin
|
|
if (!isset($_SESSION['user_id']) || !in_array('Admin', $_SESSION['user_roles'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
|
|
// Handle deletion of a submission
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submission_id_to_delete'])) {
|
|
$submission_id = $_POST['submission_id_to_delete'];
|
|
|
|
// First, delete the associated answers
|
|
$delete_answers_stmt = db()->prepare("DELETE FROM survey_answers WHERE submission_id = ?");
|
|
$delete_answers_stmt->execute([$submission_id]);
|
|
|
|
// Then, delete the submission itself
|
|
$delete_submission_stmt = db()->prepare("DELETE FROM feedback_submissions WHERE id = ?");
|
|
$delete_submission_stmt->execute([$submission_id]);
|
|
|
|
// Redirect to the same page to see the changes
|
|
header('Location: admin.php?page=' . (isset($_GET['page']) ? $_GET['page'] : 1));
|
|
exit;
|
|
}
|
|
|
|
// Pagination
|
|
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
|
$records_per_page = 10;
|
|
$offset = ($page - 1) * $records_per_page;
|
|
|
|
// Get total number of submissions
|
|
$total_stmt = db()->query("SELECT COUNT(*) FROM feedback_submissions");
|
|
$total_records = $total_stmt->fetchColumn();
|
|
$total_pages = ceil($total_records / $records_per_page);
|
|
|
|
// Fetch submissions for the current page
|
|
$submissions_stmt = db()->prepare("SELECT s.id, s.name, s.email, s.created_at, sv.title as survey_title, sv.id as survey_id FROM feedback_submissions s JOIN surveys sv ON s.survey_id = sv.id ORDER BY s.created_at DESC LIMIT :limit OFFSET :offset");
|
|
$submissions_stmt->bindValue(':limit', $records_per_page, PDO::PARAM_INT);
|
|
$submissions_stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
|
|
$submissions_stmt->execute();
|
|
$submissions = $submissions_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$pageTitle = "Admin - Feedback Submissions";
|
|
require_once 'templates/header.php';
|
|
?>
|
|
<main>
|
|
<section class="survey-section">
|
|
<div class="container">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<div class="d-flex justify-content-between align-items-center">
|
|
<h1 class="h3">Feedback Submissions</h1>
|
|
<div>
|
|
<a href="dashboard.php" class="btn btn-info">Dashboard</a>
|
|
<a href="surveys.php" class="btn btn-success">Manage Surveys</a>
|
|
<a href="export.php" class="btn btn-primary">Export to CSV</a>
|
|
<a href="logout.php" class="btn btn-secondary">Logout</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-striped table-bordered">
|
|
<thead class="thead-dark">
|
|
<tr>
|
|
<th>Survey</th>
|
|
<th>Submitter</th>
|
|
<th>Email</th>
|
|
<th>Submitted At</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($submissions)):
|
|
?>
|
|
<tr>
|
|
<td colspan="5">No feedback submissions yet.</td>
|
|
</tr>
|
|
<?php else:
|
|
?>
|
|
<?php foreach ($submissions as $submission):
|
|
?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($submission['survey_title']) ?></td>
|
|
<td><?= htmlspecialchars($submission['name']) ?></td>
|
|
<td><?= htmlspecialchars($submission['email']) ?></td>
|
|
<td><?= $submission['created_at'] ?></td>
|
|
<td>
|
|
<a href="view_submission.php?id=<?= $submission['id'] ?>" class="btn btn-sm btn-info">View</a>
|
|
<form method="POST" action="admin.php" style="display: inline-block;" onsubmit="return confirm('Are you sure you want to delete this submission?');">
|
|
<input type="hidden" name="submission_id_to_delete" value="<?= $submission['id'] ?>">
|
|
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach;
|
|
?>
|
|
<?php endif;
|
|
?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</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';
|
|
?>
|