189 lines
10 KiB
PHP
189 lines
10 KiB
PHP
<?php
|
|
session_start();
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
require_once 'db/config.php';
|
|
$db = db();
|
|
|
|
$skill_id = $_GET['skill_id'] ?? 0;
|
|
$user_id = $_SESSION['user_id'];
|
|
|
|
// Handle marking a lesson as complete
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['complete_lesson'])) {
|
|
$lesson_id = $_POST['lesson_id'];
|
|
|
|
// Check if already completed
|
|
$stmt = $db->prepare("SELECT id FROM lesson_completion WHERE user_id = ? AND lesson_id = ?");
|
|
$stmt->execute([$user_id, $lesson_id]);
|
|
if ($stmt->fetch()) {
|
|
// Already completed, do nothing
|
|
} else {
|
|
// Insert into lesson_completion
|
|
$stmt = $db->prepare("INSERT INTO lesson_completion (user_id, lesson_id) VALUES (?, ?)");
|
|
$stmt->execute([$user_id, $lesson_id]);
|
|
|
|
// Recalculate progress
|
|
$total_lessons_stmt = $db->prepare("SELECT COUNT(*) FROM lessons l JOIN modules m ON l.module_id = m.id WHERE m.skill_id = ?");
|
|
$total_lessons_stmt->execute([$skill_id]);
|
|
$total_lessons = $total_lessons_stmt->fetchColumn();
|
|
|
|
$completed_lessons_stmt = $db->prepare("SELECT COUNT(*) FROM lesson_completion lc JOIN lessons l ON lc.lesson_id = l.id JOIN modules m ON l.module_id = m.id WHERE lc.user_id = ? AND m.skill_id = ?");
|
|
$completed_lessons_stmt->execute([$user_id, $skill_id]);
|
|
$completed_lessons = $completed_lessons_stmt->fetchColumn();
|
|
|
|
$progress = ($total_lessons > 0) ? round(($completed_lessons / $total_lessons) * 100) : 0;
|
|
|
|
// Update enrollments table
|
|
$update_stmt = $db->prepare("UPDATE enrollments SET progress = ? WHERE user_id = ? AND skill_id = ?");
|
|
$update_stmt->execute([$progress, $user_id, $skill_id]);
|
|
}
|
|
header("Location: learn.php?skill_id=$skill_id");
|
|
exit;
|
|
}
|
|
|
|
|
|
// Fetch skill details
|
|
$stmt = $db->prepare("SELECT * FROM skills WHERE id = ?");
|
|
$stmt->execute([$skill_id]);
|
|
$skill = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$skill) {
|
|
die('Skill not found!');
|
|
}
|
|
|
|
// Fetch completed lesson IDs for the user
|
|
$completed_lessons_stmt = $db->prepare("SELECT lesson_id FROM lesson_completion WHERE user_id = ?");
|
|
$completed_lessons_stmt->execute([$user_id]);
|
|
$completed_lesson_ids = $completed_lessons_stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
// Fetch modules and lessons
|
|
$modules_stmt = $db->prepare("SELECT * FROM modules WHERE skill_id = ? ORDER BY `order` ASC");
|
|
$modules_stmt->execute([$skill_id]);
|
|
$modules = $modules_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$lessons_stmt = $db->prepare("SELECT * FROM lessons WHERE module_id = ? ORDER BY `order` ASC");
|
|
|
|
// Calculate progress
|
|
$total_lessons_stmt = $db->prepare("SELECT COUNT(*) FROM lessons l JOIN modules m ON l.module_id = m.id WHERE m.skill_id = ?");
|
|
$total_lessons_stmt->execute([$skill_id]);
|
|
$total_lessons = $total_lessons_stmt->fetchColumn();
|
|
$completed_lessons = count($completed_lesson_ids);
|
|
$progress = ($total_lessons > 0) ? round(($completed_lessons / $total_lessons) * 100) : 0;
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Learning: <?php echo htmlspecialchars($skill['title']); ?></title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
<div class="container mt-5">
|
|
<a href="dashboard.php" class="btn btn-secondary mb-4">Back to Dashboard</a>
|
|
<h1><?php echo htmlspecialchars($skill['title']); ?></h1>
|
|
<p class="text-muted"><?php echo htmlspecialchars($skill['category']); ?></p>
|
|
|
|
<!-- Progress Bar -->
|
|
<div class="progress my-4" style="height: 25px;">
|
|
<div class="progress-bar bg-success" role="progressbar" style="width: <?php echo $progress; ?>%;" aria-valuenow="<?php echo $progress; ?>" aria-valuemin="0" aria-valuemax="100"><?php echo $progress; ?>% Complete</div>
|
|
</div>
|
|
|
|
<hr>
|
|
<p><?php echo nl2br(htmlspecialchars($skill['description'])); ?></p>
|
|
|
|
<div class="mt-4">
|
|
<h3>Course Content</h3>
|
|
<div class="accordion" id="modulesAccordion">
|
|
<?php if (empty($modules)): ?>
|
|
<p><em>(Content for this course will be added soon.)</em></p>
|
|
<?php else: ?>
|
|
<?php foreach ($modules as $module): ?>
|
|
<div class="accordion-item">
|
|
<h2 class="accordion-header" id="heading<?php echo $module['id']; ?>">
|
|
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse<?php echo $module['id']; ?>" aria-expanded="false" aria-controls="collapse<?php echo $module['id']; ?>">
|
|
<?php echo htmlspecialchars($module['title']); ?>
|
|
</button>
|
|
</h2>
|
|
<div id="collapse<?php echo $module['id']; ?>" class="accordion-collapse collapse" aria-labelledby="heading<?php echo $module['id']; ?>" data-bs-parent="#modulesAccordion">
|
|
<div class="accordion-body">
|
|
<?php
|
|
$lessons_stmt->execute([$module['id']]);
|
|
$lessons = $lessons_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
?>
|
|
<?php if (empty($lessons)): ?>
|
|
<p>No lessons in this module yet.</p>
|
|
<?php else: ?>
|
|
<ul class="list-group">
|
|
<?php foreach ($lessons as $lesson):
|
|
$is_completed = in_array($lesson['id'], $completed_lesson_ids);
|
|
?>
|
|
<li class="list-group-item d-flex justify-content-between align-items-center <?php echo $is_completed ? 'list-group-item-light text-muted' : ''; ?>">
|
|
<div>
|
|
<h5>
|
|
<?php if ($is_completed): ?>
|
|
<i class="bi bi-check-circle-fill text-success"></i>
|
|
<?php endif; ?>
|
|
<?php echo htmlspecialchars($lesson['title']); ?>
|
|
</h5>
|
|
<p><?php echo nl2br(htmlspecialchars($lesson['content'])); ?></p>
|
|
</div>
|
|
<?php if (!$is_completed): ?>
|
|
<form method="POST" action="learn.php?skill_id=<?php echo $skill_id; ?>">
|
|
<input type="hidden" name="lesson_id" value="<?php echo $lesson['id']; ?>">
|
|
<button type="submit" name="complete_lesson" class="btn btn-sm btn-success">Mark as Complete</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
</li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<?php
|
|
// Fetch last quiz attempt
|
|
$quiz_attempt_stmt = $db->prepare("SELECT * FROM quiz_attempts WHERE user_id = ? AND skill_id = ? ORDER BY completed_at DESC LIMIT 1");
|
|
$quiz_attempt_stmt->execute([$user_id, $skill_id]);
|
|
$quiz_attempt = $quiz_attempt_stmt->fetch(PDO::FETCH_ASSOC);
|
|
?>
|
|
|
|
<?php if ($progress >= 100): ?>
|
|
<div class="card my-4">
|
|
<div class="card-body text-center">
|
|
<?php if ($quiz_attempt): ?>
|
|
<h4 class="card-title">Quiz Result</h4>
|
|
<p class="card-text">You scored <strong><?php echo $quiz_attempt['score']; ?>%</strong> on your last attempt.</p>
|
|
<?php if ($quiz_attempt['score'] >= 70): ?>
|
|
<a href="certificate.php?skill_id=<?php echo $skill_id; ?>" class="btn btn-success">
|
|
<i class="bi bi-patch-check-fill"></i> View Certificate
|
|
</a>
|
|
<?php else: ?>
|
|
<p class="text-danger">You need a score of 70% or higher to get a certificate.</p>
|
|
<a href="quiz.php?skill_id=<?php echo $skill_id; ?>" class="btn btn-primary">
|
|
<i class="bi bi-arrow-clockwise"></i> Retake Quiz
|
|
</a>
|
|
<?php endif; ?>
|
|
<?php else: ?>
|
|
<h4 class="card-title">Ready to test your knowledge?</h4>
|
|
<p class="card-text">You have completed all the lessons. It's time to take the quiz!</p>
|
|
<a href="quiz.php?skill_id=<?php echo $skill_id; ?>" class="btn btn-primary btn-lg">Take Quiz</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
</div>
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|