_ank_id = $ank_id; //get survey title and link if($ank_id > 0){ SurveyInfo::getInstance()->SurveyInit($ank_id); $this->_ank_title = SurveyInfo::getSurveyColumn('naslov'); $this->_ank_link = SurveyInfo::getSurveyLink(); } } public function display() { global $admin_type, $lang; // Izpis vseh poslanih sporocil if ($admin_type == 0) { //navigacija $this->displayNavigation(); //is survey activated $act = sisplet_query("SELECT active FROM srv_anketa WHERE id='" . $this->_ank_id . "'", 'obj'); if($act->active != 1) echo '

'.$lang['srv_anketa_noactive2'].'

'; $_sub_action = isset($_GET['m']) ? $_GET['m'] : 'maza_dashboard'; $active_step[] = array(1 => '', 2 => '', 3 => '', 4 => '', 5 => '', 6 => '', 7 => ''); switch ($_sub_action) { case 'maza_dashboard': $this -> surveyDescription(); echo '
'; $this -> getUsersStatistics(); echo '
'; $this -> generateNewUsersForm(); echo '
'; $this -> exportIdentifiersForm(); break; case 'maza_send_notification': // obrazec za posiljanje sporocil $this->sendMessageForm(); break; case 'maza_set_alarm': // obrazec za posiljanje alarmov $this->setRepeaterForm(); echo '
'; $this->setAlarmForm(); echo ''; break; case 'maza_set_geofencing': //obrazec za posiljanje geofences $sql_r = sisplet_query("SELECT COUNT(geofence_on) AS cnt FROM maza_srv_geofences WHERE ank_id='" . $this->_ank_id . "' AND geofence_on=1", 'obj'); $this->setGeofencesForm($sql_r->cnt); echo ''; break; case 'maza_set_activity': //obrazec za posiljanje aktivnosti $this->setActivityForm(); break; case 'maza_set_tracking': //obrazec za posiljanje sledenja $this->setTrackingForm(); //echo ''; break; case 'maza_set_entry': //obrazec za posiljanje sledenja $this->setEntryForm(); //echo ''; break; default: $active_step['1'] = ' active'; break; } if(isset($_GET['FCM_response'])) echo '

'.$_GET['FCM_response']; } } function displayNavigation() { global $lang; $_sub_action = isset($_GET['m']) ? $_GET['m'] : 'maza_dashboard'; $active_step[] = array(1 => '', 2 => '', 3 => '', 4 => '', 5 => '', 6 => '', 7 => ''); switch ($_sub_action) { case 'maza_dashboard': $active_step['1'] = ' active'; break; case 'maza_send_notification': $active_step['2'] = ' active'; break; case 'maza_set_alarm': $active_step['3'] = ' active'; break; case 'maza_set_geofencing': $active_step['4'] = ' active'; break; case 'maza_set_activity': $active_step['5'] = ' active'; break; case 'maza_set_tracking': $active_step['6'] = ' active'; break; case 'maza_set_entry': $active_step['7'] = ' active'; break; default: $active_step['1'] = ' active'; break; } echo '

'; echo ''; echo '
'; echo '
'; echo '
'; } // Obrazec za posiljanje notificationa private function sendMessageForm() { global $lang; //FORM FOR NOTIFICATION echo '
'; echo '' . $lang['srv_maza_send_notification'] . ''; echo '
'; /* echo ''.$lang['srv_notifications_send_reciever'].': '; // Checkboxa za posiljenje vsem uporabnikoom (slo in ang) echo '
'; echo '

'; */ echo ''; echo ''; echo '' . $lang['srv_notifications_send_title'] . ':

'; echo '' . $lang['srv_notifications_send_text'] . ':

'; echo '

'; //echo '

'; echo ''; echo '

' . $_POST['maza_result'] . '

'; echo '
'; echo '


'; //FORM FOR WEB PUSH NOTIFICATIONs /*echo '
'; echo 'PWA notification (test and fixed)'; echo '
'; /* echo ''.$lang['srv_notifications_send_reciever'].': '; // Checkboxa za posiljenje vsem uporabnikoom (slo in ang) echo '
'; echo '

'; */ /*echo ''; echo ''; echo '' . $lang['srv_notifications_send_title'] . ':

'; echo '' . $lang['srv_notifications_send_text'] . ':

'; //echo '

'; //echo '

'; echo ''; echo '

' . $_POST['maza_result'] . '

'; echo '
'; echo '
';*/ } // Obrazec za posiljanje alarmov private function setAlarmForm() { global $lang; //FORM FOR ALARM $sql_r = sisplet_query("SELECT * FROM maza_srv_alarms WHERE ank_id='" . $this->_ank_id . "'", 'obj'); echo '
'; echo '' . $lang['srv_maza_send_alarm'] . ''; $disabled = ''; if ($this->isRepeaterRunning && $sql_r->alarm_on == '1') { echo '
'; echo ''; //$disabled = ' disabled="disabled"'; echo '' . $lang['srv_maza_alarm_on'] . '
'; ///////////////////////////////////AKCIJA echo '

'; echo '
'; } //form to set or update alarm echo '
'; /* echo ''.$lang['srv_notifications_send_reciever'].': '; // Checkboxa za posiljenje vsem uporabnikoom (slo in ang) echo '
'; echo '

'; */ if ((sizeof($sql_r) == 0 || $sql_r->alarm_on == '0') && $this->isRepeaterRunning) echo '' . $lang['srv_maza_alarm_off'] . '
'; else echo '
'; echo ''; echo ''; echo '
'; echo '
'; $sql_r->alarm_notif_title = ($sql_r->alarm_notif_title) ? $sql_r->alarm_notif_title : $lang['srv_maza_alarm_default_title']; $sql_r->alarm_notif_message = ($sql_r->alarm_notif_message) ? $sql_r->alarm_notif_message : $lang['srv_maza_alarm_default_message'].$this->_ank_title; echo '' . $lang['srv_notifications_send_title'] . ':

'; echo '' . $lang['srv_notifications_send_text'] . ':

'; /*echo '

';*/ //echo '

'; echo '
'; echo '
'; echo '' . $lang['srv_maza_alarm_when_to_show'] . ''; echo '

'; echo '
repeat_by == 'daily') ? '' : 'style="display: none;"') .'>'; echo '' . $lang['srv_maza_alarm_daily_every'] . ': ' . ' ' . $lang['srv_maza_alarm_days'] . '
'; $weekly_array = (isset($sql_r->day_in_week) && $sql_r->day_in_week != 'null') ? json_decode($sql_r->day_in_week) : array(); echo '
repeat_by == 'weekly') ? '' : 'style="display: none;"') .'>'; echo '' . $lang['srv_maza_alarm_day_week'] . ':'; echo ''; //echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
At time in day:

'; $time_array = (isset($sql_r->time_in_day) && $sql_r->time_in_day != 'null') ? json_decode($sql_r->time_in_day) : array(); echo '' . $lang['srv_maza_alarm_time_day'] . ':'; echo ''; //echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
At time in day:
'; echo '
'; echo '
'; if ((sizeof($sql_r) == 0 || $sql_r->alarm_on == '0') && $this->isRepeaterRunning){ echo ''; } else echo '
'; echo '

' . $_POST['maza_result'] . '

