Path : /home/vishqocm/pcib.in/admin/
File Upload :
Current File : /home/vishqocm//pcib.in/admin/view_instructor.php

<?php
// Include header file
include 'includes/header.php';

// Check if user is logged in and is admin
if (!isset($_SESSION['user_id']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
    echo '<div class="alert alert-danger">Access denied. You must be an admin to access this page.</div>';
    include 'includes/footer.php';
    exit;
}

// Check if instructor ID is provided
if (!isset($_GET['id']) || empty($_GET['id'])) {
    echo '<div class="alert alert-danger">Invalid instructor ID.</div>';
    include 'includes/footer.php';
    exit;
}

// Include database connection
require_once 'database/db_config.php';

$instructor_id = (int)$_GET['id'];

// Get instructor details
$query = "SELECT * FROM users WHERE id = ? AND role = 'instructor'";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $instructor_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    echo '<div class="alert alert-danger">Instructor not found.</div>';
    include 'includes/footer.php';
    exit;
}

$instructor = $result->fetch_assoc();

// 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;
?>

<div class="container-fluid">
    <div class="row">
        <!-- Sidebar -->
        <?php include 'includes/sidebar.php'; ?>
        
        <!-- Main content -->
        <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
            <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                <h1 class="h2">Instructor Profile</h1>
                <div class="btn-toolbar mb-2 mb-md-0">
                    <a href="instructors.php" class="btn btn-secondary me-2">Back to List</a>
                    <a href="edit_user.php?id=<?php echo $instructor_id; ?>" class="btn btn-primary">Edit Instructor</a>
                </div>
            </div>
            
            <div class="row">
                <!-- Instructor Profile -->
                <div class="col-lg-4">
                    <div class="card mb-4">
                        <div class="card-body text-center">
                            <?php if (!empty($instructor['profile_image'])): ?>
                                <img src="../uploads/profiles/<?php echo $instructor['profile_image']; ?>" alt="Profile" class="rounded-circle img-fluid" style="width: 150px; height: 150px; object-fit: cover;">
                            <?php else: ?>
                                <div class="bg-primary rounded-circle d-flex align-items-center justify-content-center text-white mx-auto" style="width: 150px; height: 150px; font-size: 60px;">
                                    <?php echo strtoupper(substr($instructor['first_name'], 0, 1) . substr($instructor['last_name'], 0, 1)); ?>
                                </div>
                            <?php endif; ?>
                            <h5 class="my-3"><?php echo htmlspecialchars($instructor['first_name'] . ' ' . $instructor['last_name']); ?></h5>
                            <p class="text-muted mb-1">Instructor</p>
                            <p class="text-muted mb-4">
                                <?php
                                $status_badge = '';
                                switch ($instructor['status']) {
                                    case 'active':
                                        $status_badge = 'success';
                                        break;
                                    case 'pending':
                                        $status_badge = 'warning';
                                        break;
                                    case 'suspended':
                                        $status_badge = 'danger';
                                        break;
                                }
                                ?>
                                <span class="badge bg-<?php echo $status_badge; ?>"><?php echo ucfirst($instructor['status']); ?></span>
                            </p>
                            
                            <div class="d-flex justify-content-center mb-2">
                                <?php if ($instructor['status'] === 'pending'): ?>
                                    <a href="instructors.php?action=approve&id=<?php echo $instructor_id; ?>" class="btn btn-success me-1" onclick="return confirm('Are you sure you want to approve this instructor?')">Approve</a>
                                    <a href="instructors.php?action=reject&id=<?php echo $instructor_id; ?>" class="btn btn-danger" onclick="return confirm('Are you sure you want to reject this instructor?')">Reject</a>
                                <?php endif; ?>
                                
                                <?php if ($instructor['status'] === 'suspended'): ?>
                                    <a href="instructors.php?action=approve&id=<?php echo $instructor_id; ?>" class="btn btn-success" onclick="return confirm('Are you sure you want to reactivate this instructor?')">Reactivate</a>
                                <?php endif; ?>
                                
                                <?php if ($instructor['status'] === 'active'): ?>
                                    <a href="instructors.php?action=reject&id=<?php echo $instructor_id; ?>" class="btn btn-danger" onclick="return confirm('Are you sure you want to suspend this instructor?')">Suspend</a>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                    
                    <div class="card mb-4">
                        <div class="card-header">Contact Information</div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-sm-4">
                                    <p class="mb-0">Username</p>
                                </div>
                                <div class="col-sm-8">
                                    <p class="text-muted mb-0"><?php echo htmlspecialchars($instructor['username']); ?></p>
                                </div>
                            </div>
                            <hr>
                            <div class="row">
                                <div class="col-sm-4">
                                    <p class="mb-0">Email</p>
                                </div>
                                <div class="col-sm-8">
                                    <p class="text-muted mb-0"><?php echo htmlspecialchars($instructor['email']); ?></p>
                                </div>
                            </div>
                            <hr>
                            <div class="row">
                                <div class="col-sm-4">
                                    <p class="mb-0">Phone</p>
                                </div>
                                <div class="col-sm-8">
                                    <p class="text-muted mb-0"><?php echo htmlspecialchars($instructor['phone'] ?? 'Not provided'); ?></p>
                                </div>
                            </div>
                            <hr>
                            <div class="row">
                                <div class="col-sm-4">
                                    <p class="mb-0">Member Since</p>
                                </div>
                                <div class="col-sm-8">
                                    <p class="text-muted mb-0"><?php echo date('F d, Y', strtotime($instructor['created_at'])); ?></p>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="card mb-4">
                        <div class="card-header">Statistics</div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-sm-6">
                                    <p class="mb-0">Total Courses</p>
                                </div>
                                <div class="col-sm-6">
                                    <p class="text-muted mb-0"><?php echo $courses_result->num_rows; ?></p>
                                </div>
                            </div>
                            <hr>
                            <div class="row">
                                <div class="col-sm-6">
                                    <p class="mb-0">Total Students</p>
                                </div>
                                <div class="col-sm-6">
                                    <p class="text-muted mb-0"><?php echo $total_students; ?></p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="col-lg-8">
                    <!-- Bio -->
                    <div class="card mb-4">
                        <div class="card-header">Biography</div>
                        <div class="card-body">
                            <?php if (!empty($instructor['bio'])): ?>
                                <p><?php echo nl2br(htmlspecialchars($instructor['bio'])); ?></p>
                            <?php else: ?>
                                <p class="text-muted">No biography provided.</p>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <!-- Courses -->
                    <div class="card mb-4">
                        <div class="card-header d-flex justify-content-between align-items-center">
                            <span>Courses (<?php echo $courses_result->num_rows; ?>)</span>
                            <a href="add_course.php?instructor_id=<?php echo $instructor_id; ?>" class="btn btn-sm btn-primary">Add Course</a>
                        </div>
                        <div class="card-body">
                            <?php if ($courses_result->num_rows > 0): ?>
                                <div class="table-responsive">
                                    <table class="table table-hover">
                                        <thead>
                                            <tr>
                                                <th>Title</th>
                                                <th>Price</th>
                                                <th>Students</th>
                                                <th>Status</th>
                                                <th>Created</th>
                                                <th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php while ($course = $courses_result->fetch_assoc()): ?>
                                                <tr>
                                                    <td><?php echo htmlspecialchars($course['title']); ?></td>
                                                    <td>
                                                        <?php 
                                                        if ($course['price'] > 0) {
                                                            echo '$' . number_format($course['price'], 2);
                                                        } else {
                                                            echo '<span class="badge bg-success">Free</span>';
                                                        }
                                                        ?>
                                                    </td>
                                                    <td><?php echo $course['enrollment_count']; ?></td>
                                                    <td>
                                                        <?php
                                                        $course_status_badge = $course['status'] === 'published' ? 'success' : 'secondary';
                                                        ?>
                                                        <span class="badge bg-<?php echo $course_status_badge; ?>"><?php echo ucfirst($course['status']); ?></span>
                                                    </td>
                                                    <td><?php echo date('M d, Y', strtotime($course['created_at'])); ?></td>
                                                    <td>
                                                        <div class="btn-group">
                                                            <a href="view_course.php?id=<?php echo $course['id']; ?>" class="btn btn-sm btn-info" title="View"><i class="bi bi-eye"></i></a>
                                                            <a href="edit_course.php?id=<?php echo $course['id']; ?>" class="btn btn-sm btn-warning" title="Edit"><i class="bi bi-pencil"></i></a>
                                                        </div>
                                                    </td>
                                                </tr>
                                            <?php endwhile; ?>
                                        </tbody>
                                    </table>
                                </div>
                            <?php else: ?>
                                <p class="text-muted">No courses created yet.</p>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</div>

<?php include 'includes/footer.php'; ?>