<?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';

$data = array();

$sql_sfc = "select * from SelfPacedCourses   ";
$result_sfc = mysqli_query($conn, $sql_sfc);
if (mysqli_num_rows($result_sfc) > 0) {
    while ($row_sfc = mysqli_fetch_assoc($result_sfc)) {
        $sql_clp = "select * from selfPacedLearners  WHERE SelfPacedCourses_id='" . $row_sfc["SelfPacedCourses_id"] . "'  ";
        $result_clp = mysqli_query($conn, $sql_clp);
        if (mysqli_num_rows($result_clp) > 0) {
            while ($row_clp = mysqli_fetch_assoc($result_clp)) {
                $TotalRating = 0;
                $NoSessions = 0;
                $TotalPassRate = 0;
                $TotalRetries = 0;
                $Percentage = 0;
                $GradedAssignments = 0;
                $AllAssignments = 0;
                $sql_29 = "select * from selfPacedLearnerAssignment  WHERE selfPacedLearners_id='" . $row_clp["selfPacedLearners_id"] . "' ";
                $result_29 = mysqli_query($conn, $sql_29);
                $AllAssignments = mysqli_num_rows($result_29);

                if (mysqli_num_rows($result_29) > 0) {
                    while ($row_29 = mysqli_fetch_assoc($result_29)) {
                        $sql_30 = "select * from selfPacedLearnerAssignmentRating  WHERE selfPacedLearnersAssignment_id='" . $row_29["selfPacedLearnersAssignment_id"] . "'  ";
                        $result_30 = mysqli_query($conn, $sql_30);
                        if (mysqli_num_rows($result_30) > 0) {
                            while ($row_30 = mysqli_fetch_assoc($result_30)) {
                                $TotalRatingValue =  (int)$row_30["Relevance"] + (int)$row_30["WorkingCode"] + (int)$row_30["Creativity"] + (int)$row_30["TimelyCompletion"] + (int)$row_30["Completion"];
                                $TotalRatingNew = ($TotalRatingValue / 25) * 100;
                                if ($TotalRatingNew > 0) {
                                    $TotalRating =  $TotalRating +  $TotalRatingNew;
                                    $NoSessions =  $NoSessions + 1;
                                }
                                $sql_20 = "select * from selfPacedLearnerAssignmentRetry WHERE selfPacedLearnersAssignment_id='" . $row_30["selfPacedLearnersAssignment_id"] . "'   ";
                                $result_20 = mysqli_query($conn, $sql_20);
                                $NoRetries = mysqli_num_rows($result_20);
                                if ($NoRetries > 0) {
                                    $TotalRetries =  $NoRetries + 1;
                                }
                            }
                        }
                    }
                }
                $sql_20 = "select * from selfPacedLearnerAssignment WHERE selfPacedLearners_id='" . $row_clp["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;
                    $TotalPassRate = $TotalRating / $NoSessions;
                }

                $SessionTime = 0;
                $NoSessions = 0;
                $TotalSesionTime = 0;
                $sql_29 = "select * from selfPacedLearnerSession  WHERE selfPacedLearners_id='" . $row_clp["selfPacedLearners_id"] . "' AND SessionTime >'0'";
                $result_29 = mysqli_query($conn, $sql_29);
                if (mysqli_num_rows($result_29) > 0) {
                    while ($row_29 = mysqli_fetch_assoc($result_29)) {
                        $SessionTime =  (int)$row_29["SessionTime"] + $SessionTime;
                        $NoSessions =  $NoSessions + 1;
                    }
                }
                if ($NoSessions > 0) {
                    $TotalSesionTime = $SessionTime / $NoSessions;
                }


                $sql_29 = "select * from students  WHERE student_id='" . $row_clp["student_id"] . "' ";
                $result_29 = mysqli_query($conn, $sql_29);
                if (mysqli_num_rows($result_29) > 0) {
                    while ($row_29 = mysqli_fetch_assoc($result_29)) {
                        $curl_post_data = array(
                            'student_id' => $row_29["student_id"],
                            'LearnerName' => $row_29["learner_name"],
                            'LearnerLastName' => $row_29["learner_last_name"],
                            'Gender' => $row_29["learner_gender"],
                            'DateOfBirth' => $row_29["learner_age"],
                            'Username' => $row_29["username"],
                            'TotalPassRate' => $TotalPassRate,
                            'Percentage' => $Percentage,
                            'GradedAssignments' => $GradedAssignments,
                            'AllAssignments' => $AllAssignments,
                            'TotalRetries' => $TotalRetries,
                            'TotalSesionTime' => $TotalSesionTime,
                        );
                        array_push($data, $curl_post_data);
                    }
                }
                $SessionTime = 0;
                $NoSessions = 0;
                $TotalSesionTime = 0;
            }
        }
        $sql_clp = "select * from selfPacedTeachers  WHERE SelfPacedCourses_id='" . $row_sfc["SelfPacedCourses_id"] . "'  ";
        $result_clp = mysqli_query($conn, $sql_clp);
        if (mysqli_num_rows($result_clp) > 0) {

            while ($row_clp = mysqli_fetch_assoc($result_clp)) {
                $TotalRating = 0;
                $NoSessions = 0;
                $TotalPassRate = 0;
                $TotalRetries = 0;
                $Percentage = 0;
                $sql_29 = "select * from selfPacedTeacherAssignment  WHERE selfPacedTeachers_id='" . $row_clp["selfPacedTeachers_id"] . "'  ";
                $result_29 = mysqli_query($conn, $sql_29);
                $AllAssignments = mysqli_num_rows($result_29);
                if (mysqli_num_rows($result_29) > 0) {
                    while ($row_29 = mysqli_fetch_assoc($result_29)) {
                        $sql_30 = "select * from selfPacedTeacherAssignmentRating  WHERE selfPacedTeachersAssignment_id='" . $row_29["selfPacedTeachersAssignment_id"] . "'  ";
                        $result_30 = mysqli_query($conn, $sql_30);
                        if (mysqli_num_rows($result_30) > 0) {
                            while ($row_30 = mysqli_fetch_assoc($result_30)) {
                                $TotalRatingValue =  (int)$row_30["Relevance"] + (int)$row_30["WorkingCode"] + (int)$row_30["Creativity"] + (int)$row_30["TimelyCompletion"] + (int)$row_30["Completion"];
                                $TotalRatingNew = ($TotalRatingValue / 25) * 100;
                                if ($TotalRatingNew > 0) {
                                    $TotalRating =  $TotalRating +  $TotalRatingNew;
                                    $NoSessions =  $NoSessions + 1;
                                }
                            }
                        }
                        $sql_20 = "select * from selfPacedTeacherAssignmentRetry WHERE selfPacedTeachersAssignment_id='" . $row_30["selfPacedTeachersAssignment_id"] . "'   ";
                        $result_20 = mysqli_query($conn, $sql_20);
                        $NoRetries = mysqli_num_rows($result_20);
                        if ($NoRetries > 0) {
                            $TotalRetries =  $NoRetries + 1;
                        }
                    }
                }
                $sql_20 = "select * from selfPacedTeacherAssignment WHERE selfPacedTeachers_id='" . $row_clp["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;
                    $TotalPassRate = $TotalRating / $NoSessions;
                }


                $SessionTime = 0;
                $NoSessions = 0;
                $TotalSesionTime = 0;
                $sql_29 = "select * from selfPacedTeacherSession  WHERE selfPacedTeachers_id='" . $row_clp["selfPacedTeachers_id"] . "' AND SessionTime >'0'";
                $result_29 = mysqli_query($conn, $sql_29);
                $NoLessonsViewed = mysqli_num_rows($result_29);
                if (mysqli_num_rows($result_29) > 0) {
                    while ($row_29 = mysqli_fetch_assoc($result_29)) {
                        $SessionTime =  (int)$row_29["SessionTime"] + $SessionTime;
                        $NoSessions =  $NoSessions + 1;
                    }
                }
                if ($NoSessions > 0) {
                    $TotalSesionTime = $SessionTime / $NoSessions;
                }
                $sql_29 = "SELECT * FROM teachers WHERE teacher_id='" . $row_clp["teacher_id"] . "' ";
                $result_29 = mysqli_query($conn, $sql_29);
                if (mysqli_num_rows($result_29) > 0) {
                    while ($row_29 = mysqli_fetch_assoc($result_29)) {

                        $curl_post_data = array(
                            'teacher_id' => $row_29["teacher_id"],
                            'LearnerName' => $row_29["TeacherFirstName"],
                            'LearnerLastName' => $row_29["TeacherLastName"],
                            'Gender' => $row_29["Gender"],
                            'DateOfBirth' => $row_29["DateOfBirth"],
                            'Username' => $row_29["TeacherEmail"],
                            'TotalPassRate' => $TotalPassRate,
                            'Percentage' => $Percentage,
                            'GradedAssignments' => $GradedAssignments,
                            'AllAssignments' => $AllAssignments,
                            'TotalRetries' => $TotalRetries,
                            'TotalSesionTime' => $TotalSesionTime,

                        );
                        array_push($data, $curl_post_data);
                    }
                }
                $SessionTime = 0;
                $NoSessions = 0;
                $TotalSesionTime = 0;
            }
        }
    }
}


echo json_encode($data);
