-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaff_PostIdeas.php
848 lines (813 loc) · 59.4 KB
/
Staff_PostIdeas.php
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
<?php
include("connection.php");
session_start(); // Start the session
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/Exception.php';
if (!isset($_SESSION['login'])) {
header("Location: index.php");
} else {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" href="./img/favicon.ico">
<!-- fontawesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"/>
<!-- bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD"
crossorigin="anonymous">
<!-- main css -->
<link rel="stylesheet" type="text/css" href="./css/style.css">
<title>Create Topic</title>
</head>
<body class="bg-gray">
<!--============ Header =========-->
<?php
include 'header.php';
?>
<!-- ================= Main ================= -->
<div class="container-fluid" style="margin-bottom: 100px;">
<div class="row justify-content-evenly">
<!-- ================= Sidebar ================= -->
<div class="col-12 col-lg-3">
<div class="d-none d-xxl-block h-100 fixed-top overflow-hidden scrollbar"
style="max-width: 360px; width: 100%; z-index: 4">
<ul class="navbar-nav mt-4 ms-3 d-flex flex-column pb-5 mb-5" style="padding-top: 56px">
<!-- top -->
<!-- avatar -->
<li class="dropdown-item p-1 rounded">
<a href="Settings.php" class="d-flex align-items-center text-decoration-none text-dark">
<div class="p-2">
<img src="https://source.unsplash.com/collection/happy-people" alt="avatar"
class="rounded-circle me-2"
style="width: 38px; height: 38px; object-fit: cover"/>
</div>
<div>
<p class="m-0"><?php echo $_SESSION['full_name']; ?></p>
</div>
</a>
</li>
<li class="dropdown-item p-1 rounded">
<a href="Staff.php" class="d-flex align-items-center text-decoration-none text-dark"
id="manage-topics">
<div class="p-2">
<i class="fa-solid fa-bars-progress topic-icon " style="font-size: 35px;"></i>
</div>
<div>
<p class="m-0" style="padding-left: 10px;">All Ideas</p>
</div>
</a>
</li>
<li class="dropdown-item p-1 rounded">
<a href="Staff_Last.php" class="d-flex align-items-center text-decoration-none text-dark"
id="manage-topics">
<div class="p-2">
<i class="fa-solid fa-bars-progress topic-icon" style="font-size: 35px;"></i>
</div>
<div>
<p class="m-0" style="padding-left: 10px;">Last Ideas</p>
</div>
</a>
</li>
<li class="dropdown-item p-1 rounded">
<a href="Staff_Hot.php" class="d-flex align-items-center text-decoration-none text-dark"
id="manage-topics">
<div class="p-2">
<i class="fa-solid fa-bars-progress topic-icon" style="font-size: 35px;"></i>
</div>
<div>
<p class="m-0" style="padding-left: 10px;">Hot Ideas</p>
</div>
</a>
</li>
<li class="dropdown-item p-1 rounded">
<a href="Staff_MyPost.php" class="d-flex align-items-center text-decoration-none text-dark"
id="manage-topics">
<div class="p-2">
<i class="fa-solid fa-bars-progress topic-icon" style="font-size: 35px;"></i>
</div>
<div>
<p class="m-0" style="padding-left: 10px;">My Ideas</p>
</div>
</a>
</li>
<li class="dropdown-item p-1 rounded">
<a href="Staff_Topics.php" class="d-flex align-items-center text-decoration-none text-dark"
id="manage-topics">
<div class="p-2">
<i class="fa-solid fa-bars-progress topic-icon active" style="font-size: 35px;"></i>
</div>
<div>
<p class="m-0" style="padding-left: 10px;" id="manage-topics-text">Topics</p>
</div>
</a>
</li>
<hr class="m-0"/>
</ul>
</div>
</div>
<!-- ================= Timeline ================= -->
<div class="col-12 col-lg-12">
<div class="d-flex flex-column justify-content-center mx-auto main-container">
<div class="row">
<div class="col-lg-6">
<?php
if (isset($_GET['topic'])) {
$topic = $_GET['topic'];
}
$sql = "SELECT * FROM Topic WHERE TopicID = '$topic'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
?>
<!-- Create Idea Form -->
<div class="bg-white p-3 text-center mb-3">
<form class="text-start" action="Staff_PostIdeas.php?topic=<?php echo $topic; ?>"
enctype="multipart/form-data"
method="post">
<input type="hidden" name="topic-id" value="<?php echo $topic; ?>">
<!-- head -->
<div class="modal-header bg-primary text-white bg-white mx-auto">
<h5 class="modal-title text-primary" id="exampleModalLabel">
Submit Your Ideas for Topic
</h5>
</div>
<!-- body -->
<div class="modal-body">
<!-- choose topic (auto-synced) -->
<div class="mb-3">
<label for="topic" class="form-label text-primary">Topic:</label>
<input type="text" class="form-control" id="topic-name" readonly
style="background-color: #f8f9fa" name="topic-name"
value="<?php echo $row['TopicName']; ?>">
</div>
<div class="mb-3">
<label for="topic" class="form-label text-primary">Description:</label>
<textarea class="form-control" rows="5" id="topic-description" readonly
style="background-color: #f8f9fa"
name="topic-description"><?php echo $row['Description']; ?></textarea>
</div>
<div class="mb-3">
<label for="topic" class="form-label text-primary">Title:</label>
<input type="text" class="form-control" id="topic-title" name="topic-title"
placeholder="Title">
</div>
<div class="mb-3">
<!-- text -->
<label for="text" class="form-label text-primary">Your Ideas:</label>
<textarea class="form-control" id="text" rows="5" required name="message"
placeholder="Enter your ideas here..."
></textarea>
</div>
<div class="mb-3">
<!-- options -->
<label class="form-label text-primary">Upload File:</label>
<div class="input-group">
<input type="file" class="form-control" id="file" name="file-upload">
</div>
</div>
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" value=""
id="anonymousComment"
name="anonymous">
<label class="form-check-label" for="anonymousComment">
Post anonymously
</label>
</div>
<div class="form-check mb-3">
<!-- agree to terms and conditions -->
<input type="checkbox" class="form-check-input" id="terms" required>
<label class="form-check-label" for="terms">
I agree to the <a href="#" data-bs-toggle="modal"
data-bs-target="#termsModal"
class="text-primary">terms
and conditions</a>
</label>
</div>
</div>
<!-- footer -->
<?php
if (isset($_GET['topic'])) {
$topic = $_GET['topic'];
}
// Check if closure date has passed for topic disabled submit button
$sql = "SELECT * FROM Topic, Deadline WHERE Topic.DeadlineID = Deadline.DeadlineID AND Topic.TopicID = '$topic'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$closureDate = $row['ClosureDate'];
$today = date("Y-m-d");
if ($closureDate < $today) { ?>
<span class="badge bg-danger">This topic has been closed for ideas. You can no longer submit ideas for this topic.</span>
<div class="modal-footer">
<button type="submit" class="btn btn-primary w-100" disabled>Submit
</button>
</div>
<?php } else { ?>
<div class="modal-footer">
<button type="submit" class="btn btn-primary w-100" name="add-idea">Submit
</button>
</div>
<?php } ?>
</form>
</div>
<?php
// Add idea for topic
include("connection.php");
if (isset($_POST['add-idea'])) {
$topicID = $_POST['topic-id'];
$title = $_POST['topic-title'];
$message = $_POST['message'];
$isAnonymous = isset($_POST['anonymous']) ? 1 : 0;
// Check if user uploaded a file
if (isset($_FILES['file-upload']['name']) && $_FILES['file-upload']['name'] != "") {
$fileName = $_FILES['file-upload']['name'];
$tempFile = $_FILES['file-upload']['tmp_name'];
$uploadDir = "uploads/";
$fileType = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
$validFileTypes = array("pdf", "doc", "docx", "txt", "path", "png", "jpg", "jpeg", "gif");
// Check if file type is valid
if (!in_array($fileType, $validFileTypes)) {
echo "<script>alert('Invalid file type. Please upload a PDF, DOC, DOCX, TXT or PATH file.')</script>";
echo "<script>window.location.href = 'Staff_PostIdeas.php?topic=$topicID'</script>";
exit();
} else if ($_FILES['file-upload']['size'] > 5000000) {
echo "<script>alert('File size must be less than 5 MB.')</script>";
echo "<script>window.location.href = 'Staff_PostIdeas.php?topic=$topicID'</script>";
exit();
} else {
$documentPath = $uploadDir . uniqid() . "." . $fileType;
move_uploaded_file($tempFile, $documentPath);
}
} else {
$documentPath = "";
}
$postDate = date("Y-m-d H:i:s");
$staffID = $_SESSION["staff_id"];
$sql = "INSERT INTO Idea (Title, Content, is_anonymous, PostDate, StaffID, TopicID) VALUES ('$title', '$message', '$isAnonymous', '$postDate', '$staffID', '$topicID')";
$result = mysqli_query($conn, $sql);
if ($result) {
$ideaID = mysqli_insert_id($conn);
if (!empty($documentPath)) {
$sql = "INSERT INTO Document (DocumentPath, IdeaID)
VALUES ('$documentPath', '$ideaID')";
$result = mysqli_query($conn, $sql);
if (!$result) {
echo "<script>alert('Error while saving document path.')</script>";
echo "<script>window.location.href = 'Staff_PostIdeas.php?topic=$topicID'</script>";
}
}
// // Send email notification to QA Coordinator
// $sql = "SELECT * FROM Staff,Department, Role WHERE Staff.DepartmentID = Department.DepartmentID AND Staff.RoleID = Role.RoleID AND Role.RoleID = 2 AND Department.DepartmentID = " . $_SESSION['department_id'];
//
// $result = mysqli_query($conn, $sql);
// $row = mysqli_fetch_assoc($result);
// $email = $row['Email'];
// $name = $row['FullName'];
// // get topic name
// $sql = "SELECT * FROM Topic WHERE TopicID = " . $topicID;
// $result = mysqli_query($conn, $sql);
// $row = mysqli_fetch_assoc($result);
// $topicname = $row['TopicName'];
//
// $mail = new PHPMailer\PHPMailer\PHPMailer();
// $to = $email;
// $subject = 'New idea has been submitted';
// $message = 'Dear ' . $name . ',<br><br>';
// $message .= 'A new idea has been submitted to the topic ' . $topicname . '.<br><br> Please login at https://greenwichideas.cleverapps.io/ to review the idea.<br><br>';
// $message .= 'Thank you,<br>Greenwich Quality Assurance Manager';
//
// // Set up the email headers
//
// try {
// // Server settings
// $mail->SMTPDebug = 0;
// $mail->isSMTP();
// $mail->Host = 'smtp.gmail.com';
// $mail->SMTPAuth = true;
// $mail->Username = '[email protected]';
// $mail->Password = 'rwdibyoqadzmaxbw';
// $mail->SMTPSecure = 'tls';
// $mail->Port = 587;
//
// // Recipients
// $mail->setFrom('[email protected]', 'Greenwich QA');
// $mail->addAddress($to, $name);
//
// // Content
// $mail->isHTML(true);
// $mail->Subject = $subject;
// $mail->Body = $message;
//
// $mail->send();
echo "<script>alert('Idea added successfully.')</script>";
echo "<script>window.location.href = 'Staff_PostIdeas.php?topic=$topicID'</script>";
// } catch (Exception $e) {
// echo "<script>alert('Message could not be sent. Mailer Error: {$mail->ErrorInfo}')</script>";
// echo "<script>window.location.href = 'Staff_PostIdeas.php?topic=$topicID'</script>";
// }
} else {
echo "<script>alert('Error while adding idea: " . mysqli_error($conn) . "')</script>";
echo "<script>window.location.href = 'Staff_PostIdeas.php?topic=$topicID'</script>";
}
}
?>
<!-- terms and conditions modal -->
<div class="modal fade" id="termsModal" tabindex="-1" aria-labelledby="termsModalLabel"
aria-hidden="true" data-bs-backdrop="false">
<div class="modal-dialog modal-dialog-centered modal-lg">
<div class="modal-content">
<!-- head -->
<div class="modal-header align-items-center">
<h5 class="text-dark text-center w-100 m-0" id="termsModalLabel">
Terms and Conditions
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"
aria-label="Close"></button>
</div>
<!-- body -->
<?php
include "termandconditon.php"
?>
</div>
</div>
</div>
</div>
<?php
// get ideas that sent with get method ?idea = id
if (isset($_GET['topic'])) {
$topic = $_GET['topic'];
$sql = "SELECT * FROM Idea, Staff, Topic WHERE Idea.StaffID = Staff.StaffID AND Idea.TopicID = Topic.TopicID AND Topic.TopicID = $topic ORDER BY Idea.PostDate DESC";
$result = mysqli_query($conn, $sql);
}
?>
<?php if (mysqli_num_rows($result) > 0) { ?>
<div class="d-flex flex-column justify-content-center w-100 mx-auto"
style="max-width: 680px;">
<?php while ($row = mysqli_fetch_assoc($result)) { ?>
<div class="bg-white p-4 rounded shadow mt-3">
<!-- author -->
<div class="d-flex justify-content-between">
<!-- avatar -->
<div class="d-flex">
<img src="https://source.unsplash.com/collection/happy-people"
alt="avatar"
class="rounded-circle me-2"
style="width: 38px; height: 38px; object-fit: cover"/>
<div>
<p class="m-0 fw-bold"><?php
if ($row['StaffID'] == $_SESSION['staff_id'] && $row['is_anonymous'] == 1) {
echo "You (Anonymous)";
} else if ($row['StaffID'] == $_SESSION['staff_id']) {
echo "You";
} else if ($row['is_anonymous'] == 1) {
echo "Anonymous";
} else {
echo $row['FullName'];
}
?></p>
<span class="text-muted fs-7"><?php
$current_time = new DateTime();
$post_time = new DateTime($row['PostDate']);
// Calculate the difference between the current time and the post time
$diff = $current_time->diff($post_time);
if ($diff->y > 0) {
echo $diff->y . " year" . ($diff->y > 1 ? "s" : "") . " ago";
} else if ($diff->m > 0) {
echo $diff->m . " month" . ($diff->m > 1 ? "s" : "") . " ago";
} else if ($diff->d > 0) {
echo $diff->d . " day" . ($diff->d > 1 ? "s" : "") . " ago";
} else if ($diff->h > 0) {
echo $diff->h . " hour" . ($diff->h > 1 ? "s" : "") . " ago";
} else if ($diff->i > 0) {
echo $diff->i . " minute" . ($diff->i > 1 ? "s" : "") . " ago";
} else if ($diff->s > 0) {
echo $diff->s . " second" . ($diff->s > 1 ? "s" : "") . " ago";
}
?></span>
</div>
</div>
<!-- edit -->
<i class="fas fa-ellipsis-h" type="button" id="post1Menu"
data-bs-toggle="dropdown"
aria-expanded="false"></i>
<!-- edit menu -->
<ul class="dropdown-menu border-0 shadow" aria-labelledby="post1Menu">
<li class="d-flex align-items-center">
<a class="dropdown-item d-flex justify-content-around align-items-center fs-7"
href="#">Edit
Post</a>
</li>
<li class="d-flex align-items-center">
<a class="dropdown-item d-flex justify-content-around align-items-center fs-7"
href="#">Delete
Post</a>
</li>
</ul>
</div>
<div class="my-3">
<h6>Topic Name: <?php echo $row['TopicName']; ?></h6>
<h6>Idea Title: <?php echo $row['Title']; ?></h6>
</div>
<!-- content -->
<div class="my-3">
<p><?php echo $row['Content']; ?></p>
<?php
// Check if a document exists in the database and display a download button if it does
$sql_download = "SELECT * FROM Document WHERE Document.IdeaID = " . $row['IdeaID'];
$result_download = mysqli_query($conn, $sql_download);
if (mysqli_num_rows($result_download) > 0) {
// Build a zip file containing all the documents for this idea
$zip = new ZipArchive();
$zipName = "documents-" . $row['IdeaID'] . ".zip";
$zipPath = "uploads/" . $zipName;
if ($zip->open($zipPath, ZipArchive::CREATE) !== TRUE) {
echo "Error: Could not create zip file";
}
while ($row_download = mysqli_fetch_assoc($result_download)) {
$filePath = $row_download['DocumentPath'];
$fileName = basename($filePath);
$zip->addFile($filePath, $fileName);
}
$zip->close();
?>
<a href="<?php echo $zipPath; ?>" download="<?php echo $zipName; ?>"
class="btn btn-primary"><i class="fas fa-download"></i>Download
Documents</a>
<?php
}
?>
</div>
<!-- like and dislike buttons -->
<div class="d-flex justify-content-between align-items-center">
<div class="d-flex">
<button type="button"
class="btn btn-outline-primary btn-sm me-2 like-btn"
id="btnLike-<?php echo $row['IdeaID']; ?>"
data-idea-id="<?php echo $row['IdeaID']; ?>">
<i class="far fa-thumbs-up"></i> Like (<span class="like-count"><?php
$sql2 = "SELECT * FROM Vote WHERE Vote.IdeaID = " . $row['IdeaID'] . " AND Vote.Status = 1";
$result2 = mysqli_query($conn, $sql2);
echo mysqli_num_rows($result2);
// Check if the user has already liked the post change the color of the button
$staffID = $_SESSION['staff_id'];
$ideaID = $row['IdeaID'];
$sql3 = "SELECT * FROM Vote WHERE Vote.IdeaID = $ideaID AND Vote.StaffID = $staffID AND Vote.Status = 1";
$result3 = mysqli_query($conn, $sql3);
if (mysqli_num_rows($result3) > 0) {
// Change class of the button
echo "<script>document.getElementById('btnLike-$ideaID').classList.add('btn-primary');</script>";
echo "<script>document.getElementById('btnLike-$ideaID').classList.remove('btn-outline-primary');</script>";
} else {
echo "<script>document.getElementById('btnLike-$ideaID').classList.add('btn-outline-primary');</script>";
echo "<script>document.getElementById('btnLike-$ideaID').classList.remove('btn-primary');</script>";
}
?></span>)
</button>
<button type="button" class="btn btn-outline-danger btn-sm dislike-btn"
id="btnDislike-<?php echo $row['IdeaID']; ?>"
data-idea-id="<?php echo $row['IdeaID']; ?>">
<i class="far fa-thumbs-down"></i> Dislike (<span
class="dislike-count"><?php
$sql4 = "SELECT * FROM Vote WHERE Vote.IdeaID = " . $row['IdeaID'] . " AND Vote.Status = 0";
$result4 = mysqli_query($conn, $sql4);
echo mysqli_num_rows($result4);
// Check if the user has already disliked the post change the color of the button
$staffID = $_SESSION['staff_id'];
$ideaID = $row['IdeaID'];
$sql5 = "SELECT * FROM Vote WHERE Vote.IdeaID = $ideaID AND Vote.StaffID = $staffID AND Vote.Status = 0";
$result5 = mysqli_query($conn, $sql5);
if (mysqli_num_rows($result5) > 0) {
// Change class of the button
echo "<script>document.getElementById('btnDislike-$ideaID').classList.add('btn-danger');</script>";
echo "<script>document.getElementById('btnDislike-$ideaID').classList.remove('btn-outline-danger');</script>";
} else {
echo "<script>document.getElementById('btnDislike-$ideaID').classList.add('btn-outline-danger');</script>";
echo "<script>document.getElementById('btnDislike-$ideaID').classList.remove('btn-danger');</script>";
}
?></span>)
</button>
</div>
<button type="button"
class="btn btn-outline-secondary btn-sm comment-toggle"
data-bs-toggle="collapse"
href="#comment<?php echo $row['IdeaID']; ?>"
aria-expanded="false" aria-controls="comment">
<i class="far fa-comment"></i> Comment (<?php
$sql3 = "SELECT * FROM Comment WHERE Comment.IdeaID = " . $row['IdeaID'];
$result3 = mysqli_query($conn, $sql3);
echo mysqli_num_rows($result3);
?>)
</button>
</div>
<!-- comment section -->
<?php
include("connection.php");
$sql2 = "SELECT * FROM Comment, Staff WHERE Comment.StaffID = Staff.StaffID AND Comment.IdeaID = " . $row['IdeaID'] . " ORDER BY Comment.CommentDate ASC";
$result2 = mysqli_query($conn, $sql2); ?>
<div id="comment<?php echo $row['IdeaID']; ?>" class="collapse">
<?php if (mysqli_num_rows($result2) > 0) { ?>
<!-- Comment section -->
<?php while ($row2 = mysqli_fetch_assoc($result2)) { ?>
<div class="comments mt-3">
<!-- Single comment -->
<div class="d-flex align-items-start comment">
<!-- Avatar -->
<img
src="https://source.unsplash.com/collection/happy-people"
alt="avatar" class="rounded-circle me-2"
style="width: 38px; height: 38px; object-fit: cover">
<div class="comment-body">
<!-- Commenter name -->
<p class="m-0 fw-bold"><?php
if ($row2['StaffID'] == $_SESSION['staff_id'] && $row2['is_anonymous'] == 1) {
echo "You (Anonymous)";
} else if ($row2['StaffID'] == $_SESSION['staff_id']) {
echo "You";
} else if ($row2['is_anonymous'] == 1) {
echo "Anonymous";
} else {
echo $row2['FullName'];
}
?></p>
<!-- Comment time -->
<span class="text-muted fs-7"><?php
$current_time = new DateTime();
$comment_time = new DateTime($row2['CommentDate']);
// Calculate the difference between the current time and the post time
$diff = $current_time->diff($comment_time);
if ($diff->y > 0) {
echo $diff->y . " year" . ($diff->y > 1 ? "s" : "") . " ago";
} else if ($diff->m > 0) {
echo $diff->m . " month" . ($diff->m > 1 ? "s" : "") . " ago";
} else if ($diff->d > 0) {
echo $diff->d . " day" . ($diff->d > 1 ? "s" : "") . " ago";
} else if ($diff->h > 0) {
echo $diff->h . " hour" . ($diff->h > 1 ? "s" : "") . " ago";
} else if ($diff->i > 0) {
echo $diff->i . " minute" . ($diff->i > 1 ? "s" : "") . " ago";
} else if ($diff->s > 0) {
echo $diff->s . " second" . ($diff->s > 1 ? "s" : "") . " ago";
}
?>
</span>
<!-- Comment content -->
<p class="mt-2"><?php echo $row2['CommentContent']; ?></p>
</div>
</div>
</div>
<?php } ?>
<?php } ?>
<?php
// Check current data if greater than closuredate and finalclosuredate then hide the comment form. If just greater than closuredate and less than finalclosuredate then show the comment form with a warning message.
// Use sql to get ClosureDate and FinalClosureDate
$current_date = new DateTime();
$sql6 = "SELECT * FROM Topic, Deadline WHERE Topic.DeadlineID = Deadline.DeadlineID AND Topic.TopicID = " . $row['TopicID'];
$result6 = mysqli_query($conn, $sql6);
$row6 = mysqli_fetch_assoc($result6);
// Convert to DateTime
$closure_date = new DateTime($row6['ClosureDate']);
$final_closure_date = new DateTime($row6['FinalClosureDate']);
if ($current_date > $closure_date && $current_date > $final_closure_date) {
echo "<div class='alert alert-danger mt-3' role='alert'>This idea has been closed. You can no longer comment on this idea.</div>";
} else if ($current_date > $closure_date && $current_date < $final_closure_date) { ?>
<div class="alert alert-warning mt-3" role="alert">This idea has been
closed. You
can still comment on this idea but it will be reviewed by the admin.
</div>
<!-- Comment form -->
<div class="mt-3">
<form action="Staff_PostIdeas.php?topic=<?php echo $topic; ?>"
method="post">
<!-- Comment content input -->
<input type="hidden" name="ideaID"
value="<?php echo $row['IdeaID']; ?>">
<div class="form-floating mb-3">
<textarea class="form-control" placeholder="Leave a comment here"
id="commentContent" style="height: 100px"
name="commentContent" required></textarea>
<label for="commentContent">Leave a comment here</label>
</div>
<!-- Checkbox for anonymous comment -->
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" value=""
id="anonymousComment" name="anonymousComment">
<label class="form-check-label" for="anonymousComment">
Post anonymously
</label>
</div>
<!-- Submit button -->
<button type="submit" class="btn btn-primary"
name="add-comment">
Submit
</button>
</form>
</div>
<?php } else { ?>
<div class="alert alert-success mt-3" role="alert">You can comment on
this
idea.
</div>
<!-- Comment form -->
<div class="mt-3">
<form action="Staff_PostIdeas.php?topic=<?php echo $topic; ?>"
method="post">
<!-- Comment content input -->
<input type="hidden" name="ideaID"
value="<?php echo $row['IdeaID']; ?>">
<div class="form-floating mb-3">
<textarea class="form-control" placeholder="Leave a comment here"
id="commentContent" style="height: 100px"
name="commentContent" required></textarea>
<label for="commentContent">Leave a comment here</label>
</div>
<!-- Checkbox for anonymous comment -->
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" value=""
id="anonymousComment" name="anonymousComment">
<label class="form-check-label" for="anonymousComment">
Post anonymously
</label>
</div>
<!-- Submit button -->
<button type="submit" class="btn btn-primary"
name="add-comment">
Submit
</button>
</form>
</div>
<?php } ?>
</div>
</div>
<?php } ?>
</div>
<?php } ?>
<?php
include("connection.php");
// add comment for topic
if (isset($_POST['add-comment'])) {
$ideaID = $_POST['ideaID'];
$content = $_POST['commentContent'];
$staffID = $_SESSION['staff_id'];
$commentDate = date("Y-m-d H:i:s");
$isAnonymous = isset($_POST['anonymousComment']) ? 1 : 0;
$sql = "INSERT INTO Comment (CommentContent, StaffID, IdeaID, is_anonymous, CommentDate) VALUES ('$content', '$staffID', '$ideaID', '$isAnonymous', '$commentDate')";
if (mysqli_query($conn, $sql)) {
// // The author of an idea receives an automatic email notification whenever a comment is submitted to any of their ideas.
// $sql2 = "SELECT * FROM Idea WHERE IdeaID = '$ideaID'";
// $result2 = mysqli_query($conn, $sql2);
// $row2 = mysqli_fetch_assoc($result2);
// $authorID = $row2['StaffID'];
// $sql3 = "SELECT * FROM Staff WHERE StaffID = '$authorID'";
// $result3 = mysqli_query($conn, $sql3);
// $row3 = mysqli_fetch_assoc($result3);
// $authorEmail = $row3['Email'];
// $authorName = $row3['StaffName'];
// $to = $authorEmail;
// $subject = "New comment on your idea";
// $message = "Dear " . $row3['StaffName'] . ",<br><br> A new comment has been submitted to your idea $row2[Title].<br><br>";
// $message .= 'Comment: ' . $content . '<br><br>';
// $message .= 'Please login at https://greenwichideas.cleverapps.io/ to your account to view the comment.<br><br>';
// $message .= 'Thank you,<br>Greenwich Quality Assurance Manager';
//
// // Set up the email headers
// $mail = new PHPMailer\PHPMailer\PHPMailer();
//
// try {
// // Server settings
// $mail->SMTPDebug = 0;
// $mail->isSMTP();
// $mail->Host = 'smtp.gmail.com';
// $mail->SMTPAuth = true;
// $mail->Username = '[email protected]';
// $mail->Password = 'rwdibyoqadzmaxbw';
// $mail->SMTPSecure = 'tls';
// $mail->Port = 587;
//
// // Recipients
// $mail->setFrom('[email protected]', 'Greenwich QA');
// $mail->addAddress($to, $authorName);
//
// // Content
// $mail->isHTML(true);
// $mail->Subject = $subject;
// $mail->Body = $message;
// $mail->send();
echo "<script>alert('Comment added successfully!')</script>";
echo "<script>window.location.href='Staff_PostIdeas.php?topic=$topic'</script>";
// } catch (Exception $e) {
// echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
// }
} else {
echo "<script>alert('Error: " . $sql . "<br>" . mysqli_error($conn) . "')</script>";
}
}
?>
<!--============ Footer =========-->
<?php
include 'footer.php';
?>
<!-- bootstrap -->
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN"
crossorigin="anonymous"></script>
<!-- main js -->
<script src="./js/main.js"></script>
</div>
</div>
</div>
</div>
<script>
// Like button click event listener
document.querySelectorAll('.like-btn').forEach((btn) => {
btn.addEventListener('click', () => {
const ideaID = btn.getAttribute('data-idea-id');
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
// Update like count
const likeCount = btn.querySelector('.like-count');
const data = JSON.parse(this.responseText);
likeCount.textContent = data.like;
// Update dislike count
const dislikeCount = btn.parentNode.querySelector('.dislike-count');
dislikeCount.textContent = data.dislike;
// Change button style
const likeBtn = document.getElementById('btnLike-' + ideaID);
const dislikeBtn = document.getElementById('btnDislike-' + ideaID);
// Check if the user has already liked the idea
if (data.status == 1 && data.idea_id == ideaID) {
// If the user has already liked the idea, remove the like
likeBtn.classList.remove('btn-outline-primary');
likeBtn.classList.add('btn-primary');
dislikeBtn.classList.remove('btn-danger');
dislikeBtn.classList.add('btn-outline-danger');
} else if (data.status == 0 && data.idea_id == ideaID) {
// If the user has already disliked the idea, remove the dislike
dislikeBtn.classList.remove('btn-outline-danger');
dislikeBtn.classList.add('btn-danger');
likeBtn.classList.remove('btn-primary');
likeBtn.classList.add('btn-outline-primary');
} else {
// If the user has not liked or disliked the idea, add the like
likeBtn.classList.add('btn-outline-primary');
dislikeBtn.classList.add('btn-outline-danger');
}
}
};
xhr.open("POST", "vote.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(`idea_id=${ideaID}&status=1`);
});
});
// Dislike button click event listener
document.querySelectorAll('.dislike-btn').forEach((btn) => {
btn.addEventListener('click', () => {
const ideaID = btn.getAttribute('data-idea-id');
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
// Update dislike count and like count
const dislikeCount = btn.querySelector('.dislike-count');
const data = JSON.parse(this.responseText);
dislikeCount.textContent = data.dislike;
const likeCount = btn.parentNode.querySelector('.like-count');
likeCount.textContent = data.like;
// Change button style
const likeBtn = document.getElementById('btnLike-' + ideaID);
const dislikeBtn = document.getElementById('btnDislike-' + ideaID);
// Check if the user has already liked the idea
if (data.status == 1 && data.idea_id == ideaID) {
// If the user has already liked the idea, remove the like
likeBtn.classList.remove('btn-outline-primary');
likeBtn.classList.add('btn-primary');
dislikeBtn.classList.remove('btn-danger');
dislikeBtn.classList.add('btn-outline-danger');
} else if (data.status == 0 && data.idea_id == ideaID) {
// If the user has already disliked the idea, remove the dislike
dislikeBtn.classList.remove('btn-outline-danger');
dislikeBtn.classList.add('btn-danger');
likeBtn.classList.remove('btn-primary');
likeBtn.classList.add('btn-outline-primary');
} else {
// If the user has not liked or disliked the idea, add the like
likeBtn.classList.add('btn-outline-primary');
dislikeBtn.classList.add('btn-outline-danger');
}
}
};
xhr.open("POST", "vote.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(`idea_id=${ideaID}&status=0`);
});
});
</script>
</body>
</html>
<?php } ?>