<?php
require_once 'ConnectionDao.php';
class Setup{
	  private $db = null;
    private $rows = null;
    private $success = false;

    public function __construct() {
        $this->db=ConnectionDao::getDb();
    }
    //Start of Class func
     public function add_class(array $values) {
        $sql = "INSERT INTO class ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $sql .= '(' . implode(',', $fields) . ') ';
        $arr = array();
        foreach ($fields as $f) {
            $arr[] = '?';
        }
        $sql .= 'VALUES (' . implode(',', $arr) . ') ';
        $statement = $this->db->prepare($sql);
        foreach ($vals as $i => $v) {
            $statement->bindValue($i + 1, $v);
        }
        $this->success= $statement->execute();
        return $this->success;
    }


    public function update_class(array $values,$class_id) {
        
            $sql = "UPDATE class SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE class_id = '" .$class_id."' LIMIT 1 ";
            $statement = $this->db->prepare($sql);
            foreach ($vals as $i=>$v) {
            $statement->bindValue($i+1, $v);
            }
           $this->success= $statement->execute();
            return $this->success;

            }

             public function update_programme(array $values,$programme_id) {
        
            $sql = "UPDATE programme SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE programme_id = '" .$programme_id."' LIMIT 1 ";
            $statement = $this->db->prepare($sql);
            foreach ($vals as $i=>$v) {
            $statement->bindValue($i+1, $v);
            }
           $this->success= $statement->execute();
            return $this->success;

            }


