<?php
require_once(LIB_PATH.DS.'database.php');
class UPDATESTUACCOUNT extends DatabaseObject{
	protected static $table_name="admitted_student_details_2019";
	
	public $id;
	public $pix;
	public $formno;
	public $title;
	public $surname;
	public $othernames;
	public $dob;
	public $nationality;



	public $state;
	public $lga;
	public $hometown;
	public $sex;
	public $maritalStatus;
	public $religion;


	public $phone;
	public $email;
	public $level;
	public $department;
	public $olevel;
	public $examno1;


	public $exam_type1;
	public $exam_year1;
	public $sb1;
	public $grd1;
	public $sb2;
	public $grd2;


	public $sb3;
	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 $j_reg_no;
	public $poly_of_choice;
	public $j_eng;
	public $jgrd1;


	public $jsb2;
	public $jgrd2;
	public $jsb3;
	public $jgrd3;
	public $jsb4;
	public $jgrd4;
	public $j_total;
	public $ol_total_score;
	

	public $higher1;
	public $qual1;
	public $grade1;
	public $yr1;
	public $higher2;
	public $qual2;


	public $grade2;
	public $yr2;
	public $company;

	public $purpose;
	public $from_year;
	public $to_year;



	public $type;
	public $status;

	public $session;
	public $reg_date;



public static function findid($email=""){
global $database;
$email= $database->escape_value($email);
$sql = "SELECT * FROM ".self ::$table_name." ";
$sql .= " WHERE 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 authenticate_by_email($email="", $password=""){
global $database;
//$email= $database->escape_value($email);
//$password = $database->escape_value($password);
$sql = "SELECT * FROM ".self ::$table_name." ";
$sql .= " WHERE email='{$email}' ";
$sql .= "AND password='{$password}' LIMIT 1";	

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

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($email,$password,$surname,$othernames,$pics,$id){
	
	global $database;
	$sql = "UPDATE  ".self ::$table_name." SET password='{$password}',surname='{$surname}',othernames='{$othernames}',email='{$email}',status='active',pictures='{$pics}' Where id={$id} ";
	//$sql .= "password ='". $password."', ";	
	//$sql .= "surname ='". $surname."', ";	
	//$sql .="othernames = '". $othernames."', "; 
	//$sql .=" email = '". $email."', "; 
	///$sql .= "status ='". 'active'."', ";	
	//$sql .= "pictures ='". $pics."', ";	
	//$sql .= " WHERE id=". $id;
	
	$database->query($sql);
return ($database->affected_rows()==1)? true: false;
	}
	
	public function update2($email,$password,$surname,$othernames,$id){
	
	global $database;
	$sql = "UPDATE  ".self ::$table_name." SET password='{$password}',surname='{$surname}',othernames='{$othernames}',email='{$email}',status='active' Where id={$id} ";
	//$sql .= "password ='". $password."', ";	
	//$sql .= "surname ='". $surname."', ";	
	//$sql .="othernames = '". $othernames."', "; 
	//$sql .=" email = '". $email."', "; 
	///$sql .= "status ='". 'active'."', ";	
	//$sql .= "pictures ='". $pics."', ";	
	//$sql .= " WHERE id=". $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
}

?>