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

class Biodata2 
{
    protected static $table_name = "form_data";
    public $id;
    public $passport;
    public $formno;
    public $surname;
    public $othernames;
    public $title;
    public $nationality;
    public $state;
    public $lga;
    public $hometown;
    public $sex;
    public $maritalStatus;
    public $religion;
    public $phone;
    public $email;
    public $programme;
    public $courses;
    public $status;
    public $session;
    public $reg_date;
    public $level;
    public $department;

    

   
   
//Common database Methods
   
    public static function find_by_id($table, $formno = '', $email = '')
    {
        global $database2;
        
       
        $sql = "SELECT $table.*,s.* FROM $table INNER JOIN student_account s ON $table.acc_id=s.id WHERE $table.formno='{$formno}' and s.email='{$email}' LIMIT 1";
        
        $result_array = self::find_by_sql($sql);
        
        return !empty($result_array) ? array_shift($result_array) : false;

    }
    


    public static function find_by_sql($sql = "")
    {
        global $database2;
        $result_set = $database2->query($sql);
        $object_array = array();
        while ($row = $database2->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 $database2;
        $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 $database2;
        $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 $database2;
        $sql = "DELETE FROM " . self::$table_name . " ";
        $sql .= "WHERE id=" . $database2->escape_value($this->id);
        $sql .= " LIMIT 1";
        $database2->query($sql);
        return ($database2->affected_rows() == 1) ? true : false;
    }

   

//Closing tags for user's class
}

$biodata = new Biodata2();
?>