'; echo '
'; echo '
'; echo '
'; echo '
'; echo '' . $lang['srv_maza_data_export'] . ''; echo '' . '' . $lang['srv_maza_alarm_export_respondents'] . ''; echo '
'; } // Obrazec za posiljanje alarmov private function setRepeaterForm() { global $lang; //FORM FOR ALARM $sql_r = sisplet_query("SELECT * FROM maza_srv_repeaters WHERE ank_id='" . $this->_ank_id . "'", 'obj'); $dateToday = DateTime::createFromFormat('Y-m-d', date("Y-m-d")); //if start date of repeater exists if($sql_r->datetime_start){ $dateTimeStart = new DateTime($sql_r->datetime_start); $dateStart = DateTime::createFromFormat('Y-m-d', $dateTimeStart->format('Y-m-d')); //is repeater start date <= of today? if yes, repeater has started $hasRepeaterStarted = ($dateStart->getTimestamp() <= $dateToday->getTimestamp()); $this->isRepeaterRunning = $hasRepeaterStarted && $sql_r->repeater_on == '1'; $dateTimeStart = $dateTimeStart->format('d.m.Y'); } else $dateTimeStart = ""; //if end date of repeater exists if($sql_r->datetime_end){ $dateTimeEnd = new DateTime($sql_r->datetime_end); $dateEnd = DateTime::createFromFormat('Y-m-d', $dateTimeEnd->format('Y-m-d')); //is repeater end date < of today? if yes, repeater has ended $hasRepeaterEnd = ($dateEnd->getTimestamp() < $dateToday->getTimestamp()); $this->isRepeaterRunning = $hasRepeaterStarted && $sql_r->repeater_on == '1' && !$hasRepeaterEnd; $dateTimeEnd = $dateTimeEnd->format('d.m.Y'); } else $dateTimeEnd = ""; echo '
'; echo '' . $lang['srv_maza_repeater_title'] . ''; $disabled = ''; if ($this->isRepeaterRunning) { echo '
'; echo ''; //$disabled = ' disabled="disabled"'; echo '' . $lang['srv_maza_repeater_on'] . '
'; ///////////////////////////////////AKCIJA echo '

'; echo '
'; } //form to set or update alarm echo '
'; /* echo ''.$lang['srv_notifications_send_reciever'].': '; // Checkboxa za posiljenje vsem uporabnikoom (slo in ang) echo '
'; echo '

'; */ if ($sql_r->repeater_on > 0 && $hasRepeaterStarted) echo '
'; //echo ''; echo ''; //echo '
'; echo '
' . $lang['srv_maza_repeater_when_to_start'] . ''; echo ''; if (!$hasRepeaterStarted && $dateTimeStart) echo ' '.$lang['srv_maza_repeater_edit_warning'].''; echo '

'; echo '
' . $lang['srv_maza_repeater_when_to_end'] . ''; echo ''; echo '


'; echo '
' . $lang['srv_maza_repeater_when_to_repeat'] . ''; echo '


'; echo '
repeat_by == 'daily') ? '' : 'style="display: none;"') .'>'; echo '' . $lang['srv_maza_alarm_daily_every'] . ': ' . ' ' . $lang['srv_maza_alarm_days'] . '
'; $weekly_array = (isset($sql_r->day_in_week) && $sql_r->day_in_week != 'null') ? json_decode($sql_r->day_in_week) : array(); echo '
repeat_by == 'weekly') ? '' : 'style="display: none;"') .'>'; echo '' . $lang['srv_maza_alarm_day_week'] . ':'; echo ''; //echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
At time in day:

'; $time_array = (isset($sql_r->time_in_day) && $sql_r->time_in_day != 'null') ? json_decode($sql_r->time_in_day) : array(); echo '' . $lang['srv_maza_alarm_time_day'] . ':'; echo ''; //echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
At time in day:
'; //echo '
'; echo '
'; //repeater not in motion yet if ((sizeof($sql_r) == 0 || $sql_r->repeater_on < 2) && !$hasRepeaterStarted){ echo ''; } //repeater ended else echo '
'; echo '

' . $_POST['maza_result'] . '

'; echo '
'; echo '
'; } /** * Obrazec za posiljanje geofencev * @global type $admin_type * @global type $global_user_id * @global type $lang * @param type $geocnt - count of all geofences that are on */ private function setGeofencesForm($geocnt) { global $lang; echo '
'; echo '' . $lang['srv_maza_geofencing'] . ''; //FORM FOR GEOFENCES if ($geocnt > 0) { echo '
'; echo ''; //$disabled = ' disabled="disabled"'; echo '' . $lang['srv_maza_geofencing_on'] . '
'; ///////////////////////////////////AKCIJA echo '

'; echo '
'; } else echo '' . $lang['srv_maza_geofencing_off'] . '
'; //form to set or update GEOFENCES echo '
'; echo ''; echo ''; //izrisi search box za v mapo echo ''; //DIV for google map echo '
'; echo '
'; //NOTIFICATION SETTINGS //FORM FOR geofence notification $sql_r = sisplet_query("SELECT * FROM maza_srv_geofences WHERE ank_id='" . $this->_ank_id . "' LIMIT 1", 'obj'); echo '
'; echo '' . $lang['srv_maza_geofencing_notification'] . ''; echo '
'; echo '
'; if(!$sql_r) $sql_r = (object)array('notif_title'=>'', 'notif_message'=>'', 'notif_sound'=>1, 'trigger_survey' => "1", 'on_transition'=>'dwell', 'after_seconds' => 300); $disable = ($sql_r->trigger_survey == null) ? ' disabled="disabled"' : ''; echo '
'; echo '

'; echo '
'; $sql_r->notif_title = ($sql_r->notif_title) ? $sql_r->notif_title : $lang['srv_maza_geofence_default_title']; $sql_r->notif_message = ($sql_r->notif_message) ? $sql_r->notif_message : $lang['srv_maza_alarm_default_message'].$this->_ank_title; echo '' . $lang['srv_notifications_send_title'] . ':

'; echo '' . $lang['srv_notifications_send_text'] . ':

'; /*echo '

';*/ //echo '

'; echo '
'; echo '
'; echo '
'; echo '

'; echo '
'; echo '' . $lang['srv_maza_alarm_when_to_show'] . ''; echo '

'; echo '
'; echo '' . $lang['srv_maza_geofence_after'] . ': ' . ' ' . $lang['srv_maza_geofence_time_unit'] . '
'; echo '
'; echo '
'; echo '

'; echo ''; echo '

' . $_POST['maza_result'] . '

'; echo '
'; echo '
'; echo '
'; echo '
'; echo '' . $lang['srv_maza_data_export'] . ''; echo '' . '' . $lang['srv_maza_geofence_export_triggered_answered'] . ''; echo '' . '' . $lang['srv_maza_geofence_export_triggered'] . ''; echo '' . '' . $lang['srv_maza_geofence_export'] . ''; echo '
'; } // Obrazec za posiljanje nextpin aktivnosti private function setActivityForm() { global $lang; echo '
'; echo '' . $lang['srv_maza_activity'] . ''; //FORM FOR activity $sql_r = sisplet_query("SELECT COUNT(activity_on) AS cnt FROM maza_srv_activity WHERE ank_id='" . $this->_ank_id . "' AND activity_on=1", 'obj'); if ($sql_r->cnt > 0) { echo '
'; echo ''; //$disabled = ' disabled="disabled"'; echo '' . $lang['srv_maza_activity_on'] . '
'; ///////////////////////////////////AKCIJA echo '

