<?php 
//  essential functions
require "querys.php";
//read database login information and connect
require "../config/database.php";
include_once "../resources/offering.php";
include_once '../config/properties.php';
dbConnect();
//format should be unique we can combine with username and userid_scormid/contentdetail_id_offeringid
$xapi_id = $_REQUEST['xapi_id'];
$cid = $_REQUEST['contentdtl_id'];
$path = $_REQUEST['path'];
$oid = $_REQUEST['oid'];
$uid = $_REQUEST['uid'];
$coid = $_REQUEST['coid'];
$end_date = $_REQUEST['end_date'];
$rslt = $_REQUEST['rslt'];
$homepage = CLIENT_HTTP_PATH;
$result = mysqli_query($link, "select * from configuration as c,course as co where c.company_id=co.company_id and co.course_id='".$coid."'");
if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_array($result);
    $endpoint= $row['end_point'];
    $key = $row['lrs_key'];
    $secret = $row['lrs_secret'];
}
$result1 = mysqli_query($link, "select launch_page,activity_id from xapi_content_master where xapi_id='" . $xapi_id . "'");
if (mysqli_num_rows($result1) > 0) {
    $row1 = mysqli_fetch_array($result1);
    $s_path = ADMIN_HTTP_PATH . $properties['jdbc.content_folder'] . '/';
    $s_path .= $path;
    $s_path .= $row1['launch_page'];
    $activity_id = $row1['activity_id'];
}
$result2 = mysqli_query($link, "select registration_id from xapi_track where xapi_id='" . $xapi_id . "' and user_id='".$uid."' and offering_id='".$oid."' and contentdtl_id='".$cid."'");
if (mysqli_num_rows($result2) > 0) {
    $row2 = mysqli_fetch_array($result2);
    $reg_id = $row2['registration_id'];
}else{
    $reg_id=getUUID();
}
$result3 = mysqli_query($link, "select * from user_account where user_id='".$uid."'");
if (mysqli_num_rows($result3) > 0) {
    $row3 = mysqli_fetch_array($result3);
    $username = $row3['first_name']." ".$row3['last_name']; 
}
$auth_value ="Basic ".base64_encode($key.":".$secret);
?>
  <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width,initial-scale=1.0">
        </head>
    <body > 

    <iframe src='<?php echo $s_path; ?>?endpoint=<?php echo $endpoint; ?>&auth=<?php echo $auth_value;?>&actor=%7B"objectType":"Agent","name":"<?php echo urlencode($username); ?>","account":%7B"name":"<?php echo urlencode($username);?>","homePage":"<?php echo $homepage; ?>"%7D%7D&registration=<?php echo $reg_id;?>&activity_id=<?php echo $activity_id; ?>' id="iframe" width="100%" style='height:100vh;' scrolling="no" noresize="noresize"></iframe>
<!--working link-->
<!--<iframe src='../../admin_multi/files_content/30/scormpackage/GolfExmple/index.html?endpoint=https://4edgeit.com:8443/traxlrs/public/trax/ws/xapi/&auth=Basic%20NDIzYmEzNjgtYTU0Yi00OWEzLTgwOWItZjRkZWEwNzA1N2Y2OmJhZTgzYzNkLTU1NzAtNDFmNi04YWE0LWU0OWVjN2UyY2FiYw==&actor=%7B"objectType":"Agent","name":"Test9 ","account":%7B"name":"Test9 ","homePage":"http://localhost/client_multi"%7D%7D&registration=399b6e12-e54c-44f6-96b2-73efecaf2251&activity_id=http://id.tincanapi.com/activity/tincan-prototypes/golf-example' id="iframe" width="100%" style='height:100vh;'></iframe>-->

<script type="text/javascript" src="xapiwrapper.min.js"></script>
<script type="text/javascript" src="../assets/js/jquery-3.6.3.min.js"></script>
<script>
    var status='';
    //construction of Trax lrs
	const conf = {
		"endpoint": "<?php echo $endpoint; ?>",
		"auth": "<?php echo $auth_value; ?>"
	};
	ADL.XAPIWrapper.changeConfig(conf);     
        var ret = ADL.XAPIWrapper.getStatements();
        try {
            if (typeof ret.statements !== "undefined") {
                           } else { 
                alert("Failed to connect LRS");
		 $('#iframe').attr('src', "");
            }
        } catch (ex) {
            alert("Failed to connect LRS");
		 $('#iframe').attr('src', "");
        } 
                    window.onbeforeunload = function () {
                        if(status===''){
                        if ($('#iframe').attr('src') !== "") {
                            insertxapi();
                        }
                    }
                    };
                  function insertxapi() {              
                    $.ajax({
                        type: 'GET',
                        async: true,
                        url: 'xapi_track.php?uid=<?php echo $uid?>&oid=<?php echo $oid?>&cid=<?php echo $cid?>&rid=<?php echo $reg_id?>&xapi_id=<?php echo $xapi_id?>&end_date=<?php print $end_date; ?>&rslt=<?php print $rslt; ?>'
                    });
                }         
                
                function insertxapi_backToCourse(status_content) {
                    status=status_content;
            if ($('#iframe').attr('src') !== "") {
                $.ajax({
                    type: 'GET',
                    async: true,
                    url: 'xapi_track.php?uid=<?php echo $uid; ?>&oid=<?php echo $oid; ?>&cid=<?php echo $cid ?>&rid=<?php echo $reg_id ?>&xapi_id=<?php echo $xapi_id ?>&end_date=<?php echo $end_date; ?>&rslt=<?php echo $rslt; ?>',
                    success: function (data) {
                        window.parent.backToCourseView();
                    },
                    error: function (xhr, type, exception) {
                        window.parent.backToCourseView();
                        //window.location = "../courseview.php?offering=<?php echo $oid; ?>#content";
                    }
                });
            } else {
                window.parent.backToCourseView();
                //window.location = "../courseview.php?offering=<?php echo $oid; ?>#content";
            }
        }
</script>
</body>
</html>