<?php
// Start session
session_start();
// Include database configuration
require_once '../config/database.php';
// Include authentication check
require_once 'includes/auth_check.php';
// Process dues status toggle if submitted
if (isset($_POST['toggle_dues_status']) && isset($_POST['enrollment_id'])) {
$enrollment_id = intval($_POST['enrollment_id']);
// Get current dues status
$status_query = "SELECT has_dues FROM enrollments WHERE id = ?";
$stmt = $conn->prepare($status_query);
$stmt->bind_param("i", $enrollment_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$enrollment = $result->fetch_assoc();
$current_status = $enrollment['has_dues'];
// Toggle status
$new_status = $current_status ? 0 : 1;
// Update status
$update_query = "UPDATE enrollments SET has_dues = ? WHERE id = ?";
$stmt = $conn->prepare($update_query);
$stmt->bind_param("ii", $new_status, $enrollment_id);
if ($stmt->execute()) {
$_SESSION['success_message'] = "Dues status updated successfully!";
} else {
$_SESSION['error_message'] = "Error updating dues status: " . $conn->error;
}
} else {
$_SESSION['error_message'] = "Enrollment not found.";
}
// Redirect back to the same page
header("Location: " . $_SERVER['PHP_SELF']);
exit();
}
// Process enrollment update if submitted
if (isset($_POST['update_enrollment']) && isset($_POST['enrollment_id'])) {
$enrollment_id = intval($_POST['enrollment_id']);
$status = $_POST['status'];
$progress = intval($_POST['progress']);
$update_query = "UPDATE enrollments SET status = ?, progress = ? WHERE id = ?";
$stmt = $conn->prepare($update_query);
$stmt->bind_param("sii", $status, $progress, $enrollment_id);
if ($stmt->execute()) {
$_SESSION['success_message'] = "Enrollment updated successfully!";
} else {
$_SESSION['error_message'] = "Error updating enrollment: " . $conn->error;
}
// Redirect back to the same page
header("Location: " . $_SERVER['PHP_SELF']);
exit();
}
// Get all enrollments with user and course details
$query = "SELECT e.*, u.name as student_name, u.email as student_email,
c.title as course_title, c.price as course_price
FROM enrollments e
JOIN users u ON e.user_id = u.id
JOIN courses c ON e.course_id = c.id
ORDER BY e.created_at DESC";
$result = $conn->query($query);
// Include header
include_once 'includes/header.php';
?>
<div class="container-fluid">
<h1 class="h3 mb-4 text-gray-800">Manage Enrollments</h1>
<?php if (isset($_SESSION['success_message'])): ?>
<div class="alert alert-success">
<?php
echo $_SESSION['success_message'];
unset($_SESSION['success_message']);
?>
</div>
<?php endif; ?>
<?php if (isset($_SESSION['error_message'])): ?>
<div class="alert alert-danger">
<?php
echo $_SESSION['error_message'];
unset($_SESSION['error_message']);
?>
</div>
<?php endif; ?>
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">All Enrollments</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>Student</th>
<th>Course</th>
<th>Status</th>
<th>Progress</th>
<th>Has Dues</th>
<th>Enrollment Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if ($result->num_rows > 0): ?>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td>
<?php echo htmlspecialchars($row['student_name']); ?><br>
<small><?php echo htmlspecialchars($row['student_email']); ?></small>
</td>
<td><?php echo htmlspecialchars($row['course_title']); ?></td>
<td>
<span class="badge badge-<?php
echo $row['status'] === 'active' ? 'success' :
($row['status'] === 'completed' ? 'primary' : 'danger');
?>">
<?php echo ucfirst($row['status']); ?>
</span>
</td>
<td>
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: <?php echo $row['progress']; ?>%"
aria-valuenow="<?php echo $row['progress']; ?>" aria-valuemin="0" aria-valuemax="100">
<?php echo $row['progress']; ?>%
</div>
</div>
</td>
<td>
<span class="badge badge-<?php echo isset($row['has_dues']) && $row['has_dues'] ? 'danger' : 'success'; ?>">
<?php echo isset($row['has_dues']) && $row['has_dues'] ? 'Yes' : 'No'; ?>
</span>
</td>
<td><?php echo date('M d, Y', strtotime($row['created_at'])); ?></td>
<td>
<button type="button" class="btn btn-sm btn-primary edit-btn"
data-toggle="modal" data-target="#editModal"
data-id="<?php echo $row['id']; ?>"
data-status="<?php echo $row['status']; ?>"
data-progress="<?php echo $row['progress']; ?>">
<i class="fas fa-edit"></i> Edit
</button>
<form method="POST" class="d-inline">
<input type="hidden" name="enrollment_id" value="<?php echo $row['id']; ?>">
<button type="submit" name="toggle_dues_status" class="btn btn-sm btn-<?php echo isset($row['has_dues']) && $row['has_dues'] ? 'success' : 'warning'; ?>">
<i class="fas fa-<?php echo isset($row['has_dues']) && $row['has_dues'] ? 'check' : 'exclamation-triangle'; ?>"></i>
<?php echo isset($row['has_dues']) && $row['has_dues'] ? 'Mark as Paid' : 'Mark as Due'; ?>
</button>
</form>
</td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr>
<td colspan="8" class="text-center">No enrollments found.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Edit Enrollment Modal -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editModalLabel">Edit Enrollment</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form method="POST">
<div class="modal-body">
<input type="hidden" name="enrollment_id" id="enrollment_id">
<div class="form-group">
<label for="status">Status</label>
<select class="form-control" id="status" name="status" required>
<option value="active">Active</option>
<option value="completed">Completed</option>
<option value="dropped">Dropped</option>
</select>
</div>
<div class="form-group">
<label for="progress">Progress (%)</label>
<input type="number" class="form-control" id="progress" name="progress" min="0" max="100" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" name="update_enrollment" class="btn btn-primary">Save Changes</button>
</div>
</form>
</div>
</div>
</div>
<script>
// Set form values when modal is opened
$('.edit-btn').click(function() {
$('#enrollment_id').val($(this).data('id'));
$('#status').val($(this).data('status'));
$('#progress').val($(this).data('progress'));
});
</script>
<?php
// Include footer
include_once 'includes/footer.php';
?>