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

class User 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 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 matricno='{$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 authenticatematric($formno = "", $email = "")
    {
        global $database;
        // $formno = $database->escape_value($formno);
        // $email = $database->escape_value($email);
        
        $sql = "SELECT * FROM " . self::$table_name . " ";
        $sql .= " WHERE matricno='{$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 authenticateform($formno = "", $email = "")
    {
        global $database;
        $formno = $database->escape_value($formno);
        $email = $database->escape_value($email);
       // $table ="admitted_student_details_2019";
        $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;
   
    }
    
    // public static function authenticate1($formno = '', $email = '')
    // {
    //     global $database;
        
    //     if (substr($formno,2,2)=='19'){
    //         $table ="admitted_student_details_2019";
    //          $result_array = self::find_by_sql("SELECT * FROM  $table WHERE formno='{$formno}' and email='{$email}' LIMIT 1");
    //     }else {
    //     $result_array = self::find_by_sql("SELECT * FROM  " . self::$table_name . " WHERE matricno='{$formno}' and email='{$email}' LIMIT 1");
    //     }
    //     return !empty($result_array) ? array_shift($result_array) : false;
    // }

    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;

    }

    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 function update()
    {

        global $database;
        $sql = "UPDATE " . self::$table_name . "(";
        $sql .= "formno = '" . $database->escape_value($this->formno) . "',";
        $sql .= "email ='" . $database->escape_value($this->email) . "', ";
        $sql .= "WHERE id=" . $database->escape_value($this->id);

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

    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
}

?>