<?php
session_start();
include '../manage/connection.php';
if(isset($_SESSION['teacher']))
{
    $teacher_id=$_SESSION['teacher']['id'];
    // $resuld_id=$_POST['result_id'];
    $updCount=0;

    $class_id=$_POST['class_id'];
    $session_id=$_POST['session_id'];

    $getResults=$connect->query("SELECT * FROM results WHERE class_id='$class_id' AND session_id='$session_id'");
    while($allResult=$getResults->fetch_assoc())
    {
        $resuld_id=$allResult['id'];
        
        $puntuality=htmlentities($_POST['puntuality']);
        if(addRate($teacher_id, $resuld_id, 'puntuality', 'Punchtuality', $puntuality))
        {
            $updCount++;
        }

        $attendance=htmlentities($_POST['attendance']);
        if(addRate($teacher_id, $resuld_id, 'attendance', 'Attendance', $attendance))
        {
            $updCount++;
        }

        $reliability=htmlentities($_POST['reliability']);
        if(addRate($teacher_id, $resuld_id, 'reliability', 'Reliability', $reliability))
        {
            $updCount++;
        }

        $neatness=htmlentities($_POST['neatness']);
        if(addRate($teacher_id, $resuld_id, 'neatness', 'Neatness', $neatness))
        {
            $updCount++;
        }
        
        $politeness=htmlentities($_POST['politeness']);
        if(addRate($teacher_id, $resuld_id, 'politeness', 'Pilitness', $politeness))
        {
            $updCount++;
        }
        
        $honesty=htmlentities($_POST['honesty']);
        if(addRate($teacher_id, $resuld_id, 'honesty', 'Honesty', $honesty))
        {
            $updCount++;
        }
        
        $staff_relationship=htmlentities($_POST['staff_relationship']);
        if(addRate($teacher_id, $resuld_id, 'staff_relationship', 'Relationship With With Staff', $staff_relationship))
        {
            $updCount++;
        }
        
        $peers_relationship=htmlentities($_POST['peers_relationship']);
        if(addRate($teacher_id, $resuld_id, 'peers_relationship', 'Relationship With Peer', $peers_relationship))
        {
            $updCount++;
        }
        
        $self_control=htmlentities($_POST['self_control']);
        if(addRate($teacher_id, $resuld_id, 'self_control', 'Self Control', $self_control))
        {
            $updCount++;
        }
        
        $spirit_corpration=htmlentities($_POST['spirit_corpration']);
        if(addRate($teacher_id, $resuld_id, 'spirit_corpration', 'Spirit Of Cooperation', $spirit_corpration))
        {
            $updCount++;
        }
        $neatness=htmlentities($_POST['neatness']);
        if(addRate($teacher_id, $resuld_id, 'neatness', 'Neatness', $neatness))
        {
            $updCount++;
        }
        
        $learn_willingness=htmlentities($_POST['learn_willingness']);
        if(addRate($teacher_id, $resuld_id, 'learn_willingness', 'Willingness To Learn', $learn_willingness))
        {
            $updCount++;
        }
        
        $initiative=htmlentities($_POST['initiative']);
        if(addRate($teacher_id, $resuld_id, 'initiative', 'Initiative', $initiative))
        {
            $updCount++;
        }
        
        $perseverence=htmlentities($_POST['perseverence']);
        if(addRate($teacher_id, $resuld_id, 'perseverence', 'Perseverance', $perseverence))
        {
            $updCount++;
        }
        
        $physchomotor=htmlentities($_POST['physchomotor']);
        if(addRate($teacher_id, $resuld_id, 'physchomotor', 'Psycomotor', $physchomotor))
        {
            $updCount++;
        }
        
        $handwriting=htmlentities($_POST['handwriting']);
        if(addRate($teacher_id, $resuld_id, 'handwriting', 'Hand Writing', $handwriting))
        {
            $updCount++;
        }
        
        $games=htmlentities($_POST['games']);
        if(addRate($teacher_id, $resuld_id, 'games', 'Games', $games))
        {
            $updCount++;
        }
        
        $sports=htmlentities($_POST['sports']);
        if(addRate($teacher_id, $resuld_id, 'sports', 'Sports', $sports))
        {
            $updCount++;
        }
        
        $drawing_paint=htmlentities($_POST['drawing_paint']);
        if(addRate($teacher_id, $resuld_id, 'drawing_paint', 'Drawing And Painting', $drawing_paint))
        {
            $updCount++;
        }

        $craft=htmlentities($_POST['craft']);
        if(addRate($teacher_id, $resuld_id, 'craft', 'Honesty', $craft))
        {
            $updCount++;
        }
        
        $musicals=htmlentities($_POST['musicals']);
        if(addRate($teacher_id, $resuld_id, 'musicals', 'Musicals', $musicals))
        {
            $updCount++;
        }
        
        // if($updCount!=0)
        // {
        //     echo 'Recorded';
        // }
        // else
        // {
        //     echo 'Could Not Recorded';
        // }
    }
    // If not zero
    if($updCount!=0)
    {
        echo 'Recorded';
    }
    else
    {
        echo 'Could Not Recorded';
    }
} 

function addRate($teacher, $resuld_id, $code, $title, $rate)
{
    $time=time();
    include '../manage/connection.php';
    $check=$connect->query("SELECT * FROM result_rating WHERE result_id='$resuld_id' AND rate_code='$code'");
    
    if($check->num_rows==0)
    {
        $add_rate=$connect->query("INSERT INTO result_rating (teacher_id, result_id, rate_code, title, rate, time)
        VALUES('$teacher', '$resuld_id', '$code', '$title', '$rate', '$time')");
        return $add_rate;
    }
    else
    {
        $update=$connect->query("UPDATE result_rating SET rate='$rate' WHERE result_id='$resuld_id' AND rate_code='$code'");
        return $update;
    }
    
}
?>