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

    public function __construct() {
        $this->db=ConnectionDao::getDb();
    }

        public function set_global(array $values) {
        $sql = "INSERT INTO global ";
        $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_global(array $values,$global_id) {
        
            $sql = "UPDATE global SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE global_id = '" .$global_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 promote_students(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_credentials(array $values,$staff_id) {
        
            $sql = "UPDATE staff_login SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE staff_id = '" .$staff_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 setVacation(array $values,$vacation_id) {
        
            $sql = "UPDATE vacation SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE vacation_id = '" .$vacation_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 PromoteAll(array $values,$p_c_id) {
        
            $sql = "UPDATE promotion_status SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE p_s_id = '" .$p_c_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_global($global_id) {
        $statement = $this->db->prepare("SELECT * FROM global WHERE global_id=:global_id LIMIT 1");
        $statement->bindParam(':global_id', $global_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

     public function get_all_globals() {
        $statement = $this->db->prepare("SELECT a.*,b.*,c.* FROM global a JOIN academic_year b 
                                        On a.a_year_id=b.a_year_id JOIN semester c ON
                                        a.semester_id=c.sem_id");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

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

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

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


     public function get_all_gold() {
        $statement = $this->db->prepare("SELECT * FROM student_detail where track=2");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $statement->rowCount();            
        }
        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 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 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 get_classes_for_promotion() {
        $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;
    }



      }

$global = new SiteGlobal;
?>