<?php
    $servername = "localhost";
    $username = "devknestlms_dbuser";
    $password = "Narahari1!";
    $dbname = "devknestlms_multi_v3";

    date_default_timezone_set('Asia/Kolkata');
    $datetime = date("Y-m-d H:i:s");
    $date = date("Y-m-d");

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    // Turn autocommit off
    $conn->autocommit(FALSE);
    
    $batch_user_id = 0;
   
try{
    $file_name = "xapi_log.txt";
    //get W status values
    $result = mysqli_query($conn, "select * from xapi_track where xapi_batch='W'");
    if (mysqli_num_rows($result)) {
        $fp = fopen($file_name, "a");
        fwrite($fp, "Date:" . $datetime . "\n");
        $count = 0;
        while ($row = mysqli_fetch_array($result)) {
            $count = $count + 1;  
            $xapi_id = $row['xapi_id'];
            $oid = $row['offering_id'];
            $rid = $row['registration_id'];
            $cid = $row['contentdtl_id'];
            $uid = $row['user_id'];
                      
            $passed_value = "";
            $failed_value = "";
            $value_recent = "";
            $value = "";
            //get verb from db 
            $result1 = mysqli_query($conn, "SELECT id,json_extract(data, '$.verb.display') as data FROM `trax_xapiserver_statements` WHERE json_extract(data, '$.context.registration')='$rid' order By id desc;");
            if (mysqli_num_rows($result1)) {
                while ($row1 = mysqli_fetch_array($result1)) {
                    $st_id = $row1['id'];
                    $array =json_decode($row1['data'],true);
                    //convert to either lower or upper case
                    $verb = strtolower($array["en-US"]);
                    $val = $verb; //storing values for final result
                    //passed value
                    if($verb === "passed" || $verb === "completed"){
                        $val .= "_true";  //true represents completion status
                        $result2 = mysqli_query($conn, "SELECT id,json_extract(data, '$.result.score') as data FROM `trax_xapiserver_statements` WHERE id=$st_id");
                        if (mysqli_num_rows($result2)) {
                            if($row2 = mysqli_fetch_array($result2)){
                                //checking for score
                                $array1 =json_decode($row2['data'],true);
                                if (is_array($array1) && array_key_exists("raw",$array1)){
                                    $val .= "_".$array1["raw"]; 
                                }else if (is_array($array1) && array_key_exists("scaled",$array1)){
                                    $val .= "_".($array1["scaled"]*100);
                                }else{
                                    $val .= "_0";
                                }
                                if (is_array($array1) && array_key_exists("min",$array1)){
                                    $val .= "_".$array1["min"]; 
                                }else{
                                    $val .= "_0";
                                }
                                if (is_array($array1) && array_key_exists("max",$array1)){
                                    $val .= "_".$array1["max"]; 
                                }else{
                                    $val .= "_0";
                                }
                            }
                        }else{  //if no result array with score adding 0 for score
                            $val .= "_0_0_0";
                        }
                        if($passed_value === ""){
                            $passed_value = $val;
                        }
                        if($value_recent === ""){
                            $value_recent = $val;
                        }
                    }else if($verb === "failed" || $verb === "incomplete"){ //failed value
                        $val .= "_false";   //false represents completion status
                        $result3 = mysqli_query($conn, "SELECT id,json_extract(data, '$.result.score') as data FROM `trax_xapiserver_statements` WHERE id=$st_id");
                        if (mysqli_num_rows($result3)) {
                            if($row3 = mysqli_fetch_array($result3)){
                                //checking for score
                                $array1 =json_decode($row3['data'],true);
                                if (is_array($array1) && array_key_exists("raw",$array1)){
                                    $val .= "_".$array1["raw"]; 
                                }else if (is_array($array1) && array_key_exists("scaled",$array1)){
                                    $val .= "_".($array1["scaled"]*100);
                                }else{
                                    $val .= "_0";
                                }
                                if (is_array($array1) && array_key_exists("min",$array1)){
                                    $val .= "_".$array1["min"]; 
                                }else{
                                    $val .= "_0";
                                }
                                if (is_array($array1) && array_key_exists("max",$array1)){
                                    $val .= "_".$array1["max"]; 
                                }else{
                                    $val .= "_0";
                                }
                            }
                        }else{  //if no result array with score adding 0 for score
                            $val .= "_0_0_0";
                        }
                        if($failed_value === ""){
                            $failed_value = $val;
                        }
                        if($value_recent === ""){
                            $value_recent = $val;
                        }
                    }else{
                        if($value_recent === ""){
                            $value_recent = $val;
                        }
                    }
                }
            }
//    echo "pass :".$passed_value."<br/>";
//    echo "fail :".$failed_value."<br/>";
//    echo "recent :".$value_recent."<br/>";
            if($passed_value !== "" ){
                $value = $passed_value;  
            }else if($passed_value === "" && $failed_value !== ""){
                $value = $failed_value;
            }
            if($value === ""){
                $value=$value_recent;  
            }
            $myArr = explode("_",$value);
            $myArr_len= count($myArr);
            if($myArr_len > 1){
                //update
                if($myArr[1] == "true"){
                    $res = mysqli_query($conn, "update xapi_track set result='Y',status='$myArr[0]',actual_score=$myArr[2],score_min=$myArr[3],score_max=$myArr[4],last_updated_user=$batch_user_id,last_updated_ts='$datetime' where offering_id=$oid and user_id=$uid and xapi_id=$xapi_id and contentdtl_id=$cid");
                }else{
                    $res = mysqli_query($conn, "update xapi_track set result=null,status='$myArr[0]',actual_score=$myArr[2],score_min=$myArr[3],score_max=$myArr[4],last_updated_user=$batch_user_id,last_updated_ts='$datetime' where offering_id=$oid and user_id=$uid and xapi_id=$xapi_id and contentdtl_id=$cid");
                }
            }else{
                //insert
                $res = mysqli_query($conn, "update xapi_track set status='$myArr[0]',last_updated_user=$batch_user_id,last_updated_ts='$datetime' where offering_id=$oid and user_id=$uid and xapi_id=$xapi_id and contentdtl_id=$cid");
            }
            //After executes update query in xapi_track
            if($res){
                $error = false;
                $results = array();
                $tmp_score = 0;
                $incomplete = 0;
                
                //Code similar to cal_result.php      
                //First - Assessment
                    $result1 = mysqli_query($conn, "SELECT rc.*,a.assessment_name,a.assessment_id FROM result_criteria as rc,assessment as a where rc.offering_id=$oid and a.assessment_id=rc.final_assmt_id and rc.final_assmt_id!=0");
                    if (mysqli_num_rows($result1) > 0) {
                        if ($row1 = mysqli_fetch_array($result1)) {
                            $f_id = $row1['final_assmt_id'];
                            $wtg = $row1['weightage'];
                            $result2 = mysqli_query($conn, "SELECT attempt_no,score,passing_score as pass_score FROM quiz_score where offering_id=$oid and user_id=$uid and assessment_id=$f_id ORDER BY attempt_no desc limit 1");
                            if (mysqli_num_rows($result2) > 0) {
                                if ($row2 = mysqli_fetch_array($result2)) {
                                    $last_attm = $row2['attempt_no'];
                                    $tmp_score = $wtg * $row2['score'];
                                    $result3 = mysqli_query($conn, "select passing_score from course_offering WHERE offering_id=$oid");
                                        if ($row3 = mysqli_fetch_array($result3)) {
                                            $p_scr = $row3['passing_score'];
                                        }
                                    if ($row2['score'] >= $p_scr) {
                                        array_push($results, "passed");
                                    } else {
                                        array_push($results, "failed");
                                        $result4 = mysqli_query($conn, "select co.is_scorm,co.passing_score,co.max_attempts,co.score_flag,co.course_id from course_offering as co WHERE co.offering_id=$oid");
                                            if ($row4 = mysqli_fetch_array($result4)) {
                                                if ($last_attm >= $row4['max_attempts']) {
                                                    $incomplete = 1;
                                                }
                                           }
                                    }
                                }
                            }else{
                                array_push($results, "not attempted");
                            }
                        }
                    }
                  
                    //Second - Scorm
                    $result5 = mysqli_query($conn, "SELECT scorm_id,weightage FROM result_criteria 
					WHERE offering_id=" . $oid . " and scorm_id!=0");
                    if (mysqli_num_rows($result5) > 0) {
                        while ($row5 = mysqli_fetch_array($result5)) {
                            $result6 = mysqli_query($conn, "SELECT value FROM scorm_track WHERE scorm_id=" . $row5['scorm_id'] . " AND 
								offering_id=" . $oid . " AND user_id=" . $uid . " AND model='cmi.core.lesson_status'");
                            if (mysqli_num_rows($result6) > 0) {
                                if ($row6 = mysqli_fetch_array($result6)) {
                                    array_push($results, $row6['value']);
                                }
                            } else {
                                array_push($results, "not attempted");
                            }

                            $result7 = mysqli_query($conn, "SELECT value FROM scorm_track WHERE scorm_id=" . $row5['scorm_id'] . " AND 
								offering_id=" . $oid . " AND user_id=" . $uid . " AND model='cmi.core.score.raw'");
                            if (mysqli_num_rows($result7) > 0) {
                                if ($row7 = mysqli_fetch_array($result7)) {
                                    $tmp_score = $tmp_score + ($row5['weightage'] * $row7['value']);
                                }
                            }
                        }
                    }
                     //third - xapi
                    $result8 = mysqli_query($conn, "SELECT xapi_id,weightage FROM result_criteria 
					WHERE offering_id=" . $oid . " and xapi_id!=0");
                    if (mysqli_num_rows($result8) > 0) {
                        while ($row8 = mysqli_fetch_array($result8)) {
                            $result9 = mysqli_query($conn, "SELECT status FROM xapi_track WHERE xapi_id=" . $row8['xapi_id'] . " AND offering_id=" . $oid . " AND user_id=" . $uid . "");
                            if (mysqli_num_rows($result9) > 0) {
                                if ($row9 = mysqli_fetch_array($result9)) {
                                   if($row9['status'] == 'passed' || $row9['status'] == 'failed' || $row9['status'] == 'completed' || $row9['status'] == 'incomplete'){
                                        array_push($results, $row9['status']);
                                    }else{
                                        array_push($results, "not attempted");
                                    }                                }
                            } else {
                                array_push($results, "not attempted");
                            }
                            $result10 = mysqli_query($conn, "SELECT actual_score FROM xapi_track WHERE xapi_id=" . $row8['xapi_id'] . " AND 
								offering_id=" . $oid . " AND user_id=" . $uid . "");
                            if (mysqli_num_rows($result10) > 0) {
                                if ($row10 = mysqli_fetch_array($result10)) {
                                    $tmp_score = $tmp_score + ($row8['weightage'] * $row10['actual_score']);
                                }
                            }
                        }
                    }
                      //LTI
                    $result_lti = mysqli_query($conn, "SELECT lti_id,weightage FROM result_criteria 
					WHERE offering_id=" . $oid . " and lti_id!=0");
                    if (mysqli_num_rows($result_lti) > 0) {
                        while ($row_lti = mysqli_fetch_array($result_lti)) {
	                            $result6 = mysqli_query($conn, "SELECT lug.raw_grade,g.grade_pass FROM lti_user_grades as lug,grade_items as g where lug.grade_items_id=g.grade_items_id AND 
								lug.offering_id=" . $oid . " AND lug.user_id=" . $uid . " AND g.lti_id=".$row_lti['lti_id']."");
                            if (mysqli_num_rows($result6) > 0) {
                                if ($row6 = mysqli_fetch_array($result6)) {
                                   if($row6['raw_grade'] >= $row6['grade_pass'] ){
                                        array_push($results,  "completed");
                                    }else{
                                        array_push($results, "incomplete");
                                    }    
                                    $tmp_score = $tmp_score + ($row_lti['weightage'] * $row6['raw_grade']);
				  }
                            } else {
                                array_push($results, "not attempted");
                            }
                        }
                    }
                    //fourth update result
                    $final_score = $tmp_score / 100;

                    if (in_array('not attempted', $results) || in_array('failed', $results) || in_array('incomplete', $results)) {
                        if ($incomplete == 1) {
                            $overall_result = "incomplete";
                            $result9 = mysqli_query($conn, "UPDATE course_assignment 
								SET attended_date='" . $datetime . "',
								result='" . $overall_result . "',
								score=" . $final_score . ",
								last_updated_user=" . $batch_user_id . ",
								last_updated_ts='" . $datetime . "' 
								WHERE offering_id=" . $oid . " AND user_id=" . $uid . "");
                        } else {
                            $result9 = mysqli_query($conn, "UPDATE course_assignment 
								SET attended_date='" . $datetime . "',
								result=null,
								score=" . $final_score . ",
								last_updated_user=" . $batch_user_id . ",
								last_updated_ts='" . $datetime . "' 
								WHERE offering_id=" . $oid . " AND user_id=" . $uid . "");
                        }
                        if(!$result9){
                            $error = true;
                        }
                    } else if (in_array('passed', $results) || in_array('completed', $results)) {
                        //Fourth - Calculate Completion Points
                         
                        $completion_points = 'null';
                        $result10 = mysqli_query($conn, "SELECT Group_concat(completion_points) as points FROM certificate_assignment WHERE offering_id=" . $oid . "");
                        if (mysqli_num_rows($result10) > 0) {
                            $row10 = mysqli_fetch_array($result10);
                            if ($row10['points'] != null) {
                                $result11 = mysqli_query($conn, "SELECT completion_points FROM certificate_assignment 
									WHERE offering_id=" . $oid . " AND cut_off<=" . $final_score . " ORDER BY cut_off DESC LIMIT 1");
                                if (mysqli_num_rows($result10) > 0) {
                                    if ($row11 = mysqli_fetch_array($result11)) {
                                        $completion_points = $row11['completion_points'];
                                    }
                                } else {
                                    $result13 = mysqli_query($conn, "SELECT completion_points FROM certificate_assignment WHERE offering_id=" . $oid . " and cut_off is NULL");
                                    if (mysqli_num_rows($result13) > 0) {
                                        if ($row13 = mysqli_fetch_array($result13)) {
                                            $completion_points = $row13['completion_points'];
                                        }
                                    }
                                }
                            } else {
                                $result11 = mysqli_query($conn, "SELECT co.completion_points from course_offering as co where co.offering_id=" .$oid . "");
                                if (mysqli_num_rows($result11) > 0) {
                                    if ($row11 = mysqli_fetch_array($result11)) {
                                        $completion_points = $row11['completion_points'];
                                    }
                                }
                            }
                        } else {
                            $result11 = mysqli_query($conn, "SELECT co.completion_points from course_offering as co where co.offering_id=" . $oid . "");
                            if (mysqli_num_rows($result11) > 0) {
                                if ($row11 = mysqli_fetch_array($result11)) {
                                    $completion_points = $row11['completion_points'];
                                }
                            }
                        }
                        if ($completion_points == "") {
                            $completion_points = 'null';
                        }
               
                        $overall_result = "complete";
                        $result12 = mysqli_query($conn, "UPDATE course_assignment 
						  SET attended_date='" . $datetime . "',
							  result='" . $overall_result . "',
							  score=" . $final_score . ",
							  completion_points =" . $completion_points . ",
							  last_updated_user=" . $batch_user_id . ",
							  last_updated_ts='" . $datetime . "' 
						  WHERE offering_id=" . $oid . " AND user_id=" . $uid . "");
                        if ($result12) {
                      
                            $result14 = mysqli_query($conn, "SELECT SUM(completion_points) as count 
								FROM course_assignment WHERE result='complete' and user_id=" . $uid . " AND status IS NULL");
                            if (mysqli_num_rows($result14) > 0) {
                                $row14 = mysqli_fetch_array($result14);
                                $result15 = mysqli_query($conn, "UPDATE user_account SET total_completion_points=" . $row14['count'] . ",
									last_updated_user=" . $batch_user_id . ",last_updated_ts='" . $datetime . "'
									WHERE user_id=" . $uid . "");
                                if(!$result15){
                                    $error = true;
                                }
                            }
                        }else{
                            $error = true;
                        }
                    }
                    
                    //fifth update content_track,scorm_offer_criteria
                     $res1 = mysqli_query($conn, "select status from xapi_track where xapi_id=$xapi_id and offering_id=$oid and user_id=$uid and contentdtl_id=$cid"); 
                            if (mysqli_num_rows($res1) > 0) {
                                if ($row1 = mysqli_fetch_array($res1)) {
                                    $contentstatus = $row1['status'];
                                }
                            }
                    if($contentstatus == "passed" || $contentstatus == "completed"){
                       $res_upcontrack = mysqli_query($conn, "update content_track set completed='Y',content_progress='100',last_viewed_ts='" . $datetime . "' where offering_id=$oid and user_id=$uid and contentdtl_id=$cid");
                          if(!$res_upcontrack){
                                    $error = true;
                                }
                        $weightage = 0;
                        $result_scormoffer = mysqli_query($conn, "select weightage from result_criteria where xapi_id=$xapi_id and offering_id=$oid");
                        if (mysqli_num_rows($result_scormoffer)) {
                                if ($row_scormoffer = mysqli_fetch_array($result_scormoffer)) {
                                        $weightage = $row_scormoffer['weightage'];
                                }
                               $result_scormoffer1 = mysqli_query($conn, "INSERT INTO scorm_offer_criteria(user_id,offering_id,xapi_id,scorm_id,weightage,last_updated_ts) 
					values($uid,$oid,$xapi_id,0,$weightage,'$datetime') 
					ON DUPLICATE KEY UPDATE weightage=$weightage,last_updated_ts='" . $datetime . "'");
                               if(!$result_scormoffer1){
                                    $error = true;
                                }
			}
                                //cal update completion
                          $result_calupdate=mysqli_query($conn,"UPDATE course_assignment set last_updated_user=$batch_user_id,last_updated_ts='$datetime',"
                            . "completion=(SELECT FLOOR(((COUNT(cc.contentdtl_id ))*100)/(SELECT count(oc.contentdtl_id) "
                            . "FROM offering_content as oc where oc.offering_id=$oid AND oc.status='Active')) as cmp "
                            . "FROM offering_content as cc WHERE cc.offering_id =$oid AND cc.status='Active' AND EXISTS "
                            . "(SELECT 1 FROM content_track as ct WHERE ct.offering_id =$oid AND ct.user_id =$uid AND "
                            . "cc.contentdtl_id = ct.contentdtl_id AND ct.completed='Y')), "
                            . "content_completion=COALESCE((SELECT ROUND((SUM(ct.content_progress))/(SELECT count(oc.contentdtl_id) 
                           FROM offering_content as oc where oc.offering_id=$oid AND oc.status='Active'),0)
                            FROM content_track as ct,offering_content as oc  WHERE ct.offering_id =$oid AND ct.user_id =$uid AND oc.contentdtl_id=ct.contentdtl_id AND ct.offering_id=oc.offering_id),0) "
                            . "where offering_id=$oid and user_id=$uid AND (status IS NULL or status='awaited')");      
                         if($result_calupdate){
                             $result_calupdate1 = mysqli_query($conn, "SELECT result,completion,content_completion,"
                    . "if((SELECT count(oc.contentdtl_id) FROM offering_content as oc where oc.offering_id=$oid AND oc.status='Active'), True, False) as total_contents "
                    . "from course_assignment where offering_id=$oid AND user_id=$uid AND status IS NULL");
                                if (mysqli_num_rows($result_calupdate1) > 0) {
                                    if ($row_calupdate1 = mysqli_fetch_array($result_calupdate1)) {
                                        $content_completion = $row_calupdate1['content_completion'];
                                        if($content_completion == "100"){
                                             $result_calupdate2 = mysqli_query($conn, "SELECT co.offering_id,co.completion_points from course_offering as co where co.offering_id=$oid and co.end_date >=$date "
                    . "AND NOT EXISTS (SELECT offering_id from result_criteria as rc where rc.offering_id=co.offering_id)");
                                            if (mysqli_num_rows($result_calupdate2) > 0) {
                                                if($row_calupdate2 = mysqli_fetch_array($result_calupdate2)){
                                                          if($row_calupdate2['completion_points'] != NULL || $row_calupdate2['completion_points'] != ""){
                                                        $completion_points_off = $row_calupdate2['completion_points'];
                                                        $result_calupdate3 = mysqli_query($conn, "UPDATE course_assignment 
						  SET attended_date='" . $datetime . "',
							  result='complete',
							  score=0,
							  completion_points =" . $completion_points_off . ",
							  last_updated_user=" . $batch_user_id . ",
							  last_updated_ts='" . $datetime . "' 
						  WHERE offering_id=" . $oid . " AND user_id=" . $uid . "");
                                                    }else{
                                                        $result_calupdate3 = mysqli_query($conn, "UPDATE course_assignment 
						  SET attended_date='" . $datetime . "',
							  result='complete',
							  score=0,
							  completion_points =null,
							  last_updated_user=" . $batch_user_id . ",
							  last_updated_ts='" . $datetime . "' 
						  WHERE offering_id=" . $oid . " AND user_id=" . $uid . "");
                                                    }
                                                    //update_result()
                                                     
                                                    if ($result_calupdate3) {
                                                        $result_calupdate3 = mysqli_query($conn, "SELECT SUM(completion_points) as count 
                                                                                            FROM course_assignment WHERE result='complete' and user_id=" . $uid . " AND status IS NULL");
                                                        if (mysqli_num_rows($result_calupdate3) > 0) {
                                                            $row14 = mysqli_fetch_array($result_calupdate3);
                                                            $result_calupdate4 = mysqli_query($conn, "UPDATE user_account SET total_completion_points=" . $row14['count'] . ",
                                                                                                    last_updated_user=" . $batch_user_id . ",last_updated_ts='" . $datetime . "'
                                                                                                    WHERE user_id=" . $uid . "");
                                                            if(!$result_calupdate4){ $error = true; }
                                                        }
                                                    }else{ $error = true; }
                                                }
                                            }
                                        }
                                    }
                                }
                         }else{ $error = true; }  
                        
                    } else {
                        $weightage = 0;
                        $result_scormoffer = mysqli_query($conn, "select weightage from result_criteria where xapi_id=$xapi_id and offering_id=$oid");
                        if (mysqli_num_rows($result_scormoffer)) {
                                if ($row_scormoffer = mysqli_fetch_array($result_scormoffer)) {
                                    $weightage = $row_scormoffer['weightage'];        
                                }
                              $result_scormoffer1 = mysqli_query($conn, "INSERT INTO scorm_offer_criteria(user_id,offering_id,xapi_id,scorm_id,weightage,last_updated_ts) 
					values($uid,$oid,$xapi_id,0,$weightage,'$datetime') 
					ON DUPLICATE KEY UPDATE weightage=$weightage,last_updated_ts='" . $datetime . "'");
                                if(!$result_scormoffer1){ $error = true; }
				}
                                //cal update completion
                          $result_calupdate=mysqli_query($conn,"UPDATE course_assignment set last_updated_user=$batch_user_id,last_updated_ts='$datetime',"
                    . "completion=(SELECT FLOOR(((COUNT(cc.contentdtl_id ))*100)/(SELECT count(oc.contentdtl_id) "
                    . "FROM offering_content as oc where oc.offering_id=$oid AND oc.status='Active')) as cmp "
                    . "FROM offering_content as cc WHERE cc.offering_id =$oid AND cc.status='Active' AND EXISTS "
                    . "(SELECT 1 FROM content_track as ct WHERE ct.offering_id =$oid AND ct.user_id =$uid AND "
                    . "cc.contentdtl_id = ct.contentdtl_id AND ct.completed='Y')), "
                    . "content_completion=COALESCE((SELECT ROUND((SUM(ct.content_progress))/(SELECT count(oc.contentdtl_id) 
                   FROM offering_content as oc where oc.offering_id=$oid AND oc.status='Active'),0)
                    FROM content_track as ct,offering_content as oc  WHERE ct.offering_id =$oid AND ct.user_id =$uid AND oc.contentdtl_id=ct.contentdtl_id AND ct.offering_id=oc.offering_id),0) "
                    . "where offering_id=$oid and user_id=$uid AND (status IS NULL or status='awaited')");    
                          if(!$result_calupdate){ $error = true; }
                    }
            
          }else{ $error = true; }
            if(!$error){
                $update_track = mysqli_query($conn, "update xapi_track set xapi_batch=null where offering_id=$oid and user_id=$uid and xapi_id=$xapi_id and contentdtl_id=$cid");
                if($update_track){

                }
            }
        }
        $conn->commit();
        fwrite($fp, $count . "\n");
        fclose($fp);
    }
} catch (Exception $ex) {
  $conn->rollback();

    $fp = fopen($file_name, "a");
    fwrite($fp, "Date:" . $datetime . "\n");
    fwrite($fp, "Message: " . $e->getMessage() . "\n");
    fclose($fp);
}
$conn->close();
?>

