<?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
require_once("func_get_vert_block.php");                  // Gibt vertiefungsbereich zurück, zu dem gewünschten Block

// 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:


/*
create view stan_ag_wunsch_vw
as
select distinct uid,
(select distinct wagsid from stan_ag_standort_wunsch a where prio=0 and a.uid = d.uid and a.block=d.block) prio0,
(select distinct wagsid from stan_ag_standort_wunsch a where prio=1 and a.uid = d.uid  and a.block=d.block) prio1,
(select distinct wagsid from stan_ag_standort_wunsch a where prio=2 and a.uid = d.uid  and a.block=d.block) prio2,
block
from stan_ag_standort_wunsch d
where block !=0
*/
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 align='center' width=\"8%\" 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>Anz. zugewiesene Teilnehmer</b>
                            </td>
                            <td align='center' width=\"8%\" 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>Nicht zugewiesene Teilnehmer</b>
                            </td>
                            <td align='center' width=\"8%\" 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>Anz. Prio. 1</b>
                            </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, block, abdatum, pendler, c.bezeichnung
                                    FROM stan_standort a, stan_ag_standort b, stan_vertiefungsbereich c
                                   WHERE a.sid=b.sid
                                     AND b.vbid=c.vbid
                                     AND jahr='$jahrgang'
                                   ORDER BY block ASC, standort ASC";
                        $result = $db->query($query)
                               or die ("Cannot execute query");
                        while ($row = $result->fetch_array()){



/* alt: zu langsam

                          $result0 = $db->query("SELECT count(*) Anz
                                                    FROM stan_ag_wunsch_vw a, stud b
                                                   WHERE prio0='$row[agsid]'
                                                     AND a.uid=b.uid
                                                     AND b.durchgefallen != 'Y'
                                                     AND b.jahrgang='$jahrgang'
                                               ");

                          $row0 = $result0->fetch_array();
*/

                          $result0 = $db->query("SELECT count(*) Anz
                                                    FROM stan_ag_standort_wunsch a, stud b
                                                   WHERE prio=0
                                                     AND wagsid='$row[agsid]'
                                                     AND a.uid=b.uid
                                                     AND b.durchgefallen != 'Y'
                                                     AND b.jahrgang='$jahrgang'
                                               ");

                          $row0 = $result0->fetch_array();
                          

/*
                          $result1 = $db->query("SELECT count(*) Anz
                                                    FROM stan_ag_wunsch_vw a, stud b
                                                   WHERE prio1='$row[agsid]'
                                                     AND prio0 IS NULL
                                                     AND a.uid=b.uid
                                                     AND b.durchgefallen != 'Y'
                                                     AND b.jahrgang='$jahrgang'
                                               ");

                          $row1 = $result1->fetch_array();

*/
                          $result1 = $db->query("select count(distinct d.uid) Anz
                                                    from stan_ag_standort_wunsch d, stud e
                                                   where block !=0
                                                     and d.uid=e.uid
                                                     AND e.durchgefallen != 'Y'
                                                     AND e.jahrgang='$jahrgang'
                                                     AND (select distinct wagsid from stan_ag_standort_wunsch a where prio=0 and a.uid = d.uid and a.block=d.block) is null
                                                     and (select distinct wagsid from stan_ag_standort_wunsch a where prio=1 and a.uid = d.uid  and a.block=d.block)='$row[agsid]'

                                               ");

                          $row1 = $result1->fetch_array();


/* alt: zu langsam
                          $result1a = $db->query("SELECT count(*) Anz
                                                    FROM stan_ag_wunsch_vw a, stud b
                                                   WHERE prio1='$row[agsid]'
                                                     AND a.uid=b.uid
                                                     AND b.durchgefallen != 'Y'
                                                     AND b.jahrgang='$jahrgang'
                                               ");

                          $row1a = $result1a->fetch_array();


*/

                          $result1a = $db->query("SELECT count(*) Anz
                                                    FROM stan_ag_standort_wunsch a, stud b
                                                   WHERE prio='1'
                                                     AND wagsid='$row[agsid]'
                                                     AND a.uid=b.uid
                                                     AND b.durchgefallen != 'Y'
                                                     AND b.jahrgang='$jahrgang'
                                               ");

                          $row1a = $result1a->fetch_array();
                          


                        echo "
                        <tr>
                            <td height='35' 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\">$row[standort] (Block $row[block]) ab $row[abdatum] ($row[pendler])</b> ($row[bezeichnung])</p>
                            </td>
                            <td  align='center' width=\"8%\" 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\">
                            $row0[Anz]
                            </td>
                            <td align='center' width=\"8%\" 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[Anz]
                            </td>
                            <td align='center' width=\"8%\" 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\">
                            $row1a[Anz]
                            </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, block, abdatum, pendler, vbid
                          FROM stan_ag_standort a, stan_standort b
                         WHERE a.sid = b.sid
                           AND a.agsid='$agsid'
                      ");
$row2 = $result2->fetch_array();
$standort = $row2['standort'];




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


$sel_vt = $db->query("SELECT bezeichnung
                         FROM stan_vertiefungsbereich
                        WHERE vbid = '$row2[vbid]'
                     ");
$bez_vt = $sel_vt->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:260px;
}
</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: $row2[standort] (Block $row2[block]) ab $row2[abdatum] ($row2[pendler]) - ($bez_vt[bezeichnung])<br>
                    </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 a.uid, vorname, nachname, hs, ag, jahrgang, block, prio0
                             FROM stan_ag_wunsch_vw a, stud b
                            WHERE a.uid=b.uid
                              AND b.jahrgang='$jahrgang'
                              AND b.durchgefallen !='Y'
                              AND prio0 = $agsid
                            UNION
                           SELECT a.uid, vorname, nachname, hs, ag, jahrgang, block, prio1
                             FROM stan_ag_wunsch_vw a, stud b
                            WHERE a.uid=b.uid
                            AND b.jahrgang='$jahrgang'
                            AND b.durchgefallen !='Y'
                            AND prio1 = $agsid
                            AND prio0 is NULL
                          ORDER BY nachname");

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

    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\">
      ";

      $titel = "$row[nachname], $row[vorname]&nbsp;&nbsp;&nbsp;($row[ag]/$row[jahrgang])";
      $text = "Praxisstelle: Dienststelle beim Blockmodell nicht mehr eindeutig";
      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[block]>";
        $db = dbconnect();
        $result0 = $db->query("SELECT a.sid, standort, c.block, abdatum, pendler, 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.block= '$row[block]'
                                   AND b.prio = '0'");
        $row0 = $result0->fetch_array();


                                   
        $result1 = $db->query("SELECT a.sid, standort, c.block, abdatum, pendler, 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.block= '$row[block]'
                                   AND b.prio = '1'
                                   ");
        $row1 = $result1->fetch_array();
  
        $result2 = $db->query("SELECT a.sid, standort, c.block, abdatum, pendler, 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.block= '$row[block]'
                                   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] $row0[standort] (Block $row0[block]) ab $row0[abdatum] ($row0[pendler])";
        }else{
          $agsid = $row1['agsid'];
          $standort= "$row1[standort] $row1[standort] (Block $row1[block]) ab $row1[abdatum] ($row1[pendler])";
        }
  

        echo"
          <option value='$agsid'>Admin: $standort</option>
          <option value='$row1[agsid]'>Erstwahl: $row1[standort] (Block $row1[block]) ab $row1[abdatum] ($row1[pendler])</option>
          <option value='$row2[agsid]'>Zweitwahl: $row2[standort] (Block $row2[block]) ab $row2[abdatum] ($row2[pendler])</option>
          <option value='8888'>Außerhalb BWs</option>
          <optgroup label='&nbsp;'>
          </optgroup>
          <optgroup label='Mögliche Optionen'>
          ";
          
          $vert = get_vert_block($row['block'], $row['uid']);
          $result3 = $db->query("SELECT b.sid, standort,agsid, block, abdatum, pendler, maxteil
                                    FROM stan_ag_standort a, stan_standort b
                                   WHERE a.sid=b.sid
                                     AND a.vbid='$vert'
                                     AND b.block='$row[block]'
                                     AND a.jahr = '$jahrgang'
                                   ORDER BY standort ASC");

                               
          while ($row3 = $result3->fetch_array()){
          
            $result4 = $db->query("SELECT count(*) Anz
                                      FROM stan_standort a, stan_ag_standort b, stan_ag_standort_wunsch c
                                     WHERE a.sid=b.sid
                                       AND b.agsid = c.wagsid
                                       AND c.prio=0
                                       AND b.jahr = '$jahrgang'
                                       AND wagsid='$row3[agsid]'");
            $row4 = $result4->fetch_array();
            $freiepl = $row3['maxteil']-$row4['Anz'];
            echo"<option value='$row3[agsid]'>$row3[standort] (Block $row3[block]) ab $row3[abdatum] ($row3[pendler]) - $freiepl Plätz(e) frei</option>";
          }
          echo"
          </optgroup>
        </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 block='$row[block]'
	                            AND uid='$row[uid]'
	                            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] (Block $row1[block]) ab $row1[abdatum] ($row1[pendler])
      </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] (Block $row2[block]) ab $row2[abdatum] ($row2[pendler])
      </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 a.uid, vorname, nachname, hs, ag, jahrgang, block, prio0
                             FROM stan_ag_wunsch_vw a, stud b
                            WHERE a.uid=b.uid
                              AND b.jahrgang='$jahrgang'
                              AND b.durchgefallen !='Y'
                              AND prio0 = $agsid
                            UNION
                           SELECT a.uid, vorname, nachname, hs, ag, jahrgang, block, prio1
                             FROM stan_ag_wunsch_vw a, stud b
                            WHERE a.uid=b.uid
                            AND b.jahrgang='$jahrgang'
                            AND b.durchgefallen !='Y'
                            AND prio1 = $agsid
                            AND prio0 is NULL
                          ORDER BY nachname");



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

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


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

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()){
 # Bei sozuweisung_neu.php rausgenommen
 # $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
?>