<?php
session_start();
if(isset($_POST['class_id']) && isset($_POST['class_id']))
{
    $teacher_id=$_SESSION['teacher']['id'];
    $session_id=htmlentities($_POST['session_id']);
    $class_id=htmlentities($_POST['class_id']);
    $time=time();
    include '../classes/classroom_class.php';
    include '../classes/assessment_class.php';
    include '../classes/students_class.php';
    include '../classes/result_class.php';
    
    $assessment=new Assessment("../manage/connection.php");
    $classroom=new ClassRoom("../manage/connection.php");
    $dstudent=new Student("../manage/connection.php");
    $result=new Result("../manage/connection.php");
    
    $added_count=0;
    $update_count=0;
    $students_list=array();
    
    // i fetched students by session and class in order to get students that participated, instead of getting student from students table with class id, student may be promoted and results needs to be generated and it will generate with new students

    foreach($dstudent->studBySession($session_id, $class_id) as $studentInfo)
    {
        $total_marks=$assessment->sumAssessment($studentInfo['id'], $session_id, $class_id)['total'];
        $exam=$assessment->sumAssessment($studentInfo['id'], $session_id, $class_id)['exam'];
        $ca1=$assessment->sumAssessment($studentInfo['id'], $session_id, $class_id)['ca_1'];
        $ca2=$assessment->sumAssessment($studentInfo['id'], $session_id, $class_id)['ca_2'];
        $studInfo=$dstudent->getStudent($studentInfo['id']);
        $stud=$studInfo['firstname'].' '.$studInfo['lastname'];
    
        array_push($students_list, array("student_id"=>$studentInfo['id'], "student_name"=>$stud,"session_id"=>$session_id,
        "class_id"=>$class_id, "ca_1"=>$ca1, "ca_2"=>$ca2, "exam"=>$exam, "total_marks"=>$total_marks));
     
    
    }
    // Sort students
    usort($students_list, mySort('total_marks'));
    
    // Calculate position and add to database
    $position=0;
    $previousMark=0;
    foreach($students_list as $student)
    {
        
        if($previousMark!=$student['total_marks'])
        {
            $position++;
        }
        $previousMark=$student['total_marks'];
        // Check if result exist on database, update, else add new
        // Add to storage
        if($result->findStudentResult($student['student_id'], $session_id, $class_id))
        {
            // Update
            if($result->updateResult($student['student_id'], $session_id, $class_id, $student['ca_1'], $student['ca_2'], $student['exam'], $student['total_marks'], $position))
            {
                // Updated
                $update_count++;
            }
        }
        else
        {
            // add new
            if($result->addResult($student['student_id'], $session_id, $class_id, 'teacher', $teacher_id, $student['ca_1'], $student['ca_2'], $student['exam'], $student['total_marks'], $position, $time))
            {
                // Added
                $added_count++;
            }
        }
        // echo '<div> '.$student['student_name'].' '.$student['total_marks'].' POSITION '.$position.'</div>';
    }

    if($added_count!=0 || $update_count!=0)
    {
        echo 'Result Generated';
    }
    else
    {
        echo 'Could Not Generate Result';
    }
}

function mySort($key)
{
    return function($a,$b) use ($key)
    {
        return strnatcmp($b[$key], $a[$key]);
    };
}
?>