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

//require_once(LIB_PATH.DS.'user_data.php');
class Userdata extends DatabaseObject
{
    protected static $table_name = "student_activity_level";
    public $id;
    public $formno;
    public $matricno;
    public $email;
    public $std_process_id;
    public $adm_date;
    public $status;
    public $start_session;
    public $session; 


     public static function find_by_remita($formno='')
    {
        global $database;
        
        return 'Ghfffffffddd';
        
      if (substr($formno,2,2)=='19'){
            return '19Defdddddhtttt';
            $table ="admitted_student_details_2019";
            $table="admission_list";
              $sql ="SELECT a.pix,a.surname,a.othernames,a.formno,a.level,a.email,a.phone,p.department FROM  admitted_student_details_2019 a INNER JOIN admission_list p ON a.formno=p.formno WHERE p.formno='{$formno}' and a.email='{$email}' LIMIT 1";
           
      }else if (substr($formno,2,2)>= '23'){ 
           
          return 'Defdddddhtttt';
      $sql ="SELECT * FROM admission_list WHERE formno='{$formno}' LIMIT 1";
      }
        
        $result_array = self::find_by_sql($sql);
        //return !empty($result_array) ? array_shift($result_array) : false;

    }
    




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

public static function mat($formno = "", $email = "")
    {
        global $database;
        $formno = $database->escape_value($formno);
        $email = $database->escape_value($email);
        $sql = "SELECT * FROM " . self::$table_name . " ";
        $sql .= " WHERE formno='{$formno}' ";
        $sql .= "AND email='{$email}' ORDER BY matricno";
        return self::find_by_sql($sql);
        // return !empty($result_array) ? array_shift($result_array) : false;
//return $result_array;
    }
    
// public static function mat($formno = "", $email = ""){
// global $database;
// $sql = "SELECT student_activity_level.matricno,admitted_student_details.surname, admitted_student_details.othernames from student_activity_level JOIN admitted_student_details ON admitted_student_details.formno =student_activity_level.formno
//     WHERE admitted_student_details.formno = '{$formno}' AND admitted_student_details.email = '{$email}' order by matricno limit 1";
//     return self::find_by_sql($sql);
// }

    public function fullname()
    {

        if (isset($this->formno) && isset($this->username)) {
            return $this->formno . " " . $this->username;
        } else {
            return "";
        }
    }


//Common database Methods
    public static function find_all()
    {
        global $database;
        return self::find_by_sql("SELECT * FROM " . self:: $table_name);
        //return $result_set;

    }


// ............................Allow access for payment........................

    public static function Allow_access($status ="",$formno = "", $email = "")
    {
        global $database;
        $sql = "SELECT * FROM " . self::$table_name . " ";
        $sql .= " WHERE formno='{$formno}' AND status='{$status}'";
        $sql .= "AND email='{$email}' LIMIT 1";
    }

// ............................................................................


    public static function find_by_id($id = 0)
    {
        global $database;
        $result_array = self::find_by_sql("SELECT * FROM  " . self::$table_name . " WHERE id={$id} LIMIT 1");
        //$found = $database->fetch_array($result_set);
        //return $found;
        return !empty($result_array) ? array_shift($result_array) : false;

    }

    public static function find_by_sql($sql = "")
    {
        global $database;
        $result_set = $database->query($sql);
        $object_array = array();
        while ($row = $database->fetch_array($result_set)) {
            $object_array[] = self::instantiate($row);
        }
        return $object_array;

    }

    private static function instantiate($record)
    {

        $object = new self;
        foreach ($record as $attribute => $value) {
            if ($object->has_attribute($attribute)) {
                $object->$attribute = $value;
            }

        }
        return $object;
    }

    private function has_attribute($attribute)
    {

        $object_vars = get_object_vars($this);
        return array_key_exists($attribute, $object_vars);
    }

    public function save()
    {
        return isset($this->id) ? $this->update() : $this->create();
    }

    public function create()
    {
        global $database;
        $sql = "INSERT INTO " . self::$table_name . " (";
        $sql .= "formno, email";
        $sql .= ") 	VALUES ('";
        $sql .= $database->escape_value($this->formno) . "', '";
        $sql .= $database->escape_value($this->email) . "') '";
        if ($database->query($sql)) {
            $this->id = $database->insert_id();
            return true;
        } else {
            return false;
        }

    }

    public static function update($id, $level)
    {

        global $database;
        $sql = "UPDATE " . self::$table_name . " SET ";
        $sql .= "std_process_id = " . ($level) . " ";
        /* $sql .= "status = " . 'closed' . " ";*/
        $sql .= "WHERE id=" . $id;

        $database->query($sql);
        return ($database->affected_rows() == 1) ? true : false;

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

    public function delete()
    {

        global $database;
        $sql = "DELETE FROM " . self::$table_name . " ";
        $sql .= "WHERE id=" . $database->escape_value($this->id);
        $sql .= " LIMIT 1";
        $database->query($sql);
        return ($database->affected_rows() == 1) ? true : false;
    }

//Closing tags for user's class
}

$user_data = new Userdata();
?>