<?php

session_start();
include '../config/database.php';
include_once '../config/properties.php';
include_once "../resources/offering.php";
include_once "../resources/EncryptDecrypt.php";
if (isset($_SESSION['uid'])) {
    $id = $_SESSION['uid'];

    $from_date = $_GET['from_date'];
    $to_date = $_GET['to_date'];
    // initialize objects
    $offering = new Offering();
    $offering->user_id = $id;
    $total_points = 0;
    $html_course = "";
    $html_points = "";
    $off_val_date = "";
    $completion_points = 0;
    $grade = "---";
    $attended_date = "---";
    $total_time = 0;

    if ($from_date !== "") {
        $off_val_date = " AND DATE(ca.course_assigned_ts)>='$from_date' AND DATE(ca.course_assigned_ts)<='$to_date' ";
    } else {
        $off_val_date = "";
    }

    $stmt = $offering->offering_Course_Report($off_val_date);
    $num = $stmt->rowCount();
    if ($num > 0) {
        $count = 0;
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $count = $count + 1;
            $grade = "";
            $attended_date = "";
            $total_time = 0;
            $completion_points = 0;
            if ($row['completion_points'] != null) {
                $completion_points = round($row['completion_points'],1);
            }
            if ($row['attended_date'] != null) {
                $attended_date = $row['attended_date'];
                $attended_date = date("d-M-Y h:i:s", strtotime($attended_date));
            }
            if ($row['total_time'] != null) {
                $total_time = $row['total_time'];
            }
            $html_course .= "<tr class='bg-white shadow-1 rounded-8 border-light'>
                                                        <td class='text-right'>" . $count . "</td>
                                                        <td>" . $row['category_name'] . "</td>
                                                        <td>" . $row['course_name'] . "</td>
                                                        <td class='text-right'>" . ($total_time == 0 ? '' : $total_time) . "</td>
                                                        <td class='text-right'>" . ($row['content_completion'] == 0 ? '' : $row['content_completion']) . "</td>
                                                        <td class='text-right'>" . ($row['passing_score'] == 0 ? '' : $row['passing_score']) . "</td>
                                                        <td class='text-right'>" . ($row['score'] == 0 ? '' : $row['score'] ) . "</td>
                                                        <td>" . $attended_date . "</td>
                                                        <td>" . $row['result'] . "</td>
                                                        <td class='text-right'>" . ($completion_points == 0 ? '' : $completion_points) . "</td>
                                                        <td>";
            $total_points += round($row['completion_points'],1);
            $offering->offering_id = $row['offering_id'];
            $stmt1 = $offering->offering_Course_Reportbadge($off_val_date);
            if ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) {
                if ($row1['grade'] != "") {
                    $grade = $row1['grade'];
                } else {
                    $grade = "";
                }
            }
            $html_course .= $grade;
            $html_course .= "</td>";
            $stmt2 = $offering->offering_Course_Reporttemplate($off_val_date);
            if ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)) {
                if ($row2['template_count'] != 0) {
                    $html_course .= "<td><a href='' >Yes</a></td>";
                } else {
                    $html_course .= "<td></td>";
                }
            }
            $html_course .= "</tr>";
        }
        $total_duration = $offering->get_Course_totalTime($off_val_date);
        $html_course .= "<tr class='bg-white shadow-1 rounded-8 border-light'>";
        $html_course .= "<td colspan='3' class='text-right'><b> " . _('RP0119') . " </b></td>";
        $html_course .= "<td class='text-right'> <b>$total_duration</b></td>";
        $html_course .= "<td colspan='6' class='text-right'><b>" . _('RP0120') . "</b></td>";
        $html_course .= "<td class='text-right'><b>$total_points</b></td>";
        $html_course .= "<td colspan='2'></td>";
        $html_course .= "</tr>";
    } else {
        $html_course .= "<tr class='bg-white shadow-1 rounded-8 border-light' id='no_result'><td colspan='12'><center>" . _("ER0101") . "</center></td></tr>";
    }



    $discussion_forum = 0;
    $offer_completion = 0;
    $offer_ratings = 0;
    $offer_score1 = 0;
    $score1 = 0;
    $offer_score2 = 0;
    $score2 = 0;
    $offer_score3 = 0;
    $score3 = 0;
    $offer_score4 = 0;
    $score4 = 0;
    $offer_view = 0;

    $stmt_points = $offering->get_ReportActivityPoints();
    while ($row_pt = $stmt_points->fetch(PDO::FETCH_ASSOC)) {
        
        if ($row_pt['name'] == "discussion_forum") {
            $discussion_forum = $row_pt['points'];
        }
        if ($row_pt['name'] == "offer_completion") {
            $offer_completion = $row_pt['points'];
        }
        if ($row_pt['name'] == "offer_ratings") {
            $offer_ratings = $row_pt['points'];
        }
        if ($row_pt['name'] == "offer_score1") {
            $offer_score1 = $row_pt['points'];
            $score1 = $row_pt['score'];
        }
        if ($row_pt['name'] == "offer_score2") {
            $offer_score2 = $row_pt['points'];
            $score2 = $row_pt['score'];
        }
        if ($row_pt['name'] == "offer_score3") {
            $offer_score3 = $row_pt['points'];
            $score3 = $row_pt['score'];
        }
        if ($row_pt['name'] == "offer_score4") {
            $offer_score4 = $row_pt['points'];
            $score4 = $row_pt['score'];
        }
        if ($row_pt['name'] == "offer_view") {
            $offer_view = $row_pt['points'];
        }
    }
    $total_points_pt = 0.0;
    //forum
    if ($from_date !== "") {
        $forum_val_date = "AND DATE(ft.last_updated_ts)>='$from_date' AND DATE(ft.last_updated_ts)<='$to_date'";
    } else {
        $forum_val_date = "";
    }
    $forum_count = $offering->get_totaltopic($forum_val_date, $discussion_forum);
    if ($from_date !== "") {
        $forum_val_date = "AND DATE(ftr.last_updated_ts)>='$from_date' AND DATE(ftr.last_updated_ts)<='$to_date'";
    } else {
        $forum_val_date = "";
    }
    $response_count = $offering->get_totalresponse($forum_val_date, $discussion_forum);
    $total_res = $forum_count + $response_count;
    $total_points_pt += $total_res;
    $html_points .= "<tr class='bg-white shadow-1 rounded-8 border-light'>";
    $html_points .= "<td class='text-right'>1</td>
                                            <td>" . _('LB0111') . "</td>
                                            <td></td>
                                            <td class='text-right'>$total_res</td>
                                        </tr>";
