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

    function addResult($student_id, $session_id, $class_id, $added_by, $add_uid, $ca_1, $ca_2, $exam, $total, $position, $grade, $average, $time)
    {
        include $this->connLink;

        $add=$connect->query("INSERT INTO results (student_id, session_id, class_id, added_by, add_uid, ca_1, ca_2, exam, total, position, grade, average, time)
        VALUES('$student_id', '$session_id', '$class_id', '$added_by', '$add_uid', '$ca_1', '$ca_2', '$exam', '$total', '$position', '$grade', '$average', '$time')");
        return $add;
    }

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

    function getStudentResult($student_id, $session_id, $class_id)
    {
        include $this->connLink;
        $getR=$connect->query("SELECT * FROM results WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id'")->fetch_assoc();
        return $getR;
    }

    function getSubjectResult($student_id, $session_id, $class_id,$subject_id)
    {
        include $this->connLink;
        $getR=$connect->query("SELECT * FROM subject_result WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id' AND subject_id='$subject_id'")->fetch_assoc();
        return $getR;
    }

    function studentByResult($session_id, $class_id)
    {
        include $this->connLink;
        $getR=$connect->query("SELECT distinct(student_id) FROM results WHERE session_id='$session_id' AND class_id='$class_id'");
        return $getR->num_rows;
    }
    function getResultById($id)
    {
        include $this->connLink;
        $getR=$connect->query("SELECT * FROM results WHERE id='$id'")->fetch_assoc();
        return $getR;
    }
    function getAllResult($class_id, $session_id)
    {
        include $this->connLink;
        $teachers=array();
        $getR=$connect->query("SELECT * FROM results WHERE session_id='$session_id' AND class_id='$class_id' ORDER BY position ASC");
        while($teacher=$getR->fetch_assoc())
        {
            array_push($teachers, $teacher);
        }
        return $teachers;
    }

    function resultRate($result_id, $code)
    {
        include $this->connLink;
        $rating=$connect->query("SELECT * FROM result_rating WHERE result_id='$result_id' AND rate_code='$code'");
        return $rating->fetch_assoc();
    }
    function allResultRate($result_id)
    {
        include $this->connLink;
        $allrate=array();
        $rating=$connect->query("SELECT * FROM result_rating WHERE result_id='$result_id'");
        while($rates=$rating->fetch_assoc())
        {
            array_push($allrate, $rates);
        }
        return $allrate;
    }
    
    function getRemarks($result_id)
    {
        include $this->connLink;
        $remarks=$connect->query("SELECT * FROM remarks WHERE result_id='$result_id'");
        return $remarks->fetch_assoc();
    }

    function findRemarks($result_id)
    {
        include $this->connLink;
        $remarks=$connect->query("SELECT * FROM remarks WHERE result_id='$result_id'");
        if($remarks->num_rows!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    function addRemarks($result_id, $added_by, $add_uid, $t_remark, $ht_remark, $time)
    {
        include $this->connLink;
        $remarks=$connect->query("INSERT INTO remarks (result_id, added_by, added_uid, f_teacher, h_teacher, time)
         VALUES('$result_id', '$added_by', '$add_uid', '$t_remark', '$ht_remark', '$time')");
        return $remarks;
    }
    
    function updateRemarks($result_id, $t_remark, $ht_remark)
    {
        include $this->connLink;
        $remarks=$connect->query("UPDATE remarks SET f_teacher='$t_remark', h_teacher='$ht_remark' WHERE result_id='$result_id'");
        return $remarks;
    }

    function findStudentResult($student_id, $session_id, $class_id)
    {
        include $this->connLink;
        $getR=$connect->query("SELECT * FROM results WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id'");
        if($getR->num_rows!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    function clearStudentResult($student_id, $session_id, $class_id)
    {
        include $this->connLink;
        $getR=$connect->query("UPDATE results SET grade='' WHERE session_id='$session_id' AND class_id='$class_id'");
        
    }
    function findStudentGrades($session_id, $class_id,$grade)
    {
        $grade1="$grade"."1";
        $grade2="$grade"."2";
        $grade3="$grade"."3";
        include $this->connLink;
        $getFirst=$connect->query("SELECT grade FROM results WHERE session_id='$session_id' AND class_id='$class_id' AND grade='$grade1'");
        $getSec=$connect->query("SELECT grade FROM results WHERE session_id='$session_id' AND class_id='$class_id' AND grade='$grade2'");
        $getThird=$connect->query("SELECT grade FROM results WHERE session_id='$session_id' AND class_id='$class_id' AND grade='$grade3'");
        if($getFirst->num_rows!=0)
        {
            if($getSec->num_rows!=0)
            {
                if($getThird->num_rows!=0)
                {
                    return $grade;
                }
                else
                {
                    return $grade3;
                }
            }
            else
            {
                return $grade2;
            }
        }
        else
        {
            return $grade1;
        }
    }
    // TRUNCATE ` results `
    function updateStudentGrade($student_id, $total, $session_id, $class_id, $grade)
    {
        include $this->connLink;
        
        // FInd student with class id, session id and marks, if the marks match, get the grade so that the grades will be the same (Previous & current)
        $getRes=$connect->query("SELECT * FROM results WHERE session_id='$session_id' AND class_id='$class_id' AND total='$total' AND student_id!='$student_id' ORDER BY id DESC");
        if($getRes->num_rows!=0)
        {
            $fetchedInfo=$getRes->fetch_assoc();
            if($fetchedInfo['grade']!='')
            {
                $grade=$fetchedInfo['grade'];
            }
        }


        // Update Student with new grade,
        $updateR=$connect->query("UPDATE results SET grade='$grade' WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id'");
        return $updateR;
    }

    function classAverage($session_id, $class_id)
    {
        include $this->connLink;
        $getR=$connect->query("SELECT * FROM results WHERE session_id='$session_id' AND class_id='$class_id'");
        $highest_average=0;
        $lowest_average=0;

        $count=0;
        while($res=$getR->fetch_assoc())
        {
            // assign first value to average var
            if($count==0)
            {
                $highest_average=$res['average'];
                $lowest_average=$res['average'];
            }

            if($res['average']>$highest_average)
            {
                $highest_average=$res['average'];
            }
            // Assigning lowest average based on low average fetched
            if($res['average']<$lowest_average)
            {
                $lowest_average=$res['average'];
            }
            $count++;
        }

        return array("highest_average"=>$highest_average, "lowest_average"=>$lowest_average);
    }
    // Getting Next Term
    function nextTerm($end)
    {
        include $this->connLink;
        $next_term="";
        $getSessions=$connect->query("SELECT * FROM session ORDER BY begin ASC");
        while($sess=$getSessions->fetch_assoc())
        { 
            if(strtotime($sess['begin'])>strtotime($end))
            {
                // echo $sess['begin'].'<br>';
                $next_term=$sess['begin'];
                break;
            }
        }
        return $next_term;
         
    }

    function updateResult($student_id, $session_id, $class_id, $ca_1, $ca_2, $exam, $total, $position, $average)
    {
        include $this->connLink;
        $update=$connect->query("UPDATE results SET ca_1='$ca_1', ca_2='$ca_2', exam='$exam', total='$total', position='$position', average='$average' WHERE student_id='$student_id' AND session_id='$session_id' AND class_id='$class_id'");
        return $update;
        
    }
}
?>