<?php
if ($_COOKIE["uid1"] == ""){ include("kurs/anmeldefehler_subdir.php"); exit;} //Wenn man nicht angemeldet ist soll man nichts ausw&auml;hlen k&ouml;nnen
$uid1=$_COOKIE["uid1"];
include("kurs/datenbankanbindung.php");     // f&uuml;gt die Datenbankanbindung ein: Sys:\php\includes\kurs\datenbankanbindung.php
$db = dbconnect();
include("kurs/sta/stan_useronline_write.php");     // F&uuml;r Statistik, wieviele User online sind



// &Uuml;Berpr&uuml;fung, ob die Anmeldefrist &uuml;berschritten ist
$heute  = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));  // heutiges Datum
$heute_format = date("YmdHis", $heute);  // ISO-8601 Format wie in der Datenbank: Formatierung f&uuml;r Vergleiche erforderlich

$res = $db->query("SELECT Jahrgang FROM stud WHERE uid=\"$uid1\"");
$stud_jahr = $res->fetch_array();

$anm_schluss = $db->query("SELECT DATE_Format(anmeldeschluss, '%d.%m.%Y um %H:%i:%s Uhr')AS endeform, DATE_Format(anmeldeschluss, '%Y%m%d%H%i%s')AS enddatum 
                              FROM stan_frist
                             WHERE jahrgang=\"$stud_jahr[Jahrgang]\"");
$row_schluss = $anm_schluss->fetch_array(); // Herausfinden des Beginn- Schlussdatums

$a = strtotime ($heute_format); //Umrechnung des Fristgrenztages in eine Zahl
$b = strtotime ($row_schluss['enddatum']); // Umrechnung des Enddatums in eine Zahl

