';
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 ' activity_recognition == 1) ? 'checked' : '') .
' onclick="toggleARInterval(this);" />';
echo $lang['srv_maza_tracking_activity_recognition'] . ' ';
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 '';
}
else
echo '' . $lang['srv_maza_entry_off'] . ' ';
//form to set or update entry
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 ' ';
}
// 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 ' ';
}
// Obrazec za izvoz vseh identifikatorjev
private function exportIdentifiersForm() {
global $lang;
echo '
';
echo '' . $lang['srv_maza_data_export'] . ' ';
echo '';
echo ' '.$lang['srv_maza_users_activated'].'
';
echo ' '.$lang['srv_maza_users_inactive'].'
';
echo ' '.$lang['srv_maza_users_deactive'].'
';
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']);
}
}
}
}
?>