<?php
$user_admin=$_COOKIE["user_admin"];
$jahrgang=$_COOKIE["jahrgang"];
$hs=$_COOKIE["ck_hs"];
include_once("kurs/datenbankanbindung.php");     // f&uuml;gt die Datenbankanbindung ein: Sys:\php\includes\kurs\datenbankanbindung.php
require_once("FCKeditor/fckeditor.php") ;
require_once("../stud/func_get_block.php");		// Prüft in welchem Block die aktuelle Stelle sitzt
require_once("func_get_vert_block.php");                  // Gibt vertiefungsbereich zurück, zu dem gewünschten Block


// Rechte&uuml;berpr&uuml;fung
$db = dbconnect();
if ($user_admin == ""){ require("index.php"); exit;} //Wenn man nicht angemeldet ist, darf man nicht auf die Seite
$result = $db->query("SELECT 1 FROM stan_admin_rechte, stan_admin_rechte_zuord , stan_admin where stan_admin_rechte.stan_admin_rolle = stan_admin_rechte_zuord.stan_admin_rolle AND stan_admin_rechte_zuord.said = stan_admin.said AND stan_admin.user = '$user_admin' AND stan_admin_rechte_zuord.stan_admin_rolle = 'a_vber'");
$row = $result->fetch_array();
if ($row[0] != 1){ include("kurs/rechte.php"); exit;}
// Rechte&uuml;berpr&uuml;fung ende

if(isset($_POST['filter'])){
  $filter = $_POST['filter'];
}else{
  $filter = "alle";
}


$db = dbconnect();

if (isset($_GET['action']))
   $action = $_GET['action'];
else
   $action = "";

