<?php 
$kudi_username='azizaacademy@gmail.com';
$kudi_password='@z1z44c4d3my';
$kudi_sender='Aziza Acdmy';


function smsBalance()
{
    GLOBAL $kudi_username;
    GLOBAL $kudi_password;
    // $GLOBALS['kudi_password'];
    $url = "https://account.kudisms.net/api/?username=$kudi_username&password=$kudi_password&action=balance";
                    
    
    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
     

    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST,1);//1 for post
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_vals);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_VERBOSE,true); 
    //So that curl_exec returns the contents of the cURL; rather than echoing it
    // curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    // curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    //     "Authorization: Bearer \$key_ar",
    //     "Cache-Control: no-cache",
    //     'Content-Type: application/json',
    //     ));
    //execute post
    $result = curl_exec($ch);
    $decodedRes = json_decode($result, true);
    return '₦'.$decodedRes['balance'];
}
function daysInSchool($from,$to)
{
    $days_count=0;
    $dr=dates_range($from,$to);
    for($i=0;$i<=$dr;$i++)
    {
        // echo $i.' '.days_todate($from,$i).'-';
        // echo strtolower(date("D",strtotime(days_todate($from,$i)))).'<br>';
        // array_push($dates,days_todate($from,$i));
        if(strtolower(date("D",strtotime(days_todate($from,$i))))!="sat" && strtolower(date("D",strtotime(days_todate($from,$i))))!="sun")
        {
            $days_count++;
        }
    }

    return $days_count;
}
function dates_range($date1,$date2)
{
    // $date1="2020-08-01";
    // $date2="2020-08-02";
    // $d1=new DateTime($date1);
    // $d2=new DateTime($date2);

    // $day=$d1->diff($d2);
    // if(strtotime($date1)<strtotime($date2))
    // {
    //     return $day->format('%d');
    // }
    // else
    // {
    //     return $day->format('%d')*-1;
    // }
    $diff=(strtotime($date2)-strtotime($date1));
    return round($diff/(60*60*24));
}
//Add days to date and return new date 
function days_todate($date,$days)
{
    return date('Y-m-d', strtotime($date.' + '.$days.' days'));
}

//Check if date found in range
function find_inrange($from,$to,$date)
{

    $dates=array();
    $dr=dates_range($from,$to);
    for($i=0;$i<=$dr;$i++)
    {
        // echo days_todate('2020-08-02',$i).'<br>';
        array_push($dates,days_todate($from,$i));
    }
    //check if found
    if(in_array($date,$dates))
    {
        return true;
    }
    else
    {
        return false;
    }
}

// Date count 
function monthDifference($start_date,$end_date)
{
    $start_date = strtotime($start_date); 
    $end_date = strtotime($end_date); 
  
    // Get the difference and divide into  
    // total no. seconds 60/60/24 to get
    // number of days, /30 = months
    return intval((($end_date - $start_date)/60/60/24)/30);
}

function toDate($getdate, $monthCOunt)
{
    $date = new DateTime($getdate);//DateTime('now');
    $date->modify('+'.$monthCOunt.' month'); // or you can use '-90 day' for deduct
    $date = $date->format('Y-m-d');// h:i:s
    return $date;
    
}
function monthInRange($getdate, $getdate2, $findmonth)
{
    $months=array();
    $interval=monthDifference($getdate,$getdate2);
    
    for($i=0;$i<=$interval;$i++)
    {//DateTime('now');
        $date = new DateTime($getdate);
        $date->modify("+$i month"); // or you can use '-90 day' for deduct
        $date = $date->format('Y-m');// h:i:s
        array_push($months,$date);
    }
    if(in_array(date("Y-m",strtotime($findmonth)),$months))
    {
        return true;

    }
    else
    {
        return false;
    }
    
    
}
function currentTerm($conn="../manage/connection.php")
{
    include $conn;
    $year=date('Y');
    $today=date('Y-m-d');
    $term=null;
    $getSession=$connect->query("SELECT * FROM session WHERE year='$year'");
    while($session=$getSession->fetch_assoc())
    {
        if(find_inrange($session['begin'],$session['end'],$today))
        {
            $term=$session['term'];
        }
    }
    return $term;
}

function currentSession($conn="../manage/connection.php")
{
    include $conn;
    $year=date('Y');
    $today=date('Y-m-d');
    $csession=array();
    $getSession=$connect->query("SELECT * FROM session WHERE year='$year'");
    while($session=$getSession->fetch_assoc())
    {
        if(find_inrange($session['begin'],$session['end'],$today))
        {
            $csession=array("id"=>$session['id'], "title"=>$session['term']."  term ".$session['year']);;
        }
    }
    return $csession;
}

function grade($mark)
{
    $grade='F';
    $comment='Fail';
    if($mark>=70 && $mark<=100)
    {
        $grade='A';
        $comment='Excellent';
    }
    elseif($mark>=60 && $mark<70)
    {
        $grade='B';
        $comment='Very Good';
    }
    elseif($mark>=50 && $mark<60)
    {
        $grade='C';
        $comment='Good';
    }
    elseif($mark>=45 && $mark<50)
    {
        $grade='D';
        $comment='Pass';
    }
    elseif($mark>=40 && $mark<=44)
    {
        $grade='E';
        $comment='Fair';
    }
    elseif($mark<44)
    {
        $grade='F';
        $comment='Fail';
    }
    else
    { 
        $grade='?';
        $comment='Invalid Result';
    }
    return array("grade"=>$grade, "comment"=>$comment);
}
function posInfo($val)
{
    if($val==1)
    {
        return "$val<sup>st</sup>";
    } 
    if($val==2)
    {
        return "$val<sup>nd</sup>";
    } 
    if($val==3)
    {
        return "$val<sup>rd</sup>";
    }
    elseif($val>3 && $val<=20)
    {
        return "$val<sup>th</sup>";
    }
    elseif($val==21)
    {
        return "$val<sup>st</sup>";
    }
    elseif($val==22)
    {
        return "$val<sup>nd</sup>";
    }
    elseif($val==23)
    {
        return "$val<sup>nd</sup>";
    }
    elseif($val>23 && $val<=30)
    {
        return "$val<sup>th</sup>";
    }
    else
    {
        return "$val<sup></sup>";
    }
}
?>