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

class Duplicate  {
    
    
	protected static $table_name="student_activity_level";
	
	public $id;
	public $names;
	public $surname;
	public $othernames;
	public $session;
	public $semester;
	public $ses_sem_id;
	public $amount;
	public $batch;
	public $matricno;
	public $regDate;
	public $datetime;
	public $courses;
	public $course_code;
	public $course_title;
	public $course_unit;
	public $course_status;
	public $sex;
	public $pix;
	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 $prog;
	public $processing;
	public $fullname;
	public $allAd;
	public $allAc;
	public $allSchl;
	public $allCr;
	public $sumAc;
	public $sumSchl;
	public $sumCr;
	public $std_process_id;
	public $ToUse;
	public $ToDrop;
	public $NewMatricNo;

public static function showduplicate(){

global $database;

$sql = "SELECT a.formno, a.matricno, s.names,s.department from student_activity_level a 
INNER JOIN ( SELECT d.matricno from student_activity_level d 
GROUP BY matricno HAVING COUNT(d.matricno) >1 ) temp 
ON a.matricno = temp.matricno INNER JOIN admission_list s on a.formno = s.formno 
WHERE a.matricno !=0 ORDER BY matricno ";

// $sql = "SELECT * FROM student_activity_level";

return $result_array = self::find_by_sql($sql);

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

public static function updateNewMat($matricno, $formno)
{
    
    global $database; global $connection;
    $sql = "UPDATE dup_tbl SET NewMatricNo={$matricno} WHERE formno='$formno' LIMIT 1";
    
     if ($database->query($connection, $sql)) {
            //$this->id=$database->insert_id();
            return true;
          // return 'Success';
        } else {
           return false;
            // return 'Error'; 
        }
}


public static function getNewMat($formno='')
{
     global $database;
    
    $formno = $database->escape_value($formno);
    
    $sql = "SELECT * FROM student_activity_level WHERE formno='$formno' LIMIT 1 ";
    
     $result_array = self::find_by_sql($sql);
    
    return !empty($result_array)? array_shift($result_array) : false;


}
public static function allApp($matricno='')
{
    global $database;
    $matricno = $database->escape_value($matricno);
    
    $sql = "SELECT a.formno, a.matricno, s.names,s.department from student_activity_level a 
 
 INNER JOIN admission_list s on a.formno = s.formno 
 WHERE a.matricno = {$matricno} ORDER BY a.matricno";
   return $result_array = self::find_by_sql($sql);
     //return !empty($result_array)? array_shift($result_array):false;
}

public static function insertD($names='',$formno='', $matricno='',$department='')
{
    global $database; global $connection;
    
     $names = $database->escape_value($names);
    $formno = $database->escape_value($formno);
    $matricno = $database->escape_value($matricno);
   
    $department = $database->escape_value($department);
    
       $sql = "INSERT INTO dup_tbl (";
       $sql .= "fullname, formno,matricno,department";

        $sql .= ") VALUES ('";
        $sql .= $names . "', '";
        $sql .= $formno . "', '";
        $sql .= $matricno . "', '";
        $sql .= $department . "'
      
      
     
              
            )";

        if ($database->query($connection, $sql)) {
            //$this->id=$database->insert_id();
            return true;
          // return 'Success';
        } else {
           return false;
            // return 'Error'; 
        }
    }
 
  public static function allDup()
  {
      global $database;
      
      $sql = "SELECT * FROM dup_tbl";
     return $result_array = self::find_by_sql($sql);
   
      
      
  }
  
  
  public static function revokemat($formno='')
  
  {
      global $database; global $connection;
      
       $sql = "UPDATE student_activity_level SET matricno=0 WHERE formno='$formno'  LIMIT 1";
    
    if ($database->query($connection, $sql)) {
            //$this->id=$database->insert_id();
            return true;
          // return 'Success';
        } else {
           return false;
            // return 'Error'; 
        }
}



public static function selC($matricno='')
{
    
    global $database;
    
    $matricno = $database->escape_value($matricno);
    
    $sql = "SELECT * FROM student_activity_level WHERE matricno={$matricno} ";
    
    return $result_array = self::find_by_sql($sql);
    
   // return !empty($result_array)? array_shift($result_array) : false;
}

public static function updateToUse($id)
{
    global $database;
    
    $sql="UPDATE dup_tbl SET ToUse=1 WHERE id={$id} LIMIT 1";
    
  if ($database->query($sql)) {
            //$this->id=$database->insert_id();
            return true;
          // return 'Success';
        } else {
           return false;
            // return 'Error'; 
        }
}


public static function selUpdate($matricno='')
{
    
    global $database;
    
    $matricno = $database->escape_value($matricno);
    
    $sql = "SELECT * FROM dup_tbl WHERE matricno={$matricno} ORDER BY fullname LIMIT 1";
    
     $result_array = self::find_by_sql($sql);
    
    return !empty($result_array)? array_shift($result_array) : false;
}




public static function deleteId($id)
{
    global $database; global $connection;
    
    $sql = "DELETE FROM student_activity_level WHERE std_process_id=800 AND id={$id} LIMIT 1";
    
    if ($database->query($connection, $sql)) {
            //$this->id=$database->insert_id();
            return true;
          // return 'Success';
        } else {
           return false;
            // return 'Error'; 
        }
}

    
    
    
    
    
public static function updateId($id)
{
    global $database;
    
    $sql = "UPDATE student_activity_level SET std_process_id=800 WHERE id={$id}";
    
     if ($database->query($sql)) {
            //$this->id=$database->insert_id();
            return true;
          // return 'Success';
        } else {
           return false;
            // return 'Error'; 
        }
}

//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; $connection='';
	$result_set = $database->query($connection,$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
}




$duplicate = new DUPLICATE();
?>