<?php
ini_set('display_errors', 1);

function getSpecificAcademicSession($conn_rjllearning_specific,$institution_id,$session_id) {
   
    $sql = $conn_rjllearning_specific->prepare("SELECT * FROM  academic_session where id = ? and institution_id =?");
    $sql->execute(array($session_id,$institution_id));
    $rows = $sql->fetchAll();

    if (count($rows) == 1) {
        return array('session_id' => $rows[0]['id'],'institution_id' => $rows[0]['institution_id'],'name' => $rows[0]['name'],'startdate' => $rows[0]['startdate'],'enddate' =>$rows[0]['enddate'],'active_academic_session' => $rows[0]['active_academic_session']);
      
    }else{
          errorreturn("Enter Invalid Academic Session");
    }

}

function getAcademicSession($conn_rjllearning_specific, $institution_id,$search_string, $limit, $offset) {
    
    $sql = "SELECT  count(*) as total_rows FROM  academic_session  where institution_id =? and name LIKE ? ";
    $array_params = [$institution_id,'%'.$search_string.'%'];
    $stmt = $conn_rjllearning_specific->prepare($sql);
    $stmt->execute($array_params);
    $result_total = $stmt->fetchAll();
    $totalrows = $result_total[0]['total_rows'];
    
    if($limit == 'all'){
         $sql_session = "SELECT * FROM  academic_session  where institution_id =? and name LIKE ?  ";
    }else{
         $sql_session = "SELECT * FROM  academic_session  where institution_id =? and name LIKE ?  LIMIT " . $offset . " ," . $limit . " ";
    }

    $array_params = [$institution_id,'%'.$search_string.'%'];
    $stmt = $conn_rjllearning_specific->prepare($sql_session);
    $stmt->execute($array_params);
    $session_rows = $stmt->fetchAll();
  
    $array_return = [];

    foreach ($session_rows as $rows) {
        $array_return[] =  array('session_id' => $rows['id'],'institution_id' => $rows['institution_id'],'name' => $rows['name'],'startdate' => $rows['startdate'],'enddate' => $rows['enddate'],'active_academic_session' => $rows['active_academic_session']);
    }
    
    if (count($session_rows) < 50) {

        return array('total_rows' => $totalrows, 'academinc_session_info' => $array_return, 'maxreach' => 1);
    } else {
        return array('total_rows' => $totalrows, 'academinc_session_info' => $array_return, 'maxreach' => 0);
    }
}

