Added access controls for encounter categories
[openemr.git] / library / patient_tracker.inc.php
blob07457104bc22f890420f1b86c9852d5796c51ab4
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');
41 else
43 $tracker_time .= "$days ". xl('day');
45 $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');
53 else
55 $tracker_time .= ", $hours " . xl('hour');
58 else
60 if($hours >= 2){
61 $tracker_time .= "$hours " . xl('hours');
63 else
65 $tracker_time .= "$hours " . xl('hour');
68 $tracker_time_calc = $tracker_time_calc - ($hours * (60*60));
70 if ($allow_sec) {
71 if ($tracker_time_calc > 60) {
72 $minutes = floor($tracker_time_calc/60);
73 if(strlen($hours != 0)) {
74 if($minutes >= 2){
75 $tracker_time .= ", $minutes " . xl('minutes');
77 else
79 $tracker_time .= ", $minutes " . xl('minute');
82 else
84 if($minutes >= 2){
85 $tracker_time .= "$minutes " . xl('minutes');
87 else
89 $tracker_time .= "$minutes " . xl('minute');
92 $tracker_time_calc = $tracker_time_calc - ($minutes * 60);
95 else
97 $minutes = round($tracker_time_calc/60);
98 if(strlen($hours != 0)) {
99 if($minutes >= 2){
100 $tracker_time .= ", $minutes " . xl('minutes');
102 else
104 $tracker_time .= ", $minutes " . xl('minute');
107 else
109 if($minutes >= 2){
110 $tracker_time .= "$minutes " . xl('minutes');
112 else
114 if($minutes > 0){
115 $tracker_time .= "$minutes " . xl('minute');
119 $tracker_time_calc = $tracker_time_calc - ($minutes * 60);
121 if ($allow_sec) {
122 if ($tracker_time_calc > 0) {
123 if(strlen($minutes != 0)) {
124 if($tracker_time_calc >= 2){
125 $tracker_time .= ", $tracker_time_calc " . xl('seconds');
127 else
129 $tracker_time .= ", $tracker_time_calc " . xl('second');
132 else
134 if($tracker_time_calc >= 2){
135 $tracker_time .= "$tracker_time_calc " . xl('seconds');
137 else
139 $tracker_time .= "$tracker_time_calc " . xl('second');
144 return $tracker_time ;
147 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)
149 # used to determine which providers to display in the Patient Tracker
150 if ($provider_id == 'ALL'){
151 //set null to $provider id if it's 'all'
152 $provider_id = null;
154 $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);
155 return $events;
158 #check to see if a status code exist as a check in
159 function is_checkin($option)
162 $row = sqlQuery("SELECT toggle_setting_1 FROM list_options WHERE " .
163 "list_id = 'apptstat' AND option_id = ? AND activity = 1", array($option));
164 if (empty($row['toggle_setting_1'])) return(false);
165 return(true);
168 #check to see if a status code exist as a check out
169 function is_checkout($option)
172 $row = sqlQuery("SELECT toggle_setting_2 FROM list_options WHERE " .
173 "list_id = 'apptstat' AND option_id = ? AND activity = 1", array($option));
174 if (empty($row['toggle_setting_2'])) return(false);
175 return(true);
179 # This function will return false for both below scenarios:
180 # 1. The tracker item does not exist
181 # 2. If the tracker item does exist, but the encounter has not been set
182 function is_tracker_encounter_exist($apptdate,$appttime,$pid,$eid)
184 #Check to see if there is an encounter in the patient_tracker table.
185 $enc_yn = sqlQuery("SELECT encounter from patient_tracker WHERE `apptdate` = ? AND `appttime` = ? " .
186 "AND `eid` = ? AND `pid` = ?", array($apptdate,$appttime,$eid,$pid));
187 if ($enc_yn['encounter'] == '0' || $enc_yn == '0') return(false);
188 return(true);
191 # this function will return the tracker id that is managed
192 # or will return false if no tracker id was managed (in the case of a recurrent appointment)
193 function manage_tracker_status($apptdate,$appttime,$eid,$pid,$user,$status='',$room='',$enc_id='')
196 #First ensure the eid is not a recurrent appointment. If it is, then do not do anything and return false.
197 $pc_appt = sqlQuery("SELECT `pc_recurrtype` FROM `openemr_postcalendar_events` WHERE `pc_eid` = ?", array($eid));
198 if ($pc_appt['pc_recurrtype'] != 0) {
199 return false;
202 $datetime = date("Y-m-d H:i:s");
203 if (is_null($room)) {
204 $room = '';
206 #Check to see if there is an entry in the patient_tracker table.
207 $tracker = sqlQuery("SELECT id, apptdate, appttime, eid, pid, original_user, encounter, lastseq,".
208 "patient_tracker_element.room AS lastroom,patient_tracker_element.status AS laststatus ".
209 "from `patient_tracker`".
210 "LEFT JOIN patient_tracker_element " .
211 "ON patient_tracker.id = patient_tracker_element.pt_tracker_id " .
212 "AND patient_tracker.lastseq = patient_tracker_element.seq " .
213 "WHERE `apptdate` = ? AND `appttime` = ? " .
214 "AND `eid` = ? AND `pid` = ?", array($apptdate,$appttime,$eid,$pid));
216 if (empty($tracker)) {
217 #Add a new tracker.
218 $tracker_id = sqlInsert("INSERT INTO `patient_tracker` " .
219 "(`date`, `apptdate`, `appttime`, `eid`, `pid`, `original_user`, `encounter`, `lastseq`) " .
220 "VALUES (?,?,?,?,?,?,?,'1')",
221 array($datetime,$apptdate,$appttime,$eid,$pid,$user,$enc_id));
222 #If there is a status or a room, then add a tracker item.
223 if (!empty($status) || !empty($room)) {
224 sqlInsert("INSERT INTO `patient_tracker_element` " .
225 "(`pt_tracker_id`, `start_datetime`, `user`, `status`, `room`, `seq`) " .
226 "VALUES (?,?,?,?,?,'1')",
227 array($tracker_id,$datetime,$user,$status,$room));
230 else {
231 #Tracker already exists.
232 $tracker_id = $tracker['id'];
233 if (($status != $tracker['laststatus']) || ($room != $tracker['lastroom'])) {
234 #Status or room has changed, so need to update tracker.
235 #Update lastseq in tracker.
236 sqlStatement("UPDATE `patient_tracker` SET `lastseq` = ? WHERE `id` = ?",
237 array(($tracker['lastseq']+1),$tracker_id));
238 #Add a tracker item.
239 sqlInsert("INSERT INTO `patient_tracker_element` " .
240 "(`pt_tracker_id`, `start_datetime`, `user`, `status`, `room`, `seq`) " .
241 "VALUES (?,?,?,?,?,?)",
242 array($tracker_id,$datetime,$user,$status,$room,($tracker['lastseq']+1)));
244 if (!empty($enc_id)) {
245 #enc_id (encounter number) is not blank, so update this in tracker.
246 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));
254 if ($room != $pc_appt['pc_room']) {
255 sqlStatement("UPDATE `openemr_postcalendar_events` SET `pc_room` = ? WHERE `pc_eid` = ?", array($room,$eid));
257 if( $GLOBALS['drug_screen'] && !empty($status) && is_checkin($status)) {
258 $yearly_limit = $GLOBALS['maximum_drug_test_yearly'];
259 $percentage = $GLOBALS['drug_testing_percentage'];
260 random_drug_test($tracker_id,$percentage,$yearly_limit);
262 # Returning the tracker id that has been managed
263 return $tracker_id;
266 # This is used to break apart the information contained in the notes field of
267 #list_options. Currently the color and alert time are the only items stored
268 function collectApptStatusSettings($option)
270 $color_settings = array();
271 $row = sqlQuery("SELECT notes FROM list_options WHERE " .
272 "list_id = 'apptstat' AND option_id = ? AND activity = 1", array($option));
273 if (empty($row['notes'])) return $option;
274 list($color_settings['color'], $color_settings['time_alert']) = explode("|", $row['notes']);
275 return $color_settings;
278 # This is used to collect the tracker elements for the Patient Flow Board Report
279 # returns the elements in an array
280 function collect_Tracker_Elements($trackerid)
282 $res = sqlStatement("SELECT * FROM patient_tracker_element WHERE pt_tracker_id = ? ORDER BY LENGTH(seq), seq ", array($trackerid));
283 for($iter=0; $row=sqlFetchArray($res); $iter++) {
284 $returnval[$iter]=$row;
286 return $returnval;
289 #used to determine check in time
290 function collect_checkin($trackerid)
292 $tracker = sqlQuery("SELECT patient_tracker_element.start_datetime " .
293 "FROM patient_tracker_element " .
294 "INNER JOIN list_options " .
295 "ON patient_tracker_element.status = list_options.option_id " .
296 "WHERE list_options.list_id = 'apptstat' " .
297 "AND list_options.toggle_setting_1 = '1' AND list_options.activity = 1 " .
298 "AND patient_tracker_element.pt_tracker_id = ?",
299 array($trackerid));
300 if (empty($tracker['start_datetime'])) {
301 return false;
303 else {
304 return $tracker['start_datetime'];
308 #used to determine check out time
309 function collect_checkout($trackerid)
311 $tracker = sqlQuery("SELECT patient_tracker_element.start_datetime " .
312 "FROM patient_tracker_element " .
313 "INNER JOIN list_options " .
314 "ON patient_tracker_element.status = list_options.option_id " .
315 "WHERE list_options.list_id = 'apptstat' " .
316 "AND list_options.toggle_setting_2 = '1' AND list_options.activity = 1 " .
317 "AND patient_tracker_element.pt_tracker_id = ?",
318 array($trackerid));
319 if (empty($tracker['start_datetime'])) {
320 return false;
322 else {
323 return $tracker['start_datetime'];
327 function random_drug_test($tracker_id,$percentage,$yearly_limit)
330 # Check if randomization has not yet been done (is random_drug_test NULL). If already done, then exit.
331 $drug_test_done = sqlQuery("SELECT `random_drug_test`, pid from patient_tracker " .
332 "WHERE id =? ", array($tracker_id));
333 $Patient_id = $drug_test_done['pid'];
335 if (is_null($drug_test_done['random_drug_test'])) {
336 # get a count of the number of times the patient has been screened.
337 if ($yearly_limit >0) {
338 # check to see if screens are within the current year.
339 $lastyear = date("Y-m-d",strtotime("-1 year", strtotime(date("Y-m-d H:i:s"))));
340 $drug_test_count = sqlQuery("SELECT COUNT(*) from patient_tracker " .
341 "WHERE drug_screen_completed = '1' AND apptdate >= ? AND pid =? ", array($lastyear,$Patient_id));
343 # check that the patient is not at the yearly limit.
344 if($drug_test_count['COUNT(*)'] >= $yearly_limit && ($yearly_limit >0)) {
346 $drugtest = 0;
348 else
350 # Now do the randomization and set random_drug_test to the outcome.
352 $drugtest = 0;
353 $testdrug = mt_rand(0,100);
354 if ($testdrug <= $percentage) {
355 $drugtest = 1;
359 #Update the tracker file.
360 sqlStatement("UPDATE patient_tracker SET " .
361 "random_drug_test = ? " .
362 "WHERE id =? ", array($drugtest,$tracker_id));
366 /* get information the statuses of the appointments*/
367 function getApptStatus($appointments)
370 $astat = array();
371 $astat['count_all'] = count($appointments);
372 //group the appointment by status
373 foreach($appointments as $appointment){
374 $astat[$appointment['pc_apptstatus']] += 1;
376 return $astat;