<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/student-auth.php';
require_once __DIR__ . '/../includes/student-course-context.php';
require_once __DIR__ . '/../includes/grade-engine.php';
$pdo=db();requireStudent();$student=currentStudent($pdo);if(!$student){studentLogout();redirect('login.php');}
$courseId=(int)($_GET['course_id']??getSelectedStudentCourseId($pdo,(int)$student['id'])??0);if($courseId<=0||!isStudentEnrolledInCourse($pdo,(int)$student['id'],$courseId))redirect('student/my-courses.php');setSelectedStudentCourse($courseId);
$course=getStudentCourse($pdo,(int)$student['id'],$courseId);$rows=loadStudentGradeRows($pdo,(int)$student['id'],$courseId);$final=recalculateFinalGrade($pdo,(int)$student['id'],$courseId);$percentage=(float)($final['final_percentage']??0);
?>
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>My Grades</title><style>
*{box-sizing:border-box}body{margin:0;background:#f3f6fb;color:#0f172a;font-family:Inter,Arial,sans-serif}.page{max-width:1200px;margin:auto;padding:32px}.top{display:flex;justify-content:space-between;align-items:center;margin-bottom:24px}.top h1{font-size:46px;margin:0}.back{padding:13px 18px;border-radius:14px;background:#e0e7ff;color:#3730a3;text-decoration:none;font-weight:900}.hero{background:linear-gradient(135deg,#0f172a,#7c3aed);color:#fff;border-radius:30px;padding:34px;display:grid;grid-template-columns:1fr auto;gap:20px;align-items:center}.score{font-size:64px;font-weight:900}.hero p{color:#ddd6fe}.summary{display:grid;grid-template-columns:repeat(3,1fr);gap:15px;margin:22px 0}.box{background:#fff;border-radius:22px;padding:22px;box-shadow:0 14px 35px rgba(15,23,42,.06)}.box strong{font-size:30px}.table{background:#fff;border-radius:26px;padding:22px;overflow:auto}table{width:100%;border-collapse:collapse;min-width:760px}th,td{padding:14px;border-top:1px solid #e2e8f0;text-align:left}th{background:#f8fafc}.pill{padding:6px 10px;border-radius:999px;background:#ede9fe;color:#6d28d9;font-weight:800;font-size:12px}.bar{height:9px;background:#e2e8f0;border-radius:99px;overflow:hidden;margin-top:8px}.bar span{display:block;height:100%;background:#7c3aed}@media(max-width:700px){.page{padding:20px}.hero{grid-template-columns:1fr}.summary{grid-template-columns:1fr}.top h1{font-size:36px}}
</style></head><body><main class="page"><div class="top"><div><h1>My Grades</h1><p><?=e($course['title']??'')?></p></div><a class="back" href="dashboard.php?course_id=<?=$courseId?>">Dashboard</a></div><section class="hero"><div><h2>Overall course result</h2><p>Your result updates automatically when a teacher grades an assessment.</p></div><div class="score"><?=e(number_format($percentage,1))?>%</div></section><div class="summary"><div class="box"><small>Grade</small><br><strong><?=e($final['grade_letter']??gradeLetter($percentage))?></strong></div><div class="box"><small>GPA</small><br><strong><?=e(number_format((float)($final['gpa']??gradeGpa($percentage)),2))?></strong></div><div class="box"><small>Status</small><br><strong><?=e(ucfirst($final['pass_status']??getPassStatus($percentage)))?></strong></div></div><section class="table"><h2>Assessment details</h2><table><thead><tr><th>Assessment</th><th>Type</th><th>Score</th><th>Percentage</th><th>Feedback</th></tr></thead><tbody><?php foreach($rows as $r):$pct=(float)($r['percentage']??0);?><tr><td><strong><?=e($r['title']??'Assessment')?></strong></td><td><span class="pill"><?=e($r['item_type']??'extra')?></span></td><td><?=e(($r['score']??0).' / '.($r['max_score']??100))?></td><td><?=e(number_format($pct,1))?>%<div class="bar"><span style="width:<?=max(0,min(100,$pct))?>%"></span></div></td><td><?=e($r['feedback']??$r['notes']??'-')?></td></tr><?php endforeach;?><?php if(!$rows):?><tr><td colspan="5">No grades have been published yet.</td></tr><?php endif;?></tbody></table></section></main></body></html>
