<?php
error_reporting(1);
require_once(LIB_PATH.DS.'database3.php');
class EDITING {
	protected static $table_name="student_account";

	public $id;
	public $acc_id;
	public $surname;
	public $names;
	public $name;
	public $othernames;
	public $session;
	public $semester;
	public $ses_sem_id;
	public $amount;
	public $matricno;
	//public $regDate;
	public $date;
	public $courses;
	public $sex;
	public $subject;
	public $dob;
	public $lga;
	public $department;
	public $departmentcode;
	public $email;
	public $state;
	public $reg_date;
	public $admitted_date;
	public $faculty;
	public $facultycode;
	public $formno;
	public $level;
	public $levelcode;
	public $overhead;
	public $phone;
	public $programme;
	public $programmecode;
	public $ptype;
	public $responsecode;
	public $status;
	public $txt_fees;
	public $txt_ref;
	public $verified;
	public $verified_date;
	public $verified_by;
	public $deptcode;
	public $destination;

	public $pix;
	public $title;
    public $nationality;
    public $hometown;
    public $marital;
    public $religion;
    public $olevel;
	public $examno;
	public $examno2;
	public $edited_date;
	public $user;
	
    public $examyear;
    public $examyear2;
    public $sub1;

    public $sub2;
    public $sub3;
    public $sub4;

    public $sub5;
// 	public $grd3;
//     public $sb4;
//     public $grd4;
//     public $sb5;
//     public $grd5;
//     public $sb6;
//     public $grd6;
//     public $sb7;
//     public $grd7;
//     public $sb8;
//     public $grd8;
//     public $sb9;
//     public $grd9;
    // public $examno2;
    public $exam_type2;


    public $exam_year2;
	public $jregno;
    // public $poly_of_choice;
    public $j_eng;
    public $jgrd1;
    public $jsb2;    
    public $jgrd2;
    public $jsb3;
    public $jgrd3;
    public $jsb4;
    public $jgrd4;
    public $jscore;
    public $jinstitution;
    public $institution;
    public $grag;
    public $gragD;


    public $itcompany;
	public $dstarted;
    public $dcompleted;
    // public $grade2;
    // public $yr2;