//                                       //offer completion
    $offer_completionDate = "";
    if ($from_date !== "") {
        $offer_completionDate = "AND DATE(ca.course_assigned_ts)>='$from_date' AND DATE(ca.course_assigned_ts)<='$to_date'";
    } else {
        $offer_completionDate = "";
    }
    $stmt_completion = $offering->get_offer_CompletionCount($offer_completionDate, $offer_completion, $score1, $score2, $score3, $score4, $offer_score1, $offer_score2, $offer_score3, $offer_score4);
    while ($row_cmp = $stmt_completion->fetch(PDO::FETCH_ASSOC)) {
        $offer_completion_points_value = $row_cmp['offer_completion_points'];
        $offer_score1_points_value = $row_cmp['offer_score1_points'];
        $offer_score2_points_value = $row_cmp['offer_score2_points'];
        $offer_score3_points_value = $row_cmp['offer_score3_points'];
        $offer_score4_points_value = $row_cmp['offer_score4_points'];
    }
    $offer_completion_points = "";
    $stmt_completion_points = $offering->get_offer_CompletionPointsCount($offer_completionDate, $offer_completion);
    while ($row_cmppt = $stmt_completion_points->fetch(PDO::FETCH_ASSOC)) {
        $offer_completion_points .= $row_cmppt['course_name'] . "(" . $row_cmppt['offer_completion_points'] . ")<br/>";
    }
    $total_points_pt += $offer_completion_points_value;
    $html_points .= "<tr class='bg-white shadow-1 rounded-8 border-light'>";
    $html_points .= "<td class='text-right'>2</td>
                                            <td>" . _('RP0124') . "</td>
                                            <td>$offer_completion_points</td>
                                            <td class='text-right'>$offer_completion_points_value</td>
                                        </tr>";
    //above90
    $offer_score1_points = "";
    $score_value = " ca.score >= " . $score1 . " ";
    $stmt_completion_score1 = $offering->get_offer_score($offer_completionDate, $offer_score1, $score_value);
    while ($row_cmp_score1 = $stmt_completion_score1->fetch(PDO::FETCH_ASSOC)) {
        $offer_score1_points .= $row_cmp_score1['course_name'] . "(" . $row_cmp_score1['offer_score1_points'] . ")<br/>";
    }
    $total_points_pt += $offer_score1_points_value;
    $html_points .= "<tr class='bg-white shadow-1 rounded-8 border-light'>";
    $html_points .= "<td class='text-right'>3</td>
           <td>" . _('RP0125') . " $score1 and above</td>
                                            <td>$offer_score1_points</td>
                                            <td class='text-right'>$offer_score1_points_value</td>
                                        </tr>";
    //above80
    $offer_score2_points = "";
    $score_value = " ca.score >= " . $score2 . " AND ca.score<" . $score1 . " ";
    $stmt_completion_score2 = $offering->get_offer_score($offer_completionDate, $offer_score2, $score_value);
    while ($row_cmp_score2 = $stmt_completion_score2->fetch(PDO::FETCH_ASSOC)) {
        $offer_score2_points .= $row_cmp_score2['course_name'] . "(" . $row_cmp_score2['offer_score1_points'] . ")<br/>";
    }
    $total_points_pt += $offer_score2_points_value;
    $html_points .= "<tr class='bg-white shadow-1 rounded-8 border-light'>";
    $html_points .= "<td class='text-right'>4</td>
            <td>" . _('RP0125') . " " . $score2 . " - " . --$score1 . " </td>
                                            <td>$offer_score2_points</td>
                                            <td class='text-right'>$offer_score2_points_value</td>
                                        </tr>";
    //above70
    $offer_score3_points = "";
    $score_value = " ca.score >= " . $score3 . " AND ca.score<" . $score2 . " ";
    $stmt_completion_score3 = $offering->get_offer_score($offer_completionDate, $offer_score3, $score_value);
    while ($row_cmp_score3 = $stmt_completion_score3->fetch(PDO::FETCH_ASSOC)) {
        $offer_score3_points .= $row_cmp_score3['course_name'] . "(" . $row_cmp_score3['offer_score1_points'] . ")<br/>";
    }
    $total_points_pt += $offer_score3_points_value;
    $html_points .= "<tr class='bg-white shadow-1 rounded-8 border-light'>";
    $html_points .= "<td class='text-right'>5</td>
            <td>" . _('RP0125') . " " . $score3 . " - " . --$score2 . " </td>
                                            <td>$offer_score3_points</td>
                                            <td class='text-right'>$offer_score3_points_value</td>
                                        </tr>";
    //above70
    $offer_score4_points = "";
    $score_value = " ca.score >= " . $score4 . " AND ca.score<" . $score3 . " ";
    $stmt_completion_score4 = $offering->get_offer_score($offer_completionDate, $offer_score4, $score_value);
    while ($row_cmp_score4 = $stmt_completion_score4->fetch(PDO::FETCH_ASSOC)) {
        $offer_score4_points .= $row_cmp_score4['course_name'] . "(" . $row_cmp_score4['offer_score1_points'] . ")<br/>";
    }
    $total_points_pt += $offer_score4_points_value;
    $html_points .= "<tr class='bg-white shadow-1 rounded-8 border-light'>";
    $html_points .= "<td class='text-right'>6</td>
            <td>" . _('RP0125') . " " . $score4 . " - " . --$score3 . " </td>
                                            <td>$offer_score4_points</td>
                                            <td class='text-right'>$offer_score4_points_value</td>
                                        </tr>";
    //offer view
