3 * Contains functions used in the dated reminders.
5 * Copyright (C) 2012 tajemo.co.za <http://www.tajemo.co.za/>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
19 * @author Craig Bezuidenhout <http://www.tajemo.co.za/>
20 * @link http://www.open-emr.org
24 * Get Portal Alerts function
26 * @returns array of alerts count
28 function GetPortalAlertCounts()
31 $s_user = '%' . $_SESSION['authUser'] . '%';
32 $s_user_id = $_SESSION['authUserID'];
34 $query = "SELECT Count(`m`.message_status) AS count_mail FROM onsite_mail `m` " .
35 "WHERE `m`.owner LIKE ? AND `m`.recipient_id LIKE ? AND `m`.message_status LIKE ? AND `m`.deleted = 0";
36 $qrtn = sqlQueryNoLog($query, array($s_user, $s_user, '%new%'));
37 $counts['mailCnt'] = $qrtn['count_mail'] ?
$qrtn['count_mail'] : "0";
39 $query = "SELECT Count(`m`.status) AS count_audits FROM onsite_portal_activity `m` " .
40 "WHERE `m`.status LIKE ?";
41 $qrtn = sqlQueryNoLog($query, array('%waiting%'));
42 $counts['auditCnt'] = $qrtn['count_audits'] ?
$qrtn['count_audits'] : "0";
44 $query = "SELECT Count(`m`.id) AS count_chats FROM onsite_messages `m` " .
45 "WHERE `m`.recip_id LIKE ? AND `m`.date > (CURRENT_DATE()-2) AND `m`.date < (CURRENT_DATE()+1)";
46 $qrtn = sqlQueryNoLog($query, array($s_user));
47 $counts['chatCnt'] = $qrtn['count_chats'] ?
$qrtn['count_chats'] : "0";
49 $counts['total'] = $counts['mailCnt'] +
$counts['auditCnt'] +
$counts['chatCnt'];
50 return json_encode($counts);
54 * RemindersArray function
56 * @returns array reminders for specified user, defaults to current user if none specified
58 function RemindersArray($days_to_show, $today, $alerts_to_show, $userID = false)
61 $userID = $_SESSION['authId'];
65 // ----- define a blank reminders array
68 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
69 $drSQL = sqlStatement(
71 dr.pid, dr.dr_id, dr.dr_message_text,dr.dr_message_due_date,
72 u.fname ffname, u.mname fmname, u.lname flname
73 FROM `dated_reminders` dr
74 JOIN `users` u ON dr.dr_from_ID = u.id
75 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
77 AND dr.`message_processed` = 0
78 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL $days_to_show DAY)
79 ORDER BY `dr_message_due_date` ASC , `message_priority` ASC LIMIT 0,$alerts_to_show",
83 // --------- loop through the results
84 for ($i = 0; $drRow = sqlFetchArray($drSQL); $i++
) {
85 // --------- need to run patient query seperately to allow for reminders not linked to a patient
87 if ($drRow['pid'] > 0) {
88 $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']));
89 $pRow = sqlFetchArray($pSQL);
92 // --------- fill the $reminders array
93 $reminders[$i]['messageID'] = $drRow['dr_id'];
94 $reminders[$i]['PatientID'] = $drRow['pid'];
96 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
97 $reminders[$i]['PatientName'] = (empty($pRow) ?
'' : $pRow['ptitle'] . ' ' . $pRow['pfname'] . ' ' . $pRow['pmname'] . ' ' . $pRow['plname']);
98 // -------------------------------------
100 $reminders[$i]['message'] = $drRow['dr_message_text'];
101 $reminders[$i]['dueDate'] = $drRow['dr_message_due_date'];
102 $reminders[$i]['fromName'] = $drRow['ffname'] . ' ' . $drRow['fmname'] . ' ' . $drRow['flname'];
104 // --------- if the message is due or overdue set $hasAlerts to true, this will stop autohiding of reminders
105 if (strtotime($drRow['dr_message_due_date']) <= $today) {
110 // --------- END OF loop through the results
115 // ------------------------------------------------
116 // @ END OF RemindersArray function
117 // ------------------------------------------------
121 * This function is used to get a count of the number of reminders due for a specified
124 * @param $days_to_show
126 * @param defaults to current user if none specified
127 * @returns int with number of due reminders for specified user
129 function GetDueReminderCount($days_to_show, $today, $userID = false)
132 $userID = $_SESSION['authId'];
135 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
136 $drSQL = sqlStatement(
137 "SELECT count(dr.dr_id) c
138 FROM `dated_reminders` dr
139 JOIN `users` u ON dr.dr_from_ID = u.id
140 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
142 AND dr.`message_processed` = 0
143 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL $days_to_show DAY)",
147 $drRow = sqlFetchArray($drSQL);
151 // ------------------------------------------------
152 // @ END OF GetDueReminder function
153 // ------------------------------------------------
155 // ------------------------------------------------
156 // @ GetAllReminderCount function
157 // @ returns int with number of unprocessed reminders for specified user, defaults to current user if none specified
158 // ------------------------------------------------
159 function GetAllReminderCount($userID = false)
162 $userID = $_SESSION['authId'];
165 // ----- sql statement for getting uncompleted reminders
166 $drSQL = sqlStatement(
167 "SELECT count(dr.dr_id) c
168 FROM `dated_reminders` dr
169 JOIN `users` u ON dr.dr_from_ID = u.id
170 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
172 AND dr.`message_processed` = 0",
176 $drRow = sqlFetchArray($drSQL);
180 // ------------------------------------------------
181 // @ END OF GetAllReminderCount function
182 // ------------------------------------------------
184 // ------------------------------------------------
185 // @ getRemindersHTML(array $reminders)
186 // @ returns HTML as a string, for printing
187 // ------------------------------------------------
188 function getRemindersHTML($today, $reminders = array())
191 // --- initialize the string as blank
193 // --- loop through the $reminders
194 foreach ($reminders as $r) {
195 // --- initialize $warning as the date, this is placed in front of the message
196 $warning = text($r['dueDate']);
197 // --- initialize $class as 'text dr', this is the basic class
200 // --------- check if reminder is overdue
201 if (strtotime($r['dueDate']) < $today) {
202 $warning = '<i class=\'fa fa-exclamation-triangle fa-lg\' style=\'color:red\' aria-hidden=\'true\'></i> ' . xlt('OVERDUE');
203 //$class = 'bold alert dr';
205 } elseif (strtotime($r['dueDate']) == $today) {
206 // --------- check if reminder is due
207 $warning = '<i class=\'fa fa-exclamation-circle fa-lg\' style=\'color:orange\' aria-hidden=\'true\'></i> ' . xlt('TODAY');
209 } elseif (strtotime($r['dueDate']) > $today) {
210 $warning = '<i class=\'fa fa-exclamation-circle fa-lg\' style=\'color:green\' aria-hidden=\'true\'></i> ' . xlt('UPCOMING');
214 // end check if reminder is due or overdue
215 // apend to html string
216 $pdHTML .= '<p id="p_' . attr($r['messageID']) . '">
217 <a onclick="openAddScreen(' . attr($r['messageID']) . ')" class="dnForwarder btn btn-default btn-send-msg" id="' . attr($r['messageID']) . '" href="#"> ' . xlt('Forward') . ' </a>
218 <a class="dnRemover btn btn-default btn-save" onclick="updateme(' . "'" . attr($r['messageID']) . "'" . ')" id="' . attr($r['messageID']) . '" href="#">
219 <span>' . xlt('Set As Completed') . '</span>
221 <span title="' . ($r['PatientID'] > 0 ?
xla('Click Patient Name to Open Patient File') : '') . '" class="' . attr($class) . '">' .
223 <span onclick="goPid(' . attr($r['PatientID']) . ')" class="patLink" id="' . attr($r['PatientID']) . '">' .
224 text($r['PatientName']) . '
226 text($r['message']) . ' - [' . text($r['fromName']) . ']
231 return ($pdHTML == '' ?
'<i class=\'fa fa-exclamation-circle fa-lg\' style=\'color:green\' aria-hidden=\'true\'></i> ' . xlt('No Reminders') : $pdHTML);
234 // ------------------------------------------------
235 // @ END OF getRemindersHTML function
236 // ------------------------------------------------
239 // ------------------------------------------------
240 // @ setReminderAsProccessed(int $rID)
241 // @ marks reminder as processed
242 // ------------------------------------------------
243 function setReminderAsProcessed($rID, $userID = false)
246 $userID = $_SESSION['authId'];
249 if (is_numeric($rID) and $rID > 0) {
250 // --- check if this user can remove this message
251 // --- need a better way of checking the current user, I don't like using $_SESSION for checks
252 $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));
253 $rdrRow = sqlFetchArray($rdrSQL);
255 // --- if this user can delete this message (ie if it was sent to this user)
256 if ($rdrRow['c'] == 1) {
257 // ----- update the data, set the message to proccesses
258 sqlStatement("UPDATE `dated_reminders` SET `message_processed` = 1, `processed_date` = NOW(), `dr_processed_by` = ? WHERE `dr_id` = ? ", array(intval($userID), intval($rID)));
263 // ------------------------------------------------
264 // @ END OF setReminderAsProccessed function
265 // ------------------------------------------------
268 // ------------------------------------------------
269 // @ getReminderById(int $mID)
270 // @ returns an array with message details for forwarding
271 // ------------------------------------------------
272 function getReminderById($mID, $userID = false)
275 $userID = $_SESSION['authId'];
278 $rdrSQL = sqlStatement("SELECT * FROM `dated_reminders` dr
279 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
280 WHERE drl.to_id = ? AND dr.`dr_id` = ? LIMIT 0,1", array($userID, $mID));
281 $rdrRow = sqlFetchArray($rdrSQL);
282 if (!empty($rdrRow)) {
289 // ------------------------------------------------
290 // @ END OF getReminderById function
291 // ------------------------------------------------
294 // ------------------------------------------------
295 // @ getReminderById(
303 // @ returns an array with message details for forwarding
304 // ------------------------------------------------
305 function sendReminder($sendTo, $fromID, $message, $dueDate, $patID, $priority)
307 if (// ------- Should run data checks before running this function for more accurate error reporting
308 // ------- check sendTo is not empty
310 // ------- check dueDate, only allow valid dates, todo -> enhance date checker
311 preg_match('/\d{4}[-]\d{2}[-]\d{2}/', $dueDate) and
312 // ------- check priority, only allow 1-3
313 intval($priority) <= 3 and
314 // ------- check message, only up to 255 characters
315 strlen($message) <= 255 and strlen($message) > 0 and
316 // ------- check if PatientID is set and in numeric
319 // ------- check for valid recipient
320 $cRow = sqlFetchArray(sqlStatement('SELECT count(id) FROM `users` WHERE `id` = ?', array($sendDMTo)));
325 // ------- if no errors
326 // --------- insert the new message
328 "INSERT INTO `dated_reminders`
329 (`dr_from_ID` ,`dr_message_text` ,`dr_message_sent_date` ,`dr_message_due_date` ,`pid` ,`message_priority` ,`message_processed` ,`processed_date`)
330 VALUES (?, ?, NOW( ), ?, ?, ?, '0', '');",
331 array($fromID, $message, $dueDate, $patID, $priority)
334 foreach ($sendTo as $st) {
336 "INSERT INTO `dated_reminders_link`
343 } //---- end of if block
347 // ------- get current patient name
348 // ---- returns string, blank if no current patient
349 function getPatName($patientID)
351 $patientID = intval($patientID);
352 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.pid = ?", array($patientID));
353 $pRow = sqlFetchArray($pSQL);
354 return (empty($pRow) ?
'' : $pRow['ptitle'] . ' ' . $pRow['pfname'] . ' ' . $pRow['pmname'] . ' ' . $pRow['plname']);
357 // -- log reminders array function uses $_GET to filter
358 function logRemindersArray()
361 // set blank array for data to be parsed to sql
363 // set blank string for the query
365 $sentBy = $_GET['sentBy'];
366 $sentTo = $_GET['sentTo'];
367 //------------------------------------------
368 // ----- HANDLE SENT BY FILTER
369 if (!empty($sentBy)) {
371 foreach ($sentBy as $sb) {
372 $where .= ($sbCount == 0 ?
'(' : ' OR ') . 'dr.dr_from_ID = ? ';
380 //------------------------------------------
381 // ----- HANDLE SENT TO FILTER
382 if (!empty($sentTo)) {
383 $where = ($where == '' ?
'' : $where . ' AND ');
385 foreach ($sentTo as $st) {
386 $where .= ($stCount == 0 ?
'(' : ' OR ') . 'drl.to_id = ? ';
394 //------------------------------------------
395 // ----- HANDLE PROCCESSED/PENDING FILTER ONLY RUN THIS IF BOTH ARE NOT SET
396 if (isset($_GET['processed']) and !isset($_GET['pending'])) {
397 $where = ($where == '' ?
'dr.message_processed = 1' : $where . ' AND dr.message_processed = 1');
398 } elseif (!isset($_GET['processed']) and isset($_GET['pending'])) {
399 $where = ($where == '' ?
'dr.message_processed = 0' : $where . ' AND dr.message_processed = 0');
402 //------------------------------------------
403 // ----- HANDLE DATE RANGE FILTERS
404 if (isset($_GET['sd']) and $_GET['sd'] != '') {
405 $where = ($where == '' ?
'dr.dr_message_sent_date >= ?' : $where . ' AND dr.dr_message_sent_date >= ?');
406 $input[] = $_GET['sd'] . ' 00:00:00';
409 if (isset($_GET['ed']) and $_GET['ed'] != '') {
410 $where = ($where == '' ?
'dr.dr_message_sent_date <= ?' : $where . ' AND dr.dr_message_sent_date <= ?');
411 $input[] = $_GET['ed'] . ' 23:59:59';
414 //------------------------------------------
417 //-------- add the "WHERE" the string if string is not blank, avoid sql errors for blannk WHERE statements
418 $where = ($where == '' ?
'' : 'WHERE ' . $where);
420 // ----- define a blank reminders array
421 $reminders = array();
423 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
424 $drSQL = sqlStatement(
426 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,
427 u.fname ffname, u.mname fmname, u.lname flname,
428 tu.fname tfname, tu.mname tmname, tu.lname tlname
429 FROM `dated_reminders` dr
430 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
431 JOIN `users` u ON dr.dr_from_ID = u.id
432 JOIN `users` tu ON drl.to_id = tu.id
436 // --------- loop through the results
437 for ($i = 0; $drRow = sqlFetchArray($drSQL); $i++
) {
438 // --------- need to run patient query seperately to allow for messages not linked to a patient
439 $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']));
440 $pRow = sqlFetchArray($pSQL);
442 $prSQL = sqlStatement("SELECT u.fname pfname, u.mname pmname, u.lname plname FROM `users` u WHERE u.id = ?", array($drRow['dr_processed_by']));
443 $prRow = sqlFetchArray($prSQL);
445 // --------- fill the $reminders array
446 $reminders[$i]['messageID'] = $drRow['dr_id'];
447 $reminders[$i]['PatientID'] = $drRow['pid'];
449 $reminders[$i]['pDate'] = ($drRow['processedDate'] == '0000-00-00 00:00:00' ?
'N/A' : $drRow['processedDate']);
450 $reminders[$i]['sDate'] = $drRow['sDate'];
451 $reminders[$i]['dDate'] = $drRow['dDate'];
453 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
454 $reminders[$i]['PatientName'] = (empty($pRow) ?
'N/A' : $pRow['ptitle'] . ' ' . $pRow['pfname'] . ' ' . $pRow['pmname'] . ' ' . $pRow['plname']);
455 // -------------------------------------
457 $reminders[$i]['message'] = $drRow['dr_message_text'];
458 $reminders[$i]['fromName'] = $drRow['ffname'] . ' ' . $drRow['fmname'] . ' ' . $drRow['flname'];
459 $reminders[$i]['ToName'] = $drRow['tfname'] . ' ' . $drRow['tmname'] . ' ' . $drRow['tlname'];
460 $reminders[$i]['processedByName'] = (empty($prRow) ?
'N/A' : $prRow['ptitle'] . ' ' . $prRow['pfname'] . ' ' . $prRow['pmname'] . ' ' . $prRow['plname']);
463 // --------- END OF loop through the results