add English (Australian) language
[openemr.git] / library / dated_reminder_functions.php
blob18070aeb7cab120b7fe51eec482ea585106d2c0e
1 <?php
3 /**
4 * Contains functions used in the dated reminders.
6 * @package OpenEMR
7 * @link https://www.open-emr.org
8 * @author Craig Bezuidenhout <http://www.tajemo.co.za/>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @author Jerry Padgett <sjpadgett@gmail.com>
11 * @copyright Copyright (c) 2012 tajemo.co.za <http://www.tajemo.co.za/>
12 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 /**
17 * Get Portal Alerts function
19 * @returns array of alerts count
21 function GetPortalAlertCounts()
23 $counts = array();
24 $s_user = '%' . $_SESSION['authUser'] . '%';
26 $query = "SELECT Count(`m`.message_status) AS count_mail FROM onsite_mail `m` " .
27 "WHERE `m`.owner LIKE ? AND `m`.recipient_id LIKE ? AND `m`.message_status LIKE ? AND `m`.deleted = 0";
28 $qrtn = sqlQueryNoLog($query, array($s_user, $s_user, '%new%'));
29 $counts['mailCnt'] = $qrtn['count_mail'] ?: "0";
31 $query = "SELECT Count(`m`.status) AS count_audits FROM onsite_portal_activity `m` " .
32 "WHERE `m`.status LIKE ?";
33 $qrtn = sqlQueryNoLog($query, array('%waiting%'));
34 $counts['auditCnt'] = $qrtn['count_audits'] ?: "0";
36 $query = "SELECT Count(`m`.id) AS count_chats FROM onsite_messages `m` " .
37 "WHERE `m`.recip_id LIKE ? AND `m`.date > (CURRENT_DATE()-2) AND `m`.date < (CURRENT_DATE()+1)";
38 $qrtn = sqlQueryNoLog($query, array($s_user));
39 $counts['chatCnt'] = $qrtn['count_chats'] ? $qrtn['count_chats'] : "0";
41 $query = "SELECT Count(`m`.status) AS count_payments FROM onsite_portal_activity `m` " .
42 "WHERE `m`.status LIKE ? AND `m`.activity = ?";
43 $qrtn = sqlQueryNoLog($query, array('%waiting%', 'payment'));
44 $counts['paymentCnt'] = $qrtn['count_payments'] ?: "0";
46 $counts['total'] = $counts['mailCnt'] + $counts['auditCnt'] + $counts['chatCnt'] + $counts['paymentCnt'];
48 return $counts;
51 /**
52 * RemindersArray function
54 * @returns array reminders for specified user, defaults to current user if none specified
56 function RemindersArray($days_to_show, $today, $alerts_to_show, $userID = null)
58 if (!$userID) {
59 $userID = $_SESSION['authUserID'];
62 global $hasAlerts;
63 // ----- define a blank reminders array
64 $reminders = array();
66 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
67 $drSQL = sqlStatement("SELECT dr.pid, dr.dr_id, dr.dr_message_text,dr.dr_message_due_date,
68 u.fname ffname, u.mname fmname, u.lname flname FROM `dated_reminders` dr
69 JOIN `users` u ON dr.dr_from_ID = u.id
70 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
71 WHERE drl.to_id = ? AND dr.`message_processed` = 0
72 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL " . escape_limit($days_to_show) . " DAY)
73 ORDER BY `dr_message_due_date` ASC , `message_priority` ASC LIMIT 0," . escape_limit($alerts_to_show), array($userID));
75 // --------- loop through the results
76 for ($i = 0; $drRow = sqlFetchArray($drSQL); $i++) {
77 // --------- need to run patient query seperately to allow for reminders not linked to a patient
78 $pRow = array();
79 if ($drRow['pid'] > 0) {
80 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.pid = ?", array($drRow['pid']));
81 $pRow = sqlFetchArray($pSQL);
84 // --------- fill the $reminders array
85 $reminders[$i]['messageID'] = $drRow['dr_id'];
86 $reminders[$i]['PatientID'] = $drRow['pid'];
88 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
89 $reminders[$i]['PatientName'] = (empty($pRow) ? '' : $pRow['ptitle'] . ' ' . $pRow['pfname'] . ' ' . $pRow['pmname'] . ' ' . $pRow['plname']);
90 // -------------------------------------
92 $reminders[$i]['message'] = $drRow['dr_message_text'];
93 $reminders[$i]['dueDate'] = $drRow['dr_message_due_date'];
94 $reminders[$i]['fromName'] = $drRow['ffname'] . ' ' . $drRow['fmname'] . ' ' . $drRow['flname'];
96 // --------- if the message is due or overdue set $hasAlerts to true, this will stop autohiding of reminders
97 if (strtotime($drRow['dr_message_due_date']) <= $today) {
98 $hasAlerts = true;
102 // --------- END OF loop through the results
104 return $reminders;
107 // ------------------------------------------------
108 // @ END OF RemindersArray function
109 // ------------------------------------------------
113 * This function is used to get a count of the number of reminders due for a specified
114 * user.
116 * @param $days_to_show
117 * @param $today
118 * @param defaults to current user if none specified
119 * @returns int with number of due reminders for specified user
121 function GetDueReminderCount($days_to_show, $today, $userID = false)
123 if (!$userID) {
124 $userID = $_SESSION['authUserID'];
127 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
128 $drSQL = sqlStatement(
129 "SELECT count(dr.dr_id) c
130 FROM `dated_reminders` dr
131 JOIN `users` u ON dr.dr_from_ID = u.id
132 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
133 WHERE drl.to_id = ?
134 AND dr.`message_processed` = 0
135 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL " . escape_limit($days_to_show) . " DAY)",
136 array($userID)
139 $drRow = sqlFetchArray($drSQL);
140 return $drRow['c'];
143 // ------------------------------------------------
144 // @ END OF GetDueReminder function
145 // ------------------------------------------------
147 // ------------------------------------------------
148 // @ GetAllReminderCount function
149 // @ returns int with number of unprocessed reminders for specified user, defaults to current user if none specified
150 // ------------------------------------------------
151 function GetAllReminderCount($userID = false)
153 if (!$userID) {
154 $userID = $_SESSION['authUserID'];
157 // ----- sql statement for getting uncompleted reminders
158 $drSQL = sqlStatement(
159 "SELECT count(dr.dr_id) c
160 FROM `dated_reminders` dr
161 JOIN `users` u ON dr.dr_from_ID = u.id
162 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
163 WHERE drl.to_id = ?
164 AND dr.`message_processed` = 0",
165 array($userID)
168 $drRow = sqlFetchArray($drSQL);
169 return $drRow['c'];
172 // ------------------------------------------------
173 // @ END OF GetAllReminderCount function
174 // ------------------------------------------------
176 // ------------------------------------------------
177 // @ getRemindersHTML(array $reminders)
178 // @ returns HTML as a string, for printing
179 // ------------------------------------------------
180 function getRemindersHTML($today, $reminders = array())
182 global $hasAlerts;
183 // --- initialize the string as blank
184 $pdHTML = '';
185 // --- loop through the $reminders
186 foreach ($reminders as $r) {
187 // --- initialize $warning as the date, this is placed in front of the message
188 $warning = text($r['dueDate']);
189 // --- initialize $class as 'text dr', this is the basic class
190 $class = 'text dr';
192 // --------- check if reminder is overdue
193 if (strtotime($r['dueDate']) < $today) {
194 $warning = '<i class=\'fa fa-exclamation-triangle fa-lg text-danger\' aria-hidden=\'true\'></i> ' . xlt('OVERDUE');
195 //$class = 'bold alert dr';
196 $class = '';
197 } elseif (strtotime($r['dueDate']) == $today) {
198 // --------- check if reminder is due
199 $warning = '<i class=\'fa fa-exclamation-circle fa-lg\' style=\'color: var(--orange)\' aria-hidden=\'true\'></i> ' . xlt('TODAY');
200 $class = '';
201 } elseif (strtotime($r['dueDate']) > $today) {
202 $warning = '<i class=\'fa fa-exclamation-circle fa-lg text-success\' aria-hidden=\'true\'></i> ' . xlt('UPCOMING');
203 $class = '';
206 // end check if reminder is due or overdue
207 // apend to html string
208 $pdHTML .= '<p id="p_' . attr($r['messageID']) . '">
209 <a onclick="openAddScreen(' . attr(addslashes($r['messageID'])) . ')" class="dnForwarder btn btn-secondary btn-send-msg" id="' . attr($r['messageID']) . '" href="#"> ' . xlt('Forward') . ' </a>
210 <a class="dnRemover btn btn-secondary btn-save" onclick="updateme(' . "'" . attr(addslashes($r['messageID'])) . "'" . ')" id="' . attr($r['messageID']) . '" href="#">
211 <span>' . xlt('Set As Completed') . '</span>
212 </a>
213 <span title="' . ($r['PatientID'] > 0 ? xla('Click Patient Name to Open Patient File') : '') . '" class="' . attr($class) . '">' .
214 $warning . '
215 <span onclick="goPid(' . attr(addslashes($r['PatientID'])) . ')" class="patLink" id="' . attr($r['PatientID']) . '">' .
216 text($r['PatientName']) . '
217 </span> ' .
218 text($r['message']) . ' - [' . text($r['fromName']) . ']
219 </span>
220 </p>';
223 return ($pdHTML == '' ? '<i class=\'fa fa-exclamation-circle fa-lg text-success\' aria-hidden=\'true\'></i> ' . xlt('No Reminders') : $pdHTML);
226 // ------------------------------------------------
227 // @ END OF getRemindersHTML function
228 // ------------------------------------------------
231 // ------------------------------------------------
232 // @ setReminderAsProccessed(int $rID)
233 // @ marks reminder as processed
234 // ------------------------------------------------
235 function setReminderAsProcessed($rID, $userID = false)
237 if (!$userID) {
238 $userID = $_SESSION['authUserID'];
241 if (is_numeric($rID) and $rID > 0) {
242 // --- check if this user can remove this message
243 // --- need a better way of checking the current user, I don't like using $_SESSION for checks
244 $rdrSQL = sqlStatement("SELECT count(dr.dr_id) c FROM `dated_reminders` dr JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id WHERE drl.to_id = ? AND dr.`dr_id` = ? LIMIT 0,1", array($userID, $rID));
245 $rdrRow = sqlFetchArray($rdrSQL);
247 // --- if this user can delete this message (ie if it was sent to this user)
248 if ($rdrRow['c'] == 1) {
249 // ----- update the data, set the message to proccesses
250 sqlStatement("UPDATE `dated_reminders` SET `message_processed` = 1, `processed_date` = NOW(), `dr_processed_by` = ? WHERE `dr_id` = ? ", array(intval($userID), intval($rID)));
255 // ------------------------------------------------
256 // @ END OF setReminderAsProccessed function
257 // ------------------------------------------------
260 // ------------------------------------------------
261 // @ getReminderById(int $mID)
262 // @ returns an array with message details for forwarding
263 // ------------------------------------------------
264 function getReminderById($mID, $userID = false)
266 if (!$userID) {
267 $userID = $_SESSION['authUserID'];
270 $rdrSQL = sqlStatement("SELECT * FROM `dated_reminders` dr
271 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
272 WHERE drl.to_id = ? AND dr.`dr_id` = ? LIMIT 0,1", array($userID, $mID));
273 $rdrRow = sqlFetchArray($rdrSQL);
274 if (!empty($rdrRow)) {
275 return $rdrRow;
278 return false;
281 // ------------------------------------------------
282 // @ END OF getReminderById function
283 // ------------------------------------------------
286 // ------------------------------------------------
287 // @ getReminderById(
288 // array $sendTo
289 // int $fromID
290 // string $message
291 // date $dueDate
292 // int $patID
293 // int $priority
294 // )
295 // @ returns an array with message details for forwarding
296 // ------------------------------------------------
297 function sendReminder($sendTo, $fromID, $message, $dueDate, $patID, $priority)
299 if (
300 // ------- Should run data checks before running this function for more accurate error reporting
301 // ------- check sendTo is not empty
302 !empty($sendTo) and
303 // ------- check dueDate, only allow valid dates, todo -> enhance date checker
304 preg_match('/\d{4}[-]\d{2}[-]\d{2}/', $dueDate) and
305 // ------- check priority, only allow 1-3
306 intval($priority) <= 3 and
307 // ------- check message, only up to 255 characters
308 strlen($message) <= 255 and strlen($message) > 0 and
309 // ------- check if PatientID is set and in numeric
310 is_numeric($patID)
312 // ------- check for valid recipient
313 $cRow = sqlFetchArray(sqlStatement('SELECT count(id) FROM `users` WHERE `id` = ?', array($sendDMTo ?? '')));
314 if ($cRow == 0) {
315 return false;
318 // ------- if no errors
319 // --------- insert the new message
320 $mID = sqlInsert(
321 "INSERT INTO `dated_reminders`
322 (`dr_from_ID` ,`dr_message_text` ,`dr_message_sent_date` ,`dr_message_due_date` ,`pid` ,`message_priority` ,`message_processed` ,`processed_date`)
323 VALUES (?, ?, NOW( ), ?, ?, ?, '0', '');",
324 array($fromID, $message, $dueDate, $patID, $priority)
327 foreach ($sendTo as $st) {
328 sqlStatement(
329 "INSERT INTO `dated_reminders_link`
330 (`dr_id` ,`to_id`)
331 VALUES (?, ?);",
332 array($mID, $st)
335 return true;
336 } //---- end of if block
337 return false;
340 // ------- get current patient name
341 // ---- returns string, blank if no current patient
342 function getPatName($patientID)
344 $patientID = intval($patientID);
345 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.pid = ?", array($patientID));
346 $pRow = sqlFetchArray($pSQL);
347 return (empty($pRow) ? '' : $pRow['ptitle'] . ' ' . $pRow['pfname'] . ' ' . $pRow['pmname'] . ' ' . $pRow['plname']);
350 // -- log reminders array function uses $_GET to filter
351 function logRemindersArray()
354 // set blank array for data to be parsed to sql
355 $input = array();
356 // set blank string for the query
357 $where = '';
358 $sentBy = $_GET['sentBy'] ?? '';
359 $sentTo = $_GET['sentTo'] ?? '';
360 //------------------------------------------
361 // ----- HANDLE SENT BY FILTER
362 if (!empty($sentBy)) {
363 $sbCount = 0;
364 foreach ($sentBy as $sb) {
365 $where .= ($sbCount == 0 ? '(' : ' OR ') . 'dr.dr_from_ID = ? ';
366 $sbCount++;
367 $input[] = $sb;
370 $where .= ')';
373 //------------------------------------------
374 // ----- HANDLE SENT TO FILTER
375 if (!empty($sentTo)) {
376 $where = ($where == '' ? '' : $where . ' AND ');
377 $stCount = 0;
378 foreach ($sentTo as $st) {
379 $where .= ($stCount == 0 ? '(' : ' OR ') . 'drl.to_id = ? ';
380 $stCount++;
381 $input[] = $st;
384 $where .= ')';
387 //------------------------------------------
388 // ----- HANDLE PROCCESSED/PENDING FILTER ONLY RUN THIS IF BOTH ARE NOT SET
389 if (isset($_GET['processed']) and !isset($_GET['pending'])) {
390 $where = ($where == '' ? 'dr.message_processed = 1' : $where . ' AND dr.message_processed = 1');
391 } elseif (!isset($_GET['processed']) and isset($_GET['pending'])) {
392 $where = ($where == '' ? 'dr.message_processed = 0' : $where . ' AND dr.message_processed = 0');
395 //------------------------------------------
396 // ----- HANDLE DATE RANGE FILTERS
397 if (isset($_GET['sd']) and $_GET['sd'] != '') {
398 $where = ($where == '' ? 'dr.dr_message_sent_date >= ?' : $where . ' AND dr.dr_message_sent_date >= ?');
399 $input[] = DateToYYYYMMDD($_GET['sd']) . ' 00:00:00';
402 if (isset($_GET['ed']) and $_GET['ed'] != '') {
403 $where = ($where == '' ? 'dr.dr_message_sent_date <= ?' : $where . ' AND dr.dr_message_sent_date <= ?');
404 $input[] = DateToYYYYMMDD($_GET['ed']) . ' 23:59:59';
407 //------------------------------------------
410 //-------- add the "WHERE" the string if string is not blank, avoid sql errors for blannk WHERE statements
411 $where = ($where == '' ? '' : 'WHERE ' . $where);
413 // ----- define a blank reminders array
414 $reminders = array();
416 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
417 $drSQL = sqlStatement(
418 "SELECT
419 dr.pid, dr.dr_id, dr.dr_message_text, dr.dr_message_due_date dDate, dr.dr_message_sent_date sDate,dr.processed_date processedDate, dr.dr_processed_by,
420 u.fname ffname, u.mname fmname, u.lname flname,
421 tu.fname tfname, tu.mname tmname, tu.lname tlname
422 FROM `dated_reminders` dr
423 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
424 JOIN `users` u ON dr.dr_from_ID = u.id
425 JOIN `users` tu ON drl.to_id = tu.id
426 $where",
427 $input
429 // --------- loop through the results
430 for ($i = 0; $drRow = sqlFetchArray($drSQL); $i++) {
431 // --------- need to run patient query seperately to allow for messages not linked to a patient
432 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.pid = ?", array($drRow['pid']));
433 $pRow = sqlFetchArray($pSQL);
435 $prSQL = sqlStatement("SELECT u.fname pfname, u.mname pmname, u.lname plname FROM `users` u WHERE u.id = ?", array($drRow['dr_processed_by']));
436 $prRow = sqlFetchArray($prSQL);
438 // --------- fill the $reminders array
439 $reminders[$i]['messageID'] = $drRow['dr_id'];
440 $reminders[$i]['PatientID'] = $drRow['pid'];
442 $reminders[$i]['pDate'] = ($drRow['processedDate'] == '0000-00-00 00:00:00' ? 'N/A' : $drRow['processedDate']);
443 $reminders[$i]['sDate'] = $drRow['sDate'];
444 $reminders[$i]['dDate'] = $drRow['dDate'];
446 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
447 $reminders[$i]['PatientName'] = (empty($pRow) ? 'N/A' : $pRow['ptitle'] . ' ' . $pRow['pfname'] . ' ' . $pRow['pmname'] . ' ' . $pRow['plname']);
448 // -------------------------------------
450 $reminders[$i]['message'] = $drRow['dr_message_text'];
451 $reminders[$i]['fromName'] = $drRow['ffname'] . ' ' . $drRow['fmname'] . ' ' . $drRow['flname'];
452 $reminders[$i]['ToName'] = $drRow['tfname'] . ' ' . $drRow['tmname'] . ' ' . $drRow['tlname'];
453 $reminders[$i]['processedByName'] = (empty($prRow) ? 'N/A' : ($prRow['ptitle'] ?? '') . ' ' . $prRow['pfname'] . ' ' . $prRow['pmname'] . ' ' . $prRow['plname']);
456 // --------- END OF loop through the results
458 return $reminders;