<?php
session_start();
include_once '../config/database.php';
include_once '../security/session_config.php';

include_once "../resources/offering.php";
include_once '../config/properties.php';

$offering = new Offering();
$offering->user_id = $_GET['user_id'];
$offering->date = $date;

$page = $_GET['page'];
$user_id = $_GET['user_id'];

$records_per_page = 8; // set records or rows of data per page
$from_record_num = ($records_per_page * $page) - $records_per_page; // calculate for the query LIMIT clause

$html = "";
$total_rows = 0;

$stmt = $offering->getCourses_Home($from_record_num, $records_per_page);
$num = $stmt->rowCount();
if ($num > 0) {
    $total_rows = $offering->getCourses_Home_count();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);
        $time1 = date("d M Y", strtotime($start_date));
        $time2 = date("d M Y", strtotime($end_date));
        //Direct assessment check -- 31-01-2023
        $offering->offering_id = $offering_id;
        $offering->user_id = $user_id;
        $current_attempt = 0;
        $score = 0;
        $passing_score_1 = 0;
        $stmt_taketest = $offering->offering_display();
        while ($row1 = $stmt_taketest->fetch(PDO::FETCH_ASSOC)) {
            extract($row1);
            $stmt3 = $offering->offering_current_attempt();
            if ($row3 = $stmt3->fetch(PDO::FETCH_ASSOC)) {
                extract($row3);
                if ($attempt_no == NULL) {
                    $current_attempt++;
                } else {
                    $current_attempt = $attempt_no + 1;
                }
            }

            $offering->assessment_id = $final_assmt_id;
            $stmt4 = $offering->get_user_score();
            if ($row4 = $stmt4->fetch(PDO::FETCH_ASSOC)) {
                extract($row4);
                $score = $score;
                $passing_score_1 = $pass_score;
            }
        }

        $lps = false;
        $course_names = "";
        $course_names_1 = "";
        if ($result == null) {
            $offering->offering_id = $offering_id;
            $offering->user_id = $user_id;
            $lp_stmt = $offering->get_learning_paths();
            while ($lp_rows = $lp_stmt->fetch(PDO::FETCH_ASSOC)) {

                $seq_no1 = $lp_rows['seq_no'] - 1;
                $lp_stmt2 = $offering->check_prev_learning_paths($lp_rows['learning_path_id'], $seq_no1, $user_id);
                while ($lp_rows2 = $lp_stmt2->fetch(PDO::FETCH_ASSOC)) {
                    if ($lp_rows2['count'] != 0) {
                        if ($lp_rows2['result'] == null && $lp_rows2['status']=='Active') {
                            if ($course_names == "") {
                                $course_names = $lp_rows2['course_name'];
                            } else {
                                $course_names = $course_names . ',' . $lp_rows2['course_name'];
                            }
                            $lps = true;
                        }
                    } else {
                        $lp_stmt3 = $offering->learning_paths_course_name($lp_rows['learning_path_id'], $seq_no1);
                        while ($lp_rows3 = $lp_stmt3->fetch(PDO::FETCH_ASSOC)) {
                            if ($course_names_1 == "") {
                                $course_names_1 = $lp_rows3['course_name'];
                            } else {
                                $course_names_1 = $course_names_1 . ',' . $lp_rows3['course_name'];
                            }
                            $lps = true;
                        }
                    }
                }
            }
        }

        $html .= "<div class='w-1/4 xl:w-1/3 lg:w-1/2 sm:w-1/1'>";
        $html .= "<div class='coursesCard -type-1 px-8 py-10 border-light bg-white rounded-8' id='main_div'>";
        $html .= "<a";

        //Designation
        //Designation has Learning Path
        //Learning Path has this offering id
        //this offering id has dependency
        //Pervious course/assessment is complete
        //Check Learning Path Dependency

        if (!$lps) {
            if ($offering_type != Null) {
                if ($result == Null) {
                    if ($end_date >= $date) {
                        if ($start_date <= $date) {
                            if ($assmt_startdate != null && $assmt_enddate != null) {
                                if ($assmt_startdate <= $date_time && $assmt_enddate >= $date_time) {
                                    //                               echo "<a href='courses-single-1.html' class='coursesCard -type-1'>";
                                    $html .= " href='javascript:void(0)'  onclick='takeTest(\"" . $current_attempt . "\", \"" . $max_attempts . "\", \"" . $score . "\", \"" . $passing_score . "\",\"" . $final_assmt_id . "\", \"" . $offering_id . "\", \"" . $assmt_duration . "\", \"" . $passing_score_1 . "\")'>";
                                } else {
                                    $html .= " href='javascript:void(0)'  onclick='alert(\"" . _("CRT3111") ." ". date("d M Y h:i A", strtotime($assmt_startdate)) ." ".  _("CRT3112") ." ". date("d M Y h:i A", strtotime($assmt_enddate)) . "\");'>";
                                }
                            } else {
                                $html .= "  href='javascript:void(0)'  onclick='takeTest(\"" . $current_attempt . "\", \"" . $max_attempts . "\", \"" . $score . "\", \"" . $passing_score . "\",\"" . $final_assmt_id . "\", \"" . $offering_id . "\", \"" . $assmt_duration . "\", \"" . $passing_score_1 . "\")'>";
                            }
                        } else {
                            $html .= " href='javascript:void(0)'  onclick='alert(\"" . _("CRT3109") . date("d M Y", strtotime($start_date)). "\");'>";
                        }
                    } else {
                        $html .= " href='javascript:void(0)'  onclick='alert(\"" . _("CRT3110") . "\");'>";
                    }
                } else if ($result == 'awaited') {
                    $html .= " href='javascript:void(0)' onclick='alert(\"" . _("SU0103") . "\");'>";
                } else {
                    $html .= " href='courseview.php?offering={$offering_id}' >";
                }
            } else {
                $html .= " href='courseview.php?offering={$offering_id}'>";
            }
        } else {
            $error_msg = "";
            if ($course_names != "") {
                $course_names = implode(',', array_unique(explode(',', $course_names)));
                $error_msg = $error_msg . sprintf(_("CRT3115"), $course_names);
            } else if ($course_names_1 != "") {
                $course_names_1 = implode(',', array_unique(explode(',', $course_names_1)));
                $error_msg = $error_msg . sprintf(_("CRT3116"), $course_names_1, $course_names_1);
            }
            $html .= " href='javascript:void(0)'  onclick='alert(\"" . $error_msg . "\");'>";
        }

        $filepath = ADMIN_HTTP_PATH . $properties['jdbc.offer_image'] . "/" . $media_path;

        $html .= "<div class='relative course_image_div'>";
        $html .= "<div class='coursesCard__image overflow-hidden rounded-8'>";

        if ($media_path == "" || $media_path == NULL) {
            $html .= "<img src='" . DEFAULT_COURSE_IMAGE . "' alt='" . htmlentities($course_name) . "' title='" . htmlentities($course_name) . "' class='w-1/1'>";
        } else {
            $html .= "<img src='" . $filepath . "' alt='" . htmlentities($course_name) . "' title='" . htmlentities($course_name) . "' class='w-1/1'>";
        }

        $html .= "<div class='coursesCard__image_overlay rounded-8  w-1/1'></div>";
        $html .= "</div>";
        $html .= "<div class='d-flex justify-between absolute-button z-3'>";
        $html .= "<div>";
        $html .= "<span class='size-50-25 d-flex items-center justify-center rounded-8 bg-white'>";

        if ($end_date >= $date) {
            $html .= _("CR0104");
        } else {
            $html .= _("CR0105");
        }

        $html .= " </span>";
        $html .= "</div>";
        $html .= "</div>";
        $html .= "</div>";

        $html .= "<div class='py-20'>";
        $html .= "<div class='d-flex y-gap-10 justify-between items-center'>";
        $html .= "<div>" . $time1 . "</div>";
        $html .= "<div>" . $time2 . "</div>";
        $html .= "</div>";
        $html .= "<h3 class='text-16 fw-500 lh-15 mt-10 course_name'>" . htmlentities($course_name) . "</h3>";

        if ($content_count == 0) {

            $html .= "<div class='d-flex y-gap-10 justify-between items-center mt-3 mb-3'>";
            $html .= "<div class='text-red-1'>" . _("HM0118") . "</div>";
            $html .= "</div>";
        } else {
            $html .= "<div class='progress mb-0'>";
            $html .= "<div class='progress-bar progress-bar-info' role='progressbar' aria-valuemin='0' aria-valuemax='100' style='width:" . $content_completion . "%'>";
            $html .= "</div>";
            $html .= "</div>";
            $html .= "<div class='d-flex y-gap-10 justify-between items-center mt-3 mb-3'>";
            $html .= "<div class='text-dark-1'>" . $content_completion . "% "
                    . _('CRT2108')
                    . "</div>";
            $html .= "</div>  ";
        }
        $html .= "</div>";
        $html .= "</a>";

        $offering->offering_id = $offering_id;
        if ($leader_board_flag == "Y") {
            $stmt_criteria = $offering->check_course_criteria();
            if ($stmt_criteria->rowCount() > 0) {

                $html .= "<div class='d-flex justify-center mt-10 py-5' id='top3_btn'>";
                $html .= '<button type="butto" name="submit" class="button -icon -sm px-0 py-0 text-12 bg-color-primary text-white" onclick="showLeaders(\'' . $offering_id . '\',\'' . addslashes($course_name) . '\')">';
                $html .= "<i class='fa fa-trophy text-13'></i>&nbsp;&nbsp;";
                $html .= _("HM0113");
                $html .= "</button>";
                $html .= "</div>";
            } else {
                $html .= "<div class='d-flex justify-center mt-10 py-5' id='top3_btn'>";
                $html .= "<button class='button -icon -sm px-0 py-0 text-12 disabled-btn' disabled=''>";
                $html .= "<i class='fa fa-trophy text-13'></i>&nbsp;&nbsp;";
                $html .= _("HM0113");
                $html .= "</button>";
                $html .= "</div>";
            }
        } else {
            $html .= "<div class='d-flex justify-center mt-10 py-5' id='top3_btn'>";
            $html .= "<button class='button -icon -sm px-0 py-0 text-12 disabled-btn' disabled=''>";
            $html .= "<i class='fa fa-trophy text-13'></i>&nbsp;&nbsp;";
            $html .= _("HM0113");
            $html .= "</button>";
            $html .= "</div>";
        }
        $html .= "</div>";
        $html .= "</div>";
    }
} else {
    $stmt = $offering->completecourses($from_record_num, $records_per_page);
    $num = $stmt->rowCount();
    if ($num > 0) {
        $total_rows = $offering->completedcourses_count();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            extract($row);
            $time1 = date("d M Y", strtotime($start_date));
            $time2 = date("d M Y", strtotime($end_date));

            $html .= "<div class='w-1/4 xl:w-1/3 lg:w-1/2 sm:w-1/1'>";
            $html .= "<a class='coursesCard -type-1 px-8 py-10 border-light bg-white rounded-8' href='courseview.php?offering={$offering_id}'>";

            $filepath = ADMIN_HTTP_PATH . $properties['jdbc.offer_image'] . "/" . $media_path;

            $html .= "<div class='relative course_image_div'>";
            $html .= "<div class='coursesCard__image overflow-hidden rounded-8'>";

            if ($media_path == "" || $media_path == NULL) {
                $html .= "<img src='" . DEFAULT_COURSE_IMAGE . "' alt='" . htmlentities($course_name) . "' title='" . htmlentities($course_name) . "' class='w-1/1'>";
            } else {
                $html .= "<img src='" . $filepath . "' alt='" . htmlentities($course_name) . "' title='" . htmlentities($course_name) . "' class='w-1/1'>";
            }

            $html .= "<div class='coursesCard__image_overlay rounded-8  w-1/1'></div>";
            $html .= "</div>";
            $html .= "<div class='d-flex justify-between absolute-button z-3'>";
            $html .= "<div>";
            $html .= "<span class='size-50-25 d-flex items-center justify-center rounded-8 bg-white'>";

            if ($end_date >= $date) {
                $html .= _("CR0104");
            } else {
                $html .= _("CR0105");
            }

            $html .= "</span>";
            $html .= "</div>";
            $html .= "</div>";
            $html .= "</div>";

            $html .= "<div class='pt-15'>";
            $html .= "<div class='d-flex y-gap-10 justify-between items-center'>";
            $html .= "<div>" . $time1 . "</div>";
            $html .= "<div>" . $time2 . "</div>";
            $html .= "</div>";
            $html .= "<h3 class='text-16 fw-500 lh-15 mt-10 course_name'>" . htmlentities($course_name) . "</h3>";

            if ($score_flag != 'Yes') {
                if ($final_assmt_id != 0) {
                    $html .= "<div class='d-flex y-gap-10 justify-between items-center mt-3'>";
                    $html .= "<div class='text-dark-1'>" . _("CR0106") . "</div>";
                    $html .= "</div>  ";
                }
            } else {
                $html .= "<div class='progress mb-0'>";
                $html .= "<div class='progress-bar progress-bar-success' role='progressbar' aria-valuenow='40' aria-valuemin='0' aria-valuemax='100' style='width:" . $score . "'>";
                $html .= "</div>";
                $html .= "</div>";
                $html .= "<div class='d-flex y-gap-10 justify-between items-center mt-3'>";
                $html .= "<div class='text-dark-1'>" . $score . "% "
                        . _('CR0110')
                        . "</div>";
                $html .= "</div>";
            }
            $html .= "</div>";
            $html .= "</a>";
            $html .= "</div>";
        }
    } else {
        $html .= "<h3 class='py-40 text-center'>";
        $html .= _("CR0501");
        $html .= "</h3>";
    }
}
echo json_encode(array(
    'rows' => $total_rows,
    'html' => $html
));
?>