//                                        $total_viewTime = $offering->get_offer_viewpt($offer_completionDate,$offer_view);
    $offer_totalTime = 0.0;
    $offer_view_points = "";
    $stmt_view = $offering->get_offer_view($offer_completionDate, $offer_view);
    while ($row_view = $stmt_view->fetch(PDO::FETCH_ASSOC)) {
        $offer_view_points .= $row_view['course_name'] . "(" . $row_view['total_time'] . ")<br/>";
        $offer_totalTime += $row_view['total_time'];
    }
    $total_points_pt += $offer_totalTime;
    $html_points .= "<tr class='bg-white shadow-1 rounded-8 border-light'>";
    $html_points .= "<td class='text-right'>7</td>
            <td>" . _('RP0126') . "</td>
                                            <td>$offer_view_points</td>
                                            <td class='text-right'>$offer_totalTime</td>
                                        </tr>";
    //offer rating
    $offer_totalrating = 0.0;
    $offer_rating_points = "";
    $stmt_rat = $offering->get_offer_rating($offer_completionDate, $offer_ratings);
    while ($row_rat = $stmt_rat->fetch(PDO::FETCH_ASSOC)) {
        $offer_rating_points .= $row_rat['course_name'] . "(" . $row_rat['rating_value'] . ")<br/>";
        $offer_totalrating += $row_rat['rating_value'];
    }
    $total_points_pt += $offer_totalrating;
    $html_points .= "<tr class='bg-white shadow-1 rounded-8 border-light'>";
    $html_points .= "<td class='text-right'>8</td>
              <td>" . _('RP0127') . "</td>
                                            <td>$offer_rating_points</td>
                                            <td class='text-right'>$offer_totalrating</td>
                                        </tr>";
    $html_points .= "<tr class='bg-white shadow-1 rounded-8 border-light'><td colspan='3' class='text-right'><b>" . _('RP0123') . "</b></td> <td id='total_points' class='text-right'><b> " . round($total_points_pt, 0) . " </b></td></tr>";
//                                        
//                                     
    echo json_encode(array(
        'courses' => $html_course,
        'points' => $html_points
    ));
}
 