<?php
header( 'Access-Control-Allow-Origin: *' );
header( 'Access-Control-Allow-Methods: GET, POST' );
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
include '../../../config/index.php';
// var_dump( $_POST );

$data = file_get_contents( 'php://input' );
$obj = json_decode( $data, true );

$db_description = 'descriptions/'.time();
$dv_file = '/home/sendusco/app/descriptions/'.time();

$file = fopen( $dv_file, 'w' ) or die( 'Unable to open file!' );

fwrite( $file, $_POST[ 'AddAssignment' ] );

fclose( $file );
$sql10 = "select * from course_lesson_assignment WHERE course_lesson_plan_id='".$_GET[ 'course_lesson_plan_id' ]."'   ";
$result10 = mysqli_query( $conn, $sql10 );

if ( mysqli_num_rows( $result10 ) > 0 ) {
    $sql = "UPDATE course_lesson_assignment SET
    assignment='".$db_description."'
    WHERE course_lesson_plan_id='".$_GET["course_lesson_plan_id"]."'";

    if ( mysqli_query( $conn, $sql ) ) {

        $curl_post_data = array(
            'Success' => 'true',
            'Reason' => 'You Have Succesfully added a Course Assignment',
            'course_lesson_plan_id' => $_GET["course_lesson_plan_id"] ,
        );
        echo json_encode( $curl_post_data );

    } else {

        $curl_post_data = array(
            'Success' => 'false',
            'Reason' => 'Unable to Succesfully Add a Course Assignment, Kindly contact Admin for help?',
            'course_lesson_plan_id' => $_GET["course_lesson_plan_id"] ,
        );
        echo json_encode( $curl_post_data );

    }
} else {
    $sql = "INSERT INTO course_lesson_assignment VALUES (NULL,
    '".$_GET[ 'course_lesson_plan_id' ]."',
    '".$db_description."',
    now())";

    if ( mysqli_query( $conn, $sql ) ) {

        $curl_post_data = array(
            'Success' => 'true',
            'Reason' => 'You Have Succesfully added a Course Assignment',
            'course_lesson_plan_id' => $_GET["course_lesson_plan_id"] ,
        );
        echo json_encode( $curl_post_data );

    } else {

        $curl_post_data = array(
            'Success' => 'false',
            'Reason' => 'Unable to Succesfully Add a Course Assignment, Kindly contact Admin for help?',
            'course_lesson_plan_id' => $_GET["course_lesson_plan_id"] ,
        );
        echo json_encode( $curl_post_data );

    }
}

?>