'; echo '
'; } else echo '' . $lang['srv_maza_activity_off'] . '
'; //form to set or update activity echo '
'; echo ''; echo ''; //NOTIFICATION SETTINGS //FORM FOR activity notification $sql_r = sisplet_query("SELECT * FROM maza_srv_activity WHERE ank_id='" . $this->_ank_id . "' LIMIT 1", 'obj'); echo '
'; echo '' . $lang['srv_maza_geofencing_notification'] . ''; echo '
'; echo '
'; if(!$sql_r) $sql_r = (object)array('notif_title'=>'', 'notif_message'=>'', 'notif_sound'=>1, 'activity_type'=>'path', 'after_seconds' => 300); $sql_r->notif_title = ($sql_r->notif_title) ? $sql_r->notif_title : $lang['srv_maza_geofence_default_title']; $sql_r->notif_message = ($sql_r->notif_message) ? $sql_r->notif_message : $lang['srv_maza_alarm_default_message'].$this->_ank_title; echo '' . $lang['srv_notifications_send_title'] . ':

'; echo '' . $lang['srv_notifications_send_text'] . ':

'; /*echo '

';*/ //echo '

'; echo '
'; echo '
'; echo '' . $lang['srv_maza_alarm_when_to_show'] . ''; echo '

'; echo '
'; echo '' . $lang['srv_maza_activity_after'] . ': ' . ' ' . $lang['srv_maza_geofence_time_unit'] . '
'; echo '
'; echo '
'; echo '

'; echo ''; echo '

' . $_POST['maza_result'] . '

'; echo '
'; echo '
'; } // Obrazec za posiljanje sledenja private function setTrackingForm() { global $lang; echo '
'; echo '' . $lang['srv_maza_tracking'] . ''; //FORM FOR activity $sql_r = sisplet_query("SELECT COUNT(tracking_on) AS cnt FROM maza_srv_tracking WHERE ank_id='" . $this->_ank_id . "' AND tracking_on=1", 'obj'); if ($sql_r->cnt > 0) { echo '
'; echo ''; //$disabled = ' disabled="disabled"'; echo '' . $lang['srv_maza_tracking_on'] . '
'; ///////////////////////////////////AKCIJA echo '

'; echo '
'; } else echo '' . $lang['srv_maza_tracking_off'] . '
'; //form to set or update activity echo '
'; echo ''; echo ''; //NOTIFICATION SETTINGS //FORM FOR activity notification $sql_r = sisplet_query("SELECT * FROM maza_srv_tracking WHERE ank_id='" . $this->_ank_id . "' LIMIT 1", 'obj'); echo '
'; echo '' . $lang['srv_maza_tracking_settings'] . ''; echo '
'; if(!$sql_r) $sql_r = (object)array('activity_recognition'=>0, 'tracking_accuracy'=>'high', 'interval_wanted' => 30, 'interval_fastes' => 10, 'displacement_min' => 10, 'ar_interval_wanted' => 30); echo '
'; echo '' . $lang['srv_maza_tracking_accuracy'] . ': '; echo '

'; echo '
'; echo '' . $lang['srv_maza_tracking_interval_wanted'] . ': ' . ' ' . $lang['srv_maza_geofence_time_unit'] . '
'; echo '
'; echo '
'; echo '' . $lang['srv_maza_tracking_interval_fastes'] . ': ' . ' ' . $lang['srv_maza_geofence_time_unit'] . '
'; echo '
'; echo '
'; echo '' . $lang['srv_maza_tracking_displacement_min'] . ': ' . ' ' . $lang['srv_maza_tracking_displacement_m'] . '
'; echo '
'; echo '
'; echo '
'; echo '

'; echo '
'; echo '
activity_recognition == 1) ? '' : 'hidden').'>'; echo '' . $lang['srv_maza_tracking_interval_wanted'] . ': ' . ' ' . $lang['srv_maza_geofence_time_unit'] . '
'; echo '
'; echo '
'; echo '

'; echo ''; echo '

' . $_POST['maza_result'] . '

'; echo '
'; echo '
'; echo '
'; echo '
'; echo '' . $lang['srv_maza_data_export'] . ''; echo '' . '' . $lang['srv_maza_tracking_export_all_locations'] . ''; echo '' . '' . $lang['srv_maza_tracking_export_all_ar'] . ''; echo '
'; } // Obrazec za nastavljanje vnosov private function setEntryForm() { global $lang; echo '
'; echo '' . $lang['srv_maza_entry'] . ''; //FORM FOR DATA ENTRIES $sql_r = sisplet_query("SELECT COUNT(entry_on) AS cnt FROM maza_srv_entry WHERE ank_id='" . $this->_ank_id . "' AND entry_on=1", 'obj'); if ($sql_r->cnt > 0) { echo '
'; echo ''; //$disabled = ' disabled="disabled"'; echo '' . $lang['srv_maza_entry_on'] . '
'; ///////////////////////////////////AKCIJA echo '

'; echo '
'; } else echo '' . $lang['srv_maza_entry_off'] . '
'; //form to set or update entry echo '
'; echo ''; echo ''; $sql_r = sisplet_query("SELECT * FROM maza_srv_entry WHERE ank_id='" . $this->_ank_id . "' LIMIT 1", 'obj'); echo '
'; echo '

'; echo '

'; echo ''; echo '

' . $_POST['maza_result'] . '

'; echo '
'; echo '
'; echo '
'; echo '
'; echo '' . $lang['srv_maza_data_export'] . ''; echo '' . '' . $lang['srv_maza_tracking_export_all_locations'] . ''; echo '
'; } // Obrazec za generiranje novih identifikatorjev private function generateNewUsersForm() { global $lang; //FORM FOR NOTIFICATION echo '
'; echo '' . $lang['srv_maza_users_generator'] . ''; echo '
'; echo ''; echo ''; echo '' . $lang['srv_maza_users_generator_number'] . ':
'; echo ''; echo '
'; echo '
'; } // Obrazec za urejanje opisa raziskave private function surveyDescription() { global $lang; $sql_r = sisplet_query("SELECT srv_description FROM maza_survey WHERE srv_id='" . $this->_ank_id . "'", 'obj'); //FORM FOR NOTIFICATION echo '
'; echo '' . $lang['srv_maza_users_description'] . ''; echo '
'; echo ''; echo ''; echo '

'; echo ''; echo '
'; echo '
'; } // Obrazec za izvoz vseh identifikatorjev private function exportIdentifiersForm() { global $lang; echo '
'; echo '' . $lang['srv_maza_data_export'] . ''; echo '
'; echo '
'; echo '
'; echo '

