No Description

deleteQuestionnaire.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. require_once 'config.php';
  3. require_once 'dbh.inc.php';
  4. require_once 'checkLogin.php';
  5. // EXAMPLE INPUT...
  6. // array(2) { ["questionnaireID"]=> string(3) "137" ["deleteQuestionnaire"]=> string(0) "" }
  7. // WARNING: SHOULD CONFIRM THAT QUESTIONNAIRE BELONGS TO THE CORRECT USER (AUTHENTICATE THE TRANSACTION)
  8. if(isset($_POST['deleteQuestionnaire'])) {
  9. $questionnaireID = mysqli_real_escape_string($connection, trim($_POST['questionnaireID']));
  10. // Check that questionnaire ID is not empty string
  11. if($questionnaireID === "") {
  12. http_response_code(400);
  13. echo json_encode(array("error" => "Please specify questionnaire ID."));
  14. exit();
  15. }
  16. // Check that questionnaire is registered in the database
  17. if(mysqli_query($connection, "SELECT * FROM questionnair WHERE id = '$questionnaireID';")->num_rows !== 1) {
  18. http_response_code(400);
  19. echo json_encode(array("error" => "Given questionnaire ID ($questionnaireID) not in database."));
  20. exit();
  21. }
  22. // Check that no corresponding moments have been answered yet
  23. if(mysqli_query($connection, "SELECT * FROM student_subquestionnair WHERE id_subquestionnair IN (SELECT id FROM subquestionnair WHERE id_questionnair = '$questionnaireID');")->num_rows !== 0) {
  24. http_response_code(400);
  25. echo json_encode(array("error" => "Questionnaire already active, deletion denied."));
  26. exit();
  27. }
  28. /*** STEPS ***/
  29. // DELETE CATEGORIES (USE QUESTIONNAIRE-QUESTION, THEN QUESTION TABLE TO GET TO CATEGORY TABLE)*
  30. // DELETE SUBCATEGORIES (USE QUESTIONNAIRE-QUESTION, THEN QUESTION TABLE TO GET TO SUBCATEGORY TABLE)*
  31. // DELETE REFERENCE (USE QUESTIONNAIRE-QUESTION, THEN QUESTION TABLE TO GET TO REFERENCE TABLE)*
  32. // DELETE QUESTION-TYPE (USE QUESTIONNAIRE-QUESTION TABLE)*
  33. // DELETE QUESTIONS (USE QUESTIONNAIRE-QUESTION TABLE)*
  34. // DELETE MOMENT-QUESTION RELATIONS (USE MOMENT TABLE TO GET TO MOMENT-QUESTION TABLE)*
  35. // DELETE EXPERIENCE-MOMENT RELATIONS (USE MOMENT TABLE TO GET TO EXPERIENCE-MOMENT TABLE)*
  36. // DELETE MOMENTS
  37. // DELETE QUESTIONNAIRE-QUESTION RELATIONS
  38. // DELETE EXPERIENCE-QUESTIONNAIRE RELATION
  39. // DELETE QUESTIONNAIRE
  40. // Delete categories
  41. $queryDeleteCategories = "DELETE FROM category WHERE id IN (SELECT DISTINCT id_category FROM question WHERE id IN (SELECT id_question FROM questionnair_question WHERE id_questionnair = '$questionnaireID'));";
  42. $result = mysqli_query($connection, $queryDeleteCategories) or die("Error: ".mysqli_error($connection));
  43. // Delete subcategories
  44. $queryDeleteSubcategories = "DELETE FROM subcategory WHERE id IN (SELECT DISTINCT id_subcategory FROM question WHERE id IN (SELECT id_question FROM questionnair_question WHERE id_questionnair = '$questionnaireID'));";
  45. $result = mysqli_query($connection, $queryDeleteSubcategories) or die("Error: ".mysqli_error($connection));
  46. // Delete reference
  47. $queryDeleteReference = "DELETE FROM reference WHERE id IN (SELECT DISTINCT id_referencia FROM question WHERE id IN (SELECT id_question FROM questionnair_question WHERE id_questionnair = '$questionnaireID'));";
  48. $result = mysqli_query($connection, $queryDeleteReference) or die("Error: ".mysqli_error($connection));
  49. // Delete question types
  50. $queryDeleteQuestionTypes = "DELETE FROM question_type WHERE id_question IN (SELECT id_question FROM questionnair_question WHERE id_questionnair = '$questionnaireID');";
  51. $result = mysqli_query($connection, $queryDeleteQuestionTypes) or die("Error: ".mysqli_error($connection));
  52. // Delete questions
  53. $queryDeleteQuestions = "DELETE FROM question WHERE id IN (SELECT id_question FROM questionnair_question WHERE id_questionnair = '$questionnaireID');";
  54. $result = mysqli_query($connection, $queryDeleteQuestions) or die("Error: ".mysqli_error($connection));
  55. // Delete moment-question relations
  56. $queryUnhookQuestionFromMoment = "DELETE FROM subquestionnair_question WHERE id_subquestionnair IN (SELECT id FROM subquestionnair WHERE id_questionnair = '$questionnaireID');";
  57. $result = mysqli_query($connection, $queryUnhookQuestionFromMoment) or die("Error: ".mysqli_error($connection));
  58. // Delete experience-moment relations
  59. $queryUnhookMomentFromExperience = "DELETE FROM experience_subquestionnair WHERE id_subquestionnair IN (SELECT id FROM subquestionnair WHERE id_questionnair = '$questionnaireID');";
  60. $result = mysqli_query($connection, $queryUnhookMomentFromExperience) or die("Error: ".mysqli_error($connection));
  61. // Delete moments
  62. $queryDeleteMoments = "DELETE FROM subquestionnair WHERE id_questionnair = '$questionnaireID';";
  63. $result = mysqli_query($connection, $queryDeleteMoments) or die("Error: ".mysqli_error($connection));
  64. // Delete questionnaire-question relations
  65. $queryUnhookQuestionFromQuestionnaire = "DELETE FROM questionnair_question WHERE id_questionnair = '$questionnaireID';";
  66. $result = mysqli_query($connection, $queryUnhookQuestionFromQuestionnaire) or die("Error: ".mysqli_error($connection));
  67. // Delete experience-questionnaire relations
  68. $queryUnhookQuestionnaireFromExperience = "DELETE FROM experience_questionnair WHERE id_questionnair = '$questionnaireID';";
  69. $result = mysqli_query($connection, $queryUnhookQuestionnaireFromExperience) or die("Error: ".mysqli_error($connection));
  70. // Delete questionnaire
  71. $queryDeleteQuestionnaire = "DELETE FROM questionnair WHERE id = '$questionnaireID';";
  72. $result = mysqli_query($connection, $queryDeleteQuestionnaire) or die("Error: ".mysqli_error($connection));
  73. }