incremented database counter for prior feature
[openemr.git] / library / dated_reminder_functions.php
blob10eeacff4dbfd4596fafbb8c54e1546ec8b92955
1 <?php
2 /**
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>;.
18 * @package OpenEMR
19 * @author Craig Bezuidenhout <http://www.tajemo.co.za/>
20 * @link http://www.open-emr.org
23 /**
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){
29 if(!$userID) $userID = $_SESSION['authId'];
30 global $hasAlerts;
31 // ----- define a blank reminders array
32 $reminders = array();
34 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
35 $drSQL = sqlStatement(
36 "SELECT
37 dr.pid, dr.dr_id, dr.dr_message_text,dr.dr_message_due_date,
38 u.fname ffname, u.mname fmname, u.lname flname
39 FROM `dated_reminders` dr
40 JOIN `users` u ON dr.dr_from_ID = u.id
41 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
42 WHERE drl.to_id = ?
43 AND dr.`message_processed` = 0
44 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL $days_to_show DAY)
45 ORDER BY `dr_message_due_date` ASC , `message_priority` ASC LIMIT 0,$alerts_to_show"
46 , array($userID)
49 // --------- loop through the results
50 for($i=0; $drRow=sqlFetchArray($drSQL); $i++){
51 // --------- need to run patient query seperately to allow for reminders not linked to a patient
52 $pRow = array();
53 if($drRow['pid'] > 0){
54 $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']));
55 $pRow = sqlFetchArray($pSQL);
58 // --------- fill the $reminders array
59 $reminders[$i]['messageID'] = $drRow['dr_id'];
60 $reminders[$i]['PatientID'] = $drRow['pid'];
62 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
63 $reminders[$i]['PatientName'] = (empty($pRow) ? '' : $pRow['ptitle'].' '.$pRow['pfname'].' '.$pRow['pmname'].' '.$pRow['plname']);
64 // -------------------------------------
66 $reminders[$i]['message'] = $drRow['dr_message_text'];
67 $reminders[$i]['dueDate'] = $drRow['dr_message_due_date'];
68 $reminders[$i]['fromName'] = $drRow['ffname'].' '.$drRow['fmname'].' '.$drRow['flname'];
70 // --------- if the message is due or overdue set $hasAlerts to true, this will stop autohiding of reminders
71 if(strtotime($drRow['dr_message_due_date']) <= $today) $hasAlerts = true;
73 // --------- END OF loop through the results
75 return $reminders;
77 // ------------------------------------------------
78 // @ END OF RemindersArray function
79 // ------------------------------------------------
84 /**
85 * This function is used to get a count of the number of reminders due for a specified
86 * user.
88 * @param $days_to_show
89 * @param $today
90 * @param defaults to current user if none specified
91 * @returns int with number of due reminders for specified user
93 function GetDueReminderCount($days_to_show,$today,$userID = false){
94 if(!$userID) $userID = $_SESSION['authId'];
96 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
97 $drSQL = sqlStatement(
98 "SELECT count(dr.dr_id) c
99 FROM `dated_reminders` dr
100 JOIN `users` u ON dr.dr_from_ID = u.id
101 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
102 WHERE drl.to_id = ?
103 AND dr.`message_processed` = 0
104 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL $days_to_show DAY)"
105 , array($userID)
108 $drRow=sqlFetchArray($drSQL);
109 return $drRow['c'];
111 // ------------------------------------------------
112 // @ END OF GetDueReminder function
113 // ------------------------------------------------
115 // ------------------------------------------------
116 // @ GetAllReminderCount function
117 // @ returns int with number of unprocessed reminders for specified user, defaults to current user if none specified
118 // ------------------------------------------------
119 function GetAllReminderCount($userID = false){
120 if(!$userID) $userID = $_SESSION['authId'];
122 // ----- sql statement for getting uncompleted reminders
123 $drSQL = sqlStatement(
124 "SELECT count(dr.dr_id) c
125 FROM `dated_reminders` dr
126 JOIN `users` u ON dr.dr_from_ID = u.id
127 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
128 WHERE drl.to_id = ?
129 AND dr.`message_processed` = 0"
130 , array($userID)
133 $drRow=sqlFetchArray($drSQL);
134 return $drRow['c'];
136 // ------------------------------------------------
137 // @ END OF GetAllReminderCount function
138 // ------------------------------------------------
140 // ------------------------------------------------
141 // @ getRemindersHTML(array $reminders)
142 // @ returns HTML as a string, for printing
143 // ------------------------------------------------
144 function getRemindersHTML($reminders = array(),$today){
145 global $hasAlerts;
146 // --- initialize the string as blank
147 $pdHTML = '';
148 // --- loop through the $reminders
149 foreach($reminders as $r){
150 // --- initialize $warning as the date, this is placed in front of the message
151 $warning = text($r['dueDate']);
152 // --- initialize $class as 'text dr', this is the basic class
153 $class='text dr';
155 // --------- check if reminder is overdue
156 if(strtotime($r['dueDate']) < $today){
157 $warning = '! '.xlt('OVERDUE');
158 $class = 'bold alert dr';
160 // --------- check if reminder is due
161 elseif(strtotime($r['dueDate']) == $today){
162 $warning = xlt('TODAY');
163 $class='bold alert dr';
165 // end check if reminder is due or overdue
166 // apend to html string
167 $pdHTML .= '<p id="p_'.attr($r['messageID']).'">
168 <a class="dnRemover css_button_small" onclick="updateme('."'".attr($r['messageID'])."'".')" id="'.attr($r['messageID']).'" href="#">
169 <span>'.xlt('Set As Completed').'</span>
170 </a>
171 <span title="'.($r['PatientID'] > 0 ? xla('Click Patient Name to Open Patient File') : '').'" class="'.attr($class).'">'.
172 $warning.'
173 <span onclick="goPid('.attr($r['PatientID']).')" class="patLink" id="'.attr($r['PatientID']).'">'.
174 text($r['PatientName']).'
175 </span> '.
176 text($r['message']).' - ['.text($r['fromName']).']
177 </span> ----->
178 <a onclick="openAddScreen('.attr($r['messageID']).')" class="dnForwarder" id="'.attr($r['messageID']).'" href="#">[ '.xlt('Forward').' ]</a>
179 </p>';
181 return ($pdHTML == '' ? '<p class="alert"><br />'.xlt('No Reminders').'</p>' : $pdHTML);
183 // ------------------------------------------------
184 // @ END OF getRemindersHTML function
185 // ------------------------------------------------
188 // ------------------------------------------------
189 // @ setReminderAsProccessed(int $rID)
190 // @ marks reminder as processed
191 // ------------------------------------------------
192 function setReminderAsProcessed($rID,$userID = false){
193 if(!$userID) $userID = $_SESSION['authId'];
194 if(is_numeric($rID) and $rID > 0){
195 // --- check if this user can remove this message
196 // --- need a better way of checking the current user, I don't like using $_SESSION for checks
197 $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));
198 $rdrRow=sqlFetchArray($rdrSQL);
200 // --- if this user can delete this message (ie if it was sent to this user)
201 if($rdrRow['c'] == 1){
202 // ----- update the data, set the message to proccesses
203 sqlStatement("UPDATE `dated_reminders` SET `message_processed` = 1, `processed_date` = NOW(), `dr_processed_by` = ? WHERE `dr_id` = ? ", array(intval($userID),intval($rID)));
207 // ------------------------------------------------
208 // @ END OF setReminderAsProccessed function
209 // ------------------------------------------------
212 // ------------------------------------------------
213 // @ getReminderById(int $mID)
214 // @ returns an array with message details for forwarding
215 // ------------------------------------------------
216 function getReminderById($mID,$userID = false){
217 if(!$userID) $userID = $_SESSION['authId'];
218 $rdrSQL = sqlStatement("SELECT * FROM `dated_reminders` dr
219 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
220 WHERE drl.to_id = ? AND dr.`dr_id` = ? LIMIT 0,1", array($userID,$mID));
221 $rdrRow=sqlFetchArray($rdrSQL);
222 if(!empty($rdrRow)){
223 return $rdrRow;
225 return false;
227 // ------------------------------------------------
228 // @ END OF getReminderById function
229 // ------------------------------------------------
232 // ------------------------------------------------
233 // @ getReminderById(
234 // array $sendTo
235 // int $fromID
236 // string $message
237 // date $dueDate
238 // int $patID
239 // int $priority
240 // )
241 // @ returns an array with message details for forwarding
242 // ------------------------------------------------
243 function sendReminder($sendTo,$fromID,$message,$dueDate,$patID,$priority){
245 // ------- Should run data checks before running this function for more accurate error reporting
246 // ------- check sendTo is not empty
247 !empty($sendTo) and
248 // ------- check dueDate, only allow valid dates, todo -> enhance date checker
249 preg_match('/\d{4}[-]\d{2}[-]\d{2}/',$dueDate) and
250 // ------- check priority, only allow 1-3
251 intval($priority) <= 3 and
252 // ------- check message, only up to 255 characters
253 strlen($message) <= 255 and strlen($message) > 0 and
254 // ------- check if PatientID is set and in numeric
255 is_numeric($patID)
257 // ------- check for valid recipient
258 $cRow=sqlFetchArray(sqlStatement('SELECT count(id) FROM `users` WHERE `id` = ?',array($sendDMTo)));
259 if($cRow == 0){
260 return false;
262 // ------- if no errors
263 // --------- insert the new message
264 $mID = sqlInsert("INSERT INTO `dated_reminders`
265 (`dr_from_ID` ,`dr_message_text` ,`dr_message_sent_date` ,`dr_message_due_date` ,`pid` ,`message_priority` ,`message_processed` ,`processed_date`)
266 VALUES (?, ?, NOW( ), ?, ?, ?, '0', '');",
267 array($fromID,$message,$dueDate,$patID,$priority));
269 foreach($sendTo as $st){
270 sqlInsert("INSERT INTO `dated_reminders_link`
271 (`dr_id` ,`to_id`)
272 VALUES (?, ?);",
273 array($mID,$st));
275 return true;
276 } //---- end of if block
277 return false;
280 // ------- get current patient name
281 // ---- returns string, blank if no current patient
282 function getPatName($patientID){
283 $patientID = intval($patientID);
284 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.pid = ?",array($patientID));
285 $pRow = sqlFetchArray($pSQL);
286 return (empty($pRow) ? '' : $pRow['ptitle'].' '.$pRow['pfname'].' '.$pRow['pmname'].' '.$pRow['plname']);
289 // -- log reminders array function uses $_GET to filter
290 function logRemindersArray(){
292 // set blank array for data to be parsed to sql
293 $input = array();
294 // set blank string for the query
295 $where = '';
296 $sentBy = $_GET['sentBy'];
297 $sentTo = $_GET['sentTo'];
298 //------------------------------------------
299 // ----- HANDLE SENT BY FILTER
300 if(!empty($sentBy)){
301 $sbCount = 0;
302 foreach($sentBy as $sb){
303 $where .= ($sbCount == 0 ? '(' : ' OR ').'dr.dr_from_ID = ? ';
304 $sbCount++;
305 $input[] = $sb;
307 $where .= ')';
309 //------------------------------------------
310 // ----- HANDLE SENT TO FILTER
311 if(!empty($sentTo)){
312 $where = ($where == '' ? '' : $where.' AND ');
313 $stCount = 0;
314 foreach($sentTo as $st){
315 $where .= ($stCount == 0 ? '(' : ' OR ').'drl.to_id = ? ';
316 $stCount++;
317 $input[] = $st;
319 $where .= ')';
321 //------------------------------------------
322 // ----- HANDLE PROCCESSED/PENDING FILTER ONLY RUN THIS IF BOTH ARE NOT SET
323 if(isset($_GET['processed']) and !isset($_GET['pending'])){
324 $where = ($where == '' ? 'dr.message_processed = 1' : $where.' AND dr.message_processed = 1');
326 elseif(!isset($_GET['processed']) and isset($_GET['pending'])){
327 $where = ($where == '' ? 'dr.message_processed = 0' : $where.' AND dr.message_processed = 0');
329 //------------------------------------------
330 // ----- HANDLE DATE RANGE FILTERS
331 if(isset($_GET['sd']) and $_GET['sd'] != ''){
332 $where = ($where == '' ? 'dr.dr_message_sent_date >= ?' : $where.' AND dr.dr_message_sent_date >= ?');
333 $input[] = $_GET['sd'].' 00:00:00';
335 if(isset($_GET['ed']) and $_GET['ed'] != ''){
336 $where = ($where == '' ? 'dr.dr_message_sent_date <= ?' : $where.' AND dr.dr_message_sent_date <= ?');
337 $input[] = $_GET['ed'].' 23:59:59';
339 //------------------------------------------
342 //-------- add the "WHERE" the string if string is not blank, avoid sql errors for blannk WHERE statements
343 $where = ($where == '' ? '' : 'WHERE '.$where);
345 // ----- define a blank reminders array
346 $reminders = array();
348 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
349 $drSQL = sqlStatement(
350 "SELECT
351 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,
352 u.fname ffname, u.mname fmname, u.lname flname,
353 tu.fname tfname, tu.mname tmname, tu.lname tlname
354 FROM `dated_reminders` dr
355 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
356 JOIN `users` u ON dr.dr_from_ID = u.id
357 JOIN `users` tu ON drl.to_id = tu.id
358 $where"
359 ,$input);
360 // --------- loop through the results
361 for($i=0; $drRow=sqlFetchArray($drSQL); $i++){
362 // --------- need to run patient query seperately to allow for messages not linked to a patient
363 $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']));
364 $pRow = sqlFetchArray($pSQL);
366 $prSQL = sqlStatement("SELECT u.fname pfname, u.mname pmname, u.lname plname FROM `users` u WHERE u.id = ?",array($drRow['dr_processed_by']));
367 $prRow = sqlFetchArray($prSQL );
369 // --------- fill the $reminders array
370 $reminders[$i]['messageID'] = $drRow['dr_id'];
371 $reminders[$i]['PatientID'] = $drRow['pid'];
373 $reminders[$i]['pDate'] = ($drRow['processedDate'] == '0000-00-00 00:00:00' ? 'N/A' : $drRow['processedDate']);
374 $reminders[$i]['sDate'] = $drRow['sDate'];
375 $reminders[$i]['dDate'] = $drRow['dDate'];
377 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
378 $reminders[$i]['PatientName'] = (empty($pRow) ? 'N/A' : $pRow['ptitle'].' '.$pRow['pfname'].' '.$pRow['pmname'].' '.$pRow['plname']);
379 // -------------------------------------
381 $reminders[$i]['message'] = $drRow['dr_message_text'];
382 $reminders[$i]['fromName'] = $drRow['ffname'].' '.$drRow['fmname'].' '.$drRow['flname'];
383 $reminders[$i]['ToName'] = $drRow['tfname'].' '.$drRow['tmname'].' '.$drRow['tlname'];
384 $reminders[$i]['processedByName'] = (empty($prRow) ? 'N/A' : $prRow['ptitle'].' '.$prRow['pfname'].' '.$prRow['pmname'].' '.$prRow['plname']);
386 // --------- END OF loop through the results
388 return $reminders;