-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-update.php
More file actions
544 lines (408 loc) · 20.1 KB
/
user-update.php
File metadata and controls
544 lines (408 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
<?php
ob_start();
//initialize modules on start
require 'php/auth-mods/auth-login.php';
require ("php/mod_conn.php");
//get request values
$id = mysqli_real_escape_string($conn, $_GET['id']);
$requestType = mysqli_real_escape_string($conn, $_GET['request']);
$current_loggedUser = $_SESSION["user_ID"];
// user-profile redirect extra references
$user_type;
//personal credentials
$q_fname;
$q_mname;
$q_lname;
$q_birthdate;
$q_address;
//staff credentials
$q_position;
$q_organization;
//student credentials
$q_students;
$q_student_class;
//user credentials
$q_username;
$q_password;
$q_account_level;
//query variables for values
$fetch_query;
$fetch_result;
//query for values
if($requestType == "staffupdate"){
$user_type = "staff";
//get staff data
$fetch_query = mysqli_query($conn, "
SELECT * FROM `staffs` WHERE `staff_id` = '$id'
");
while($fetch_result = mysqli_fetch_assoc($fetch_query)){
//assign values
//personal credentials
$q_fname = $fetch_result['staff_fname'];
$q_mname = $fetch_result['staff_mname'];
$q_lname = $fetch_result['staff_lname'];
$q_birthdate = $fetch_result['staff_birthdate'];
$q_address = $fetch_result['staff_address'];
//staff credentials
$q_position = $fetch_result['staff_position'];
$q_organization = $fetch_result['staff_organization'];
//user credentials
$q_username = $fetch_result['staff_username'];
$q_password = $fetch_result['staff_password'];
$q_account_level = $fetch_result['staff_accountLevel'];
}
}else if ($requestType == "studentupdate"){
$user_type = "student";
//get student data
$fetch_query = mysqli_query($conn, "
SELECT * FROM `students` WHERE `student_id` = '$id'
");
while($fetch_result = mysqli_fetch_assoc($fetch_query)){
//assign values
//personal credentials
$q_fname = $fetch_result['student_fname'];
$q_mname = $fetch_result['student_mname'];
$q_lname = $fetch_result['student_lname'];
$q_birthdate = $fetch_result['student_birthdate'];
$q_address = $fetch_result['student_address'];
//user credentials
$q_student_class = $fetch_result['student_classID'];
$q_username = $fetch_result['student_username'];
$q_password = $fetch_result['student_password'];
$q_account_level = $fetch_result['student_accountLevel'];
}
}else if($requestType == "classupdate"){
//class query here
}
?>
<html>
<header>
<?php
//evaluate title
echo "<title>";
if($requestType == "staffupdate" || $requestType == "studentupdate"){
//staff & student update title
echo "User Update";
}else if($requestType == "classupdate"){
//class update title
echo "Class Update";
}else{
die("Invalid request type to access update module!");
}
echo "</title>";
?>
<!--Module HTML references to work properly-->
<link href="css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="css/global-style.css" rel="stylesheet" type="text/css"/>
<link href="css/register.css" rel="stylesheet" type="text/css"/>
<script src="js/jquery.js"></script>
<script src="js/functions.js"></script>
</header>
<body class="container-fluid fill-height">
<div id = "divContainer" class="col-lg-6 col-lg-offset-3">
<center>
<?php
//evaluate title
echo "<h1>";
if($requestType == "staffupdate"){
//staff update header
echo "Staff Update";
}else if($requestType == "studentupdate"){
//student update header
echo "Student Update";
}else if($requestType == "classupdate"){
//class update title
echo "Class Update";
}else{
die("Invalid request type to access update module!");
}
echo "</h1>";
?>
<br>
<form method = "post">
<h6 align = "left" id = "elementLabel"><span><strong>First Name</strong></span></h6>
<input onkeyup = "textOnly(this)" value = "<?php echo $q_fname; ?>" required = "required" name="fname" type="text" placeholder="First Name" autofocus/><br>
<h6 align = "left" id = "elementLabel"><span><strong>Middle Name</strong> <i>(optional)</i></span></h6>
<input onkeyup = "textOnly(this)" value = "<?php echo $q_mname; ?>" name="mname" type="text" placeholder="Middle Name"/><br>
<h6 align = "left" id = "elementLabel"><span><strong>Last Name</strong></span></h6>
<input onkeyup = "textOnly(this)" value = "<?php echo $q_lname; ?>" required = "required" name="lname" type="text" placeholder="Last Name"/><br>
<h6 align = "left" id = "elementLabel"><span><strong>Address</strong></span></h6>
<input value = "<?php echo $q_address; ?>" required = "required" name="address" type="text" placeholder="Address"/><br>
<h6 align = "left" id = "elementLabel"><span><strong>Birth Date</strong></span></h6>
<input value = "<?php echo $q_birthdate; ?>" class="datePicker" required = "required" name="birthdate" type="date"/><br>
<?php
if($requestType == "staffupdate"){
// staff registration inputfield for admin access only
/*
1. position
2. organization
*/
echo '<h6 align = "left" id = "elementLabel"><span><strong>Position</strong></span></h6>';
echo '<input value = "'.$q_position.'" required = "required" name="position" type="text" placeholder="Position"/><br>';
echo '<h6 align = "left" id = "elementLabel"><span><strong>Organization</strong></span></h6>';
echo '<input value = "'.$q_organization.'" required = "required" name="organization" type="text" placeholder="Organization"/><br>';
}else if ($requestType == "studentupdate"){
/*<input value = "'.$q_student_class.'" style = "text-align: center;width:80%; margin: 0px; margin-top: 3px; margin-bottom: 3px; border-radius: 3px; border: 1px solid;" required = "required" id = "class_search" name = "classID" list = "result-class" placeholder="Class"/>
<datalist id="result-class">
</datalist>*/
echo '<h6 align = "left" id = "elementLabel"><span><strong>Current Class</strong></span></h6>';
echo '<select name = "classID" style = "text-align-last: center; width: 80%; padding-top: 15px; padding-bottom: 15px;">';
echo '<option value = "">-- Remove student from class --</option>';
$q_registered_classes;
if($_SESSION["user_account_level"] == 1){
$q_registered_classes = mysqli_query(
$conn,
"SELECT * FROM class ORDER BY class_grade
"
)
or die (mysqli_error($con));
}else{
$q_registered_classes = mysqli_query(
$conn,
"SELECT * FROM class WHERE class_staff = $current_loggedUser ORDER BY class_grade
"
)
or die (mysqli_error($con));
}
while($r_registered_classes = mysqli_fetch_assoc($q_registered_classes)){
$student_class = "";
// select the current class
if($r_registered_classes['class_ID'] == $q_student_class){
$student_class = "selected = \"selected\"";
}
echo "<option value = \"".$r_registered_classes['class_ID']."\" $student_class>Grade ".$r_registered_classes['class_grade']." - ".$r_registered_classes['class_section']."</option>";
}
echo '</select>';
}
?>
<h6 align = "left" id = "elementLabel"><span><strong>Username</strong> <i>(must be unique)</i></span></h6>
<input onkeyup = "usernameChars(this)" value = "<?php echo $q_username?>" required = "required" name="username" type="text" placeholder="Username" value=""><br>
<h6 align = "left" id = "elementLabel"><span><strong>Change Password</strong></span></h6>
<input id = "password" name="password" type="password" placeholder="Change password"/><br>
<h6 align = "left" id = "elementLabel"><span><strong>Confirm Password</strong></span></h6>
<input id = "confpassword" name="confpassword" type="password" placeholder="Confirm password"/><br>
<?php
/*
admin element inputfield control
select for account level
*/
if( $requestType == "staffupdate" && empty($_GET["accproperty"])){
echo '<h6 align = "left" id = "elementLabel"><span><strong>Account Access Level</strong></span></h6>';
if($q_account_level == 1){
echo('
<select style = "text-align-last: center; width: 80%; padding-top: 15px; padding-bottom: 15px;" name = "access_Level">
<option value = "'.$q_account_level.'">Currently (Administrator)</option>
');
}else{
echo('
<select style = "text-align-last: center; width: 80%; padding-top: 15px; padding-bottom: 15px;" name = "access_Level">
<option value = "'.$q_account_level.'">Currently (Teacher)</option>
');
}
echo('
<option value="1">Administrator</option>
<option value="2">Teacher</option>
</select><br>');
}
?>
<hr>
<input onclick = "return updateValidate('password', 'confpassword')" name="update" type="submit" value="Update"><br>
</form>
<?php
//cancel redirect link
$linktoReturn;
if($requestType == "staffupdate"){
//staff link
$linktoReturn = "staff-search.php";
}else if($requestType == "studentupdate"){
//student link
$linktoReturn = "student-search.php";
}else if($requestType == "classupdate"){
//class link
$linktoReturn = "class-search.php";
}else{
die("Invalid request type to access update module!");
}
if(!isset($_GET['redirect']) == "userprofile"){
echo "<a href = \"".$linktoReturn."\"><button>Cancel</button></a>";
}else{
echo "<a href = \"user-profile.php?id=$id&user=$q_username&userType=$user_type\"><button>Cancel</button></a>";
}
?>
</center>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#class_search').keyup(function(){
var searchtext = $('#class_search').val();
$.get('php/minimod_class-search.php',{search:searchtext}, function(response){
$('#result-class').html(response);
});
});
})
</script>
<?php
//error messages
$username_exist = "<script>alert(\"Username already exist\")</script>";
$pf_update_query;
//universal variables
$redirect_location;
if(isset($_POST['update'])){
$updated_username; // updated username
//staff update
if($requestType == "staffupdate"){
$fname = mysqli_real_escape_string($conn, $_POST['fname']);
$mname = mysqli_real_escape_string($conn, $_POST['mname']);
$lname = mysqli_real_escape_string($conn, $_POST['lname']);
$bdate = mysqli_real_escape_string($conn, $_POST['birthdate']);
$address = mysqli_real_escape_string($conn, $_POST['address']);
$organization = mysqli_real_escape_string($conn, $_POST['organization']);
$position = mysqli_real_escape_string($conn, $_POST['position']);
$username = mysqli_real_escape_string($conn, $_POST['username']);
$acc_level = mysqli_real_escape_string($conn, $_POST['access_Level']);
$password;
$enc_password;
// replace new session value for username if it's the account owner
if(isset($_GET['accproperty'])){
$_SESSION["username"] = $username;
}
//update PF Information
$q_target_staff = mysqli_query($conn,
"SELECT
staffs.staff_ID,
staffs.staff_username AS username,
COUNT(performance_data.pf_id)
FROM `performance_data` INNER JOIN staffs ON staffs.staff_ID = performance_data.pf_userID
WHERE staffs.staff_ID = $id");
$r_target_staff = mysqli_fetch_assoc($q_target_staff);
// update if the results is more than 0
if($count_target_staff = mysqli_num_rows($q_target_staff) > 0){
$target_staff_id = $r_target_staff['staff_ID'];
$target_staff_username = $r_target_staff['username'];
$update_staff_pfdata = mysqli_query($conn,
"UPDATE
`performance_data`
SET
`pf_username`= '$username'
WHERE
performance_data.pf_userID = $target_staff_id AND
performance_data.pf_username = '$target_staff_username'");
}
//redirect location
$redirect_location = "location: staff-search.php";
// if the password inputfield has value, change the password and encrypt again then update the database
if (!empty($_POST['password'])){
$password = mysqli_real_escape_string($conn, $_POST['password']);
$enc_password = password_hash($password,PASSWORD_BCRYPT);
$update = mysqli_multi_query($conn,
"UPDATE `staffs`
SET
`staff_fname`='".$fname."',
`staff_mname`='".$mname."',
`staff_lname`='".$lname."',
`staff_birthdate`='".$bdate."',
`staff_address`='".$address."',
`staff_organization`='".$organization."',
`staff_position`='".$position."',
`staff_username`='".$username."',
`staff_password`='".$enc_password."',
`staff_accountLevel`='".$acc_level."'
WHERE `staff_ID` = '$id'
") or die (mysqli_error($conn));
}else{
// if inputfield password is empty then do not update the password
$update = mysqli_multi_query($conn,
"UPDATE `staffs`
SET
`staff_fname`='".$fname."',
`staff_mname`='".$mname."',
`staff_lname`='".$lname."',
`staff_birthdate`='".$bdate."',
`staff_address`='".$address."',
`staff_organization`='".$organization."',
`staff_position`='".$position."',
`staff_username`='".$username."',
`staff_accountLevel`='".$acc_level."'
WHERE `staff_ID` = '$id'
") or die (mysqli_error($conn));
}
}else if($requestType == "studentupdate"){
//input elemetns for studetnts
$fname = mysqli_real_escape_string($conn, $_POST['fname']);
$mname = mysqli_real_escape_string($conn, $_POST['mname']);
$lname = mysqli_real_escape_string($conn, $_POST['lname']);
$bdate = mysqli_real_escape_string($conn, $_POST['birthdate']);
$address = mysqli_real_escape_string($conn, $_POST['address']);
$classID = mysqli_real_escape_string($conn, $_POST['classID']);
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
//update PF Information
$q_target_student = mysqli_query($conn,
"SELECT
students.student_ID,
students.student_username AS username,
COUNT(performance_data.pf_id)
FROM `performance_data` INNER JOIN students ON students.student_ID = performance_data.pf_userID
WHERE students.student_ID = $id");
$r_target_student = mysqli_fetch_assoc($q_target_student);
// update if the results is more than 0
if($count_target_student = mysqli_num_rows($q_target_student) > 0){
$target_student_id = $r_target_student['student_ID'];
$target_student_username = $r_target_student['username'];
$update_student_pfdata = mysqli_query($conn,
"UPDATE
`performance_data`
SET
`pf_username`= '$username'
WHERE
performance_data.pf_userID = $target_student_id AND
performance_data.pf_username = '$target_student_username'");
}
//redirect location
$redirect_location = "location: student-search.php";
// if the password inputfield has value, change the password and encrypt again then update the database
if (!empty($_POST['password'])){
$student_enc_password = password_hash($password,PASSWORD_BCRYPT);
//update student information
$student_query = mysqli_query($conn,
"UPDATE `students`
SET
`student_fname`='".$fname."',
`student_mname`='".$mname."',
`student_lname`='".$lname."',
`student_birthdate`='".$bdate."',
`student_address`='".$address."',
`student_classID`='".$classID."',
`student_username`='".$username."',
`student_password`='".$student_enc_password."'
WHERE
`student_ID` = '".$id."'
") or die(mysqli_error($conn));
}else{
// if inputfield password is empty then do not update the password
//update student information
$student_query = mysqli_query($conn,
"UPDATE `students`
SET
`student_fname`='".$fname."',
`student_mname`='".$mname."',
`student_lname`='".$lname."',
`student_birthdate`='".$bdate."',
`student_address`='".$address."',
`student_classID`='".$classID."',
`student_username`='".$username."'
WHERE
`student_ID` = '".$id."'
") or die(mysqli_error($conn));
}
}
if(!$_GET['redirect'] == "user-profile"){
header($redirect_location);
}else{
echo $_GET['redirect'];
header('location: user-profile.php?id='.$id.'&user='.$username."&userType=".$user_type);
}
}
?>