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

class SESSIONCLASS extends DatabaseObject
{
    protected static $table_name = "session_semester";
    public $id;
    public $ses_sem_id;
    public $session;
    public $semester;
    public $department;
    public $status;
    public $course_code;
    public $course_title;
    public $course_unit;

    public static function session_authenticate()
    {
        global $database;
//$formno= $database->escape_value($formno);
//$email = $database->escape_value($email);
        $sql = "SELECT * FROM " . self::$table_name . " ";
        $sql .= " WHERE status='active' ";
        $sql .= " ORDER BY ses_sem_id DESC LIMIT 1 ";

//$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 department_authenticate()
    {
        global $database;
        $dept = $biodata->courses;
//$email = $database->escape_value($email);
        $sql = "SELECT * FROM " . 'department_tbl' . " ";
        $sql .= " WHERE department='{dept}' LIMIT 1 ";
//$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 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()
    {
        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
}

$sessionclass = new SESSIONCLASS();

?>