<?php
class Student
{
    public $connLink;
    function __construct($url="../manage/connection.php")
    {
        $this->connLink=$url;
    }

    function addStudent($firstname, $lastname, $dob, $gender, $state, $address, $classroom_id, $parent_name, $parent_phone, $added_by, $image, $added_on)
    {
        include $this->connLink;

        $add=$connect->query("INSERT INTO students (firstname, lastname, dob, gender, state, address, classroom_id, parent_name, parent_phone, added_by, image, added_on)
        VALUES('$firstname', '$lastname', '$dob', '$gender', '$state', '$address', '$classroom_id', '$parent_name', '$parent_phone', '$added_by', '$image', '$added_on')");
        if($add)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    function getStudent($id)
    {
        include $this->connLink;
        $getT=$connect->query("SELECT * FROM students WHERE id='$id'")->fetch_assoc();
        $getT['reg_no']="AZA/".substr(date('Y',$getT['added_on']),2,3)."/".$getT['id'];
        return $getT;
    }
    function checkStudent($id)
    {
        include $this->connLink;
        $getT=$connect->query("SELECT * FROM students WHERE id='$id'");
        if($getT->num_rows!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    function getAllStudents()
    {
        include $this->connLink;
        $students=array();
        $getT=$connect->query("SELECT * FROM students");
        while($student=$getT->fetch_assoc())
        {
            $student['reg_no']="AZA/".substr(date('Y',$student['added_on']),2,3)."/".$student['id'];
            array_push($students, $student);
        }
        return $students;
    }

    function getAllDropouts()
    {
        include $this->connLink;
        $students=array();
        $getT=$connect->query("SELECT dropouts.student_id, dropouts.class_id, dropouts.date, students.id, students.firstname, students.lastname, students.gender, students.address, students.parent_name, students.parent_phone FROM dropouts LEFT JOIN students ON dropouts.student_id=students.id");
        while($student=$getT->fetch_assoc())
        {
            $student['reg_no']="AZA/".substr(date('Y',$student['added_on']),2,3)."/".$student['id'];
            array_push($students, $student);
        }
        return $students;
    }

    function getStudentsByClass($class_id)
    {
        include $this->connLink;
        $students=array();
        $getT=$connect->query("SELECT * FROM students WHERE classroom_id='$class_id'");
        while($student=$getT->fetch_assoc())
        {
            $student['reg_no']="AZA/".substr(date('Y',$student['added_on']),2,3)."/".$student['id'];
            array_push($students, $student);
        }
        return $students;
    }
    // Getting student id by session id and class
    function studBySession($session_id, $class_id)
    {
        include $this->connLink;
        
        $allStudents=array();
        $getStud=$connect->query("SELECT DISTINCT(student_id) FROM ca_2 WHERE session_id='$session_id' AND class_id='$class_id'
        UNION SELECT DISTINCT(student_id) FROM ca_1 WHERE session_id='$session_id' AND class_id='$class_id'
        UNION SELECT DISTINCT(student_id) FROM exam WHERE session_id='$session_id' AND class_id='$class_id'");
        while($stud=$getStud->fetch_assoc())
        {
            array_push($allStudents, array("id"=>$stud['student_id']));
        }

        return $allStudents;
    }
// All Result by
    function allResults($session_id, $class_id)
    {
        include $this->connLink;
        
        $allStudents=array();
        $getStud=$connect->query("SELECT * FROM results WHERE session_id='$session_id' AND class_id='$class_id' ORDER BY total DESC");
        while($stud=$getStud->fetch_assoc())
        {
            array_push($allStudents, $stud);
        }

        return $allStudents;
    }

    function studentAttendance($student_id,$session_id)
    {
        include $this->connLink;
        $getP=$connect->query("SELECT * FROM attendance WHERE student_id='$student_id' AND session_id='$session_id' AND attendance='present'")->num_rows;
        $getAbs=$connect->query("SELECT * FROM attendance WHERE student_id='$student_id' AND session_id='$session_id' AND attendance='absent'")->num_rows;
        return array("present"=>$getP, "absent"=>$getAbs);
    }
    function findStudent($phone)
    {
        include $this->connLink;
        $getT=$connect->query("SELECT * FROM students WHERE parent_phone='$phone'");
        if($getT->num_rows!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    function updateStudent($firstname, $lastname, $dob, $gender, $state, $address, $classroom_id, $parent_name, $parent_phone, $image, $student_id)
    {
        include $this->connLink;
        $update=$connect->query("UPDATE students SET firstname='$firstname', lastname='$lastname', dob='$dob', gender='$gender', state='$state', address='$address', classroom_id='$classroom_id', parent_name='$parent_name', parent_phone='$parent_phone', image='$image' WHERE id='$student_id'");
        return $update;
        
    }
}
?>