<?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();
    

    $mentor_course_ids = array();
    $sql_d1="SELECT * FROM learners WHERE parent_id='".$_GET["parent_id"]."'";
    $result_d1 = mysqli_query($conn, $sql_d1);  
    if (mysqli_num_rows($result_d1) > 0) {
        while($row_d1 = mysqli_fetch_assoc($result_d1)) {

            $sql_lc="SELECT * FROM learner_class WHERE learner_id='".$row_d1["learner_id"]."' ";
            $result_lc = mysqli_query($conn, $sql_lc);  
            if (mysqli_num_rows($result_lc) > 0) {
                while($row_lc = mysqli_fetch_assoc($result_lc)) {
                    $coursessArray=array();
                    $course_name="";
                    $course_id="";
                    $class_name="";
                    $mnt_name="";
                    $sql_c_c_c="SELECT * FROM crs_clss_cntr WHERE center_class_id='".$row_lc["center_class_id"]."'";
                    $result_c_c_c = mysqli_query($conn, $sql_c_c_c);  
                    if (mysqli_num_rows($result_c_c_c) > 0) {
                        while($row_c_c_c = mysqli_fetch_assoc($result_c_c_c)) {
                            $course_id=$row_c_c_c["course_id"];
                            $sql_n="SELECT * FROM courses WHERE course_id='".$row_c_c_c["course_id"]."'";
                            $result_n = mysqli_query($conn, $sql_n ); 
                            if (mysqli_num_rows($result_n) > 0) {
                                while($row_n = mysqli_fetch_assoc($result_n)) {
                                    $course_name=$row_n["course_name"];
                                }
                            }
                            $sql_c_c="SELECT * FROM center_class WHERE center_class_id='".$row_c_c_c["center_class_id"]."'";
                            $result_c_c = mysqli_query($conn, $sql_c_c ); 
                            if (mysqli_num_rows($result_c_c) > 0) {
                                while($row_c_c = mysqli_fetch_assoc($result_c_c)) {
                                    $sql_n="SELECT * FROM classes WHERE class_id='".$row_c_c["class_id"]."'";
                                    $result_n = mysqli_query($conn, $sql_n ); 
                                    if (mysqli_num_rows($result_n) > 0) {
                                        while($row_n = mysqli_fetch_assoc($result_n)) {
                                            $class_name=$row_n["class_name"];
                                        }
                                    }
                                }
                            }
                            $sql_an="SELECT * FROM mentor_course WHERE center_class_cohort_id='".$row_lc["center_class_cohort_id"]."' AND cohort_session_id='".$row_lc["cohort_session_id"]."' AND crs_clss_cntr_id='".$row_c_c_c["crs_clss_cntr_id"]."' ";
                            $result_an = mysqli_query($conn, $sql_an);  
                            if (mysqli_num_rows($result_an) > 0) {
                                while($row_an = mysqli_fetch_assoc($result_an)) {
                                    $sql_e="SELECT * FROM mentor_lesson WHERE mentor_course_id='".$row_an["mentor_course_id"]."'";
                                    $result_e = mysqli_query($conn, $sql_e);  
                                    if (mysqli_num_rows($result_e) > 0) {
                                        while($row_e = mysqli_fetch_assoc($result_e)) {
                                            $sql_d="SELECT * FROM lesson_report WHERE learner_id='".$row_d1["learner_id"]."' AND mentor_lesson_id='".$row_e["mentor_lesson_id"]."'";
                                            $result_d = mysqli_query($conn, $sql_d);  
                                            if (mysqli_num_rows($result_d) > 0) {
                                                while($row_d = mysqli_fetch_assoc($result_d)) {
                                                    $mentor_name;
                                                    $sql_q="SELECT * FROM mentors WHERE mentor_id='".$row_d["mentor_id"]."'";
                                                    $result_q = mysqli_query($conn, $sql_q);  
                                                    if (mysqli_num_rows($result_q) > 0) {
                                                        while($row_q = mysqli_fetch_assoc($result_q)) {
                                                            $mentor_name= $row_q["mentor_name"];
                                                            $mnt_name= $row_q["mentor_name"];
                                                        }
                                                    }
                                                    $sql_qn="SELECT * FROM course_lesson_plan WHERE course_lesson_plan_id='".$row_e["course_lesson_plan_id"]."'";
                                                    $result_qn = mysqli_query($conn, $sql_qn);  
                                                    if (mysqli_num_rows($result_qn) > 0) {
                                                        while($row_qn = mysqli_fetch_assoc($result_qn)) {
                                                            $lesson_sub_topic=$row_qn["lesson_sub_topic"];
                                                            $lesson_topic=$row_qn["lesson_topic"];
                                                            $session=$row_qn["lesson_session"];
                                                            $start_time="";
                                                            $end_time="";
                                                            $outcomesArray=array();
                                                            $sql_g="SELECT * FROM course_desired_outcomes WHERE course_lesson_plan_id='".$row_e["course_lesson_plan_id"]."'";
                                                            $result_g = mysqli_query($conn, $sql_g);  
                                                            if (mysqli_num_rows($result_g) > 0) {
                                                                while($row_g = mysqli_fetch_assoc($result_g)) {
                                                                    $sql_h="SELECT * FROM lesson_r_desired_outcomes WHERE course_desired_outcomes_id='".$row_g["course_desired_outcomes_id"]."' AND lesson_report_id='".$row_d["lesson_report_id"]."'";
                                                                    $result_h = mysqli_query($conn, $sql_h);  
                                                                    if (mysqli_num_rows($result_h) > 0) {
                                                                        while($row_h = mysqli_fetch_assoc($result_h)) {
                                                                            $curl_post_data = array(
                                                                                'course_desired_outcome' => $row_g["course_desired_outcome"] ,
                                                                                'r_desired_outcome' => $row_h["r_desired_outcome"] ,
                                                                            );
                                                                            // $outcomesArray= $curl_post_data;
                                                                            array_push($outcomesArray, $curl_post_data );
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            $sql_g="SELECT * FROM time_slots WHERE time_slot_id='".$row_an["time_slot_id"]."'";
                                                            $result_g = mysqli_query($conn, $sql_g);  
                                                            if (mysqli_num_rows($result_g) > 0) {
                                                                while($row_g = mysqli_fetch_assoc($result_g)) {
                                                                    $start_time=$row_g["start_time"];
                                                                    $end_time=$row_g["end_time"];
                                                                   
                                                                }
                                                            }
                                                            $assignment_status="";
                                                            $assignmentsArray=array();
                                                            $sql_l_a="SELECT * FROM learner_assignment WHERE course_lesson_plan_id='".$row_e["course_lesson_plan_id"]."' AND learner_id='".$row_d1["learner_id"]."' ";
                                                            $result_l_a = mysqli_query($conn, $sql_l_a);   
                                                            if (mysqli_num_rows($result_l_a) > 0) {
                                                                while($row_l_a = mysqli_fetch_assoc($result_l_a)) {
                                                                   
                                                                    $reason="";
                                                                    $assignment_status=$row_l_a["status"];
                                                                    $learner_assignment="";
                                                                    $course_assignment="";
                                                                    $mentor_comments="";
                                                                    $relevance="";
                                                                    $working_code="";
                                                                    $creativity="";
                                                                    $timely_completion="";
                                                                    $completion="";
                                                                    if(file_exists("/home/sendusco/lms.digifunzi.com/".$row_l_a['assignment'])){
                                                                        $learner_assignment =file_get_contents("/home/sendusco/lms.digifunzi.com/".$row_l_a["assignment"]);
                                                                    }
                                                                    if($row_l_a["status"]=="Rejected"){
                                                                        if(file_exists("/home/sendusco/lms.digifunzi.com/".$row_l_a['reason'])){
                                                                            $reason =file_get_contents("/home/sendusco/lms.digifunzi.com/".$row_l_a["reason"]);
                                                                        }
                                                                    }else{
                                                                        $sql_l_a_r="SELECT * FROM learner_assignment_rating WHERE learner_assignment_id='".$row_l_a["learner_assignment_id"]."'";
                                                                        
                                                                        $result_l_a_r = mysqli_query($conn, $sql_l_a_r);  
                                                                        if (mysqli_num_rows($result_l_a_r) > 0) {
                                                                            while($row_l_a_r = mysqli_fetch_assoc($result_l_a_r)) {
                                                                                $relevance=$row_l_a_r["relevance"];
                                                                                $working_code=$row_l_a_r["working_code"];
                                                                                $creativity=$row_l_a_r["creativity"];
                                                                                $timely_completion=$row_l_a_r["timely_completion"];
                                                                                $completion=$row_l_a_r["completion"];
                                                                                $mentor_comments=$row_l_a_r["mentor_comments"];
                                                                                // if(file_exists("/home/sendusco/lms.digifunzi.com/".$row_l_a_r['mentor_comments'])){
                                                                                //     $mentor_comments =file_get_contents("/home/sendusco/lms.digifunzi.com/".$row_l_a_r["mentor_comments"]);
                                                                                // }else{
                                                                                //     $mentor_comments=$row_l_a_r["mentor_comments"]; 
                                                                                // }
                                                                            }
                                                                        }
                                                                    }
                                                                    $sql_1="SELECT * FROM course_lesson_assignment WHERE course_lesson_plan_id='".$_GET["course_lesson_plan_id"]."'";
                                                                    $result_1 = mysqli_query($conn, $sql_1);  
                                                                    if (mysqli_num_rows($result_1) > 0) {
                                                                        while($row_1 = mysqli_fetch_assoc($result_1)) {
                                                                            $course_assignment=$row_1["assignment"];
                                                                            // if(file_exists("/home/sendusco/lms.digifunzi.com/".$row_1['assignment'])){
                                                                            //     $course_assignment =file_get_contents("/home/sendusco/lms.digifunzi.com/".$row_1["assignment"]);
                                                                            // }
                                                                        }
                                                                    }
                                                        
                                                                    $curl_post_data = array(
                                                                        'learner_assignment_id' => $row_l_a["learner_assignment_id"] ,
                                                                        'AssignmentStatus' => $row_l_a["status"] ,
                                                                        'learner_assignment' => $learner_assignment ,
                                                                        'course_assignment' => $course_assignment ,
                                                                        'mentor_comments' => $mentor_comments ,
                                                                        'reason' => $reason ,
                                                                        'relevance' => $relevance ,
                                                                        'working_code' => $working_code ,
                                                                        'creativity' => $creativity ,
                                                                        'timely_completion' => $timely_completion ,
                                                                        'completion' => $completion ,
                                                                        'mentor_name' => $mentor_name ,
                                                                        'user_type'=>"Technical Mentor",
                                                                        'dated' => $row_l_a["dated"] ,
                                                                    );
                                                                    array_push($assignmentsArray, $curl_post_data );
                                                        
                                                        
                        
                                                                }
                                                            }
                                                        
                                                            $curl_post_data = array(
                                                                'learner_id' => $row_d1["learner_id"] ,
                                                                'learner_name' => $row_d1["learner_name"] ,
                                                                'learner_last_name' => $row_d1["learner_last_name"] ,
                                                                'learner_age' => $row_d1["learner_age"] ,
                                                                'learner_image' => $row_d1["learner_image"] ,
                                                                'status' => $row_d1["status"] ,
                                                                'course_name'=> $course_name,
                                                                'class_name'=> $class_name,
                                                                'homeschool' => $row_d1["homeschool"] ,
                                                                'mentor_lesson_id' => $row_e["mentor_lesson_id"] ,
                                                                'lesson_report_id' => $row_d["lesson_report_id"] ,
                                                                'report_type' => $row_d["type"] ,
                                                                'mentor_name' => $mentor_name ,
                                                                'user_type'=>"Technical Mentor",
                                                                'lesson_topic' => $row_qn["lesson_topic"] ,
                                                                'lesson_session' => $row_qn["lesson_session"] ,
                                                                'lesson_sub_topic' => $row_qn["lesson_sub_topic"] ,
                                                                'mentor_course_id' => $row_e["mentor_course_id"] ,
                                                                'course_lesson_plan_id' => $row_e["course_lesson_plan_id"] ,
                                                                'date_of_lesson' =>$row_e["date_of_lesson"] ,
                                                                'status' =>$row_e["status"] ,
                                                                'crs_clss_cntr_id' =>$row_an["crs_clss_cntr_id"] ,
                                                                'center_class_cohort_id' => $row_an["center_class_cohort_id"] ,
                                                                'cohort_session_id' => $row_an["cohort_session_id"] ,
                                                                'time_slot_id' => $row_an["time_slot_id"] ,
                                                                'assigned' =>$row_an["assigned"] ,
                                                                'coursestatus' =>$row_an["status"] ,
                                                                'crs_clss_cntr_id' =>$row_an["crs_clss_cntr_id"] ,
                                                                'start_time'=> $start_time,
                                                                'end_time'=> $end_time,
                                                                'outcomesArray'=> $outcomesArray,
                                                                'assignmentsArray'=> $assignmentsArray,
                                                                'assignment_status'=>$assignment_status,
                                                            );
                                                            array_push($coursessArray, $curl_post_data );
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    $curl_post_data = array(
                        'learner_id' => $row_d1["learner_id"] ,
                        'learner_name' => $row_d1["learner_name"] ,
                        'learner_last_name' => $row_d1["learner_last_name"] ,
                        'learner_age' => $row_d1["learner_age"] ,
                        'learner_image' => $row_d1["learner_image"] ,
                        'status' => $row_d1["status"] ,
                        'homeschool' => $row_d1["homeschool"] ,
                        'mentor_name' => $mnt_name ,
                        'course_name'=> $course_name,
                        'class_name'=> $class_name,
                        'course_id'=> $course_id,
                        'coursessArray'=> $coursessArray,
                    );
                    array_push($data, $curl_post_data );

                }
            }
        }
    }

    echo json_encode($data);
?>