Ei kuvausta

insertExperience.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. require_once 'config.php';
  3. require_once 'dbh.inc.php';
  4. require_once 'checkLogin.php';
  5. // EXAMPLE INPUT...
  6. // array(9) { ["title"]=> string(5) "title" ["description"]=> string(11) "description" ["type"]=> string(4) "type" ["duration"]=> string(1) "1" ["start"]=> string(10) "2019-11-22" ["end"]=> string(10) "2019-11-29" ["institution"]=> string(1) "1" ["expiry_time"]=> string(2) "60" ["newExperience"]=> string(0) "" }
  7. // IF USER ENTERS PAGE AFTER 'Create' BUTTON HAS BEEN PRESSED (FROM home.php), INSERT NEW EXPERIENCE TO DATABASE
  8. // ELSE (IF USER ENTERED THIS PAGE WITHOUT SUBMITING A FORM) REDIRECT TO home.php
  9. if(isset($_POST['newExperience'])) {
  10. // CLEAN USER INPUT
  11. $title = mysqli_real_escape_string($connection, trim($_POST['title']));
  12. $description = mysqli_real_escape_string($connection, trim($_POST['description']));
  13. $type = mysqli_real_escape_string($connection, trim($_POST['type']));
  14. $duration_weeks = mysqli_real_escape_string($connection, trim($_POST['duration']));
  15. $start_date = mysqli_real_escape_string($connection, trim($_POST['start']));
  16. $end_date = mysqli_real_escape_string($connection, trim($_POST['end']));
  17. $institution = mysqli_real_escape_string($connection, trim($_POST['institution']));
  18. $expiry_time = mysqli_real_escape_string($connection, trim($_POST['expiry_time']));
  19. // Check that experienceTitle is not an empty string
  20. // And that it doesn't exceed 60 characters (database limit)
  21. if($title === "") {
  22. http_response_code(400);
  23. echo json_encode(array("error" => "Please specify experience title."));
  24. exit();
  25. } else if(mb_strlen($title) > 60) {
  26. http_response_code(400);
  27. echo json_encode(array("error" => "Experience title too long (max. is 60 characters)."));
  28. exit();
  29. }
  30. // Check that experienceDescription is not an empty string
  31. // And that it doesn't exceed 100 characters (database limit)
  32. if($description === "") {
  33. http_response_code(400);
  34. echo json_encode(array("error" => "Please specify experience description."));
  35. exit();
  36. } else if(mb_strlen($description) > 100) {
  37. http_response_code(400);
  38. echo json_encode(array("error" => "Experience description too long (max. is 100 characters)."));
  39. exit();
  40. }
  41. // Check that experienceType is Course-Based Research or Independent Research
  42. if($type !== "Course-Based Research" && $type !== "Independent Research") {
  43. http_response_code(400);
  44. echo json_encode(array("error" => "Please specify valid experience type (either 'Course-Based Research' or 'Independent Research')."));
  45. exit();
  46. } else if(mb_strlen($type) > 60) {
  47. http_response_code(400);
  48. echo json_encode(array("error" => "Experience type too long (max. is 60 characters)."));
  49. exit();
  50. }
  51. // Check that startDate is not an empty string
  52. // And that startDate is in appropriate format YYYY-MM-DD (e.g. 2222-02-22)
  53. // WARNING: only handling AST
  54. // HELP: https://www.codexworld.com/how-to/validate-date-input-string-in-php/
  55. function validDate($date) {
  56. $d = date_create_from_format("Y-m-d", $date);
  57. return $d && date_format($d, "Y-m-d") === $date;
  58. }
  59. if($start_date === "") {
  60. http_response_code(400);
  61. echo json_encode(array("error" => "Please specify experience's start date."));
  62. exit();
  63. } else if(!validDate($start_date)) {
  64. http_response_code(400);
  65. echo json_encode(array("error" => "Experience's start date ($start_date) given in wrong format (use YYYY-MM-DD instead)."));
  66. exit();
  67. }
  68. // Check that endDate is not an empty string
  69. // And that endDate is in appropriate format YYYY-MM-DD (e.g. 2222-02-22)
  70. // WARNING: only handling AST
  71. // HELP: https://www.codexworld.com/how-to/validate-date-input-string-in-php/
  72. if($end_date === "") {
  73. http_response_code(400);
  74. echo json_encode(array("error" => "Please specify experience's end date."));
  75. exit();
  76. } else if(!validDate($end_date)) {
  77. http_response_code(400);
  78. echo json_encode(array("error" => "Experience's end date ($end_date) given in wrong format (use YYYY-MM-DD instead)."));
  79. exit();
  80. }
  81. // Calculate duration in seconds
  82. $duration_seconds = strtotime($end_date) - strtotime($start_date);
  83. // Check that endDate occurs after the startDate
  84. if($duration_seconds <= 0) {
  85. http_response_code(400);
  86. echo json_encode(array("error" => "Experience's end date ($end_date) must occur at least a day after the start date ($start_date)."));
  87. exit();
  88. }
  89. // Change seconds to weeks and round up
  90. $duration_weeks = round($duration_seconds / 604800); // 60 * 60 * 24 * 7
  91. // Check that institutionID is not an empty string
  92. // And that the institutionID given is in database
  93. if($institution === "") {
  94. http_response_code(400);
  95. echo json_encode(array("error" => "Please specify institution ID."));
  96. exit();
  97. } else if(mysqli_query($connection, "SELECT * FROM institution WHERE id = '$institution';")->num_rows !== 1) {
  98. http_response_code(400);
  99. echo json_encode(array("error" => "Given institution ID not in database."));
  100. exit();
  101. }
  102. // Check that expiryTime is permitted (30min, 1hr, 2hr, 3hr, 6hr, 12hr, 18hr, 24hr)
  103. $permittedExpiryTimes = array("30", "60", "120", "180", "360", "720", "1080", "1440");
  104. if(!in_array($expiry_time, $permittedExpiryTimes, TRUE)) {
  105. http_response_code(400);
  106. echo json_encode(array("error" => "Expiry time given is not permitted (use only 30, 60, 120, 180, 360, 720, 1080 or 1440)."));
  107. exit();
  108. }
  109. /*** START OF DB QUERIES ***/
  110. // exit();
  111. // while(TRUE) {
  112. // exit();
  113. // }
  114. // exit();
  115. /*** IF IT GOT THROUGH VALIDATION, IT'S TOO LATE ***/
  116. // INSERTAR A TABLA experience EL TÍTULO, DESCRIPCIÓN, TIPO, DURACIÓN, START Y END DE LA EXPERIENCIA NUEVA
  117. $queryInsert = "INSERT INTO `experience` (`title`, `description`, `type`, `duration_weeks`, `start_date`, `end_date`, `expiry_time`) VALUES ('$title', '$description', '$type', '$duration_weeks', '$start_date', '$end_date', '$expiry_time');";
  118. if(!mysqli_query($connection, $queryInsert)) die("Error: ".mysqli_error($connection));
  119. // BUSCAR id DE LA EXPERIENCIA ACABADA DE INSERTAR
  120. $id_experience = mysqli_insert_id($connection) or die('Error: '.mysqli_error($connection));
  121. // CREAR HASH PARA EL ENVÍO DEL URL A ESTUDIANTES
  122. $hash = substr(sha1($id_experience), 0, 40);
  123. $queryHash = "UPDATE experience SET hash_id = '$hash' WHERE id = '$id_experience';";
  124. if(!mysqli_query($connection, $queryHash)) die("Error: ".mysqli_error($connection));
  125. // ASOCIAR LA EXPERIENCIA NUEVA CON LA INSTITUCIÓN ESCOGIDA
  126. $queryHookExperienceToInstitution = "INSERT INTO `institution_experience` (`id_institution`, `id_experience`) VALUES ('$institution', '$id_experience');";
  127. if(!mysqli_query($connection, $queryHookExperienceToInstitution)) die("Error: ".mysqli_error($connection));
  128. // MAKE IT CLIENT SIDE!!!!!!!! HAVE TO SEND IT IN SERVER RESPONSE!!!!!!!
  129. // header('Location: ../viewExperience.php?view=$id_experience');
  130. // exit();
  131. }