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



class DEPARTMENTCLASS extends DatabaseObject
{
    protected static $table_name = "department_tbl";
    public $dept_id;
    public $faculty;
    public $department;
    public $names;
    public $status;
    public $session;
    public $admitted_date;
    public $courses;
    public $formno;




     public static function getfac($dept)
    {
        global $database;
        
        $sql = "SELECT * FROM department_tbl ";
        $sql .= " WHERE department like '{$dept}' LIMIT 1 ";
	

        return $result_array = self::find_by_sql($sql);
       // return !empty($result_array) ? array_shift($result_array) : false;

    }
    
    
    
    
    public static function department_authenticate($dept='')
    {
        global $database;
        
        $sql = "SELECT * FROM department_tbl ";
        $sql .= " WHERE department='{$dept}' LIMIT 1 ";
	

        $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;

    }
    
    
     public static function new_admin_dept($formno)
    {
        global $database;
        
        $sql = "SELECT * FROM admission_list ";
        $sql .= " WHERE formno='{$formno}' LIMIT 1 ";
	

        $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;

    }
    
    
    public static function new_admin_dept_old($formno)
    {
        global $database;
        
        $sql = "SELECT * FROM admitted_student_details ";
        $sql .= " WHERE formno='{$formno}' LIMIT 1 ";
	

        $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;

    }

    public static function fetch_all_course($department = "", $programme = "", $semester = "")
    {
        $sql = "SELECT course_code, course_title, course_unit ";
        $sql .= " FROM " . 'tbl_courses' . " ";
        $sql .= " WHERE department = '$department' AND  level = 'HND I' AND semester = '$semester'";
        $result_array = self::find_by_sql($sql);
//$result_array = 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($faculty = "")
    {
        global $database;
        return self::find_by_sql("SELECT * FROM " . self:: $table_name . " WHERE faculty='{$faculty}'");
        //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
}

$departmentclass = new DEPARTMENTCLASS();

?>