2.0
This commit is contained in:
parent
a60b54ddf4
commit
db1d5e038b
@ -8,6 +8,42 @@ if (!isset($_SESSION['user_id'])) {
|
||||
}
|
||||
|
||||
$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);
|
||||
@ -18,11 +54,29 @@ if (!$user) {
|
||||
exit;
|
||||
}
|
||||
|
||||
// Fetch all skills
|
||||
// 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 = [];
|
||||
$stmt = db()->query("SELECT * FROM skills ORDER BY category, title");
|
||||
$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;
|
||||
}
|
||||
@ -83,6 +137,13 @@ $enrolled_skills = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
<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">
|
||||
@ -132,6 +193,15 @@ $enrolled_skills = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
<!-- 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">
|
||||
@ -142,13 +212,17 @@ $enrolled_skills = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
<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>
|
||||
<a href="#" class="btn btn-outline-primary mt-auto">Enroll</a>
|
||||
<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>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user