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

    function addExam($student_id, $session_id, $class_id, $subject_id, $teacher_id, $marks, $time)
    {
        include $this->connLink;

        $addExam=$connect->query("INSERT INTO exam (student_id, session_id, class_id, subject_id, teacher_id, marks, time)
        VALUES('$student_id', '$session_id', '$class_id', '$subject_id', '$teacher_id', '$marks', '$time')");
        return $addExam;
    }
    // Add ca1
    function addCA1($student_id, $session_id, $class_id, $subject_id, $teacher_id, $marks, $time)
    {
        include $this->connLink;

        $addExam=$connect->query("INSERT INTO ca_1 (student_id, session_id, class_id, subject_id, teacher_id, marks, time)
        VALUES('$student_id', '$session_id', '$class_id', '$subject_id', '$teacher_id', '$marks', '$time')");
        return $addExam;
    }
    // Add ca2
    function addCA2($student_id, $session_id, $class_id, $subject_id, $teacher_id, $marks, $time)
    {
        include $this->connLink;

        $addExam=$connect->query("INSERT INTO ca_2 (student_id, session_id, class_id, subject_id, teacher_id, marks, time)
        VALUES('$student_id', '$session_id', '$class_id', '$subject_id', '$teacher_id', '$marks', '$time')");
        return $addExam;
    }

    function getCA1($id)
    {
        include $this->connLink;
        $getCA=$connect->query("SELECT * FROM ca_1 WHERE id='$id'")->fetch_assoc();
        return $getCA;
    }

    function findCA1($student_id, $session_id, $class_id, $subject_id)
    {
        include $this->connLink;
        $getCA=$connect->query("SELECT * FROM ca_1 WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'");
        if($getCA->num_rows!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    function findCA2($student_id, $session_id, $class_id, $subject_id)
    {
        include $this->connLink;
        $getCA=$connect->query("SELECT * FROM ca_2 WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'");
        if($getCA->num_rows!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    function findExam($student_id, $session_id, $class_id, $subject_id)
    {
        include $this->connLink;
        $getCA=$connect->query("SELECT * FROM exam WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'");
        if($getCA->num_rows!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    function getExamMark($student_id, $session_id, $class_id, $subject_id)
    {
        include $this->connLink;
        $getExam=$connect->query("SELECT * FROM exam WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'");
        return $getExam->fetch_assoc();
    }
    // Ca1
    function getCA1Mark($student_id, $session_id, $class_id, $subject_id)
    {
        include $this->connLink;
        $getExam=$connect->query("SELECT * FROM ca_1 WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'");
        return $getExam->fetch_assoc();
    }
    // Ca2
    function getCA2Mark($student_id, $session_id, $class_id, $subject_id)
    {
        include $this->connLink;
        $getExam=$connect->query("SELECT * FROM ca_2 WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'");
        return $getExam->fetch_assoc();
    }

    function getAllTeachers()
    {
        include $this->connLink;
        $teachers=array();
        $getCA=$connect->query("SELECT * FROM teacher");
        while($teacher=$getCA->fetch_assoc())
        {
            array_push($teachers, $teacher);
        }
        return $teachers;
    }
    function findTeacher($phone)
    {
        include $this->connLink;
        $getCA=$connect->query("SELECT * FROM teacher WHERE phone='$phone'");
        if($getCA->num_rows!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    function updateExam($student_id, $session_id, $class_id, $subject_id, $marks)
    {
        include $this->connLink;
        $updateExam=$connect->query("UPDATE exam SET marks='$marks' WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'");
        return $updateExam;
        
    }
    // Update CA
    function updateCA1($student_id, $session_id, $class_id, $subject_id, $marks)
    {
        include $this->connLink;
        $updateExam=$connect->query("UPDATE ca_1 SET marks='$marks' WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'");
        return $updateExam;
        
    }
    function updateCA2($student_id, $session_id, $class_id, $subject_id, $marks)
    {
        include $this->connLink;
        $updateExam=$connect->query("UPDATE ca_2 SET marks='$marks' WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'");
        return $updateExam;
        
    }

    // Sum Student's Accessment
    function sumAssessment($student_id, $session_id, $class_id)
    {
        include $this->connLink;
        $ca1=0;
        $ca2=0;
        $exam=0;
        $sumCA1=$connect->query("SELECT SUM(marks) as marks from ca_1 WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id'")->fetch_assoc();
        $sumCA2=$connect->query("SELECT SUM(marks) as marks from ca_2 WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id'")->fetch_assoc();
        $sumExam=$connect->query("SELECT SUM(marks) as marks from exam WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id'")->fetch_assoc();
        
         
        if($sumCA1['marks']!=null)
        {
            $ca1=$sumCA1['marks'];
        }

        if($sumCA2['marks']!=null)
        {
            $ca2=$sumCA2['marks'];
        }

        if($sumExam['marks']!=null)
        {
            $exam=$sumExam['marks'];
        }
        return array("ca_1"=>$ca1, "ca_2"=>$ca2, "exam"=>$exam, "total"=>($ca1+$ca2+$exam));
    }
}
?>