<?php

include("kurs/datenbankanbindung.php"); // f&uuml;gt die Datenbankanbindung ein: Sys:\php\includes\kurs\datenbankanbindung.php
function decrypt($string, $key) {
  $result = '';
  $string = base64_decode($string);

  for($i=0; $i<strlen($string); $i++) {
    $char = substr($string, $i, 1);
    $keychar = substr($key, ($i % strlen($key))-1, 1);
    $char = chr(ord($char)-ord($keychar));
    $result.=$char;
  }

  return $result;
}

$querystring = $_SERVER['QUERY_STRING'];
$querystring = decrypt($querystring, "kurssystem");
parse_str($querystring);
# parse_str stellt die übergebenen Variablen in $Variablen zur verfügugn ?standort=1&jahrgang=13 => $standort=13, $standort=1
#echo $querystring;

#echo "<br>datid: ".$datid."<br>";
#echo "standort: ".$sid."<br>";
#echo "jahrgang: ".$jahrgang."<br>";
#exit;

#require_once("class.fpdf_table.php");
require_once('as_utf_class.php');
require_once("table_def.inc");
$pdf=new utfFPDF();


$pdf->SetAutoPageBreak(true, 20);
$pdf->SetTopMargin(6);
$pdf->SetLeftMargin(6);
$pdf->AddPage();

$db = dbconnect();


#$pagecount = $pdf->setSourceFile("praxisstellen.pdf");
#$pdf->addPage();
#$pdf->useTemplate($tplidx,0,0,0);

