<?php
require_once(LIB_PATH . DS . 'database.php');
date_default_timezone_set('Africa/Lagos');
class Result extends DatabaseObject
{
    protected static $table_name = "result_backup";
    public $id;
    public $formno;
    public $courses;
    public $matricno;
    public $prog_type;
    public $session;
    public $semester;
    public $level;
    public $tnu;
    public $tcp;
    public $gpa;
    public $course_code;
    public $course_title;
    public $course_unit;
    public $score;
    public $names;
    public $surname;
    public $othernames;
    
    public static function fetchallResult($id="", $matricno="",$semester="",$level="" ){
        
        global $database;
        
        $sql = "SELECT * FROM result_backup 
			WHERE matricno ='$matricno' and id = $id and semester='{$semester}' and level='{$level}' LIMIT 1";
			
			 $result_array = self::find_by_sql($sql);
           return !empty($result_array)? array_shift($result_array):false;

    }
    public static function geteachPrintableResult($id="", $semester="",$level=""){
        
        global $database; 
         $sql = "SELECT r.*,a.formno FROM result_backup r ";
         $sql .= " INNER JOIN student_activity_level a ";
        $sql .= " WHERE r.id=$id ";
        $sql .= "AND r.semester='{$semester}' AND r.level='{$level}' LIMIT 1";
        
        $result_array = self::find_by_sql($sql);
return !empty($result_array)? array_shift($result_array):false;

        
    }
    
    public static function getStudentAdmissionData18($formno='')
    {
        global $database;
        $sql = "SELECT * FROM admitted_student_details where formno='{$formno}' LIMIT 1";
        $result_array = self::find_by_sql($sql);
return !empty($result_array)? array_shift($result_array):false;
    }
    public static function getStudentAdmissionData($formno='')
    {
        global $database;
        $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 findacceptance($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}' AND responsecode='00' LIMIT 1";
//$sql .= "AND responsecode = 'Successful' LIMIT 1" ;	

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


    }*/


public static function getTitleandUnit($course_code='',$level=''){
    
    global $database;
    
    $sql ="SELECT * FROM tbl_courses WHERE course_code='{$course_code}' AND level='{$level}' LIMIT 1";
    
    $result_array = self::find_by_sql($sql);
return !empty($result_array)? array_shift($result_array):false;

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


    }
    
    public static function updateDepartment($formno="", $newdept=""){
        
        global $database;
        
        $sql="Update admission_list set department='{$newdept}' where formno='{$formno}' LIMIT 1";
        
         if ($database->query($sql)) {
            //$this->id=$database->insert_id();
            return true;
        } else {
            return false;
        }




    }
    public static function confirmschoolfees2($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}' AND responsecode='00' ";
        $sql .= "AND level IN ('ND II','HND II') LIMIT 1";
        
        $result_array = self::find_by_sql($sql);
return !empty($result_array)? array_shift($result_array):false;


    }


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

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

}
        
     public static function findschoolfees($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}' ";
$sql .= "AND responsecode = '00' LIMIT 1" ;	

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

}   
    
