<?php

// echo "he";

// die();

session_start();

include "../db.php";

date_default_timezone_set('Asia/Kolkata');

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

    $question_name = $_POST['question_name'];

    $coption = $_POST['coption'];

    $difficulty = $_POST['difficulty'];

    $marks = $_POST['marks'];

    $mocktest = $_POST['mocktest'];

    $answer_decs = $_POST['answer_decs'];

  $NewFileName ="";

  $DescNewFileName ="";

  $filename = pathinfo(basename($_FILES["imageQuestion"]["name"]));

  if ($filename['filename']  != null) {

    $tempname = $_FILES["imageQuestion"]["tmp_name"];

    $NewFileName = md5(uniqid()) . '.' . $filename['extension'];

    $folder = "./upload/question/" . $NewFileName;

    move_uploaded_file($tempname, $folder);

  }

  $descfilename = pathinfo(basename($_FILES["answer_decs_image"]["name"]));

  if ($descfilename['filename']  != null) {

    $desctempname = $_FILES["answer_decs_image"]["tmp_name"];

    $DescNewFileName = md5(uniqid()) . '.' . $descfilename['extension'];

   

    $Descfolder = "./upload/question/" . $DescNewFileName;

    move_uploaded_file($desctempname, $Descfolder);

  }



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



  $ins_sql = "INSERT INTO `question`( `mocktest_id`, `question_name`, `question_image`, `correct_option`, `difficulty`, `marks`, `answer_desc`, `answer_desc_image`, `created_at`) VALUES ('$mocktest','$question_name','$NewFileName','$coption','$difficulty','$marks','$answer_decs','$DescNewFileName','$currentTime')";

  // print_r($ins_sql);

  //     die();

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

      ?>

    <script>

      window.location = "view-questions.php?mocktest=<?php echo $_POST['mocktest']?>";

    </script>

    <?php

    } else {

      ?>

    <script>

      window.location = "add-question.php?mocktest=<?php echo $_POST['mocktest']?>";

    </script>

    <?php

    }

 }

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

  $question_name = $_POST['question_name'];

    $coption = $_POST['coption'];

    $difficulty = $_POST['difficulty_edit'];

    $marks = $_POST['marks'];

    $answer_decs = $_POST['answer_decs'];

  $NewFileName =$_POST['question_img_link'];

  $DescNewFileName =$_POST['question_desc_img_link'];

  $filename = pathinfo(basename($_FILES["imageQuestion"]["name"]));

  if ($filename['filename']  != null) {

    $tempname = $_FILES["imageQuestion"]["tmp_name"];

    $NewFileName = md5(uniqid()) . '.' . $filename['extension'];

    $folder = "./upload/question/" . $NewFileName;

    move_uploaded_file($tempname, $folder);

  }

  $descfilename = pathinfo(basename($_FILES["answer_decs_image"]["name"]));

  if ($descfilename['filename']  != null) {

    $desctempname = $_FILES["answer_decs_image"]["tmp_name"];

    $DescNewFileName = md5(uniqid()) . '.' . $descfilename['extension'];

   

    $Descfolder = "./upload/question/" . $DescNewFileName;

    move_uploaded_file($desctempname, $Descfolder);

  }



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

  $ins_sql = "UPDATE question SET question_name='$question_name', question_image='$NewFileName', correct_option='$coption', difficulty='$difficulty', marks='$marks',  answer_desc='$answer_decs', answer_desc_image='$DescNewFileName' WHERE id='$_POST[question_id]'";

  $getMost = "SELECT * FROM question WHERE id='$_POST[question_id]'";

  $mockResult=mysqli_query($con, $getMost);

  $mockId = mysqli_fetch_assoc($mockResult);

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

      ?>

    <script>

      window.location = "view-questions.php?mocktest=<?php echo $mockId['mocktest_id']?>";

    </script>

    <?php

    } else {

      ?>

    <script>

      window.location = "add-question.php?mocktest=<?php echo $mockId['mocktest_id']?>";

    </script>

    <?php

    }

 }

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

  $mocktest = "SELECT * FROM mocktest WHERE class_id = '$_POST[Class_data]' AND subject_id = '$_POST[Subject_Data]'";

    $result = mysqli_query($con, $mocktest);

    $rowcount = mysqli_num_rows($result);

    if($rowcount==0){

      $result = "FALSE";

    }else{

      $result = "TRUE";

    }

    echo $result;

}



?>