123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
-
- require_once 'config.php';
- require_once 'dbh.inc.php';
- require_once 'checkLogin.php';
-
-
- // EXAMPLE INPUT...
- // array(2) { ["questionnaireID"]=> string(3) "137" ["deleteQuestionnaire"]=> string(0) "" }
-
- // WARNING: SHOULD CONFIRM THAT QUESTIONNAIRE BELONGS TO THE CORRECT USER (AUTHENTICATE THE TRANSACTION)
-
-
- if(isset($_POST['deleteQuestionnaire'])) {
-
-
-
- $questionnaireID = mysqli_real_escape_string($connection, trim($_POST['questionnaireID']));
-
-
- // Check that questionnaire ID is not empty string
- if($questionnaireID === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Please specify questionnaire ID."));
- exit();
- }
-
-
- // Check that questionnaire is registered in the database
- if(mysqli_query($connection, "SELECT * FROM questionnair WHERE id = '$questionnaireID';")->num_rows !== 1) {
- http_response_code(400);
- echo json_encode(array("error" => "Given questionnaire ID ($questionnaireID) not in database."));
- exit();
- }
-
-
- // Check that no corresponding moments have been answered yet
- if(mysqli_query($connection, "SELECT * FROM student_subquestionnair WHERE id_subquestionnair IN (SELECT id FROM subquestionnair WHERE id_questionnair = '$questionnaireID');")->num_rows !== 0) {
- http_response_code(400);
- echo json_encode(array("error" => "Questionnaire already active, deletion denied."));
- exit();
- }
-
-
-
- /*** STEPS ***/
-
- // DELETE CATEGORIES (USE QUESTIONNAIRE-QUESTION, THEN QUESTION TABLE TO GET TO CATEGORY TABLE)*
- // DELETE SUBCATEGORIES (USE QUESTIONNAIRE-QUESTION, THEN QUESTION TABLE TO GET TO SUBCATEGORY TABLE)*
- // DELETE REFERENCE (USE QUESTIONNAIRE-QUESTION, THEN QUESTION TABLE TO GET TO REFERENCE TABLE)*
- // DELETE QUESTION-TYPE (USE QUESTIONNAIRE-QUESTION TABLE)*
- // DELETE QUESTIONS (USE QUESTIONNAIRE-QUESTION TABLE)*
-
- // DELETE MOMENT-QUESTION RELATIONS (USE MOMENT TABLE TO GET TO MOMENT-QUESTION TABLE)*
- // DELETE EXPERIENCE-MOMENT RELATIONS (USE MOMENT TABLE TO GET TO EXPERIENCE-MOMENT TABLE)*
- // DELETE MOMENTS
-
- // DELETE QUESTIONNAIRE-QUESTION RELATIONS
- // DELETE EXPERIENCE-QUESTIONNAIRE RELATION
- // DELETE QUESTIONNAIRE
-
-
-
- // Delete categories
- $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'));";
- $result = mysqli_query($connection, $queryDeleteCategories) or die("Error: ".mysqli_error($connection));
-
- // Delete subcategories
- $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'));";
- $result = mysqli_query($connection, $queryDeleteSubcategories) or die("Error: ".mysqli_error($connection));
-
- // Delete reference
- $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'));";
- $result = mysqli_query($connection, $queryDeleteReference) or die("Error: ".mysqli_error($connection));
-
- // Delete question types
- $queryDeleteQuestionTypes = "DELETE FROM question_type WHERE id_question IN (SELECT id_question FROM questionnair_question WHERE id_questionnair = '$questionnaireID');";
- $result = mysqli_query($connection, $queryDeleteQuestionTypes) or die("Error: ".mysqli_error($connection));
-
- // Delete questions
- $queryDeleteQuestions = "DELETE FROM question WHERE id IN (SELECT id_question FROM questionnair_question WHERE id_questionnair = '$questionnaireID');";
- $result = mysqli_query($connection, $queryDeleteQuestions) or die("Error: ".mysqli_error($connection));
-
- // Delete moment-question relations
- $queryUnhookQuestionFromMoment = "DELETE FROM subquestionnair_question WHERE id_subquestionnair IN (SELECT id FROM subquestionnair WHERE id_questionnair = '$questionnaireID');";
- $result = mysqli_query($connection, $queryUnhookQuestionFromMoment) or die("Error: ".mysqli_error($connection));
-
- // Delete experience-moment relations
- $queryUnhookMomentFromExperience = "DELETE FROM experience_subquestionnair WHERE id_subquestionnair IN (SELECT id FROM subquestionnair WHERE id_questionnair = '$questionnaireID');";
- $result = mysqli_query($connection, $queryUnhookMomentFromExperience) or die("Error: ".mysqli_error($connection));
-
- // Delete moments
- $queryDeleteMoments = "DELETE FROM subquestionnair WHERE id_questionnair = '$questionnaireID';";
- $result = mysqli_query($connection, $queryDeleteMoments) or die("Error: ".mysqli_error($connection));
-
- // Delete questionnaire-question relations
- $queryUnhookQuestionFromQuestionnaire = "DELETE FROM questionnair_question WHERE id_questionnair = '$questionnaireID';";
- $result = mysqli_query($connection, $queryUnhookQuestionFromQuestionnaire) or die("Error: ".mysqli_error($connection));
-
- // Delete experience-questionnaire relations
- $queryUnhookQuestionnaireFromExperience = "DELETE FROM experience_questionnair WHERE id_questionnair = '$questionnaireID';";
- $result = mysqli_query($connection, $queryUnhookQuestionnaireFromExperience) or die("Error: ".mysqli_error($connection));
-
- // Delete questionnaire
- $queryDeleteQuestionnaire = "DELETE FROM questionnair WHERE id = '$questionnaireID';";
- $result = mysqli_query($connection, $queryDeleteQuestionnaire) or die("Error: ".mysqli_error($connection));
-
-
-
- }
|