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

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

     public function insert(array $values) {
        $sql = "INSERT INTO staff_detail ";
        $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 create_login(array $values) {
        $sql = "INSERT INTO staff_login ";
        $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 clear_assigned_classes($class_id) {
        $statement = $this->db->prepare("DELETE FROM class_scheduler WHERE class_id=:class_id");
        $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 assign_class(array $values) {
        $sql = "INSERT INTO class_scheduler ";
        $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(array $values,$staff_id) {
        
            $sql = "UPDATE staff_detail 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 isFormMasterFor(array $values,$staff_id) {
        
            $sql = "UPDATE staff_detail 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 assign_subject(array $values,$subject_id,$class_id) {
        
            $sql = "UPDATE class_scheduler 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."' AND 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 get_staff_detail($staff_id) {
        $statement = $this->db->prepare("SELECT * FROM staff_detail WHERE staff_id=:staff_id LIMIT 1");
        $statement->bindParam(':staff_id', $staff_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

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


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

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

     public function verify_if_subject_assigned($staff_id,$class_id) {
        $statement = $this->db->prepare("SELECT * FROM class_scheduler WHERE staff_id=:staff_id AND class_id=:class_id");
        $statement->bindParam(':staff_id', $staff_id);
        $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_subjects($staff_id) {
        $statement = $this->db->prepare("SELECT * FROM class_scheduler WHERE staff_id=:staff_id ");
        $statement->bindParam(':staff_id', $staff_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

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

     public function check_assignment($staff_id) {
        $statement = $this->db->prepare("SELECT * FROM class_scheduler WHERE staff_id=:staff_id ORDER BY scheduler_id ASC");
        $statement->bindParam(':staff_id', $staff_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

     public function get_class($class_id) {
        $statement = $this->db->prepare("SELECT class_name 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_house($house_id) {
        $statement = $this->db->prepare("SELECT house_name 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_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 get_staff($staff_detail_id) {
        $statement = $this->db->prepare("SELECT * FROM staff_detail WHERE staff_detail_id=:staff_detail_id LIMIT 1");
        $statement->bindParam(':staff_detail_id', $staff_detail_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

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


    public function get_subject($subject_id) {
        $statement = $this->db->prepare("SELECT subject_name 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 get_student_houses($house_id) {
        $statement = $this->db->prepare("SELECT * FROM student_detail WHERE house_id=:house_id");
        $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_class_list() {
        $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_subject_list() {
        $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_all_staff() {
        $statement = $this->db->prepare("SELECT * FROM staff_detail");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

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

     public function get_all_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_all_parents() {
        $statement = $this->db->prepare("SELECT * FROM parent_detail");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }


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


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

     public function get_role($role_id) {
        $statement = $this->db->prepare("SELECT * FROM role WHERE role_id=:role_id");
        $statement->bindParam(":role_id",$role_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

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


    public function get_form_class($staff_id) {
        $statement = $this->db->prepare("SELECT a.*,b.* FROM staff_detail a JOIN class b ON a.is_form_master=b.class_id WHERE a.staff_id=:staff_id");
        $statement->bindParam(":staff_id",$staff_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

     public function assign_house_master(array $values) {
        $sql = "INSERT INTO house_master_scheduler ";
        $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_form_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;
    }

      }

$profile = new Profile;
?>