Browse Source

Added sharing questionnaire

Victor Hernandez 4 years ago
parent
commit
49f0685332
2 changed files with 117 additions and 17 deletions
  1. 115
    14
      processes/duplicateQuestionnaire.php
  2. 2
    3
      processes/importQuestionnaire.php

+ 115
- 14
processes/duplicateQuestionnaire.php View File

@@ -12,13 +12,57 @@
12 12
 	
13 13
 
14 14
 		/*** FETCH INPUT ***/
15
-		// WARNING: SHOULD VERIFY IF THESE ACTUALLY EXIST
16
-		$fromQuestionnaireID = mysqli_real_escape_string($connection, $_POST['fromQuestionnaireID']);
17
-		$toExperienceID = mysqli_real_escape_string($connection, $_POST['toExperienceID']);
15
+		$fromQuestionnaireID = mysqli_real_escape_string($connection, trim($_POST['fromQuestionnaireID']));
16
+		$toExperienceID = mysqli_real_escape_string($connection, trim($_POST['toExperienceID']));
17
+		$projectID = mysqli_real_escape_string($connection, trim($_POST['projectID']));
18
+		
19
+		
20
+		// Check that questionnaire ID is not empty string
21
+		// And that it's registered in the database
22
+		if($fromQuestionnaireID === "") {
23
+			http_response_code(400);
24
+			echo json_encode(array("error" => "Please specify questionnaire ID."));
25
+			exit();
26
+		} else if(mysqli_query($connection, "SELECT * FROM questionnair WHERE id = '$fromQuestionnaireID';")->num_rows !== 1) {
27
+			http_response_code(400);
28
+			echo json_encode(array("error" => "Given questionnaire ID ($fromQuestionnaireID) not in database."));
29
+			exit();
30
+		}
31
+		
32
+		
33
+		// Check that experience ID is not empty string
34
+		// And that it's registered in the database
35
+		if($toExperienceID === "") {
36
+			http_response_code(400);
37
+			echo json_encode(array("error" => "Please specify experience ID."));
38
+			exit();
39
+		} else if(mysqli_query($connection, "SELECT * FROM experience WHERE id = '$toExperienceID';")->num_rows !== 1) {
40
+			http_response_code(400);
41
+			echo json_encode(array("error" => "Given experience ID ($toExperienceID) not in database."));
42
+			exit();
43
+		}
44
+		
45
+		
46
+		
47
+		
48
+		// IF EXPERIENCE BELONGS TO PROJECT, JUST MAKE A "SHALLOW DUPLICATE"
49
+		if(mysqli_query($connection, "SELECT * FROM project_experience WHERE id_project = '$projectID' AND id_experience = '$toExperienceID';")->num_rows === 1) {
50
+	
51
+			$queryHookOldQuestionnaireToExperience = "INSERT INTO experience_questionnair (`id_experience`, `id_questionnair`) VALUES ('$toExperienceID', '$fromQuestionnaireID');";
52
+			if(!mysqli_query($connection, $queryHookOldQuestionnaireToExperience)) {
53
+				echo $queryHookOldQuestionnaireToExperience . "<br>";
54
+				die("Error: Couldn't hook questionnaire to experience<br>".mysqli_error($connection));
55
+			}
56
+			exit();
57
+			
58
+		}
59
+		
60
+		
61
+		
18 62
 		
19 63
 		
20 64
 		/*** FETCH THE QUESTIONNAIRE INFO ***/
21
-		$queryOldQuestionnaire = "SELECT * FROM questionnair WHERE id = $fromQuestionnaireID";
65
+		$queryOldQuestionnaire = "SELECT * FROM questionnair WHERE id = '$fromQuestionnaireID';";
22 66
 		$resultOldQuestionnaire = mysqli_query($connection, $queryOldQuestionnaire) or die("Error: Couldn't fetch questionnaire info from given ID<br>".mysqli_error($connection));
23 67
 		$rowOldQuestionnaire = mysqli_fetch_assoc($resultOldQuestionnaire);
24 68
 		
@@ -32,7 +76,7 @@
32 76
 
33 77
 		$questionnaireID = mysqli_insert_id($connection) or die("Error: Couldn't fetch questionnaire id after creating it<br>".mysqli_error($connection));
34 78
 
