<?php
require_once ("kontrolDB.php");
require_once ("kriteria_nilai.php");

class testing
{
	private $db_handle;
	private $kriteria_nilai;

    public function __construct()
    {
       $this->db_handle = new kontrolDB();
       $this->kriteria_nilai=new kriteria_nilai();
    }
    
    public function tampil()
    {        
		$sql = "SELECT * FROM knn_testing ORDER BY terdaftar DESC";
        $result = $this->db_handle->getDataAll($sql);
        return $result;
    }
	
    public function tampil_hasil($pilih=0)
    {        
		$qpilih=($pilih==0) ? "" : (($pilih==1) ? "WHERE c.id_kriteria_nilai=17" : "AND c.id_kriteria_nilai=18");
		$sql = "SELECT a.*, c.nilai FROM knn_testing a LEFT OUTER JOIN knn_hasil b ON a.id_testing=b.id_testing LEFT OUTER JOIN 
									knn_kriteria_nilai c ON b.id_kriteria_nilai=c.id_kriteria_nilai
				".$qpilih."
				ORDER BY a.terdaftar DESC";
        $result = $this->db_handle->getDataAll($sql);
        return $result;
    }
	
    public function tampilById($id)
    {        
		$query = "SELECT * FROM knn_testing WHERE id_testing=?";
        $paramType = "i";
        $paramValue = array(
            $id
        );
        
        $result = $this->db_handle->getData($query, $paramType, $paramValue);
        return $result;
    }
	
    public function cektesting($data, $pilih)
    {     
		$query=""; $paramType=""; $paramValue=array();  
		if($pilih==1) {
			$query = "SELECT * FROM knn_testing WHERE kode=?";
			$paramType = "s";
			$paramValue = array(
				$data[0]
			);
		}elseif($pilih==2) {
			$query = "SELECT * FROM knn_testing WHERE kode=? AND id_testing<>?";
			$paramType = "si";
			$paramValue = array(
				$data[0], $data[1]
			);
		}
        
        $result = $this->db_handle->cekData($query, $paramType, $paramValue);
        return $result;
    }
	
	private function cek_nilai($testing, $kriteria){
		$sql = "SELECT id_penilaian FROM knn_penilaian WHERE id_testing=$testing AND id_kriteria=$kriteria";
        $result = $this->db_handle->getDataAll($sql);
        if(count($result)>0){
			return true;
		} else {
			return false;
		}
	}

    public function getKodeTerakhir()
    {        
		$sql = "SELECT kode FROM knn_testing ORDER BY kode DESC LIMIT 0, 1";
        $result = $this->db_handle->getDataAll($sql);
        return $result[0];
    }
		
