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

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

     public function assess(array $values) {
        $sql = "INSERT INTO assessment ";
        $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 importAssessment(array $values) {
        $sql = "INSERT INTO assessment ";
        $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 add_parent(array $values) {
    //     $sql = "INSERT INTO assessment ";
    //     $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 add_doc(array $values) {
    //     $sql = "INSERT INTO assessment ";
    //     $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,$ass_id) {
        
            $sql = "UPDATE assessment SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE ass_id = '" .$ass_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 add_parent(array $values,$ass_id) {
        
            $sql = "UPDATE parent_detail SET ";
            $fields = array_keys($values);
            $vals = array_values($values);
            foreach ($fields as $i=>$f) {
            $fields[$i] .= ' = ? ';
            }
            $sql .= implode(',', $fields);
            $sql .= " WHERE ass_id = '" .$ass_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_assessment($ass_id) {
        $statement = $this->db->prepare("SELECT * FROM assessment WHERE ass_id=:ass_id LIMIT 1");
        $statement->bindParam(':ass_id', $ass_id);
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }


    public function get_staff($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 if_isset($student_id,$class_id,$subject_id,$track_id,$semester_id,$a_year_id) {
        $statement = $this->db->prepare("SELECT * FROM assessment WHERE student_id=:student_id AND class_id=:class_id
                                        AND subject_id=:subject_id AND track_id=:track_id AND semester_id=:semester_id
                                         AND a_year_id=:a_year_id LIMIT 1");
        $statement->bindParam(':student_id', $student_id);
        $statement->bindParam(':class_id', $class_id);
        $statement->bindParam(':subject_id', $subject_id);
        $statement->bindParam(':track_id', $track_id);
        $statement->bindParam(':semester_id', $semester_id);
        $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_all_assessment() {
        $statement = $this->db->prepare("SELECT a.*,b.* FROM assessment a JOIN class b ON a.class=b.class_id WHERE alumini='no'");
        $statement->execute();
        if ($statement->rowCount() > 0) {
            $this->rows=$statement->fetchAll(\PDO::FETCH_ASSOC);
            return $this->rows;            
        }
        return $this->success;
    }

      }

$assessment = new Assessment;
?>