<?php
function isLoggedIn() {
    return isset($_SESSION['user_id']) && !empty($_SESSION['user_id']);
}

function isAdmin() {
    return isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin';
}

function isAdminSoal() {
    return isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin_soal';
}

function isPenguji() {
    return isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'penguji';
}

function isReviwer() {
    return isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'reviewer';
}

function redirect($url) {
    // Clean any output buffers
    while (ob_get_level()) {
        ob_end_clean();
    }
    
    // Simple redirect
    header("Location: $url");
    exit;
}
    
   

function formatTanggal($date) {
    if (empty($date) || $date == '0000-00-00') return '-';
    return date('d/m/Y', strtotime($date));
}


function sanitize($data) {
    if (is_array($data)) {
        return array_map('sanitize', $data);
    }
    return htmlspecialchars(trim($data), ENT_QUOTES, 'UTF-8');
}

function formatWaktu($datetime) {
    if (empty($datetime) || $datetime == '0000-00-00 00:00:00') return '-';
    return date('H:i', strtotime($datetime));
}

function formatDateTime($datetime) {
    if (empty($datetime) || $datetime == '0000-00-00 00:00:00') return '-';
    return date('d/m/Y H:i', strtotime($datetime));
}

function getNamaHari($date) {
    if (empty($date) || $date == '0000-00-00') return '-';
    
    $hari = array(
        'Sunday' => 'Minggu',
        'Monday' => 'Senin', 
        'Tuesday' => 'Selasa',
        'Wednesday' => 'Rabu',
        'Thursday' => 'Kamis',
        'Friday' => 'Jumat',
        'Saturday' => 'Sabtu'
    );
    $day = date('l', strtotime($date));
    return $hari[$day] ?? $day;
}
?>