<?php require_once __DIR__.'/../includes/auth.php'; require_login(['staff','manager','admin']); $uid=current_user()['id']; $courseId=(int)($_GET['course']??$_POST['course_id']??0);
$stmt=db()->prepare('SELECT * FROM courses WHERE id=?'); $stmt->execute([$courseId]); $course=$stmt->fetch(); if(!$course){exit('Course not found');}
$qstmt=db()->prepare('SELECT * FROM questions WHERE course_id=? ORDER BY RAND() LIMIT 50'); $qstmt->execute([$courseId]); $questions=$qstmt->fetchAll();
if($_SERVER['REQUEST_METHOD']==='POST'){ verify_csrf(); $all=db()->prepare('SELECT * FROM questions WHERE course_id=?'); $all->execute([$courseId]); $qs=$all->fetchAll(); $total=0;$correct=0; foreach($qs as $q){ if(!isset($_POST['q_'.$q['id']])) continue; $total++; if(trim(strtolower($_POST['q_'.$q['id']]))===trim(strtolower($q['correct_answer']))) $correct++; } $score=$total?round(($correct/$total)*100):0; $status=$score >= (int)$course['pass_mark'] ? 'passed':'failed'; db()->prepare('INSERT INTO course_progress(user_id,course_id,status,score,completed_at) VALUES(?,?,?,?,NOW()) ON DUPLICATE KEY UPDATE status=VALUES(status),score=VALUES(score),completed_at=VALUES(completed_at)')->execute([$uid,$courseId,$status,$score]); if($status==='passed' && !$course['requires_practical']){ db()->prepare('INSERT IGNORE INTO certificates(user_id,course_id,certificate_code,issued_at) VALUES(?,?,?,NOW())')->execute([$uid,$courseId,'FSA-'.strtoupper(bin2hex(random_bytes(4)))]); } require __DIR__.'/../includes/header.php'; ?><div class="card"><h1>Result</h1><p class="<?= $status==='passed'?'correct':'wrong' ?>">Score: <?= $score ?>% — <?= strtoupper($status) ?></p><?php if($status==='passed'): ?><a class="btn green" href="../certificate/view.php?course=<?= $courseId ?>">View Certificate</a><?php else: ?><a class="btn" href="quiz.php?course=<?= $courseId ?>">Retry</a><?php endif; ?></div><?php require __DIR__.'/../includes/footer.php'; exit; }
require __DIR__.'/../includes/header.php'; ?>
<div class="card"><h1><?= htmlspecialchars($course['title']) ?> Assessment</h1><p>Pass mark: <?= $course['pass_mark'] ?>%</p><form method="post"><input type="hidden" name="csrf" value="<?= csrf_token() ?>"><input type="hidden" name="course_id" value="<?= $courseId ?>"><?php foreach($questions as $q): $opts=json_decode($q['options_json'],true) ?: []; ?><div class="question"><strong><?= htmlspecialchars($q['question']) ?></strong><?php if($q['image_path']): ?><img class="lesson-img" src="../<?= htmlspecialchars($q['image_path']) ?>"><?php endif; ?><?php if($q['type']==='true_false'):$opts=['True','False']; endif; ?><?php foreach($opts as $o): ?><label><input style="width:auto" type="radio" name="q_<?= $q['id'] ?>" value="<?= htmlspecialchars($o) ?>" required> <?= htmlspecialchars($o) ?></label><br><?php endforeach; ?><?php if(!$opts): ?><input name="q_<?= $q['id'] ?>" required><?php endif; ?></div><?php endforeach; ?><button>Submit Assessment</button></form></div><?php require __DIR__.'/../includes/footer.php'; ?>
