<?php
/* Copyright (C) 2007 Technikum-Wien
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors: Christian Paminger <christian.paminger@technikum-wien.at>,
 *          Andreas Oesterreicher <andreas.oesterreicher@technikum-wien.at>,
 *          Rudolf Hangl <rudolf.hangl@technikum-wien.at> and
 *          Gerald Raab <gerald.raab@technikum-wien.at>.
 */
require_once('../config.inc.php');
require_once('../../include/functions.inc.php');
require_once('../../include/benutzerberechtigung.class.php');
require_once('../../include/studiengang.class.php');
require_once('../../include/preinteressent.class.php');
require_once('../../include/person.class.php');
require_once('../../include/prestudent.class.php');
require_once('../../include/datum.class.php');

if(!$conn=pg_pconnect(CONN_STRING))
   die("Konnte Verbindung zur Datenbank nicht herstellen");

$user = get_uid();

$rechte = new benutzerberechtigung($conn);
$rechte->getBerechtigungen($user);

$datum_obj = new datum();

$studiengang = new studiengang($conn);
$studiengang->getAll('typ, kurzbz', false);

echo '<html>
	<head>
		<title>PreInteressenten</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
		<link rel="stylesheet" href="../../skin/vilesci.css" type="text/css">
		<link rel="stylesheet" href="../../include/js/tablesort/table.css" type="text/css">
		<script src="../../include/js/tablesort/table.js" type="text/javascript"></script>
	</head>
	<body class="Background_main">
	';

if(!$rechte->isBerechtigt('admin') && 
   !$rechte->isBerechtigt('preinteressent') &&
   !$rechte->isBerechtigt('assistenz'))
	die('Sie haben keine Berechtigung fuer diese Seite');

if(isset($_GET['id']) && is_numeric($_GET['id']))
	$id = $_GET['id'];
else 
	die('der Parameter id mit der Person_id muss uebergeben werden');

$person = new person($conn);

if(!$person->load($id))
	die('Person wurde nicht gefunden');
	
//PERSON
echo '<h2>Person</h2>';
echo "ID: $person->person_id<br>";
echo "Name: $person->titelpre $person->nachname $person->vorname $person->titelpost<br>";
echo "Geburtsdatum: ".$datum_obj->formatDatum($person->gebdatum,'d.m.Y')."<br>";

//PREINTERESSENT

$preinteressent = new preinteressent($conn);
$preinteressent->getPreinteressenten($person->person_id);
if(count($preinteressent->result)>0)
{
	echo '<br><h2>Preinteressent</h2>';
	echo '<table class="liste table-autosort:0 table-stripeclass:alternate table-autostripe"><thead><tr><th>ID</th><th>Studiensemester</th><th>Erfassungsdatum</th><th>Anmerkung</th><th>Studiengänge</th></tr></thead><tbody>';
	foreach ($preinteressent->result as $row)
	{
		echo '<tr>';
		echo "<td>$row->preinteressent_id</td>";
		echo "<td>$row->studiensemester_kurzbz</td>";
		echo "<td>".$datum_obj->formatDatum($row->erfassungsdatum, 'd.m.Y')."</td>";
		echo "<td>$row->anmerkung</td>";
		echo '<td>';
		$preinteressent1 = new preinteressent($conn);
		$preinteressent1->loadZuordnungen($row->preinteressent_id);
		
		$stgs='';
		foreach ($preinteressent1->result as $row_zuordnung)
		{
			if($stgs!='')
				$stgs.=', ';
			$stgs.= $studiengang->kuerzel_arr[$row_zuordnung->studiengang_kz]."(".$preinteressent1->prioritaet_arr[$row_zuordnung->prioritaet].")";
		}
		echo $stgs;
		echo '</td>';
		echo '</tr>';
	}
	echo '</tbody></table>';
}

$prestudent = new prestudent($conn);
$prestudent->getPrestudenten($person->person_id);
if(count($prestudent->result)>0)
{
	echo '<br><h2>Pre-/Studenten</h2>';
	echo '<table class="liste table-autosort:0 table-stripeclass:alternate table-autostripe">
			<thead>
				<tr>
					<th>ID</th>
					<th>Studiengang</th>
					<th>Reihungstest</th>
					<th>UID</th>
					<th>Gruppe</th>
					<th>Status</th>
				</tr>
			</thead><tbody>';
	foreach ($prestudent->result as $row)
	{
		echo '<tr>';
		echo "<td>$row->prestudent_id</td>";
		echo "<td>".$studiengang->kuerzel_arr[$row->studiengang_kz]."</td>";
		echo "<td>".($row->reihungstestangetreten?'Ja':'Nein')."</td>";
		$uid='';
		$gruppe='';
		$qry ="SELECT * FROM public.tbl_student WHERE prestudent_id='$row->prestudent_id'";
		if($result = pg_query($conn, $qry))
		{
			if(pg_num_rows($result)>1)
			{
				$uid='ACHTUNG: Es gibt mehrere Studenteneinträge die auf diesen Prestudenten zeigen!';
			}
			else 
			{
				if($row_std = pg_fetch_object($result))
				{
					$uid = $row_std->student_uid;
					$gruppe = $row_std->semester.$row_std->verband.$row_std->gruppe;
				}
			}
		}
		echo "<td>$uid</td>";
		echo "<td>$gruppe</td>";
		$prestudent1 = new prestudent($conn);
		$prestudent1->getLastStatus($row->prestudent_id);	
		echo "<td>$prestudent1->rolle_kurzbz ".($prestudent1->ausbildungssemester!=''?"($prestudent1->ausbildungssemester. Semester)":'')."</td>";
		echo '</tr>';
	}
	echo '</tbody></table>';
}

$qry = "SELECT * FROM campus.vw_mitarbeiter WHERE person_id='$person->person_id'";
if($result = pg_query($conn, $qry))
{
	if(pg_num_rows($result)>0)
	{
		echo '<br><h2>Mitarbeiter</h2>';		
		echo '<table class="liste table-autosort:0 table-stripeclass:alternate table-autostripe">
			<thead>
				<tr>
					<th>UID</th>
					<th>Kurzbz</th>
					<th>Lektor</th>
					<th>Fixangestellt</th>
					<th>Telefonklappe</th>
				</tr>
			</thead><tbody>';
		while($row = pg_fetch_object($result))
		{
			echo "<tr>";
			echo "<td>$row->uid</td>";
			echo "<td>$row->kurzbz</td>";
			echo "<td>".($row->lektor=='t'?'Ja':'Nein')."</td>";
			echo "<td>".($row->fixangestellt=='t'?'Ja':'Nein')."</td>";
			echo "<td>$row->telefonklappe</td>";
			echo "</tr>";
		}
		echo '</tbody></table>';
	}
}
echo '</body>';
echo '</html>';
?>