<?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();
$SelfPacedCourses = array();


$sql_tlsn10 = "SELECT * FROM selfPacedAdultStudent WHERE AdultStudent_id='" . $_GET['AdultStudent_id'] . "' ";
$result_tlsn10 = mysqli_query($conn, $sql_tlsn10);
if (mysqli_num_rows($result_tlsn10) > 0) {
    while ($row_tlsn10 = mysqli_fetch_assoc($result_tlsn10)) {



        $sql_spc = "SELECT * FROM SelfPacedClasses WHERE SelfPacedClasses_id='" . $row_tlsn10["SelfPacedClasses_id"] . "'";
        $result_spc = mysqli_query($conn, $sql_spc);
        if (mysqli_num_rows($result_spc) > 0) {
            while ($row_spc = mysqli_fetch_assoc($result_spc)) {
                $class_name = "";
                $class_description = "";
                $age_from = "";
                $age_to = "";
                $StarterCourseName = "";


                $sql_d12 = "SELECT * FROM classes WHERE class_id='" . $row_spc["class_id"] . "'";
                $result_d12 = mysqli_query($conn, $sql_d12);
                if (mysqli_num_rows($result_d12) > 0) {
                    while ($row_d12 = mysqli_fetch_assoc($result_d12)) {
                        $class_name = $row_d12["class_name"];
                        $class_description = $row_d12["class_description"];
                        $age_from = $row_d12["age_from"];
                        $age_to = $row_d12["age_to"];
                    }
                }
                $sql_d12 = "SELECT * FROM courses WHERE course_id='" . $row_spc["StarterCourse_id"] . "'";
                $result_d12 = mysqli_query($conn, $sql_d12);
                if (mysqli_num_rows($result_d12) > 0) {
                    while ($row_d12 = mysqli_fetch_assoc($result_d12)) {
                        $StarterCourseName = $row_d12["course_name"];
                    }
                }
                $sql_d12 = "SELECT * FROM SelfPacedCourses WHERE SelfPacedClasses_id='" . $row_spc["SelfPacedClasses_id"] . "' ORDER BY orderNo ASC";
                $result_d12 = mysqli_query($conn, $sql_d12);
                if (mysqli_num_rows($result_d12) > 0) {
                    while ($row_d12 = mysqli_fetch_assoc($result_d12)) {
                        $sql_clp = "SELECT * FROM courses WHERE  course_id='" . $row_d12['courses_id'] . "'";
                        $result_clp = mysqli_query($conn, $sql_clp);
                        if (mysqli_num_rows($result_clp) > 0) {
                            while ($row_clp = mysqli_fetch_assoc($result_clp)) {
                                $Status = "Inactive";
                                $VideoName = "";
                                $VideoDuration = "";
                                $Video = "";
                                $course_description = '';
                                $AllLessons = 0;
                                $GradedLessons = 0;
                                $Percentage = 0;
                                $TotalRating = 0;
                                $TotalPassRate = 0;
                                $Available = 0;
                                $Assignments = array();

                                // $sql_spl = "SELECT * FROM selfPacedAdultStudent WHERE AdultStudent_id='" . $_GET['AdultStudent_id'] . "' AND SelfPacedCourses_id='" . $row_d12['SelfPacedCourses_id'] . "' ";

                                // $result_spl = mysqli_query($conn, $sql_spl);
                                // if (mysqli_num_rows($result_spl) > 0) {
                                //     while ($row_spl = mysqli_fetch_assoc($result_spl)) {
                                $Status = "Active";

                                $sql_civ = "SELECT * FROM selfPacedAdultStudentAssignment WHERE  SelfPacedCourses_id='" . $row_d12['SelfPacedCourses_id'] . "' AND   selfPacedAdultStudent_id='" . $row_tlsn10['selfPacedAdultStudent_id'] . "'";
                                $result_civ = mysqli_query($conn, $sql_civ);
                                $AllLessonsNew = mysqli_num_rows($result_civ);

                                $sql_civ = "SELECT * FROM selfPacedAdultStudentAssignment WHERE  SelfPacedCourses_id='" . $row_d12['SelfPacedCourses_id'] . "' AND   selfPacedAdultStudent_id='" . $row_tlsn10['selfPacedAdultStudent_id'] . "' AND Status='Graded'";
                                $result_civ = mysqli_query($conn, $sql_civ);
                                $GradedLessonsNew = mysqli_num_rows($result_civ);

                                $sql_civ = "SELECT * FROM selfPacedAdultStudentAssignment WHERE  SelfPacedCourses_id='" . $row_d12['SelfPacedCourses_id'] . "' AND   selfPacedAdultStudent_id='" . $row_tlsn10['selfPacedAdultStudent_id'] . "' AND Status='Submitted'";
                                $result_civ = mysqli_query($conn, $sql_civ);
                                $SubmittedLessonsNew = mysqli_num_rows($result_civ);

                                $FinishedAssignments = (int)$GradedLessonsNew + (int)$SubmittedLessonsNew;
                                $AllLessons = $AllLessonsNew;
                                $GradedLessons = $GradedLessonsNew;


                                if ($AllLessonsNew > 0 && $FinishedAssignments > 0) {
                                    $Percentage = ($FinishedAssignments / $AllLessonsNew) * 100;
                                }

                                $sql_29 = "select * from selfPacedAdultStudentAssignment  WHERE selfPacedAdultStudent_id='" . $row_tlsn10["selfPacedAdultStudent_id"] . "' AND Status='Graded' ";
                                $result_29 = mysqli_query($conn, $sql_29);
                                if (mysqli_num_rows($result_29) > 0) {
                                    while ($row_29 = mysqli_fetch_assoc($result_29)) {
                                        $selfPacedAdultStudentAssignmentRating_id;
                                        $Relevance;
                                        $WorkingCode;
                                        $Creativity;
                                        $TimelyCompletion;
                                        $Completion;
                                        $MentorComments;
                                        $Status = $row_29['Status'];
                                        $PassMark = "";
                                        $TotalRatingValue = 0;
                                        $TotalRatingNew = 0;
                                        $MentorImage = "";
                                        $MentorName = "";
                                        $MentorEmail = "";
                                        $sql_30 = "select * from selfPacedAdultStudentAssignmentRating  WHERE selfPacedAdultStudentAssignment_id='" . $row_29["selfPacedAdultStudentAssignment_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;
                                                }
                                                $PassMark = "";
                                                if ($TotalRatingNew  > 4) {
                                                    $PassMark = "Exceeding";
                                                } else if ($TotalRatingNew  > 3) {
                                                    $PassMark = "Meeting";
                                                } else if ($TotalRatingNew  > 2) {
                                                    $PassMark = "Approaching";
                                                } else {
                                                    $PassMark = "Below";
                                                }
                                                $selfPacedAdultStudentAssignmentRating_id = $row_30['selfPacedAdultStudentAssignmentRating_id'];
                                                $Relevance = $row_30['Relevance'];
                                                $WorkingCode = $row_30['WorkingCode'];
                                                $Creativity = $row_30['Creativity'];
                                                $TimelyCompletion = $row_30['TimelyCompletion'];
                                                $Completion = $row_30['Completion'];
                                                $PassMark = $PassMark;
                                                $TotalRatingValue = $TotalRatingValue;
                                                $TotalRatingNew = $TotalRatingNew;
                                                $MentorComments = $row_30['MentorComments'];

                                                $sql_civ = "SELECT * FROM MentorAdultAssignmentRating WHERE  selfPacedAdultStudentAssignmentRating_id='" . $row_30['selfPacedAdultStudentAssignmentRating_id'] . "'";
                                                $result_civ = mysqli_query($conn, $sql_civ);
                                                if (mysqli_num_rows($result_civ) > 0) {
                                                    while ($row_civ = mysqli_fetch_assoc($result_civ)) {
                                                        $sql_civ_10 = "SELECT * FROM mentors WHERE  mentor_id='" . $row_civ['mentor_id'] . "'";
                                                        $result_civ_10 = mysqli_query($conn, $sql_civ_10);
                                                        if (mysqli_num_rows($result_civ_10) > 0) {
                                                            while ($row_civ_10 = mysqli_fetch_assoc($result_civ_10)) {
                                                                $MentorImage = $row_civ_10['mentor_image'];
                                                                $MentorName = $row_civ_10['mentor_name'];
                                                                $MentorEmail = $row_civ_10['mentor_email'];
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        $lesson_topic;
                                        $lesson_session;
                                        $sql_civ = "SELECT * FROM course_lesson_plan WHERE  course_lesson_plan_id='" . $row_29['course_lesson_plan_id'] . "'";
                                        $result_civ = mysqli_query($conn, $sql_civ);
                                        if (mysqli_num_rows($result_civ) > 0) {
                                            while ($row_civ = mysqli_fetch_assoc($result_civ)) {
                                                $lesson_topic = $row_civ['lesson_topic'];
                                                $lesson_session = $row_civ['lesson_session'];
                                            }
                                        }
                                        $NoRetries = 0;
                                        $sql_civ = "SELECT * FROM selfPacedAdultStudentAssignmentRetry WHERE  selfPacedAdultStudentAssignment_id='" . $row_29['selfPacedAdultStudentAssignment_id'] . "'";
                                        $result_civ = mysqli_query($conn, $sql_civ);
                                        $NoRetries = mysqli_num_rows($result_civ);
                                        $curl_post_data = array(
                                            'selfPacedAdultStudentAssignment_id' => $row_29['selfPacedAdultStudentAssignment_id'],
                                            'course_lesson_plan_id' => $row_29['course_lesson_plan_id'],
                                            'Assignment' => $row_29['Assignment'],
                                            'Status' => $row_29['Status'],
                                            'Reason' => $row_29['Reason'],
                                            'dated' => $row_29['dated'],
                                            'selfPacedAdultStudentAssignmentRating_id' => $selfPacedAdultStudentAssignmentRating_id,
                                            'Relevance' => $Relevance,
                                            'WorkingCode' => $WorkingCode,
                                            'Creativity' => $Creativity,
                                            'TimelyCompletion' => $TimelyCompletion,
                                            'Completion' => $Completion,
                                            'MentorComments' => $MentorComments,
                                            'MentorImage' => $MentorImage,
                                            'MentorName' => $MentorName,
                                            'MentorEmail' => $MentorEmail,
                                            'lesson_topic' => $lesson_topic,
                                            'lesson_session' => $lesson_session,
                                            'PassMark' => $PassMark,
                                            'TotalRatingValue' => $TotalRatingValue,
                                            'TotalRatingNew' => $TotalRatingNew,
                                            'NoRetries' => $NoRetries,
                                        );
                                        array_push($Assignments, $curl_post_data);

                                        $selfPacedAdultStudentAssignmentRating_id = '';
                                        $Relevance = '';
                                        $WorkingCode = '';
                                        $Creativity = '';
                                        $TimelyCompletion = '';
                                        $Completion = '';
                                        $MentorComments = '';
                                        $PassMark = "";
                                        $TotalRatingValue = 0;
                                        $TotalRatingNew = 0;
                                    }
                                }
                                //     }
                                // }
                                $sql_civ = "SELECT * FROM CourseIntroductionVideo WHERE  SelfPacedCourses_id='" . $row_d12['SelfPacedCourses_id'] . "'";
                                $result_civ = mysqli_query($conn, $sql_civ);
                                if (mysqli_num_rows($result_civ) > 0) {
                                    while ($row_civ = mysqli_fetch_assoc($result_civ)) {
                                        $VideoName = $row_civ["VideoName"];
                                        $VideoDuration = $row_civ["VideoDuration"];
                                        $Video = $row_civ["Video"];
                                    }
                                }
                                $sql_civ = "SELECT * FROM course_lesson_plan WHERE  course_id='" . $row_d12['courses_id'] . "'";
                                $result_civ = mysqli_query($conn, $sql_civ);
                                $NoLessons = mysqli_num_rows($result_civ);
                                if (file_exists("/home/sendusco/app/" . $row_clp["course_description"])) {
                                    $course_description = file_get_contents("/home/sendusco/app/" . $row_clp["course_description"]);
                                }

                                if ($TotalRating > 0) {
                                    $TotalPassRate = $TotalRating / $NoSessions;
                                }
                                if (!in_array($row_tlsn10["SelfPacedCourses_id"], $SelfPacedCourses)) {
                                    $curl_post_data = array(
                                        'SelfPacedCourses_id' => $row_d12["SelfPacedCourses_id"],
                                        'SelfPacedClasses_id' => $row_d12["SelfPacedClasses_id"],
                                        'orderNo' => $row_d12["orderNo"],
                                        'course_name' => $row_clp["course_name"],
                                        'course_image' => $row_clp["course_image"],
                                        'course_description' => $course_description,
                                        'course_id' => $row_clp["course_id"],
                                        'orderNo' => $row_d12["orderNo"],
                                        'Price' => $row_d12["Price"],
                                        'Status' => $Status,
                                        'NoLessons' => $NoLessons,
                                        'VideoName' => $VideoName,
                                        'VideoDuration' => $VideoDuration,
                                        'Video' => $Video,
                                        'AllLessons' => $AllLessons,
                                        'GradedLessons' => $GradedLessons,
                                        'Percentage' => $Percentage,
                                        'TotalRating' => $TotalRating,
                                        'TotalPassRate' => $TotalPassRate,
                                        'Assignments' => $Assignments,
                                    );
                                    array_push($data, $curl_post_data);

                                    array_push($SelfPacedCourses, $row_tlsn10["SelfPacedCourses_id"]);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
echo json_encode($data);
