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

class penilaian
{
	private $db_handle;
	
    public function __construct()
    {
       $this->db_handle = new kontrolDB();
    }
    
    public function tampil_satu($testing, $kriteria)
    {        
		$query = "SELECT a.*, b.nilai, b.bobot FROM knn_penilaian a, knn_kriteria_nilai b 
				  WHERE a.id_kriteria_nilai=b.id_kriteria_nilai AND a.id_testing=? AND a.id_kriteria=?";
        $paramType = "ii";
        $paramValue = array(
            $testing, $kriteria
        );
        
        $result = $this->db_handle->getData($query, $paramType, $paramValue);
		//print_r($result);
        return $result;
    }

    public function tampil_satu_b($testing, $kriteria)
    {        
        $query = "SELECT a.* FROM knn_penilaian a
                  WHERE a.id_testing=? AND a.id_kriteria=?";
        $paramType = "ii";
        $paramValue = array(
            $testing, $kriteria
        );
        
        $result = $this->db_handle->getData($query, $paramType, $paramValue);
        //print_r($result);
        return $result;
    }

    public function tampil_hasil($testing, $kriteria)
    {        
		$query = "SELECT a.*, b.nilai FROM knn_hasil a, knn_kriteria_nilai b 
				  WHERE a.id_kriteria_nilai=b.id_kriteria_nilai AND a.id_testing=? AND b.id_kriteria=?";
        $paramType = "ii";
        $paramValue = array(
            $testing, $kriteria
        );
        
        $result = $this->db_handle->getData($query, $paramType, $paramValue);
		//print_r($result);
        return $result;
    }
	
    public function tampilById($id)
    {        
		$query = "SELECT * FROM knn_kriteria WHERE id_kriteria=?";
        $paramType = "i";
        $paramValue = array(
            $id
        );
        
        $result = $this->db_handle->getData($query, $paramType, $paramValue);
        return $result;
    }
	
    public function cekkriteria($data, $pilih)
    {     
		$query=""; $paramType=""; $paramValue=array();  
		if($pilih==1) {
			$query = "SELECT * FROM knn_kriteria WHERE kriteria=?";
			$paramType = "s";
			$paramValue = array(
				$data[0]
			);
		}elseif($pilih==2) {
			$query = "SELECT * FROM knn_kriteria WHERE kriteria=? AND id_kriteria<>?";
			$paramType = "si";
			$paramValue = array(
				$data[0], $data[1]
			);
		}
        
        $result = $this->db_handle->cekData($query, $paramType, $paramValue);
        return $result;
    }
		
    public function tambah($data=array()) 
    {
        $query = "INSERT INTO knn_kriteria (kriteria,jenis) 
									  VALUES (?, ?)";
        $paramType  = "si";
        $paramValue = array();
		$paramValue = $data;
        
        $insertId   = $this->db_handle->insert($query, $paramType, $paramValue);
        
        if ($insertId>0) {
			return true;
        } else {
            echo 'Error: cannot execute the command';
            return false;
        }        
    }
	
    public function ubah($data=array(), $id) 
    {
		$query = "UPDATE knn_kriteria SET kriteria=?, jenis=? WHERE id_kriteria=?";
		$paramType  = "sii";
		$paramValue = array($data[0],$data[1],$id);        
		
		$result = $this->db_handle->update($query, $paramType, $paramValue);
        if ($result==false) {
            echo 'Error: cannot execute the command';
            return false;
        } else {
            return true;
        } 
    }
		
    public function hapus($id) {
        $query = "DELETE FROM knn_kriteria WHERE id_kriteria= ?";
        $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);
    }
    
}
?>