    public function get_class($class_id) {
        $statement = $this->db->prepare("SELECT * FROM class WHERE class_id=:class_id LIMIT 1");
        $statement->bindParam(':class_id', $class_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function get_classes() {
        $statement = $this->db->prepare("SELECT * FROM class");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }
    public function get_core_elective() {
        $statement = $this->db->prepare("SELECT * FROM core_elective");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function delete_class($class_id) {
        $statement = $this->db->prepare("DELETE FROM class WHERE class_id=:class_id LIMIT 1");
        $statement->bindParam(':class_id', $class_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    //End of Class func

    //Start of Semester func

      public function add_semester(array $values) {
        $sql = "INSERT INTO semester ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $sql .= '(' . implode(',', $fields) . ') ';
        $arr = array();
        foreach ($fields as $f) {
            $arr[] = '?';
        }
        $sql .= 'VALUES (' . implode(',', $arr) . ') ';
        $statement = $this->db->prepare($sql);
        foreach ($vals as $i => $v) {
            $statement->bindValue($i + 1, $v);
        }
        $this->success= $statement->execute();
        return $this->success;
    }


    public function update_semester(array $values,$sem_id) {
        
            $sql = "UPDATE semester SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE sem_id = '" .$sem_id."' LIMIT 1 ";
            $statement = $this->db->prepare($sql);
            foreach ($vals as $i=>$v) {
            $statement->bindValue($i+1, $v);
            }
           $this->success= $statement->execute();
            return $this->success;

            }


    public function get_semester($sem_id) {
        $statement = $this->db->prepare("SELECT * FROM semester WHERE sem_id=:sem_id LIMIT 1");
        $statement->bindParam(':sem_id', $sem_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function get_semesters() {
        $statement = $this->db->prepare("SELECT * FROM semester");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function delete_semester($sem_id) {
        $statement = $this->db->prepare("DELETE FROM semester WHERE sem_id=:sem_id LIMIT 1");
        $statement->bindParam(':sem_id', $sem_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }    

    // End of semester func

     //Start of academic func

      public function add_a_year(array $values) {
        $sql = "INSERT INTO academic_year ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $sql .= '(' . implode(',', $fields) . ') ';
        $arr = array();
        foreach ($fields as $f) {
            $arr[] = '?';
        }
        $sql .= 'VALUES (' . implode(',', $arr) . ') ';
        $statement = $this->db->prepare($sql);
        foreach ($vals as $i => $v) {
            $statement->bindValue($i + 1, $v);
        }
        $this->success= $statement->execute();
        return $this->success;
    }


    public function update_a_year(array $values,$a_year_id) {
        
            $sql = "UPDATE academic_year SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE a_year_id = '" .$a_year_id."' LIMIT 1 ";
            $statement = $this->db->prepare($sql);
            foreach ($vals as $i=>$v) {
            $statement->bindValue($i+1, $v);
            }
           $this->success= $statement->execute();
            return $this->success;

            }


    public function get_a_year($a_year_id) {
        $statement = $this->db->prepare("SELECT * FROM academic_year WHERE a_year_id=:a_year_id LIMIT 1");
        $statement->bindParam(':a_year_id', $a_year_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function get_a_years() {
        $statement = $this->db->prepare("SELECT * FROM academic_year");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function delete_a_year($a_year_id) {
        $statement = $this->db->prepare("DELETE FROM academic_year WHERE a_year_id=:a_year_id LIMIT 1");
        $statement->bindParam(':a_year_id', $a_year_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }    
    // end of academic yr func

    //Start Track Func

    public function add_track(array $values) {
        $sql = "INSERT INTO track ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $sql .= '(' . implode(',', $fields) . ') ';
        $arr = array();
        foreach ($fields as $f) {
            $arr[] = '?';
        }
        $sql .= 'VALUES (' . implode(',', $arr) . ') ';
        $statement = $this->db->prepare($sql);
        foreach ($vals as $i => $v) {
            $statement->bindValue($i + 1, $v);
        }
        $this->success= $statement->execute();
        return $this->success;
    }


    public function update_track(array $values,$track_id) {
        
            $sql = "UPDATE track SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE track_id = '" .$track_id."' LIMIT 1 ";
            $statement = $this->db->prepare($sql);
            foreach ($vals as $i=>$v) {
            $statement->bindValue($i+1, $v);
            }
           $this->success= $statement->execute();
            return $this->success;

            }


    public function get_track($track_id) {
        $statement = $this->db->prepare("SELECT * FROM track WHERE track_id=:track_id LIMIT 1");
        $statement->bindParam(':track_id', $track_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function get_tracks() {
        $statement = $this->db->prepare("SELECT * FROM track");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function delete_track($track_id) {
        $statement = $this->db->prepare("DELETE FROM track WHERE track_id=:track_id LIMIT 1");
        $statement->bindParam(':track_id', $track_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }  

    // End of Track func  
    // Start Sucject func

          public function add_subject(array $values) {
        $sql = "INSERT INTO subject ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $sql .= '(' . implode(',', $fields) . ') ';
        $arr = array();
        foreach ($fields as $f) {
            $arr[] = '?';
        }
        $sql .= 'VALUES (' . implode(',', $arr) . ') ';
        $statement = $this->db->prepare($sql);
        foreach ($vals as $i => $v) {
            $statement->bindValue($i + 1, $v);
        }
        $this->success= $statement->execute();
        return $this->success;
    }


    public function update_subject(array $values,$subject_id) {
        
            $sql = "UPDATE subject SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE subject_id = '" .$subject_id."' LIMIT 1 ";
            $statement = $this->db->prepare($sql);
            foreach ($vals as $i=>$v) {
            $statement->bindValue($i+1, $v);
            }
           $this->success= $statement->execute();
            return $this->success;

            }


    public function get_subject($subject_id) {
        $statement = $this->db->prepare("SELECT * FROM subject WHERE subject_id=:subject_id LIMIT 1");
        $statement->bindParam(':subject_id', $subject_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

     public function add_prog(array $values) {
        $sql = "INSERT INTO programme ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $sql .= '(' . implode(',', $fields) . ') ';
        $arr = array();
        foreach ($fields as $f) {
            $arr[] = '?';
        }
        $sql .= 'VALUES (' . implode(',', $arr) . ') ';
        $statement = $this->db->prepare($sql);
        foreach ($vals as $i => $v) {
            $statement->bindValue($i + 1, $v);
        }
        $this->success= $statement->execute();
        return $this->success;
    }

    public function get_prog_subject() {
        $statement = $this->db->prepare("SELECT * FROM subject");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function get_subjects() {
        $statement = $this->db->prepare("SELECT a.*,b.* FROM subject a 
        JOIN core_elective b ON a.core_elective_id=b.core_elective_id");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }


    public function get_programmes() {
        $statement = $this->db->prepare("SELECT * FROM programme");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function delete_subject($subject_id) {
        $statement = $this->db->prepare("DELETE FROM subject WHERE subject_id=:subject_id LIMIT 1");
        $statement->bindParam(':subject_id', $subject_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

     
      //End of Subject

      public function add_house(array $values) {
        $sql = "INSERT INTO house ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $sql .= '(' . implode(',', $fields) . ') ';
        $arr = array();
        foreach ($fields as $f) {
            $arr[] = '?';
        }
        $sql .= 'VALUES (' . implode(',', $arr) . ') ';
        $statement = $this->db->prepare($sql);
        foreach ($vals as $i => $v) {
            $statement->bindValue($i + 1, $v);
        }
        $this->success= $statement->execute();
        return $this->success;
    }


    public function update_house(array $values,$house_id) {
        
        $sql = "UPDATE house SET ";
        $fields = array_keys($values);
        $vals = array_values($values);
        foreach ($fields as $i=>$f) {
        $fields[$i] .= ' = ? ';
        }
        $sql .= implode(',', $fields);
        $sql .= " WHERE house_id = '" .$house_id."' LIMIT 1 ";
        $statement = $this->db->prepare($sql);
        foreach ($vals as $i=>$v) {
        $statement->bindValue($i+1, $v);
        }
        $this->success= $statement->execute();
        return $this->success;

    }


    public function get_house($house_id) {
        $statement = $this->db->prepare("SELECT * FROM house WHERE house_id=:house_id LIMIT 1");
        $statement->bindParam(':house_id', $house_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function get_programme($programme_id) {
        $statement = $this->db->prepare("SELECT * FROM programme WHERE programme_id=:programme_id LIMIT 1");
        $statement->bindParam(':programme_id', $programme_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }


    public function get_houses() {
        $statement = $this->db->prepare("SELECT * FROM house");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function delete_house($house_id) {
        $statement = $this->db->prepare("DELETE FROM house WHERE house_id=:house_id LIMIT 1");
        $statement->bindParam(':house_id', $house_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }    


    public function delete_programme($programme_id) {
        $statement = $this->db->prepare("DELETE FROM programme WHERE programme_id=:programme_id LIMIT 1");
        $statement->bindParam(':programme_id', $programme_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }    

    // End of semester func

    // Fees
    public function add_fee_type(array $values) {
        $sql = "INSERT INTO fee_type ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $sql .= '(' . implode(',', $fields) . ') ';
        $arr = array();
        foreach ($fields as $f) {
            $arr[] = '?';
        }
        $sql .= 'VALUES (' . implode(',', $arr) . ') ';
        $statement = $this->db->prepare($sql);
        foreach ($vals as $i => $v) {
            $statement->bindValue($i + 1, $v);
        }
        $this->success= $statement->execute();
        return $this->success;
    }

    public function pay_fee(array $values) {
        $sql = "INSERT INTO pay_fee ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $sql .= '(' . implode(',', $fields) . ') ';
        $arr = array();
        foreach ($fields as $f) {
            $arr[] = '?';
        }
        $sql .= 'VALUES (' . implode(',', $arr) . ') ';
        $statement = $this->db->prepare($sql);
        foreach ($vals as $i => $v) {
            $statement->bindValue($i + 1, $v);
        }
        $this->success= $statement->execute();
        return $this->success;
    }

    public function set_class_fee(array $values) {
        $sql = "INSERT INTO set_fee ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $sql .= '(' . implode(',', $fields) . ') ';
        $arr = array();
        foreach ($fields as $f) {
            $arr[] = '?';
        }
        $sql .= 'VALUES (' . implode(',', $arr) . ') ';
        $statement = $this->db->prepare($sql);
        foreach ($vals as $i => $v) {
            $statement->bindValue($i + 1, $v);
        }
        $this->success= $statement->execute();
        return $this->success;
    }

    public function update_fee_type(array $values,$fee_type_id) {
        $sql = "UPDATE fee_type SET ";
        $fields = array_keys($values);
        $vals = array_values($values);
        foreach ($fields as $i=>$f) {
        $fields[$i] .= ' = ? ';
        }
        $sql .= implode(',', $fields);
        $sql .= " WHERE fee_type_id = '" .$fee_type_id."' LIMIT 1 ";
        $statement = $this->db->prepare($sql);
        foreach ($vals as $i=>$v) {
        $statement->bindValue($i+1, $v);
        }
        $this->success= $statement->execute();
        return $this->success;
    }

    public function update_set_fee(array $values,$set_fee_id) {
        $sql = "UPDATE set_fee SET ";
        $fields = array_keys($values);
        $vals = array_values($values);
        foreach ($fields as $i=>$f) {
        $fields[$i] .= ' = ? ';
        }
        $sql .= implode(',', $fields);
        $sql .= " WHERE set_fee_id = '" .$set_fee_id."' LIMIT 1 ";
        $statement = $this->db->prepare($sql);
        foreach ($vals as $i=>$v) {
        $statement->bindValue($i+1, $v);
        }
        $this->success= $statement->execute();
        return $this->success;
    }

    public function get_fee_types() {
        $statement = $this->db->prepare("SELECT * FROM fee_type");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function get_set_fees() {
        $statement = $this->db->prepare("SELECT a.*,b.*,c.* FROM set_fee a 
        JOIN class b ON a.class_id=b.class_id
         JOIN fee_type c ON a.fee_type_id=c.fee_type_id");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function get_fee_type($fee_type_id) {
        $statement = $this->db->prepare("SELECT * FROM fee_type WHERE fee_type_id=:fee_type_id LIMIT 1");
        $statement->bindParam(':fee_type_id', $fee_type_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function get_set_fee($set_fee_id) {
        $statement = $this->db->prepare("SELECT a.*,b.*,c.* FROM set_fee a 
        JOIN class b ON a.class_id=b.class_id
         JOIN fee_type c ON a.fee_type_id=c.fee_type_id
          WHERE a.set_fee_id=:set_fee_id LIMIT 1");
        $statement->bindParam(':set_fee_id', $set_fee_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function get_pay_fee($pay_fee_id) {
        $statement = $this->db->prepare("SELECT a.*,b.*,c.*,d.*,e.* FROM pay_fee a 
        JOIN student_detail b ON a.student_id=b.student_id 
        JOIN set_fee c ON a.set_fee_id=c.set_fee_id 
        JOIN class d ON c.class_id=d.class_id 
        JOIN fee_type e ON c.fee_type_id=e.fee_type_id WHERE a.pay_fee_id=:pay_fee_id LIMIT 1");
        $statement->bindParam(':pay_fee_id', $pay_fee_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function get_pay_fees() {
        $statement = $this->db->prepare("SELECT a.*,b.*,c.*,d.*,e.* FROM pay_fee a 
        JOIN student_detail b ON a.student_id=b.student_id 
        JOIN set_fee c ON a.set_fee_id=c.set_fee_id 
        JOIN class d ON c.class_id=d.class_id 
        JOIN fee_type e ON c.fee_type_id=e.fee_type_id");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

    public function delete_fee_type($fee_type_id) {
        $statement = $this->db->prepare("DELETE FROM fee_type WHERE fee_type_id=:fee_type_id LIMIT 1");
        $statement->bindParam(':fee_type_id', $fee_type_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }  

    public function delete_set_fee($set_fee_id) {
        $statement = $this->db->prepare("DELETE FROM set_fee WHERE set_fee_id=:set_fee_id LIMIT 1");
        $statement->bindParam(':set_fee_id', $set_fee_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }  



}
$setup = new Setup;
?>