using OemrUI class in Fees > Fee Sheet, Billing, Payment, Checkout, Batch Payments...
[openemr.git] / library / dated_reminder_functions.php
blob10f0585c13fe1b7640fa73aa3ab8cd0fb25ba26d
1 <?php
2 /**
3 * Contains functions used in the dated reminders.
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Craig Bezuidenhout <http://www.tajemo.co.za/>
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2012 tajemo.co.za <http://www.tajemo.co.za/>
10 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 /**
16 * Get Portal Alerts function
18 * @returns array of alerts count
20 function GetPortalAlertCounts()
22 $counts = array();
23 $s_user = '%' . $_SESSION['authUser'] . '%';
24 $s_user_id = $_SESSION['authUserID'];
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'] ? $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'] ? $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 $counts['total'] = $counts['mailCnt'] + $counts['auditCnt'] + $counts['chatCnt'];
42 return json_encode($counts);
45 /**
46 * RemindersArray function
48 * @returns array reminders for specified user, defaults to current user if none specified
50 function RemindersArray($days_to_show, $today, $alerts_to_show, $userID = false)
52 if (!$userID) {
53 $userID = $_SESSION['authId'];
56 global $hasAlerts;
57 // ----- define a blank reminders array
58 $reminders = array();
60 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
61 $drSQL = sqlStatement(
62 "SELECT
63 dr.pid, dr.dr_id, dr.dr_message_text,dr.dr_message_due_date,
64 u.fname ffname, u.mname fmname, u.lname flname
65 FROM `dated_reminders` dr
66 JOIN `users` u ON dr.dr_from_ID = u.id
67 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
68 WHERE drl.to_id = ?
69 AND dr.`message_processed` = 0
70 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL " . escape_limit($days_to_show) . " DAY)
71 ORDER BY `dr_message_due_date` ASC , `message_priority` ASC LIMIT 0," . escape_limit($alerts_to_show),
72 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['authId'];
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['authId'];
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\' style=\'color:red\' 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: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\' style=\'color:green\' 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-default btn-send-msg" id="' . attr($r['messageID']) . '" href="#"> ' . xlt('Forward') . ' </a>
210 <a class="dnRemover btn btn-default 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\' style=\'color:green\' 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['authId'];
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['authId'];
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 (// ------- Should run data checks before running this function for more accurate error reporting
300 // ------- check sendTo is not empty
301 !empty($sendTo) and
302 // ------- check dueDate, only allow valid dates, todo -> enhance date checker
303 preg_match('/\d{4}[-]\d{2}[-]\d{2}/', $dueDate) and
304 // ------- check priority, only allow 1-3
305 intval($priority) <= 3 and
306 // ------- check message, only up to 255 characters
307 strlen($message) <= 255 and strlen($message) > 0 and
308 // ------- check if PatientID is set and in numeric
309 is_numeric($patID)
311 // ------- check for valid recipient
312 $cRow = sqlFetchArray(sqlStatement('SELECT count(id) FROM `users` WHERE `id` = ?', array($sendDMTo)));
313 if ($cRow == 0) {
314 return false;
317 // ------- if no errors
318 // --------- insert the new message
319 $mID = sqlInsert(
320 "INSERT INTO `dated_reminders`
321 (`dr_from_ID` ,`dr_message_text` ,`dr_message_sent_date` ,`dr_message_due_date` ,`pid` ,`message_priority` ,`message_processed` ,`processed_date`)
322 VALUES (?, ?, NOW( ), ?, ?, ?, '0', '');",
323 array($fromID, $message, $dueDate, $patID, $priority)
326 foreach ($sendTo as $st) {
327 sqlInsert(
328 "INSERT INTO `dated_reminders_link`
329 (`dr_id` ,`to_id`)
330 VALUES (?, ?);",
331 array($mID, $st)
334 return true;
335 } //---- end of if block
336 return false;
339 // ------- get current patient name
340 // ---- returns string, blank if no current patient
341 function getPatName($patientID)
343 $patientID = intval($patientID);
344 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.pid = ?", array($patientID));
345 $pRow = sqlFetchArray($pSQL);
346 return (empty($pRow) ? '' : $pRow['ptitle'] . ' ' . $pRow['pfname'] . ' ' . $pRow['pmname'] . ' ' . $pRow['plname']);
349 // -- log reminders array function uses $_GET to filter
350 function logRemindersArray()
353 // set blank array for data to be parsed to sql
354 $input = array();
355 // set blank string for the query
356 $where = '';
357 $sentBy = $_GET['sentBy'];
358 $sentTo = $_GET['sentTo'];
359 //------------------------------------------
360 // ----- HANDLE SENT BY FILTER
361 if (!empty($sentBy)) {
362 $sbCount = 0;
363 foreach ($sentBy as $sb) {
364 $where .= ($sbCount == 0 ? '(' : ' OR ') . 'dr.dr_from_ID = ? ';
365 $sbCount++;
366 $input[] = $sb;
369 $where .= ')';
372 //------------------------------------------
373 // ----- HANDLE SENT TO FILTER
374 if (!empty($sentTo)) {
375 $where = ($where == '' ? '' : $where . ' AND ');
376 $stCount = 0;
377 foreach ($sentTo as $st) {
378 $where .= ($stCount == 0 ? '(' : ' OR ') . 'drl.to_id = ? ';
379 $stCount++;
380 $input[] = $st;
383 $where .= ')';
386 //------------------------------------------
387 // ----- HANDLE PROCCESSED/PENDING FILTER ONLY RUN THIS IF BOTH ARE NOT SET
388 if (isset($_GET['processed']) and !isset($_GET['pending'])) {
389 $where = ($where == '' ? 'dr.message_processed = 1' : $where . ' AND dr.message_processed = 1');
390 } elseif (!isset($_GET['processed']) and isset($_GET['pending'])) {
391 $where = ($where == '' ? 'dr.message_processed = 0' : $where . ' AND dr.message_processed = 0');
394 //------------------------------------------
395 // ----- HANDLE DATE RANGE FILTERS
396 if (isset($_GET['sd']) and $_GET['sd'] != '') {
397 $where = ($where == '' ? 'dr.dr_message_sent_date >= ?' : $where . ' AND dr.dr_message_sent_date >= ?');
398 $input[] = $_GET['sd'] . ' 00:00:00';
401 if (isset($_GET['ed']) and $_GET['ed'] != '') {
402 $where = ($where == '' ? 'dr.dr_message_sent_date <= ?' : $where . ' AND dr.dr_message_sent_date <= ?');
403 $input[] = $_GET['ed'] . ' 23:59:59';
406 //------------------------------------------
409 //-------- add the "WHERE" the string if string is not blank, avoid sql errors for blannk WHERE statements
410 $where = ($where == '' ? '' : 'WHERE ' . $where);
412 // ----- define a blank reminders array
413 $reminders = array();
415 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
416 $drSQL = sqlStatement(
417 "SELECT
418 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,
419 u.fname ffname, u.mname fmname, u.lname flname,
420 tu.fname tfname, tu.mname tmname, tu.lname tlname
421 FROM `dated_reminders` dr
422 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
423 JOIN `users` u ON dr.dr_from_ID = u.id
424 JOIN `users` tu ON drl.to_id = tu.id
425 $where",
426 $input
428 // --------- loop through the results
429 for ($i = 0; $drRow = sqlFetchArray($drSQL); $i++) {
430 // --------- need to run patient query seperately to allow for messages not linked to a patient
431 $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']));
432 $pRow = sqlFetchArray($pSQL);
434 $prSQL = sqlStatement("SELECT u.fname pfname, u.mname pmname, u.lname plname FROM `users` u WHERE u.id = ?", array($drRow['dr_processed_by']));
435 $prRow = sqlFetchArray($prSQL);
437 // --------- fill the $reminders array
438 $reminders[$i]['messageID'] = $drRow['dr_id'];
439 $reminders[$i]['PatientID'] = $drRow['pid'];
441 $reminders[$i]['pDate'] = ($drRow['processedDate'] == '0000-00-00 00:00:00' ? 'N/A' : $drRow['processedDate']);
442 $reminders[$i]['sDate'] = $drRow['sDate'];
443 $reminders[$i]['dDate'] = $drRow['dDate'];
445 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
446 $reminders[$i]['PatientName'] = (empty($pRow) ? 'N/A' : $pRow['ptitle'] . ' ' . $pRow['pfname'] . ' ' . $pRow['pmname'] . ' ' . $pRow['plname']);
447 // -------------------------------------
449 $reminders[$i]['message'] = $drRow['dr_message_text'];
450 $reminders[$i]['fromName'] = $drRow['ffname'] . ' ' . $drRow['fmname'] . ' ' . $drRow['flname'];
451 $reminders[$i]['ToName'] = $drRow['tfname'] . ' ' . $drRow['tmname'] . ' ' . $drRow['tlname'];
452 $reminders[$i]['processedByName'] = (empty($prRow) ? 'N/A' : $prRow['ptitle'] . ' ' . $prRow['pfname'] . ' ' . $prRow['pmname'] . ' ' . $prRow['plname']);
455 // --------- END OF loop through the results
457 return $reminders;