<?php
require_once(LIB_PATH . DS . 'database.php');

class User
{
    protected static $table_name = "user_profile";
    public $id;
    public $picture;
   
    public $title;
    public $staffno;
    
    public $surname;
    public $othernames;
    public $username;
    public $password;
    public $question;
    public $department;
    public $designation;
    public $additional;

    public $found;
    public $answer;

    public $email;
    public $access;
    public $status;



public static function updateacc($staffno='',$email='',$password=''){

  global $database;
        $sql = "UPDATE " . self::$table_name . " SET password= '{$password}' WHERE staffno='{$staffno}' AND email='{$email}' LIMIT 1";
        $database->query($sql);
        return ($database->affected_rows() == 1) ? true : false;

//$sql .= "email ='". $database->escape_value($this->email)."', ";

}




public static function status ($key=""){

  global $database;
  
      $sql = "SELECT * FROM accesstbl WHERE access='{$key}' AND status= 1 LIMIT 1 ";
      $result_array = self::find_by_sql($sql);
    return !empty($result_array) ? array_shift($result_array) : false;

}


public static function check_st(){
    global $database;
   // $staffno = $database->escape_value($staffno);

    $sql = "SELECT id FROM " . self::$table_name . " ORDER BY id DESC LIMIT 0,1 ";
   // $sql .= " WHERE staffno='{$staffno}' ";
        
     $result_array = self::find_by_sql($sql);
    return !empty($result_array) ? array_shift($result_array) : false;


}

    public static function lu($id="",$email="",$question="",$answer="")
    {
        global $database;
        $id = $database->escape_value($id);
        $email = $database->escape_value($email);
        $question = $database->escape_value($question);
        $answer = $database->escape_value($answer);
    
        $sql = "SELECT count(*) as found FROM " . self::$table_name . " ";
        $sql .= " WHERE id= $id AND email='{$email}' ";
        $sql .= "AND question='{$question}' AND answer='{$answer}'  LIMIT 1";
         
        $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;
//return $result_array;
    }


    public static function authenticate($email = "", $password = "")
    {
        global $database;
        $username = $database->escape_value($email);
        $password = $database->escape_value($password);
    //     $logas = $database->escape_value($logas);
    //     if ($logas=='hod'){
    //         $logas='Head of Department'
    //     }else if ($logas=='lecturer'){
    //     $logas='principal_lecturer' or $logas='chief_lecturer' or $logas='lecturer1' or $logas='lecturer2' or $logas='lecturer3'
    // }
        $sql = "SELECT * FROM " . self::$table_name . " ";
        $sql .= " WHERE email='{$email}' ";
        $sql .= "AND password='{$password}'  LIMIT 1";
         
        $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;
//return $result_array;
    }

    public function fullname()
    {
        global $database;
        if (isset($this->title) && isset($this->surname) && isset($this->othernames)) {
            return ucfirst($this->title)." ".strtoupper($this->surname)."," . " " . ucfirst($this->othernames);
        } else {
            return "";
        }
    }
    public static function insert_user ($picture='', $title='', $staffno='', $surname='',$othernames='',$department='',$phone='', $designation='',$email='',$question='', $answer='', $password=''){
        global $database;
        $picture = $database->escape_value($picture);
        $title = $database->escape_value($title);
        $staffno = $database->escape_value($staffno);
        $surname = $database->escape_value($surname);
        $othernames = $database->escape_value($othernames);
        $department = $database->escape_value($department);
         $phone = $database->escape_value($phone);
        $designation = $database->escape_value($designation);
        $email = $database->escape_value($email);

        $question = $database->escape_value($question);
        $answer = $database->escape_value($answer);
        $password = $database->escape_value($password);
       // $additional = $database->escape_value($additional);
        $sql = "INSERT INTO " . self::$table_name . " (";
        $sql .= "picture, title, staffno, surname,othernames,department,phone, designation,email,question,answer,password";

        $sql .= ") VALUES ('";
        $sql .= $picture . "', '";
        $sql .= $title . "', '";
         $sql .= $staffno . "', '";
        $sql .= $surname . "', '";
        $sql .= $othernames . "', '";
        $sql .= $department . "', '";
        $sql .= $phone . "', '";
        $sql .= $designation . "', '";
        
        $sql .= $email . "', '";
        $sql .= $question . "', '";
        $sql .= $answer . "', '";
        $sql .= $password . "'
       
            )";

        if ($database->query($sql)) {
            //$this->id=$database->insert_id();
            return true;
          
        } else {
            return false;
        }


    }

   
}

//Common database Methods
 $User = new User();
?>