-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification_helper.php
More file actions
216 lines (185 loc) · 8.38 KB
/
notification_helper.php
File metadata and controls
216 lines (185 loc) · 8.38 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
<?php
// Notification helper functions
/**
* Create a notification in the database
* @param mysqli $conn Database connection
* @param int|null $uid User ID (null for expert notifications)
* @param int|null $eid Expert ID (null for user notifications)
* @param string $type Notification type
* @param string $description Notification description
* @param bool $isPersonal Whether notification is personal (true) or global (false)
* @return bool Success status
*/
function createNotification($conn, $uid = null, $eid = null, $type, $description, $isPersonal = true) {
$sql = "INSERT INTO notification (uid, eid, type, description, is_personal) VALUES (?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
if ($stmt) {
$stmt->bind_param("iissi", $uid, $eid, $type, $description, $isPersonal);
$result = $stmt->execute();
$stmt->close();
return $result;
}
return false;
}
/**
* Create notification for new blog post
* @param mysqli $conn Database connection
* @param string $authorName Blog author name
* @param string $blogTitle Blog title
* @param int $authorId Author's user ID (to exclude from notifications)
*/
function notifyNewBlog($conn, $authorName, $blogTitle, $authorId) {
// Global notification - goes to everyone, no specific uid/eid
$description = "New blog posted: \"$blogTitle\" by $authorName.";
createNotification($conn, null, null, 'new_blog', $description, false);
}
/**
* Create notification for blog like
* @param mysqli $conn Database connection
* @param int $blogAuthorId Blog author's user ID
* @param string $likerName Name of person who liked
* @param string $blogTitle Blog title
* @param bool $isExpertBlog Whether the blog was created by an expert
*/
function notifyBlogLike($conn, $blogAuthorId, $likerName, $blogTitle, $isExpertBlog = false) {
$description = "Your blog \"$blogTitle\" has been liked by $likerName.";
if ($isExpertBlog) {
// Expert blog - notify expert author
createNotification($conn, null, $blogAuthorId, 'blog_like', $description, true);
} else {
// User blog - notify user author
createNotification($conn, $blogAuthorId, null, 'blog_like', $description, true);
}
}
/**
* Create notification for blog comment
* @param mysqli $conn Database connection
* @param int $blogAuthorId Blog author's user ID
* @param string $commenterName Name of person who commented
* @param string $blogTitle Blog title
* @param bool $isExpertBlog Whether the blog was created by an expert
*/
function notifyBlogComment($conn, $blogAuthorId, $commenterName, $blogTitle, $isExpertBlog = false) {
$description = "Your blog \"$blogTitle\" has a new comment from $commenterName.";
if ($isExpertBlog) {
// Expert blog - notify expert author
createNotification($conn, null, $blogAuthorId, 'blog_comment', $description, true);
} else {
// User blog - notify user author
createNotification($conn, $blogAuthorId, null, 'blog_comment', $description, true);
}
}
/**
* Create notification for new course enrollment
* @param mysqli $conn Database connection
* @param int $courseId Course ID
* @param string $newMemberName Name of new member
* @param string $courseName Course name
* @param int $newMemberId New member's ID (to exclude from notifications)
*/
function notifyCourseNewMember($conn, $courseId, $newMemberName, $courseName, $newMemberId) {
$description = "$newMemberName has joined the course \"$courseName\" that you are taking.";
// Notify the expert instructor
$expertSql = "SELECT eid FROM course WHERE cid = ?";
$expertStmt = $conn->prepare($expertSql);
if ($expertStmt) {
$expertStmt->bind_param("i", $courseId);
$expertStmt->execute();
$expertResult = $expertStmt->get_result();
if ($expertRow = $expertResult->fetch_assoc()) {
// Create notification for the expert
createNotification($conn, null, $expertRow['eid'], 'course_new_member', $description, true);
}
$expertStmt->close();
}
// Notify all enrolled users except the new member
$userSql = "SELECT DISTINCT uid FROM enrollment WHERE cid = ? AND uid != ?";
$userStmt = $conn->prepare($userSql);
if ($userStmt) {
$userStmt->bind_param("ii", $courseId, $newMemberId);
$userStmt->execute();
$userResult = $userStmt->get_result();
while ($userRow = $userResult->fetch_assoc()) {
// Create notification for each enrolled user
createNotification($conn, $userRow['uid'], null, 'course_new_member', $description, true);
}
$userStmt->close();
}
}
/**
* Create notification for course feedback
* @param mysqli $conn Database connection
* @param int $courseId Course ID
* @param string $feedbackGiverName Name of feedback giver
* @param string $courseName Course name
* @param int $feedbackGiverId Feedback giver's ID (to exclude from notifications)
*/
function notifyCourseFeedback($conn, $courseId, $feedbackGiverName, $courseName, $feedbackGiverId) {
$description = "$feedbackGiverName has given feedback on the course \"$courseName\" that you are taking.";
// Notify the expert instructor
$expertSql = "SELECT eid FROM course WHERE cid = ?";
$expertStmt = $conn->prepare($expertSql);
if ($expertStmt) {
$expertStmt->bind_param("i", $courseId);
$expertStmt->execute();
$expertResult = $expertStmt->get_result();
if ($expertRow = $expertResult->fetch_assoc()) {
// Create notification for the expert
createNotification($conn, null, $expertRow['eid'], 'course_feedback', $description, true);
}
$expertStmt->close();
}
// Notify all enrolled users except the feedback giver
$userSql = "SELECT DISTINCT uid FROM enrollment WHERE cid = ? AND uid != ?";
$userStmt = $conn->prepare($userSql);
if ($userStmt) {
$userStmt->bind_param("ii", $courseId, $feedbackGiverId);
$userStmt->execute();
$userResult = $userStmt->get_result();
while ($userRow = $userResult->fetch_assoc()) {
// Create notification for each enrolled user
createNotification($conn, $userRow['uid'], null, 'course_feedback', $description, true);
}
$userStmt->close();
}
}
/**
* Create notification for new appointment
* @param mysqli $conn Database connection
* @param int $expertId Expert ID
* @param int $userId User ID who booked the appointment
* @param string $userName User name
* @param string $appointmentDate Appointment date
* @param string $appointmentTime Appointment time
*/
function notifyNewAppointment($conn, $expertId, $userId, $userName, $appointmentDate, $appointmentTime) {
$formattedDate = date('l, F j, Y', strtotime($appointmentDate));
$formattedTime = date('g:i A', strtotime($appointmentTime));
// Personal notification - shows only to the expert (with both uid and eid for context)
$description = "$userName has booked an appointment with you on $formattedDate at $formattedTime.";
createNotification($conn, $userId, $expertId, 'appointment_new', $description, true);
}
/**
* Create notification for appointment status change
* @param mysqli $conn Database connection
* @param int $userId User ID
* @param int $expertId Expert ID who made the status change
* @param string $expertName Expert name
* @param string $status New status (accepted/declined)
* @param string $appointmentDate Appointment date
* @param string $appointmentTime Appointment time
*/
function notifyAppointmentStatusChange($conn, $userId, $expertId, $expertName, $status, $appointmentDate, $appointmentTime) {
$formattedDate = date('l, F j, Y', strtotime($appointmentDate));
$formattedTime = date('g:i A', strtotime($appointmentTime));
// Personal notification - shows only to the patient (with both uid and eid for context)
if ($status === 'accepted') {
$description = "Your appointment with $expertName on $formattedDate at $formattedTime has been accepted.";
$type = 'appointment_accepted';
} else {
$description = "Your appointment with $expertName on $formattedDate at $formattedTime has been declined.";
$type = 'appointment_declined';
}
createNotification($conn, $userId, $expertId, $type, $description, true);
}
?>