Fully responsive globals.php with vertical menu (#2460)
[openemr.git] / library / dated_reminder_functions.php
blob8fb561ce65c188da8b24fee35e3a46d80acb312e
1 <?php
2 /**
3 * Contains functions used in the dated reminders.
5 * @package OpenEMR
6 * @link https://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 $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'] ? $qrtn['count_payments'] : "0";
46 $counts['total'] = $counts['mailCnt'] + $counts['auditCnt'] + $counts['chatCnt'] + $counts['paymentCnt'];
47 return json_encode($counts);
50 /**
51 * RemindersArray function
53 * @returns array reminders for specified user, defaults to current user if none specified
55 function RemindersArray($days_to_show, $today, $alerts_to_show, $userID = false)
57 if (!$userID) {
58 $userID = $_SESSION['authId'];
61 global $hasAlerts;
62 // ----- define a blank reminders array
63 $reminders = array();
65 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
66 $drSQL = sqlStatement(
67 "SELECT
68 dr.pid, dr.dr_id, dr.dr_message_text,dr.dr_message_due_date,
69 u.fname ffname, u.mname fmname, u.lname flname
70 FROM `dated_reminders` dr
71 JOIN `users` u ON dr.dr_from_ID = u.id
72 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
73 WHERE drl.to_id = ?
74 AND dr.`message_processed` = 0
75 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL " . escape_limit($days_to_show) . " DAY)
76 ORDER BY `dr_message_due_date` ASC , `message_priority` ASC LIMIT 0," . escape_limit($alerts_to_show),
77 array($userID)
80 // --------- loop through the results
81 for ($i = 0; $drRow = sqlFetchArray($drSQL); $i++) {
82 // --------- need to run patient query seperately to allow for reminders not linked to a patient
83 $pRow = array();
84 if ($drRow['pid'] > 0) {
85 $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']));
86 $pRow = sqlFetchArray($pSQL);
89 // --------- fill the $reminders array
90 $reminders[$i]['messageID'] = $drRow['dr_id'];
91 $reminders[$i]['PatientID'] = $drRow['pid'];
93 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
94 $reminders[$i]['PatientName'] = (empty($pRow) ? '' : $pRow['ptitle'] . ' ' . $pRow['pfname'] . ' ' . $pRow['pmname'] . ' ' . $pRow['plname']);
95 // -------------------------------------
97 $reminders[$i]['message'] = $drRow['dr_message_text'];
98 $reminders[$i]['dueDate'] = $drRow['dr_message_due_date'];
99 $reminders[$i]['fromName'] = $drRow['ffname'] . ' ' . $drRow['fmname'] . ' ' . $drRow['flname'];
101 // --------- if the message is due or overdue set $hasAlerts to true, this will stop autohiding of reminders
102 if (strtotime($drRow['dr_message_due_date']) <= $today) {
103 $hasAlerts = true;
107 // --------- END OF loop through the results
109 return $reminders;
112 // ------------------------------------------------
113 // @ END OF RemindersArray function
114 // ------------------------------------------------
118 * This function is used to get a count of the number of reminders due for a specified
119 * user.
121 * @param $days_to_show
122 * @param $today
123 * @param defaults to current user if none specified
124 * @returns int with number of due reminders for specified user
126 function GetDueReminderCount($days_to_show, $today, $userID = false)
128 if (!$userID) {
129 $userID = $_SESSION['authId'];
132 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
133 $drSQL = sqlStatement(
134 "SELECT count(dr.dr_id) c
135 FROM `dated_reminders` dr
136 JOIN `users` u ON dr.dr_from_ID = u.id
137 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
138 WHERE drl.to_id = ?
139 AND dr.`message_processed` = 0
140 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL " . escape_limit($days_to_show) . " DAY)",
141 array($userID)
144 $drRow = sqlFetchArray($drSQL);
145 return $drRow['c'];
148 // ------------------------------------------------
149 // @ END OF GetDueReminder function
150 // ------------------------------------------------
152 // ------------------------------------------------
153 // @ GetAllReminderCount function
154 // @ returns int with number of unprocessed reminders for specified user, defaults to current user if none specified
155 // ------------------------------------------------
156 function GetAllReminderCount($userID = false)
158 if (!$userID) {
159 $userID = $_SESSION['authId'];
162 // ----- sql statement for getting uncompleted reminders
163 $drSQL = sqlStatement(
164 "SELECT count(dr.dr_id) c
165 FROM `dated_reminders` dr
166 JOIN `users` u ON dr.dr_from_ID = u.id
167 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
168 WHERE drl.to_id = ?
169 AND dr.`message_processed` = 0",
170 array($userID)
173 $drRow = sqlFetchArray($drSQL);
174 return $drRow['c'];
177 // ------------------------------------------------
178 // @ END OF GetAllReminderCount function
179 // ------------------------------------------------
181 // ------------------------------------------------
182 // @ getRemindersHTML(array $reminders)
183 // @ returns HTML as a string, for printing
184 // ------------------------------------------------
185 function getRemindersHTML($today, $reminders = array())
187 global $hasAlerts;
188 // --- initialize the string as blank
189 $pdHTML = '';
190 // --- loop through the $reminders
191 foreach ($reminders as $r) {
192 // --- initialize $warning as the date, this is placed in front of the message
193 $warning = text($r['dueDate']);
194 // --- initialize $class as 'text dr', this is the basic class
195 $class = 'text dr';
197 // --------- check if reminder is overdue
198 if (strtotime($r['dueDate']) < $today) {
199 $warning = '<i class=\'fa fa-exclamation-triangle fa-lg\' style=\'color:red\' aria-hidden=\'true\'></i> ' . xlt('OVERDUE');
200 //$class = 'bold alert dr';
201 $class = '';
202 } elseif (strtotime($r['dueDate']) == $today) {
203 // --------- check if reminder is due
204 $warning = '<i class=\'fa fa-exclamation-circle fa-lg\' style=\'color:orange\' aria-hidden=\'true\'></i> ' . xlt('TODAY');
205 $class = '';
206 } elseif (strtotime($r['dueDate']) > $today) {
207 $warning = '<i class=\'fa fa-exclamation-circle fa-lg\' style=\'color:green\' aria-hidden=\'true\'></i> ' . xlt('UPCOMING');
208 $class = '';
211 // end check if reminder is due or overdue
212 // apend to html string
213 $pdHTML .= '<p id="p_' . attr($r['messageID']) . '">
214 <a onclick="openAddScreen(' . attr(addslashes($r['messageID'])) . ')" class="dnForwarder btn btn-default btn-send-msg" id="' . attr($r['messageID']) . '" href="#"> ' . xlt('Forward') . ' </a>
215 <a class="dnRemover btn btn-default btn-save" onclick="updateme(' . "'" . attr(addslashes($r['messageID'])) . "'" . ')" id="' . attr($r['messageID']) . '" href="#">
216 <span>' . xlt('Set As Completed') . '</span>
217 </a>
218 <span title="' . ($r['PatientID'] > 0 ? xla('Click Patient Name to Open Patient File') : '') . '" class="' . attr($class) . '">' .
219 $warning . '
220 <span onclick="goPid(' . attr(addslashes($r['PatientID'])) . ')" class="patLink" id="' . attr($r['PatientID']) . '">' .
221 text($r['PatientName']) . '
222 </span> ' .
223 text($r['message']) . ' - [' . text($r['fromName']) . ']
224 </span>
225 </p>';
228 return ($pdHTML == '' ? '<i class=\'fa fa-exclamation-circle fa-lg\' style=\'color:green\' aria-hidden=\'true\'></i> ' . xlt('No Reminders') : $pdHTML);
231 // ------------------------------------------------
232 // @ END OF getRemindersHTML function
233 // ------------------------------------------------
236 // ------------------------------------------------
237 // @ setReminderAsProccessed(int $rID)
238 // @ marks reminder as processed
239 // ------------------------------------------------
240 function setReminderAsProcessed($rID, $userID = false)
242 if (!$userID) {
243 $userID = $_SESSION['authId'];
246 if (is_numeric($rID) and $rID > 0) {
247 // --- check if this user can remove this message
248 // --- need a better way of checking the current user, I don't like using $_SESSION for checks
249 $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));
250 $rdrRow = sqlFetchArray($rdrSQL);
252 // --- if this user can delete this message (ie if it was sent to this user)
253 if ($rdrRow['c'] == 1) {
254 // ----- update the data, set the message to proccesses
255 sqlStatement("UPDATE `dated_reminders` SET `message_processed` = 1, `processed_date` = NOW(), `dr_processed_by` = ? WHERE `dr_id` = ? ", array(intval($userID), intval($rID)));
260 // ------------------------------------------------
261 // @ END OF setReminderAsProccessed function
262 // ------------------------------------------------
265 // ------------------------------------------------
266 // @ getReminderById(int $mID)
267 // @ returns an array with message details for forwarding
268 // ------------------------------------------------
269 function getReminderById($mID, $userID = false)
271 if (!$userID) {
272 $userID = $_SESSION['authId'];
275 $rdrSQL = sqlStatement("SELECT * FROM `dated_reminders` dr
276 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
277 WHERE drl.to_id = ? AND dr.`dr_id` = ? LIMIT 0,1", array($userID, $mID));
278 $rdrRow = sqlFetchArray($rdrSQL);
279 if (!empty($rdrRow)) {
280 return $rdrRow;
283 return false;
286 // ------------------------------------------------
287 // @ END OF getReminderById function
288 // ------------------------------------------------
291 // ------------------------------------------------
292 // @ getReminderById(
293 // array $sendTo
294 // int $fromID
295 // string $message
296 // date $dueDate
297 // int $patID
298 // int $priority
299 // )
300 // @ returns an array with message details for forwarding
301 // ------------------------------------------------
302 function sendReminder($sendTo, $fromID, $message, $dueDate, $patID, $priority)
304 if (// ------- Should run data checks before running this function for more accurate error reporting
305 // ------- check sendTo is not empty
306 !empty($sendTo) and
307 // ------- check dueDate, only allow valid dates, todo -> enhance date checker
308 preg_match('/\d{4}[-]\d{2}[-]\d{2}/', $dueDate) and
309 // ------- check priority, only allow 1-3
310 intval($priority) <= 3 and
311 // ------- check message, only up to 255 characters
312 strlen($message) <= 255 and strlen($message) > 0 and
313 // ------- check if PatientID is set and in numeric
314 is_numeric($patID)
316 // ------- check for valid recipient
317 $cRow = sqlFetchArray(sqlStatement('SELECT count(id) FROM `users` WHERE `id` = ?', array($sendDMTo)));
318 if ($cRow == 0) {
319 return false;
322 // ------- if no errors
323 // --------- insert the new message
324 $mID = sqlInsert(
325 "INSERT INTO `dated_reminders`
326 (`dr_from_ID` ,`dr_message_text` ,`dr_message_sent_date` ,`dr_message_due_date` ,`pid` ,`message_priority` ,`message_processed` ,`processed_date`)
327 VALUES (?, ?, NOW( ), ?, ?, ?, '0', '');",
328 array($fromID, $message, $dueDate, $patID, $priority)
331 foreach ($sendTo as $st) {
332 sqlStatement(
333 "INSERT INTO `dated_reminders_link`
334 (`dr_id` ,`to_id`)
335 VALUES (?, ?);",
336 array($mID, $st)
339 return true;
340 } //---- end of if block
341 return false;
344 // ------- get current patient name
345 // ---- returns string, blank if no current patient
346 function getPatName($patientID)
348 $patientID = intval($patientID);
349 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.pid = ?", array($patientID));
350 $pRow = sqlFetchArray($pSQL);
351 return (empty($pRow) ? '' : $pRow['ptitle'] . ' ' . $pRow['pfname'] . ' ' . $pRow['pmname'] . ' ' . $pRow['plname']);
354 // -- log reminders array function uses $_GET to filter
355 function logRemindersArray()
358 // set blank array for data to be parsed to sql
359 $input = array();
360 // set blank string for the query
361 $where = '';
362 $sentBy = $_GET['sentBy'];
363 $sentTo = $_GET['sentTo'];
364 //------------------------------------------
365 // ----- HANDLE SENT BY FILTER
366 if (!empty($sentBy)) {
367 $sbCount = 0;
368 foreach ($sentBy as $sb) {
369 $where .= ($sbCount == 0 ? '(' : ' OR ') . 'dr.dr_from_ID = ? ';
370 $sbCount++;
371 $input[] = $sb;
374 $where .= ')';
377 //------------------------------------------
378 // ----- HANDLE SENT TO FILTER
379 if (!empty($sentTo)) {
380 $where = ($where == '' ? '' : $where . ' AND ');
381 $stCount = 0;
382 foreach ($sentTo as $st) {
383 $where .= ($stCount == 0 ? '(' : ' OR ') . 'drl.to_id = ? ';
384 $stCount++;
385 $input[] = $st;
388 $where .= ')';
391 //------------------------------------------
392 // ----- HANDLE PROCCESSED/PENDING FILTER ONLY RUN THIS IF BOTH ARE NOT SET
393 if (isset($_GET['processed']) and !isset($_GET['pending'])) {
394 $where = ($where == '' ? 'dr.message_processed = 1' : $where . ' AND dr.message_processed = 1');
395 } elseif (!isset($_GET['processed']) and isset($_GET['pending'])) {
396 $where = ($where == '' ? 'dr.message_processed = 0' : $where . ' AND dr.message_processed = 0');
399 //------------------------------------------
400 // ----- HANDLE DATE RANGE FILTERS
401 if (isset($_GET['sd']) and $_GET['sd'] != '') {
402 $where = ($where == '' ? 'dr.dr_message_sent_date >= ?' : $where . ' AND dr.dr_message_sent_date >= ?');
403 $input[] = $_GET['sd'] . ' 00:00:00';
406 if (isset($_GET['ed']) and $_GET['ed'] != '') {
407 $where = ($where == '' ? 'dr.dr_message_sent_date <= ?' : $where . ' AND dr.dr_message_sent_date <= ?');
408 $input[] = $_GET['ed'] . ' 23:59:59';
411 //------------------------------------------
414 //-------- add the "WHERE" the string if string is not blank, avoid sql errors for blannk WHERE statements
415 $where = ($where == '' ? '' : 'WHERE ' . $where);
417 // ----- define a blank reminders array
418 $reminders = array();
420 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
421 $drSQL = sqlStatement(
422 "SELECT
423 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,
424 u.fname ffname, u.mname fmname, u.lname flname,
425 tu.fname tfname, tu.mname tmname, tu.lname tlname
426 FROM `dated_reminders` dr
427 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
428 JOIN `users` u ON dr.dr_from_ID = u.id
429 JOIN `users` tu ON drl.to_id = tu.id
430 $where",
431 $input
433 // --------- loop through the results
434 for ($i = 0; $drRow = sqlFetchArray($drSQL); $i++) {
435 // --------- need to run patient query seperately to allow for messages not linked to a patient
436 $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']));
437 $pRow = sqlFetchArray($pSQL);
439 $prSQL = sqlStatement("SELECT u.fname pfname, u.mname pmname, u.lname plname FROM `users` u WHERE u.id = ?", array($drRow['dr_processed_by']));
440 $prRow = sqlFetchArray($prSQL);
442 // --------- fill the $reminders array
443 $reminders[$i]['messageID'] = $drRow['dr_id'];
444 $reminders[$i]['PatientID'] = $drRow['pid'];
446 $reminders[$i]['pDate'] = ($drRow['processedDate'] == '0000-00-00 00:00:00' ? 'N/A' : $drRow['processedDate']);
447 $reminders[$i]['sDate'] = $drRow['sDate'];
448 $reminders[$i]['dDate'] = $drRow['dDate'];
450 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
451 $reminders[$i]['PatientName'] = (empty($pRow) ? 'N/A' : $pRow['ptitle'] . ' ' . $pRow['pfname'] . ' ' . $pRow['pmname'] . ' ' . $pRow['plname']);
452 // -------------------------------------
454 $reminders[$i]['message'] = $drRow['dr_message_text'];
455 $reminders[$i]['fromName'] = $drRow['ffname'] . ' ' . $drRow['fmname'] . ' ' . $drRow['flname'];
456 $reminders[$i]['ToName'] = $drRow['tfname'] . ' ' . $drRow['tmname'] . ' ' . $drRow['tlname'];
457 $reminders[$i]['processedByName'] = (empty($prRow) ? 'N/A' : $prRow['ptitle'] . ' ' . $prRow['pfname'] . ' ' . $prRow['pmname'] . ' ' . $prRow['plname']);
460 // --------- END OF loop through the results
462 return $reminders;