    // public $company;    
    // public $purpose;
    // public $from_year;
    // public $to_year;
    public $type;





	
public static function authenticate($formno="", $email=""){
global $database3;
//$formno= $database->escape_value($formno);
//$email = $database->escape_value($email);
$sql = "SELECT * FROM ".self ::$table_name." ";
$sql .= " WHERE formno='{$formno}' ";
$sql .= "AND email='{$email}' LIMIT 1";
//$sql .= "AND responsecode = 'Successful' LIMIT 1" ;	

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


public static function showdept(){
global $database3;
//$formno= $database->escape_value($formno);
//$email = $database->escape_value($email);
$sql = "SELECT dept_id, dept FROM department ORDER BY dept ASC" ;

//$sql .= "AND responsecode = 'Successful' LIMIT 1" ;	

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

public static function subject(){
	global $database3;
	$sql = "SELECT name FROM subjects ORDER BY id ASC" ;	
	return self::find_by_sql($sql);
	}

public static function lga(){
global $database3;
$sql = "SELECT id, name FROM local_governments ORDER BY id ASC" ;

return self::find_by_sql($sql);
}


public static function state(){
global $database3;
$sql = "SELECT state_id, name FROM states ORDER BY state_id ASC" ;
return self::find_by_sql($sql);
}

// Search 2019/2020 admitted_student_details_2019 by Form Number-------------------
public static function search_by_formno($table, $formno){
global $database3;
$sql = "SELECT * FROM $table ";
$sql .= " WHERE formno='{$formno}' LIMIT 1";

// $sql = "SELECT * FROM $table_name WHERE formno='{$formno}'" ;
// return self::find_by_sql($sql);
$result_array = self::find_by_sql($sql);
return !empty($result_array)? array_shift($result_array):
false;
}
// ------------end of search-------------------------------------------------------

public static function search($table, $data){
global $database3;
$sql = "SELECT b.*, s.*, a.* FROM $table b ";
$sql .=" INNER JOIN student_account s ON b.acc_id = s.id ";
$sql .=" INNER JOIN academics_data a ON a.acc_id = b.acc_id ";
$sql .= " WHERE b.formno='$data' LIMIT 1";

// $sql = "SELECT * FROM $table_name WHERE formno='{$formno}'" ;
// return self::find_by_sql($sql);
$result_array = self::find_by_sql($sql);
return !empty($result_array)? array_shift($result_array):
false;
}


public static function monitorBU($date){
	global $database3;
		$sql = "SELECT user, COUNT(edited_date) FROM admitted_student_details_2019 
		WHERE edited_date LIKE '%{$date}%' GROUP BY user";
			$database3->query($sql);
			$database3->query($sql); return self::find_by_sql($sql);
	}
	
	
public static function update_pix($pix,$formno){
global $database3;
    $sql = "UPDATE admitted_student_details_2019 SET pix='{$pix}' WHERE formno ='{$formno}'";
    //return self::find_by_sql($sql);
    	$database3->query($sql);
return ($database3->affected_rows()==1)? true: false;
}


public static function updateBU($date, $user, $acc_id){
global $database3;
	$sql = "UPDATE student_account SET edited_date='{$date}',user='{$user}' WHERE id ='{$acc_id}' LIMIT 1";
	//return self::find_by_sql($sql);
		$database3->query($sql);
return ($database3->affected_rows()==1)? true: false;
}

public static function update1($surname, $othername, $acc_id){
global $database3;
    $sql = "UPDATE student_account SET surname = '{$surname}', othernames = '{$othername}' WHERE id =$acc_id LIMIT 1";
    //return self::find_by_sql($sql);
    	$database3->query($sql);
return ($database3->affected_rows()==1)? true: false;
}

public static function adm_update($surname, $dept, $sta, $formno){
global $database;
    $sql = "UPDATE admission_list SET names = '{$surname}',department='{$dept}',status='{$sta}' WHERE formno ='{$formno}'";
    //return self::find_by_sql($sql);
    	$database->query($sql);
return ($database->affected_rows()==1)? true: false;
}


public static function update2($table, $nat,$dob,$sta,$lga,$ht,$sex,$reg,$mt,$phone, $formno){
global $database3;
	$sql = "UPDATE $table SET nationality = '{$nat}', dob = '{$dob}', state ='{$sta}',
	 lga='{$lga}', hometown = '{$ht}', sex='{$sex}',religion='{$reg}', marital= '{$mt}', phone='{$phone}'
	  WHERE formno ='{$formno}' LIMIT 1";

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


public static function update3($examno,$examyear1,$examno2,$exam_year2,$sb1,$grd1,$sb2,$grd2,$sb3,$grd3,
$sb4,$grd4,$sb5,$grd5,$acc_id){
global $database3;
	$sql = "UPDATE academics_data SET examno = '{$examno}', examyear='{$examyear1}',
	 examno2='{$examno2}', examyear2='{$exam_year2}',sub1='{$sb1}:{$grd1}',
	 sub2='{$sb2}:{$grd2}',sub3='{$sb3}:{$grd3}',sub4='{$sb4}:{$grd4}',sub5='{$sb5}:{$grd5}' WHERE acc_id = $acc_id LIMIT 1";
	$database3->query($sql);
return ($database3->affected_rows()==1)? true: false;
}


public static function update6($company, $dstarted, $dcompleted, $acc_id){
global $database3;
	$sql = "UPDATE academics_data SET itcompany = '{$company}', dstarted='{$dstarted}',dcompleted='{$dcompleted}' WHERE acc_id ='{$acc_id}' LIMIT 1";
	//return self::find_by_sql($sql);
		$database3->query($sql);
return ($database3->affected_rows()==1)? true: false;
}


public static function update5($institution, $grag, $gragD, $acc_id){
global $database3;
	$sql = "UPDATE academics_data SET institution = '{$institution}', grag = '{$grag}', gragD='{$gragD}' WHERE acc_id ='{$acc_id}' LIMIT 1";
	//return self::find_by_sql($sql);
		$database3->query($sql);
return ($database3->affected_rows()==1)? true: false;
}


public static function update4($jregno,$jscore,$jinstitution, $acc_id){
global $database3;
	$sql = "UPDATE academics_data SET jregno = '{$jregno}',jscore='{$jscore}',jinstitution='{$jinstitution}' WHERE acc_id ='{$acc_id}' LIMIT 1";
	//return self::find_by_sql($sql);
		$database3->query($sql);
return ($database3->affected_rows()==1)? true: false;
}


public static function get_admitted_students(){
global $database3;
	$sql = "SELECT * FROM admission_list ORDER BY department";
	
		$database3->query($sql); return self::find_by_sql($sql);
}


public static function check_students($formno){
global $database3;
	$sql = "SELECT * FROM admission_list WHERE formno = '{$formno}'";
	
		$database3->query($sql); return self::find_by_sql($sql);
}

public static function students_act($formno){
global $database3;
	$sql = "SELECT email FROM admitted_student_details_2019 WHERE formno = '{$formno}'";
		$database3->query($sql); return self::find_by_sql($sql);
}






public static function bu_insert($staff,$date){
global $database3;
	$sql = "INSERT INTO ".bunit_histroy." (";
	$sql .= "staff, date,count";
	$sql .= ") 	VALUES ('";
	$sql .= $database3->escape_value($this->formno)."', '"	;
	$sql .= $database3->escape_value($this->email)."') '"	;
	if ($database3->query($sql)){
		$this->id=$database3->insert_id();
		return true;
	}else {
		return false;
	}
	
}



public static function t_processing($id=0, $curr_session=""){
	
global $database3;
$user_data = Userdata::find_by_id($id);
 $biodata = Biodata::find_by_id($user_data->formno,$user_data->email);
 $dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);
$alldp = DEPARTMENTCLASS::find_all($dp->faculty);
foreach ($alldp as $aldp){
	$faculty = $aldp->faculty;
}
if ($faculty=='Engineering'or $faculty=='Applied Sciences'or $faculty=='Enviromental Studies'){
$pay_cat='science';	
}elseif ($faculty=='Management Sciences'){
	$pay_cat='nonscience';
}
$level = $biodata->programme;
 $level = $level." "."I";
$time = date('Y-m-d h:i:s');
$a = (substr(rand(1, 10000000000),0,10));
					$txt_ref = $a.(substr( $biodata->surname,0,2));	


$pay_settings=Pay_settings::find_by_id(1,$level,$pay_cat);

$sql = "INSERT INTO ".self ::$table_name ." (";
$sql .= "formno,surname, othername,faculty,department,programme,phone,email,session,level,txt_ref,amount,txt_fees,overhead,datetime,status,responsecode";

$sql .= ") VALUES ('";
$sql .= $biodata->formno."', '";
$sql .= $biodata->surname."', '";
$sql .= $biodata->othernames. "', '";
$sql .=$biodata->courses. "', '";
$sql .=$biodata->courses. "', '";
$sql .=$biodata->programme."', '";
$sql .= $biodata->phone."', '";
$sql.=$biodata->email."', '";
$sql .= $curr_session ."', '";
$sql .=$biodata->programme."', '";
$sql .=$txt_ref."' ,'";
$sql.= $pay_settings->amount."', '"; 
$sql .=$pay_settings->charge_interswitch + $pay_settings->charge_omoluabi."' ,'";
$sql .=$pay_settings->charge_interswitch + $pay_settings->charge_omoluabi."' ,'";
$sql .= $time. "' ,'" ;
$sql .=$biodata->programme."', '";
$sql .='Approved Successful'.
"')" ;

if ($database3->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 $database3;
	return self::find_by_sql("SELECT * FROM ".self:: $table_name);
	//return $result_set;
	
}
public static function find_by_id($id=0){
	global $database3;
	$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 $database3;
	$result_set = $database3->query($sql);
	$object_array = array();
	while ($row= $database3->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 $database3;
	$sql = "INSERT INTO ".self ::$table_name." (";
	$sql .= "formno, email";
	$sql .= ") 	VALUES ('";
	$sql .= $database3->escape_value($this->formno)."', '"	;
	$sql .= $database3->escape_value($this->email)."') '"	;
	if ($database3->query($sql)){
		$this->id=$database3->insert_id();
		return true;
	}else {
		return false;
	}
	
}

public function update(){
	
	global $database3;
	$sql = "UPDATE ".self ::$table_name."(";
	$sql .="formno = '". $database3->escape_value($this->formno)."',"; 
	$sql .= "email ='". $database3->escape_value($this->email)."', ";	
	$sql .= "WHERE id=". $database3->escape_value($this->id);
	
	$database3->query($sql);
return ($database3->affected_rows()==1)? true: false;
	}
public function delete(){
	
global $database3;
$sql = "DELETE FROM ".self ::$table_name." ";
$sql .= "WHERE id=" . $database3->escape_value($this->id);
$sql .= " LIMIT 1";	
$database3->query($sql);
return ($database3->affected_rows() ==1) ? true: false;
}

//Closing tags for user's class
}
//$onlinepay = new ONLINEPAY();
?>