'; echo '' . '' . $lang['srv_lnk_excel'] . ''; echo '
'; echo '
'; //header('location: ' . makeEncodedIzvozUrlString('izvoz.php?b=export&m=maza_csv&anketa=' . $this->_ank_id) . '&m=maza_csv&a=ident_export'); } // Obrazec za posiljanje notificationa private function getUsersStatistics() { global $lang; $Query = "SELECT COUNT(*) cnt FROM maza_user_srv_access AS sa " . "LEFT JOIN (SELECT id, datetime_last_active FROM maza_app_users) AS au " . "ON au.id = sa.maza_user_id WHERE sa.ank_id='$this->_ank_id' AND au.datetime_last_active IS NOT NULL AND datetime_unsubscribed IS NULL;"; $resultActivated = sisplet_query($Query, "obj"); $Query = "SELECT COUNT(*) cnt FROM maza_user_srv_access " . "WHERE ank_id='$this->_ank_id' AND datetime_unsubscribed IS NOT NULL;"; $resultDeactivated = sisplet_query($Query, "obj"); $Query = "SELECT COUNT(*) cnt FROM maza_user_srv_access WHERE ank_id='$this->_ank_id';"; $resultAll = sisplet_query($Query, "obj"); //TABLE OF USERS STATISTICS echo '
'; echo '' . $lang['srv_maza_users_statistics'] . ''; echo '' . $lang['srv_maza_users_activated'] . ':' . $resultActivated->cnt.'
'; echo '' . $lang['srv_maza_users_inactive'] . ':' . ($resultAll->cnt - $resultActivated->cnt - $resultDeactivated->cnt) .'
'; echo '' . $lang['srv_maza_users_deactive'] . ':' . $resultDeactivated->cnt .'
'; echo '
'.$lang['srv_maza_users_sum'].': '.$resultAll->cnt.'

