<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

function generate_unique_no() {
    $ci = &get_instance();
    $unique = mt_rand(1000000, 9999999999);
    return $unique;

}



function checkPassword($pwd) {
    $errors = [];

    if (strlen($pwd) < 6) {
        $errors[] = "Password too short!";
    }

    if (!preg_match("#[0-9]+#", $pwd)) {
        $error[] = "Password must include at least one number!";
    }

    if (!preg_match("#[A-Z]+#", $pwd)) {
        $errors[] = "Password must include at least one uppercase letter!";
    }

    return $errors;
}

function passwordChange(){

  $ci = &get_instance();

  $change_status = $ci->session->userdata('password_change');

  if($change_status == 1){
    return true;
  } else {
    redirect(base_url('index.php/ChangePassword'));
  }

}

function get_user_name($userId) {
    $ci = &get_instance();
    if ($userId != 1) {
        $sql = "SELECT 	full_name FROM User_Master WHERE id =".$userId;
        $query = $ci->db->query($sql);
        $result = $query->result_array();
        //echo"<pre>".$userId;print_r($result);exit;
        if (count($result) > 0) {
            return $result[0]['full_name'];
        } else
            return "-";

    } else {
        return "Administrator";
    }

}

function get_encript_id($pass) {
    $id_encode = base64_encode($pass);
    return urlencode($id_encode);
}

function get_decript_id($pass) {
    $id_encode = urldecode($pass);
    return base64_decode($id_encode);
}


function check_login() {
    $ci = &get_instance();
    if ($ci->session->userdata('admin_login_session') == "true") {
        return "true";
    } else {
        redirect(base_url().
            "Login");
    }
}

function isAdmin()
{
  $ci = &get_instance();
  $usrLvl = $ci->session->userdata('admin_lvl');
  if($usrLvl == 1 && $ci->session->userdata('admin_login_session') == "true")
  {
    return "true";
  }
  else
  {
    redirect(base_url()."Login");
  }
}

function isSubAdmin()
{
  $ci = &get_instance();
  $usrLvl = $ci->session->userdata('admin_lvl');
  if($usrLvl == 2 && $ci->session->userdata('admin_login_session') == "true")
  {
    return "true";
  }
  else
  {
    redirect(base_url()."Login");
  }
}

function isFaculty()
{
  $ci = &get_instance();
  $usrLvl = $ci->session->userdata('admin_lvl');
  if($usrLvl == 3 && $ci->session->userdata('admin_login_session') == "true")
  {
    return "true";
  }
  else
  {
    redirect(base_url()."Login");
  }
}

function isStudent()
{
  $ci = &get_instance();
  $usrLvl = $ci->session->userdata('admin_lvl');
  if($usrLvl == 4 && $ci->session->userdata('admin_login_session') == "true")
  {
    return "true";
  }
  else
  {
    redirect(base_url()."Login");
  }
}

function isPublicUser()
{
  $ci = &get_instance();
  $usrLvl = $ci->session->userdata('admin_lvl');
  if($usrLvl == 5 && $ci->session->userdata('admin_login_session') == "true")
  {
    return "true";
  }
  else
  {
    redirect(base_url()."Login");
  }
}





function userDate($date_form) // d-m-y to y-m-d
{

    if ($date_form == '') {
        $dateformat = '';

        return $dateformat;

    } else if ($date_form == '0000-00-00') {

        $dateformat = 'N/A';

        return $dateformat;

    } else {

        $date1 = explode("-", $date_form);

        $dateformat = $date1[2].
        "/".$date1[1].
        "/".$date1[0];

        return $dateformat;

    }

}



function getNameTable($table, $col, $field = '', $value = '', $field1 = '', $value1 = '') {
    $ci = &get_instance();
    $query = "SELECT ".$col.
    " FROM ".$table.
    " where 1 ";
    if ($field != '' && $value != '') {
        $query .= "AND ".$field.
        "='".$value.
        "' ";
    }
    if ($field1 != '' && $value1 != '') {
        $query .= "AND ".$field1.
        "='".$value1.
        "' ";
    }
    $recordSet = $ci->db->query($query);
    if ($recordSet->num_rows() > 0) {
        $row = $recordSet->row_array();
        return $row[$col];
    } else {
        return "";
    }
}



function checkNull($data) {
    if ($data != '') {
        $return_data = $data;
    } else {
        $return_data = 'N/A';
    }

    return $return_data;
}


function last_login($login_time) {
    if ($login_time == '0000-00-00 00:00:00') {
        $return = 'N/A';
    } else {
        $return = date('d-m-Y h:i A', strtotime($login_time));
    }

    return $return;
}



function get_status($status) {
    if ($status == 1) {
        $return = 'Active';
    } else if ($status == 0) {
        $return = 'Inactive';
    } else {
        $return = 'N/A';
    }
    return $return;
}


?>
