<?php
session_start();

include '../manage/connection.php';

include '../classes/students_class.php';
include '../classes/classroom_class.php';
include '../classes/class_subjects_class.php';
include '../classes/subjects_class.php';
include '../classes/session_class.php';

$student=new Student("../manage/connection.php");
$classroom=new ClassRoom("../manage/connection.php");
$classSubject=new ClassSubject("../manage/connection.php");
$subject=new Subject("../manage/connection.php");
$class_session=new Session("../manage/connection.php");

$pcount=0;
if(isset($_SESSION['teacher']) || isset($_SESSION['admin']))
{
    $time=time();
    if(isset($_SESSION['teacher']))
    {
        $promoted_by="teacher";
        $p_id=$_SESSION['teacher']['id'];
    }
    else
    {
        $promoted_by="admin";
        $p_id=$_SESSION['admin']['id'];
    }

    if(isset($_POST['promote_class']) && !empty($_POST['promote_class']) && isset($_POST['class_id']))
    {
        $class_id=htmlentities($_POST['class_id']);
        $promoted_class=htmlentities($_POST['promote_class']);

        foreach($classroom->getAllClassStudents($class_id) as $stud)
        {
            $student_id=htmlentities($stud['id']);
            if(isset($_POST['promote_'.$student_id]))
            {
                // Get Session from last students result and class
                
                $getSession=$connect->query("SELECT * FROM results WHERE student_id='$student_id' AND class_id='$class_id'")->fetch_assoc();
                $session=$getSession['session_id'];

                // Update class
                $promote=$connect->query("UPDATE students SET classroom_id='$promoted_class' WHERE id='$student_id'");
                if($promoted_class!=0)
                {
                    // Promote
                    if($promote)
                    {
                        $promo_history=$connect->query("INSERT INTO promotion_history (promoted_by, p_uid, student_id, session_id, promoted_from, promoted_to, time)
                         VALUES('$promoted_by', '$p_id', '$student_id', '$session', '$class_id', '$promoted_class', '$time')");
                         $pcount++;
                    }
                }
                else
                {
                    //Graduated
                    $getClass=$connect->query("SELECT * FROM classroom WHERE id='$class_id'")->fetch_assoc();//
                    $class_session=$getClass['section'];
                    $graduate=$connect->query("INSERT INTO graduation (recorded_by, student_id, graduated_from, class_id, graduation_session, graduated_on) 
                    VALUES ('$p_id', '$student_id', '$class_session', '$class_id', '$session', '$time')");
                    $pcount++;
                }
                
            }
        }
        if($pcount>0)
        {
            echo 'success';
        }
        else
        {
            echo 'Failed to promote student(s)';
        }
    }
    else
    {
        echo 'No class Selected';
    }
}
else
{
    echo 'Not logged in';
}
?>