    public function tambah($data=array()) 
    {
		$kode="";
		if(trim($data[0])!=""){
			$kode=$data[0];
		} else {
			//Buat Kode Otomatis
			$kode=@$this->getKodeTerakhir()['kode'];
			if($kode!=""){
				$kode=sprintf("%06d", (int)$kode+1);
			} else {
				$kode=sprintf("%06d", 1);
			}
		}

        $query = "INSERT INTO knn_testing (kode,nama,terdaftar,nk) 
									  VALUES (?, ?, ?, ?)";
        $paramType  = "sssi";
        $paramValue = array();
		$paramValue = array($kode,$data[1],$data[4],$data[6]);        
        $insertId   = $this->db_handle->insert($query, $paramType, $paramValue);
		
        if ($insertId>0) {
			for($i=0; $i<count($data[7]); $i++){
				$kriteria=$data[7][$i];
				$nilai=$data[8][$kriteria];
				$masukan=$data[9][$kriteria];

				$nilai1=0; $nilai2=0;
				if($masukan==1){
					$nilai1=$nilai;
					$nilai2=0;
				} elseif($masukan==2){
					$result=$this->kriteria_nilai->tampil($kriteria);
					if($result){
						foreach ($result as $key => $datax) {
							$arr_angka=explode("-",$datax['nilai']);
							if($nilai>=doubleval(@$arr_angka[0]) && $nilai<=doubleval(@$arr_angka[1])){
								$nilai1=$datax['id_kriteria_nilai'];
							}
						}
					}
					$nilai2=$nilai;
				} elseif($masukan==3){
					$nilai1=0;
					$nilai2=$nilai;
				} 

				if($this->cek_nilai($insertId, $kriteria)==false) {
					$query = "INSERT INTO knn_penilaian (id_testing,id_kriteria,id_kriteria_nilai,masukan)
												  VALUES (?, ?, ?, ?)";
					$paramType  = "iiid";
					$paramValue = array($insertId,$kriteria,$nilai1,$nilai2);
					$this->db_handle->insert($query, $paramType, $paramValue);
				} else {
					$query = "UPDATE knn_penilaian SET id_kriteria_nilai=?, masukan=? WHERE id_testing=? AND id_kriteria=?";
					$paramType  = "idii";
					$paramValue = array($nilai1, $nilai2,$insertId,$kriteria);
					$this->db_handle->update($query, $paramType, $paramValue);
				}
				
			}
			
			return $insertId;
        } else {
            echo 'Error: cannot execute the command';
            return false;
        }        
    }
	
	
    public function ubah($data=array(), $id) 
    {
		$query = "UPDATE knn_testing SET kode=?, nama=?, nk=? WHERE id_testing=?";
		$paramType  = "ssii";
		$paramValue = array($data[0],$data[1],$data[6],$id);        
		$result = $this->db_handle->update($query, $paramType, $paramValue);
        if ($result==true) {
			for($i=0; $i<count($data[7]); $i++){
				$kriteria=$data[7][$i];
				$nilai=$data[8][$kriteria];
				$masukan=$data[9][$kriteria];

				$nilai1=0; $nilai2=0;
				if($masukan==1){
					$nilai1=$nilai;
					$nilai2=0;
				} elseif($masukan==2){
					$result=$this->kriteria_nilai->tampil($kriteria);
					if($result){
						foreach ($result as $key => $datax) {
							$arr_angka=explode("-",$datax['nilai']);
							if($nilai>=doubleval(@$arr_angka[0]) && $nilai<=doubleval(@$arr_angka[1])){
								$nilai1=$datax['id_kriteria_nilai'];
							}
						}
					}
					$nilai2=$nilai;
				} elseif($masukan==3){
					$nilai1=0;
					$nilai2=$nilai;
				} 

				if($this->cek_nilai($id, $kriteria)==false) {
					$query = "INSERT INTO knn_penilaian (id_testing,id_kriteria,id_kriteria_nilai,masukan)
												  VALUES (?, ?, ?, ?)";
					$paramType  = "iiid";
					$paramValue = array($id,$kriteria,$nilai1,$nilai2);
					$this->db_handle->insert($query, $paramType, $paramValue);
				} else {
					$query = "UPDATE knn_penilaian SET id_kriteria_nilai=?, masukan=? WHERE id_testing=? AND id_kriteria=?";
					$paramType  = "idii";
					$paramValue = array($nilai1, $nilai2,$id,$kriteria);
					$this->db_handle->update($query, $paramType, $paramValue);
				}
								
			}			
            return true;
        } else {
            echo 'Error: cannot execute the command';
            return false;
        } 
    }
	
	private function cek_hasil($testing){
		$sql = "SELECT id_hasil FROM knn_hasil WHERE id_testing='$testing'";
        $result = $this->db_handle->getDataAll($sql);
        if(count($result)>0){
			return true;
		} else {
			return false;
		}
	}
	
    public function simpan_hasil($testing, $hasil) 
    {
		$sql = "SELECT id_kriteria_nilai FROM knn_kriteria_nilai WHERE nilai='$hasil'";
		$result = $this->db_handle->getDataAll($sql);
		$id_hasil=(int)@$result[0]['id_kriteria_nilai'];
		
		if($this->cek_hasil($testing)==false) {
			$query = "INSERT INTO knn_hasil (id_testing,id_kriteria_nilai)
										  VALUES (?, ?)";
			$paramType  = "ii";
			$paramValue = array($testing,$id_hasil);
			$this->db_handle->insert($query, $paramType, $paramValue);
		} else {
			$query = "UPDATE knn_hasil SET id_kriteria_nilai=? WHERE id_testing=?";
			$paramType  = "ii";
			$paramValue = array($id_hasil,$testing);
			$this->db_handle->update($query, $paramType, $paramValue);
		}
    }
	
    public function hapus($id) {
        $query = "DELETE FROM knn_testing WHERE id_testing= ?";
        $paramType = "i";
        $paramValue = array(
            $id
        );
        $result = $this->db_handle->update($query, $paramType, $paramValue);
		
        $query = "DELETE FROM knn_penilaian WHERE id_testing= ?";
        $paramType = "i";
        $paramValue = array(
            $id
        );
        $result = $this->db_handle->update($query, $paramType, $paramValue);

        $query = "DELETE FROM knn_hasil WHERE id_testing= ?";
        $paramType = "i";
        $paramValue = array(
            $id
        );
        $result = $this->db_handle->update($query, $paramType, $paramValue);
		
		return $result;
    }
	
	public function escapeString($value)
    {
        return $this->db_handle->escape_string($value);
    }
    
}
?>