<?php

    session_start();

    include "../db.php";

    date_default_timezone_set('Asia/Kolkata');

    $currentTime = date('M j, Y g:i a');



    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) && !isset($_SESSION['uname']) )

        header("Location: logout.php");



    $admin_2_run = "SELECT admin_ref FROM users WHERE uref = '$_SESSION[uref]'";

    $admin_2_row2 = mysqli_query($con, $admin_2_run);

    $admin_2_row = mysqli_fetch_assoc($admin_2_row2);

    $admin_2 = $admin_2_row["admin_ref"];

?>

<!DOCTYPE html>

<html lang="en">

<head>

 <?php include "includes/meta.php"; ?>        

    <?php include "includes/css.php"; ?>  

        <link hr ef="css/all.min.css" rel="stylesheet" type="text/css">  



</head>

<body id="page-top" class="mt-55">

            <div style="margin-top: 130px;"></div>

                <?php include "includes/header2.php"; ?>

                <div class="container">

                    <div class="row justify-content-center">

                    <div class="col-md-8">

                        <div class="card shadow mt-3">

                        <h5 class="card-header text-dark  text-center font-weight-bold"> Student Homework</h5>

                            <div class="card-body">

                                <form method="POST" enctype="multipart/form-data">

                                    <div class="row">

                                        <div class="col-md-6">

                                        <label class="badge badge-warning">Class</label>



                                        <select class="form-control" name="sclass" id="category-dropdown">

                                            <option value="" selected disabled>Select Class...</option>

                                            <?php

                                            $result = mysqli_query($con,"SELECT * FROM class WHERE uploadedby = '$admin_2'");

                                            while($row = mysqli_fetch_array($result)) {

                                            ?>

                                            <option value="<?php echo $row['sid'];?>"><?php echo $row["sclass"];?></option>

                                            <?php

                                            }

                                            ?>

                                        </select>



   

                                        </div>

                                        <div class="col-md-6">

                                            <label class="badge badge-warning">Subject</label>

                                            <select class="form-control" name="subject" id="sub-category-dropdown">

                                                <option value="" id="classMsg" selected disabled >Select Class First</option>

                                            </select>

                                        </div>

                                        

                                    </div>

                                    <div class="row mt-4">

                                        <div class="col-md-12">

                                            <label class="badge badge-warning">Homework Title</label>

                                            <input type="text" class="form-control"name="title" placeholder="Enter Title" required>

                                        </div>

                                    </div>

                                    <div class="row mt-4">

                                        <div class="col-md-12">

                                            <label class="badge badge-warning">Homework Script</label>

                                            <textarea name="content" id="content" class="form-control ckeditor"></textarea>

                                        </div>

                                    </div>

                                    <div class="row mt-4">

                                        <div class="col-md-12">

                                            <label class="badge badge-warning">Image</label>

                                            <input type="file" class="form-control"name="image">

                                        </div>

                                    </div>

                                   

                                    <br><br>

                                    <div class=" row justify-content-center">

                                         <input type="submit" value="Submit" name="uploadStudenthomework" id="upload" 

                                         class="btn btn-success text-sm-center mt-4"/>



                                         &nbsp;&nbsp;&nbsp; <a href="view-homework.php" class="btn btn-primary text-sm-center mt-4">

                                                <i class="fa fa-eye"></i> Review Home Work

                                            </a>

                                    </div>

                                </form>

                            </div>

                        </div>

                    </div>

                </div>

            </div>



     <div style="margin-top: 170px;"></div>

            <?php include "includes/ft.php"; ?>

            <?php include "includes/js.php"; ?>

            <!-------Including ckeditor Js File------>

            <script src="vendor/ckeditor.js"></script>



<script>

$(document).ready(function() {



    $('#category-dropdown').on('change', function() {

        var category_id = this.value;

        var admin_2 = "<?php echo $admin_2; ?>";

        $("#classMsg").hide();

        $.ajax({

            url: "notes-upload-CatSubcat-Ajax.php",

            type: "POST",

            data: {

            admin_2: admin_2,

            category_id: category_id

            },

            cache: false,

            success: function(result){

            $("#sub-category-dropdown").html(result);

            }

        });

    });



});

</script>



    <script>

     CKEDITOR.replace( 'content', {

        height: 300,

        filebrowserUploadUrl: "upload.php"

    });

</script>



</body>

</html>



<?php

if (isset($_POST['uploadStudenthomework'])) {



    $subject = mysqli_real_escape_string($con, strip_tags($_POST['subject']));

    $sclass = mysqli_real_escape_string($con, strip_tags($_POST['sclass']));

    $title = mysqli_real_escape_string($con, strip_tags($_POST['title']));

    $content = mysqli_real_escape_string($con, strip_tags($_POST['content']));



    $h_ref = rand().'_'.time();





    if(isset($_FILES['image']['name']) && !empty($_FILES['image']['name'])){



        // Getting file name

        $filename = $_FILES['image']['name'];

             

        // Valid extension

        $valid_ext = array('png','jpeg','jpg');



          

        $photoExt1 = @end(explode('.', $filename)); // explode the image name to get the extension

        $phototest1 = strtolower($photoExt1);

          

        $new_profle_pic = ''.md5(uniqid()).'.'.$phototest1;

          

        // Location

        $location = "../upload/homework/".$new_profle_pic;



        // file extension

        $file_extension = pathinfo($location, PATHINFO_EXTENSION);

        $file_extension = strtolower($file_extension);



        // Check extension

        if(in_array($file_extension,$valid_ext)){  



                // Compress Image

                compressedImage($_FILES['image']['tmp_name'],$location,20);

            

                //Here i am enter the insert code in the step ........

             

           $ins_sql = "INSERT INTO homework (h_class, h_subject, h_title,homework, homework_script, h_ref, uploadedby, h_date, h_admin_2) VALUES ('$sclass', '$subject', '$title','$new_profle_pic', '$content', '$h_ref','$_SESSION[uref]', '$currentTime', '$admin_2')";

               

                

        }

        else

        {   

            ?>

                <script>

                    alert("File format is not correct.");

                </script>

            <?php

        }

  

    }else{



     echo   $ins_sql = "INSERT INTO homework (h_class, h_subject, h_title, homework_script, h_ref, uploadedby, h_date, h_admin_2) VALUES ('$sclass', '$subject', '$title', '$content', '$h_ref', '$_SESSION[uref]', '$currentTime', '$admin_2')";



    }



    if(mysqli_query($con, $ins_sql)){

        ?>

        <script>

            alert('Submit Successfully');

            //window.location = 'notes-upload-img.php?ref=<?php// echo $h_ref; ?>';

            window.location = 'homework-upload.php';

        </script>

        <?php

    }

    else{

        ?>

        <script>

            alert('OOPS !!!!');

            setTimeout(() => {

                window.location = 'homework-upload.php';

            }, 1200);

        </script>

        <?php

    }





}





// Compress image

function compressedImage($source, $path, $quality) {



        $info = getimagesize($source);



        if ($info['mime'] == 'image/jpeg') 

            $image = imagecreatefromjpeg($source);



        elseif ($info['mime'] == 'image/gif') 

            $image = imagecreatefromgif($source);



        elseif ($info['mime'] == 'image/png') 

            $image = imagecreatefrompng($source);



        imagejpeg($image, $path, $quality);



}



?>

