added 2 new functions, GetDueReminder and GetAllReminderCount
[openemr.git] / library / dated_reminders.php
blob1437f278b5c3fc2272de0329609f6085fa7b2a1e
1 <?php
2 // ------------------------------------------------------------------------ //
3 // OpenEMR Electronic Medical Records System //
4 // Copyright (c) 2012 tajemo.co.za //
5 // <http://www.tajemo.co.za/> //
6 // ------------------------------------------------------------------------ //
7 // This program is free software; you can redistribute it and/or modify //
8 // it under the terms of the GNU General Public License as published by //
9 // the Free Software Foundation; either version 2 of the License, or //
10 // (at your option) any later version. //
11 // //
12 // You may not change or alter any portion of this comment or credits //
13 // of supporting developers from this source code or any supporting //
14 // source code which is considered copyrighted (c) material of the //
15 // original comment or credit authors. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details. //
21 // //
22 // You should have received a copy of the GNU General Public License //
23 // along with this program; if not, write to the Free Software //
24 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
25 // --------------------------------------------------------------------------//
26 // Original Author of this file: Craig Bezuidenhout (Tajemo Enterprises) //
27 // Purpose of this file: Contains functions used in the dated reminders //
28 // --------------------------------------------------------------------------//
29 require_once("$srcdir/htmlspecialchars.inc.php");
31 // ------------------------------------------------
32 // @ RemindersArray function
33 // @ returns array with reminders for specified user, defaults to current user if none specified
34 // ------------------------------------------------
35 function RemindersArray($days_to_show,$today,$alerts_to_show,$userID = false){
36 if(!$userID) $userID = $_SESSION['authId'];
37 global $hasAlerts;
38 // ----- define a blank reminders array
39 $reminders = array();
41 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
42 $drSQL = sqlStatement(
43 "SELECT
44 dr.pid, dr.dr_id, dr.dr_message_text,dr.dr_message_due_date,
45 u.fname ffname, u.mname fmname, u.lname flname
46 FROM `dated_reminders` dr
47 JOIN `users` u ON dr.dr_from_ID = u.id
48 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
49 WHERE drl.to_id = ?
50 AND dr.`message_processed` = 0
51 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL $days_to_show DAY)
52 ORDER BY `dr_message_due_date` ASC , `message_priority` ASC LIMIT 0,$alerts_to_show"
53 , array($userID)
54 );
56 // --------- loop through the results
57 for($i=0; $drRow=sqlFetchArray($drSQL); $i++){
58 // --------- need to run patient query seperately to allow for reminders not linked to a patient
59 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.id = ?",array($drRow['pid']));
60 $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) $hasAlerts = true;
77 // --------- END OF loop through the results
79 return $reminders;
81 // ------------------------------------------------
82 // @ END OF RemindersArray function
83 // ------------------------------------------------
88 // ------------------------------------------------
89 // @ GetDueReminder function
90 // @ returns int with number of due reminders for specified user, defaults to current user if none specified
91 // ------------------------------------------------
92 function GetDueReminderCount($days_to_show,$today,$userID = false){
93 if(!$userID) $userID = $_SESSION['authId'];
95 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
96 $drSQL = sqlStatement(
97 "SELECT count(dr.dr_id) c
98 FROM `dated_reminders` dr
99 JOIN `users` u ON dr.dr_from_ID = u.id
100 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
101 WHERE drl.to_id = ?
102 AND dr.`message_processed` = 0
103 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL $days_to_show DAY)"
104 , array($userID)
107 $drRow=sqlFetchArray($drSQL);
108 return $drRow['c'];
110 // ------------------------------------------------
111 // @ END OF GetDueReminder function
112 // ------------------------------------------------
114 // ------------------------------------------------
115 // @ GetAllReminderCount function
116 // @ returns int with number of unprocessed reminders for specified user, defaults to current user if none specified
117 // ------------------------------------------------
118 function GetAllReminderCount($userID = false){
119 if(!$userID) $userID = $_SESSION['authId'];
121 // ----- sql statement for getting uncompleted reminders
122 $drSQL = sqlStatement(
123 "SELECT count(dr.dr_id) c
124 FROM `dated_reminders` dr
125 JOIN `users` u ON dr.dr_from_ID = u.id
126 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
127 WHERE drl.to_id = ?
128 AND dr.`message_processed` = 0"
129 , array($userID)
132 $drRow=sqlFetchArray($drSQL);
133 return $drRow['c'];
135 // ------------------------------------------------
136 // @ END OF GetAllReminderCount function
137 // ------------------------------------------------
139 // ------------------------------------------------
140 // @ getRemindersHTML(array $reminders)
141 // @ returns HTML as a string, for printing
142 // ------------------------------------------------
143 function getRemindersHTML($reminders = array(),$today){
144 global $hasAlerts;
145 // --- initialize the string as blank
146 $pdHTML = '';
147 // --- loop through the $reminders
148 foreach($reminders as $r){
149 // --- initialize $warning as the date, this is placed in front of the message
150 $warning = text($r['dueDate']);
151 // --- initialize $class as 'text dr', this is the basic class
152 $class='text dr';
154 // --------- check if reminder is overdue
155 if(strtotime($r['dueDate']) < $today){
156 $warning = '! '.xlt('OVERDUE');
157 $class = 'bold alert dr';
159 // --------- check if reminder is due
160 elseif(strtotime($r['dueDate']) == $today){
161 $warning = xlt('TODAY');
162 $class='bold alert dr';
164 // end check if reminder is due or overdue
165 // apend to html string
166 $pdHTML .= '<p id="p_'.attr($r['messageID']).'">
167 <a class="dnRemover css_button_small" onclick="updateme('."'".attr($r['messageID'])."'".')" id="'.attr($r['messageID']).'" href="#">
168 <span>'.xlt('Set As Completed').'</span>
169 </a>
170 <span title="'.xla('Click Patient Name to Open Patient').'" class="'.attr($class).'">'.
171 $warning.'
172 <span onclick="goPid('.attr($r['PatientID']).')" class="patLink" id="'.attr($r['PatientID']).'">'.
173 text($r['PatientName']).'
174 </span> '.
175 text($r['message']).' - ['.text($r['fromName']).']
176 </span> ----->
177 <a onclick="openAddScreen('.attr($r['messageID']).')" class="dnForwarder" id="'.attr($r['messageID']).'" href="#">[ '.xlt('Forward').' ]</a>
178 </p>';
180 return ($pdHTML == '' ? '<p class="alert"><br />'.xlt('No Reminders').'</p>' : $pdHTML);
182 // ------------------------------------------------
183 // @ END OF getRemindersHTML function
184 // ------------------------------------------------
187 // ------------------------------------------------
188 // @ setReminderAsProccessed(int $rID)
189 // @ marks reminder as processed
190 // ------------------------------------------------
191 function setReminderAsProcessed($rID,$userID = false){
192 if(!$userID) $userID = $_SESSION['authId'];
193 if(is_numeric($rID) and $rID > 0){
194 // --- check if this user can remove this message
195 // --- need a better way of checking the current user, I don't like using $_SESSION for checks
196 $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));
197 $rdrRow=sqlFetchArray($rdrSQL);
199 // --- if this user can delete this message (ie if it was sent to this user)
200 if($rdrRow['c'] == 1){
201 // ----- update the data, set the message to proccesses
202 sqlStatement("UPDATE `dated_reminders` SET `message_processed` = 1, `processed_date` = NOW(), `dr_processed_by` = ? WHERE `dr_id` = ? ", array(intval($userID),intval($rID)));
206 // ------------------------------------------------
207 // @ END OF setReminderAsProccessed function
208 // ------------------------------------------------
211 // ------------------------------------------------
212 // @ getReminderById(int $mID)
213 // @ returns an array with message details for forwarding
214 // ------------------------------------------------
215 function getReminderById($mID,$userID = false){
216 if(!$userID) $userID = $_SESSION['authId'];
217 $rdrSQL = sqlStatement("SELECT * FROM `dated_reminders` dr
218 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
219 WHERE drl.to_id = ? AND dr.`dr_id` = ? LIMIT 0,1", array($userID,$mID));
220 $rdrRow=sqlFetchArray($rdrSQL);
221 if(!empty($rdrRow)){
222 return $rdrRow;
224 return false;
226 // ------------------------------------------------
227 // @ END OF getReminderById function
228 // ------------------------------------------------
231 // ------------------------------------------------
232 // @ getReminderById(
233 // array $sendTo
234 // int $fromID
235 // string $message
236 // date $dueDate
237 // int $patID
238 // int $priority
239 // )
240 // @ returns an array with message details for forwarding
241 // ------------------------------------------------
242 function sendReminder($sendTo,$fromID,$message,$dueDate,$patID,$priority){
243 if(
244 // ------- Should run data checks before running this function for more accurate error reporting
245 // ------- check sendTo is not empty
246 !empty($sendTo) and
247 // ------- check dueDate, only allow valid dates, todo -> enhance date checker
248 preg_match('/\d{4}[-]\d{2}[-]\d{2}/',$dueDate) and
249 // ------- check priority, only allow 1-3
250 intval($priority) <= 3 and
251 // ------- check message, only up to 144 characters
252 strlen($message) <= 144 and strlen($message) > 0 and
253 // ------- check if PatientID is set and in numeric
254 is_numeric($patID)
256 // ------- check for valid recipient
257 $cRow=sqlFetchArray(sqlStatement('SELECT count(id) FROM `users` WHERE `id` = ?',array($sendDMTo)));
258 if($cRow == 0){
259 return false;
261 // ------- if no errors
262 // --------- insert the new message
263 $mID = sqlInsert("INSERT INTO `dated_reminders`
264 (`dr_from_ID` ,`dr_message_text` ,`dr_message_sent_date` ,`dr_message_due_date` ,`pid` ,`message_priority` ,`message_processed` ,`processed_date`)
265 VALUES (?, ?, NOW( ), ?, ?, ?, '0', '');",
266 array($fromID,$message,$dueDate,$patID,$priority));
268 foreach($sendTo as $st){
269 sqlInsert("INSERT INTO `dated_reminders_link`
270 (`dr_id` ,`to_id`)
271 VALUES (?, ?);",
272 array($mID,$st));
274 return true;
275 } //---- end of if block
276 return false;
279 // ------- get current patient name
280 // ---- returns string, blank if no current patient
281 function getPatName($patientID){
282 $patientID = intval($patientID);
283 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.id = ?",array($patientID));
284 $pRow = sqlFetchArray($pSQL);
285 return (empty($pRow) ? '' : $pRow['ptitle'].' '.$pRow['pfname'].' '.$pRow['pmname'].' '.$pRow['plname']);
288 // -- log reminders array function uses $_GET to filter
289 function logRemindersArray(){
291 // set blank array for data to be parsed to sql
292 $input = array();
293 // set blank string for the query
294 $where = '';
295 $sentBy = array();
296 $sentTo = array();
298 // ----- HANDLE SENT BY AND SEND TO FILTER
299 // ----- CREATES ARRAYS OF EACH TO BE HANDLED LATER
300 foreach($_GET as $key=>$val){
301 // check for matches, make sure they are integers
302 if(preg_match('/^sentBy/',$key) and is_numeric($val)){
303 $sentBy[] = intval($val);
304 unset($_GET[$key]);
306 if(preg_match('/^sentTo/',$key) and is_numeric($val)){
307 $sentTo[] = intval($val);
308 unset($_GET[$key]);
311 //------------------------------------------
312 // ----- HANDLE SENT BY FILTER
313 if(!empty($sentBy)){
314 $sbCount = 0;
315 foreach($sentBy as $sb){
316 $where .= ($sbCount == 0 ? '(' : ' OR ').'dr.dr_from_ID = ? ';
317 $sbCount++;
318 $input[] = $sb;
320 $where .= ')';
322 //------------------------------------------
323 // ----- HANDLE SENT TO FILTER
324 if(!empty($sentTo)){
325 $where = ($where == '' ? '' : $where.' AND ');
326 $stCount = 0;
327 foreach($sentTo as $st){
328 $where .= ($stCount == 0 ? '(' : ' OR ').'drl.to_id = ? ';
329 $stCount++;
330 $input[] = $st;
332 $where .= ')';
334 //------------------------------------------
335 // ----- HANDLE PROCCESSED/PENDING FILTER ONLY RUN THIS IF BOTH ARE NOT SET
336 if(isset($_GET['processed']) and !isset($_GET['pending'])){
337 $where = ($where == '' ? 'dr.message_processed = 1' : $where.' AND dr.message_processed = 1');
339 elseif(!isset($_GET['processed']) and isset($_GET['pending'])){
340 $where = ($where == '' ? 'dr.message_processed = 0' : $where.' AND dr.message_processed = 0');
342 //------------------------------------------
343 // ----- HANDLE DATE RANGE FILTERS
344 if(isset($_GET['sd']) and $_GET['sd'] != ''){
345 $where = ($where == '' ? 'dr.dr_message_sent_date >= ?' : $where.' AND dr.dr_message_sent_date >= ?');
346 $input[] = $_GET['sd'].' 00:00:00';
348 if(isset($_GET['ed']) and $_GET['ed'] != ''){
349 $where = ($where == '' ? 'dr.dr_message_sent_date <= ?' : $where.' AND dr.dr_message_sent_date <= ?');
350 $input[] = $_GET['ed'].' 24:00:00';
352 //------------------------------------------
355 //-------- add the "WHERE" the string if string is not blank, avoid sql errors for blannk WHERE statements
356 $where = ($where == '' ? '' : 'WHERE '.$where);
358 // ----- define a blank reminders array
359 $reminders = array();
361 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
362 $drSQL = sqlStatement(
363 "SELECT
364 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,
365 u.fname ffname, u.mname fmname, u.lname flname,
366 tu.fname tfname, tu.mname tmname, tu.lname tlname
367 FROM `dated_reminders` dr
368 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
369 JOIN `users` u ON dr.dr_from_ID = u.id
370 JOIN `users` tu ON drl.to_id = tu.id
371 $where"
372 ,$input);
374 // --------- loop through the results
375 for($i=0; $drRow=sqlFetchArray($drSQL); $i++){
376 // --------- need to run patient query seperately to allow for messages not linked to a patient
377 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.id = ?",array($drRow['pid']));
378 $pRow = sqlFetchArray($pSQL);
380 $prSQL = sqlStatement("SELECT u.fname pfname, u.mname pmname, u.lname plname FROM `users` u WHERE u.id = ?",array($drRow['dr_processed_by']));
381 $prRow = sqlFetchArray($prSQL );
383 // --------- fill the $reminders array
384 $reminders[$i]['messageID'] = $drRow['dr_id'];
385 $reminders[$i]['PatientID'] = $drRow['pid'];
387 $reminders[$i]['pDate'] = ($drRow['processedDate'] == '0000-00-00 00:00:00' ? 'N/A' : $drRow['processedDate']);
388 $reminders[$i]['sDate'] = $drRow['sDate'];
389 $reminders[$i]['dDate'] = $drRow['dDate'];
391 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
392 $reminders[$i]['PatientName'] = (empty($pRow) ? 'N/A' : $pRow['ptitle'].' '.$pRow['pfname'].' '.$pRow['pmname'].' '.$pRow['plname']);
393 // -------------------------------------
395 $reminders[$i]['message'] = $drRow['dr_message_text'];
396 $reminders[$i]['fromName'] = $drRow['ffname'].' '.$drRow['fmname'].' '.$drRow['flname'];
397 $reminders[$i]['ToName'] = $drRow['tfname'].' '.$drRow['tmname'].' '.$drRow['tlname'];
398 $reminders[$i]['processedByName'] = (empty($prRow) ? 'N/A' : $prRow['ptitle'].' '.$prRow['pfname'].' '.$prRow['pmname'].' '.$prRow['plname']);
400 // --------- END OF loop through the results
402 return $reminders;