Nenhuma descrição

duplicateQuestionnaire.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. require_once 'config.php';
  3. require_once 'dbh.inc.php';
  4. require_once 'checkLogin.php';
  5. // EXAMPLE INPUT...
  6. // array(3) { ["fromQuestionnaireID"]=> string(1) "1" ["toExperienceID"]=> string(1) "11" ["duplicateQuestionnaire"]=> string(1) "" }
  7. if(isset($_POST['duplicateQuestionnaire'])) {
  8. /*** FETCH INPUT ***/
  9. $fromQuestionnaireID = mysqli_real_escape_string($connection, trim($_POST['fromQuestionnaireID']));
  10. $toExperienceID = mysqli_real_escape_string($connection, trim($_POST['toExperienceID']));
  11. $projectID = mysqli_real_escape_string($connection, trim($_POST['projectID']));
  12. // Check that questionnaire ID is not empty string
  13. // And that it's registered in the database
  14. if($fromQuestionnaireID === "") {
  15. http_response_code(400);
  16. echo json_encode(array("error" => "Please specify questionnaire ID."));
  17. exit();
  18. } else if(mysqli_query($connection, "SELECT * FROM questionnair WHERE id = '$fromQuestionnaireID';")->num_rows !== 1) {
  19. http_response_code(400);
  20. echo json_encode(array("error" => "Given questionnaire ID ($fromQuestionnaireID) not in database."));
  21. exit();
  22. }
  23. // Check that experience ID is not empty string
  24. // And that it's registered in the database
  25. if($toExperienceID === "") {
  26. http_response_code(400);
  27. echo json_encode(array("error" => "Please specify experience ID."));
  28. exit();
  29. } else if(mysqli_query($connection, "SELECT * FROM experience WHERE id = '$toExperienceID';")->num_rows !== 1) {
  30. http_response_code(400);
  31. echo json_encode(array("error" => "Given experience ID ($toExperienceID) not in database."));
  32. exit();
  33. }
  34. // IF EXPERIENCE BELONGS TO PROJECT, JUST MAKE A "SHALLOW DUPLICATE"
  35. if(mysqli_query($connection, "SELECT * FROM project_experience WHERE id_project = '$projectID' AND id_experience = '$toExperienceID';")->num_rows === 1) {
  36. $queryHookOldQuestionnaireToExperience = "INSERT INTO experience_questionnair (`id_experience`, `id_questionnair`) VALUES ('$toExperienceID', '$fromQuestionnaireID');";
  37. if(!mysqli_query($connection, $queryHookOldQuestionnaireToExperience)) {
  38. echo $queryHookOldQuestionnaireToExperience . "<br>";
  39. die("Error: Couldn't hook questionnaire to experience<br>".mysqli_error($connection));
  40. }
  41. exit();
  42. }
  43. /*** FETCH THE QUESTIONNAIRE INFO ***/
  44. $queryOldQuestionnaire = "SELECT * FROM questionnair WHERE id = '$fromQuestionnaireID';";
  45. $resultOldQuestionnaire = mysqli_query($connection, $queryOldQuestionnaire) or die("Error: Couldn't fetch questionnaire info from given ID<br>".mysqli_error($connection));
  46. $rowOldQuestionnaire = mysqli_fetch_assoc($resultOldQuestionnaire);
  47. /*** DUPLICATE QUESTIONNAIRE, RETRIEVE ITS ID (TO USE IT IN TABLES experience_questionnair AND questionnair_question) AND HOOK TO CURRENT EXPERIENCE ***/
  48. $queryQuestionnaire = "INSERT INTO questionnair (`q_title`, `description`) VALUES ('".$rowOldQuestionnaire['q_title']."', '".$rowOldQuestionnaire['description']."');";
  49. if(!mysqli_query($connection, $queryQuestionnaire)) {
  50. echo $queryQuestionnaire . "<br>";
  51. die("Error: Couldn't create questionnaire<br>".mysqli_error($connection));
  52. }
  53. $questionnaireID = mysqli_insert_id($connection) or die("Error: Couldn't fetch questionnaire id after creating it<br>".mysqli_error($connection));
  54. $queryHookQuestionnaireToExperience = "INSERT INTO experience_questionnair (`id_experience`, `id_questionnair`) VALUES ('$toExperienceID', '$questionnaireID');";
  55. if(!mysqli_query($connection, $queryHookQuestionnaireToExperience)) {
  56. echo $queryHookQuestionnaireToExperience . "<br>";
  57. die("Error: Couldn't hook questionnaire to experience<br>".mysqli_error($connection));
  58. }
  59. /*** DUPLICATE REFERENCES AND RETRIEVE THEIR ID (TO USE IT IN TABLE question) ***/
  60. $queryGetReferences = "SELECT DISTINCT q.id_referencia, r.referencia FROM question q JOIN reference r ON q.id_referencia = r.id WHERE q.id IN (SELECT id_question FROM questionnair_question WHERE id_questionnair = '$fromQuestionnaireID');";
  61. $resultGetReferences = mysqli_query($connection, $queryGetReferences);
  62. $oldToNewReference = array();
  63. while($reference = mysqli_fetch_assoc($resultGetReferences)) {
  64. $queryReference = "INSERT INTO reference (`referencia`) VALUES ('" . $reference['referencia'] . "');";
  65. if(!mysqli_query($connection, $queryReference)) {
  66. http_response_code(400);
  67. die(json_encode(array("error" => "Error: Couldn't create questionnaire reference<br>".mysqli_error($connection)."<br>Query: $queryReference")));
  68. }
  69. $referenceID = mysqli_insert_id($connection) or die(json_encode(array("error" => "Error: Couldn't fetch reference id after creating it<br>".mysqli_error($connection))));
  70. // LINK OLD REFERENCE ID TO NEW REFERENCE ID
  71. $oldToNewReference[$reference['id_referencia']] = $referenceID;
  72. }
  73. /*** DUPLICATE CATEGORIES AND RETRIEVE THEIR ID (TO USE IT IN TABLE question AND subcategory) ***/
  74. $queryGetCategories = "SELECT DISTINCT q.id_category, c.categoria FROM question q JOIN category c ON q.id_category = c.id WHERE q.id IN (SELECT id_question FROM questionnair_question WHERE id_questionnair = '$fromQuestionnaireID');";
  75. $resultGetCategories = mysqli_query($connection, $queryGetCategories);
  76. $oldToNewCategory = array();
  77. while($category = mysqli_fetch_assoc($resultGetCategories)) {
  78. $queryCategory = "INSERT INTO category (`categoria`) VALUES ('" . $category['categoria'] . "');";
  79. if(!mysqli_query($connection, $queryCategory)) {
  80. http_response_code(400);
  81. die(json_encode(array("error" => "Error: Couldn't create questionnaire category<br>".mysqli_error($connection)."<br>Query: $queryCategory")));
  82. }
  83. $categoryID = mysqli_insert_id($connection) or die(json_encode(array("error" => "Error: Couldn't fetch category id after creating it<br>".mysqli_error($connection))));
  84. // LINK OLD CATEGORY ID TO NEW CATEGORY ID
  85. $oldToNewCategory[$category['id_category']] = $categoryID;
  86. }
  87. /*** DUPLICATE SUBCATEGORIES AND RETRIEVE THEIR ID (TO USE IT IN TABLE question) ***/
  88. $queryGetSubcategories = "SELECT DISTINCT q.id_subcategory, s.subcategoria, s.id_categoria FROM question q JOIN subcategory s ON q.id_subcategory = s.id WHERE q.id IN (SELECT id_question FROM questionnair_question WHERE id_questionnair = '$fromQuestionnaireID');";
  89. $resultGetSubcategories = mysqli_query($connection, $queryGetSubcategories);
  90. $oldToNewSubcategory = array();
  91. while($subcategory = mysqli_fetch_assoc($resultGetSubcategories)) {
  92. $querySubcategory = "INSERT INTO subcategory (`subcategoria`, `id_categoria`) VALUES ('" . $subcategory['subcategoria'] . "', '" . $oldToNewCategory[$subcategory['id_categoria']] . "');";
  93. if(!mysqli_query($connection, $querySubcategory)) {
  94. http_response_code(400);
  95. die(json_encode(array("error" => "Error: Couldn't create questionnaire subcategory<br>".mysqli_error($connection)."<br>Query: $querySubcategory. Dump: ".var_export($oldToNewCategory, true))));
  96. }
  97. $subcategoryID = mysqli_insert_id($connection) or die(json_encode(array("error" => "Error: Couldn't fetch subcategory id after creating it<br>".mysqli_error($connection))));
  98. // LINK OLD SUBCATEGORY ID TO NEW SUBCATEGORY ID
  99. $oldToNewSubcategory[$subcategory['id_subcategory']] = $subcategoryID;
  100. }
  101. $queryForQuestions = "SELECT id_question FROM questionnair_question WHERE id_questionnair = '$fromQuestionnaireID';";
  102. $resultForQuestions = mysqli_query($connection, $queryForQuestions);
  103. /*** CREATE QUESTIONS ONE BY ONE, RETRIEVE THEIR RESPECTIVE IDs (TO USE IN TABLES question_type AND questionnair_question), ENTER THE MIN/MAX VALUES/TEXTS, AND HOOK QUESTION TO QUESTIONNAIR ***/
  104. while($rowForQuestions = mysqli_fetch_assoc($resultForQuestions)) {
  105. // FETCH THE INFO FOR A GIVEN QUESTION
  106. $queryOldQuestion = "SELECT * FROM question WHERE id = " . $rowForQuestions['id_question'];
  107. $resultOldQuestion = mysqli_query($connection, $queryOldQuestion);
  108. $rowOldQuestion = mysqli_fetch_assoc($resultOldQuestion);
  109. // DUPLICATE QUESTION
  110. $queryQuestion = "INSERT INTO question (`premise`, `id_category`, `id_subcategory`, `id_type`, `id_referencia`) VALUES ('" . mysqli_real_escape_string($connection, $rowOldQuestion['premise']) . "', '" . $oldToNewCategory[$rowOldQuestion['id_category']] . "', '" . $oldToNewSubcategory[$rowOldQuestion['id_subcategory']] . "', '" . $rowOldQuestion['id_type'] . "', '" . $oldToNewReference[$rowOldQuestion['id_referencia']] . "');";
  111. if(!mysqli_query($connection, $queryQuestion)) {
  112. echo $queryQuestion . "<br>";
  113. die("Error: Couldn't duplicate question with id ".$rowForQuestions['id_question']."<br>".mysqli_error($connection));
  114. }
  115. // FETCH DUPLICATE QUESTION ID
  116. $questionID = mysqli_insert_id($connection) or die("Error: Couldn't fetch new question id (from old question id ".$rowForQuestions['id_question'].")<br>".mysqli_error($connection));
  117. // IF QUESTION IS SCALED, DUPLICATE MIN/MAX VALUES/TEXT
  118. if($rowOldQuestion['id_type'] == '1') {
  119. // FETCH THE QUESTION TYPES FOR A GIVEN QUESTION
  120. $queryOldQuestionTypes = "SELECT * FROM question_type WHERE id_question = " . $rowForQuestions['id_question'];
  121. $resultOldQuestionTypes = mysqli_query($connection, $queryOldQuestionTypes);
  122. // LOOP THROUGH ALL QUESTION TYPES
  123. while($rowOldQuestionTypes = mysqli_fetch_assoc($resultOldQuestionTypes)) {
  124. $queryMinVal = "INSERT INTO question_type (`id_type`, `id_question`, `label`, `value`) VALUES ('".$rowOldQuestionTypes['id_type']."', '$questionID', '".$rowOldQuestionTypes['label']."', '".$rowOldQuestionTypes['value']."');";
  125. if(!mysqli_query($connection, $queryMinVal)) {
  126. echo $queryMinVal . "<br>";
  127. die("Error: Couldn't duplicate question ".$rowOldQuestionTypes['label']." (from old question with id ".$rowForQuestions['id_question'].")<br>".mysqli_error($connection));
  128. }
  129. }
  130. }
  131. $queryHookQuestionToQuestionnaire = "INSERT INTO questionnair_question (`id_questionnair`, `id_question`) VALUES ('$questionnaireID', '$questionID');";
  132. if(!mysqli_query($connection, $queryHookQuestionToQuestionnaire)) {
  133. echo $queryHookQuestionToQuestionnaire . "<br>";
  134. die("Error: Couldn't hook question to questionnaire (Question ID: ".$questionID.")<br>".mysqli_error($connection));
  135. }
  136. }
  137. // header('Location: ../questionnaires.php');
  138. // exit();
  139. }
  140. ?>