234 lines
9.3 KiB
PHP
234 lines
9.3 KiB
PHP
<?php
|
||
session_start();
|
||
require_once 'db/config.php';
|
||
|
||
if (!isset($_SESSION['user_id'])) {
|
||
header("Location: login.php");
|
||
exit;
|
||
}
|
||
|
||
$userId = $_SESSION['user_id'];
|
||
$message = '';
|
||
$message_type = '';
|
||
|
||
// Handle Enrollment
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['enroll_skill_id'])) {
|
||
$skillId = $_POST['enroll_skill_id'];
|
||
|
||
// Check if already enrolled
|
||
$stmt = db()->prepare("SELECT id FROM enrollments WHERE user_id = ? AND skill_id = ?");
|
||
$stmt->execute([$userId, $skillId]);
|
||
if ($stmt->fetch()) {
|
||
$_SESSION['message'] = "You are already enrolled in this skill.";
|
||
$_SESSION['message_type'] = 'warning';
|
||
} else {
|
||
// Enroll user
|
||
$stmt = db()->prepare("INSERT INTO enrollments (user_id, skill_id, progress, date_enrolled) VALUES (?, ?, 0, NOW())");
|
||
if ($stmt->execute([$userId, $skillId])) {
|
||
$_SESSION['message'] = "Enrollment successful! You can now continue learning from your dashboard.";
|
||
$_SESSION['message_type'] = 'success';
|
||
} else {
|
||
$_SESSION['message'] = "An error occurred. Please try again.";
|
||
$_SESSION['message_type'] = 'danger';
|
||
}
|
||
}
|
||
header("Location: dashboard.php");
|
||
exit;
|
||
}
|
||
|
||
if (isset($_SESSION['message'])) {
|
||
$message = $_SESSION['message'];
|
||
$message_type = $_SESSION['message_type'];
|
||
unset($_SESSION['message']);
|
||
unset($_SESSION['message_type']);
|
||
}
|
||
|
||
|
||
$stmt = db()->prepare("SELECT * FROM users WHERE id = ?");
|
||
$stmt->execute([$userId]);
|
||
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
||
|
||
if (!$user) {
|
||
session_destroy();
|
||
header("Location: login.php");
|
||
exit;
|
||
}
|
||
|
||
// Fetch all skills, excluding those the user is already enrolled in for the explorer
|
||
$enrolled_skill_ids = [];
|
||
$stmt_enrolled_ids = db()->prepare("SELECT skill_id FROM enrollments WHERE user_id = ?");
|
||
$stmt_enrolled_ids->execute([$userId]);
|
||
$enrolled_skill_ids = $stmt_enrolled_ids->fetchAll(PDO::FETCH_COLUMN);
|
||
|
||
$skills_by_category = [];
|
||
$sql = "SELECT * FROM skills";
|
||
if (!empty($enrolled_skill_ids)) {
|
||
$placeholders = implode(',', array_fill(0, count($enrolled_skill_ids), '?'));
|
||
$sql .= " WHERE id NOT IN ($placeholders)";
|
||
}
|
||
$sql .= " ORDER BY category, title";
|
||
|
||
$stmt = db()->prepare($sql);
|
||
if (!empty($enrolled_skill_ids)) {
|
||
$stmt->execute($enrolled_skill_ids);
|
||
} else {
|
||
$stmt->execute();
|
||
}
|
||
$all_skills = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||
|
||
|
||
foreach ($all_skills as $skill) {
|
||
$skills_by_category[$skill['category']][] = $skill;
|
||
}
|
||
|
||
// Fetch enrolled skills for the current user
|
||
$stmt = db()->prepare("
|
||
SELECT s.id, s.title, s.category, s.thumbnail, e.progress
|
||
FROM enrollments e
|
||
JOIN skills s ON e.skill_id = s.id
|
||
WHERE e.user_id = ?
|
||
");
|
||
$stmt->execute([$userId]);
|
||
$enrolled_skills = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||
|
||
?>
|
||
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>Dashboard - TAP2SKILL</title>
|
||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
|
||
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||
</head>
|
||
<body>
|
||
|
||
<div class="sidebar">
|
||
<a class="navbar-brand" href="index.php">TAP2SKILL</a>
|
||
<hr>
|
||
<ul class="nav flex-column">
|
||
<li class="nav-item">
|
||
<a class="nav-link active" href="dashboard.php">
|
||
<i class="bi bi-grid-1x2-fill"></i>
|
||
Dashboard
|
||
</a>
|
||
</li>
|
||
<li class="nav-item">
|
||
<a class="nav-link" href="#">
|
||
<i class="bi bi-person-circle"></i>
|
||
Profile
|
||
</a>
|
||
</li>
|
||
<li class="nav-item">
|
||
<a class="nav-link" href="#">
|
||
<i class="bi bi-gear-fill"></i>
|
||
Settings
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
<hr>
|
||
<a href="logout.php" class="btn btn-outline-primary">Logout</a>
|
||
</div>
|
||
|
||
<div class="main-content">
|
||
<div class="container-fluid">
|
||
<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">Welcome, <?php echo htmlspecialchars($user['username']); ?>!</h1>
|
||
</div>
|
||
|
||
<?php if ($message): ?>
|
||
<div class="alert alert-<?php echo $message_type; ?> alert-dismissible fade show" role="alert">
|
||
<?php echo htmlspecialchars($message); ?>
|
||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||
</div>
|
||
<?php endif; ?>
|
||
|
||
<!-- Quick Access Buttons -->
|
||
<div class="row mb-4">
|
||
<div class="col-md-4">
|
||
<a href="#skill-explorer" class="btn btn-primary btn-lg w-100">Explore Skills</a>
|
||
</div>
|
||
<div class="col-md-4">
|
||
<a href="#enrolled-skills" class="btn btn-success btn-lg w-100">Continue Learning</a>
|
||
</div>
|
||
<div class="col-md-4">
|
||
<a href="#" class="btn btn-info btn-lg w-100">Community</a>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Enrolled Skills Section -->
|
||
<section id="enrolled-skills" class="mb-5">
|
||
<h2 class="h4">My Enrolled Skills</h2>
|
||
<div class="row">
|
||
<?php if (empty($enrolled_skills)): ?>
|
||
<div class="col">
|
||
<div class="card">
|
||
<div class="card-body text-center">
|
||
<p class="card-text">You haven’t enrolled in any skills yet. Explore new skills to start learning!</p>
|
||
<a href="#skill-explorer" class="btn btn-primary">Explore Skills</a>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<?php else: ?>
|
||
<?php foreach ($enrolled_skills as $skill): ?>
|
||
<div class="col-md-6 col-lg-4 mb-3">
|
||
<div class="card">
|
||
<img src="<?php echo htmlspecialchars($skill['thumbnail']); ?>" class="card-img-top" alt="<?php echo htmlspecialchars($skill['title']); ?>">
|
||
<div class="card-body">
|
||
<h5 class="card-title"><?php echo htmlspecialchars($skill['title']); ?></h5>
|
||
<p class="card-text text-muted"><?php echo htmlspecialchars($skill['category']); ?></p>
|
||
<div class="progress mb-2">
|
||
<div class="progress-bar" role="progressbar" style="width: <?php echo $skill['progress']; ?>%;" aria-valuenow="<?php echo $skill['progress']; ?>" aria-valuemin="0" aria-valuemax="100"><?php echo $skill['progress']; ?>%</div>
|
||
</div>
|
||
<a href="learn.php?skill_id=<?php echo $skill['id']; ?>" class="btn btn-primary mt-3">Continue Learning</a>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<?php endforeach; ?>
|
||
<?php endif; ?>
|
||
</div>
|
||
</section>
|
||
|
||
<!-- Skill Explorer Section -->
|
||
<section id="skill-explorer">
|
||
<h2 class="h4">Skill Explorer</h2>
|
||
<?php if (empty($all_skills)): ?>
|
||
<div class="col">
|
||
<div class="card">
|
||
<div class="card-body text-center">
|
||
<p class="card-text">You've enrolled in all available skills. Great job!</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<?php else: ?>
|
||
<?php foreach ($skills_by_category as $category => $skills): ?>
|
||
<h3 class="h5 mt-4"><?php echo htmlspecialchars($category); ?></h3>
|
||
<div class="row">
|
||
<?php foreach ($skills as $skill): ?>
|
||
<div class="col-md-6 col-lg-3 mb-3">
|
||
<div class="card h-100">
|
||
<img src="<?php echo htmlspecialchars($skill['thumbnail']); ?>" class="card-img-top" alt="<?php echo htmlspecialchars($skill['title']); ?>">
|
||
<div class="card-body d-flex flex-column">
|
||
<h5 class="card-title"><?php echo htmlspecialchars($skill['title']); ?></h5>
|
||
<p class="card-text flex-grow-1"><?php echo htmlspecialchars($skill['description']); ?></p>
|
||
<form method="POST" action="dashboard.php" class="mt-auto">
|
||
<input type="hidden" name="enroll_skill_id" value="<?php echo $skill['id']; ?>">
|
||
<button type="submit" class="btn btn-outline-primary">Enroll</button>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<?php endforeach; ?>
|
||
</div>
|
||
<?php endforeach; ?>
|
||
<?php endif; ?>
|
||
</section>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
||
</body>
|
||
</html>
|