psr fix to prior commit
[openemr.git] / library / patient_tracker.inc.php
blob4394cd89788ce34288987dfcc4e8542a1a58ad13
1 <?php
2 /**
3 * library/patient_tracker.inc.php Functions used in the Patient Flow Board.
5 * Functions for use in the Patient Flow Board and Patient Flow Board Reports.
8 * Copyright (C) 2015 Terry Hill <terry@lillysystems.com>
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)
33 $tracker_time_calc = strtotime($tracker_to_time) - strtotime($tracker_from_time);
35 $tracker_time = "";
36 if ($tracker_time_calc > 60*60*24) {
37 $days = floor($tracker_time_calc/60/60/24);
38 if ($days >= 2) {
39 $tracker_time .= "$days ". xl('days');
40 } else {
41 $tracker_time .= "$days ". xl('day');
44 $tracker_time_calc = $tracker_time_calc - ($days * (60*60*24));
47 if ($tracker_time_calc > 60*60) {
48 $hours = floor($tracker_time_calc/60/60);
49 if (strlen($days != 0)) {
50 if ($hours >= 2) {
51 $tracker_time .= ", $hours " . xl('hours');
52 } else {
53 $tracker_time .= ", $hours " . xl('hour');
55 } else {
56 if ($hours >= 2) {
57 $tracker_time .= "$hours " . xl('hours');
58 } else {
59 $tracker_time .= "$hours " . xl('hour');
63 $tracker_time_calc = $tracker_time_calc - ($hours * (60*60));
66 if ($allow_sec) {
67 if ($tracker_time_calc > 60) {
68 $minutes = floor($tracker_time_calc/60);
69 if (strlen($hours != 0)) {
70 if ($minutes >= 2) {
71 $tracker_time .= ", $minutes " . xl('minutes');
72 } else {
73 $tracker_time .= ", $minutes " . xl('minute');
75 } else {
76 if ($minutes >= 2) {
77 $tracker_time .= "$minutes " . xl('minutes');
78 } else {
79 $tracker_time .= "$minutes " . xl('minute');
83 $tracker_time_calc = $tracker_time_calc - ($minutes * 60);
85 } else {
86 $minutes = round($tracker_time_calc/60);
87 if (strlen($hours != 0)) {
88 if ($minutes >= 2) {
89 $tracker_time .= ", $minutes " . xl('minutes');
90 } else {
91 $tracker_time .= ", $minutes " . xl('minute');
93 } else {
94 if ($minutes >= 2) {
95 $tracker_time .= "$minutes " . xl('minutes');
96 } else {
97 if ($minutes > 0) {
98 $tracker_time .= "$minutes " . xl('minute');
103 $tracker_time_calc = $tracker_time_calc - ($minutes * 60);
106 if ($allow_sec) {
107 if ($tracker_time_calc > 0) {
108 if (strlen($minutes != 0)) {
109 if ($tracker_time_calc >= 2) {
110 $tracker_time .= ", $tracker_time_calc " . xl('seconds');
111 } else {
112 $tracker_time .= ", $tracker_time_calc " . xl('second');
114 } else {
115 if ($tracker_time_calc >= 2) {
116 $tracker_time .= "$tracker_time_calc " . xl('seconds');
117 } else {
118 $tracker_time .= "$tracker_time_calc " . xl('second');
124 return $tracker_time ;
127 function fetch_Patient_Tracker_Events($from_date, $to_date, $provider_id = null, $facility_id = null, $form_apptstatus = null, $form_apptcat = null, $form_patient_name = null, $form_patient_id = null)
129 # used to determine which providers to display in the Patient Tracker
130 if ($provider_id == 'ALL') {
131 //set null to $provider id if it's 'all'
132 $provider_id = null;
135 $events = fetchAppointments($from_date, $to_date, $form_patient_id, $provider_id, $facility_id, $form_apptstatus, null, null, $form_apptcat, true, 0, null, $form_patient_name);
136 return $events;
139 #check to see if a status code exist as a check in
140 function is_checkin($option)
143 $row = sqlQuery("SELECT toggle_setting_1 FROM list_options WHERE " .
144 "list_id = 'apptstat' AND option_id = ? AND activity = 1", array($option));
145 if (empty($row['toggle_setting_1'])) {
146 return(false);
149 return(true);
152 #check to see if a status code exist as a check out
153 function is_checkout($option)
156 $row = sqlQuery("SELECT toggle_setting_2 FROM list_options WHERE " .
157 "list_id = 'apptstat' AND option_id = ? AND activity = 1", array($option));
158 if (empty($row['toggle_setting_2'])) {
159 return(false);
162 return(true);
166 # This function will return false for both below scenarios:
167 # 1. The tracker item does not exist
168 # 2. If the tracker item does exist, but the encounter has not been set
169 function is_tracker_encounter_exist($apptdate, $appttime, $pid, $eid)
171 #Check to see if there is an encounter in the patient_tracker table.
172 $enc_yn = sqlQuery("SELECT encounter from patient_tracker WHERE `apptdate` = ? AND `appttime` = ? " .
173 "AND `eid` = ? AND `pid` = ?", array($apptdate,$appttime,$eid,$pid));
174 if ($enc_yn['encounter'] == '0' || $enc_yn == '0') {
175 return(false);
178 return(true);
181 # this function will return the tracker id that is managed
182 # or will return false if no tracker id was managed (in the case of a recurrent appointment)
183 function manage_tracker_status($apptdate, $appttime, $eid, $pid, $user, $status = '', $room = '', $enc_id = '')
186 #First ensure the eid is not a recurrent appointment. If it is, then do not do anything and return false.
187 $pc_appt = sqlQuery("SELECT `pc_recurrtype` FROM `openemr_postcalendar_events` WHERE `pc_eid` = ?", array($eid));
188 if ($pc_appt['pc_recurrtype'] != 0) {
189 return false;
192 $datetime = date("Y-m-d H:i:s");
193 if (is_null($room)) {
194 $room = '';
197 #Check to see if there is an entry in the patient_tracker table.
198 $tracker = sqlQuery("SELECT id, apptdate, appttime, eid, pid, original_user, encounter, lastseq,".
199 "patient_tracker_element.room AS lastroom,patient_tracker_element.status AS laststatus ".
200 "from `patient_tracker`".
201 "LEFT JOIN patient_tracker_element " .
202 "ON patient_tracker.id = patient_tracker_element.pt_tracker_id " .
203 "AND patient_tracker.lastseq = patient_tracker_element.seq " .
204 "WHERE `apptdate` = ? AND `appttime` = ? " .
205 "AND `eid` = ? AND `pid` = ?", array($apptdate,$appttime,$eid,$pid));
207 if (empty($tracker)) {
208 #Add a new tracker.
209 $tracker_id = sqlInsert(
210 "INSERT INTO `patient_tracker` " .
211 "(`date`, `apptdate`, `appttime`, `eid`, `pid`, `original_user`, `encounter`, `lastseq`) " .
212 "VALUES (?,?,?,?,?,?,?,'1')",
213 array($datetime,$apptdate,$appttime,$eid,$pid,$user,$enc_id)
215 #If there is a status or a room, then add a tracker item.
216 if (!empty($status) || !empty($room)) {
217 sqlInsert(
218 "INSERT INTO `patient_tracker_element` " .
219 "(`pt_tracker_id`, `start_datetime`, `user`, `status`, `room`, `seq`) " .
220 "VALUES (?,?,?,?,?,'1')",
221 array($tracker_id,$datetime,$user,$status,$room)
224 } else {
225 #Tracker already exists.
226 $tracker_id = $tracker['id'];
227 if (($status != $tracker['laststatus']) || ($room != $tracker['lastroom'])) {
228 #Status or room has changed, so need to update tracker.
229 #Update lastseq in tracker.
230 sqlStatement(
231 "UPDATE `patient_tracker` SET `lastseq` = ? WHERE `id` = ?",
232 array(($tracker['lastseq']+1),$tracker_id)
234 #Add a tracker item.
235 sqlInsert(
236 "INSERT INTO `patient_tracker_element` " .
237 "(`pt_tracker_id`, `start_datetime`, `user`, `status`, `room`, `seq`) " .
238 "VALUES (?,?,?,?,?,?)",
239 array($tracker_id,$datetime,$user,$status,$room,($tracker['lastseq']+1))
243 if (!empty($enc_id)) {
244 #enc_id (encounter number) is not blank, so update this in tracker.
245 sqlStatement("UPDATE `patient_tracker` SET `encounter` = ? WHERE `id` = ?", array($enc_id,$tracker_id));
249 #Ensure the entry in calendar appt entry has been updated.
250 $pc_appt = sqlQuery("SELECT `pc_apptstatus`, `pc_room` FROM `openemr_postcalendar_events` WHERE `pc_eid` = ?", array($eid));
251 if ($status != $pc_appt['pc_apptstatus']) {
252 sqlStatement("UPDATE `openemr_postcalendar_events` SET `pc_apptstatus` = ? WHERE `pc_eid` = ?", array($status,$eid));
255 if ($room != $pc_appt['pc_room']) {
256 sqlStatement("UPDATE `openemr_postcalendar_events` SET `pc_room` = ? WHERE `pc_eid` = ?", array($room,$eid));
259 if ($GLOBALS['drug_screen'] && !empty($status) && is_checkin($status)) {
260 $yearly_limit = $GLOBALS['maximum_drug_test_yearly'];
261 $percentage = $GLOBALS['drug_testing_percentage'];
262 random_drug_test($tracker_id, $percentage, $yearly_limit);
265 # Returning the tracker id that has been managed
266 return $tracker_id;
269 # This is used to break apart the information contained in the notes field of
270 #list_options. Currently the color and alert time are the only items stored
271 function collectApptStatusSettings($option)
273 $color_settings = array();
274 $row = sqlQuery("SELECT notes FROM list_options WHERE " .
275 "list_id = 'apptstat' AND option_id = ? AND activity = 1", array($option));
276 if (empty($row['notes'])) {
277 return $option;
280 list($color_settings['color'], $color_settings['time_alert']) = explode("|", $row['notes']);
281 return $color_settings;
284 # This is used to collect the tracker elements for the Patient Flow Board Report
285 # returns the elements in an array
286 function collect_Tracker_Elements($trackerid)
288 $res = sqlStatement("SELECT * FROM patient_tracker_element WHERE pt_tracker_id = ? ORDER BY LENGTH(seq), seq ", array($trackerid));
289 for ($iter=0; $row=sqlFetchArray($res); $iter++) {
290 $returnval[$iter]=$row;
293 return $returnval;
296 #used to determine check in time
297 function collect_checkin($trackerid)
299 $tracker = sqlQuery(
300 "SELECT patient_tracker_element.start_datetime " .
301 "FROM patient_tracker_element " .
302 "INNER JOIN list_options " .
303 "ON patient_tracker_element.status = list_options.option_id " .
304 "WHERE list_options.list_id = 'apptstat' " .
305 "AND list_options.toggle_setting_1 = '1' AND list_options.activity = 1 " .
306 "AND patient_tracker_element.pt_tracker_id = ?",
307 array($trackerid)
309 if (empty($tracker['start_datetime'])) {
310 return false;
311 } else {
312 return $tracker['start_datetime'];
316 #used to determine check out time
317 function collect_checkout($trackerid)
319 $tracker = sqlQuery(
320 "SELECT patient_tracker_element.start_datetime " .
321 "FROM patient_tracker_element " .
322 "INNER JOIN list_options " .
323 "ON patient_tracker_element.status = list_options.option_id " .
324 "WHERE list_options.list_id = 'apptstat' " .
325 "AND list_options.toggle_setting_2 = '1' AND list_options.activity = 1 " .
326 "AND patient_tracker_element.pt_tracker_id = ?",
327 array($trackerid)
329 if (empty($tracker['start_datetime'])) {
330 return false;
331 } else {
332 return $tracker['start_datetime'];
336 function random_drug_test($tracker_id, $percentage, $yearly_limit)
339 # Check if randomization has not yet been done (is random_drug_test NULL). If already done, then exit.
340 $drug_test_done = sqlQuery("SELECT `random_drug_test`, pid from patient_tracker " .
341 "WHERE id =? ", array($tracker_id));
342 $Patient_id = $drug_test_done['pid'];
344 if (is_null($drug_test_done['random_drug_test'])) {
345 # get a count of the number of times the patient has been screened.
346 if ($yearly_limit >0) {
347 # check to see if screens are within the current year.
348 $lastyear = date("Y-m-d", strtotime("-1 year", strtotime(date("Y-m-d H:i:s"))));
349 $drug_test_count = sqlQuery("SELECT COUNT(*) from patient_tracker " .
350 "WHERE drug_screen_completed = '1' AND apptdate >= ? AND pid =? ", array($lastyear,$Patient_id));
353 # check that the patient is not at the yearly limit.
354 if ($drug_test_count['COUNT(*)'] >= $yearly_limit && ($yearly_limit >0)) {
355 $drugtest = 0;
356 } else {
357 # Now do the randomization and set random_drug_test to the outcome.
359 $drugtest = 0;
360 $testdrug = mt_rand(0, 100);
361 if ($testdrug <= $percentage) {
362 $drugtest = 1;
366 #Update the tracker file.
367 sqlStatement("UPDATE patient_tracker SET " .
368 "random_drug_test = ? " .
369 "WHERE id =? ", array($drugtest,$tracker_id));
373 /* get information the statuses of the appointments*/
374 function getApptStatus($appointments)
377 $astat = array();
378 $astat['count_all'] = count($appointments);
379 //group the appointment by status
380 foreach ($appointments as $appointment) {
381 $astat[$appointment['pc_apptstatus']] += 1;
384 return $astat;