<?php
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Include database configuration
require_once '../database/db_config.php';
// Authentication check
session_start();
if (!isset($_SESSION['admin_id']) && !isset($_SESSION['user_id'])) {
echo "<div style='background-color: #f8d7da; color: #721c24; padding: 10px; border: 1px solid #f5c6cb; border-radius: 4px; margin-bottom: 10px;'>
<strong>Error:</strong> You must be logged in to access this page.
</div>";
exit;
}
// Check if the user has admin rights
$is_admin = isset($_SESSION['role']) && $_SESSION['role'] === 'admin';
if (!$is_admin) {
echo "<div style='background-color: #f8d7da; color: #721c24; padding: 10px; border: 1px solid #f5c6cb; border-radius: 4px; margin-bottom: 10px;'>
<strong>Error:</strong> You must have administrator privileges to access this page.
</div>";
exit;
}
// Initialize variables
$course_filter = isset($_GET['course_id']) ? intval($_GET['course_id']) : 0;
$plan_filter = isset($_GET['plan']) ? $_GET['plan'] : '';
$problems_only = isset($_GET['problems_only']) && $_GET['problems_only'] === '1';
// Fix enrollments with null payment plans if requested
if (isset($_GET['fix_null_plans']) && $_GET['fix_null_plans'] === '1') {
$fix_count = 0;
$sql = "UPDATE enrollments SET payment_plan = 'full' WHERE payment_plan IS NULL OR payment_plan = ''";
if ($conn->query($sql)) {
$fix_count = $conn->affected_rows;
}
}
// Get courses for filter dropdown
$courses_sql = "SELECT id, title FROM courses ORDER BY title";
$courses_result = $conn->query($courses_sql);
// Get all payment plans with related data
$sql = "SELECT e.id, e.course_id, e.user_id, e.payment_plan, e.enrollment_date,
c.title AS course_title, c.duration, c.price, c.discount_price,
u.first_name, u.last_name, u.email
FROM enrollments e
JOIN courses c ON e.course_id = c.id
JOIN users u ON e.user_id = u.id
WHERE 1=1";
// Add filters if set
if ($course_filter > 0) {
$sql .= " AND e.course_id = $course_filter";
}
if (!empty($plan_filter)) {
$sql .= " AND e.payment_plan = '$plan_filter'";
}
if ($problems_only) {
$sql .= " AND (e.payment_plan IS NULL OR e.payment_plan = '')";
}
$sql .= " ORDER BY e.enrollment_date DESC";
$result = $conn->query($sql);
// Count by payment plan type
$plan_counts_sql = "SELECT payment_plan, COUNT(*) as count
FROM enrollments
GROUP BY payment_plan
ORDER BY count DESC";
$plan_counts_result = $conn->query($plan_counts_sql);
$plan_counts = [];
if ($plan_counts_result) {
while ($row = $plan_counts_result->fetch_assoc()) {
$plan_counts[$row['payment_plan']] = $row['count'];
}
}
// Count total and problematic enrollments
$total_enrollments = 0;
$null_plan_enrollments = 0;
foreach ($plan_counts as $plan => $count) {
$total_enrollments += $count;
if ($plan === null || $plan === '') {
$null_plan_enrollments = $count;
}
}
// HTML header with Bootstrap
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Payment Plan Diagnostics</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/css/font-awesome.min.css" rel="stylesheet">
<style>
.problem-row { background-color: #ffe6e6; }
.dashboard-card { transition: all 0.3s; }
.dashboard-card:hover { transform: translateY(-5px); box-shadow: 0 5px 15px rgba(0,0,0,0.1); }
</style>
</head>
<body>
<div class="container-fluid p-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1>Payment Plan Diagnostics</h1>
<a href="../index.php" class="btn btn-secondary">Back to Admin</a>
</div>
<!-- Dashboard cards -->
<div class="row mb-4">
<div class="col-md-3">
<div class="card dashboard-card bg-primary text-white">
<div class="card-body">
<h5 class="card-title">Total Enrollments</h5>
<h3 class="card-text"><?php echo $total_enrollments; ?></h3>
</div>
</div>
</div>
<div class="col-md-3">
<div class="card dashboard-card <?php echo $null_plan_enrollments > 0 ? 'bg-danger' : 'bg-success'; ?> text-white">
<div class="card-body">
<h5 class="card-title">NULL/Empty Plans</h5>
<h3 class="card-text"><?php echo $null_plan_enrollments; ?></h3>
<?php if ($null_plan_enrollments > 0): ?>
<a href="?fix_null_plans=1" class="btn btn-sm btn-light mt-2">Fix NULL Plans</a>
<?php endif; ?>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card dashboard-card bg-info text-white">
<div class="card-body">
<h5 class="card-title">Payment Plan Distribution</h5>
<div class="d-flex justify-content-around">
<?php
foreach ($plan_counts as $plan => $count):
$plan_display = $plan;
if ($plan === null || $plan === '') {
$plan_display = "<em>NULL/Empty</em>";
}
?>
<div class="text-center">
<h6><?php echo $plan_display; ?></h6>
<h4><?php echo $count; ?></h4>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
</div>
<!-- Filters -->
<div class="card mb-4">
<div class="card-header">
<h5 class="mb-0">Filter Enrollments</h5>
</div>
<div class="card-body">
<form method="get" class="row">
<div class="col-md-4">
<div class="mb-3">
<label for="course_id" class="form-label">Course</label>
<select name="course_id" id="course_id" class="form-select">
<option value="0">All Courses</option>
<?php while ($course = $courses_result->fetch_assoc()): ?>
<option value="<?php echo $course['id']; ?>" <?php echo $course_filter == $course['id'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($course['title']); ?>
</option>
<?php endwhile; ?>
</select>
</div>
</div>
<div class="col-md-4">
<div class="mb-3">
<label for="plan" class="form-label">Payment Plan</label>
<select name="plan" id="plan" class="form-select">
<option value="">All Plans</option>
<option value="monthly" <?php echo $plan_filter === 'monthly' ? 'selected' : ''; ?>>Monthly</option>
<option value="half_duration" <?php echo $plan_filter === 'half_duration' ? 'selected' : ''; ?>>Half Duration</option>
<option value="six_month" <?php echo $plan_filter === 'six_month' ? 'selected' : ''; ?>>Six Month</option>
<option value="quarterly" <?php echo $plan_filter === 'quarterly' ? 'selected' : ''; ?>>Quarterly</option>
<option value="full" <?php echo $plan_filter === 'full' ? 'selected' : ''; ?>>Full Payment</option>
<option value="NULL" <?php echo $plan_filter === 'NULL' ? 'selected' : ''; ?>>NULL/Empty</option>
</select>
</div>
</div>
<div class="col-md-4">
<div class="mb-3">
<label class="form-label d-block">Options</label>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="problems_only" id="problems_only" value="1" <?php echo $problems_only ? 'checked' : ''; ?>>
<label class="form-check-label" for="problems_only">
Show only problematic enrollments
</label>
</div>
</div>
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary">Apply Filters</button>
<a href="?" class="btn btn-secondary">Reset Filters</a>
</div>
</form>
</div>
</div>
<!-- Results Table -->
<div class="card">
<div class="card-header bg-primary text-white">
<h5 class="mb-0">Enrollment Records</h5>
</div>
<div class="card-body">
<?php if ($result && $result->num_rows > 0): ?>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>Student</th>
<th>Course</th>
<th>Enrollment Date</th>
<th>Course Duration</th>
<th>Payment Plan</th>
</tr>
</thead>
<tbody>
<?php while ($row = $result->fetch_assoc()):
$is_problem_row = ($row['payment_plan'] === null || $row['payment_plan'] === '');
?>
<tr class="<?php echo $is_problem_row ? 'problem-row' : ''; ?>">
<td><?php echo $row['id']; ?></td>
<td>
<?php echo htmlspecialchars($row['first_name'] . ' ' . $row['last_name']); ?>
<br>
<small><?php echo htmlspecialchars($row['email']); ?></small>
</td>
<td><?php echo htmlspecialchars($row['course_title']); ?></td>
<td><?php echo date('M d, Y', strtotime($row['enrollment_date'])); ?></td>
<td><?php echo htmlspecialchars($row['duration']); ?></td>
<td>
<?php
if ($is_problem_row) {
echo '<span class="badge bg-danger">NULL/Empty</span>';
} else {
echo '<span class="badge bg-success">' . htmlspecialchars($row['payment_plan']) . '</span>';
}
?>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div class="alert alert-info">
No enrollments found matching your criteria.
</div>
<?php endif; ?>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>