<?php
// admin/courses/toggle_semester.php
session_start();
require_once "../../includes/db.php";

// normalize DB connection
if (isset($mysqli) && $mysqli instanceof mysqli) {
    $conn = $mysqli;
} elseif (!isset($conn) || !($conn instanceof mysqli)) {
    die("Database connection not available");
}

if (isset($_POST['semester_id'], $_POST['release'])) {
    $semester_id = (int) $_POST['semester_id'];
    $release     = (int) $_POST['release'];
    $course_id   = (int) $_POST['course_id'];

    $stmt = $conn->prepare("UPDATE lms_semesters SET is_released=? WHERE id=?");
    $stmt->bind_param("ii", $release, $semester_id);
    $stmt->execute();
}

header("Location: semesters.php?course_id=$course_id");
exit;
