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

$SessionTime = 0;
$NoSessions = 0;
$ViewPercentage = 0;
$ViewSessions = 0;

$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_2 = "select * from course_lesson_plan  WHERE course_id='" . $row_sfc["courses_id"] . "'  ";
        $result_2 = mysqli_query($conn, $sql_2);
        $NoLessonsCourse = mysqli_num_rows($result_2);

        $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)) {
                $sql_29 = "select * from selfPacedLearnerSession  WHERE selfPacedLearners_id='" . $row_clp["selfPacedLearners_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 ($NoLessonsViewed > 0) {
                    $ViewPercentageIndividual = ($NoLessonsViewed / $NoLessonsCourse) * 100;
                    $ViewPercentage =  $ViewPercentage + $ViewPercentageIndividual;
                    $ViewSessions =  $ViewSessions + 1;
                }
            }
        }
        $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)) {


                $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 ($NoLessonsViewed > 0) {
                    $ViewPercentageIndividual = ($NoLessonsViewed / $NoLessonsCourse) * 100;
                    $ViewPercentage =  $ViewPercentage + $ViewPercentageIndividual;
                    $ViewSessions =  $ViewSessions + 1;
                }
            }
        }
    }
}

$TotalSesionTime = $SessionTime / $NoSessions;
$TotalLessonView = $ViewPercentage / $ViewSessions;

$curl_post_data = array(
    'TotalSesionTime' => $TotalSesionTime,
    'TotalLessonView' => $TotalLessonView,

);
// array_push($prerequisite_courses, $curl_post_data);


echo json_encode($curl_post_data);