'; //echo '' . $lang['srv_lnk_excel'] . ' (' . $lang['srv_maza_users_inactive'] . ')'; //echo '' . $lang['srv_lnk_excel'] . ' (' . $lang['srv_maza_users_activated'] . ')'; echo '
'; } function addUrl($what) { global $site_url; if ($what == null || trim($what) == '') { $what = 'maza_dashboard'; } $url = $site_url . 'admin/survey/index.php?anketa=' . $this->_ank_id . '&a=' . A_MAZA . '&m=' . $what; return $url; } private function send_FCM_message($msg, $action) { //get all registration ids $reg_ids = array(); $Query = "SELECT registration_id FROM maza_app_users AS au " . "LEFT JOIN (SELECT maza_user_id, ank_id FROM maza_user_srv_access) AS sa " . "ON au.id = sa.maza_user_id WHERE sa.ank_id='$this->_ank_id' AND " . "au.registration_id != 'NULL' AND au.registration_id != ''"; $sql_array = sisplet_query($Query, 'array'); //$rql = mysqli_fetch_assoc ($reg_ids); foreach ($sql_array as $pair) array_push($reg_ids, $pair['registration_id']); $fields = array ( //can send to max 1000 reg_ids at once - split it on 1000 and send each chuck 'registration_ids' => $reg_ids, 'data' => $msg ); if ((isset($_POST['maza_notification_priority']) && $_POST['maza_notification_priority'] == 1) || $action == 'alarm' || $action == 'geofencing' || $action == 'activity' || $action == 'tracking' || $action == 'entry' || $action == 'all') $fields['priority'] = 'high'; //do a curl to send notifications $result = $this->send_FCM_mesage_curl($fields); $this->send_FCM_message_navigate($action, $fields, $result); } /** * Do a curl to firebase to send notifications to devices * @global type $FCM_server_key - FCM server key * @param type $fields - json object containig FCM registration ids of devices and data to send * @return type - json result of curl output */ private function send_FCM_mesage_curl($fields){ #API access key from Google API's Console $headers = array ( 'Authorization: key=' . AppSettings::getInstance()->getSetting('maza-FCM_server_key'), 'Content-Type: application/json' ); #Send Reponse To FireBase Server $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); $result = curl_exec($ch); curl_close($ch); #Echo Result Of FireBase Server //error_log($result); //error_log(json_encode($headers)); //error_log(json_encode($fields)); //error_log(json_encode($result)); return $result; } /** * After sending FCM message, navigate to right submodule * @param type $action - action or name of submodule * @param type $fields - optional (just for test) * @param type $headers - optional (just for test) * @param type $result - optional (just for test) */ private function send_FCM_message_navigate($action, $fields, $result){ switch ($action) { case 'alarm': $action = '&m=maza_set_alarm'; break; case 'notification': $action = '&m=maza_send_notification'; break; case 'geofencing': $action = '&m=maza_set_geofencing'; break; case 'activity': $action = '&m=maza_set_activity'; break; case 'tracking': $action = '&m=maza_set_tracking'; break; case 'entry': $action = '&m=maza_set_entry'; break; case 'survey_info': $action = '&m=maza_dashboard'; break; default: $action = '&m=maza_dashboard'; break; } if($action) header('location: index.php?anketa=' . $this->_ank_id . '&a=maza' . $action . '&FCM_response=' . json_encode($fields, JSON_UNESCAPED_SLASHES). /*json_encode($headers, JSON_UNESCAPED_SLASHES).*/ json_encode($result, JSON_UNESCAPED_SLASHES)); } public function ajax_sendNotification() { $action = $_POST['maza_action']; $title = $_POST['maza_title']; $message = $_POST['maza_message']; $srv_title = ""; $repeat = 0; $repeater = 0; $geofences = 0; $activity = 0; $entry = 0; if ($action == 'alarm'){ $this->maza_save_alarm($title, $message, /*$_POST['maza_notification_sound']*/1); $sql_a = sisplet_query("SELECT repeat_by, time_in_day, day_in_week, every_which_day FROM maza_srv_alarms WHERE ank_id='" . $this->_ank_id . "'", 'obj'); $repeat = array("repeat_by" => $sql_a->repeat_by, "time_in_day" => json_decode($sql_a->time_in_day), "day_in_week" => json_decode($sql_a->day_in_week), "every_which_day" => $sql_a->every_which_day); $sql_r = sisplet_query("SELECT repeat_by, time_in_day, day_in_week, every_which_day, datetime_start, datetime_end FROM maza_srv_repeaters WHERE ank_id='" . $this->_ank_id . "'", 'obj'); $repeater = array("repeat_by" => $sql_r->repeat_by, "time_in_day" => json_decode($sql_r->time_in_day), "day_in_week" => json_decode($sql_r->day_in_week), "every_which_day" => $sql_r->every_which_day, "datetime_start" => $sql_r->datetime_start, "datetime_end" => $sql_r->datetime_end); } else if ($action == 'geofencing'){ $geofences = $this->getRunningGeofences(); } else if ($action == 'activity'){ $sql_a = sisplet_query("SELECT id, activity_type, after_seconds FROM maza_srv_activity WHERE ank_id='" . $this->_ank_id . "'", 'obj'); $activity = array("id" => $sql_a->id, "activity_type" => $sql_a->activity_type, "after_seconds" => $sql_a->after_seconds); $sql_n = sisplet_query("SELECT naslov FROM srv_anketa WHERE id='" . $this->_ank_id . "'", 'obj'); $srv_title = $sql_n->naslov; } else if ($action == 'tracking'){ $sql_a = sisplet_query("SELECT * FROM maza_srv_tracking WHERE ank_id='" . $this->_ank_id . "'", 'obj'); $tracking = array("id" => $sql_a->id, "activity_recognition" => $sql_a->activity_recognition, "tracking_accuracy" => $sql_a->tracking_accuracy, "interval_wanted" => $sql_a->interval_wanted, "interval_fastes" => $sql_a->interval_fastes, "displacement_min" => $sql_a->displacement_min, "ar_interval_wanted" => $sql_a->ar_interval_wanted); $sql_n = sisplet_query("SELECT naslov FROM srv_anketa WHERE id='" . $this->_ank_id . "'", 'obj'); $srv_title = $sql_n->naslov; } else if ($action == 'entry'){ $entry = $this->getRunningEntry(); $sql_n = sisplet_query("SELECT naslov FROM srv_anketa WHERE id='" . $this->_ank_id . "'", 'obj'); $srv_title = $sql_n->naslov; } #prep the bundle $msg = array ( 'action' => $action, 'ank_id' => $this->_ank_id, 'message' => $message, 'title' => $title, 'link' => $this->_ank_link, 'srv_title' => $srv_title, 'repeat' => $repeat, 'repeater' => $repeater, 'geofences' => $geofences, 'activity' => $activity, 'tracking' => $tracking, 'entry' => $entry, 'sound' => /*(isset($_POST['maza_notification_sound']) && $_POST['maza_notification_sound'] == 1) ?*/ 1 //: 0 ); $this->send_FCM_message($msg, $action); } private function send_FCM_message_pwa($msg, $action) { $reg_ids = sisplet_query("SELECT endpoint_key FROM browser_notifications_respondents", 'onevalarray'); //get all registration ids /*$reg_ids = array('cJyD7jXFuGY:APA91bGmB4qXhZE2QOxPbjHm8ZTeyslXaejUfyVDmr06FDm-2p76sF2_A8Q0HGm0EZGdJ_O_iUACfJcsXznVWGe5SeDuI9S8F5HgqwZ1d7G2hN0PQiJL7Q8TlqjWCpbajjAWZ_OmxzLU', 'e0jGT0OYYCY:APA91bGRjTI7iAuuvevYEtz9QLg5bQE2ha1L44PLSVg_kkrFn4up7ZeHzh50dW9vOxCch_hdTs-cEmx3HKuKIoxOnvCMuj7faInTkU4t80fxOjL7D1isuEmWXBM52pheEiE9nScF-i63');*/ /*$Query = "SELECT registration_id FROM maza_app_users AS au " . "LEFT JOIN (SELECT maza_user_id, ank_id FROM maza_user_srv_access) AS sa " . "ON au.id = sa.maza_user_id WHERE sa.ank_id='$this->_ank_id' AND " . "au.registration_id != 'NULL' AND au.registration_id != ''"; $sql_array = sisplet_query($Query, 'array'); //$rql = mysqli_fetch_assoc ($reg_ids); foreach ($sql_array as $pair) array_push($reg_ids, $pair['registration_id']);*/ $fields = array ( //can send to max 1000 reg_ids at once - split it on 1000 and send each chuck 'registration_ids' => $reg_ids, 'data' => $msg ); //do a curl to send notifications $result = $this->send_FCM_mesage_curl($fields); $this->send_FCM_message_navigate($action, $fields, $result); } public function ajax_sendNotification_pwa() { $action = $_POST['maza_action']; $title = $_POST['maza_title']; $message = $_POST['maza_message']; $sql_n = sisplet_query("SELECT naslov FROM srv_anketa WHERE id='" . $this->_ank_id . "'", 'obj'); #prep the bundle $msg = array ( 'action' => $action, 'ank_id' => $this->_ank_id, 'message' => $message, 'title' => $title, 'link' => $this->_ank_link, 'srv_title' => $sql_n->naslov, 'sound' => /*(isset($_POST['maza_notification_sound']) && $_POST['maza_notification_sound'] == 1) ?*/ 1 //: 0 ); $this->send_FCM_message_pwa($msg, $action); } /** * Check if id of maza app user and indentifier match * @param type $maza_uid - id of maza app auser * @param type $maza_identifier - string identifier of maza app user * @return boolean - true if match, false otherwise */ public function maza_validate_user($maza_uid, $maza_identifier) { $sql = sisplet_query("SELECT * FROM maza_app_users WHERE id='$maza_uid' AND identifier='$maza_identifier'"); if (mysqli_num_rows($sql) > 0) return true; else return false; } /** * Update datetime of user last_active * @param type $maza_uid - id of maza app user */ public function maza_update_user_active($maza_uid, $registration_id = null) { $registration_id_query = ''; if (isset($registration_id) && $registration_id != '') $registration_id_query = ", registration_id = '$registration_id'"; sisplet_query("UPDATE maza_app_users SET datetime_last_active = NOW()$registration_id_query WHERE id='$maza_uid'"); } /** * Insert pair of users id's in maza_srv_users table if not already exists * @param type $maza_uid - id of maza app user * @param type $srv_uid - id of survey respondent * @param type $srv_version - timestamp version of survey (important with repeaters) * @param type $tgeofence_id - id of triggered geofence * @param type $tactivity_id - id of triggered activity * @param type $mode - mode of survey (repeater, geofence, activity, entry) */ public function maza_save_srv_user($maza_uid, $srv_uid, $srv_version, $tgeofence_id=null, $tactivity_id=null, $mode=null) { //check if pair already existst in DB $sql = sisplet_query("SELECT * FROM maza_srv_users WHERE maza_user_id='$maza_uid' AND srv_user_id='$srv_uid'"); //it is already there if (mysqli_num_rows($sql) > 0) return false; else { if(!$tgeofence_id) $tgeofence_id = 'NULL'; if(!$tactivity_id) $tactivity_id = 'NULL'; //get datetime from version if($srv_version){ $myDateTime = new DateTime(); $myDateTime->setTimestamp($srv_version); $newDateString = $myDateTime->format('Y-m-d H:i:s'); } //insert in table sisplet_query("INSERT INTO maza_srv_users (maza_user_id, srv_user_id, srv_version_datetime, tgeof_id, tact_id, mode) VALUES ('" . $maza_uid . "', '" . $srv_uid . "', '$newDateString', $tgeofence_id, $tactivity_id, ".($mode ? "'$mode'" : 'NULL').");"); return true; } } public function ajax_maza_on_off() { $on_off = json_decode($_POST['on_off']); if (isset($on_off) && $on_off) $this->maza_on(); else $this->maza_off(); } public function ajax_maza_cancel_alarm() { $msg = array ( 'action' => 'cancel_alarm', 'srv_id' => $this->_ank_id ); sisplet_query("UPDATE maza_srv_alarms SET alarm_on='0' WHERE ank_id='" . $this->_ank_id . "'"); $this->send_FCM_message($msg, 'alarm'); } public function maza_off($ank_id = null) { if($ank_id) $this->_ank_id = $ank_id; $this->maza_cancel_repeater_db(); $this->maza_cancel_geofencing_db(); $this->maza_cancel_entry_db(); $this->maza_cancel_activity_db(); $this->maza_cancel_tracking_db(); $msg = array ( 'action' => 'cancel_all', 'srv_id' => $this->_ank_id ); $this->send_FCM_message($msg, 'all'); } public function maza_cancel_repeater(){ $msg = array ( 'action' => 'stop_repeater', 'srv_id' => $this->_ank_id ); $this->send_FCM_message($msg, 'alarm'); $this->maza_cancel_repeater_db(); header('location: index.php?anketa=' . $this->_ank_id . '&a=maza&m=maza_set_alarm'); } public function maza_cancel_repeater_db(){ sisplet_query("UPDATE maza_srv_alarms SET alarm_on='0' WHERE ank_id = '" . $this->_ank_id . "'"); //for now, we cannot change repeater of survey - repeater_on = 2 means that survey was canceled while repeater was on sisplet_query("UPDATE maza_srv_repeaters SET repeater_on='2', datetime_end=NOW() WHERE ank_id='" . $this->_ank_id . "'"); } public function maza_on() { $sel = sisplet_query("SELECT COUNT(id) as cnt FROM maza_srv_alarms WHERE ank_id = '" . $this->_ank_id . "';", 'obj'); if($sel->cnt == 0) sisplet_query("INSERT INTO maza_srv_alarms (ank_id) VALUES ('" . $this->_ank_id . "')"); $sel = sisplet_query("SELECT COUNT(id) as cnt FROM maza_srv_repeaters WHERE ank_id = '" . $this->_ank_id . "';", 'obj'); if($sel->cnt == 0) sisplet_query("INSERT INTO maza_srv_repeaters (ank_id) VALUES ('" . $this->_ank_id . "')"); $sel = sisplet_query("SELECT COUNT(id) as cnt FROM maza_survey WHERE srv_id = '" . $this->_ank_id . "';", 'obj'); if($sel->cnt == 0) sisplet_query("INSERT INTO maza_survey (srv_id) VALUES ('" . $this->_ank_id . "')"); } private function maza_save_alarm($title, $message, $sound) { //for now, available title is only default sisplet_query("UPDATE maza_srv_alarms SET alarm_on='1', "//alarm_notif_title='$title', " . "alarm_notif_message='$message', alarm_notif_sound='$sound' WHERE ank_id='" . $this->_ank_id . "'"); } //returns random alphanumerical code public function randomAlphaNumericCode($length) { $token = ""; $codeAlphabet = "ABCDEFGHJKLMNOPQRSTUVWXYZ"; $codeAlphabet .= "abcdefghijkmnopqrstuvwxyz"; $codeAlphabet .= "0123456789"; $max = /*strlen($codeAlphabet)-1;*/59; // edited for ($i = 0; $i < $length; $i++) { $token .= $codeAlphabet[mt_rand(0, $max)]; } return $token; } /** * Generate new users or * @param type $howManyUsers - number of new users to generate or 'self' for self identifier creation by app via API * @return array array of identifiers or pair of id and identifier if 'self' */ public function insertNewUsers ($howManyUsers = 0){ //create users from modul if($howManyUsers > 0){ $identifiers = array(); $query = "BEGIN; "; for($i = 0; $i < $howManyUsers; $i++){ $generated_identifier = $this -> randomAlphaNumericCode(8); //$salted_identifier = base64_encode(hash(SHA256, $generated_identifier . $pass_salt)); array_push($identifiers, $generated_identifier); $query .= "INSERT INTO maza_app_users (identifier) VALUES('$generated_identifier'); INSERT INTO maza_user_srv_access (maza_user_id, ank_id) VALUES(LAST_INSERT_ID(), '$this->_ank_id'); "; } $query .= "COMMIT;"; sisplet_query($query, 'multi_query'); return $identifiers; } //user self creates an identifier (from mobile app via API) elseif($howManyUsers == 'self'){ $generated_identifier = $this -> randomAlphaNumericCode(8); sisplet_query("INSERT INTO maza_app_users (identifier, datetime_last_active) VALUES ('".$generated_identifier."', NOW())"); $id = mysqli_insert_id($GLOBALS['connect_db']); return array("id"=>$id, "identifier"=>$generated_identifier); } } public function ajax_maza_generate_users() { $userNumb = $_POST['maza_users_generator_number']; $this->insertNewUsers($userNumb); header('location: index.php?anketa=' . $this->_ank_id . '&a=maza&m=maza_dashboard'); } public function ajax_maza_survey_description() { $desc = $_POST['srv_maza_users_description']; sisplet_query("UPDATE maza_survey SET srv_description='$desc' WHERE srv_id='" . $this->_ank_id . "'"); /*$msg = array ( 'action' => 'survey_info', 'description' => $desc, 'srv_id' => $this->_ank_id ); $this->send_FCM_message($msg, 'survey_info');*/ header('location: index.php?anketa=' . $this->_ank_id . '&a=maza&m=maza_dashboard'); } public function ajax_changeRepeatBy() { $repeatby = $_POST['maza_repeatby']; $table = $_POST['maza_table']; sisplet_query("UPDATE maza_srv_$table SET repeat_by='$repeatby' WHERE ank_id='" . $this->_ank_id . "'"); } public function ajax_changeTimeInDay() { $time_in_day = json_encode($_POST['maza_time_in_day']); $table = $_POST['maza_table']; sisplet_query("UPDATE maza_srv_$table SET time_in_day='$time_in_day' WHERE ank_id='" . $this->_ank_id . "'"); } public function ajax_changeDayInWeek() { $day_in_week = json_encode($_POST['maza_day_in_week']); $table = $_POST['maza_table']; sisplet_query("UPDATE maza_srv_$table SET day_in_week='$day_in_week' WHERE ank_id='" . $this->_ank_id . "'"); } public function ajax_changeEveryWhichDay() { $every_which_day = $_POST['maza_every_which_day']; $table = $_POST['maza_table']; sisplet_query("UPDATE maza_srv_$table SET every_which_day='$every_which_day' WHERE ank_id='" . $this->_ank_id . "'"); } public function ajax_saveRepeater() { $repeatby = $_POST['maza_repeater_intervalby']; $time_in_day = json_encode($_POST['maza_repeater_timeinday']); $every_which_day = ($_POST['maza_repeater_everywhichday']) ? $_POST['maza_repeater_everywhichday'] : 1; $day_in_week = json_encode($_POST['maza_repeater_dayinweek']); $Start_date = $_POST['maza_repeater_date_start']; $End_date = $_POST['maza_repeater_date_end']; if($Start_date){ if($End_date){ $End_date = DateTime::createFromFormat('d.m.Y', $End_date); $End_date = $End_date->format("Y-m-d"); $End_date = "datetime_end='$End_date', "; } $Start_date = DateTime::createFromFormat('d.m.Y', $Start_date); $Start_date = $Start_date->format("Y-m-d"); sisplet_query("UPDATE maza_srv_repeaters SET repeater_on='1', datetime_start='$Start_date', $End_date" . "every_which_day='$every_which_day', day_in_week='$day_in_week', " . "time_in_day='$time_in_day', repeat_by='$repeatby' WHERE ank_id='" . $this->_ank_id . "'"); } header('location: index.php?anketa=' . $this->_ank_id . '&a=maza&m=maza_set_alarm'); } /** * Insert/save new geofence */ function ajax_insert_geofence(){ $add = $_POST['address']; $lat = $_POST['lat']; $lng = $_POST['lng']; $rad = $_POST['radius']; $trigger_survey = "NOW()"; $sql = sisplet_query("SELECT trigger_survey FROM maza_srv_geofences WHERE ank_id=".$this->_ank_id." LIMIT 1", 'obj'); if($sql && $sql->trigger_survey==null){ $trigger_survey="NULL"; } //last decimals of coordiates are not exact same in database, because float in mySql is not precise - practical variations are minimal $id = sisplet_query("INSERT INTO maza_srv_geofences (ank_id, address, lat, lng, radius, trigger_survey) " . "VALUES ('$this->_ank_id', '$add', '$lat', '$lng', '$rad', $trigger_survey)", "id"); echo $id; } /** * Update geofence by ID */ function ajax_update_geofence(){ if($_POST['id']){ $update = ''; $update .= $_POST['address'] ? "address = '".$_POST['address']."', " : ''; $update .= $_POST['lat'] ? "lat = '".$_POST['lat']."', " : ''; $update .= $_POST['lng'] ? "lng = '".$_POST['lng']."', " : ''; $update .= $_POST['radius'] ? "radius = '".$_POST['radius']."', " : ''; $update = substr($update, 0, -2); //last decimals of coordiates are not exact same in database, because float in mySql is not precise - practical variations are minimal sisplet_query("UPDATE maza_srv_geofences SET $update WHERE id='".$_POST['id']."'"); } } /** * Update geofenceinternal name by ID */ function ajax_update_geofence_name(){ if($_POST['id']){ sisplet_query("UPDATE maza_srv_geofences SET name='".$_POST['name']."' WHERE id='".$_POST['id']."'"); } } /** * Delete geofence by ID */ function ajax_delete_geofence(){ if($_POST['id']){ $sql = sisplet_query("DELETE FROM maza_srv_geofences WHERE id='".$_POST['id']."'"); if($sql) echo 'OK'; } } /** * Get array of geofences for this survey * @return type string JSON array of all geofences for this survey */ function ajax_get_all_geofences(){ $sql = sisplet_query("SELECT * FROM maza_srv_geofences WHERE ank_id=".$this->_ank_id, 'array'); echo json_encode($sql); } /** * Calncel all geofences for this survey * Turn geofences off in DB and send cancelation to all subscriptors via FCM */ public function maza_cancel_geofencing(){ $msg = array ( 'action' => 'cancel_geofencing', 'srv_id' => $this->_ank_id ); $this->send_FCM_message($msg, 'geofencing'); $this->maza_cancel_geofencing_db(); header('location: index.php?anketa=' . $this->_ank_id . '&a=maza&m=maza_set_geofencing'); } public function maza_cancel_geofencing_db(){ sisplet_query("UPDATE maza_srv_geofences SET geofence_on='0' WHERE ank_id = '" . $this->_ank_id . "'"); } /** * Run all geofences for this survey * Turn geofences on in DB and send them to subscriptors via FCM */ public function maza_run_geofences(){ $title = $_POST['maza_title']; $message = $_POST['maza_message']; $sound = /*$_POST['maza_notification_sound']*/1; $transition = $_POST['maza_geofence_on_transition']; $time = $_POST['maza_geofence_after_time']; $location_triggered = $_POST['maza_geofence_location_triggered']; $trigger_survey = $_POST['maza_geofence_trigger_survey']; sisplet_query("UPDATE maza_srv_geofences SET geofence_on='1', notif_title='$title', " . "notif_message='$message', notif_sound='$sound', after_seconds='$time', " . "on_transition='$transition', location_triggered='$location_triggered', " . "trigger_survey=".($trigger_survey ? 'NULL' : 'NOW()' )." " . "WHERE ank_id='" . $this->_ank_id . "'"); $this->ajax_sendNotification(); } /** * Get array of running geofences for this survey * @return type array of all running geofences for this survey */ public function getRunningGeofences(){ return sisplet_query("SELECT * FROM maza_srv_geofences WHERE geofence_on=1 AND ank_id='" . $this->_ank_id . "'", 'array'); } /** * Get object of running data entry for this survey * @return type object of running data entry for this survey */ private function getRunningEntry(){ return sisplet_query("SELECT * FROM maza_srv_entry WHERE entry_on=1 AND ank_id='" . $this->_ank_id . "'", 'obj'); } /** * Calncel data entry for this survey * Turn data entry off in DB and send cancelation to all subscriptors via FCM */ public function maza_cancel_entry(){ $msg = array ( 'action' => 'cancel_entry', 'srv_id' => $this->_ank_id ); $this->send_FCM_message($msg, 'entry'); $this->maza_cancel_entry_db(); header('location: index.php?anketa=' . $this->_ank_id . '&a=maza&m=maza_set_entry'); } public function maza_cancel_entry_db(){ sisplet_query("UPDATE maza_srv_entry SET entry_on='0' WHERE ank_id = '" . $this->_ank_id . "'"); } /** * Run all data entry for this survey * Turn data entry on in DB and send them to subscriptors via FCM */ public function maza_run_entry(){ $location_triggered = $_POST['maza_entry_location_check']; $sel = sisplet_query("SELECT COUNT(id) as cnt FROM maza_srv_entry WHERE ank_id = '" . $this->_ank_id . "';", 'obj'); if($sel->cnt == 0) sisplet_query("INSERT INTO maza_srv_entry (ank_id, location_check, entry_on) VALUES ('" . $this->_ank_id . "', '$location_triggered', '1')"); else sisplet_query("UPDATE maza_srv_entry SET entry_on='1', location_check='$location_triggered' WHERE ank_id='" . $this->_ank_id . "'"); //if location is on, create system variables for coordinates if($location_triggered == '1'){ //is there already system varibles in survey for coordinates $sel = sisplet_query("SELECT COUNT(s.id) as cnt, g.id, s.gru_id, g.ank_id, s.variable FROM srv_spremenljivka as s " . "LEFT JOIN (SELECT id, ank_id FROM srv_grupa) AS g ON g.id = s.gru_id " . "WHERE g.ank_id='$this->_ank_id' AND variable='latitude';", 'obj'); //sys variables not created yet if($sel->cnt == 0){ //create empty variables $ba = new BranchingAjax($this->_ank_id); $spr_lng = $ba->spremenljivka_new(0, 0, 0, 0, true); $spr_lat = $ba->spremenljivka_new(0, 0, 0, 0, true); //set system variables sisplet_query("UPDATE srv_spremenljivka SET naslov='latitude', variable='latitude', tip='21', " . "visible='0', sistem='1', variable_custom='1' WHERE id='$spr_lat';"); sisplet_query("UPDATE srv_spremenljivka SET naslov='longitude', variable='longitude', tip='21', " . "visible='0', sistem='1', variable_custom='1' WHERE id='$spr_lng';"); sisplet_query("INSERT INTO srv_vrednost (id, spr_id, naslov, variable, vrstni_red, variable_custom, hidden) VALUES ('', '$spr_lat', 'latitude', 'latitude', '1', '1', '1');"); sisplet_query("INSERT INTO srv_vrednost (id, spr_id, naslov, variable, vrstni_red, variable_custom, hidden) VALUES ('', '$spr_lng', 'longitude', 'longitude', '1', '1', '1');"); Common::getInstance()->updateEditStamp(); } } $this->ajax_sendNotification(); } /** * Run activity for this survey * Turn activity on in DB and send them to subscriptors via FCM */ public function maza_run_activity(){ $title = $_POST['maza_title']; $message = $_POST['maza_message']; $sound = /*$_POST['maza_notification_sound']*/1; $transition = $_POST['maza_activity_type']; $time = $_POST['maza_activity_after_time']; $sel = sisplet_query("SELECT COUNT(id) as cnt FROM maza_srv_activity WHERE ank_id = '" . $this->_ank_id . "';", 'obj'); if($sel->cnt > 0) sisplet_query("UPDATE maza_srv_activity SET activity_on='1', notif_title='$title', " . "notif_message='$message', notif_sound='$sound', after_seconds='$time', activity_type='$transition' WHERE ank_id='" . $this->_ank_id . "'"); else sisplet_query("INSERT INTO maza_srv_activity (ank_id, activity_on, notif_title, notif_message, notif_sound, after_seconds, activity_type) " . "VALUES ('".$this->_ank_id."', '1', '".$title."', '".$message."', '".$sound."', '".$time."', '".$transition."')"); $this->ajax_sendNotification(); } /** * Calncel all activities for this survey * Turn activities off in DB and send cancelation to all subscriptors via FCM */ public function maza_cancel_activity(){ $msg = array ( 'action' => 'cancel_activity', 'srv_id' => $this->_ank_id ); $this->send_FCM_message($msg, 'activity'); $this->maza_cancel_activity_db(); header('location: index.php?anketa=' . $this->_ank_id . '&a=maza&m=maza_set_activity'); } public function maza_cancel_activity_db(){ sisplet_query("UPDATE maza_srv_activity SET activity_on='0' WHERE ank_id = '" . $this->_ank_id . "'"); } /** * Send post request to nextpin API to create new user * @param type $identifier - identifier of user to register on nextpin * @param type $password - passwor to set in nextpin for this user */ public function nextpin_create_user($identifier, $password){ #credentials to use nextpin API $fields = array ( 'username' => $this->nextpin_token_prefix . $identifier, 'password' => $password ); $this -> nextpin_API_call(AppSettings::getInstance()->getSetting('maza-NextPinMainToken'), $fields, 'auth/createUser'); } /** * Send post request to nextpin API to set new activity listener * @param type $identifier - identifier of user to register on nextpin * @param type $password - passwor to set in nextpin for this user */ public function nextpin_set_activity_listener($identifier){ global $site_url; $sql_a = sisplet_query("SELECT id, activity_type, after_seconds FROM maza_srv_activity WHERE ank_id='" . $this->_ank_id . "'", 'obj'); $target = $sql_a->activity_type == 'path' ? 'P' : 'S'; $fields = array ( 'target' => $target, 'trigger_group' => "1KAPanel_trigger_".$this->_ank_id, 'threshold' => $sql_a->after_seconds, 'host' => $site_url, 'port' => "80", 'path' => "/admin/survey/api/api.php?action=nextpinalarmgetter&act_id=".$sql_a->id."&identifier=".$identifier ); $this -> nextpin_API_call($this->nextpin_token_prefix.$identifier, $fields, 'alerts/setActivityForUser'); } /** * Send post request to nextpin API * @param type $token - token/username to use it in request header * @param type $body - data to put it in body of request * @param type $method - method to append in link after .../NextPin/ */ private function nextpin_API_call($token, $body, $method){ $headers = array ( 'token: ' . $token, 'Content-Type: application/json' ); //error_log(json_encode($body, JSON_UNESCAPED_SLASHES)); //error_log(json_encode($headers)); #Send Reponse To FireBase Server $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://traffic.ijs.si/NextPin/'.$method); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body, JSON_UNESCAPED_SLASHES)); $result = curl_exec($ch); curl_close($ch); $myfile = fopen("nextpin_test3.txt", "a+") or die("Unable to open file!"); //if($this->params['action'] != 'mazaUpdateTrackingLog'){ $date = new DateTime(); $date = $date->format('d.m.Y H:i:s'); //} fwrite($myfile, $date.' nextpin_API_call'."\n"); fwrite($myfile, "\n".json_encode($headers)."\n"); fwrite($myfile, json_encode($body, JSON_UNESCAPED_SLASHES)."\n"); fwrite($myfile, json_encode($result, JSON_UNESCAPED_SLASHES)."\n"); $txt = "------------------------------------------------------------------------------------------------\n\n"; fwrite($myfile, $txt); fclose($myfile); //error_log(json_encode($result)); } public function writeintxt ($text){ $myfile = fopen("nextpin_test3.txt", "a+") or die("Unable to open file!"); //if($this->params['action'] != 'mazaUpdateTrackingLog'){ $date = new DateTime(); $date = $date->format('d.m.Y H:i:s'); //} fwrite($myfile, $date.' '.$text."\n"); $txt = "------------------------------------------------------------------------------------------------\n\n"; fwrite($myfile, $txt); fclose($myfile); } /** * Run tracking for this survey * Turn tracking on in DB and send them to subscriptors via FCM */ public function maza_run_tracking(){ $activity_recognition = $_POST['maza_tracking_activity_recognition']; $tracking_accuracy = $_POST['maza_tracking_accuracy']; $interval_wanted = $_POST['maza_tracking_interval_wanted']; $interval_fastes = $_POST['maza_tracking_interval_fastes']; $displacement_min = $_POST['maza_tracking_displacement_min']; $ar_interval_wanted = $_POST['maza_tracking_ar_interval_wanted']; $sel = sisplet_query("SELECT COUNT(id) as cnt FROM maza_srv_tracking WHERE ank_id = '" . $this->_ank_id . "';", 'obj'); if($sel->cnt > 0) sisplet_query("UPDATE maza_srv_tracking SET tracking_on='1', activity_recognition='$activity_recognition', " . "tracking_accuracy='$tracking_accuracy', interval_wanted='$interval_wanted', " . "interval_fastes='$interval_fastes', displacement_min='$displacement_min', " . "ar_interval_wanted='$ar_interval_wanted' WHERE ank_id='" . $this->_ank_id . "'"); else sisplet_query("INSERT INTO maza_srv_tracking (ank_id, tracking_on, activity_recognition, tracking_accuracy, interval_wanted, interval_fastes, displacement_min, ar_interval_wanted) " . "VALUES ('".$this->_ank_id."', '1', '".$activity_recognition."', '".$tracking_accuracy."', '".$interval_wanted."', '".$interval_fastes."', '".$displacement_min."', '".$ar_interval_wanted."')"); $this->ajax_sendNotification(); } /** * Calncel all activities for this survey * Turn activities off in DB and send cancelation to all subscriptors via FCM */ public function maza_cancel_tracking(){ $msg = array ( 'action' => 'cancel_tracking', 'srv_id' => $this->_ank_id ); $this->send_FCM_message($msg, 'tracking'); $this->maza_cancel_tracking_db(); header('location: index.php?anketa=' . $this->_ank_id . '&a=maza&m=maza_set_tracking'); } public function maza_cancel_tracking_db(){ sisplet_query("UPDATE maza_srv_tracking SET tracking_on='0' WHERE ank_id = '" . $this->_ank_id . "'"); } public function maza_check_expired_surveys(){ $arr = sisplet_query("SELECT ank.id, ank.expire, ank.active, mo.ank_id, mo.modul, mo.vrednost FROM srv_anketa AS ank " . "LEFT JOIN (SELECT * FROM srv_anketa_module) AS mo ON ank.id = mo.ank_id " . "WHERE ank.active = '1' AND ank.expire < CURDATE() AND mo.modul = 'maza' AND mo.vrednost = '1'", 'array'); if($arr){ foreach ($arr as $ank){ $this -> maza_off($ank['ank_id']); } } } } ?>