<?php
#include_once("kurs/datenbankanbindung.php");     // f&uuml;gt die Datenbankanbindung ein: Sys:\php\includes\kurs\datenbankanbindung.php
function get_block($saaid, $jahrgang){
  global $db;
  $db = dbconnect();
  $result_gb1 = $db->query("SELECT antrag_von, date_format(antrag_von, '%d') tag, date_format(antrag_von, '%m') monat, date_format(antrag_von, '%Y') jahr
                               FROM stan_frist
                              WHERE jahrgang='$jahrgang'");
  $row_gb1 = $result_gb1->fetch_array();

  $result_gb2 = $db->query("SELECT vert_bereich, uid
                               FROM stan_antrag
                              WHERE saaid='$saaid'");
  $row_gb2 = $result_gb2->fetch_array();

    
  $result_gb3 = $db->query("SELECT min(beginn) beginn, max(ende) ende
                               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.vert_bereich='$row_gb2[vert_bereich]'
                                AND t1.uid='$row_gb2[uid]'
                                AND (t1.zuweisung is NULL OR t1.zuweisung='Z')
                                AND t1.saaid = $saaid # hinzugefügt am 25.07.2019, da Agnes Schach sonst kein Block 2 bekommen hätte
                                
                                ");
                                

  $row_gb3 = $result_gb3->fetch_array();


  
  if($row_gb3['beginn'] == $row_gb1['antrag_von']){
    $block = 1;
  }
  
  $block2_beg_min = date("Y-m-d", mktime(0, 0, 0, $row_gb1['monat']+3, $row_gb1['tag'], $row_gb1['jahr']));
  #$block2_beg_max = date("Y-m-d", mktime(0, 0, 0, $row_gb1[monat]+5, $row_gb1[tag], $row_gb1[jahr]));
  
  if($row_gb3['beginn'] >= $block2_beg_min){
    $block = 2;
  }
  
  $block3_beg_min = date("Y-m-d", mktime(0, 0, 0, $row_gb1['monat']+6, $row_gb1['tag'], $row_gb1['jahr']));
  #$block3_beg_max = date("Y-m-d", mktime(0, 0, 0, $row_gb1[monat]+8, $row_gb1[tag], $row_gb1[jahr]));
  echo @$block3_beg_max;
  if($row_gb3['beginn'] >= $block3_beg_min){
    $block = 3;
  }
  
  
  $block4_beg_min = date("Y-m-d", mktime(0, 0, 0, $row_gb1['monat']+9, $row_gb1['tag'], $row_gb1['jahr']));
  #$block4_beg_max = date("Y-m-d", mktime(0, 0, 0, $row_gb1[monat]+11, $row_gb1[tag], $row_gb1[jahr]));
  echo @$block4_beg_max;
  if($row_gb3['beginn'] >= $block4_beg_min){
    $block = 4;
  }
  
  
  return $block;
}

#echo get_block(87047, 18);
?>