Browse Source

Allow user to change type and end date for a given experience.

Victor Hernandez 4 years ago
parent
commit
bef41ccb07
2 changed files with 154 additions and 75 deletions
  1. 122
    61
      processes/updateExperience.php
  2. 32
    14
      viewExperience.php

+ 122
- 61
processes/updateExperience.php View File

@@ -10,7 +10,15 @@
10 10
 		
11 11
 
12 12
 		$id = mysqli_real_escape_string($connection, trim($_POST['id']));
13
+		$newTitle = mysqli_real_escape_string($connection, trim($_POST['newTitle']));
14
+		$newDescription = mysqli_real_escape_string($connection, trim($_POST['newDescription']));
15
+		$newType = mysqli_real_escape_string($connection, trim($_POST['newType']));
16
+		$newStart = mysqli_real_escape_string($connection, trim($_POST['newStart']));
17
+		$newEnd = mysqli_real_escape_string($connection, trim($_POST['newEnd']));
13 18
 		
19
+		
20
+		
21
+		// INSPECT EXPERIENCE ID
14 22
 		// Check that experience ID is not empty string
15 23
 		// And that it's registered in the database
16 24
 		if($id === "") {
@@ -22,84 +30,137 @@
22 30
 			echo json_encode(array("error" => "Given experience ID ($id) not in database."));
23 31
 			exit();
24 32
 		}
33
+		
34
+		
35
+		
36
+		// INSPECT TITLE
37
+		// Check that experience title is not empty
38
+		// And that it's less than 60 characters in length (database limit)
39
+		if($newTitle === "") {
40
+			http_response_code(400);
41
+			echo json_encode(array("error" => "Please specify title."));
42
+			exit();
43
+		} else if(mb_strlen($newTitle) > 60) {
44
+			http_response_code(400);
45
+			echo json_encode(array("error" => "Title too long (max. is 60 characters)."));
46
+			exit();
47
+		}
48
+            
25 49
 
