Internationalization: some more documentation fixes
[openemr.git] / library / appointments.inc.php
blobe828456d0cef32a5ef6f991ec612d91764997d10
1 <?php
3 // Copyright (C) 2011 Ken Chapple
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // Holds library functions (and hashes) used by the appointment reporting module
12 $COMPARE_FUNCTION_HASH = array(
13 'doctor' => 'compareAppointmentsByDoctorName',
14 'patient' => 'compareAppointmentsByPatientName',
15 'pubpid' => 'compareAppointmentsByPatientId',
16 'date' => 'compareAppointmentsByDate',
17 'time' => 'compareAppointmentsByTime',
18 'type' => 'compareAppointmentsByType',
19 'comment' => 'compareAppointmentsByComment'
22 $ORDERHASH = array(
23 'doctor' => array( 'doctor', 'date', 'time' ),
24 'patient' => array( 'patient', 'date', 'time' ),
25 'pubpid' => array( 'pubpid', 'date', 'time' ),
26 'date' => array( 'date', 'time', 'type', 'patient' ),
27 'time' => array( 'time', 'date', 'patient' ),
28 'type' => array( 'type', 'date', 'time', 'patient' ),
29 'comment' => array( 'comment', 'date', 'time', 'patient' )
32 function fetchEvents( $from_date, $to_date, $where_param = null, $orderby_param = null )
34 $where =
35 "( (e.pc_endDate >= '$from_date' AND e.pc_eventDate <= '$to_date' AND e.pc_recurrtype = '1') OR " .
36 "(e.pc_eventDate >= '$from_date' AND e.pc_eventDate <= '$to_date') )";
37 if ( $where_param ) $where .= $where_param;
39 $order_by = "e.pc_eventDate, e.pc_startTime";
40 if ( $orderby_param ) {
41 $order_by = $orderby_param;
44 $query = "SELECT " .
45 "e.pc_eventDate, e.pc_endDate, e.pc_startTime, e.pc_endTime, e.pc_duration, e.pc_recurrtype, e.pc_recurrspec, e.pc_recurrfreq, e.pc_catid, e.pc_eid, " .
46 "e.pc_title, e.pc_hometext, " .
47 "p.fname, p.mname, p.lname, p.pid, p.pubpid, " .
48 "u.fname AS ufname, u.mname AS umname, u.lname AS ulname, u.id AS uprovider_id, " .
49 "c.pc_catname, c.pc_catid " .
50 "FROM openemr_postcalendar_events AS e " .
51 "LEFT OUTER JOIN patient_data AS p ON p.pid = e.pc_pid " .
52 "LEFT OUTER JOIN users AS u ON u.id = e.pc_aid " .
53 "LEFT OUTER JOIN openemr_postcalendar_categories AS c ON c.pc_catid = e.pc_catid " .
54 "WHERE $where " .
55 "ORDER BY $order_by";
57 $res = sqlStatement( $query );
58 $events = array();
59 if ( $res )
61 while ( $row = sqlFetchArray($res) )
63 // if it's a repeating appointment, fetch all occurances in date range
64 if ( $row['pc_recurrtype'] ) {
65 $reccuringEvents = getRecurringEvents( $row, $from_date, $to_date );
66 $events = array_merge( $events, $reccuringEvents );
67 } else {
68 $events []= $row;
73 return $events;
76 function fetchAllEvents( $from_date, $to_date, $provider_id = null, $facility_id = null )
78 $where = "";
79 if ( $provider_id ) $where .= " AND e.pc_aid = '$provider_id'";
81 $facility_filter = '';
82 if ( $facility_id ) {
83 $event_facility_filter = " AND e.pc_facility = '$facility_id'";
84 $provider_facility_filter = " AND users.facility_id = '$facility_id'";
87 $where .= $facility_filter;
88 $appointments = fetchEvents( $from_date, $to_date, $where );
89 return $appointments;
92 function fetchAppointments( $from_date, $to_date, $patient_id = null, $provider_id = null, $facility_id = null )
94 $where = "";
95 if ( $provider_id ) $where .= " AND e.pc_aid = '$provider_id'";
96 if ( $patient_id ) {
97 $where .= " AND e.pc_pid = '$patient_id'";
98 } else {
99 $where .= " AND e.pc_pid != ''";
102 $facility_filter = '';
103 if ( $facility_id ) {
104 $event_facility_filter = " AND e.pc_facility = '$facility_id'";
105 $provider_facility_filter = " AND users.facility_id = '$facility_id'";
108 $where .= $facility_filter;
109 $appointments = fetchEvents( $from_date, $to_date, $where );
110 return $appointments;
113 function getRecurringEvents( $event, $from_date, $to_date )
115 $repeatEvents = array();
116 $from_date_time = strtotime( $from_date . " 00:00:00" );
117 $thistime = strtotime( $event['pc_eventDate'] . " 00:00:00" );
118 $thistime = max( $thistime, $from_date_time );
119 if ( $event['pc_recurrtype'] )
121 preg_match( '/"event_repeat_freq_type";s:1:"(\d)"/', $event['pc_recurrspec'], $matches );
122 $repeattype = $matches[1];
124 preg_match( '/"event_repeat_freq";s:1:"(\d)"/', $event['pc_recurrspec'], $matches );
125 $repeatfreq = $matches[1];
126 if ( !$repeatfreq ) $repeatfreq = 1;
128 $upToDate = strtotime( $to_date." 23:59:59" ); // set the up-to-date to the last second of the "to_date"
129 $endtime = strtotime( $event['pc_endDate'] . " 23:59:59" );
130 if ( $endtime > $upToDate ) $endtime = $upToDate;
132 $repeatix = 0;
133 while ( $thistime < $endtime )
135 // Skip the event if a repeat frequency > 1 was specified and this is
136 // not the desired occurrence.
137 if ( !$repeatix)
139 $newEvent = $event;
140 $eventDate = date( "Y-m-d", $thistime );
141 $newEvent['pc_eventDate'] = $eventDate;
142 $newEvent['pc_endDate'] = $eventDate;
143 $repeatEvents []= $newEvent;
146 if ( ++$repeatix >= $repeatfreq ) $repeatix = 0;
148 $adate = getdate($thistime);
149 if ($repeattype == 0) { // daily
150 $adate['mday'] += 1;
151 } else if ($repeattype == 1) { // weekly
152 $adate['mday'] += 7;
153 } else if ($repeattype == 2) { // monthly
154 $adate['mon'] += 1;
155 } else if ($repeattype == 3) { // yearly
156 $adate['year'] += 1;
157 } else if ($repeattype == 4) { // work days
158 if ($adate['wday'] == 5) // if friday, skip to monday
159 $adate['mday'] += 3;
160 else if ($adate['wday'] == 6) // saturday should not happen
161 $adate['mday'] += 2;
162 else
163 $adate['mday'] += 1;
164 } else {
165 die("Invalid repeat type '$repeattype'");
167 $thistime = mktime(0, 0, 0, $adate['mon'], $adate['mday'], $adate['year']);
171 return $repeatEvents;
174 // get the event slot size in seconds
175 function getSlotSize()
177 if ( isset( $GLOBALS['calendar_interval'] ) ) {
178 return $GLOBALS['calendar_interval'] * 60;
180 return 15 * 60;
183 function getAvailableSlots( $from_date, $to_date, $provider_id = null, $facility_id = null )
185 $appointments = fetchAllEvents( $from_date, $to_date, $provider_id, $facility_id );
186 $appointments = sortAppointments( $appointments, "date" );
187 $from_datetime = strtotime( $from_date." 00:00:00" );
188 $to_datetime = strtotime( $to_date." 23:59:59" );
189 $availableSlots = array();
190 $start_time = 0;
191 $date = 0;
192 for ( $i = 0; $i < count( $appointments ); ++$i )
194 if ( $appointments[$i]['pc_catid'] == 2 ) { // 2 == In Office
195 $start_time = $appointments[$i]['pc_startTime'];
196 $date = $appointments[$i]['pc_eventDate'];
197 $provider_id = $appointments[$i]['uprovider_id'];
198 } else if ( $appointments[$i]['pc_catid'] == 3 ) { // 3 == Out Of Office
199 continue;
200 } else {
201 $start_time = $appointments[$i]['pc_endTime'];
202 $date = $appointments[$i]['pc_eventDate'];
203 $provider_id = $appointments[$i]['uprovider_id'];
206 // find next appointment with the same provider
207 $next_appointment_date = 0;
208 $next_appointment_time = 0;
209 for ( $j = $i+1; $j < count( $appointments ); ++$j ) {
210 if ( $appointments[$j]['uprovider_id'] == $provider_id ) {
211 $next_appointment_date = $appointments[$j]['pc_eventDate'];
212 $next_appointment_time = $appointments[$j]['pc_startTime'];
213 break;
217 $same_day = ( strtotime( $next_appointment_date ) == strtotime( $date ) ) ? true : false;
219 if ( $next_appointment_time && $same_day ) {
220 // check the start time of the next appointment
222 $start_datetime = strtotime( $date." ".$start_time );
223 $next_appointment_datetime = strtotime( $next_appointment_date." ".$next_appointment_time );
224 $curr_time = $start_datetime;
225 while ( $curr_time < $next_appointment_datetime - (getSlotSize() / 2) ) {
226 //create a new appointment ever 15 minutes
227 $time = date( "H:i:s", $curr_time );
228 $available_slot = createAvailableSlot(
229 $appointments[$i]['pc_eventDate'],
230 $time,
231 $appointments[$i]['ufname'],
232 $appointments[$i]['ulname'],
233 $appointments[$i]['umname'] );
234 $availableSlots []= $available_slot;
235 $curr_time += getSlotSize(); // add a 15-minute slot
240 return $availableSlots;
243 function createAvailableSlot( $event_date, $start_time, $provider_fname, $provider_lname, $provider_mname = "", $cat_name = "Available" )
245 $newSlot = array();
246 $newSlot['ulname'] = $provider_lname;
247 $newSlot['ufname'] = $provider_fname;
248 $newSlot['umname'] = $provider_mname;
249 $newSlot['pc_eventDate'] = $event_date;
250 $newSlot['pc_startTime'] = $start_time;
251 $newSlot['pc_endTime'] = $start_time;
252 $newSlot['pc_catname'] = $cat_name;
253 return $newSlot;
256 function getCompareFunction( $code ) {
257 global $COMPARE_FUNCTION_HASH;
258 return $COMPARE_FUNCTION_HASH[$code];
261 function getComparisonOrder( $code ) {
262 global $ORDERHASH;
263 return $ORDERHASH[$code];
266 function sortAppointments( array $appointments, $orderBy = 'date' )
268 global $appointment_sort_order;
269 $appointment_sort_order = $orderBy;
270 usort( $appointments, "compareAppointments" );
271 return $appointments;
274 // cmp_function for usort
275 // The comparison function must return an integer less than, equal to,
276 // or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
277 function compareAppointments( $appointment1, $appointment2 )
279 global $appointment_sort_order;
280 $comparisonOrder = getComparisonOrder( $appointment_sort_order );
281 foreach ( $comparisonOrder as $comparison )
283 $cmp_function = getCompareFunction( $comparison );
284 $result = $cmp_function( $appointment1, $appointment2 );
285 if ( 0 != $result ) {
286 return $result;
290 return 0;
293 function compareBasic( $e1, $e2 )
295 if ( $e1 < $e2 ) {
296 return -1;
297 } else if ( $e1 > $e2 ) {
298 return 1;
301 return 0;
304 function compareAppointmentsByDate( $appointment1, $appointment2 )
306 $date1 = strtotime( $appointment1['pc_eventDate'] );
307 $date2 = strtotime( $appointment2['pc_eventDate'] );
309 return compareBasic( $date1, $date2 );
312 function compareAppointmentsByTime( $appointment1, $appointment2 )
314 $time1 = strtotime( $appointment1['pc_startTime'] );
315 $time2 = strtotime( $appointment2['pc_startTime'] );
317 return compareBasic( $time1, $time2 );
320 function compareAppointmentsByDoctorName( $appointment1, $appointment2 )
322 $name1 = $appointment1['ulname'];
323 $name2 = $appointment2['ulname'];
324 $cmp = compareBasic( $name1, $name2 );
325 if ( $cmp == 0 ) {
326 $name1 = $appointment1['ufname'];
327 $name2 = $appointment2['ufname'];
328 return compareBasic( $name1, $name2 );
331 return $cmp;
334 function compareAppointmentsByPatientName( $appointment1, $appointment2 )
336 $name1 = $appointment1['lname'];
337 $name2 = $appointment2['lname'];
338 $cmp = compareBasic( $name1, $name2 );
339 if ( $cmp == 0 ) {
340 $name1 = $appointment1['fname'];
341 $name2 = $appointment2['fname'];
342 return compareBasic( $name1, $name2 );
345 return $cmp;
348 function compareAppointmentsByType( $appointment1, $appointment2 )
350 $type1 = $appointment1['pc_catid'];
351 $type2 = $appointment2['pc_catid'];
352 return compareBasic( $type1, $type2 );
355 function compareAppointmentsByPatientId( $appointment1, $appointment2 )
357 $id1 = $appointment1['pubpid'];
358 $id2 = $appointment2['pubpid'];
359 return compareBasic( $id1, $id2 );
362 function compareAppointmentsByComment( $appointment1, $appointment2 )
364 $comment1 = $appointment1['pc_hometext'];
365 $comment2 = $appointment2['pc_hometext'];
366 return compareBasic( $comment1, $comment2 );