2 // Copyright (C) 2010 Maviq <info@maviq.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 ////////////////////////////////////////////////////////////////////
10 // Package: cron_phone_notification
11 // Purpose: to be run by cron every hour, look for appointments
12 // in the pre-notification period and send an phone reminder
13 // Based on cron_email_notification by Larry Lart
15 // Updated by: Maviq on 01/12/2010
16 ////////////////////////////////////////////////////////////////////
22 //Set the working directory to the path of the file
23 $current_dir = dirname($_SERVER['SCRIPT_FILENAME']);
26 //SANITIZE ALL ESCAPES
27 $sanitize_all_escapes=true;
29 //STOP FAKE REGISTER GLOBALS
30 $fake_register_globals=false;
32 require_once("../../interface/globals.php");
33 require_once("$srcdir/maviq_phone_api.php");
36 $before_trigger_hours = 72; // 3 days is default
37 //Get the values from Global
38 $before_trigger_hours = $GLOBALS['phone_notification_hour'];
39 //set up the phone notification settings for external phone service
40 $phone_url = $GLOBALS['phone_gateway_url'] ;
41 $phone_id = $GLOBALS['phone_gateway_username'];
42 $phone_token = $GLOBALS['phone_gateway_password'];
43 $phone_time_range = $GLOBALS['phone_time_range'];
45 //get the facility_id-message map
46 $facilities = cron_getFacilitiesMap();
47 //print_r($facilities);
48 $fac_phone_map = $facilities['phone_map'];
49 $fac_msg_map = $facilities['msg_map'];
51 // get patient data for send alert
52 $db_patient = cron_getPhoneAlertpatientData($type, $before_trigger_hours);
53 echo "<br>" . htmlspecialchars( xl("Total Records Found") . ": " . count($db_patient), ENT_QUOTES
);
55 //Create a new instance of the phone service client
56 $client = new MaviqClient($phone_id, $phone_token, $phone_url);
58 for($p=0;$p<count($db_patient);$p++
)
60 $prow =$db_patient[$p];
62 //Get the apptDate and apptTime
63 $p_date = $prow['pc_eventDate'];
64 //Need to format date to m/d/Y for Maviq API
65 $pieces = explode("-",$p_date);
66 $appt_date = date("m/d/Y", mktime( 0,0,0,$pieces[1],$pieces[2],$pieces[0]));
67 $appt_time = $prow['pc_startTime'];
69 $greeting = $fac_msg_map[$prow['pc_facility']];
70 if ($greeting == null) {
71 //Use the default when the message is not found
72 $greeting = $GLOBALS['phone_appt_message']['Default'];
74 //Set up the parameters for the call
76 "firstName" => $prow['fname'],
77 "lastName" => $prow['lname'],
78 "phone" => $prow['phone_home'],
79 "apptDate" => $appt_date,
80 "apptTime" => $appt_time,
81 "doctor" => $prow['pc_aid'],
82 "greeting" => $greeting,
83 "timeRange" => $phone_time_range,
84 "type" => "appointment",
85 "timeZone" => date('P'),
86 "callerId" => $fac_phone_map[$prow['pc_facility']]
90 $response = $client->sendRequest("appointment", "POST", $data);
92 // check response for success or error
93 if($response->IsError
) {
94 $strMsg = "Error starting phone call for {$prow['fname']} | {$prow['lname']} | {$prow['phone_home']} | {$appt_date} | {$appt_time} | {$response->ErrorMessage}\n";
97 $strMsg = "\n========================".$type." || ".date("Y-m-d H:i:s")."=========================";
98 $strMsg .= "\nPhone reminder sent successfully: {$prow['fname']} | {$prow['lname']} | | {$prow['phone_home']} | {$appt_date} | {$appt_time} ";
99 // insert entry in notification_log table
100 cron_InsertNotificationLogEntry($prow,$greeting,$phone_url);
102 //update entry >> pc_sendalertsms='Yes'
103 cron_updateentry($type,$prow['pid'],$prow['pc_eid']);
114 ////////////////////////////////////////////////////////////////////
115 // Function: cron_updateentry
116 // Purpose: update status yes if alert send to patient
117 ////////////////////////////////////////////////////////////////////
118 function cron_updateentry($type,$pid,$pc_eid)
121 $query = "update openemr_postcalendar_events set ";
123 // larry :: and here again same story - this time for sms pc_sendalertsms - no such field in the table
125 $query.=" pc_sendalertsms='YES' ";
126 elseif ($type=='Email')
127 $query.=" pc_sendalertemail='YES' ";
128 //Added by Yijin for phone reminder.. Uses the same field as SMS.
129 elseif($type=='Phone')
130 $query.=" pc_sendalertsms='YES' ";
132 $query .=" where pc_pid=? and pc_eid=? ";
133 //echo "<br>".$query;
134 $db_sql = (sqlStatement($query, array($pid, $pc_eid)));
137 ////////////////////////////////////////////////////////////////////
138 // Function: cron_getPhoneAlertpatientData
139 // Purpose: get patient data for send to alert
140 ////////////////////////////////////////////////////////////////////
141 function cron_getPhoneAlertpatientData( $type, $trigger_hours )
144 //Added by Yijin 1/12/10 to handle phone reminders. Patient needs to have hipaa Voice flag set to yes and a home phone
146 $ssql = " and pd.hipaa_voice='YES' and pd.phone_home<>'' and ope.pc_sendalertsms='NO' and ope.pc_apptstatus != '*' ";
148 $check_date = date("Y-m-d", mktime(date("H")+
$trigger_hours, 0, 0, date("m"), date("d"), date("Y")));
152 $patient_field = "pd.pid,pd.title,pd.fname,pd.lname,pd.mname,pd.phone_cell,pd.email,pd.hipaa_allowsms,pd.hipaa_allowemail,pd.phone_home,pd.hipaa_voice,";
153 $ssql .= " and (ope.pc_eventDate=?)";
155 $query = "select $patient_field pd.pid,ope.pc_eid,ope.pc_pid,ope.pc_title,
156 ope.pc_hometext,ope.pc_eventDate,ope.pc_endDate,
157 ope.pc_duration,ope.pc_alldayevent,ope.pc_startTime,ope.pc_endTime,ope.pc_facility
159 openemr_postcalendar_events as ope ,patient_data as pd
161 ope.pc_pid=pd.pid $ssql
163 ope.pc_eventDate,ope.pc_endDate,pd.pid";
165 $db_patient = (sqlStatement($query, array($check_date)));
166 $patient_array = array();
168 while ($prow = sqlFetchArray($db_patient))
170 $patient_array[$cnt] = $prow;
173 return $patient_array;
176 ////////////////////////////////////////////////////////////////////
177 // Function: cron_InsertNotificationLogEntry
178 // Purpose: insert log entry in table
179 ////////////////////////////////////////////////////////////////////
180 function cron_InsertNotificationLogEntry($prow,$phone_msg,$phone_gateway)
182 $patient_info = $prow['title']." ".$prow['fname']." ".$prow['mname']." ".$prow['lname']."|||".$prow['phone_home'];
184 $message = $phone_msg;
186 $sql_loginsert = "INSERT INTO `notification_log` ( `iLogId` , `pid` , `pc_eid` , `message`, `type` , `patient_info` , `smsgateway_info` , `pc_eventDate` , `pc_endDate` , `pc_startTime` , `pc_endTime` , `dSentDateTime` ) VALUES ";
187 $sql_loginsert .= "(NULL , ?, ?, ?, 'Phone', ?, ?, ?, ?, ?, ?, ?)";
188 $db_loginsert = ( sqlStatement( $sql_loginsert, array($prow[pid
], $prow[pc_eid
], $message, $patient_info, $phone_gateway, $prow[pc_eventDate
], $prow[pc_endDate
], $prow[pc_startTime
], $prow[pc_endTime
], date("Y-m-d H:i:s"))));
191 ////////////////////////////////////////////////////////////////////
192 // Function: WriteLog
193 // Purpose: written log into file
194 ////////////////////////////////////////////////////////////////////
195 function WriteLog( $data )
197 $log_file = $GLOBALS['phone_reminder_log_dir'];
199 if ($log_file != null) {
201 $filename = $log_file . "/"."phone_reminder_cronlog_".date("Ymd").".html";
203 if (!$fp = fopen($filename, 'a'))
205 print "Cannot open file ($filename)";
209 $sdata = "\n====================================================================\n";
211 if (!fwrite($fp, $data.$sdata))
213 print "Cannot write to file ($filename)";
220 ////////////////////////////////////////////////////////////////////
221 // Function: cron_getFacilities
222 // Purpose: get facilities data once and store in map
223 ////////////////////////////////////////////////////////////////////
224 function cron_getFacilitiesMap()
226 //get the facility_name-message map from Globals
227 $message_map = $GLOBALS['phone_appt_message'];
228 //create a new array to store facility_id to message map
229 $facility_msg_map = array();
230 $facility_phone_map = array();
231 //get facilities from the database
232 $query = "select fac.id, fac.name, fac.phone from facility as fac";
233 $db_res = (sqlStatement($query));
234 while ($prow = sqlFetchArray($db_res))
236 $facility_msg_map[$prow['id']] = $message_map[$prow['name']];
237 $facility_phone_map[$prow['id']] = $prow['phone'];
240 $facility_map = array(
241 'msg_map' => $facility_msg_map,
242 'phone_map' => $facility_phone_map
245 return $facility_map;