$txtchck = sprintf('user_id,Projektdaten_2,Projektdaten_4,Projektdaten_5');
$checkvar = getVar($txtchck);
$allvars = $txtchck;
    foreach (explode(",",$allvars) as $itemvar) {
           $$itemvar = $checkvar[$itemvar] ;
    }
$i_userid = reset(getVar("user_id"));
$i_projectid = $_SESSION["id_project"];
if ($i_userid==0) {
    $i_projectid = 309; //Test MTU 2.HJ 2017
    $i_userid = 57; // 135 test, 57 Boden
    $Projektdaten_4 = 820919;
    }

if ($user_id == 0) {$visitsTable = "_Capi_interviewer_visits"; } else {$visitsTable = "_capi_interviewer_visits"; }
if ($user_id == 0) {$ProjectsTable = "_Projects"; } else {$ProjectsTable = "_projects"; }
if ($user_id == 0) {$AddressTables = "_CapiTables"; } else {$AddressTables = "_capitables"; }

$i_tableIds = getVarFromTable($ProjectsTable, "proj_name,naziv_proj,id_capiTable", "WHERE id = ".$i_projectid);
$i_AddressTableInfo = getVarFromTable($AddressTables, "name", "WHERE id = ".$i_tableIds[0]["id_capiTable"]);

$s_projname = $_SESSION["proj_name"];
$s_structure = $s_projname."_structure";
$s_sample = $s_projname."___sample";
$s_address = "capi___".$i_AddressTableInfo[0]["name"];
/*
echo "<pre>";
//var_dump($_SESSION);
echo '$i_userid:'.$i_userid."<br>";
echo '$i_projectid:'.$i_projectid."<br>";
echo '$s_projname:'.$s_projname."<br>";
echo '$user_id:'.$user_id."<br>";
echo '$Projektdaten_2:'.$Projektdaten_2."<br>";
echo '$Projektdaten_4:'.$Projektdaten_4."-".$Projektdaten_5."<br>";
echo "</pre>";
*/
$i_address = getVarFromTable($s_address, "id,municipality,id_settlement,name",
    "WHERE id_municipality= ".$Projektdaten_2.
    " and municipality= ".$Projektdaten_4);

$questtxt = "<table  align='left' style='text-align: center;font-size:70%;'>
    <tr><th>Listennummer: ".$Projektdaten_4."</th></tr>";
$questtxt .="
    <tr><th style='padding-left: 15px;padding-right: 15px;'>LfdNr.</th>
    <th style='padding-left: 15px;padding-right: 15px;'>Name.</th>
    <th style='padding-left: 15px;padding-right: 15px;'>Anzahl Kontakte</th>
    <th style='padding-left: 15px;padding-right: 15px;'>letzter Kontakt</th></tr>";

foreach ($i_address as $adresse) {
    //var_dump($kontakt);
    if ($user_id == 0) {$s_sample = "p27293105___sample"; } // TEST
    $i_sample = getVarFromTable($s_sample, "id,user_id",
        "WHERE id_sample= ".$adresse["id"]);
    $i_contacts = getVarFromTable($visitsTable, "sample_point,cas_obiska,status_obiska,id_structure,user_id",
    "WHERE user_id =".$i_userid.
    " and id_project = ".$i_projectid.
     " and sample_point = ".$i_sample[0]["id"]);
if ($i_sample[0]["user_id"] == $user_id || $user_id == 0 ) {
$questtxt .= "<tr><td>".$adresse["id_settlement"] ."</td><td style='text-align: left;'>"
          . $adresse["name"]."</td>";
  
    if ($i_contacts) {
       $i_count_con = 0;
        foreach ($i_contacts as $Kontakt) {
            $i_count_con ++;
            switch($Kontakt[status_obiska]) {
                case 1:
                    $kontxt = "Interview abgeschlossen";
                    break;
                case 9:
                    $kontxt = "Interview unterbrochen";
                    break;
                case 21:
                    $kontxt = "Niemand erreicht/angetroffen";
                    break;
                case 22:
                    $kontxt = "Rückruf";
                    break;
                case 8:
                    $kontxt = "Terminvereinbarung";
                    break;
                case 23:
                    $kontxt = "Verweigerung bei 1.Kontakt";
                    break;
                case 24:
                    $kontxt = "Verweigerung";
                    break;
                case 31:
                    $kontxt = "kritische Verweigerung";
                    break;
                case 32:
                    $kontxt = "keine geeignete ZP";
                    break;
                case 33:
                    $kontxt = "Adresse unauffindbar";
                    break;
                case 34:
                    $kontxt = "ungültige Adresse";
                    break;
                case 35:
                    $kontxt = "Adresse unbearbeitet abgelegt";
                    break;
                default:                    
                    $kontxt = "unbekannt";
                }
         }
$questtxt .= "<td>".$i_count_con."</td><td>".$kontxt."</td>";        
    }
$questtxt .= "</tr>";
    }

 }

$questtxt .= "</table>";

saveVar("questtext_1='".$txtQuest."'");


