Browse Source

Added Please ask your administrator. logic during the creation of .

Victor Hernandez 3 years ago
parent
commit
b68257fbee
1 changed files with 96 additions and 9 deletions
  1. 96
    9
      processes/insertExperience.php

+ 96
- 9
processes/insertExperience.php View File

@@ -17,12 +17,20 @@
17 17
 		$title = mysqli_real_escape_string($connection, trim($_POST['title']));
18 18
 		$description = mysqli_real_escape_string($connection, trim($_POST['description']));
19 19
 		$type = mysqli_real_escape_string($connection, trim($_POST['type']));
20
-		$duration_weeks = mysqli_real_escape_string($connection, trim($_POST['duration']));
20
+// 		$duration_weeks = mysqli_real_escape_string($connection, trim($_POST['duration']));
21 21
 		$start_date = mysqli_real_escape_string($connection, trim($_POST['start']));
22 22
 		$end_date = mysqli_real_escape_string($connection, trim($_POST['end']));
23 23
 		$institution = mysqli_real_escape_string($connection, trim($_POST['institution']));
24 24
 		$expiry_time = mysqli_real_escape_string($connection, trim($_POST['expiry_time']));
25 25
 		
26
+		// (PROJECT INPUT)
27
+		$typeOfExperience = mysqli_real_escape_string($connection, trim($_POST['typeOfExperience']));
28
+		$typeOfProject = mysqli_real_escape_string($connection, trim($_POST['typeOfProject']));
29
+		$projectID = mysqli_real_escape_string($connection, trim($_POST['projectID']));
30
+		$projectName = mysqli_real_escape_string($connection, trim($_POST['projectName']));
31
+		$projectDescription = mysqli_real_escape_string($connection, trim($_POST['projectDescription']));
32
+		
33
+			
26 34
 		
27 35
 		// Check that experienceTitle is not an empty string
28 36
 		// And that it doesn't exceed 60 characters (database limit)
@@ -51,11 +59,14 @@
51 59
 		
52 60
 
53 61
 		// Check that experienceType is Course-Based Research or Independent Research
54
-		if($type !== "Course-Based Research" && $type !== "Independent Research") {
55
-			http_response_code(400);
56
-			echo json_encode(array("error" => "Please specify valid experience type (either 'Course-Based Research' or 'Independent Research')."));
57
-			exit();
58
-		} else if(mb_strlen($type) > 60) {
62
+		// EDIT: THE TERMINOLOGY ITSELF MIGHT CHANGE OVER TIME, DON'T CHECK AGAINST HARD-CODED VALUES
63
+// 		if($type !== "Course-Based Research" && $type !== "Independent Research") {
64
+// 			http_response_code(400);
65
+// 			echo json_encode(array("error" => "Please specify valid experience type (either 'Course-Based Research' or 'Independent Research')."));
66
+// 			exit();
67
+// 		} else
68
+		
69
+		if(mb_strlen($type) > 60) {
59 70
 			http_response_code(400);
60 71
 			echo json_encode(array("error" => "Experience type too long (max. is 60 characters)."));
61 72
 			exit();
@@ -139,15 +150,62 @@
139 150
 		
140 151
 		
141 152
 		
153
+		// Check if type of experience is valid
154
+		if($typeOfExperience !== "Standalone" && $typeOfExperience !== "Part of a Project") {
155
+			http_response_code(400);
156
+			echo json_encode(array("error" => "Invalid type of experience."));
157
+			exit();
158
+		}
159
+		
160
+		// If experience is part of a project, decide if it's an existing project or a new project
161
+		if($typeOfExperience === "Part of a Project") {
162
+
163
+			// If existing project, check validity of project ID
164
+			// If new project, check validity of project name and description
165
+			if($typeOfProject === "Existing") {
166
+				
167
+				if(mysqli_query($connection, "SELECT * FROM project WHERE id = '$projectID';")->num_rows !== 1) {
168
+					http_response_code(400);
169
+					echo json_encode(array("error" => "Given project ID ($projectID) not in database."));
170
+					exit();
171
+				}
172
+				
173
+			} else if($typeOfProject === "New") {
174
+				
175
+				if($projectName === "") {
176
+					http_response_code(400);
177
+					echo json_encode(array("error" => "Please specify project name."));
178
+					exit();
179
+				} else if(mb_strlen($projectName) > 256) {
180
+					http_response_code(400);
181
+					echo json_encode(array("error" => "Project name too long (max. is 256 characters)."));
182
+					exit();				
183
+				}
184
+
185
+				if($projectDescription === "") {
186
+					http_response_code(400);
187
+					echo json_encode(array("error" => "Please specify project description."));
188
+					exit();
189
+				} else if(mb_strlen($projectDescription) > 256) {
190
+					http_response_code(400);
191
+					echo json_encode(array("error" => "Project description too long (max. is 256 characters)."));
192
+					exit();				
193
+				}
194
+				
195
+			}
196
+			
197
+		}
198
+		
199
+		
200
+		
142 201
 		/*** START OF DB QUERIES ***/
143 202
 // 		exit();
144 203
 // 		while(TRUE) {
145 204
 // 			exit();
146 205
 // 		}
147
-// 		exit();	
206
+// 		exit();
148 207
 		/*** IF IT GOT THROUGH VALIDATION, IT'S TOO LATE ***/
149
-		
150
-		
208
+	
151 209
 		
152 210
 		
153 211
 		
@@ -175,6 +233,35 @@
175 233
 		$queryHookExperienceToUser = "INSERT INTO `researcher_experience` (`id_researcher`, `id_experience`) VALUES ('" . $_SESSION['dbUserData']['id_researcher'] . "', '$id_experience')";
176 234
 		if(!mysqli_query($connection, $queryHookExperienceToUser)) die("Error: ".mysqli_error($connection));
177 235
 		
236
+		
237
+		
238
+		
239
+		
240
+		
241
+		
242
+		
243
+		// CREAR PROJECT
244
+		if($typeOfExperience === "Part of a Project") {
245
+
246
+			// If existing project, just hook experience to project
247
+			// If new project, create project and then hook
248
+			if($typeOfProject === "New") {
249
+				
250
+				$queryProject = "INSERT INTO project (`name`, `description`) VALUES ('$projectName', '$projectDescription');";
251
+				if(!mysqli_query($connection, $queryProject)) die("Error: ".mysqli_error($connection));
252
+				
253
+				// BUSCAR id DEL PROYECTO ACABADO DE INSERTAR
254
+				$projectID = mysqli_insert_id($connection) or die('Error: '.mysqli_error($connection));
255
+				
256
+			}
257
+			
258
+			$queryHookExperienceToProject = "INSERT INTO project_experience (`id_experience`, `id_project`) VALUES ('$id_experience', '$projectID');";
259
+			if(!mysqli_query($connection, $queryHookExperienceToProject)) die("Error: ".mysqli_error($connection));
260
+			
261
+		}
262
+		
263
+		
264
+		
178 265
 		// MAKE IT CLIENT SIDE!!!!!!!! HAVE TO SEND IT IN SERVER RESPONSE!!!!!!!
179 266
 // 		header('Location: ../viewExperience.php?view=$id_experience');
180 267
 // 		exit();