# Wenn Erstanträge abgesendet wurden, dürfen keine Stellen mehr vorgemerkt werden
$res2 = $db->query("SELECT distinct status 
                      FROM stan_antrag
                     WHERE uid='$uid1'
                     LIMIT 1");
$stud_status = $res2->fetch_array();

if($stud_status['status'] != '0'){

 if ($a <= $b){
 echo "
 <html>
 <head>
 <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
 <link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
 <title>Statusauskunft nicht möglich</title>
 </head>
 <body>
 <table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" height=\"100%\" id=\"table1\">
    <tr>
        <td>
        <div align=\"center\">
            <table cellspacing=\"0\" cellpadding=\"0\" width=\"550\" border=\"0\">
                <tr>
                    <td width=\"10\" background=\"../images/box_e1.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"3%\" background=\"../images/box_top.gif\" valign=\"top\"></td>
                    <td width=\"95%\" background=\"../images/box_top.gif\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <span class=\"sh\">Statusauskunft nicht möglich</span><br>
                    <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e2.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td width=\"1%\" background=\"../images/box_l.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td colspan=\"2\"><br>
                    <p align=\"center\">&Auml;nderungsantr&auml;ge können erst<br><b>nach Anmeldeschluss oder gestelltem Erstantrag</b><br>abgegeben werden!<br>
                                       <br>Sie können bis zum $row_schluss[endeform]<br>Anträge <a href=\"antrag.php\">abgeben</a>.</a></b>
                    </p>
                    <p align=\"center\"><a href=\"hauptframe.php\">
                    <img border=\"0\" src=\"../images/zurueck.gif\" width=\"77\" height=\"22\"></a><br>
                    </p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p></p>
                    <p><br>
                    </p>
                    </td>
                    <td width=\"1%\" background=\"../images/box_r.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"1%\" background=\"../images/box_e3.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\" colspan=\"2\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"> </td>
                </tr>
            </table>
        </div>
        </td>
    </tr>
 </table>
 </body>
 </html>";
 exit;
 } // Ende &Uuml;berpr&uuml;fung der Zeit&uuml;berschreitung
}
#################






echo "
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Status der Änderungsanträge</title>
</head>
<body>
<table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\" height=\"100%\" id=\"table1\">
    <tr>
        <td>
        <div align=\"center\">
            <table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" border=\"0\">
                <tr>
                    <td width=\"10\" background=\"../images/box_e1.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_top.gif\" valign=\"top\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <font class=\"hd\">Status der Änderungsanträge</font><br>
                    <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></font></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e2.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td width=\"1%\" background=\"../images/box_l.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td>
                        <table  border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-width:0; border-collapse: collapse\" bordercolor=\"#111111\" width=\"100%\" id=\"AutoNumber1\">
                            <tr>
                                <td align=\"left\" style=\"border-style: none; border-width: medium\">
                                  <b>Bezeichnung</b>
                                </td>
                                <td align=\"left\" style=\"border-style: none; border-width: medium\">
                                  <b>Vertiefungsbereich</b>
                                </td>
                                <td align=\"left\" style=\"border-style: none; border-width: medium\">
                                  <b>Zeitraum</b>
                                </td>
                                <td align=\"left\" style=\"border-style: none; border-width: medium\">
                                  <b>Status</b>
                                </td>
                            </tr>
                            ";
                            
				$result_tmp1 = $db->query("DELETE FROM stan_tmp_check_data WHERE uid='$uid1'");


				$result_tmp1 = $db->query("INSERT INTO stan_tmp_check_data
			                                         SELECT t1.saaid
			                                              , t1.vert_bereich
								      , t1.uid
								      , t1.beginn
								      , t1.ende
								      , t1.dst_id
								   FROM stan_antrag t1
								  WHERE t1.status = (
								                     SELECT MAX(t2.status)
								                       FROM stan_antrag t2
								                      WHERE t1.aendid = t2.aendid
								                    )
                                                                    AND t1.uid = '$uid1'
                                                                    AND (t1.zuweisung is NULL OR t1.zuweisung='Z')
                                                          ");


				#$result = $db->query("SELECT max(saaid) max_id FROM stan_tmp_check_data WHERE uid='$uid1'");
				#$row4 = $result->fetch_array();
				#$saaid_neu = $row4[max_id] + 1; // Damit alle Einträge eine ID haben
				#$result_tmp1 = $db->query("INSERT INTO stan_tmp_check_data SELECT $saaid_neu, NULL, '$uid1', DATE_SUB(antrag_von, INTERVAL 1 DAY), DATE_SUB(antrag_von, INTERVAL 1 DAY), 'Antragszeitraum Beginn' FROM stan_frist WHERE jahrgang ='$row[Jahrgang]'");

				#$result = $db->query("SELECT max(saaid) max_id FROM stan_tmp_check_data WHERE uid='$uid1'");
				#$row4 = $result->fetch_array();
				#$saaid_neu = $row4[max_id] + 1; // Damit alle Einträge eine ID haben
				#$result_tmp1 = $db->query("INSERT INTO stan_tmp_check_data SELECT $saaid_neu, NULL, '$uid1', antrag_bis, antrag_bis, 'Antragszeitraum Ende' FROM stan_frist WHERE jahrgang ='$row[Jahrgang]'");

                                $query = "SELECT ma.uid, DATE_FORMAT(beginn, '%d.%m.%Y') beginn, DATE_FORMAT(ende, '%d.%m.%Y') ende, bezeichnung, COALESCE( luecke, 'OK' ) AS Luecke, ma.saaid, ma.vbid
                                            FROM stan_tmp_check_data ma
                                            LEFT JOIN ( SELECT saaid, 'Lücke folgt' AS luecke
                                                          FROM stan_tmp_check_data a
                                                         WHERE a.ende != (SELECT MAX( ende ) FROM stan_tmp_check_data x WHERE x.uid = a.uid)
                                                           AND NOT EXISTS ( SELECT saaid FROM stan_tmp_check_data b
                                                                             WHERE a.uid = b.uid
                                                                               AND b.beginn = DATE_ADD( a.ende, INTERVAL 1 DAY )
                                                                          )
                                                            OR ( a.beginn IN (select beginn FROM stan_tmp_check_data d WHERE d.saaid != a.saaid)
                                                                                 AND a.ende  IN (select ende FROM stan_tmp_check_data e WHERE e.saaid != a.saaid AND e.uid = '$uid1')
                                                               )
                                                      ) L
                                              ON L.saaid = ma.saaid
                                           WHERE ma.uid = '$uid1'
                                           ORDER BY ma.uid, ma.beginn";



				$result = $db->query($query)
				          or die ("Cannot execute query");
                                while ($row2 = $result->fetch_array()){
                                
                                $res4a = $db->query("SELECT dst_id, status, zuweisung FROM stan_antrag WHERE saaid='$row2[saaid]'");
                                $row4a = $res4a->fetch_array();
                                
                                $res4b = $db->query("SELECT bezeichnung FROM stan_vertiefungsbereich WHERE vbid='$row2[vbid]'");
                                $row4b = $res4b->fetch_array();
                                
                                $res4 = $db->query("SELECT bez, ort FROM stan_dienststellen WHERE dst_id='$row4a[dst_id]'");
                                $row4 = $res4->fetch_array();
                                if($row4['bez'] != ''){
                                  $dst_bez = $row4['bez']." ".$row4['name'];
                                  $ort = $row4['ort'];
                                }
                                if($row4['bez'] == ''){
                                  $res5 = $db->query("SELECT bez, ort FROM stan_dienststellen_tmp WHERE dst_id='$row4a[dst_id]'");
                                  $row5 = $res5->fetch_array();
                                  $dst_bez = $row5['bez']." ".$row5['name'];
                                  $ort = $row5['ort'];
                                }
                                if($row4['bez'] == '' AND $row5['bez'] == ''){
                                  $dst_bez = '<i>Zeitraum vorgemerkt</i>';
                                  $ort = "";
                                }
                                
                                echo"
                                <tr>
                                <td align=\"left\" style=\"border-style: none; border-width: medium\">
                                  $dst_bez
                                </td>
                                <td align=\"left\" style=\"border-style: none; border-width: medium\">
                                  $row4b[bezeichnung]
                                </td>
                                <td align=\"left\" style=\"border-style: none; border-width: medium\">
                                  $row2[beginn] - $row2[ende]
                                </td>
                                <td align=\"left\" style=\"border-style: none; border-width: medium\">";
                                                                 
                                  if($row4a['status'] == '0' AND $row4a['zuweisung'] == NULL){
                                    $status ='Erstantrag unbearbeitet';
                                  }
                                  if($row4a['status'] == '2' AND $row4a['zuweisung'] == NULL){
                                    $status ='Neu / nicht abgeschickt';
                                  }
                                  if($row4a['status'] == '1' AND $row4a['zuweisung'] == NULL){
                                    $status ='Änderungsantrag neu / abgeschickt';
                                  }
                                  
                                  if($row4a['status'] == '0' AND $row4a['zuweisung'] == 'Z'){
                                    $status ='Erstantrag genehmigt';
                                  }

                                  if($row4a['status'] == '1' AND $row4a['zuweisung'] == 'Z'){
                                    $status ='Änderungsantrag genehmigt';
                                  }
                                 
                                echo "<nobr>$status</nobr>";

                                
                                echo"
                                </td>
                                </tr>
                                ";				
                                }
                            echo "
                            

                        </table>
                    </td>
                    <td width=\"1%\" background=\"../images/box_r.gif\">
                    <img height=\"1\" src=\"../images/blank.gif\" width=\"22\"></td>
                </tr>
                <tr>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"1%\" background=\"../images/box_e3.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td nowrap=\"nowrap\" align=\"left\" width=\"98%\" background=\"../images/box_bottom.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"8\"></td>
                    <td nowrap=\"nowrap\" align=\"right\" width=\"1%\" background=\"../images/box_e4.gif\">
                    <img height=\"16\" src=\"../images/blank.gif\" width=\"22\"> </td>
                </tr>
            </table>
        </div>
        </td>
    </tr>
</table>
</body>
</html>";
$result_tmp1 = $db->query("DELETE FROM stan_tmp_check_data WHERE uid='$uid1'");
?>
