Exemplo n.º 1
0
 function check_sms_alert($date_passed)
 {
     $alert = new alert();
     $arr_alert = array();
     $include_wkend = 0;
     if (isset($date_passed)) {
         $today = $date_passed;
     } else {
         $today = date('Y-m-d');
         if (date('l', strtotime($today)) == 'Friday') {
             $include_wkend = 1;
         }
     }
     //echo $today;
     //echo date('l',strtotime($today));
     for ($i = 1; $i <= 2; $i++) {
         $date = strtotime("+{$i} day", strtotime($today));
         //echo date("Y-m-d", $date)."<br>";
     }
     $q_sms_alert = mysql_query("SELECT sms_id FROM m_lib_sms_alert WHERE alert_date='{$today}'") or die("Cannot query 732: " . mysql_error());
     $q_fam_id = mysql_query("SELECT DISTINCT a.family_id FROM m_family_address a, m_family_members b, m_lib_barangay c WHERE a.family_id=b.family_id AND a.barangay_id=c.barangay_id ORDER by c.barangay_name ASC") or die("Cannot query 1576: " . mysql_error());
     while ($r_fam = mysql_fetch_array($q_fam_id)) {
         if ($alert->check_px_enrolled_sms($r_fam['family_id'])) {
             array_push($arr_alert, $alert->determine_alert_hh($r_fam['family_id']));
         }
         //echo $r_fam[family_id].' ';
         //print_r($arr_alert);
         //echo "<br><br><br>";
     }
     if (mysql_num_rows($q_sms_alert) == 0) {
         foreach ($arr_alert as $key1 => $value1) {
             foreach ($value1 as $key2 => $value2) {
                 foreach ($value2 as $key3 => $value3) {
                     foreach ($value3 as $key4 => $value4) {
                         foreach ($value4 as $key5 => $value5) {
                             foreach ($value5 as $key6 => $value6) {
                                 //key6 is the patient_id
                                 $q_name = mysql_query("SELECT patient_lastname,patient_firstname FROM m_patient WHERE patient_id='{$key6}'") or die("Cannot query 1545: " . mysql_error());
                                 list($lname, $fname) = mysql_fetch_array($q_name);
                                 $ngalan = $lname . ', ' . $fname;
                                 foreach ($value6 as $key7 => $alert_details) {
                                     //echo $key7.'<br>';
                                     //print_r($value6).'<br><br>';
                                     foreach ($alert_details as $alert_id => $arr_alert) {
                                         //arr_alert[0] is the program_id, arr_alert[1] is the base date
                                         if ($alert->determine_px_enrollment($key6, $alert_id)) {
                                             $arr_alert_msg = $alert->check_alert_msg($alert_id, $arr_alert[1], $key6);
                                             if (count($arr_alert_msg) != 0) {
                                                 //print_r($arr_alert_msg);
                                                 //$day_diff = $alert->get_date_diff_days(date('Y-m-d'),$arr_alert[1]);
                                                 $day_diff = $alert->get_date_diff_days($today, $arr_alert[1]);
                                                 //echo $arr_alert[1];
                                                 //echo $key6.' '.$arr_alert[1].'  '.$day_diff.'<br>';
                                                 $mensahe = $alert->get_message($day_diff, $arr_alert_msg, $ngalan, $arr_alert[1]);
                                                 //echo $day_diff.'/'.$ngalan.'/'.$mensahe.'/'.$alert_id.'/'.$arr_alert[1].'<br>xxx';
                                                 if ($mensahe != '') {
                                                     /* $key: patient_id, $arr_alert[0]: program_id, $arr_alert[1]: base date, $alert_id: id for alert, 'queue': sms_status, $mensahe: alert message
                                                      */
                                                     $alert->queue_sms($key6, $arr_alert[0], $arr_alert[1], $alert_id, 'queue', $mensahe);
                                                 }
                                             }
                                         }
                                     }
                                     //$insert_sms_alert = mysql_query("INSERT INTO m_lib_sms_alert SET alert_date=NOW()") or die("Cannot query 735: ".mysql_error());
                                 }
                             }
                         }
                     }
                 }
             }
         }
     } else {
         $arr_config = $alert->get_sms_config();
         if (count($arr_config) != 0) {
             if ($arr_config['sms_time_sched'] <= date('H:i')) {
                 $date_today = date('Y-m-d');
                 $q_alert = mysql_query("SELECT sms_id,alert_date,sms_message,sms_number FROM m_lib_sms_alert WHERE alert_date='{$date_today}' AND sms_status!='sent'") or die("Cannot query 1644: " . mysql_error());
                 if (mysql_num_rows($q_alert) != 0) {
                     while ($r_alert = mysql_fetch_array($q_alert)) {
                         if ($alert->send_sms($arr_config['sms_url'], $arr_config['sms_port'], $r_alert['sms_number'], $r_alert['sms_message'])) {
                             $alert->update_sms_status($r_alert['sms_id'], 'sent');
                         } else {
                             $alert->update_sms_status($r_alert['sms_id'], 'not_sent');
                         }
                     }
                 }
             }
         } else {
             echo "<font>There is no SMS configuration!</font>";
         }
     }
 }