<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
include '../../../config/index.php';
$NoMaleStudents = 0;
$NoFemaleStudents = 0;
$TotalPercentage = 0;
$DropNoLearners = 0;

$sql1 = "SELECT * FROM selfPacedLearners ";
$result1 = mysqli_query($conn, $sql1);
$NoLearners = mysqli_num_rows($result1);
if (mysqli_num_rows($result1) > 0) {
    while ($row1 = mysqli_fetch_assoc($result1)) {
        $sql10 = "SELECT * FROM students WHERE student_id='" . $row1["student_id"] . "'  AND learner_gender='Male'";
        $result10 = mysqli_query($conn, $sql10);
        if (mysqli_num_rows($result10) > 0) {
            $NoMaleStudents =  $NoMaleStudents + 1;
        } else {
            $NoFemaleStudents =  $NoFemaleStudents + 1;
        }

        $sql_20 = "select * from selfPacedLearnerAssignment WHERE selfPacedLearners_id='" . $row1["selfPacedLearners_id"] . "'   ";
        $result_20 = mysqli_query($conn, $sql_20);
        $AllAssignments = mysqli_num_rows($result_20);
        $sql_20 = "select * from selfPacedLearnerAssignment WHERE selfPacedLearners_id='" . $row1["selfPacedLearners_id"] . "'  AND Status='Graded' ";
        $result_20 = mysqli_query($conn, $sql_20);
        $GradedAssignments = mysqli_num_rows($result_20);
        if ($GradedAssignments > 0 && $AllAssignments > 0) {
            $Percentage = ($GradedAssignments / $AllAssignments) * 100;
            $TotalPercentage = $TotalPercentage + $Percentage;
        }

        $sql_aa = "select * from ActiveUsers WHERE UserType='Learner' AND UserID='" . $row1["student_id"] . "' AND TimeLogged < DATE_SUB(NOW(), INTERVAL 3 MONTH) ";
        $result_aa = mysqli_query($conn, $sql_aa);
        if (mysqli_num_rows($result_aa) > 0) {
            $DropNoLearners =  $DropNoLearners + 1;
        }
    }
}
$sql1 = "SELECT * FROM selfPacedTeachers ";
$result1 = mysqli_query($conn, $sql1);
$NoTeachers = mysqli_num_rows($result1);
if (mysqli_num_rows($result1) > 0) {
    while ($row1 = mysqli_fetch_assoc($result1)) {
        $sql10 = "SELECT * FROM teachers WHERE teacher_id='" . $row1["teacher_id"] . "'  AND Gender='Male'";
        $result10 = mysqli_query($conn, $sql10);
        if (mysqli_num_rows($result10) > 0) {
            $NoMaleStudents =  $NoMaleStudents + 1;
        } else {
            $NoFemaleStudents =  $NoFemaleStudents + 1;
        }
        $sql_20 = "select * from selfPacedTeacherAssignment WHERE selfPacedTeachers_id='" . $row1["selfPacedTeachers_id"] . "'   ";
        $result_20 = mysqli_query($conn, $sql_20);
        $AllAssignments = mysqli_num_rows($result_20);
        $sql_20 = "select * from selfPacedTeacherAssignment WHERE selfPacedTeachers_id='" . $row1["selfPacedTeachers_id"] . "'  AND Status='Graded' ";
        $result_20 = mysqli_query($conn, $sql_20);
        $GradedAssignments = mysqli_num_rows($result_20);

        if ($GradedAssignments > 0 && $AllAssignments > 0) {
            $Percentage = ($GradedAssignments / $AllAssignments) * 100;
            $TotalPercentage = $TotalPercentage + $Percentage;
        }
        $sql_aa = "select * from ActiveUsers WHERE UserType='TEacher' AND UserID='" . $row1["teacher_id"] . "' AND TimeLogged < DATE_SUB(NOW(), INTERVAL 3 MONTH) ";
        $result_aa = mysqli_query($conn, $sql_aa);
        if (mysqli_num_rows($result_aa) > 0) {
            $DropNoLearners =  $DropNoLearners + 1;
        }
    }
}
$sql1 = "SELECT * FROM selfPacedLearnerAssignmentRetry ";
$result1 = mysqli_query($conn, $sql1);
$NoLearnerRetries = mysqli_num_rows($result1);

$sql1 = "SELECT * FROM selfPacedTeacherAssignmentRetry ";
$result1 = mysqli_query($conn, $sql1);
$NoTeacherRetries = mysqli_num_rows($result1);


$TotalNoStudents = (int)$NoLearners + (int)$NoTeachers;
$TotalNoRetries = (int)$NoLearnerRetries + (int)$NoTeacherRetries;

$MalePercentage = ($NoMaleStudents / $TotalNoStudents) * 100;
$FemalePercentage = ($NoFemaleStudents / $TotalNoStudents) * 100;
$CompletionRate = $TotalPercentage / $NoLearners;
$DropPercentage = ($DropNoLearners / $NoLearners) * 100;

$curl_post_data = array(
    'TotalNoStudents' => $TotalNoStudents,
    'TotalNoRetries' => $TotalNoRetries,
    'MalePercentage' => $MalePercentage,
    'FemalePercentage' => $FemalePercentage,
    'CompletionRate' => $CompletionRate,
    'DropPercentage' => $DropPercentage,
);
// array_push($prerequisite_courses, $curl_post_data);


echo json_encode($curl_post_data);