switch($action){
default:

echo "
<html>
<head>
<style type='text/css'>
.nextprev:link, .nextprev:visited {
  background-color: #D6DFF7;
  color: black;
  font-weight:bold;
  text-decoration: none;
  width: 99%;
  display: block;
  font-size: 8pt;
  text-align: center;
  padding: 1px;
}
.nextprev:active, .nextprev:hover {
  background-color: #6487DC;
  color: #FFFFFF;
  font-weight:bold;
  text-decoration: none;
  width: 99%;
  display: block;
  font-size: 8pt;
  text-align: center;
  padding: 1px;
}
</style>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<script type=\"text/javascript\" src=\"./overlib/overlibmws.js\"><!-- overLIB (c) Erik Bosrup --></script>
<script type=\"text/javascript\">
<!--
OLregisterPlugins('crossframe','iframe','hide','shadow');
overlib=parent.overlib;
nd=parent.nd;
if (typeof window.createPopup!='undefined') {
var oPopup = window.createPopup();
var oPopupBody = oPopup.document.body;
oPopupBody.style.backgroundColor = \"#ccccff\";
oPopupBody.style.border = \"solid blue 1px\";
}

// -->
</script>
<style type=\"text/css\">
.text_overlib {font-family:Verdana,Arial,sans-serif; font-size:10px;}
.r{text-decoration: none;font-weight: bold; color: black;}
.text_overlib {font-family:Verdana,Arial,sans-serif; font-size:10px;}
</style>


<title>Jahrgangsstatus</title>
	<script type=\"text/javascript\" src=\"livesearch/ajax.js\"></script>
	<script type=\"text/javascript\" src=\"livesearch/ajax-dynamic-list.js\"></script>
<style type=\"text/css\">
select{
	width:250px;
}


a{
		color:red;
		font-weight		:	bold;
}
</style>
</head>
<body>
<div id=\"overDiv\" style=\"position: absolute; visibility: hidden; z-index: 1000; left: 10px; top: 0px; background-image: none;\"></div>

<!--
                    <form action="; echo $_SERVER['PHP_SELF']; echo" method=\"POST\" name=\"filter\">
                    Filter: <select name='filter' onchange='this.form.submit()'>
                      <option value='alle'"; if($filter =='alle'){echo " selected"; }echo ">Alle</option>
                      <option value='filter_wird'"; if($filter =='filter_wird'){echo " selected"; }echo ">Dienststellen mit Schlüsselwort: \"wird\", \"offen\", \"Nachtrag\"</option>
                    </select>
                    </form>
                    <br>
                    <br>
-->
    <br>
    <table valign='middle' style='border-top: 2px solid #6487DC; border-bottom: 2px solid #6487DC;' width='100%' align='center' border='0' cellpadding='0' cellspacing='0'>
      <tbody><tr>
          <td valign='middle' width='100%' height='30px' class='nextprev'>
          <a target='_top' href='expo_jahrgangsstatus.php' class='nextprev'><font size='3'>&nbsp;</font>CSV-EXPORT (Excel)<font size='3'>&nbsp;</font></a>
          </td>

      </tr>
    </tbody>
    </table>
    <br>
    <br>
			  <table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse; border-width: 0\" bordercolor=\"#111111\" width=\"100%\" id=\"AutoNumber1\">
			    <tr>
			      <td height=\"17\">
			        <b>Name</b>
			      </td>
			      <td height=\"17\">
			        <b>Matrikelnr.</b>
			      </td>
			      <td height=\"17\">
			       <b>AG</b>
			      </td>
			      <td height=\"17\">
			       <b>Vertiefungsbereich</b>
			      </td>
			      <td height=\"17\">
			       <b>Block</b>
			      </td>
			      <td height=\"17\">
			       <b>Datum</b>
			      </td>
			    </tr>
			  
			  ";
			  

                          
                          if($filter == 'filter_wird'){
                          $query = "SELECT t2.vorname
                                         ,t2.nachname
                                         ,t2.hs
                                         ,t2.ag
                                         ,t2.mtknr
                                         ,t1.saaid
                                         , t1.vert_bereich
                                         , t1.uid
                                         , date_format(t1.beginn, '%d.%m.%Y') beginn
                                         , date_format(t1.ende, '%d.%m.%Y') ende
                                         ,t1.beginn beginn_order
                                         , t1.dst_id
                                         , t1.zuweisung
                                      FROM stan_antrag t1, stud t2, stan_dienststellen_tmp t3
                                     WHERE t1.status = ( SELECT MAX(t2.status)
                                                           FROM stan_antrag t2
                                                          WHERE t1.aendid = t2.aendid
                                                            AND t2.status !='2'
                                                       )
                                       AND (t1.zuweisung is NULL OR t1.zuweisung='Z')
                                       AND t1.uid=t2.uid
                                       AND t1.dst_id=t3.dst_id
                                       AND t2.jahrgang = '$jahrgang'
                                       AND t2.hs ='$hs'
                                       AND t2.durchgefallen != 'Y'
                                       AND (upper(t3.bez) LIKE '%WIRD %'
                                           OR upper(t3.bez) LIKE '%OFFEN %'
                                           OR upper(t3.bez) LIKE '%NACHTRAG %'
                                           OR upper(t3.bez) LIKE ''
                                           )
                                       ORDER BY nachname, vorname, beginn_order";

                          }else{
                          $query = "SELECT t2.vorname
                                         ,t2.nachname
                                         ,t2.hs
                                         ,t2.ag
                                         ,t2.mtknr
                                         ,t1.saaid
                                         , t1.vert_bereich
                                         , t1.uid
                                         , date_format(t1.beginn, '%d.%m.%Y') beginn
                                         , date_format(t1.ende, '%d.%m.%Y') ende
                                         ,t1.beginn beginn_order
                                         , t1.dst_id
                                         , t1.zuweisung
                                      FROM stan_antrag t1, stud t2
                                     WHERE t1.status = ( SELECT MAX(t2.status)
                                                           FROM stan_antrag t2
                                                          WHERE t1.aendid = t2.aendid
                                                            AND t2.status !='2'
                                                       )
                                       AND (t1.zuweisung is NULL OR t1.zuweisung='Z')
                                       AND t1.uid=t2.uid
                                       AND t2.jahrgang = '$jahrgang'
                                       AND t2.hs ='$hs'
                                       AND t2.durchgefallen != 'Y'
                                       ORDER BY nachname, vorname, beginn_order";

                          }

			  $result = $db->query($query)
			          or die ("Cannot execute query1");

                          while ($row = $result->fetch_array()){
                          
                                    $result5 = $db->query("SELECT concat(bezeichnung, ' ',name) name, ort
                                                              FROM stan_dienststellen
                                                             WHERE dst_id ='$row[dst_id]'");
                                    $row5 = $result5->fetch_array();

                                    $result6 = $db->query("SELECT bez, ort
                                                              FROM stan_dienststellen_tmp
                                                             WHERE dst_id ='$row[dst_id]'");
                                    $row6 = $result6->fetch_array();

                                    if($row5['name'] != '' and $row6['bez'] == ''){
                                      $bezeichnung = $row5['name'];
                                      $ort = $row5['ort'];
                                    }


                                    if($row5['name'] == '' and $row6['bez'] != ''){
                                      $bezeichnung = $row6['bez'];
                                      $ort = $row6['ort'];
                                    }

                                    if($row5['name'] == '' and $row6['bez'] == ''){
                                      $bezeichnung = '<i>Zeitraum vorgemerkt</i>';
                                      $ort = '';
                                    }
                                    
				       if($ort!=''){
				         $bez_neu= "$bezeichnung in $ort";
				       }else{
				         $bez_neu= "$bezeichnung";
				       }
				       
				       
                                       $block = get_block($row['saaid'], $jahrgang);
                                       
                                       $result_zuw0 = $db->query("SELECT 1,wagsid
                                                                    FROM stan_ag_standort_wunsch
                                                                   WHERE block ='$block'
                                                                     AND uid= '$row[uid]'
                                                                     AND prio='0'");
                                       $row_zuw0 = $result_zuw0->fetch_array();
                                       
                                       $result_zuw1 = $db->query("SELECT 1,wagsid
                                                                    FROM stan_ag_standort_wunsch
                                                                   WHERE block ='$block'
                                                                     AND uid= '$row[uid]'
                                                                     AND prio='1'");
                                                                     
                                       $row_zuw1 = $result_zuw1->fetch_array();
                                       
                                       $result_zuw2 = $db->query("SELECT 1,wagsid
                                                                    FROM stan_ag_standort_wunsch
                                                                   WHERE block ='$block'
                                                                     AND uid= '$row[uid]'
                                                                     AND prio='2'");
                                       $row_zuw2 = $result_zuw2->fetch_array();
                                       

                                       
                                       if($row_zuw0[0] == '' AND $row_zuw1[0] == ''){
                                        $status='Nicht erfasst';
#                                        $zuw_sperre='X';
#                                        $zuw_alle_sperre='X';
                                       }
                                       
                                       if($row_zuw0[0] == '' AND $row_zuw1[0] == '1'){


                                        $result_standort1 = $db->query("SELECT agsid, a.sid, standort, block, abdatum, pendler, c.bezeichnung
                                                                           FROM stan_ag_standort a, stan_standort b, stan_vertiefungsbereich c
                                                                          WHERE a.sid=b.sid
                                                                            AND a.vbid = c.vbid
                                                                            AND a.agsid ='$row_zuw1[wagsid]'");

                                        $row_s1 = $result_standort1->fetch_array();
                                        
                                        
                                        $result_standort2 = $db->query("SELECT agsid, a.sid, standort, block, abdatum, pendler, c.bezeichnung
                                                                           FROM stan_ag_standort a, stan_standort b, stan_vertiefungsbereich c
                                                                          WHERE a.sid=b.sid
                                                                            AND a.vbid = c.vbid
                                                                            AND a.agsid ='$row_zuw2[wagsid]'");
                                        $row_s2 = $result_standort2->fetch_array();
                                        
                                        if($row_zuw1[wagsid] == '8888'){
                                         $standort1="Stelle außerhalb BWs";
                                        }else{
                                         $standort1="$row_s1[standort] (Block $row_s1[block]) ab $row_s1[abdatum] ($row_s1[pendler]) ($row_s1[bezeichnung])";
                                        }
                                        
                                        if($row_zuw2[wagsid] == '9999'){
                                         $standort2="Stelle außerhalb BWs";
                                        }else{
                                         $standort2="$row_s2[standort] (Block $row_s2[block]) ab $row_s2[abdatum] ($row_s2[pendler]) ($row_s2[bezeichnung])";
                                        }
                                        if($row_zuw2[wagsid] == '9999' OR $row_zuw2[wagsid] == '8888'){
  	  	                          $titel = "Nicht zugewiesen:";
  	  	                          $text = "Priorität 1: $standort1<br>Priorität 2: $standort2";
  	  	                        }else{
  	  	                          $titel = "<font color='red'><b>Nicht zugewiesen:</b></font>";
  	  	                          $text = "<a href='sozuweisung_neu.php?action=bearbeiten&agsid=$row_s1[agsid]'><b>Priorität 1:</b></a> $standort1<br><a href='sozuweisung_neu.php?action=bearbeiten&agsid=$row_s2[agsid]'><b>Priorität 2:</a></b> $standort2";
  	  	                        }
		     			

                                        $status="$titel<br>$text";

#Wenn Student nicht dem Standort zugewiesen ist, dann darf man nicht zuweisen
# Entfällt für Jahrgang 2008, da in den Zuweisungsschreiben der AG-Standort nicht mitausgegeben wird
#                                        $zuw_sperre='X';
#                                        $zuw_alle_sperre='X';
                                       }
                                       
                                       if($row_zuw0[0] == '1' AND $row_zuw1[0] == '1'){

                                        $result_standort0 = $db->query("SELECT agsid, a.sid, standort, block, abdatum, pendler, c.bezeichnung
                                                                           FROM stan_ag_standort a, stan_standort b, stan_vertiefungsbereich c
                                                                          WHERE a.sid=b.sid
                                                                            AND a.vbid = c.vbid
                                                                            AND a.agsid ='$row_zuw0[wagsid]'");



                                        $row_s0 = $result_standort0->fetch_array();
		     			$status="$row_s0[standort] (Block $row_s0[block]) ab $row_s0[abdatum] ($row_s0[pendler]) ($row_s0[bezeichnung])";
                                        
                                       }
                                       
                                    $result3art = $db->query("SELECT artid, bezeichnung
                                                              FROM stan_antrag a, stan_stellenart b
                                                             WHERE a.art= b.artid
                                                               AND saaid='$row[saaid]'");
                                    $row3art = $result3art->fetch_array();
                                    # 6 = Ausland
                                    # 7 = Anderes Bundesland
                                    
                                    
                                    # Wenn Stelle außerhalb BW bzw Ausland, darf kein Standort zugeweisen sein
                                    if (($row3art['artid'] == 6 OR $row3art['artid'] == 7) and ($row_zuw1['wagsid'] != '8888' or $row_zuw2['wagsid'] != '9999') and $status != 'Nicht erfasst'){
                                      $hinweis2 = "<font color='red'><b>Stelle darf keine AG-Zuweisung haben</b>";
                                    }

                                    $resultvert = $db->query("SELECT bezeichnung
		                                                 FROM stan_vertiefungsbereich
		                                                WHERE vbid ='$row[vert_bereich]'");
		                    $rowvert = $resultvert->fetch_array();

                                       


			    echo"
			    <tr>
			      <td valign='top'>
			      $row[nachname], $row[vorname]
			      </td>
			      <td valign='top'>
			      $row[mtknr]
			      </td>
			      <td valign='top' align='center'>
			         $row[ag]
			      </td>
			      <td valign='top'>
			       $rowvert[bezeichnung]
			      </td>
			      <td valign='top' align='center'>
			       $block
			      </td>
			      <td valign='top'>
			       $row[beginn]&nbsp;- $row[ende]
			      </td>
			    </tr>";
			    $hinweis="";
			    $hinweis2="";
                          }
			  echo"
			  </table>
</body>
</html>";

} // Ende Action script
?>