<?php
$user_admin=$_COOKIE["user_admin"];
$jahrgang=$_COOKIE["jahrgang"];
$hs=$_COOKIE["ck_hs"];

if ($jahrgang == ""){ require("jahrgang.php"); exit;} //Wenn man kein Jahrgang ausgew&auml;hlt hat wird die Jahrgangsauswahlseite geladen
include("kurs/datenbankanbindung.php");     // f&uuml;gt die Datenbankanbindung ein: Sys:\php\includes\kurs\datenbankanbindung.php

// Rechteüberprü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_sozuw'");
$row = $result->fetch_array();
if ($row[0] != 1){ include("kurs/rechte.php"); exit;}
// Rechte&uuml;berpr&uuml;fung ende

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

switch($action){
default:

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>Priorit&auml;ten bearbeiten</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 align=\"left\" width=\"1%\" width=\"10\" background=\"../images/box_e1.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td align=\"left\" width=\"98%\" background=\"../images/box_top.gif\" valign=\"top\" colspan=\"2\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <font class=\"hd\">Standortzuweisung</font><br>
                    <img height=\"5\" src=\"../images/blank.gif\" width=\"1\"></td>
                    <td 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\">
                <form action="; echo $_SERVER['PHP_SELF'] . "?action=bearbeiten"; echo" method=\"POST\" name=\"Wpf_bearb\">
                    <table border=\"0\" cellpadding=\"4\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#E7E8ED\" width=\"100%\">
                        <tr>
                            <td align='left' width=\"64%\"style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
                              <b>Bezeichnung</b>
                            </td>
                            <td width=\"24%\" align=\"center\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
                              &nbsp;
                            </td>
                            <td width=\"12%\" align=\"center\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
                              <b>Auswahl</b>
                            </td>
                        </tr>";
                        $db = dbconnect();
                        $query = "SELECT agsid, a.sid, standort, vbid
                                    FROM stan_standort a, stan_ag_standort b
                                   WHERE a.sid=b.sid
                                     AND jahr='$jahrgang'
                                   ORDER BY standort";

                        $result = $db->query($query)
                               or die ("Cannot execute query");
                        while ($row = $result->fetch_array()){
                          $result1 = $db->query("SELECT count(distinct a.uid) anzahl 
                                                   FROM stan_ag_standort_wunsch a, stan_ag_standort b
                                                  WHERE a.wagsid = b.agsid
                                                    AND b.jahr = '$jahrgang'
                                                    AND a.wagsid = '$row[agsid]'
                                                    AND a.prio = '0'
                                               ");

                          $row1 = $result1->fetch_array();
                          
                          $result2 = $db->query("SELECT bezeichnung
                                                   FROM stan_vertiefungsbereich
                                                  WHERE vbid = '$row[vbid]'
                                               ");

                          $row2 = $result2->fetch_array();
                        echo "
                        <tr>
                            <td width=\"64%\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
                            <p align=\"left\"><b>$row[standort]</b> ($row2[bezeichnung])</p>
                            </td>
                            <td width=\"24%\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
                            &nbsp;
                            </td>
                            <td width=\"12%\" align=\"center\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
                            <p>
                            <input type=\"radio\" value=\"$row[agsid]\" name=\"aus_bearb\" size=\"20\">
                            </td>
                        </tr>";
                           }
                      echo "
                      </table>
                      <br>
                      <input type=\"submit\" value=\"Abschicken\" name=\"B1\" size=\"20\"></p>
                      </form>
                      </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>";

break;
case "bearbeiten":

if(isset($_POST["aus_bearb"])){
 $agsid = $_POST["aus_bearb"];
}else{
 $agsid = $_GET["agsid"];
}

setcookie("ck_agsid","");
setcookie("ck_agsid","$agsid");

$result2 = $db->query("SELECT standort
                          FROM stan_ag_standort a, stan_standort b
                         WHERE a.sid = b.sid
                           AND a.agsid='$agsid'
                      ");
$row2 = $result2->fetch_array();
$standort = $row2[standort];
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>Erfasste Termine</title>
</head>
<body onload=\"if(document.ver_erf)document.ver_erf.beg_tag.focus();return false;\">
<table border=\"0\" style=\"border-collapse: collapse\" width=\"100%\" cellpadding=\"0\"id=\"table2\">
    <tr>
        <td>
        <div align=\"center\">
        <form action="; echo $_SERVER['PHP_SELF'] . "?action=bearbeiten2 method=\"POST\" name='searchform'>
            <table cellspacing=\"0\" cellpadding=\"0\" width=\"95%\" 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\">Termin wählen für Standortzuweisung</font><br>
                    <span class='sh'>Standort: $standort</span>
                    <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=\"4\" cellspacing=\"0\" style=\"border-width:0; border-collapse: collapse\" bordercolor=\"#111111\" width=\"100%\" id=\"AutoNumber1\" height=\"18\">
                        <tr>
                          <td width=\"5%\">
                          <b>Nr</b>
                          </td>
                          <td width=\"25%\">
                          <b>Beginn</b>
                          </td>
                          <td width=\"40%\" align=\"left\">
                           <b>Vertiefungsbereich</b>
                          </td>
                          <td width=\"8%\" align=\"center\">
                           <b>Anz. zugewiesene Teilnehmer</b>
                          </td>
                          <td width=\"9%\" align=\"center\">
                           <b>Nicht zugewiesene Teilnehmer</b>
                          </td>
                          <td width=\"8%\" align=\"center\">
                           <b>Anz. Prio. 1</b>
                          </td>
                          <td width=\"10%\" align=\"center\">
                           <b>Auswahl</b>
                          </td>
                        </tr>";
                          $db = dbconnect();
                        $ln_zaehler = 1; // Z&auml;hler f&uuml;r Anzahl der Termine
                        
                        
                         $query = "SELECT  DATE_Format(datum, '%d.%m.%y') Datum_von
                                         , DATE_Format(datum, '%H:%i') Zeit_von
                                         , DATE_Format(datum, '%d') tag_von
                                         , DATE_Format(datum, '%m') mon_von
                                         , DATE_Format(datum, '%y') jahr_von
                                         , oertlichkeit
                                         , hinweis
                                         , status
                                         , c.bezeichnung
                                         , DatID
                                      FROM stan_ag_standort_termine a, stan_ag_standort b, stan_vertiefungsbereich c
                                     WHERE a.fagsid = b.agsid
                                       AND c.vbid = b.vbid
                                       AND b.jahr='$jahrgang'
                                       AND b.agsid='$agsid'
                                     ORDER BY b.vbid, a.datum ASC";

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

                        while ($row = $result->fetch_array()){
                          $res_tn = $db->query("SELECT count(*) Anz
                                                   FROM stan_ag_standort_termine a, stan_ag_standort b, stan_ag_standort_wunsch c, stan_antrag d, stud f
						  WHERE a.fagsid =b.agsid
						    AND b.agsid = c.wagsid
						    AND c.saaid=d.saaid
						    AND d.uid = f.uid
						    AND a.datid='$row[DatID]'
						    AND DATE_Format(a.datum,'%Y%m%d') >=DATE_Format(d.beginn,'%Y%m%d')
						    AND DATE_Format(a.datum,'%Y%m%d') <=DATE_Format(d.ende,'%Y%m%d')
						    AND c.prio='0'
						 AND f.durchgefallen != 'Y'
						 AND f.jahrgang = '$jahrgang'
						 AND f.stg='I'
						 AND d.status = (
						 	           SELECT MAX(t2.status)
						 	             FROM stan_antrag t2
						                     WHERE d.aendid = t2.aendid
						                       AND t2.status !='2' #hinzugefügt am 10.06.2009, weil Status 2 angezeigt wurde
						                 )
						 AND (d.zuweisung != 'EZ' AND d.zuweisung != 'E')
                                                 ORDER BY f.nachname
		                                ");
                          $row_tn = $res_tn->fetch_array();
                          

  
                          
                          $res_tn1 = $db->query("SELECT count(*) Anz
                                                   FROM stan_ag_standort_termine a, stan_ag_standort b, stan_ag_standort_wunsch c, stan_antrag d, stud f
						  WHERE a.fagsid =b.agsid
						    AND b.agsid = c.wagsid
						    AND c.saaid=d.saaid
						    AND d.uid = f.uid
						    AND a.datid='$row[DatID]'
						    AND DATE_Format(a.datum,'%Y%m%d') >=DATE_Format(d.beginn,'%Y%m%d')
						    AND DATE_Format(a.datum,'%Y%m%d') <=DATE_Format(d.ende,'%Y%m%d')
						    AND c.prio='1'
						 AND f.durchgefallen != 'Y'
						 AND f.jahrgang = '$jahrgang'
						 AND f.stg='I'
						 AND d.status = (
						 	           SELECT MAX(t2.status)
						 	             FROM stan_antrag t2
						                     WHERE d.aendid = t2.aendid
						                       AND t2.status !='2' #hinzugefügt am 10.06.2009, weil Status 2 angezeigt wurde
						                 )
						 AND (d.zuweisung != 'EZ' AND d.zuweisung != 'E')
                                                 ORDER BY f.nachname
		                                 ");
                          $row_tn1 = $res_tn1->fetch_array();
                          
                          $wochentag_von = date("l", mktime(0,0,0,$row['mon_von'],$row['tag_von'],$row['jahr_von']));
                          switch($wochentag_von)
                          {
                            case 'Sunday':
                                $wochentag_von = "So.";
                                break;
                            case 'Monday':
                                $wochentag_von = "Mo.";
                                break;
                            case 'Tuesday':
                                $wochentag_von = "Di.";
                                break;
                            case 'Wednesday':
                                $wochentag_von = "Mi.";
                                break;
                            case 'Thursday':
                                $wochentag_von = "Do.";
                                break;
                            case 'Friday':
                                $wochentag_von = "Fr.";
                                break;
                            case 'Saturday':
                                $wochentag_von = "Sa.";
                                break;
                          }
                          
                          
                          ### Berechnung nicht zugewiesene Stellen
                          
                          $result_ist = $db->query("SELECT count(*) Anz
                                                       FROM stan_ag_standort_termine a, stan_ag_standort b, stan_ag_standort_wunsch c, stan_antrag d, stud f
                                                      WHERE a.fagsid =b.agsid
                                                        AND b.agsid = c.wagsid
                                                        AND c.saaid=d.saaid
                                                        AND d.uid = f.uid
                                                        AND a.datid='$row[DatID]'
                                                        AND DATE_Format(a.datum,'%Y%m%d') >=DATE_Format(d.beginn,'%Y%m%d')
                                                        AND DATE_Format(a.datum,'%Y%m%d') <=DATE_Format(d.ende,'%Y%m%d')
                                                        AND (c.prio='0' or c.prio=(SELECT CASE 1 WHEN (SELECT count(*)
			                                                                                 FROM stan_ag_standort_wunsch e
			                                                                                WHERE e.uid=c.uid
			                                                                                  AND c.saaid=e.saaid
			                                                                                GROUP BY uid
			                                                                              )>2
			                                                                         THEN '0'
			                                                                         ELSE '1' 
			                                                                          END
			                                                   )
                                                         )
                                                      AND f.durchgefallen != 'Y'
                                                      AND f.jahrgang = '$jahrgang'
                                                      AND f.stg='I'
                                                      AND d.status = (SELECT MAX(t2.status)
                                                                        FROM stan_antrag t2
                                                                       WHERE d.aendid = t2.aendid
                                                                         AND t2.status !='2' #hinzugefügt am 10.06.2009, weil Status 2 angezeigt wurde
                                                                     )
                                                      AND (d.zuweisung != 'EZ' AND d.zuweisung != 'E')
                                                    ORDER BY f.nachname");
                         $row_ist = $result_ist->fetch_array();

                         $anzahl_unzug= $row_ist[Anz]-$row_tn[Anz];
                         
                        echo"
                        <tr>
                          <td width=\"5%\">
                          <b>$ln_zaehler</b>
                          </td>
                          <td width=\"25%\">
                          $wochentag_von $row[Datum_von] um $row[Zeit_von] Uhr
                          </td>
                          <td width=\"40%\" align=\"left\">
                           $row[bezeichnung]		                                                   
                          </td>
                          <td width=\"8%\" align=\"center\">
                           $row_tn[Anz]
                          </td>
                          <td width=\"9%\" align=\"center\">
                           $anzahl_unzug
                          </td>
                          <td width=\"8%\" align=\"center\">
                           $row_tn1[Anz]
                          </td>
                          <td width=\"10%\" align=\"center\">";
                            if($row_tn1[Anz] != 0 OR $row_tn[Anz] !=0){
                              echo"<input type=\"radio\" name=\"termin\" value='$row[DatID]'><br>";
                            }else{
                              echo "<i>keine: $row_tn1[Anz]</i>";
                            }
                            echo"
                          </td>
                        </tr>";
                        $ln_zaehler = $ln_zaehler+1;
                       }
                       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>
            <br>
            <input type=\"submit\" value=\"Markierten Termin auswählen\" name=\"B1\" size=\"20\"><br><br><a href=".$_SERVER['PHP_SELF'] ."?action=allezuw&agsid=$agsid><img title = \"Alle Prio1 zuweisen\"border=\"0\" src=\"../images/alle_zuweisen.gif\"></a>
            </form>
        </div>
        </td>
    </tr>
</table>
</body>
</html>
";


break;
case "bearbeiten2":
$datid = $_POST["termin"];
setcookie("ck_datid","");
setcookie("ck_datid","$datid");


$agsid =$_COOKIE["ck_agsid"];



$db = dbconnect();
$sel_so = $db->query("SELECT a.sid, standort, vbid
                         FROM stan_standort a, stan_ag_standort b
                        WHERE a.sid=b.sid
                          AND b.agsid='$agsid'");
$bez_so = $sel_so->fetch_array();


setcookie("ck_vbid","");
setcookie("ck_vbid","$bez_so[vbid]");


$sel_vt = $db->query("SELECT bezeichnung
                         FROM stan_vertiefungsbereich
                        WHERE vbid = '$bez_so[vbid]'
                     ");
$bez_vt = $sel_vt->fetch_array();


$sel_dat = $db->query("SELECT date_format(datum, '%d.%m.%Y um %H:%i:%s Uhr') datum
                          FROM stan_ag_standort_termine
                         WHERE datid='$datid'");
$row_dat = $sel_dat->fetch_array();

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>PU Priorit&auml;ten bearbeiten</title>
<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;}
-->
select{
	width:200px;
}
</style>
</head>
<body>
<div id=\"overDiv\" style=\"position: absolute; visibility: hidden; z-index: 1000; left: 10px; top: 0px; background-image: none;\"></div>
<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 align=\"left\" width=\"1%\" width=\"10\" background=\"../images/box_e1.gif\">
                    <img height=\"40\" src=\"../images/blank.gif\" width=\"22\"></td>
                    <td align=\"left\" width=\"98%\" background=\"../images/box_top.gif\" valign=\"top\" colspan=\"2\">
                    <img height=\"10\" src=\"../images/blank.gif\" width=\"1\"><br>
                    <font class=\"hd\">Zuordnung der Studenten zu den AG-Standorten</font><br>
                    <span class=\"sh\">Standort: $bez_so[standort] ($bez_vt[bezeichnung])<br>
                    Termin: $row_dat[datum]
                    </span>
                    <td 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\">

    <form action="; echo $_SERVER['PHP_SELF'] . "?action=bearbeiten3"; echo" method=\"POST\" name=\"PU_bearb\">
    <table border=\"0\" cellpadding=\"4\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#E7E8ED\" width=\"100%\">
    <tr>
      <td width=\"36%\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
      <b>Name</b>
      </td>
      <td width=\"16%\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
      <b>Hochschule</b>
      </td>
      <td width=\"18%\" align=right style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
      <p align=\"center\"><b>Verschieben</b></p>
      </td>
      <td width=\"10%\" align=center style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
      <b>Status</b></td>
      <td width=\"10%\" align=center style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
      <b>Prio. 1</b></td>
      <td width=\"10%\" align=center style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
      <b>Prio. 2</b></td>
    </tr>";
    $db = dbconnect();

    # Selektiere alle Studenten, die Prio=0 haben, wenn der Student keine Prio=0 hat (noch keine Zuweisung), dann Prio 1 verwenden
    # technisch gesehen, wird die summe der Prios pro Stelle gezählt. bei 2 Prios fehlt Prio 0 und es muss nach Prio 1 gesucht werden
    $result = $db->query("SELECT distinct f.uid, vorname, nachname, ag, jahrgang, d.saaid, vbid, hs
                                                   FROM stan_ag_standort_termine a, stan_ag_standort b, stan_ag_standort_wunsch c, stan_antrag d, stud f
                                                  WHERE a.fagsid =b.agsid
                                                    AND b.agsid = c.wagsid
                                                    AND c.saaid=d.saaid
                                                    AND d.uid = f.uid
                                                    AND a.datid='$datid'
                                                    AND DATE_Format(a.datum,'%Y%m%d') >=DATE_Format(d.beginn,'%Y%m%d')
                                                    AND DATE_Format(a.datum,'%Y%m%d') <=DATE_Format(d.ende,'%Y%m%d')
                                                    AND (c.prio='0' or c.prio=(SELECT CASE 1 WHEN (SELECT count(*)
						                                                     FROM stan_ag_standort_wunsch e
						                                                    WHERE e.uid=c.uid
						                                                      AND c.saaid=e.saaid
						                                                    GROUP BY uid)>2
						                                                     THEN '0'
						                                                     ELSE '1' 
						                                                      END
						                                                    )
						         )
						   AND f.durchgefallen != 'Y'
						   AND f.jahrgang = '$jahrgang'
						   AND f.stg='I'
						   AND d.status = (
						   	           SELECT MAX(t2.status)
						   	             FROM stan_antrag t2
                                                                    WHERE d.aendid = t2.aendid
                                                                      AND t2.status !='2' #hinzugefügt am 10.06.2009, weil Status 2 angezeigt wurde
		                                                   )
		                                   AND (d.zuweisung != 'EZ' AND d.zuweisung != 'E')
                                                 ORDER BY f.nachname");
						     
                                  







    while ($row = $result->fetch_array()){

/*    
    ## Abfrage selektiert alle Studenten, die Prio 1 haben. Eigentlich braucht man hier ein Full outer join (left outer join und right outer join). Den gibts
    # in MYSQL nicht, daher left outer und right outer mit union kombiniert. Das ergibt full outer join
    # Mann möchte alle datensätze von der wunschtabelle und die Datensätze von der Zuordnugnstabelle eines Termins und Standorts
    # Wenn man der Admin entgegen der Erstwahl zuweist ist der aktuelle Standort in wunsch so nicht vertreten sondern nur in zuord.
    # Aus diesem Grund benötigt man right outer join
    $result = $db->query("SELECT ifnull(e.wagsid,b.wagsid) wagsid, d.uid, vorname, nachname, ag, jahrgang, c.beginn, c.ende, c.saaid, c.vert_bereich vbid, d.hs
                             FROM stan_ag_standort_termine a, stan_antrag c, stud d, stan_ag_standort_wunsch b
                             LEFT OUTER JOIN stan_ag_standort_zuord e ON (b.saaid=e.saaid)
                            WHERE a.fagsid =b.wagsid
                              AND b.saaid = c.saaid
                              AND d.uid= b.uid
                              AND a.datid='$datid'
                              AND a.datum >=c.beginn
                              AND a.datum <=c.ende
                              AND b.prio='1'
                              AND ifnull(e.wagsid,b.wagsid)='$agsid'
                            UNION
                           SELECT ifnull(e.wagsid,b.wagsid) wagsid, d.uid, vorname, nachname, ag, jahrgang, c.beginn, c.ende, c.saaid, c.vert_bereich vbid, d.hs
                             FROM stan_ag_standort_termine a, stan_antrag c, stud d, stan_ag_standort_wunsch b
                            RIGHT OUTER JOIN stan_ag_standort_zuord e ON (b.saaid=e.saaid)
                            WHERE a.fagsid =e.wagsid
                              AND b.saaid = c.saaid
                              AND d.uid= b.uid
                              AND a.datid='$datid'
                              AND a.datum >=c.beginn
                              AND a.datum <=c.ende
                              AND b.prio='1'
                              AND ifnull(e.wagsid,b.wagsid)='$agsid'
                            ORDER BY nachname
");
*/

    echo "
      <td width=\"20%\" valign=\"middle\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
      ";
 $result_data1 = $db->query("SELECT t1.dst_id
                                FROM stan_antrag t1
                               WHERE t1.status = (
			                          SELECT MAX(t2.status)
			                            FROM stan_antrag t2
			                           WHERE t1.aendid = t2.aendid
			                             AND t2.status !='2' #hinzugefügt am 10.06.2009, weil Status 2 angezeigt wurde
			                          )
			         AND t1.saaid='$row[saaid]'
                                 ");
 $rowdata1 = $result_data1->fetch_array();
 
 $result_data2 = $db->query("SELECT distinct 1 FROM stan_dienststellen WHERE dst_id= '$rowdata1[dst_id]'");
 $rowdata2 = $result_data2->fetch_array();

 if($rowdata2[0] == '1'){
   $result_data3 = $db->query("SELECT bez
                                     , str
                                     , plz
                                     , postfach
                                     , plz_postfach
                                     , ort
                                     , bundesland
                                     , staat
                                  FROM stan_dienststellen
                                 WHERE dst_id='$rowdata1[dst_id]'");
    $rowdata3 = $result_data3->fetch_array();
    
 }else{

    $result_data3 = $db->query("SELECT bez
                                      , str
                                      , plz
                                      , postfach
                                      , plz_postfach
                                      , ort
                                      , bundesland
                                      , staat
                                   FROM stan_dienststellen_tmp
                                  WHERE dst_id='$rowdata1[dst_id]'");
     $rowdata3 = $result_data3->fetch_array();
  }
      
      
      $titel = "$row[nachname], $row[vorname]&nbsp;&nbsp;&nbsp;($row[ag]/$row[jahrgang])";
      $text = "Praxisstelle: $rowdata3[bez]";
      echo"
       <a href=\"javascript:void(0);\" onmouseover=\"return overlib('$text',ABOVE, TEXTFONTCLASS,'text_overlib', WIDTH, 350, CAPTION, '$titel',CAPTIONSIZE, '10px');\" onmouseout=\"nd();\">$titel</a>
      </td>
      <td width=\"16%\" valign=\"middle\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">";
      if($row[hs] == 'K' or $row[hs]=='k'){
        echo "Kehl";
      }else{
        echo "Ludwigsburg";
      }
      echo"
      </td>
      <td width=\"18%\" align='left' valign=\"middle\" style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
        <select size=1 name=$row[uid]|$row[saaid]>";
        $db = dbconnect();
        $result0 = $db->query("SELECT a.sid, standort, a.agsid
                                  FROM stan_ag_standort a, stan_ag_standort_wunsch b, stan_standort c
                                 WHERE a.agsid = b.wagsid
                                   AND a.sid=c.sid
                                   AND b.uid = '$row[uid]'
                                   AND b.saaid ='$row[saaid]'
                                   AND b.prio = '0'");
        $row0 = $result0->fetch_array();

                                   
        $result1 = $db->query("SELECT a.sid, standort, a.agsid
                                  FROM stan_ag_standort a, stan_ag_standort_wunsch b, stan_standort c
                                 WHERE a.agsid = b.wagsid
                                   AND a.sid=c.sid
                                   AND b.uid = '$row[uid]'
                                   AND b.saaid ='$row[saaid]'
                                   AND b.prio = '1'
                                   ");
        $row1 = $result1->fetch_array();
  
        $result2 = $db->query("SELECT a.sid, standort, a.agsid
                                  FROM stan_ag_standort a, stan_ag_standort_wunsch b, stan_standort c
                                 WHERE a.agsid = b.wagsid
                                   AND a.sid=c.sid
                                   AND b.uid = '$row[uid]'
                                   AND b.saaid ='$row[saaid]'
                                   AND b.prio = '2'");
        $row2 = $result2->fetch_array();
        
        
        
        ## Admin vorbelegen, wenn keine Prio zugewiesen, dann prio1 ausgeben
        if($row0[agsid] != ''){
          $agsid = $row0[agsid];
          $standort= $row0[standort];
        }else{
          $agsid = $row1[agsid];
          $standort= $row1[standort];
        }
  

        echo"
          <option value='$agsid'>Admin: $standort</option>
          <option value='$row1[agsid]'>Erstwahl: $row1[standort]</option>
          <option value='$row2[agsid]'>Zweitwahl: $row2[standort]</option>
          ";
          
    $result3 = $db->query("SELECT b.sid, standort,agsid
                              FROM stan_ag_standort a, stan_standort b
                             WHERE a.sid=b.sid
                               AND a.vbid='$row[vbid]'
                               AND a.jahr = '$jahrgang'");
          while ($row3 = $result3->fetch_array()){
            echo"<option value='$row3[agsid]'>$row3[standort]</option>";
          }
          echo"

        </select>
      </td>
      <td width=\"10%\" align=center style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">";
        $res_stat = $db->query("SELECT 1
	                           FROM stan_ag_standort_wunsch 
	                          WHERE saaid='$row[saaid]'
	                            AND prio='0'");
        $row_stat = $res_stat->fetch_array();
        if($row_stat[0] == '1'){
          echo "zugewiesen";
        }else{
          echo "nicht&nbsp;zugewiesen";
        }
      echo"
      </td>
      <td width=\"10%\" align=center style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
        $row1[standort]
      </td>
        <td width=\"10%\" align=center style=\"border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1px\">
        $row2[standort]
      </td>
    </tr>";
    }
  echo "
</table>
<br>
                      <input type=\"submit\" value=\"Abschicken\" name=\"B1\" size=\"20\"></p>
                      </form>
                      </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>";


break;
case "bearbeiten3":


$agsid=$_COOKIE["ck_agsid"];
$datid=$_COOKIE["ck_datid"];

$db = dbconnect();
$result8 = $db->query("SELECT distinct f.uid, vorname, nachname, ag, jahrgang, d.saaid, vbid, hs
                          FROM stan_ag_standort_termine a, stan_ag_standort b, stan_ag_standort_wunsch c, stan_antrag d, stud f
                         WHERE a.fagsid =b.agsid
                           AND b.agsid = c.wagsid
                           AND c.saaid=d.saaid
                           AND d.uid = f.uid
                           AND a.datid='$datid'
                           AND DATE_Format(a.datum,'%Y%m%d') >=DATE_Format(d.beginn,'%Y%m%d')
                           AND DATE_Format(a.datum,'%Y%m%d') <=DATE_Format(d.ende,'%Y%m%d')
                           AND (c.prio='0' or c.prio=(SELECT CASE 1 WHEN (SELECT count(*)
			                                                   FROM stan_ag_standort_wunsch e
			                                                  WHERE e.uid=c.uid
			                                                    AND c.saaid=e.saaid
			                                                  GROUP BY uid)>2
			                                                   THEN '0'
			                                                   ELSE '1' 
			                                                    END
			                                                  )
			       )
			 AND f.durchgefallen != 'Y'
			 AND f.jahrgang = '$jahrgang'
			 AND f.stg='I'
			 AND d.status = (
			 	           SELECT MAX(t2.status)
			 	             FROM stan_antrag t2
                                           WHERE d.aendid = t2.aendid
                                             AND t2.status !='2' #hinzugefügt am 10.06.2009, weil Status 2 angezeigt wurde
		                          )
		         AND (d.zuweisung != 'EZ' AND d.zuweisung != 'E')
		         AND hs='$hs' #Mail Koschek 28.01.2010, weil sie immer auch die Kehler zuweist
                        ORDER BY f.nachname");

while ($row8 = $result8->fetch_array()){
      $stud="$row8[uid]|$row8[saaid]";

        $result1 = $db->query("SELECT wagsid
                                  FROM stan_ag_standort_wunsch
                                 WHERE uid = '$row8[uid]'
                                   AND saaid ='$row8[saaid]'
                                   AND prio = '0'
                                   ");
        $row1 = $result1->fetch_array();

       # Bei Veränderung löschen und neuanlegen
       if($row1[wagsid] != $_POST[$stud]){
         $sql = $db->query ("DELETE FROM stan_ag_standort_wunsch WHERE prio='0' AND uid='$row8[uid]' AND saaid='$row8[saaid]'"); // hier entspricht die uid den namen des Dropdownfeldes in der der neue Ort des PU steht
         $sql = $db->query ("INSERT INTO stan_ag_standort_wunsch (wagsid, uid, saaid, prio) VALUES ('$_POST[$stud]', '$row8[uid]', '$row8[saaid]', '0')"); // hier entspricht die uid den namen des Dropdownfeldes in der der neue Ort des PU steht
         $sql = $db->query ("DELETE FROM stan_printqueue_antrag WHERE zuweisung='SOZ' AND saaid='$row8[saaid]'"); // Das bisherige Zuweisungschreiben rauslöschen, damit die view "stan_zuw_so" den Satz wieder findet und ein neues Zuweisungsschreiben generiert wird
       }
      
}

if($sql){
echo "
<meta http-equiv=\"refresh\" content=\"1; URL=sozuweisung.php\">
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Aktualisierung</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\">Aktualisierung</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\"><b>Standorte wurde aktualisiert!</b>
                    </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>
";
}else{
echo "
<meta http-equiv=\"refresh\" content=\"5; URL=sozuweisung.php\">
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Aktualisierung</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\">Aktualisierung</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\"><b>Es wurden keine Veränderungen festgestellt!</b>
                    </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>
";
} // Ende Wenn update fehlgeschlagne


break;
case "allezuw":
$agsid =$_COOKIE["ck_agsid"];

$db = dbconnect();
$query = "SELECT a.uid, saaid
            FROM stan_ag_standort_wunsch a, stud b
           WHERE a.uid=b.uid
             AND b.jahrgang='$jahrgang'
             AND b.hs='$hs'
             AND wagsid='$agsid'
             AND prio='1'
           ORDER BY a.uid";


$result = $db->query($query)
       or die ("Cannot execute query");
while ($row = $result->fetch_array()){
  $sql = $db->query ("DELETE FROM stan_ag_standort_wunsch WHERE saaid='$row[saaid]' AND uid='$row[uid]' AND prio='0'"); // hier entspricht die uid den namen des Dropdownfeldes in der der neue Ort des PU steht
  $sql = $db->query ("INSERT INTO stan_ag_standort_wunsch (wagsid, uid, saaid, prio) VALUES ('$agsid', '$row[uid]', '$row[saaid]', '0')"); // hier entspricht die uid den namen des Dropdownfeldes in der der neue Ort des PU steht
}

echo "
<meta http-equiv=\"refresh\" content=\"3; URL=sozuweisung.php\">
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<link rel=\"stylesheet\" href=\"../styles_pc.css\" type=\"text/css\">
<title>Aktualisierung</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\">Aktualisierung</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\"><b>Zuweisungen vorgenommen!</b>
                    </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>
";


} //Ende Action Abfrage
?>