minor improvement to tabs style
[openemr.git] / library / patient_tracker.inc.php
blobc555164083427284bfb11eb8de11d8ae628dc7ff
1 <?php
2 /**
3 * library/patient_tracker.inc.php Functions used in the Patient Flow Board.
4 *
5 * Functions for use in the Patient Flow Board and Patient Flow Board Reports.
6 *
7 *
8 * Copyright (C) 2015 Terry Hill <terry@lillysystems.com>
9 *
10 * LICENSE: This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 3
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
21 * @package OpenEMR
22 * @author Terry Hill <terry@lillysystems.com>
23 * @link http://www.open-emr.org
25 * Please help the overall project by sending changes you make to the author and to the OpenEMR community.
28 require_once(dirname(__FILE__) . '/appointments.inc.php');
30 function get_Tracker_Time_Interval ($tracker_from_time, $tracker_to_time, $allow_sec=false) {
32 $tracker_time_calc = strtotime($tracker_to_time) - strtotime($tracker_from_time);
34 $tracker_time = "";
35 if ($tracker_time_calc > 60*60*24) {
36 $days = floor($tracker_time_calc/60/60/24);
37 if($days >= 2){
38 $tracker_time .= "$days ". xl('days');
40 else
42 $tracker_time .= "$days ". xl('day');
44 $tracker_time_calc = $tracker_time_calc - ($days * (60*60*24));
46 if ($tracker_time_calc > 60*60) {
47 $hours = floor($tracker_time_calc/60/60);
48 if(strlen($days != 0)) {
49 if($hours >= 2){
50 $tracker_time .= ", $hours " . xl('hours');
52 else
54 $tracker_time .= ", $hours " . xl('hour');
57 else
59 if($hours >= 2){
60 $tracker_time .= "$hours " . xl('hours');
62 else
64 $tracker_time .= "$hours " . xl('hour');
67 $tracker_time_calc = $tracker_time_calc - ($hours * (60*60));
69 if ($allow_sec) {
70 if ($tracker_time_calc > 60) {
71 $minutes = floor($tracker_time_calc/60);
72 if(strlen($hours != 0)) {
73 if($minutes >= 2){
74 $tracker_time .= ", $minutes " . xl('minutes');
76 else
78 $tracker_time .= ", $minutes " . xl('minute');
81 else
83 if($minutes >= 2){
84 $tracker_time .= "$minutes " . xl('minutes');
86 else
88 $tracker_time .= "$minutes " . xl('minute');
91 $tracker_time_calc = $tracker_time_calc - ($minutes * 60);
94 else
96 $minutes = round($tracker_time_calc/60);
97 if(strlen($hours != 0)) {
98 if($minutes >= 2){
99 $tracker_time .= ", $minutes " . xl('minutes');
101 else
103 $tracker_time .= ", $minutes " . xl('minute');
106 else
108 if($minutes >= 2){
109 $tracker_time .= "$minutes " . xl('minutes');
111 else
113 if($minutes > 0){
114 $tracker_time .= "$minutes " . xl('minute');
118 $tracker_time_calc = $tracker_time_calc - ($minutes * 60);
120 if ($allow_sec) {
121 if ($tracker_time_calc > 0) {
122 if(strlen($minutes != 0)) {
123 if($tracker_time_calc >= 2){
124 $tracker_time .= ", $tracker_time_calc " . xl('seconds');
126 else
128 $tracker_time .= ", $tracker_time_calc " . xl('second');
131 else
133 if($tracker_time_calc >= 2){
134 $tracker_time .= "$tracker_time_calc " . xl('seconds');
136 else
138 $tracker_time .= "$tracker_time_calc " . xl('second');
143 return $tracker_time ;
146 function fetch_Patient_Tracker_Events($from_date, $to_date, $provider_id = null, $facility_id = null, $form_apptstatus = null, $form_apptcat =null)
148 # used to determine which providers to display in the Patient Tracker
149 if ($provider_id == 'ALL'){
150 //set null to $provider id if it's 'all'
151 $provider_id = null;
153 $events = fetchAppointments( $from_date, $to_date, null, $provider_id, $facility_id, $form_apptstatus, null, null, $form_apptcat, true );
154 return $events;
157 #check to see if a status code exist as a check in
158 function is_checkin($option) {
160 $row = sqlQuery("SELECT toggle_setting_1 FROM list_options WHERE " .
161 "list_id = 'apptstat' AND option_id = ? AND activity = 1", array($option));
162 if (empty($row['toggle_setting_1'])) return(false);
163 return(true);
166 #check to see if a status code exist as a check out
167 function is_checkout($option) {
169 $row = sqlQuery("SELECT toggle_setting_2 FROM list_options WHERE " .
170 "list_id = 'apptstat' AND option_id = ? AND activity = 1", array($option));
171 if (empty($row['toggle_setting_2'])) return(false);
172 return(true);
176 # This function will return false for both below scenarios:
177 # 1. The tracker item does not exist
178 # 2. If the tracker item does exist, but the encounter has not been set
179 function is_tracker_encounter_exist($apptdate,$appttime,$pid,$eid) {
180 #Check to see if there is an encounter in the patient_tracker table.
181 $enc_yn = sqlQuery("SELECT encounter from patient_tracker WHERE `apptdate` = ? AND `appttime` = ? " .
182 "AND `eid` = ? AND `pid` = ?", array($apptdate,$appttime,$eid,$pid));
183 if ($enc_yn['encounter'] == '0' || $enc_yn == '0') return(false);
184 return(true);
187 # this function will return the tracker id that is managed
188 # or will return false if no tracker id was managed (in the case of a recurrent appointment)
189 function manage_tracker_status($apptdate,$appttime,$eid,$pid,$user,$status='',$room='',$enc_id='') {
191 #First ensure the eid is not a recurrent appointment. If it is, then do not do anything and return false.
192 $pc_appt = sqlQuery("SELECT `pc_recurrtype` FROM `openemr_postcalendar_events` WHERE `pc_eid` = ?", array($eid));
193 if ($pc_appt['pc_recurrtype'] != 0) {
194 return false;
197 $datetime = date("Y-m-d H:i:s");
198 if (is_null($room)) {
199 $room = '';
201 #Check to see if there is an entry in the patient_tracker table.
202 $tracker = sqlQuery("SELECT id, apptdate, appttime, eid, pid, original_user, encounter, lastseq,".
203 "patient_tracker_element.room AS lastroom,patient_tracker_element.status AS laststatus ".
204 "from `patient_tracker`".
205 "LEFT JOIN patient_tracker_element " .
206 "ON patient_tracker.id = patient_tracker_element.pt_tracker_id " .
207 "AND patient_tracker.lastseq = patient_tracker_element.seq " .
208 "WHERE `apptdate` = ? AND `appttime` = ? " .
209 "AND `eid` = ? AND `pid` = ?", array($apptdate,$appttime,$eid,$pid));
211 if (empty($tracker)) {
212 #Add a new tracker.
213 $tracker_id = sqlInsert("INSERT INTO `patient_tracker` " .
214 "(`date`, `apptdate`, `appttime`, `eid`, `pid`, `original_user`, `encounter`, `lastseq`) " .
215 "VALUES (?,?,?,?,?,?,?,'1')",
216 array($datetime,$apptdate,$appttime,$eid,$pid,$user,$enc_id));
217 #If there is a status or a room, then add a tracker item.
218 if (!empty($status) || !empty($room)) {
219 sqlInsert("INSERT INTO `patient_tracker_element` " .
220 "(`pt_tracker_id`, `start_datetime`, `user`, `status`, `room`, `seq`) " .
221 "VALUES (?,?,?,?,?,'1')",
222 array($tracker_id,$datetime,$user,$status,$room));
225 else {
226 #Tracker already exists.
227 $tracker_id = $tracker['id'];
228 if (($status != $tracker['laststatus']) || ($room != $tracker['lastroom'])) {
229 #Status or room has changed, so need to update tracker.
230 #Update lastseq in tracker.
231 sqlStatement("UPDATE `patient_tracker` SET `lastseq` = ? WHERE `id` = ?",
232 array(($tracker['lastseq']+1),$tracker_id));
233 #Add a tracker item.
234 sqlInsert("INSERT INTO `patient_tracker_element` " .
235 "(`pt_tracker_id`, `start_datetime`, `user`, `status`, `room`, `seq`) " .
236 "VALUES (?,?,?,?,?,?)",
237 array($tracker_id,$datetime,$user,$status,$room,($tracker['lastseq']+1)));
239 if (!empty($enc_id)) {
240 #enc_id (encounter number) is not blank, so update this in tracker.
241 sqlStatement("UPDATE `patient_tracker` SET `encounter` = ? WHERE `id` = ?", array($enc_id,$tracker_id));
244 #Ensure the entry in calendar appt entry has been updated.
245 $pc_appt = sqlQuery("SELECT `pc_apptstatus`, `pc_room` FROM `openemr_postcalendar_events` WHERE `pc_eid` = ?", array($eid));
246 if ($status != $pc_appt['pc_apptstatus']) {
247 sqlStatement("UPDATE `openemr_postcalendar_events` SET `pc_apptstatus` = ? WHERE `pc_eid` = ?", array($status,$eid));
249 if ($room != $pc_appt['pc_room']) {
250 sqlStatement("UPDATE `openemr_postcalendar_events` SET `pc_room` = ? WHERE `pc_eid` = ?", array($room,$eid));
252 if( $GLOBALS['drug_screen'] && !empty($status) && is_checkin($status)) {
253 $yearly_limit = $GLOBALS['maximum_drug_test_yearly'];
254 $percentage = $GLOBALS['drug_testing_percentage'];
255 random_drug_test($tracker_id,$percentage,$yearly_limit);
257 # Returning the tracker id that has been managed
258 return $tracker_id;
261 # This is used to break apart the information contained in the notes field of
262 #list_options. Currently the color and alert time are the only items stored
263 function collectApptStatusSettings($option) {
264 $color_settings = array();
265 $row = sqlQuery("SELECT notes FROM list_options WHERE " .
266 "list_id = 'apptstat' AND option_id = ? AND activity = 1", array($option));
267 if (empty($row['notes'])) return $option;
268 list($color_settings['color'], $color_settings['time_alert']) = explode("|", $row['notes']);
269 return $color_settings;
272 # This is used to collect the tracker elements for the Patient Flow Board Report
273 # returns the elements in an array
274 function collect_Tracker_Elements($trackerid)
276 $res = sqlStatement("SELECT * FROM patient_tracker_element WHERE pt_tracker_id = ? ORDER BY LENGTH(seq), seq ", array($trackerid));
277 for($iter=0; $row=sqlFetchArray($res); $iter++) {
278 $returnval[$iter]=$row;
280 return $returnval;
283 #used to determine check in time
284 function collect_checkin($trackerid) {
285 $tracker = sqlQuery("SELECT patient_tracker_element.start_datetime " .
286 "FROM patient_tracker_element " .
287 "INNER JOIN list_options " .
288 "ON patient_tracker_element.status = list_options.option_id " .
289 "WHERE list_options.list_id = 'apptstat' " .
290 "AND list_options.toggle_setting_1 = '1' AND list_options.activity = 1 " .
291 "AND patient_tracker_element.pt_tracker_id = ?",
292 array($trackerid));
293 if (empty($tracker['start_datetime'])) {
294 return false;
296 else {
297 return $tracker['start_datetime'];
301 #used to determine check out time
302 function collect_checkout($trackerid) {
303 $tracker = sqlQuery("SELECT patient_tracker_element.start_datetime " .
304 "FROM patient_tracker_element " .
305 "INNER JOIN list_options " .
306 "ON patient_tracker_element.status = list_options.option_id " .
307 "WHERE list_options.list_id = 'apptstat' " .
308 "AND list_options.toggle_setting_2 = '1' AND list_options.activity = 1 " .
309 "AND patient_tracker_element.pt_tracker_id = ?",
310 array($trackerid));
311 if (empty($tracker['start_datetime'])) {
312 return false;
314 else {
315 return $tracker['start_datetime'];
319 function random_drug_test($tracker_id,$percentage,$yearly_limit) {
321 # Check if randomization has not yet been done (is random_drug_test NULL). If already done, then exit.
322 $drug_test_done = sqlQuery("SELECT `random_drug_test`, pid from patient_tracker " .
323 "WHERE id =? ", array($tracker_id));
324 $Patient_id = $drug_test_done['pid'];
326 if (is_null($drug_test_done['random_drug_test'])) {
327 # get a count of the number of times the patient has been screened.
328 if ($yearly_limit >0) {
329 # check to see if screens are within the current year.
330 $lastyear = date("Y-m-d",strtotime("-1 year", strtotime(date("Y-m-d H:i:s"))));
331 $drug_test_count = sqlQuery("SELECT COUNT(*) from patient_tracker " .
332 "WHERE drug_screen_completed = '1' AND apptdate >= ? AND pid =? ", array($lastyear,$Patient_id));
334 # check that the patient is not at the yearly limit.
335 if($drug_test_count['COUNT(*)'] >= $yearly_limit && ($yearly_limit >0)) {
337 $drugtest = 0;
339 else
341 # Now do the randomization and set random_drug_test to the outcome.
343 $drugtest = 0;
344 $testdrug = mt_rand(0,100);
345 if ($testdrug <= $percentage) {
346 $drugtest = 1;
350 #Update the tracker file.
351 sqlStatement("UPDATE patient_tracker SET " .
352 "random_drug_test = ? " .
353 "WHERE id =? ", array($drugtest,$tracker_id));
357 /* get information the statuses of the appointments*/
358 function getApptStatus($appointments){
360 $astat = array();
361 $astat['count_all'] = count($appointments);
362 //group the appointment by status
363 foreach($appointments as $appointment){
364 $astat[$appointment['pc_apptstatus']] += 1;
366 return $astat;