$res_so = $db->query("SELECT standort, block, abdatum, pendler
                         FROM stan_standort
                        WHERE sid = '$sid'");
$row_so = $res_so->fetch_array();

$pdf->SetFont('Arial', '', 10);
$pdf->SetY(20);
$pdf->SetX(20);
$pdf->MultiCell(0,4, "AG-Standort:",0,'J',0);

$pdf->SetFont('Arial', '', 10);
$pdf->SetY(20);
$pdf->SetX(60);
$pdf->MultiCell(0,4, "$row_so[standort] (Block $row_so[block]) ab $row_so[abdatum] ($row_so[pendler])",0,'J',0);


$pos=$pdf->GetY()+2;


$res_vt = $db->query("SELECT bezeichnung
                         FROM stan_ag_standort_termine a, stan_ag_standort b, stan_vertiefungsbereich c
                        WHERE a.fagsid =b.agsid
                          AND b.vbid = c.vbid
                          AND a.datid='$datid'");
$row_vt = $res_vt->fetch_array();

$pdf->SetFont('Arial', '', 10);
$pdf->SetY($pos);
$pdf->SetX(20);
$pdf->MultiCell(0,4, "Vertiefungsbereich:",0,'J',0);

$pdf->SetFont('Arial', '', 10);
$pdf->SetY($pos);
$pdf->SetX(60);
$pdf->MultiCell(0,4, "$row_vt[bezeichnung]",0,'J',0);


$pos=$pdf->GetY()+2;

$res_dt = $db->query("SELECT date_format(datum, '%d.%m.%Y') datum
                         FROM stan_ag_standort_termine
                        WHERE datid='$datid'");
$row_dt = $res_dt->fetch_array();
$pdf->SetFont('Arial', '', 10);
$pdf->SetY($pos);
$pdf->SetX(20);
$pdf->MultiCell(0,4, "Datum:",0,'J',0);

$pdf->SetFont('Arial', '', 10);
$pdf->SetY($pos);
$pdf->SetX(60);
$pdf->MultiCell(0,4, "$row_dt[datum]",0,'J',0);





$pos=$pdf->GetY()-16;

$pdf->Rect(20,18,170,$pos);

$columns = 4; //five columns


$pos=$pdf->GetY()+10;


$pdf->SetY($pos);

	//we initialize the table class
	$pdf->Table_Init($columns, true, true);

	$table_subtype = $table_default_table_type;
	$pdf->Set_Table_Type($table_subtype);
	
	//TABLE HEADER SETTINGS
	$header_subtype = $table_default_header_type;
	
	
	for($i=0; $i<$columns; $i++) $header_type[$i] = $table_default_header_type;

	$header_type[0]['WIDTH'] = 10;
	$header_type[0]['BG_COLOR'] = array(200,200,200);
	$header_type[0]['T_ALIGN'] = "L"; //C=CENTER, L=LEFT, R=RIGHT, J=JUSTIFY
	$header_type[0]['V_ALIGN'] = "M"; //T=TOP, M=MIDDLE, B=BOTTOM
	$header_type[0]['TEXT'] = "Nr";
	
	$header_type[1]['WIDTH'] = 53;
	$header_type[1]['BG_COLOR'] = array(200,200,200);
	$header_type[1]['T_ALIGN'] = "L"; //C=CENTER, L=LEFT, R=RIGHT, J=JUSTIFY
	$header_type[1]['V_ALIGN'] = "M"; //T=TOP, M=MIDDLE, B=BOTTOM
	$header_type[1]['TEXT'] = "Name";
	
	
	$header_type[2]['WIDTH'] = 53;
	$header_type[2]['BG_COLOR'] = array(200,200,200);
	$header_type[2]['T_ALIGN'] = "L"; //C=CENTER, L=LEFT, R=RIGHT, J=JUSTIFY
	$header_type[2]['V_ALIGN'] = "M"; //T=TOP, M=MIDDLE, B=BOTTOM
	$header_type[2]['TEXT'] = "Hochschule";
	
	$header_type[3]['WIDTH'] = 53;
	$header_type[3]['BG_COLOR'] = array(200,200,200);
	$header_type[3]['T_ALIGN'] = "L"; //C=CENTER, L=LEFT, R=RIGHT, J=JUSTIFY
	$header_type[3]['V_ALIGN'] = "M"; //T=TOP, M=MIDDLE, B=BOTTOM
	$header_type[3]['TEXT'] = "AG";
	
	#$header_type[1]['COLSPAN'] = "2";

	//set the header type
	$pdf->Set_Header_Type($header_type);
	
	$pdf->Draw_Header();
	
	//TABLE DATA SETTINGS
	$data_subtype = $table_default_data_type;
	
	$data_type = Array();//reset the array
	for ($i=0; $i<$columns; $i++) $data_type[$i] = $data_subtype;

	$pdf->Set_Data_Type($data_type);
	
#	$fsize = 10;
#	$colspan = 1;
#	$rr = 255;

        $db = dbconnect();
        $query = "SELECT vorname, nachname, hs, ag, jahrgang
		    FROM stan_ag_wunsch_vw a, stan_ag_standort b, stan_ag_standort_termine c, stud d
		   WHERE a.prio0 =b.agsid
		     AND b.agsid = c.fagsid
		     AND a.uid=d.uid
		     AND c.datid='$datid'
		     AND b.sid='$sid'
		     AND d.durchgefallen != 'Y'
		     AND d.jahrgang = '$jahrgang'
		     AND d.stg='I'
                 ORDER BY d.nachname, d.vorname";

        $result = $db->query ($query)
           or die ("Cannot execute query1");
           
 $ln_zaehler = 0;
 $ln_zaehler1 = 0;
        while ($row = $result->fetch_array())
	{
	$ln_zaehler1 = $ln_zaehler1+1;
	$ln_zaehler = $ln_zaehler+1;
		$data = Array();
		


                if($row[hs] == 'K' or $row[hs] == 'k'){
                  $hs = "Kehl";
                }else{
                  $hs = "Ludwigsburg";
                }

/* Schriftgröße
		$data[0]['T_SIZE'] = $fsize;
		$data[1]['T_SIZE'] = $fsize+3;
		$data[3]['T_SIZE'] = $fsize+4;
*/
/* Schriftfarbe und Zellenhintergrund
		$data[0]['T_COLOR'] = array(0,0,0); //Schriftfarbe
		$data[0]['BG_COLOR'] = array($rr,$rr,$rr);
		$data[3]['T_COLOR'] = array($rr,240,240);
		$data[3]['BG_COLOR'] = array($rr,100,135);
*/
			$data[0]['TEXT'] = "$ln_zaehler";
			$data[0]['T_ALIGN'] = "L"; //C=CENTER, L=LEFT, R=RIGHT, J=JUSTIFY
			$data[0]['V_ALIGN'] = "M"; //T=TOP, M=MIDDLE, B=BOTTOM
		       #$data[0]['T_TYPE'] = "BI"; // B=BOLD, I=ITALIC
		       
			$data[1]['TEXT'] = "$row[vorname] $row[nachname]";
			$data[1]['T_ALIGN'] = "L"; //C=CENTER, L=LEFT, R=RIGHT, J=JUSTIFY
			$data[1]['V_ALIGN'] = "M"; //T=TOP, M=MIDDLE, B=BOTTOM
		       #$data[1]['T_TYPE'] = "BI"; // B=BOLD, I=ITALIC
			
			$data[2]['TEXT'] = "$hs";
			$data[2]['T_ALIGN'] = "L";
			$data[2]['V_ALIGN'] = "M";

			$data[3]['TEXT'] = "$row[ag]/$row[jahrgang]";
			$data[3]['T_ALIGN'] = "L";
			$data[3]['V_ALIGN'] = "M";

		$pdf->Draw_Data($data,NULL,10); // -->letzter Parameter Höhe der Datenspalte. Neu prgrammiert in prints/class.fpdf_table.php am 15.04.2009
		if($ln_zaehler1 == 50){ // --> Nach X Zeilen Seitenumbruch einfügen
		  $pdf->AddPage();
		  $ln_zaehler1 = 0;
		}
	}
	$pdf->Draw_Table_Border();
$pdf->Output();


?>