26
-				
27
-		// UPDATE TITLE
28
-		if(isset($_POST['newTitle'])) {
29 50
 		
30
-			$newTitle = mysqli_real_escape_string($connection, trim($_POST['newTitle']));
31
-			
32
-			// Check that experience title is not empty
33
-			// And that it's less than 60 characters in length (database limit)
34
-			if($newTitle === "") {
35
-				http_response_code(400);
36
-				echo json_encode(array("error" => "Please specify title."));
37
-				exit();
38
-			} else if(mb_strlen($newTitle) > 60) {
39
-				http_response_code(400);
40
-				echo json_encode(array("error" => "Title too long (max. is 60 characters)."));
41
-				exit();
42
-			}
51
+		// INSPECT DESCRIPTION
52
+		// Check that experience title is not empty
53
+		// And that it's less than 60 characters in length (database limit)
54
+		if($newDescription === "") {
55
+			http_response_code(400);
56
+			echo json_encode(array("error" => "Please specify description."));
57
+			exit();
58
+		} else if(mb_strlen($newDescription) > 100) {
59
+			http_response_code(400);
60
+			echo json_encode(array("error" => "Description too long (max. is 100 characters)."));
61
+			exit();
62
+		}
43 63
 			
44
-			$query = "UPDATE `experience` SET `title` = '$newTitle' WHERE `id` = '$id';";
45
-			$result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
64
+
46 65
             
66
+
67
+		
68
+		// INSPECT TYPE
69
+		// Check that experience type is not empty
70
+		// And that it's either Test, CBRE or URE
71
+		if($newType === "") {
72
+			http_response_code(400);
73
+			echo json_encode(array("error" => "Please specify type."));
74
+			exit();
75
+		} else if($newType !== 'Course-Based Research Experience' AND $newType !== 'Undergraduate Research Experience' AND $newType !== 'Test') {
76
+			http_response_code(400);
77
+			echo json_encode(array("error" => "Invalid type ($newType)."));
78
+			exit();
47 79
 		}
48 80
 		
49
-		// UPDATE DESCRIPTION
50
-		if(isset($_POST['newDescription'])) {
51 81
 		
52
-			$newDescription = mysqli_real_escape_string($connection, trim($_POST['newDescription']));
53
-			
54
-			// Check that experience title is not empty
55
-			// And that it's less than 60 characters in length (database limit)
56
-			if($newDescription === "") {
57
-				http_response_code(400);
58
-				echo json_encode(array("error" => "Please specify description."));
59
-				exit();
60
-			} else if(mb_strlen($newDescription) > 100) {
61
-				http_response_code(400);
62
-				echo json_encode(array("error" => "Description too long (max. is 100 characters)."));
63
-				exit();
64
-			}
65
-			
66
-			$query = "UPDATE `experience` SET `description` = '$newDescription' WHERE `id` = '$id';";
67
-			$result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
68
-            
69
-		}
70 82
 		
71 83
 		
72 84
 		
73 85
 		
74
-		/*** REMEMBER TO VALIDATE INPUT IF THE CODE BELOW IS TO BE USED ***/
86
+		function validDate($date) {
87
+			$d = date_create_from_format("Y-m-d", $date);
88
+			return $d && date_format($d, "Y-m-d") === $date;
89
+		}
75 90
 		
76
-		// UPDATE TYPE
77
-// 		if(isset($_POST['newType']) AND $_POST['newType'] != " ") {
78
-// 			$newType = mysqli_real_escape_string($connection, trim($_POST['newType']));
79
-// 			$query = "UPDATE `experience` SET `type` = '$newType' WHERE `id` = '$id';";
80
-// 			$result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection)); 
81
-// 		}
82 91
 		
83
-		// UPDATE DURATION
84
-// 		if(isset($_POST['newDuration'])) {
85
-// 			$newDuration = mysqli_real_escape_string($connection, trim($_POST['newDuration']));
86
-// 			$query = "UPDATE `experience` SET `duration_weeks` = '$newDuration' WHERE `id` = '$id';";
87
-// 			$result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));  
92
+		
93
+		
94
+		// INSPECT START DATE
95
+		// Check that startDate is not an empty string
96
+		// And that startDate is in appropriate format YYYY-MM-DD (e.g. 2222-02-22)
97
+		// WARNING: only handling AST
98
+		// HELP: https://www.codexworld.com/how-to/validate-date-input-string-in-php/
99
+// 		if($newStart === "") {
100
+// 			http_response_code(400);
101
+// 			echo json_encode(array("error" => "Please specify experience's start date."));
102
+// 			exit();
103
+// 		} else if(!validDate($newStart)) {
104
+// 			http_response_code(400);
105
+// 			echo json_encode(array("error" => "Experience's start date ($newStart) given in wrong format (use YYYY-MM-DD instead)."));
106
+// 			exit();
88 107
 // 		}
108
+
109
+
110
+		// INSPECT END DATE
111
+		// Check that endDate is not an empty string
112
+		// And that endDate is in appropriate format YYYY-MM-DD (e.g. 2222-02-22)
113
+		// WARNING: only handling AST
114
+		// HELP: https://www.codexworld.com/how-to/validate-date-input-string-in-php/
115
+		if($newEnd === "") {
116
+			http_response_code(400);
117
+			echo json_encode(array("error" => "Please specify experience's end date."));
118
+			exit();
119
+		} else if(!validDate($newEnd)) {
120
+			http_response_code(400);
121
+			echo json_encode(array("error" => "Experience's end date ($newEnd) given in wrong format (use YYYY-MM-DD instead)."));
122
+			exit();
123
+		}
124
+		
125
+		
126
+		// Calculate duration in seconds
127
+		$duration_seconds = strtotime($newEnd) - strtotime($newStart);
128
+		
129
+		
130
+		// Check that endDate occurs after the startDate
131
+		if($duration_seconds <= 0) {
132
+			http_response_code(400);
133
+			echo json_encode(array("error" => "Experience's end date ($newEnd) must occur at least a day after the start date ($newStart)."));
134
+			exit();
135
+		}
136
+		
137
+		
138
+		// Change seconds to weeks and round up
139
+		$duration_weeks = round($duration_seconds / 604800); // 60 * 60 * 24 * 7
140
+		
141
+		
142
+		
143
+		// UPDATE TITLE, DESCRIPTION, TYPE, END DATE & DURATION
144
+		$newDuration = mysqli_real_escape_string($connection, trim($duration_weeks));
145
+		$query = "UPDATE `experience`
146
+				  SET `title` = '$newTitle',
147
+				  	  `description` = '$newDescription',
148
+				  	  `type` = '$newType',
149
+				  	  `end_date` = '$newEnd',
150
+				  	  `duration_weeks` = '$newDuration'
151
+				  WHERE `id` = '$id';";
152
+		$result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
153
+		
154
+		
155
+		
156
+		
89 157
 		
90 158
 		// UPDATE START DATE
91
-// 		if(isset($_POST['newStart'])) {
92
-// 			$newStart = mysqli_real_escape_string($connection, trim($_POST['newStart']));
93
-// 			$query = "UPDATE `experience` SET `start_date` = '$newStart' WHERE `id` = '$id';";
94
-// 			$result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
95
-// 		}
159
+// 		$query = "UPDATE `experience` SET `start_date` = '$newStart' WHERE `id` = '$id';";
160
+// 		$result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
161
+
162
+		
96 163
 		
97
-		// UPDATE END DATE
98
-// 		if(isset($_POST['newEnd'])) {
99
-// 			$newEnd = mysqli_real_escape_string($connection, trim($_POST['newEnd']));
100
-// 			$query = "UPDATE `experience` SET `end_date` = '$newEnd' WHERE `id` = '$id';";
101
-// 			$result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
102
-// 		}
103 164
 		
104 165
 		
105 166
 		

+ 32
- 14
viewExperience.php View File

@@ -469,7 +469,7 @@
469 469
 		<!-- POPUP FOR EDIT EXPERIENCE -->
470 470
 <form id="editForm"><!--method='POST' action='processes/updateExperience.php'-->
471 471
 		<div class="modal fade" id="Edit" tabindex="-1" role="dialog" aria-labelledby="EditLabel" aria-hidden="true">
472
-			<div class="modal-dialog" role="document">
472
+			<div class="modal-dialog modal-lg" role="document">
473 473
  				<div class="modal-content">
474 474
  				
475 475
  					<div class="modal-body">
@@ -508,15 +508,21 @@
508 508
 								</div>
509 509
 							</div>
510 510
 							
511
-<!--
512
-							<!~~ NEW TYPE ~~>
513
-							<div class='form-group'>
514
-								<label class='control-label col-sm-2 col-sm-offset-1'>Type:</label>
515
-								<div class='col-sm-7'>
516
-									<input type='text' class='form-control text-center' name='newType' required value="<?php echo $row1['type']; ?>">
511
+							<!-- NEW TYPE -->
512
+							<div class="form-group">
513
+								<label class="control-label col-sm-2 col-sm-offset-1">Type:</label>
514
+								<div class="col-sm-7">
515
+									<select class="form-control text-center" style="text-align-last: center;" name="newType" required>
516
+										<option <?php echo ($row1['type'] === 'Course-Based Research Experience' ? 'selected' : ''); ?> value="Course-Based Research Experience">Course-Based Research Experience</option>
517
+										<option <?php echo ($row1['type'] === 'Undergraduate Research Experience' ? 'selected' : ''); ?> value="Undergraduate Research Experience">Undergraduate Research Experience</option>
518
+										<?php if($_SESSION['dbUserData']['admin'] === '1'): ?>
519
+											<option selected value="Test">Test</option>
520
+										<?php endif; ?>
521
+									</select>
517 522
 								</div>
518 523
 							</div>
519 524
 
525
+<!-- 
520 526
 							<!~~ NEW DURATION ~~>
521 527
 							<div class='form-group'>
522 528
 								<label class='control-label col-sm-2 col-sm-offset-1'>Duration:</label>
@@ -524,23 +530,27 @@
524 530
 									<input type='number' class='form-control text-center' name='newDuration' required value="<?php echo $row1['duration_weeks']; ?>" readonly>
525 531
 								</div>
526 532
 							</div>
527
-				
528
-							<!~~ NEW START DATE ~~>
533
+ -->
534
+
535
+
536
+							<!-- NEW START DATE -->
529 537
 							<div class='form-group'>
530 538
 								<label class='control-label col-sm-2 col-sm-offset-1'>Start Date:</label>
531 539
 								<div class='col-sm-7'>
532
-									<input type='date' class='form-control text-center' name='newStart' required value="<?php echo $row1['start_date']; ?>">
540
+									<input disabled type='date' class='form-control text-center' name='newStart' required value="<?php echo $row1['start_date']; ?>">
533 541
 								</div>
534 542
 							</div>
535 543
 
536
-							<!~~ NEW END DATE ~~>
544
+							<!-- NEW END DATE -->
537 545
 							<div class='form-group'>
538 546
 								<label class='control-label col-sm-2 col-sm-offset-1'>End Date:</label>
539 547
 								<div class='col-sm-7'>
540 548
 									<input type='date' class='form-control text-center' name='newEnd' required value="<?php echo $row1['end_date']; ?>">
541 549
 								</div>
542 550
 							</div>
543
--->
551
+
552
+
553
+
544 554
 
545 555
 						</div><!--form-horizontal-->
546 556
 
@@ -562,7 +572,7 @@
562 572
       				<!-- SUBMIT OR CANCEL -->
563 573
      				<div class="modal-footer">
564 574
        					<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
565
-        				<button type="submit" class="btn btn-primary" name="updateExperience" disabled>Save Changes</button>
575
+        				<button type="submit" class="btn btn-primary" name="updateExperience">Save Changes</button>
566 576
       				</div>
567 577
       				
568 578
     			</div><!--modal-content-->
@@ -590,7 +600,7 @@
590 600
 		// 2) and both strings (title and description) are not empty
591 601
 		function validateEditExperience() {
592 602
 			let updateExperience = document.querySelector('[name=updateExperience]');
593
-			updateExperience.disabled = !newTitle.checkValidity() || !newDescription.checkValidity();
603
+// 			updateExperience.disabled = !newTitle.checkValidity() || !newDescription.checkValidity();
594 604
 		}
595 605
 		
596 606
 		</script>
@@ -1523,6 +1533,14 @@ var calendar = new FullCalendar.Calendar(calendarElement, {
1523 1533
 			alert('An error occurred! Resetting calendar...');
1524 1534
 			eventDropInfo.revert();
1525 1535
 		});
1536
+	},
1537
+	eventClick: function(info) {
1538
+		const eventObj = info.event;
1539
+		
1540
+		// If clicked event is a milstone, change view to "milestones" tab
1541
+		if(eventObj.id.includes('milestone')) {
1542
+			document.location.hash = '#milestones';
1543
+		}
1526 1544
 	}
1527 1545
 });
1528 1546
 calendar.render();