Nessuna descrizione

duplicateQuestionnaire.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // WARNING: SHOULD VERIFY IF THESE ACTUALLY EXIST
  10. $fromQuestionnaireID = mysqli_real_escape_string($connection, $_POST['fromQuestionnaireID']);
  11. $toExperienceID = mysqli_real_escape_string($connection, $_POST['toExperienceID']);
  12. /*** FETCH THE QUESTIONNAIRE INFO ***/
  13. $queryOldQuestionnaire = "SELECT * FROM questionnair WHERE id = $fromQuestionnaireID";
  14. $resultOldQuestionnaire = mysqli_query($connection, $queryOldQuestionnaire) or die("Error: Couldn't fetch questionnaire info from given ID<br>".mysqli_error($connection));
  15. $rowOldQuestionnaire = mysqli_fetch_assoc($resultOldQuestionnaire);
  16. /*** DUPLICATE QUESTIONNAIRE, RETRIEVE ITS ID (TO USE IT IN TABLES experience_questionnair AND questionnair_question) AND HOOK TO CURRENT EXPERIENCE ***/
  17. $queryQuestionnaire = "INSERT INTO questionnair (`q_title`, `description`) VALUES ('".$rowOldQuestionnaire['q_title']."', '".$rowOldQuestionnaire['description']."');";
  18. if(!mysqli_query($connection, $queryQuestionnaire)) {
  19. echo $queryQuestionnaire . "<br>";
  20. die("Error: Couldn't create questionnaire<br>".mysqli_error($connection));
  21. }
  22. $questionnaireID = mysqli_insert_id($connection) or die("Error: Couldn't fetch questionnaire id after creating it<br>".mysqli_error($connection));
  23. $queryHookQuestionnaireToExperience = "INSERT INTO experience_questionnair (`id_experience`, `id_questionnair`) VALUES ('".$toExperienceID."', '".$questionnaireID."');";
  24. if(!mysqli_query($connection, $queryHookQuestionnaireToExperience)) {
  25. echo $queryHookQuestionnaireToExperience . "<br>";
  26. die("Error: Couldn't hook questionnaire to experience<br>".mysqli_error($connection));
  27. }
  28. // WE DON'T NEED TO INSERT A NEW REFERENCE, WE JUST NEED TO DUPLICATE THE QUESTIONS (WHICH HAVE THE REFERENCE ID ALREADY)
  29. // WE DON'T NEED TO CREATE THE NEW CATEGORIES, WE JUST NEED TO DUPLICATE THE QUESTIONS (WHICH HAVE THE CATEGORY ID ALREADY)
  30. // WE DON'T NEED TO CREATE THE NEW SUBCATEGORIES, WE JUST NEED TO DUPLICATE THE QUESTIONS (WHICH HAVE THE SUBCATEGORY ID ALREADY)
  31. $queryForQuestions = "SELECT id_question FROM questionnair_question WHERE id_questionnair = $fromQuestionnaireID;";
  32. $resultForQuestions = mysqli_query($connection, $queryForQuestions);
  33. /*** 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 ***/
  34. while($rowForQuestions = mysqli_fetch_assoc($resultForQuestions)) {
  35. // FETCH THE INFO FOR A GIVEN QUESTION
  36. $queryOldQuestion = "SELECT * FROM question WHERE id = " . $rowForQuestions['id_question'];
  37. $resultOldQuestion = mysqli_query($connection, $queryOldQuestion);
  38. $rowOldQuestion = mysqli_fetch_assoc($resultOldQuestion);
  39. // DUPLICATE QUESTION
  40. $queryQuestion = "INSERT INTO question (`premise`, `id_category`, `id_subcategory`, `id_type`, `id_referencia`) VALUES ('".mysqli_real_escape_string($connection, $rowOldQuestion['premise'])."', '".$rowOldQuestion['id_category']."', '".$rowOldQuestion['id_subcategory']."', '".$rowOldQuestion['id_type']."', '".$rowOldQuestion['id_referencia']."');";
  41. if(!mysqli_query($connection, $queryQuestion)) {
  42. echo $queryQuestion . "<br>";
  43. die("Error: Couldn't duplicate question with id ".$rowForQuestions['id_question']."<br>".mysqli_error($connection));
  44. }
  45. // FETCH DUPLICATE QUESTION ID
  46. $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));
  47. // IF QUESTION IS SCALED, DUPLICATE MIN/MAX VALUES/TEXT
  48. if($rowOldQuestion['id_type'] == '1') {
  49. // FETCH THE QUESTION TYPES FOR A GIVEN QUESTION
  50. $queryOldQuestionTypes = "SELECT * FROM question_type WHERE id_question = " . $rowForQuestions['id_question'];
  51. $resultOldQuestionTypes = mysqli_query($connection, $queryOldQuestionTypes);
  52. // LOOP THROUGH ALL QUESTION TYPES
  53. while($rowOldQuestionTypes = mysqli_fetch_assoc($resultOldQuestionTypes)) {
  54. $queryMinVal = "INSERT INTO question_type (`id_type`, `id_question`, `label`, `value`) VALUES ('".$rowOldQuestionTypes['id_type']."', '".$questionID."', '".$rowOldQuestionTypes['label']."', '".$rowOldQuestionTypes['value']."');";
  55. if(!mysqli_query($connection, $queryMinVal)) {
  56. echo $queryMinVal . "<br>";
  57. die("Error: Couldn't duplicate question ".$rowOldQuestionTypes['label']." (from old question with id ".$rowForQuestions['id_question'].")<br>".mysqli_error($connection));
  58. }
  59. }
  60. }
  61. $queryHookQuestionToQuestionnaire = "INSERT INTO questionnair_question (`id_questionnair`, `id_question`) VALUES ('".$questionnaireID."', '".$questionID."');";
  62. if(!mysqli_query($connection, $queryHookQuestionToQuestionnaire)) {
  63. echo $queryHookQuestionToQuestionnaire . "<br>";
  64. die("Error: Couldn't hook question to questionnaire (Question ID: ".$questionID.")<br>".mysqli_error($connection));
  65. }
  66. }
  67. header('Location: ../questionnaires.php');
  68. exit();
  69. }
  70. ?>