<?php
session_start();
include_once '../config/database.php';
include_once '../security/session_config.php';
include_once "../resources/offering.php";
include_once '../config/properties.php';
// echo "../". ADMIN_PATH . $properties['jdbc.offer_image'];
$offering = new Offering();
$offering->user_id = $_GET['user_id'];
$offering->date = $date;

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

$html = "";
$total_rows = 0;

$records_per_page = 10; // set records or rows of data per page
$from_record_num = ($records_per_page * $page) - $records_per_page; // calculate for the query LIMIT clause
//Active mode    
if ($mode == "active") {
    $stmt = $offering->activecourses($from_record_num, $records_per_page); // read all products in the database                            
    $num = $stmt->rowCount();
    // needed for paging
    if ($num > 0) {
        $total_rows = $offering->activecourses_count();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            extract($row);

            if ($view == "grid") {
                $html .= "<div class='w-1/5 xl:w-1/3 lg:w-1/2 sm:w-1/1'>";
            } else {
                $html .= "<div class='col-md-6'>";
            }
            $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;
                }
            }

            //Learning Path
            $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) {
                                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;
                            }
                        }
                    }
                }
            }


            if ($view == "grid") {
                $html .= "<a class='coursesCard -type-1' ";
            } else {
                $html .= "<a class='relative d-block rounded-8 px-10 py-10 border-light coursesCard -type-1' ";
            }
            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) {
                                        $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}' name='dfs'>";
                }
            } 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;

            if ($view == "list") {
                $html .= " <div class='row x-gap-20 y-gap-20 items-center'>";
                $html .= "<div class='col-md-6 m-0'>";
            }
            $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>";

            if ($view == "list") {
                $html .= "</div>";
            }
            if ($view == "grid") {
                $html .= "<div class='pt-15'>";
            } else {
                $html .= "<div class='col-md-6 m-0'>";
            }
            $html .= "<div class='d-flex y-gap-10 justify-between items-center'>";
            $html .= "<div>" . $time1 . "</div>";
            $html .= "<div>" . $time2 . "</div>";
            $html .= "</div>";
            $cname_cls = $view == "grid" ? "text-16 fw-500 lh-15 mt-10 course_name" : "text-17 lh-16 fw-500 mt-10 pr-40 xl:pr-0 course_name";
            $html .= "<h3 class='" . $cname_cls . "'>" . htmlentities($course_name) . "</h3>";
            if ($content_count == 0) {
                $html .= "<div class='d-flex y-gap-10 justify-between items-center'>";
                $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-valuenow='40' 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'>";
                $html .= "<div class='text-dark-1'>" . $content_completion . "% "
                        . _('CRT2108')
                        . "</div>";
                $html .= "</div>";
            }
            $html .= "</div>";
            if ($view == "list") {
                $html .= "</div>";
            }
            $html .= "</a>";
            $html .= "</div>";
        }
    }  //num rowcount end
    else {
        $html .= "<h3 class='py-40 text-center'>";
        $html .= _("CR0501");
        $html .= "</h3>";
    }
}
//Active mode end
//complete mode
else {
    if ($mode == "complete") {
        // read all products in the database
        $stmt = $offering->completecourses($from_record_num, $records_per_page);
        // count number of retrieved products
        $num = $stmt->rowCount();
        $total_rows = $offering->completedcourses_count();
    } else if ($mode == "incomplete") {
        // read all products in the database
        $stmt = $offering->incompletecourses($from_record_num, $records_per_page);
        // count number of retrieved products
        $num = $stmt->rowCount();
        $total_rows = $offering->incompletecourses_count();
    }
    if ($num > 0) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            extract($row);

            if ($view == "grid") {
                $html .= "<div class='w-1/5 xl:w-1/3 lg:w-1/2 sm:w-1/1'>";
            } else {
                $html .= "<div class='col-md-6'>";
            }
            $time1 = date("d M Y", strtotime($start_date));
            $time2 = date("d M Y", strtotime($end_date));

            if ($view == "grid") {
                $html .= " <a href='courseview.php?offering={$offering_id}' class='coursesCard -type-1'>";
            } else {
                $html .= " <a href='courseview.php?offering={$offering_id}' class='relative d-block rounded-8 px-10 py-10 border-light coursesCard -type-1'>";
            }
            $filepath = ADMIN_HTTP_PATH . $properties['jdbc.offer_image'] . "/" . $media_path;

            if ($view == "list") {
                $html .= "<div class='row x-gap-20 y-gap-20 items-center'>";
                $html .= "<div class='col-md-6 m-0'>";
            }
            $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>";

            if ($view == "list") {
                $html .= "</div>";
            }
            if ($view == "grid") {
                $html .= "<div class='pt-15'>";
            } else {
                $html .= "<div class='col-md-6 m-0'>";
            }
            $html .= "<div class='d-flex y-gap-10 justify-between items-center'>";
            $html .= "<div>" . $time1 . "</div>";
            $html .= "<div>" . $time2 . "</div>";
            $html .= "</div>";
            $cname_cls = $view == "grid" ? "text-16 fw-500 lh-15 mt-10 course_name" : "text-17 lh-16 fw-500 mt-10 pr-40 xl:pr-0 course_name";
            $html .= "<h3 class='" . $cname_cls . "'>" . 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'>";
                    $html .= "<div class='text-dark-1'>" . _("CR0106") . "</div>";
                    $html .= "</div>";
                }
            } else {
                $progress_color = $mode == "complete" ? "progress-bar-success" : "progress-bar-danger";

                $html .= "<div class='progress mb-0'>";
                $html .= "<div class='progress-bar " . $progress_color . "' 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>";
            if ($view == "list") {
                $html .= "</div>";
            }
            $html .= "</a>";
            $html .= "</div>";
        }
    }  //num rowcount end
    else {
        $html .= "<h3 class='py-40 text-center'>";
        $html .= _("CR0501");
        $html .= "</h3>";
    }
}
//complete mode end
echo json_encode(array(
    'rows' => $total_rows,
    'html' => $html
));
?>
              
