 <?php

    session_start();

    include_once  "../db.php";

    

    date_default_timezone_set('Asia/Kolkata');

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



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

        header("Location: logout.php");



    if (!isset($_GET['edit_ref']) OR empty($_GET['edit_ref'])) {

       header("Location: view-homework.php");

    }



    $h_sql = "SELECT * FROM homework WHERE h_ref = '$_GET[edit_ref]'";

    $h_res = mysqli_query($con,$h_sql);

    $h_row= mysqli_fetch_assoc($h_res);

?>

<!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"> 

        <!-------Including Image Upload CSS File------>

        <link rel="stylesheet" type="text/css" href="multiPhotoUploadApp/style.css"> 



</head>

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

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

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

<?php 

$ck_sql = "SELECT * FROM homework_img  WHERE hi_ref = '$_GET[edit_ref]' AND sh_id = '$_SESSION[uid]' LIMIT 1";

$ck_res = mysqli_query($con,$ck_sql);

if ($ck_total_rows=mysqli_num_rows($ck_res)>0) {

    $ck_row=mysqli_fetch_assoc($ck_res);

    $ck_sl = $ck_row['hi_sl'];

    $ck_content = $ck_row['hi_name'];

}else{

    ?>

            <script>

                alert('You have to Submit Your Homework First, Then Edit...');

                window.location = "submit-homework.php?h_ref=<?php echo $_GET['edit_ref']; ?>";

            </script>

        <?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"> Edit Homework</h5>

                            <div class="card-body">

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

                                    <div class="row mt-4">

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

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

                                            <div class="form-control"><?php echo $h_row['h_title']; ?></div>

                                        </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" required><?php echo $ck_content; ?></textarea>

                                            <input type="hidden" name="ck_sl" value="<?php echo $ck_sl; ?>">

                                        </div>

                                    </div>

                                   



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

                                         <input type="submit" value=" Update Homework " name="uploadHomeworkCk" id="upload" 

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



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

                                                <i class="fa fa-backward"></i> Back

                                            </a>

                                    </div>

                                </form>

                            </div>

                        </div>

                    </div>

                </div>

            </div>







<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">Edit Image</h5>

                <div class="card-body">

                    

                        

                        <?php 

                            $img_sql = "SELECT * FROM homework_img  WHERE hi_ref = '$_GET[edit_ref]' AND sh_id = '$_SESSION[uid]' ORDER BY hi_pos ASC LIMIT 1,50";

                            $img_res = mysqli_query($con,$img_sql);

                            $img_pos = 0;

                            if ($img_total_rows=mysqli_num_rows($img_res)>0) {

                                

                                while ($img_row=mysqli_fetch_assoc($img_res)) {

                                    $img_path = "../upload/homework_submit/";

                                    $img_sl = $img_row['hi_sl'];

                                    $img_name = $img_row['hi_name'];

                                    $img_pos=$img_row['hi_pos'];

                                ?>  

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

                                <div class="row">

                                    <div class="col-md-1 col-xs-1" style="margin-bottom: 10px;">

                                        <h3 style="margin-top: 20px;"><?php echo $img_row['hi_pos']-1; ?></h3>

                                    </div>

                                    <div class="col-md-5 col-xs-5" style="margin-bottom: 10px;">

                                        

                                        <img src="<?php echo $img_path.$img_row['hi_name']; ?>" width="90" height="120" style="display: block;margin-left: auto;margin-right: auto;">

                                    </div>

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

                                           

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



                                         <input type="hidden" name="hi_sl" value="<?php echo $img_sl; ?>" class="form-control"/>



                                         <input type="hidden" name="img_name" value="<?php echo $img_name; ?>" class="form-control"/>



                                         <input type="submit" value="Update Img <?php echo $img_row['hi_pos']-1; ?>" name="updateImage" id="upload" class="btn btn-success text-sm-center mt-4"/>



                                         

                                    </div>

                                 </div>

                                 </form>

                                <?php

                                }

                            }

                        ?>

                        <hr>

                        <hr>

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

                                    

                            <div class="row mt-4">

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

                                    <label class="badge badge-warning">Add New Images</label> 

                                    <br><br>

                                    <div id="filediv"><input name="file[]" type="file" id="file"/></div><br/>

   

                                    <input type="button" id="add_more" class="upload" value="Add More Files"/>



                                    <input type="hidden" name="homework_ref" value="<?php echo $_GET['edit_ref']; ?>" class="form-control"/>

                                    <?php 

                                        if ($img_pos == 0) {

                                            $img_pos = 1;

                                        }

                                     ?>

                                    <input type="hidden" name="img_pos" value="<?php echo $img_pos; ?>" class="form-control"/>

                                    <input type="hidden" name="teacher_id" value="<?php echo $h_row['uploadedby']; ?>">

                                </div>

                            </div>



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

                                 <input type="submit" value="Upload New Images" name="img_submit" id="upload" 

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

                                 &nbsp;&nbsp;&nbsp; 

                                 <a href="view-homework.php" class="btn btn-warning text-sm-center mt-4"><i class="fa fa-backward"></i>  Back</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>

            <!-------Including Image Upload Js File------>

            <script src="multiPhotoUploadApp/multiPhotoScript.js"></script>





</body>

</html>



<?php include "multiPhotoUploadApp/edit_upload.php"; ?>



<?php

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



    $content = $_POST['content'];

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

    



    $up_sql = "UPDATE homework_img SET hi_name='$content', hi_editDT='$currentDT' WHERE hi_sl='$ck_sl' AND hi_ref = '$_GET[edit_ref]' AND sh_id = '$_SESSION[uid]' ";





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

            ?>

            <script>

                //alert('Submit Successfully');

                window.location = "edit-homework.php?edit_ref=<?php echo $_GET['edit_ref']; ?>";

            </script>

            <?php

        }

        else{

            ?>

            <script>

                alert('OOPS !!!!');

                setTimeout(() => {

                    window.location = "edit-homework.php?edit_ref=<?php echo $_GET['edit_ref']; ?>";

                }, 1200);

            </script>

            <?php

        }

}



?>

<?php

    

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





    $hi_sl = $_POST['hi_sl'];

    $old_img_name = $_POST['img_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_submit/".$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 ........

         

        echo $img_up_sql = "UPDATE homework_img SET hi_name='$new_profle_pic', hi_editDT='$currentDT' WHERE hi_sl='$hi_sl' AND hi_ref = '$_GET[edit_ref]' AND sh_id = '$_SESSION[uid]'";

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



                $del_img_path = "../upload/homework_submit/".$old_img_name;

                unlink($del_img_path);

                ?>

                <script>

                    alert("Image Upload Successfully");

                    window.location = "edit-homework.php?edit_ref=<?php echo $_GET['edit_ref']; ?>";

                </script>

                <?php

            }

            else{

                ?>

                <script>

                    alert("Something's Wrong");

                    //window.location = "edit-homework.php?edit_ref=<?php// echo $_GET['edit_ref']; ?>";

                </script>

                <?php

            }



        }

        else

        {   

            ?>

                <script>

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

                </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);



    }

?>