<?php
session_start();
// Check if user is logged in and is an instructor
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'instructor') {
header("Location: login.php");
exit;
}
// Include database configuration
require_once '../admin/database/db_config.php';
// Get instructor ID
$instructor_id = $_SESSION['user_id'];
// Get courses taught by instructor
$courses_query = "SELECT c.*,
(SELECT COUNT(*) FROM enrollments WHERE course_id = c.id) as enrollment_count
FROM courses c
WHERE c.instructor_id = ?
ORDER BY c.created_at DESC";
$courses_stmt = $conn->prepare($courses_query);
$courses_stmt->bind_param("i", $instructor_id);
$courses_stmt->execute();
$courses_result = $courses_stmt->get_result();
// Get total students enrolled in instructor's courses
$total_students_query = "SELECT COUNT(DISTINCT e.user_id) as total_students
FROM enrollments e
JOIN courses c ON e.course_id = c.id
WHERE c.instructor_id = ?";
$total_students_stmt = $conn->prepare($total_students_query);
$total_students_stmt->bind_param("i", $instructor_id);
$total_students_stmt->execute();
$total_students_result = $total_students_stmt->get_result();
$total_students = $total_students_result->fetch_assoc()['total_students'] ?? 0;
// Get recent enrollments
$recent_enrollments_query = "SELECT e.*, u.first_name, u.last_name, u.profile_image, c.title as course_title
FROM enrollments e
JOIN users u ON e.user_id = u.id
JOIN courses c ON e.course_id = c.id
WHERE c.instructor_id = ?
ORDER BY e.enrollment_date DESC
LIMIT 5";
$recent_enrollments_stmt = $conn->prepare($recent_enrollments_query);
$recent_enrollments_stmt->bind_param("i", $instructor_id);
$recent_enrollments_stmt->execute();
$recent_enrollments_result = $recent_enrollments_stmt->get_result();
// Include header
include '../includes/header.php';
?>
<div class="container py-5">
<div class="row mb-4">
<div class="col-md-8">
<h2>Welcome, <?php echo htmlspecialchars($_SESSION['first_name'] . ' ' . $_SESSION['last_name']); ?>!</h2>
<p class="text-muted">Manage your courses and students from this dashboard.</p>
</div>
<div class="col-md-4 text-md-end">
<a href="add_course.php" class="btn btn-primary">
<i class="bi bi-plus-circle me-1"></i> Add New Course
</a>
</div>
</div>
<!-- Stats Cards -->
<div class="row mb-4">
<div class="col-md-4 mb-3">
<div class="card bg-primary text-white h-100">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center">
<div>
<h6 class="text-uppercase">Total Courses</h6>
<h2 class="mb-0"><?php echo $courses_result->num_rows; ?></h2>
</div>
<div>
<i class="bi bi-journals fa-3x"></i>
</div>
</div>
</div>
<div class="card-footer d-flex align-items-center justify-content-between">
<a href="courses.php" class="text-white text-decoration-none">View Details</a>
<div><i class="bi bi-arrow-right"></i></div>
</div>
</div>
</div>
<div class="col-md-4 mb-3">
<div class="card bg-success text-white h-100">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center">
<div>
<h6 class="text-uppercase">Total Students</h6>
<h2 class="mb-0"><?php echo $total_students; ?></h2>
</div>
<div>
<i class="bi bi-people fa-3x"></i>
</div>
</div>
</div>
<div class="card-footer d-flex align-items-center justify-content-between">
<a href="students.php" class="text-white text-decoration-none">View Details</a>
<div><i class="bi bi-arrow-right"></i></div>
</div>
</div>
</div>
<div class="col-md-4 mb-3">
<div class="card bg-info text-white h-100">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center">
<div>
<h6 class="text-uppercase">Reviews</h6>
<h2 class="mb-0">--</h2>
</div>
<div>
<i class="bi bi-star fa-3x"></i>
</div>
</div>
</div>
<div class="card-footer d-flex align-items-center justify-content-between">
<a href="reviews.php" class="text-white text-decoration-none">View Details</a>
<div><i class="bi bi-arrow-right"></i></div>
</div>
</div>
</div>
</div>
<!-- Recent Courses and Enrollments -->
<div class="row">
<!-- Courses -->
<div class="col-lg-7 mb-4">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0">Your Courses</h5>
<a href="courses.php" class="btn btn-sm btn-outline-primary">View All</a>
</div>
<div class="card-body p-0">
<?php if ($courses_result->num_rows > 0): ?>
<div class="table-responsive">
<table class="table table-hover mb-0">
<thead>
<tr>
<th>Title</th>
<th>Students</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$counter = 0;
while ($course = $courses_result->fetch_assoc()):
if ($counter++ >= 5) break; // Show only 5 courses
?>
<tr>
<td><?php echo htmlspecialchars($course['title']); ?></td>
<td><?php echo $course['enrollment_count']; ?></td>
<td>
<?php
$status_badge = $course['status'] === 'published' ? 'success' : 'secondary';
?>
<span class="badge bg-<?php echo $status_badge; ?>"><?php echo ucfirst($course['status']); ?></span>
</td>
<td>
<div class="btn-group btn-group-sm">
<a href="edit_course.php?id=<?php echo $course['id']; ?>" class="btn btn-outline-primary" title="Edit"><i class="bi bi-pencil"></i></a>
<a href="view_course.php?id=<?php echo $course['id']; ?>" class="btn btn-outline-info" title="View"><i class="bi bi-eye"></i></a>
</div>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div class="text-center py-5">
<img src="../assets/images/empty-courses.svg" alt="No courses" style="width: 120px; opacity: 0.5;" class="mb-3">
<h5>You haven't created any courses yet</h5>
<p class="text-muted">Get started by adding your first course.</p>
<a href="add_course.php" class="btn btn-primary">Create Course</a>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- Recent Enrollments -->
<div class="col-lg-5 mb-4">
<div class="card">
<div class="card-header">
<h5 class="mb-0">Recent Enrollments</h5>
</div>
<div class="card-body p-0">
<?php if ($recent_enrollments_result->num_rows > 0): ?>
<ul class="list-group list-group-flush">
<?php while ($enrollment = $recent_enrollments_result->fetch_assoc()): ?>
<li class="list-group-item">
<div class="d-flex align-items-center">
<?php if (!empty($enrollment['profile_image'])): ?>
<img src="../uploads/profiles/<?php echo $enrollment['profile_image']; ?>" alt="Profile" class="rounded-circle me-3" width="40" height="40" style="object-fit: cover;">
<?php else: ?>
<div class="bg-primary rounded-circle d-flex align-items-center justify-content-center text-white me-3" style="width: 40px; height: 40px;">
<?php echo strtoupper(substr($enrollment['first_name'], 0, 1) . substr($enrollment['last_name'], 0, 1)); ?>
</div>
<?php endif; ?>
<div>
<h6 class="mb-0"><?php echo htmlspecialchars($enrollment['first_name'] . ' ' . $enrollment['last_name']); ?></h6>
<small class="text-muted">
Enrolled in <strong><?php echo htmlspecialchars($enrollment['course_title']); ?></strong>
<br>
<?php echo date('M d, Y', strtotime($enrollment['enrollment_date'])); ?>
</small>
</div>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php else: ?>
<div class="text-center py-5">
<img src="../assets/images/empty-enrollments.svg" alt="No enrollments" style="width: 120px; opacity: 0.5;" class="mb-3">
<h5>No enrollments yet</h5>
<p class="text-muted">Students will appear here when they enroll in your courses.</p>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<!-- Profile Completion -->
<div class="row">
<div class="col-12">
<div class="card bg-light">
<div class="card-body">
<h5>Complete Your Profile</h5>
<p>A complete profile helps build credibility with potential students.</p>
<div class="progress mb-3">
<!-- Placeholder for profile completion percentage -->
<div class="progress-bar bg-success" role="progressbar" style="width: 70%;" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100">70%</div>
</div>
<div class="d-flex flex-wrap gap-2">
<a href="profile.php" class="btn btn-sm btn-outline-primary">Update Profile</a>
<a href="update_bio.php" class="btn btn-sm btn-outline-secondary">Add Biography</a>
<a href="add_credentials.php" class="btn btn-sm btn-outline-secondary">Add Credentials</a>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include '../includes/footer.php'; ?>