//$transact = Schoolfees::t_processing($id, $biodata->session, $total_sc_fees, $txt_ref);
    public static function t_processing($id = 0, $curr_session = "", $totamount,$txt_ref="", $adm_type="",$level="")
    {


        global $database;

        $user_data = Userdata::find_by_id($id);
        
        if (substr($user_data->formno,2,2)==20){
      
       $biodata = Biodata2::find_by_id($user_data->formno, $user_data->email);
        }else{
        $biodata = Biodata::find_by_id($user_data->formno, $user_data->email);
		
        }
		
		//$user_data = Userdata::find_by_id($session->user_id);
//$biodata = Biodata::find_by_id($user_data->formno, $user_data->email);
//$mode= $user_data->mode;
//$user_data->std_process_id;

if (isset($biodata->courses)){
     $dept = $biodata->courses;
}else {
    $dept = $biodata->department;
    $biodata->courses = $dept;
}
//$dp = DEPARTMENTCLASS::department_authenticate($dept);

if ($dept=='Microbiology (Option)'){
      $biodata->courses= $dept='(Microbiology (Option)'; 
       
    }else if ($dept=='Electronic/Telecommunication (Option)'){
       $biodata->courses= $dept = 'Electronics/Telecommunication (Option)';
    }

		
		
		 /* if (($adm_type == 'PU') or ($adm_type == 'FT')) {
            $adm_type = 'regular';

        } elseif ($adm_type == 'DP') {

            $adm_type = 'dailypt';
        }*/
		
        $dp = DEPARTMENTCLASS::department_authenticate($dept);
        $alldp = DEPARTMENTCLASS::find_all($dp->faculty);
        foreach ($alldp as $aldp) {
            $faculty = $aldp->faculty;
        }
        if ($faculty == 'Engineering' or $faculty == 'Applied Sciences' or $faculty == 'Enviromental Studies' or $faculty=='Information Communication and Technology') {
            $pay_cat = 'science';
        } elseif ($faculty == 'Management Sciences') {
            $pay_cat = 'nonscience';
        }
       /* $level = $biodata->programme;
        $level = $level . " " . "I";

        $pay_settings = Pay_settings::find_by_id(3, $level, $pay_cat);
        $time = date('Y-m-d h:i:s');
        $a = (substr(rand(1, 10000000000), 0, 10));
        $txt_ref = $a . (substr($biodata->surname, 0, 2));
*/		 $time_tosave = time();
			$time = strftime("%Y-%m-%d %H:%M:%S", $time_tosave);
        $sql = "INSERT INTO " . self::$table_name . " (";
        $sql .= "formno,surname, othernames,faculty,department,programme,phone,email,session,level,txt_ref,amount,txt_fees,overhead,datetime,status,responsecode,mode";
        $sql .= ") VALUES ('";
        $sql .= $biodata->formno . "', '";
        $sql .= $biodata->surname . "', '";
        $sql .= $biodata->othernames . "', '";
        $sql .= $faculty . "', '";
        $sql .= $biodata->courses . "', '";
        $sql .= $level . "', '";
        $sql .= $biodata->phone . "', '";
        $sql .= $biodata->email . "', '";
        $sql .= $curr_session . "', '";
        $sql .= $level . "', '";
        $sql .= $txt_ref . "' ,'";
        $sql .= $totamount . "', '";
        $sql .= 300 . "' ,'";
        $sql .= 700 . "' ,'";
        $sql .= $time . "' ,'";
        $sql .= 'UNPAID' . "', '";
        $sql .= '' ."', '";
		$sql .= $adm_type .
            "')";
        if ($database->query($sql)) {
            //$this->id=$database->insert_id();
            return true;
        } else {
            return false;
        }

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

    }

    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_txnref($txt = 0)
    {
        global $database;
        $result_array = self::find_by_sql("SELECT * FROM  " . self::$table_name . " WHERE txt_ref='{$txt}' LIMIT 1");
        //$found = $database->fetch_array($result_set);
        //return $found;
        return !empty($result_array) ? array_shift($result_array) : false;

    }

    public static function find_by_txnref_failed($txt = 0)
    {
        global $database;
        $result_array = self::find_by_sql("SELECT * FROM  " . self::$table_name . " WHERE txt_ref='{$txt}'and status != 'Approved Successful' LIMIT 1");
        //$found = $database->fetch_array($result_set);
        //return $found;
//	txt_ref='{$txt}'
        return !empty($result_array) ? array_shift($result_array) : false;

    }

    public static function find_by_txnref_failed_by_formno($txt = 0)
    {
        global $database;
        $result_array = self::find_by_sql("SELECT * FROM  " . self::$table_name . " WHERE formno='{$txt}'and status != 'Approved Successful' LIMIT 0,1");
        //$found = $database->fetch_array($result_set);
        //return $found;
//	txt_ref='{$txt}'
        return !empty($result_array) ? array_shift($result_array) : false;

    }

    public static function find_by_txnref_failed_by_transid($txt = 0)
    {
        global $database;
        $result_array = self::find_by_sql("SELECT * FROM  " . self::$table_name . " WHERE txt_ref='{$txt}'and status != 'Approved Successful' LIMIT 0,1");
        //$found = $database->fetch_array($result_set);
        //return $found;
//	txt_ref='{$txt}'
        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
}

$result = new Result();
?>