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 * RemindersArray function
26 * @returns array reminders for specified user, defaults to current user if none specified
28 function RemindersArray($days_to_show, $today, $alerts_to_show, $userID = false)
31 $userID = $_SESSION['authId'];
35 // ----- define a blank reminders array
38 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
39 $drSQL = sqlStatement(
41 dr.pid, dr.dr_id, dr.dr_message_text,dr.dr_message_due_date,
42 u.fname ffname, u.mname fmname, u.lname flname
43 FROM `dated_reminders` dr
44 JOIN `users` u ON dr.dr_from_ID = u.id
45 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
47 AND dr.`message_processed` = 0
48 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL $days_to_show DAY)
49 ORDER BY `dr_message_due_date` ASC , `message_priority` ASC LIMIT 0,$alerts_to_show",
53 // --------- loop through the results
54 for ($i=0; $drRow=sqlFetchArray($drSQL); $i++
) {
55 // --------- need to run patient query seperately to allow for reminders not linked to a patient
57 if ($drRow['pid'] > 0) {
58 $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']));
59 $pRow = sqlFetchArray($pSQL);
62 // --------- fill the $reminders array
63 $reminders[$i]['messageID'] = $drRow['dr_id'];
64 $reminders[$i]['PatientID'] = $drRow['pid'];
66 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
67 $reminders[$i]['PatientName'] = (empty($pRow) ?
'' : $pRow['ptitle'].' '.$pRow['pfname'].' '.$pRow['pmname'].' '.$pRow['plname']);
68 // -------------------------------------
70 $reminders[$i]['message'] = $drRow['dr_message_text'];
71 $reminders[$i]['dueDate'] = $drRow['dr_message_due_date'];
72 $reminders[$i]['fromName'] = $drRow['ffname'].' '.$drRow['fmname'].' '.$drRow['flname'];
74 // --------- if the message is due or overdue set $hasAlerts to true, this will stop autohiding of reminders
75 if (strtotime($drRow['dr_message_due_date']) <= $today) {
80 // --------- END OF loop through the results
84 // ------------------------------------------------
85 // @ END OF RemindersArray function
86 // ------------------------------------------------
92 * This function is used to get a count of the number of reminders due for a specified
95 * @param $days_to_show
97 * @param defaults to current user if none specified
98 * @returns int with number of due reminders for specified user
100 function GetDueReminderCount($days_to_show, $today, $userID = false)
103 $userID = $_SESSION['authId'];
106 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
107 $drSQL = sqlStatement(
108 "SELECT count(dr.dr_id) c
109 FROM `dated_reminders` dr
110 JOIN `users` u ON dr.dr_from_ID = u.id
111 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
113 AND dr.`message_processed` = 0
114 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL $days_to_show DAY)",
118 $drRow=sqlFetchArray($drSQL);
121 // ------------------------------------------------
122 // @ END OF GetDueReminder function
123 // ------------------------------------------------
125 // ------------------------------------------------
126 // @ GetAllReminderCount function
127 // @ returns int with number of unprocessed reminders for specified user, defaults to current user if none specified
128 // ------------------------------------------------
129 function GetAllReminderCount($userID = false)
132 $userID = $_SESSION['authId'];
135 // ----- sql statement for getting uncompleted reminders
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",
146 $drRow=sqlFetchArray($drSQL);
149 // ------------------------------------------------
150 // @ END OF GetAllReminderCount function
151 // ------------------------------------------------
153 // ------------------------------------------------
154 // @ getRemindersHTML(array $reminders)
155 // @ returns HTML as a string, for printing
156 // ------------------------------------------------
157 function getRemindersHTML($reminders = array(), $today)
160 // --- initialize the string as blank
162 // --- loop through the $reminders
163 foreach ($reminders as $r) {
164 // --- initialize $warning as the date, this is placed in front of the message
165 $warning = text($r['dueDate']);
166 // --- initialize $class as 'text dr', this is the basic class
169 // --------- check if reminder is overdue
170 if (strtotime($r['dueDate']) < $today) {
171 $warning = '! '.xlt('OVERDUE');
172 $class = 'bold alert dr';
173 } // --------- check if reminder is due
174 elseif (strtotime($r['dueDate']) == $today) {
175 $warning = xlt('TODAY');
176 $class='bold alert dr';
179 // end check if reminder is due or overdue
180 // apend to html string
181 $pdHTML .= '<p id="p_'.attr($r['messageID']).'">
182 <a class="dnRemover css_button_small" onclick="updateme('."'".attr($r['messageID'])."'".')" id="'.attr($r['messageID']).'" href="#">
183 <span>'.xlt('Set As Completed').'</span>
185 <span title="'.($r['PatientID'] > 0 ?
xla('Click Patient Name to Open Patient File') : '').'" class="'.attr($class).'">'.
187 <span onclick="goPid('.attr($r['PatientID']).')" class="patLink" id="'.attr($r['PatientID']).'">'.
188 text($r['PatientName']).'
190 text($r['message']).' - ['.text($r['fromName']).']
192 <a onclick="openAddScreen('.attr($r['messageID']).')" class="dnForwarder" id="'.attr($r['messageID']).'" href="#">[ '.xlt('Forward').' ]</a>
196 return ($pdHTML == '' ?
'<p class="alert-custom"><br />'.xlt('No Reminders').'</p>' : $pdHTML);
198 // ------------------------------------------------
199 // @ END OF getRemindersHTML function
200 // ------------------------------------------------
203 // ------------------------------------------------
204 // @ setReminderAsProccessed(int $rID)
205 // @ marks reminder as processed
206 // ------------------------------------------------
207 function setReminderAsProcessed($rID, $userID = false)
210 $userID = $_SESSION['authId'];
213 if (is_numeric($rID) and $rID > 0) {
214 // --- check if this user can remove this message
215 // --- need a better way of checking the current user, I don't like using $_SESSION for checks
216 $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));
217 $rdrRow=sqlFetchArray($rdrSQL);
219 // --- if this user can delete this message (ie if it was sent to this user)
220 if ($rdrRow['c'] == 1) {
221 // ----- update the data, set the message to proccesses
222 sqlStatement("UPDATE `dated_reminders` SET `message_processed` = 1, `processed_date` = NOW(), `dr_processed_by` = ? WHERE `dr_id` = ? ", array(intval($userID),intval($rID)));
226 // ------------------------------------------------
227 // @ END OF setReminderAsProccessed function
228 // ------------------------------------------------
231 // ------------------------------------------------
232 // @ getReminderById(int $mID)
233 // @ returns an array with message details for forwarding
234 // ------------------------------------------------
235 function getReminderById($mID, $userID = false)
238 $userID = $_SESSION['authId'];
241 $rdrSQL = sqlStatement("SELECT * FROM `dated_reminders` dr
242 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
243 WHERE drl.to_id = ? AND dr.`dr_id` = ? LIMIT 0,1", array($userID,$mID));
244 $rdrRow=sqlFetchArray($rdrSQL);
245 if (!empty($rdrRow)) {
251 // ------------------------------------------------
252 // @ END OF getReminderById function
253 // ------------------------------------------------
256 // ------------------------------------------------
257 // @ getReminderById(
265 // @ returns an array with message details for forwarding
266 // ------------------------------------------------
267 function sendReminder($sendTo, $fromID, $message, $dueDate, $patID, $priority)
269 if (// ------- Should run data checks before running this function for more accurate error reporting
270 // ------- check sendTo is not empty
272 // ------- check dueDate, only allow valid dates, todo -> enhance date checker
273 preg_match('/\d{4}[-]\d{2}[-]\d{2}/', $dueDate) and
274 // ------- check priority, only allow 1-3
275 intval($priority) <= 3 and
276 // ------- check message, only up to 255 characters
277 strlen($message) <= 255 and strlen($message) > 0 and
278 // ------- check if PatientID is set and in numeric
281 // ------- check for valid recipient
282 $cRow=sqlFetchArray(sqlStatement('SELECT count(id) FROM `users` WHERE `id` = ?', array($sendDMTo)));
287 // ------- if no errors
288 // --------- insert the new message
290 "INSERT INTO `dated_reminders`
291 (`dr_from_ID` ,`dr_message_text` ,`dr_message_sent_date` ,`dr_message_due_date` ,`pid` ,`message_priority` ,`message_processed` ,`processed_date`)
292 VALUES (?, ?, NOW( ), ?, ?, ?, '0', '');",
293 array($fromID,$message,$dueDate,$patID,$priority)
296 foreach ($sendTo as $st) {
298 "INSERT INTO `dated_reminders_link`
305 } //---- end of if block
309 // ------- get current patient name
310 // ---- returns string, blank if no current patient
311 function getPatName($patientID)
313 $patientID = intval($patientID);
314 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.pid = ?", array($patientID));
315 $pRow = sqlFetchArray($pSQL);
316 return (empty($pRow) ?
'' : $pRow['ptitle'].' '.$pRow['pfname'].' '.$pRow['pmname'].' '.$pRow['plname']);
319 // -- log reminders array function uses $_GET to filter
320 function logRemindersArray()
323 // set blank array for data to be parsed to sql
325 // set blank string for the query
327 $sentBy = $_GET['sentBy'];
328 $sentTo = $_GET['sentTo'];
329 //------------------------------------------
330 // ----- HANDLE SENT BY FILTER
331 if (!empty($sentBy)) {
333 foreach ($sentBy as $sb) {
334 $where .= ($sbCount == 0 ?
'(' : ' OR ').'dr.dr_from_ID = ? ';
342 //------------------------------------------
343 // ----- HANDLE SENT TO FILTER
344 if (!empty($sentTo)) {
345 $where = ($where == '' ?
'' : $where.' AND ');
347 foreach ($sentTo as $st) {
348 $where .= ($stCount == 0 ?
'(' : ' OR ').'drl.to_id = ? ';
356 //------------------------------------------
357 // ----- HANDLE PROCCESSED/PENDING FILTER ONLY RUN THIS IF BOTH ARE NOT SET
358 if (isset($_GET['processed']) and !isset($_GET['pending'])) {
359 $where = ($where == '' ?
'dr.message_processed = 1' : $where.' AND dr.message_processed = 1');
360 } elseif (!isset($_GET['processed']) and isset($_GET['pending'])) {
361 $where = ($where == '' ?
'dr.message_processed = 0' : $where.' AND dr.message_processed = 0');
364 //------------------------------------------
365 // ----- HANDLE DATE RANGE FILTERS
366 if (isset($_GET['sd']) and $_GET['sd'] != '') {
367 $where = ($where == '' ?
'dr.dr_message_sent_date >= ?' : $where.' AND dr.dr_message_sent_date >= ?');
368 $input[] = $_GET['sd'].' 00:00:00';
371 if (isset($_GET['ed']) and $_GET['ed'] != '') {
372 $where = ($where == '' ?
'dr.dr_message_sent_date <= ?' : $where.' AND dr.dr_message_sent_date <= ?');
373 $input[] = $_GET['ed'].' 23:59:59';
376 //------------------------------------------
379 //-------- add the "WHERE" the string if string is not blank, avoid sql errors for blannk WHERE statements
380 $where = ($where == '' ?
'' : 'WHERE '.$where);
382 // ----- define a blank reminders array
383 $reminders = array();
385 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
386 $drSQL = sqlStatement(
388 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,
389 u.fname ffname, u.mname fmname, u.lname flname,
390 tu.fname tfname, tu.mname tmname, tu.lname tlname
391 FROM `dated_reminders` dr
392 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
393 JOIN `users` u ON dr.dr_from_ID = u.id
394 JOIN `users` tu ON drl.to_id = tu.id
398 // --------- loop through the results
399 for ($i=0; $drRow=sqlFetchArray($drSQL); $i++
) {
400 // --------- need to run patient query seperately to allow for messages not linked to a patient
401 $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']));
402 $pRow = sqlFetchArray($pSQL);
404 $prSQL = sqlStatement("SELECT u.fname pfname, u.mname pmname, u.lname plname FROM `users` u WHERE u.id = ?", array($drRow['dr_processed_by']));
405 $prRow = sqlFetchArray($prSQL);
407 // --------- fill the $reminders array
408 $reminders[$i]['messageID'] = $drRow['dr_id'];
409 $reminders[$i]['PatientID'] = $drRow['pid'];
411 $reminders[$i]['pDate'] = ($drRow['processedDate'] == '0000-00-00 00:00:00' ?
'N/A' : $drRow['processedDate']);
412 $reminders[$i]['sDate'] = $drRow['sDate'];
413 $reminders[$i]['dDate'] = $drRow['dDate'];
415 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
416 $reminders[$i]['PatientName'] = (empty($pRow) ?
'N/A' : $pRow['ptitle'].' '.$pRow['pfname'].' '.$pRow['pmname'].' '.$pRow['plname']);
417 // -------------------------------------
419 $reminders[$i]['message'] = $drRow['dr_message_text'];
420 $reminders[$i]['fromName'] = $drRow['ffname'].' '.$drRow['fmname'].' '.$drRow['flname'];
421 $reminders[$i]['ToName'] = $drRow['tfname'].' '.$drRow['tmname'].' '.$drRow['tlname'];
422 $reminders[$i]['processedByName'] = (empty($prRow) ?
'N/A' : $prRow['ptitle'].' '.$prRow['pfname'].' '.$prRow['pmname'].' '.$prRow['plname']);
425 // --------- END OF loop through the results