some portal work
[openemr.git] / library / appointments.inc.php
blob3899936be7690611e86d73076ec0a647c937af6e
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
14 require_once(dirname(__FILE__)."/encounter_events.inc.php");
15 require_once(dirname(__FILE__)."/../interface/main/calendar/modules/PostCalendar/pnincludes/Date/Calc.php");
17 use OpenEMR\Events\Appointments\AppointmentsFilterEvent;
18 use OpenEMR\Events\BoundFilter;
20 $COMPARE_FUNCTION_HASH = array(
21 'doctor' => 'compareAppointmentsByDoctorName',
22 'patient' => 'compareAppointmentsByPatientName',
23 'pubpid' => 'compareAppointmentsByPatientId',
24 'date' => 'compareAppointmentsByDate',
25 'time' => 'compareAppointmentsByTime',
26 'type' => 'compareAppointmentsByType',
27 'comment' => 'compareAppointmentsByComment',
28 'status' => 'compareAppointmentsByStatus',
29 'completed' => 'compareAppointmentsByCompletedDrugScreen',
30 'trackerstatus' => 'compareAppointmentsByTrackerStatus'
33 $ORDERHASH = array(
34 'doctor' => array( 'doctor', 'date', 'time' ),
35 'patient' => array( 'patient', 'date', 'time' ),
36 'pubpid' => array( 'pubpid', 'date', 'time' ),
37 'date' => array( 'date', 'time', 'type', 'patient' ),
38 'time' => array( 'time', 'date', 'patient' ),
39 'type' => array( 'type', 'date', 'time', 'patient' ),
40 'comment' => array( 'comment', 'date', 'time', 'patient' ),
41 'status' => array( 'status', 'date', 'time', 'patient' ),
42 'completed' => array( 'completed', 'date', 'time', 'patient' ),
43 'trackerstatus' => array( 'trackerstatus', 'date', 'time', 'patient' ),
46 /*Arrays for the interpretation of recurrence patterns.*/
47 $REPEAT_FREQ = array(
48 '1' => xl('Every'),
49 '2' => xl('Every 2nd'),
50 '3' => xl('Every 3rd'),
51 '4' => xl('Every 4th'),
52 '5' => xl('Every 5th'),
53 '6' => xl('Every 6th')
56 $REPEAT_FREQ_TYPE = array(
57 '0' => xl('day'),
58 '1' => xl('week'),
59 '2' => xl('month'),
60 '3' => xl('year'),
61 '4' => xl('workday')
64 $REPEAT_ON_NUM = array(
65 '1' => xl('1st{{nth}}'),
66 '2' => xl('2nd{{nth}}'),
67 '3' => xl('3rd{{nth}}'),
68 '4' => xl('4th{{nth}}'),
69 '5' => xl('Last')
72 $REPEAT_ON_DAY = array(
73 '0' => xl('Sunday'),
74 '1' => xl('Monday'),
75 '2' => xl('Tuesday'),
76 '3' => xl('Wednesday'),
77 '4' => xl('Thursday'),
78 '5' => xl('Friday'),
79 '6' => xl('Saturday')
82 function checkEvent($recurrtype, $recurrspec)
85 $eFlag = 0;
87 switch ($recurrtype) {
88 case 1:
89 case 3:
90 if (empty($recurrspec['event_repeat_freq']) || !isset($recurrspec['event_repeat_freq_type'])) {
91 $eFlag = 1; }
93 break;
95 case 2:
96 if (empty($recurrspec['event_repeat_on_freq']) || empty($recurrspec['event_repeat_on_num']) || !isset($recurrspec['event_repeat_on_day'])) {
97 $eFlag = 1; }
99 break;
102 return $eFlag;
105 function fetchEvents($from_date, $to_date, $where_param = null, $orderby_param = null, $tracker_board = false, $nextX = 0, $bind_param = null, $query_param = null)
108 $sqlBindArray = array();
110 if ($query_param) {
111 $query = $query_param;
113 if ($bind_param) {
114 $sqlBindArray = $bind_param;
116 } else {
117 //////
118 if ($nextX) {
119 $where =
120 "((e.pc_endDate >= ? AND e.pc_recurrtype > '0') OR " .
121 "(e.pc_eventDate >= ?))";
123 array_push($sqlBindArray, $from_date, $from_date);
124 } else {
125 //////
126 $where =
127 "((e.pc_endDate >= ? AND e.pc_eventDate <= ? AND e.pc_recurrtype > '0') OR " .
128 "(e.pc_eventDate >= ? AND e.pc_eventDate <= ?))";
130 array_push($sqlBindArray, $from_date, $to_date, $from_date, $to_date);
133 if ($where_param) {
134 $where .= $where_param;
137 // Filter out appointments based on a custom module filter
138 $apptFilterEvent = new AppointmentsFilterEvent(new BoundFilter());
139 $apptFilterEvent = $GLOBALS["kernel"]->getEventDispatcher()->dispatch(AppointmentsFilterEvent::EVENT_HANDLE, $apptFilterEvent, 10);
140 $boundFilter = $apptFilterEvent->getBoundFilter();
141 $sqlBindArray = array_merge($sqlBindArray, $boundFilter->getBoundValues());
142 $where .= " AND ".$boundFilter->getFilterClause();
144 $order_by = "e.pc_eventDate, e.pc_startTime";
145 if ($orderby_param) {
146 $order_by = $orderby_param;
149 // Tracker Board specific stuff
150 $tracker_fields = '';
151 $tracker_joins = '';
152 if ($tracker_board) {
153 $tracker_fields = "e.pc_room, e.pc_pid, t.id, t.date, t.apptdate, t.appttime, t.eid, t.pid, t.original_user, t.encounter, t.lastseq, t.random_drug_test, t.drug_screen_completed, " .
154 "q.pt_tracker_id, q.start_datetime, q.room, q.status, q.seq, q.user, " .
155 "s.toggle_setting_1, s.toggle_setting_2, s.option_id, ";
156 $tracker_joins = "LEFT OUTER JOIN patient_tracker AS t ON t.pid = e.pc_pid AND t.apptdate = e.pc_eventDate AND t.appttime = e.pc_starttime AND t.eid = e.pc_eid " .
157 "LEFT OUTER JOIN patient_tracker_element AS q ON q.pt_tracker_id = t.id AND q.seq = t.lastseq " .
158 "LEFT OUTER JOIN list_options AS s ON s.list_id = 'apptstat' AND s.option_id = q.status AND s.activity = 1 " ;
161 $query = "SELECT " .
162 "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, e.pc_gid, " .
163 "e.pc_title, e.pc_hometext, e.pc_apptstatus, " .
164 "p.fname, p.mname, p.lname, p.pid, p.pubpid, p.phone_home, p.phone_cell, " .
165 "p.hipaa_allowsms, p.phone_home, p.phone_cell, p.hipaa_voice, p.hipaa_allowemail, p.email, " .
166 "u.fname AS ufname, u.mname AS umname, u.lname AS ulname, u.id AS uprovider_id, " .
167 "f.name, " .
168 "$tracker_fields" .
169 "c.pc_catname, c.pc_catid, e.pc_facility " .
170 "FROM openemr_postcalendar_events AS e " .
171 "$tracker_joins" .
172 "LEFT OUTER JOIN facility AS f ON e.pc_facility = f.id " .
173 "LEFT OUTER JOIN patient_data AS p ON p.pid = e.pc_pid " .
174 "LEFT OUTER JOIN users AS u ON u.id = e.pc_aid " .
175 "LEFT OUTER JOIN openemr_postcalendar_categories AS c ON c.pc_catid = e.pc_catid " .
176 "WHERE $where " .
177 "ORDER BY $order_by";
179 if ($bind_param) {
180 $sqlBindArray = array_merge($sqlBindArray, $bind_param);
185 ///////////////////////////////////////////////////////////////////////
186 // The following code is from the calculateEvents function in the //
187 // PostCalendar Module modified and inserted here by epsdky //
188 ///////////////////////////////////////////////////////////////////////
190 $events2 = array();
192 $res = sqlStatement($query, $sqlBindArray);
194 ////////
195 if ($nextX) {
196 global $resNotNull;
197 $resNotNull = (isset($res) && $res != null);
200 while ($event = sqlFetchArray($res)) {
201 ///////
202 if ($nextX) {
203 $stopDate = $event['pc_endDate'];
204 } else {
205 $stopDate = ($event['pc_endDate'] <= $to_date) ? $event['pc_endDate'] : $to_date;
208 ///////
209 $incX = 0;
210 switch ($event['pc_recurrtype']) {
211 case '0':
212 $events2[] = $event;
214 break;
215 //////
216 case '1':
217 case '3':
218 $event_recurrspec = @unserialize($event['pc_recurrspec'], ['allowed_classes' => false]);
220 if (checkEvent($event['pc_recurrtype'], $event_recurrspec)) {
221 break; }
223 $rfreq = $event_recurrspec['event_repeat_freq'];
224 $rtype = $event_recurrspec['event_repeat_freq_type'];
225 $exdate = $event_recurrspec['exdate'];
227 list($ny,$nm,$nd) = explode('-', $event['pc_eventDate']);
228 // $occurance = Date_Calc::dateFormat($nd,$nm,$ny,'%Y-%m-%d');
229 $occurance = $event['pc_eventDate'];
231 while ($occurance < $from_date) {
232 $occurance =& __increment($nd, $nm, $ny, $rfreq, $rtype);
233 list($ny,$nm,$nd) = explode('-', $occurance);
236 while ($occurance <= $stopDate) {
237 $excluded = false;
238 if (isset($exdate)) {
239 foreach (explode(",", $exdate) as $exception) {
240 // occurrance format == yyyy-mm-dd
241 // exception format == yyyymmdd
242 if (preg_replace("/-/", "", $occurance) == $exception) {
243 $excluded = true;
248 if ($excluded == false) {
249 $event['pc_eventDate'] = $occurance;
250 $event['pc_endDate'] = '0000-00-00';
251 $events2[] = $event;
252 //////
253 if ($nextX) {
254 ++$incX;
255 if ($incX == $nextX) {
256 break;
260 //////
263 $occurance =& __increment($nd, $nm, $ny, $rfreq, $rtype);
264 list($ny,$nm,$nd) = explode('-', $occurance);
266 break;
268 //////
269 case '2':
270 $event_recurrspec = @unserialize($event['pc_recurrspec'], ['allowed_classes' => false]);
272 if (checkEvent($event['pc_recurrtype'], $event_recurrspec)) {
273 break; }
275 $rfreq = $event_recurrspec['event_repeat_on_freq'];
276 $rnum = $event_recurrspec['event_repeat_on_num'];
277 $rday = $event_recurrspec['event_repeat_on_day'];
278 $exdate = $event_recurrspec['exdate'];
280 list($ny,$nm,$nd) = explode('-', $event['pc_eventDate']);
282 if (isset($event_recurrspec['rt2_pf_flag']) && $event_recurrspec['rt2_pf_flag']) {
283 $nd = 1;
286 $occuranceYm = "$ny-$nm"; // YYYY-mm
287 $from_dateYm = substr($from_date, 0, 7); // YYYY-mm
288 $stopDateYm = substr($stopDate, 0, 7); // YYYY-mm
290 // $nd will sometimes be 29, 30 or 31 and if used in the mktime functions below
291 // a problem with overflow will occur so it is set to 1 to avoid this (for rt2
292 // appointments set prior to fix $nd remains unchanged). This can be done since
293 // $nd has no influence past the mktime functions.
294 while ($occuranceYm < $from_dateYm) {
295 $occuranceYmX = date('Y-m-d', mktime(0, 0, 0, $nm+$rfreq, $nd, $ny));
296 list($ny,$nm,$nd) = explode('-', $occuranceYmX);
297 $occuranceYm = "$ny-$nm";
300 while ($occuranceYm <= $stopDateYm) {
301 // (YYYY-mm)-dd
302 $dnum = $rnum;
303 do {
304 $occurance = Date_Calc::NWeekdayOfMonth($dnum--, $rday, $nm, $ny, $format = "%Y-%m-%d");
305 } while ($occurance === -1);
307 if ($occurance >= $from_date && $occurance <= $stopDate) {
308 $excluded = false;
309 if (isset($exdate)) {
310 foreach (explode(",", $exdate) as $exception) {
311 // occurrance format == yyyy-mm-dd
312 // exception format == yyyymmdd
313 if (preg_replace("/-/", "", $occurance) == $exception) {
314 $excluded = true;
319 if ($excluded == false) {
320 $event['pc_eventDate'] = $occurance;
321 $event['pc_endDate'] = '0000-00-00';
322 $events2[] = $event;
323 //////
324 if ($nextX) {
325 ++$incX;
326 if ($incX == $nextX) {
327 break;
331 //////
335 $occuranceYmX = date('Y-m-d', mktime(0, 0, 0, $nm+$rfreq, $nd, $ny));
336 list($ny,$nm,$nd) = explode('-', $occuranceYmX);
337 $occuranceYm = "$ny-$nm";
339 break;
343 return $events2;
344 ////////////////////// End of code inserted by epsdky
347 function fetchAllEvents($from_date, $to_date, $provider_id = null, $facility_id = null)
349 $sqlBindArray = array();
351 $where = "";
353 if ($provider_id) {
354 $where .= " AND e.pc_aid = ?";
355 array_push($sqlBindArray, $provider_id);
358 if ($facility_id) {
359 $where .= " AND e.pc_facility = ? AND u.facility_id = ?";
360 array_push($sqlBindArray, $facility_id, $facility_id);
363 $appointments = fetchEvents($from_date, $to_date, $where, null, false, 0, $sqlBindArray);
364 return $appointments;
367 //Support for therapy group appointments added by shachar z.
368 function fetchAppointments($from_date, $to_date, $patient_id = null, $provider_id = null, $facility_id = null, $pc_appstatus = null, $with_out_provider = null, $with_out_facility = null, $pc_catid = null, $tracker_board = false, $nextX = 0, $group_id = null, $patient_name = null)
370 $sqlBindArray = array();
372 $where = "";
374 if ($provider_id) {
375 $where .= " AND e.pc_aid = ?";
376 array_push($sqlBindArray, $provider_id);
379 if ($patient_id) {
380 $where .= " AND e.pc_pid = ?";
381 array_push($sqlBindArray, $patient_id);
382 } elseif ($group_id) {
383 //if $group_id this means we want only the group events
384 $where .= " AND e.pc_gid = ? AND e.pc_pid = ''";
385 array_push($sqlBindArray, $group_id);
386 } else {
387 $where .= " AND e.pc_pid != ''";
390 if ($facility_id) {
391 $where .= " AND e.pc_facility = ?";
392 array_push($sqlBindArray, $facility_id);
395 //Appointment Status Checking
396 if ($pc_appstatus != '') {
397 $where .= " AND e.pc_apptstatus = ?";
398 array_push($sqlBindArray, $pc_appstatus);
401 if ($pc_catid != null) {
402 $where .= " AND e.pc_catid = ?";
403 array_push($sqlBindArray, $pc_catid);
406 if ($patient_name != null) {
407 $where .= " AND (p.fname LIKE CONCAT('%',?,'%') OR p.lname LIKE CONCAT('%',?,'%'))";
408 array_push($sqlBindArray, $patient_name, $patient_name);
411 //Without Provider checking
412 if ($with_out_provider != '') {
413 $where .= " AND e.pc_aid = ''";
416 //Without Facility checking
417 if ($with_out_facility != '') {
418 $where .= " AND e.pc_facility = 0";
421 $appointments = fetchEvents($from_date, $to_date, $where, '', $tracker_board, $nextX, $sqlBindArray);
422 return $appointments;
425 //Support for therapy group appointments added by shachar z.
426 function fetchNextXAppts($from_date, $patient_id, $nextX = 1, $group_id = null)
429 $appts = array();
430 $nextXAppts = array();
431 $appts = fetchAppointments($from_date, null, $patient_id, null, null, null, null, null, null, false, $nextX, $group_id);
432 if ($appts) {
433 $appts = sortAppointments($appts);
434 $nextXAppts = array_slice($appts, 0, $nextX);
437 return $nextXAppts;
440 // get the event slot size in seconds
441 function getSlotSize()
443 if (isset($GLOBALS['calendar_interval'])) {
444 return $GLOBALS['calendar_interval'] * 60;
447 return 15 * 60;
450 function getAvailableSlots($from_date, $to_date, $provider_id = null, $facility_id = null)
452 $appointments = fetchAllEvents($from_date, $to_date, $provider_id, $facility_id);
453 $appointments = sortAppointments($appointments, "date");
454 $from_datetime = strtotime($from_date." 00:00:00");
455 $to_datetime = strtotime($to_date." 23:59:59");
456 $availableSlots = array();
457 $start_time = 0;
458 $date = 0;
459 for ($i = 0; $i < count($appointments); ++$i) {
460 if ($appointments[$i]['pc_catid'] == 2) { // 2 == In Office
461 $start_time = $appointments[$i]['pc_startTime'];
462 $date = $appointments[$i]['pc_eventDate'];
463 $provider_id = $appointments[$i]['uprovider_id'];
464 } else if ($appointments[$i]['pc_catid'] == 3) { // 3 == Out Of Office
465 continue;
466 } else {
467 $start_time = $appointments[$i]['pc_endTime'];
468 $date = $appointments[$i]['pc_eventDate'];
469 $provider_id = $appointments[$i]['uprovider_id'];
472 // find next appointment with the same provider
473 $next_appointment_date = 0;
474 $next_appointment_time = 0;
475 for ($j = $i+1; $j < count($appointments); ++$j) {
476 if ($appointments[$j]['uprovider_id'] == $provider_id) {
477 $next_appointment_date = $appointments[$j]['pc_eventDate'];
478 $next_appointment_time = $appointments[$j]['pc_startTime'];
479 break;
483 $same_day = ( strtotime($next_appointment_date) == strtotime($date) ) ? true : false;
485 if ($next_appointment_time && $same_day) {
486 // check the start time of the next appointment
488 $start_datetime = strtotime($date." ".$start_time);
489 $next_appointment_datetime = strtotime($next_appointment_date." ".$next_appointment_time);
490 $curr_time = $start_datetime;
491 while ($curr_time < $next_appointment_datetime - (getSlotSize() / 2)) {
492 //create a new appointment ever 15 minutes
493 $time = date("H:i:s", $curr_time);
494 $available_slot = createAvailableSlot(
495 $appointments[$i]['pc_eventDate'],
496 $time,
497 $appointments[$i]['ufname'],
498 $appointments[$i]['ulname'],
499 $appointments[$i]['umname']
501 $availableSlots []= $available_slot;
502 $curr_time += getSlotSize(); // add a 15-minute slot
507 return $availableSlots;
510 function createAvailableSlot($event_date, $start_time, $provider_fname, $provider_lname, $provider_mname = "", $cat_name = "Available")
512 $newSlot = array();
513 $newSlot['ulname'] = $provider_lname;
514 $newSlot['ufname'] = $provider_fname;
515 $newSlot['umname'] = $provider_mname;
516 $newSlot['pc_eventDate'] = $event_date;
517 $newSlot['pc_startTime'] = $start_time;
518 $newSlot['pc_endTime'] = $start_time;
519 $newSlot['pc_catname'] = $cat_name;
520 return $newSlot;
523 function getCompareFunction($code)
525 global $COMPARE_FUNCTION_HASH;
526 return $COMPARE_FUNCTION_HASH[$code];
529 function getComparisonOrder($code)
531 global $ORDERHASH;
532 return $ORDERHASH[$code];
536 function sortAppointments(array $appointments, $orderBy = 'date')
538 global $appointment_sort_order;
539 $appointment_sort_order = $orderBy;
540 usort($appointments, "compareAppointments");
541 return $appointments;
544 // cmp_function for usort
545 // The comparison function must return an integer less than, equal to,
546 // or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
547 function compareAppointments($appointment1, $appointment2)
549 global $appointment_sort_order;
550 $comparisonOrder = getComparisonOrder($appointment_sort_order);
551 foreach ($comparisonOrder as $comparison) {
552 $cmp_function = getCompareFunction($comparison);
553 $result = $cmp_function($appointment1, $appointment2);
554 if (0 != $result) {
555 return $result;
559 return 0;
562 function compareBasic($e1, $e2)
564 if ($e1 < $e2) {
565 return -1;
566 } else if ($e1 > $e2) {
567 return 1;
570 return 0;
573 function compareAppointmentsByDate($appointment1, $appointment2)
575 $date1 = strtotime($appointment1['pc_eventDate']);
576 $date2 = strtotime($appointment2['pc_eventDate']);
578 return compareBasic($date1, $date2);
581 function compareAppointmentsByTime($appointment1, $appointment2)
583 $time1 = strtotime($appointment1['pc_startTime']);
584 $time2 = strtotime($appointment2['pc_startTime']);
586 return compareBasic($time1, $time2);
589 function compareAppointmentsByDoctorName($appointment1, $appointment2)
591 $name1 = $appointment1['ulname'];
592 $name2 = $appointment2['ulname'];
593 $cmp = compareBasic($name1, $name2);
594 if ($cmp == 0) {
595 $name1 = $appointment1['ufname'];
596 $name2 = $appointment2['ufname'];
597 return compareBasic($name1, $name2);
600 return $cmp;
603 function compareAppointmentsByPatientName($appointment1, $appointment2)
605 $name1 = $appointment1['lname'];
606 $name2 = $appointment2['lname'];
607 $cmp = compareBasic($name1, $name2);
608 if ($cmp == 0) {
609 $name1 = $appointment1['fname'];
610 $name2 = $appointment2['fname'];
611 return compareBasic($name1, $name2);
614 return $cmp;
617 function compareAppointmentsByType($appointment1, $appointment2)
619 $type1 = $appointment1['pc_catid'];
620 $type2 = $appointment2['pc_catid'];
621 return compareBasic($type1, $type2);
624 function compareAppointmentsByPatientId($appointment1, $appointment2)
626 $id1 = $appointment1['pubpid'];
627 $id2 = $appointment2['pubpid'];
628 return compareBasic($id1, $id2);
631 function compareAppointmentsByComment($appointment1, $appointment2)
633 $comment1 = $appointment1['pc_hometext'];
634 $comment2 = $appointment2['pc_hometext'];
635 return compareBasic($comment1, $comment2);
638 function compareAppointmentsByStatus($appointment1, $appointment2)
640 $status1 = $appointment1['pc_apptstatus'];
641 $status2 = $appointment2['pc_apptstatus'];
642 return compareBasic($status1, $status2);
645 function compareAppointmentsByTrackerStatus($appointment1, $appointment2)
647 $trackerstatus1 = $appointment1['status'];
648 $trackerstatus2 = $appointment2['status'];
649 return compareBasic($trackerstatus1, $trackerstatus2);
652 function compareAppointmentsByCompletedDrugScreen($appointment1, $appointment2)
654 $completed1 = $appointment1['drug_screen_completed'];
655 $completed2 = $appointment2['drug_screen_completed'];
656 return compareBasic($completed1, $completed2);
659 function fetchAppointmentCategories()
661 $catSQL= " SELECT pc_catid as id, pc_catname as category "
662 . " FROM openemr_postcalendar_categories WHERE pc_active=1 and pc_recurrtype=0 and pc_cattype=0";
663 if ($GLOBALS['enable_group_therapy']) {
664 $catSQL .= " OR pc_cattype=3";
667 $catSQL .= " ORDER BY category";
668 return sqlStatement($catSQL);
671 function interpretRecurrence($recurr_freq, $recurr_type)
673 global $REPEAT_FREQ, $REPEAT_FREQ_TYPE, $REPEAT_ON_NUM, $REPEAT_ON_DAY;
674 $interpreted = "";
675 $recurr_freq = unserialize($recurr_freq, ['allowed_classes' => false]);
676 if ($recurr_type == 1) {
677 $interpreted = $REPEAT_FREQ[$recurr_freq['event_repeat_freq']];
678 $interpreted .= " " . $REPEAT_FREQ_TYPE[$recurr_freq['event_repeat_freq_type']];
679 } elseif ($recurr_type == 2) {
680 $interpreted = $REPEAT_FREQ[$recurr_freq['event_repeat_on_freq']];
681 $interpreted .= " " . $REPEAT_ON_NUM[$recurr_freq['event_repeat_on_num']];
682 $interpreted .= " " . $REPEAT_ON_DAY[$recurr_freq['event_repeat_on_day']];
683 } elseif ($recurr_type == 3) {
684 $interpreted = $REPEAT_FREQ[1];
685 $comma = "";
686 $day_arr = explode(",", $recurr_freq['event_repeat_freq']);
687 foreach ($day_arr as $day) {
688 $interpreted .= $comma . " " . $REPEAT_ON_DAY[$day - 1];
689 $comma = ",";
693 return $interpreted;
696 function fetchRecurrences($pid)
698 $query = "SELECT pe.pc_title, pe.pc_endDate, pe.pc_recurrtype, pe.pc_recurrspec, pc.pc_catname FROM openemr_postcalendar_events AS pe "
699 . "JOIN openemr_postcalendar_categories AS pc ON pe.pc_catid=pc.pc_catid "
700 . "WHERE pe.pc_pid = ? AND pe.pc_recurrtype > 0;";
701 $sqlBindArray = array();
702 array_push($sqlBindArray, $pid);
703 $res = sqlStatement($query, $sqlBindArray);
704 $result_data = array();
705 while ($row = sqlFetchArray($res)) {
706 $u_recurrspec = unserialize($row['pc_recurrspec'], ['allowed_classes' => false]);
707 if (checkEvent($row['pc_recurrtype'], $u_recurrspec)) {
708 continue; }
709 $row['pc_recurrspec'] = interpretRecurrence($row['pc_recurrspec'], $row['pc_recurrtype']);
710 $result_data[] = $row;
712 return $result_data;
715 function ends_in_a_week($end_date)
717 $timestamp_in_a_week = strtotime('+7 day');
718 $timestamp_end_date = strtotime($end_date);
719 if ($timestamp_in_a_week > $timestamp_end_date) {
720 return true; //ends in a week
723 return false; // ends in more than a week
726 //Checks if recurrence is current (didn't end yet).
727 function recurrence_is_current($end_date)
729 $end_date_timestamp = strtotime($end_date);
730 $current_timestamp = time();
731 if ($current_timestamp <= $end_date_timestamp) {
732 return true; //recurrence is current
735 return false;