<?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 = "select * from SelfPacedClasses  WHERE SelfPacedClasses_id='" . $row_sfc["SelfPacedClasses_id"] . "'  ";
        $result = mysqli_query($conn, $sql);
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_assoc($result)) {

                $Lessons = array();
                $CourseName = "";
                $ClassName = "";
                $CourseImage = "";
                $CourseTime = 0;

                $sql_20 = "select * from classes WHERE class_id='" . $row["class_id"] . "'  ";
                $result_20 = mysqli_query($conn, $sql_20);
                if (mysqli_num_rows($result_20) > 0) {
                    while ($row_20 = mysqli_fetch_assoc($result_20)) {
                        $ClassName = $row_20["class_name"];
                    }
                }

                $sql_clp = "select * from courses  WHERE course_id='" . $row_sfc["courses_id"] . "'  ";
                $result_clp = mysqli_query($conn, $sql_clp);
                if (mysqli_num_rows($result_clp) > 0) {
                    while ($row_clp = mysqli_fetch_assoc($result_clp)) {
                        $CourseName = $row_clp["course_name"];
                        $CourseImage = $row_clp["course_image"];
                    }
                }

                $sql_clp = "select * from selfPacedAdultStudent  WHERE SelfPacedCourses_id='" . $row_sfc["SelfPacedCourses_id"] . "' AND SelfPacedClasses_id='" . $row["SelfPacedClasses_id"] . "'   ";
                $result_clp = mysqli_query($conn, $sql_clp);
                if (mysqli_num_rows($result_clp) > 0) {
                    $LastLogin = null;
                    $Status = "Inactive";
                    $LearnerName = "";
                    $LearnerLastName = "";
                    $Gender = "";
                    $Image = "";
                    $Username = "";
                    $IssuedAssignments = 0;
                    $AllAssignments = 0;
                    $GradedAssignments = 0;
                    $Percentage = 0;
                    $NoCourses = mysqli_num_rows($result_clp);
                    while ($row_clp = mysqli_fetch_assoc($result_clp)) {
                        $IssuedAssignments = 0;
                        $AllAssignments = 0;
                        $CourseTime = $row_clp["CourseTime"];
                        $sql_20 = "select * from selfPacedAdultStudentAssignment WHERE selfPacedAdultStudent_id='" . $row_clp["selfPacedAdultStudent_id"] . "' AND Status='Issued' OR Status='Re-Issued' AND Status='Submitted' ";
                        $result_20 = mysqli_query($conn, $sql_20);
                        $IssuedAssignments = mysqli_num_rows($result_20);

                        $sql_20 = "select * from selfPacedAdultStudentAssignment WHERE selfPacedAdultStudent_id='" . $row_clp["selfPacedAdultStudent_id"] . "'   ";
                        $result_20 = mysqli_query($conn, $sql_20);
                        $AllAssignments = mysqli_num_rows($result_20);
                        $sql_20 = "select * from selfPacedAdultStudentAssignment WHERE selfPacedAdultStudent_id='" . $row_clp["selfPacedAdultStudent_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;
                        } else {
                            $Percentage = 0;
                        }
                        // $Percentage = ($GradedAssignments / $AllAssignments) * 100;
                        $sql_20 = "select * from AdultStudents WHERE AdultStudent_id='" . $row_clp["AdultStudent_id"] . "'   ";
                        $result_20 = mysqli_query($conn, $sql_20);
                        if (mysqli_num_rows($result_20) > 0) {
                            while ($row_20 = mysqli_fetch_assoc($result_20)) {
                                $sql_aa = "select * from ActiveUsers WHERE UserType='AdultStudent' AND UserID='" . $row_clp["AdultStudent_id"] . "'   ";

                                $result_aa = mysqli_query($conn, $sql_aa);
                                if (mysqli_num_rows($result_aa) > 0) {
                                    while ($row_aa = mysqli_fetch_assoc($result_aa)) {
                                        $LastLogin = $row_aa["TimeLogged"];
                                    }
                                }
                                $sql_aa = "select * from ActiveUsers WHERE UserType='AdultStudent' AND UserID='" . $row_clp["AdultStudent_id"] . "' AND TimeLogged > DATE_SUB(NOW(), INTERVAL 3 MONTH) ";
                                $result_aa = mysqli_query($conn, $sql_aa);
                                if (mysqli_num_rows($result_aa) > 0) {
                                    $Status = "Active";
                                }

                                $curl_post_data = array(
                                    'Role' => "Adult",
                                    'CourseName' => $CourseName,
                                    'ClassName' => $ClassName,
                                    'CourseImage' => $CourseImage,
                                    'CourseTime' => $CourseTime,
                                    'IssuedAssignments' => $IssuedAssignments,
                                    'AllAssignments' => $AllAssignments,
                                    'GradedAssignments' => $GradedAssignments,
                                    'Percentage' => $Percentage,
                                    'LastLogin' => $LastLogin,
                                    'Status' => $Status,
                                    'NoCourses' => $NoCourses,
                                    'SelfPacedClasses_id' => $row_sfc["SelfPacedClasses_id"],
                                    'orderNo' => $row_sfc["orderNo"],
                                    'Price' => $row_sfc["Price"],
                                    'Active' => $row_sfc["Active"],
                                    'LearnerName' => $row_20["FirstName"],
                                    'LearnerLastName' => $row_20["LastName"],
                                    'Gender' => $row_20["Gender"],
                                    'Image' => $row_20["Image"],
                                    'Username' =>  $row_20["Email"],

                                );
                                array_push($data, $curl_post_data);
                            }
                        }
                    }
                }
                $sql_clp = "select * from selfPacedLearners  WHERE SelfPacedCourses_id='" . $row_sfc["SelfPacedCourses_id"] . "'  AND SelfPacedClasses_id='" . $row["SelfPacedClasses_id"] . "'";
                $result_clp = mysqli_query($conn, $sql_clp);
                if (mysqli_num_rows($result_clp) > 0) {
                    $LastLogin = null;
                    $Status = "Inactive";
                    $LearnerName = "";
                    $LearnerLastName = "";
                    $Gender = "";
                    $Image = "";
                    $Username = "";
                    $IssuedAssignments = 0;
                    $AllAssignments = 0;
                    $GradedAssignments = 0;
                    $Percentage = 0;
                    $NoCourses = mysqli_num_rows($result_clp);
                    while ($row_clp = mysqli_fetch_assoc($result_clp)) {
                        $IssuedAssignments = 0;
                        $AllAssignments = 0;
                        $CourseTime = $row_clp["CourseTime"];
                        $sql_20 = "select * from selfPacedLearnerAssignment WHERE selfPacedLearners_id='" . $row_clp["selfPacedLearners_id"] . "' AND Status='Issued' OR Status='Re-Issued' AND Status='Submitted' ";
                        $result_20 = mysqli_query($conn, $sql_20);
                        $IssuedAssignments = mysqli_num_rows($result_20);

                        $sql_20 = "select * from selfPacedLearnerAssignment WHERE selfPacedLearners_id='" . $row_clp["selfPacedLearners_id"] . "'   ";
                        $result_20 = mysqli_query($conn, $sql_20);
                        $AllAssignments = mysqli_num_rows($result_20);
                        $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;
                        } else {
                            $Percentage = 0;
                        }
                        // $Percentage = ($GradedAssignments / $AllAssignments) * 100;
                        $sql_20 = "select * from students WHERE student_id='" . $row_clp["student_id"] . "'   ";
                        $result_20 = mysqli_query($conn, $sql_20);
                        if (mysqli_num_rows($result_20) > 0) {

                            while ($row_20 = mysqli_fetch_assoc($result_20)) {

                                $sql_aa = "select * from ActiveUsers WHERE UserType='Learner' AND UserID='" . $row_clp["student_id"] . "'   ";
                                $result_aa = mysqli_query($conn, $sql_aa);
                                if (mysqli_num_rows($result_aa) > 0) {
                                    while ($row_aa = mysqli_fetch_assoc($result_aa)) {
                                        $LastLogin = $row_aa["TimeLogged"];
                                    }
                                }
                                $sql_aa = "select * from ActiveUsers WHERE UserType='Learner' AND UserID='" . $row_clp["student_id"] . "' AND TimeLogged > DATE_SUB(NOW(), INTERVAL 3 MONTH) ";
                                $result_aa = mysqli_query($conn, $sql_aa);
                                if (mysqli_num_rows($result_aa) > 0) {
                                    $Status = "Active";
                                }
                                $LearnerName = $row_20["learner_name"];
                                $LearnerLastName = $row_20["learner_last_name"];
                                $Gender = $row_20["learner_gender"];
                                $Image = $row_20["learner_image"];
                                $Username = $row_20["username"];
                            }
                        }
                    }

                    $curl_post_data = array(
                        'Role' => "Learner",
                        'CourseName' => $CourseName,
                        'ClassName' => $ClassName,
                        'CourseImage' => $CourseImage,
                        'CourseTime' => $CourseTime,
                        'IssuedAssignments' => $IssuedAssignments,
                        'AllAssignments' => $AllAssignments,
                        'GradedAssignments' => $GradedAssignments,
                        'Percentage' => $Percentage,
                        'LastLogin' => $LastLogin,
                        'Status' => $Status,
                        'NoCourses' => $NoCourses,
                        'SelfPacedClasses_id' => $row_sfc["SelfPacedClasses_id"],
                        'orderNo' => $row_sfc["orderNo"],
                        'Price' => $row_sfc["Price"],
                        'Active' => $row_sfc["Active"],
                        'LearnerName' => $LearnerName,
                        'LearnerLastName' => $LearnerLastName,
                        'Gender' => $Gender,
                        'Image' => $Image,
                        'Username' => $Username,

                    );
                    array_push($data, $curl_post_data);
                }
                $sql_clp = "select * from selfPacedTeachers  WHERE SelfPacedCourses_id='" . $row_sfc["SelfPacedCourses_id"] . "'  AND SelfPacedClasses_id='" . $row["SelfPacedClasses_id"] . "'";
                $result_clp = mysqli_query($conn, $sql_clp);
                if (mysqli_num_rows($result_clp) > 0) {
                    $LastLogin = null;
                    $Status = "Inactive";
                    $LearnerName = "";
                    $LearnerLastName = "";
                    $Gender = "";
                    $Image = "";
                    $Username = "";
                    $IssuedAssignments = 0;
                    $AllAssignments = 0;
                    $GradedAssignments = 0;
                    $Percentage = 0;
                    $NoCourses = mysqli_num_rows($result_clp);
                    while ($row_clp = mysqli_fetch_assoc($result_clp)) {

                        $sql_20 = "select * from selfPacedTeacherAssignment WHERE selfPacedTeachers_id='" . $row_clp["selfPacedTeachers_id"] . "' AND Status='Issued' OR Status='Re-Issued' AND Status='Submitted' ";
                        $result_20 = mysqli_query($conn, $sql_20);
                        $IssuedAssignments = mysqli_num_rows($result_20);

                        $sql_20 = "select * from selfPacedTeacherAssignment WHERE selfPacedTeachers_id='" . $row_clp["selfPacedTeachers_id"] . "'   ";
                        $result_20 = mysqli_query($conn, $sql_20);
                        $AllAssignments = mysqli_num_rows($result_20);

                        $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;
                        } else {
                            $Percentage = 0;
                        }
                        $sql_20 = "select * from teachers WHERE teacher_id='" . $row_clp["teacher_id"] . "'   ";
                        $result_20 = mysqli_query($conn, $sql_20);
                        if (mysqli_num_rows($result_20) > 0) {
                            while ($row_20 = mysqli_fetch_assoc($result_20)) {
                                $LastLogin = null;
                                $Status = "Inactive";
                                $sql_aa = "select * from ActiveUsers WHERE UserType='Teacher' AND UserID='" . $row_clp["teacher_id"] . "'   ";
                                $result_aa = mysqli_query($conn, $sql_aa);
                                if (mysqli_num_rows($result_aa) > 0) {
                                    while ($row_aa = mysqli_fetch_assoc($result_aa)) {
                                        $LastLogin = $row_aa["TimeLogged"];
                                    }
                                }
                                $sql_aa = "select * from ActiveUsers WHERE UserType='Teacher' AND UserID='" . $row_clp["teacher_id"] . "'  AND TimeLogged > DATE_SUB(NOW(), INTERVAL 3 MONTH)  ";
                                $result_aa = mysqli_query($conn, $sql_aa);
                                if (mysqli_num_rows($result_aa) > 0) {
                                    $Status = "Active";
                                }
                                $LearnerName = $row_20["TeacherFirstName"];
                                $LearnerLastName = $row_20["TeacherLastName"];
                                $Gender = $row_20["Gender"];
                                $Image = $row_20["TeacherImage"];
                                $Username = $row_20["TeacherEmail"];
                            }
                        }
                    }
                    $curl_post_data = array(
                        'Role' => "Teacher",
                        'CourseName' => $CourseName,
                        'ClassName' => $ClassName,
                        'CourseImage' => $CourseImage,
                        'CourseTime' => $CourseTime,
                        'IssuedAssignments' => $IssuedAssignments,
                        'AllAssignments' => $AllAssignments,
                        'GradedAssignments' => $GradedAssignments,
                        'Percentage' => $Percentage,
                        'LastLogin' => $LastLogin,
                        'Status' => $Status,
                        'NoCourses' => $NoCourses,
                        'SelfPacedClasses_id' => $row_sfc["SelfPacedClasses_id"],
                        'orderNo' => $row_sfc["orderNo"],
                        'Price' => $row_sfc["Price"],
                        'Active' => $row_sfc["Active"],
                        'LearnerName' => $LearnerName,
                        'LearnerLastName' => $LearnerLastName,
                        'Gender' => $Gender,
                        'Image' => $Image,
                        'Username' => $Username,

                    );
                    array_push($data, $curl_post_data);
                }
            }
        }
    }
}

echo json_encode($data);