35
-		$queryHookQuestionnaireToExperience = "INSERT INTO experience_questionnair (`id_experience`, `id_questionnair`) VALUES ('".$toExperienceID."', '".$questionnaireID."');";
79
+		$queryHookQuestionnaireToExperience = "INSERT INTO experience_questionnair (`id_experience`, `id_questionnair`) VALUES ('$toExperienceID', '$questionnaireID');";
36 80
 		if(!mysqli_query($connection, $queryHookQuestionnaireToExperience)) {
37 81
 			echo $queryHookQuestionnaireToExperience . "<br>";
38 82
 			die("Error: Couldn't hook questionnaire to experience<br>".mysqli_error($connection));
@@ -40,13 +84,70 @@
40 84
 
41 85
 
42 86
 
43
-// WE DON'T NEED TO INSERT A NEW REFERENCE, WE JUST NEED TO DUPLICATE THE QUESTIONS (WHICH HAVE THE REFERENCE ID ALREADY)
44
-// WE DON'T NEED TO CREATE THE NEW CATEGORIES, WE JUST NEED TO DUPLICATE THE QUESTIONS (WHICH HAVE THE CATEGORY ID ALREADY)
45
-// WE DON'T NEED TO CREATE THE NEW SUBCATEGORIES, WE JUST NEED TO DUPLICATE THE QUESTIONS (WHICH HAVE THE SUBCATEGORY ID ALREADY)
87
+
88
+		/*** DUPLICATE REFERENCES AND RETRIEVE THEIR ID (TO USE IT IN TABLE question) ***/
89
+		$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');";
90
+		$resultGetReferences = mysqli_query($connection, $queryGetReferences);
91
+		$oldToNewReference = array();
92
+		while($reference = mysqli_fetch_assoc($resultGetReferences)) {
93
+		
94
+			$queryReference = "INSERT INTO reference (`referencia`) VALUES ('" . $reference['referencia'] . "');";
95
+			if(!mysqli_query($connection, $queryReference)) {
96
+				http_response_code(400);
97
+				die(json_encode(array("error" => "Error: Couldn't create questionnaire reference<br>".mysqli_error($connection)."<br>Query: $queryReference")));
98
+			}
99
+			
100
+			$referenceID = mysqli_insert_id($connection) or die(json_encode(array("error" => "Error: Couldn't fetch reference id after creating it<br>".mysqli_error($connection))));
101
+			
102
+			// LINK OLD REFERENCE ID TO NEW REFERENCE ID
103
+			$oldToNewReference[$reference['id_referencia']] = $referenceID;
104
+			
105
+		}
106
+
107
+
108
+		/*** DUPLICATE CATEGORIES AND RETRIEVE THEIR ID (TO USE IT IN TABLE question AND subcategory) ***/
109
+		$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');";
110
+		$resultGetCategories = mysqli_query($connection, $queryGetCategories);
111
+		$oldToNewCategory = array();
112
+		while($category = mysqli_fetch_assoc($resultGetCategories)) {
113
+		
114
+			$queryCategory = "INSERT INTO category (`categoria`) VALUES ('" . $category['categoria'] . "');";
115
+			if(!mysqli_query($connection, $queryCategory)) {
116
+				http_response_code(400);
117
+				die(json_encode(array("error" => "Error: Couldn't create questionnaire category<br>".mysqli_error($connection)."<br>Query: $queryCategory")));
118
+			}
119
+			
120
+			$categoryID = mysqli_insert_id($connection) or die(json_encode(array("error" => "Error: Couldn't fetch category id after creating it<br>".mysqli_error($connection))));
121
+			
122
+			// LINK OLD CATEGORY ID TO NEW CATEGORY ID
123
+			$oldToNewCategory[$category['id_category']] = $categoryID;
124
+			
125
+		}
126
+
127
+
128
+		/*** DUPLICATE SUBCATEGORIES AND RETRIEVE THEIR ID (TO USE IT IN TABLE question) ***/
129
+		$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');";
130
+		$resultGetSubcategories = mysqli_query($connection, $queryGetSubcategories);
131
+		$oldToNewSubcategory = array();
132
+		while($subcategory = mysqli_fetch_assoc($resultGetSubcategories)) {
133
+		
134
+			$querySubcategory = "INSERT INTO subcategory (`subcategoria`, `id_categoria`) VALUES ('" . $subcategory['subcategoria'] . "', '" . $oldToNewCategory[$subcategory['id_categoria']] . "');";
135
+			if(!mysqli_query($connection, $querySubcategory)) {
136
+				http_response_code(400);
137
+				die(json_encode(array("error" => "Error: Couldn't create questionnaire subcategory<br>".mysqli_error($connection)."<br>Query: $querySubcategory. Dump: ".var_export($oldToNewCategory, true))));
138
+			}
139
+			
140
+			$subcategoryID = mysqli_insert_id($connection) or die(json_encode(array("error" => "Error: Couldn't fetch subcategory id after creating it<br>".mysqli_error($connection))));
141
+			
142
+			// LINK OLD SUBCATEGORY ID TO NEW SUBCATEGORY ID
143
+			$oldToNewSubcategory[$subcategory['id_subcategory']] = $subcategoryID;
144
+			
145
+		}
146
+
46 147
 
47 148
 
48 149
 		
49
-		$queryForQuestions = "SELECT id_question FROM questionnair_question WHERE id_questionnair = $fromQuestionnaireID;";
150
+		$queryForQuestions = "SELECT id_question FROM questionnair_question WHERE id_questionnair = '$fromQuestionnaireID';";
50 151
 		$resultForQuestions = mysqli_query($connection, $queryForQuestions);
51 152
 		
52 153
 		/*** 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 ***/
@@ -60,7 +161,7 @@
60 161
 			
61 162
 			
62 163
 			// DUPLICATE QUESTION
63
-			$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']."');";
164
+			$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']] . "');";
64 165
 			if(!mysqli_query($connection, $queryQuestion)) {
65 166
 				echo $queryQuestion . "<br>";
66 167
 				die("Error: Couldn't duplicate question with id ".$rowForQuestions['id_question']."<br>".mysqli_error($connection));
@@ -82,7 +183,7 @@
82 183
 				// LOOP THROUGH ALL QUESTION TYPES 
83 184
 				while($rowOldQuestionTypes = mysqli_fetch_assoc($resultOldQuestionTypes)) {
84 185
 					
85
-					$queryMinVal = "INSERT INTO question_type (`id_type`, `id_question`, `label`, `value`) VALUES ('".$rowOldQuestionTypes['id_type']."', '".$questionID."', '".$rowOldQuestionTypes['label']."', '".$rowOldQuestionTypes['value']."');";
186
+					$queryMinVal = "INSERT INTO question_type (`id_type`, `id_question`, `label`, `value`) VALUES ('".$rowOldQuestionTypes['id_type']."', '$questionID', '".$rowOldQuestionTypes['label']."', '".$rowOldQuestionTypes['value']."');";
86 187
 					if(!mysqli_query($connection, $queryMinVal)) {
87 188
 						echo $queryMinVal . "<br>";
88 189
 						die("Error: Couldn't duplicate question ".$rowOldQuestionTypes['label']." (from old question with id ".$rowForQuestions['id_question'].")<br>".mysqli_error($connection));
@@ -93,7 +194,7 @@
93 194
 				
94 195
 			}
95 196
 			
96
-			$queryHookQuestionToQuestionnaire = "INSERT INTO questionnair_question (`id_questionnair`, `id_question`) VALUES ('".$questionnaireID."', '".$questionID."');";
197
+			$queryHookQuestionToQuestionnaire = "INSERT INTO questionnair_question (`id_questionnair`, `id_question`) VALUES ('$questionnaireID', '$questionID');";
97 198
 			if(!mysqli_query($connection, $queryHookQuestionToQuestionnaire)) {
98 199
 				echo $queryHookQuestionToQuestionnaire . "<br>";
99 200
 				die("Error: Couldn't hook question to questionnaire  (Question ID: ".$questionID.")<br>".mysqli_error($connection));
@@ -106,8 +207,8 @@
106 207
 
107 208
 		
108 209
 		
109
-		header('Location: ../questionnaires.php');
110
-		exit();
210
+// 		header('Location: ../questionnaires.php');
211
+// 		exit();
111 212
 
112 213
 
113 214
 

+ 2
- 3
processes/importQuestionnaire.php View File

@@ -124,9 +124,8 @@ if (($handle = fopen($filename, "r")) !== FALSE)
124 124
 // $id_cat_db=2345;
125 125
 foreach($categoria as &$cat)
126 126
 {
127
-	$sql=sprintf("insert into category (categoria, id_questionnair) values (%s,%s)",
128
-	GetSQLValueString($cat["catText"],"text"),
129
-	GetSQLValueString($id_cuestionario,"int")
127
+	$sql=sprintf("insert into category (categoria) values (%s)",
128
+	GetSQLValueString($cat["catText"],"text")
130 129
 	);
131 130
 // 	print($sql);print "\n";
132 131
 	mysqli_query($connection, $sql);