<?php
include_once "db.php";

function get_dosen_pa($npm)
{
    global $conn;
    $sql = "SELECT a.nip, b.uwp_account_first_name, b.uwp_account_last_name, b.uwp_account_username, b.uwp_account_pangkat, b.uwp_account_golongan, b.uwp_account_jabfung FROM apps_pa a, wp_uwp_usermeta b WHERE a.npm = '$npm' AND a.nip=b.uwp_account_username";
    
    $results = $conn->query($sql)->fetch_assoc();
    return $results;
}

function get_profile_by_username($username) {
    global $conn;
    $sql = "SELECT * FROM wp_uwp_usermeta WHERE uwp_account_username = '".$username."'";
    $results = $conn->query($sql)->fetch_assoc();
    return $results;
}

function get_form_by_login_ID($user_login, $ID)
{
    global $conn;
    $sql = "SELECT a.*, b.ID_Form, b.Kode, DATE(b.Created_at) as tgl
        FROM apps_user_layananmeta a, apps_user_layanan2 b 
        WHERE a.id_user_layanan = b.ID AND b.NPM = '$user_login' AND a.id_user_layanan = $ID";
    //echo $sql;
    $results = $conn->query($sql);
    $data = array();
    while($row = $results->fetch_assoc())
    {
        $data['id_user_layanan'] = $row['id_user_layanan'];
        $data['id_form'] = $row['ID_Form'];
        $data['kode'] = $row['Kode'];
        $data['tgl'] = $row['tgl'];
        $data[$row['meta_key']] = $row['meta_value'];
    }
    return $data;
}

function convert_date($input)
{
    $date = explode("-", $input);
    $d = $date[2]." ";
    $m = "";
    $y = $date[0];
    switch($date[1])
    {
        case '01': $m = "Januari"; break;
        case '02': $m = "Februari"; break;
        case '03': $m = "Maret"; break;
        case '04': $m = "April"; break;
        case '05': $m = "Mei"; break;
        case '06': $m = "Juni"; break;
        case '07': $m = "Juli"; break;
        case '08': $m = "Agustus"; break;
        case '09': $m = "September"; break;
        case '10': $m = "Oktober"; break;
        case '11': $m = "November"; break;
        case '12': $m = "Desember"; break;
    }
    
    return $d." ".$m." ".$y;
}

function get_prodi_from_npm($npm)
{
    // 0517032014
    $y = substr($npm, 0, 2);
    $y_now = date('y');
    $interval = $y_now - $y;
    if(date('m') == 1)
    {
        $sem = $interval * 2 - 1;
    }
    elseif(date('m') > 1 && date('m') < 8)
    {
        $sem = $interval * 2;
    }
    else
    {
        $sem = $interval * 2 + 1;
    }
    
    $level = substr($npm, 2, 1);
    $jur = substr($npm, 5, 1);
    $pro = substr($npm, 6, 1);
    $str_jur = "";
    $str_prod = "";
    //echo $level."-".$jur."-".$pro;
    
    if($jur == 1)
    {
        $str_jur = "Kimia";
        $str_prod = "S".$level." ".$str_jur;
    }
    elseif($jur == 2)
    {
        $str_jur = "Biologi";
        if($pro == 1) 
        {
            $str_prod = "S".$level." ".$str_jur;
        }
        elseif($pro == 2) 
        {
            $str_prod = "S".$level." ".$str_jur." Terapan";
        }
    }
    elseif($jur == 3)
    {
        $str_jur = "Matematika";
        
        if($pro == 1)
        {
            $str_prod = "S".$level." ".$str_jur;
        }
        elseif($pro == 2)
        {
            $str_prod = "S".$level." Ilmu Komputer";
        }
    }
    elseif($jur == 4)
    {
        $str_jur = "Fisika";
        $str_prod = "S".$level." ".$str_jur;
    }
    elseif($jur == 5)
    {
        $str_jur = "Ilmu Komputer";
        if($level == 0)
        {
            $str_prod = "D3 Manajemen Informatika";
        }
        else
        {
            $str_prod = "S".$level." ".$str_jur;
        }
    }
    elseif($jur == 6)
    {
        $str_prod = "Doktor MIPA";
    }
    
    $result = array(
            'jurusan' => $str_jur,
            'prodi' => $str_prod,
            'semester' => $sem
        );
        
    return $result;
}

?>