<?php
// admin/reset_attempts.php
session_start();
require_once "../includes/db.php";
if (!isset($_SESSION['admin'])) { header("Location: index.php"); exit; }

$sid    = isset($_GET['sid']) ? (int)$_GET['sid'] : 0;
$course = trim($_GET['course'] ?? '');
$unit   = trim($_GET['unit'] ?? '');
$type   = ($_GET['type'] ?? 'evaluation') === 'final' ? 'final' : 'evaluation';

if ($sid && $course !== '' && $unit !== '') {
  $stmt = $conn->prepare("
    DELETE r FROM results r
    JOIN exams e ON e.id = r.exam_id
    WHERE r.student_id=? AND e.course_name=? AND e.unit_exam=? AND e.exam_type=?
  ");
  $stmt->bind_param('isss', $sid, $course, $unit, $type);
  $stmt->execute();
}

header("Location: add_student.php?course_name=".urlencode($course)."&unit_exam=".urlencode($unit));
exit;
