<?php
include_once "config/database.php";
include_once "resources/offering.php";
$results = array();
$tmp_score = 0;
$incomplete = 0;
$date_time = $date_time;
$offering = new Offering();

$offering->offering_id = $oid;
$offering->user_id = $id;

$stmt7 = $offering->get_assessment_score_card();
if ($stmt7->rowCount() > 0) {
    if ($row7 = $stmt7->fetch(PDO::FETCH_ASSOC)) {

        $f_id = $row7['final_assmt_id'];
        $wtg = $row7['weightage'];
        $offering->assessment_id=$f_id;
        $stmt8 = $offering->get_user_score();
        if ($stmt8->rowCount() > 0) {
            if ($row8 = $stmt8->fetch(PDO::FETCH_ASSOC)) {
                $last_attm = $row8['attempt_no'];
                $tmp_score = $wtg * $row8['score'];
                $stmt12 = $offering->getPassingScore();
                if ($row12 = $stmt12->fetch(PDO::FETCH_ASSOC)) {
                    $p_scr = $row12['passing_score'];
                }
                if ($row8['score'] >= $p_scr) {
                    array_push($results, "passed");
                } else {
                    array_push($results, "failed");
                    $stmt8a = $assessment->get_offer_detail();
                    if ($row8a = $stmt8a->fetch(PDO::FETCH_ASSOC)) {
                        extract($row8a);
                        if ($last_attm >= $max_attempts) {
                            $incomplete = 1;
                        }
                    }
                }
            }
        } else {
            array_push($results, "not attempted");
        }
    }
}
$stmt9 = $offering->getScormCriteria();
if ($stmt9->rowCount() > 0) {
    while ($row9 = $stmt9->fetch(PDO::FETCH_ASSOC)) {
        $wtg = 0;
        $s_id = $row9['scorm_id'];
        $wtg = $row9['weightage'];
        $offering->scorm_id = $s_id;
        $stmt10 = $offering->get_scorm_lesson_status_user();
        if ($stmt10->rowCount() > 0) {
            if ($row10 = $stmt10->fetch(PDO::FETCH_ASSOC)) {
                array_push($results, $row10['value']);
            }
        } else {
            array_push($results, "not attempted");
        }
        $stmt11 = $offering->get_scorm_score_user();
        if ($stmt11->rowCount() > 0) {
            if ($row11 = $stmt11->fetch(PDO::FETCH_ASSOC)) {
                $tmp_score = $tmp_score + ($wtg * $row11['value']);
            }
        }
    }
}
$stmt10 = $offering->getXApiCriteria();
if ($stmt10->rowCount() > 0) {
    while ($row10 = $stmt10->fetch(PDO::FETCH_ASSOC)) {
        $wtg = 0;
        $x_id = $row10['xapi_id'];
        $wtg = $row10['weightage'];
        $offering->xapi_id = $x_id;
        $stmt11 = $offering->get_xapi_lesson_status_user();
        if ($stmt11->rowCount() > 0) {
            if ($row11 = $stmt11->fetch(PDO::FETCH_ASSOC)) {
                if($row11['status'] == 'passed' || $row11['status'] == 'failed' || $row11['status'] == 'completed' || $row11['status'] == 'incomplete'){
                    array_push($results, $row11['status']);
                }else{
                    array_push($results, "not attempted");
                }
            }
        } else {
            array_push($results, "not attempted");
        }
        $stmt12 = $offering->get_xapi_score_user();
        if ($stmt12->rowCount() > 0) {
            if ($row12 = $stmt12->fetch(PDO::FETCH_ASSOC)) {
                $tmp_score = $tmp_score + ($wtg * $row12['actual_score']);
            }
        }
    }
}

$stmt11 = $offering->getLticriteria();
if ($stmt11->rowCount() > 0) {
    while ($row11 = $stmt11->fetch(PDO::FETCH_ASSOC)) {
        $wtg = 0;
        $l_id = $row11['lti_id'];
        $wtg = $row11['weightage'];
        $offering->lti_id = $l_id;
        $stmt_lti = $offering->get_lti_lesson_status_user();
        if ($stmt_lti->rowCount() > 0) {
            if ($row_lti = $stmt_lti->fetch(PDO::FETCH_ASSOC)) {
                if($row_lti['raw_grade'] >= $row_lti['grade_pass'] ){
                    array_push($results, "completed");
                }else{
                    array_push($results, "incomplete");
                }
                $tmp_score = $tmp_score + ($wtg * $row_lti['raw_grade']);
            }
        } else {
            array_push($results, "not attempted");
        }  
    }
}

$final_score = $tmp_score / 100;
//print_r($results);
if (in_array('not attempted', $results) || in_array('failed', $results) || in_array('incomplete', $results)) {
    $offering->attended_date = $date_time;
    $offering->result = NULL;
    if ($incomplete == 1) {
        $offering->result = "incomplete";
    }
    $offering->score = $final_score;
    $offering->last_updated_ts = $date_time;
    $val = $offering->update_result();
    if ($val) {
        //echo "fail";
    }
} else if (in_array('passed', $results) || in_array('completed', $results)) {
    $completion_points=NULL;
    $offering->score = $final_score;
    
    //added in 6/7/2022 for completion points
    if($offering->isCertificatepoints()){
        $stmt12 = $offering->completion_pt_certificate();
        if($stmt12 == NULL){
                $completion_points = $offering->completion_pt_assignment();
        }else{
                $completion_points=$stmt12;
        }
    }else{
        $stmt14 = $offering->completion_pt_assignment();
        $completion_points=$stmt14;
    }
    $offering->attended_date = $date_time;
    $offering->result = "complete";
    $offering->completion_points=$completion_points;
    $offering->last_updated_ts = $date_time;
    $val = $offering->update_result();
    if ($val) {
       $points = $offering->Sum_completionPoints();
       if($points !=NULL){
            $update_pt = $offering->update_userpoint($points);     
            if($update_pt){
                
            } 
       }
    }
}
?>