<?php

class Reihungstest_model extends DB_Model
{
	/**
	 * Constructor
	 */
	public function __construct()
	{
		parent::__construct();
		$this->dbTable = 'public.tbl_reihungstest';
		$this->pk = 'reihungstest_id';
	}
	
	/**
	 * Gets a test from a test id only if it is available
	 */
	public function checkAvailability($reihungstest_id)
	{
		$query = 'SELECT public.tbl_reihungstest.*
					FROM public.tbl_reihungstest LEFT JOIN public.tbl_rt_studienplan USING(reihungstest_id)
				   WHERE tbl_reihungstest.oeffentlich = TRUE
					 AND tbl_reihungstest.datum > NOW()
					 AND tbl_reihungstest.anmeldefrist >= NOW()
					 AND COALESCE (
							tbl_reihungstest.max_teilnehmer,
							(
								SELECT SUM(arbeitsplaetze)
								  FROM public.tbl_ort JOIN public.tbl_rt_ort USING(ort_kurzbz)
								 WHERE rt_id = tbl_reihungstest.reihungstest_id
							)
							) - (
								SELECT COUNT(*)
								  FROM public.tbl_rt_person
								 WHERE rt_id = tbl_reihungstest.reihungstest_id
							) > 0
					  AND reihungstest_id = ?';
		
		return $this->execQuery($query, array($reihungstest_id));
	}
	
	/**
	 * Checks if there are active studyplans which have no public placement tests assigned yet.
	 * Only check assignment to studyplans that are
	 *	- Bachelor,
	 *	- active,
	 *	- set as online application
	 *  - valid for 1st terms
	 * @return array Returns object array with studyplans that have no public placement tests assigned yet.
	 */
	public function checkMissingReihungstest()
	{
		$query = '
			SELECT 
				bezeichnung
			FROM
				lehre.tbl_studienplan
			WHERE
				studienplan_id
			IN 
			(
				SELECT DISTINCT 
					studienplan_id
				FROM 
					public.tbl_studiensemester
				JOIN
					lehre.tbl_studienplan_semester 
					USING (studiensemester_kurzbz)
				JOIN
					lehre.tbl_studienplan
					USING (studienplan_id)
				JOIN
					lehre.tbl_studienordnung
					USING (studienordnung_id)
				JOIN
					public.tbl_studiengang
					USING (studiengang_kz)
				WHERE
					tbl_studiengang.aktiv = \'t\'
						AND
					tbl_studiensemester.onlinebewerbung = \'t\'
				AND
					tbl_studienplan.onlinebewerbung_studienplan = \'t\'
				AND 
					semester = 1
				AND
					typ = \'b\'

				EXCEPT

				SELECT DISTINCT 
					studienplan_id
				FROM 
					public.tbl_reihungstest 
				JOIN
					public.tbl_rt_studienplan
					USING (reihungstest_id)
				WHERE 
					datum >= now() 
				AND
					oeffentlich = \'t\'
				)
			';
		
		return $this->execQuery($query);
	}
	
	/**
	 *  Gets amount of free places.
	 * @return array Returns object array with faculty and amount of free places
	 * for each public actual placement test date.
	 */
	public function getFreePlaces()
	{
		$query = '
			SELECT
				datum,
				fakultaet,
				max_plaetze - anzahl_angemeldet AS freie_plaetze
			FROM
			(
				SELECT
					studiengang_kz,
					oeffentlich,
					tbl_studiengang.bezeichnung,
					reihungstest_id,
					tbl_reihungstest.datum,
					COALESCE
					(
						max_teilnehmer,
						(
							SELECT
								sum(arbeitsplaetze) - ceil(sum(arbeitsplaetze)/100.0*'. REIHUNGSTEST_ARBEITSPLAETZE_SCHWUND. ')
							FROM
								public.tbl_rt_ort  
							JOIN
								public.tbl_ort 
							ON (tbl_rt_ort.ort_kurzbz = tbl_ort.ort_kurzbz)
							WHERE
								tbl_rt_ort.rt_id = tbl_reihungstest.reihungstest_id
						)
					)
					AS max_plaetze,
					(
					SELECT
						count(*)
					FROM
						public.tbl_rt_person
					WHERE
						rt_id = tbl_reihungstest.reihungstest_id
					)
					AS anzahl_angemeldet,
					(
						WITH RECURSIVE meine_oes
						(
							oe_kurzbz,
							oe_parent_kurzbz,
							organisationseinheittyp_kurzbz
						)
						AS
						(
							SELECT
								oe_kurzbz, oe_parent_kurzbz, organisationseinheittyp_kurzbz
							FROM
								public.tbl_organisationseinheit
							WHERE
								oe_kurzbz in
								(
									SELECT
										oe_kurzbz
									FROM
										public.tbl_rt_studienplan
									JOIN 	
										lehre.tbl_studienplan sp USING (studienplan_id)
									JOIN 
										lehre.tbl_studienordnung USING (studienordnung_id)
									JOIN
										public.tbl_studiengang sg USING (studiengang_kz)
										WHERE
										tbl_rt_studienplan.reihungstest_id = tbl_reihungstest.reihungstest_id
								)
							AND 
								aktiv = true
								
							UNION ALL
							
							SELECT
								o.oe_kurzbz, o.oe_parent_kurzbz, o.organisationseinheittyp_kurzbz
							FROM
								public.tbl_organisationseinheit o, meine_oes
							WHERE
								o.oe_kurzbz = meine_oes.oe_parent_kurzbz
							AND 
								aktiv = true
						)
					SELECT
						ARRAY_TO_STRING(ARRAY_AGG(DISTINCT tbl_organisationseinheit.bezeichnung),\', \')
					FROM
						meine_oes
						JOIN public.tbl_organisationseinheit USING(oe_kurzbz)
					WHERE
						meine_oes.organisationseinheittyp_kurzbz=\'Fakultaet\'
				)
				AS fakultaet
			FROM
				public.tbl_reihungstest
			JOIN
				public.tbl_studiengang
				USING (studiengang_kz)
			WHERE 
				tbl_reihungstest.datum >= now()
			AND
				tbl_reihungstest.oeffentlich = \'t\'
			GROUP BY
				tbl_studiengang.bezeichnung,
				oe_kurzbz,
				reihungstest_id
			)
			AS tbl
		ORDER BY
			fakultaet,
			freie_plaetze
		';
		
		return $this->execQuery($query);
	}
	
	/**
	 * Checks if a registration date (Anmeldefrist) of a placement test has been reached yesterday.
	 * @param integer $studiengang_kz Optional. Kennzahl of degree program whose registration date should be checked.
	 * @return array Returns object array with reihungstest_ids whose registration date has been reached yesterday.
	 */
	public function checkReachedRegistrationDate($studiengang_kz = null)
	{
		$query = '
			SELECT *
			FROM PUBLIC.tbl_reihungstest
			WHERE anmeldefrist = (
					SELECT CURRENT_DATE - 1
					)
			';
		
		$parametersArray = array();
		
		if (!isEmptyString($studiengang_kz))
		{
			$query .= ' AND studiengang_kz = ?';
			array_push($parametersArray, $studiengang_kz);
		}

		return $this->execQuery($query, $parametersArray);
	}
	
	/**
	 * Loads all applicants of a placement test for runZentraleReihungstestAnmeldefristAssistenzJob
	 * @param integer $reihungstest_id ID of placement test
	 * @return array Returns object array with data of applicants.
	 */
	public function getApplicantsOfPlacementTestForCronjob($reihungstest_id)
	{
		$query = '
			SELECT tbl_rt_person.person_id,
				ps.prestudent_id,
				tbl_studienplan.orgform_kurzbz,
				tbl_prestudentstatus.studienplan_id,
				tbl_prestudentstatus.ausbildungssemester,
				nachname,
				vorname,
				tbl_zgv.zgv_kurzbz,
				ps.studiengang_kz,
				CASE WHEN tbl_prestudentstatus.statusgrund_id=9
				THEN \'Ja\'
				ELSE \'Nein\'
				END AS "qualifikationskurs",
				(
					SELECT count(*) AS prio_relativ
					FROM (
						SELECT *,
							(
								SELECT status_kurzbz
								FROM PUBLIC.tbl_prestudentstatus
								WHERE prestudent_id = pst.prestudent_id
								ORDER BY datum DESC,
									tbl_prestudentstatus.insertamum DESC LIMIT 1
								) AS laststatus
						FROM PUBLIC.tbl_prestudent pst
						JOIN PUBLIC.tbl_prestudentstatus USING (prestudent_id)
						WHERE person_id = (
								SELECT person_id
								FROM PUBLIC.tbl_prestudent
								WHERE prestudent_id = ps.prestudent_id
								)
							AND studiensemester_kurzbz = (
								SELECT studiensemester_kurzbz
								FROM PUBLIC.tbl_prestudentstatus
								WHERE prestudent_id = ps.prestudent_id
									AND status_kurzbz = \'Interessent\' LIMIT 1
								)
							AND status_kurzbz = \'Interessent\'
						) prest
					WHERE laststatus NOT IN (\'Abbrecher\', \'Abgewiesener\', \'Absolvent\')
						AND priorisierung <= (
							SELECT priorisierung
							FROM PUBLIC.tbl_prestudent
							WHERE prestudent_id = ps.prestudent_id
							)
					) AS "prioritaet",
				(
					SELECT kontakt
					FROM PUBLIC.tbl_kontakt
					WHERE kontakttyp = \'email\'
						AND zustellung = true
						AND person_id = tbl_rt_person.person_id
					ORDER BY insertamum DESC,
						updateamum DESC LIMIT 1
					) AS "email"
			FROM PUBLIC.tbl_rt_person
			JOIN PUBLIC.tbl_person ON (tbl_rt_person.person_id = tbl_person.person_id)
			JOIN PUBLIC.tbl_reihungstest rt ON (rt_id = reihungstest_id)
			JOIN PUBLIC.tbl_prestudent ps ON (ps.person_id = tbl_rt_person.person_id)
			JOIN PUBLIC.tbl_prestudentstatus USING (prestudent_id)
			JOIN lehre.tbl_studienplan ON (tbl_prestudentstatus.studienplan_id = tbl_studienplan.studienplan_id)
			LEFT JOIN bis.tbl_zgv ON (ps.zgv_code = tbl_zgv.zgv_code)
			WHERE rt_id = ?
				AND get_rolle_prestudent(prestudent_id, rt.studiensemester_kurzbz) IN (\'Interessent\', \'Bewerber\')
				AND tbl_prestudentstatus.studiensemester_kurzbz = rt.studiensemester_kurzbz
				AND bewerbung_abgeschicktamum IS NOT NULL
				AND bestaetigtam IS NOT NULL
			ORDER BY studiengang_kz, 
				orgform_kurzbz, 
				prioritaet, 
				nachname, 
				vorname, 
				person_id
			';

		return $this->execQuery($query, array($reihungstest_id));
	}

	/**
	 * Checks if an Applicant was assigned to a plament test after Anmeldefrist and before Test-Date
	 * @param integer $studiengang_kz Kennzahl of degree program those tests should be checked
	 * @return array Returns object array with data of applicants.
	 */
	public function getApplicantAssignedAfterDate($studiengang_kz)
	{
		$query = '
			SELECT tbl_rt_person.person_id,
				ps.prestudent_id,
				rt.datum,
				rt.uhrzeit,
				rt.reihungstest_id,
				tbl_studienplan.orgform_kurzbz,
				tbl_prestudentstatus.studienplan_id,
				tbl_prestudentstatus.ausbildungssemester,
				nachname,
				vorname,
				tbl_zgv.zgv_kurzbz,
				ps.studiengang_kz,
				CASE WHEN tbl_prestudentstatus.statusgrund_id=9
				THEN \'Ja\'
				ELSE \'Nein\'
				END AS "qualifikationskurs",
				(
					SELECT count(*) AS prio_relativ
					FROM (
						SELECT *,
							(
								SELECT status_kurzbz
								FROM PUBLIC.tbl_prestudentstatus
								WHERE prestudent_id = pst.prestudent_id
								ORDER BY datum DESC,
									tbl_prestudentstatus.insertamum DESC LIMIT 1
								) AS laststatus
						FROM PUBLIC.tbl_prestudent pst
						JOIN PUBLIC.tbl_prestudentstatus USING (prestudent_id)
						WHERE person_id = (
								SELECT person_id
								FROM PUBLIC.tbl_prestudent
								WHERE prestudent_id = ps.prestudent_id
								)
							AND studiensemester_kurzbz = (
								SELECT studiensemester_kurzbz
								FROM PUBLIC.tbl_prestudentstatus
								WHERE prestudent_id = ps.prestudent_id
									AND status_kurzbz = \'Interessent\' LIMIT 1
								)
							AND status_kurzbz = \'Interessent\'
						) prest
					WHERE laststatus NOT IN (\'Abbrecher\', \'Abgewiesener\', \'Absolvent\')
						AND priorisierung <= (
							SELECT priorisierung
							FROM PUBLIC.tbl_prestudent
							WHERE prestudent_id = ps.prestudent_id
							)
					) AS "prioritaet",
				(
					SELECT kontakt
					FROM PUBLIC.tbl_kontakt
					WHERE kontakttyp = \'email\'
						AND zustellung = true
						AND person_id = tbl_rt_person.person_id
					ORDER BY insertamum DESC,
						updateamum DESC LIMIT 1
					) AS "email"
			FROM PUBLIC.tbl_rt_person
			JOIN PUBLIC.tbl_person ON (tbl_rt_person.person_id = tbl_person.person_id)
			JOIN PUBLIC.tbl_reihungstest rt ON (rt_id = reihungstest_id)
			JOIN PUBLIC.tbl_prestudent ps ON (ps.person_id = tbl_rt_person.person_id)
			JOIN PUBLIC.tbl_prestudentstatus USING (prestudent_id)
			JOIN lehre.tbl_studienplan ON (tbl_prestudentstatus.studienplan_id = tbl_studienplan.studienplan_id)
			LEFT JOIN bis.tbl_zgv ON (ps.zgv_code = tbl_zgv.zgv_code)
			WHERE rt.studiengang_kz = ?
				AND get_rolle_prestudent(prestudent_id, rt.studiensemester_kurzbz) IN (\'Interessent\', \'Bewerber\')
				AND tbl_prestudentstatus.studiensemester_kurzbz = rt.studiensemester_kurzbz
				AND bewerbung_abgeschicktamum IS NOT NULL
				AND bestaetigtam IS NOT NULL
				AND anmeldefrist < (SELECT CURRENT_DATE)
				AND rt.datum > (SELECT CURRENT_DATE)
				--AND tbl_rt_person.insertamum > anmeldefrist
				--AND tbl_rt_person.insertamum < rt.datum
				AND tbl_rt_person.insertamum::date = (SELECT CURRENT_DATE -1)
			ORDER BY studiengang_kz,
				orgform_kurzbz,
				prioritaet,
				nachname,
				vorname,
				person_id
			';

		return $this->execQuery($query, array($studiengang_kz));
	}

	/**
	 * Loads all applicants of a placement test
	 * @param integer $reihungstest_id ID of placement test
	 * @return array Returns object array with data of applicants.
	 */
	public function getApplicantsOfPlacementTest($reihungstest_id)
	{
		$query = '
			SELECT DISTINCT tbl_rt_person.person_id,
				anrede,
				nachname,
				vorname,
				(
					SELECT kontakt
					FROM PUBLIC.tbl_kontakt
					WHERE kontakttyp = \'email\'
						AND zustellung = true
						AND person_id = tbl_rt_person.person_id
					ORDER BY insertamum DESC,
						updateamum DESC LIMIT 1
					) AS "email",
				tbl_ort.planbezeichnung,
				tbl_ort.lageplan
			FROM PUBLIC.tbl_rt_person
			JOIN PUBLIC.tbl_person ON (tbl_rt_person.person_id = tbl_person.person_id)
			JOIN PUBLIC.tbl_reihungstest rt ON (rt_id = reihungstest_id)
			JOIN PUBLIC.tbl_prestudent ps ON (ps.person_id = tbl_rt_person.person_id)
			JOIN PUBLIC.tbl_prestudentstatus USING (prestudent_id)
			LEFT JOIN bis.tbl_zgv ON (ps.zgv_code = tbl_zgv.zgv_code)
			LEFT JOIN PUBLIC.tbl_ort ON (tbl_rt_person.ort_kurzbz = tbl_ort.ort_kurzbz)
			WHERE rt_id = ?
				AND get_rolle_prestudent(prestudent_id, rt.studiensemester_kurzbz) IN (\'Interessent\', \'Bewerber\')
				AND tbl_prestudentstatus.studiensemester_kurzbz = rt.studiensemester_kurzbz
				AND bewerbung_abgeschicktamum IS NOT NULL
				AND bestaetigtam IS NOT NULL
			ORDER BY nachname,
				vorname,
				person_id
			';

		return $this->execQuery($query, array($reihungstest_id));
	}

	/**
	 * Loads the dates of the next placement tests within $days days to the given degree program
	 * @param integer $studiengang_kz Kennzahl of degree program to load the next placement tests
	 * @param integer $days Number of days in the future to load
	 * @return string Returns dates of the next placement test
	 */
	public function getNextPlacementtests($studiengang_kz, $days)
	{
		$query = '
			SELECT *
			FROM PUBLIC.tbl_reihungstest
			WHERE studiengang_kz = ?
			AND datum > now()
			AND datum <= now() + interval ?
			ORDER BY datum ASC
			';

		return $this->execQuery($query, array($studiengang_kz, $days.' days'));
	}

	/**
	 * Loads all placement tests of the given day and optional degree program
	 * @param string $date Date of the tests to be loaded (YYYY-MM-DD)
	 * @param integer $studiengang_kz Optional. Kennzahl of degree program to load
	 * @return array Returns object array with data of placement tests
	 */
	public function getTestsOnDate($date, $studiengang_kz = null)
	{
		$query = '
			SELECT *
			FROM PUBLIC.tbl_reihungstest
			WHERE datum = ?
				AND studiengang_kz = ?
			';

		return $this->execQuery($query, array($date, $studiengang_kz));
	}

	/**
	 * Loads all placement tests of a given person
	 * @param integer $person_id
	 * @return array Returns object array with data of placement tests
	 */
	public function getReihungstestPerson($person_id)
	{
		$query = ' 
			SELECT 
			  tbl_rt_person.*,
			  tbl_reihungstest.studiengang_kz,
			  tbl_reihungstest.anmerkung,
			  tbl_reihungstest.datum,
			  tbl_reihungstest.uhrzeit,
			  tbl_reihungstest.ext_id,
			  tbl_reihungstest.max_teilnehmer,
			  tbl_reihungstest.oeffentlich,
			  tbl_reihungstest.freigeschaltet,
			  tbl_reihungstest.studiensemester_kurzbz as studiensemester,
			  tbl_reihungstest.stufe,
			  tbl_reihungstest.anmeldefrist,
			  tbl_reihungstest.aufnahmegruppe_kurzbz,
			  tbl_studiengang.typ,
			  UPPER(typ::varchar(1) || kurzbz) AS stg_kuerzel,
			  so.studiengangbezeichnung,
			  so.studiengangbezeichnung_englisch,
			  so.studiengangkurzbzlang
			FROM
			  public.tbl_rt_person
			JOIN public.tbl_reihungstest ON (rt_id=reihungstest_id)
			JOIN public.tbl_studiengang ON tbl_reihungstest.studiengang_kz = tbl_studiengang.studiengang_kz
			JOIN lehre.tbl_studienplan sp USING(studienplan_id)
			JOIN lehre.tbl_studienordnung so USING(studienordnung_id)
			WHERE
			  tbl_rt_person.person_id = ?
			ORDER BY datum, uhrzeit ASC';

		return $this->execQuery($query, array($person_id));
	}

	/**
	 * Calculates Result of Placement Test for a given Person and given placementtest
	 * and with taking account of weighting per area
	 *
	 * @param Number $person_id ID of Person
	 * @param Boolean $punkte if true result is points else result is percentage of sum
	 * @param Number $reihungstest_id ID of Placementtest
	 * @param Array $weightedArray array of weighting per area (gewicht per gebiet_id)
	 * @param Boolean $has_excluded_gebiete if true, areas in the configArray will be excluded
	 * @param Array $basis_gebiet_id_toString areas to exclude
	 * @return float result points of RT
	 */
	public function getReihungstestErgebnisPerson(
		$person_id,
		$punkte,
		$reihungstest_id,
		$weightedArray = null,
		$has_excluded_gebiete = false,
		$basis_gebiet_id_toString = null
	)
	{
		$parametersArray = array($reihungstest_id);

		$qry =  "
				SELECT DISTINCT ON (vw_auswertung_ablauf.gebiet_id) gebiet_id,
					vw_auswertung_ablauf.*,
					tbl_studiengang.typ
				FROM
					testtool.vw_auswertung_ablauf
				JOIN
					public.tbl_studiengang USING (studiengang_kz)
				WHERE
					reihungstest_id = ? ";

		//areas of Studiengang
		if (!empty($basis_gebiet_id_toString))
		{
			$qry .= "
				AND
					gebiet_id IN (". $basis_gebiet_id_toString. ")
			";
		}

		//areas to exclude
		if($has_excluded_gebiete)
		{
			if (defined('FAS_REIHUNGSTEST_EXCLUDE_GEBIETE') && !empty(FAS_REIHUNGSTEST_EXCLUDE_GEBIETE))
			{
				$excluded_gebiete = unserialize(FAS_REIHUNGSTEST_EXCLUDE_GEBIETE);
				$exclude_gebiet_id_arr = $excluded_gebiete;
				if (is_array($exclude_gebiet_id_arr) && count($exclude_gebiet_id_arr) > 0)
				{
					$exclude_gebiet_id_toString = implode(', ', $exclude_gebiet_id_arr);
					$qry .= "
							AND
								gebiet_id NOT IN (". $exclude_gebiet_id_toString. ")
						--	AND
						--		typ = 'b'
						";
				}
			}
		}

		//using prestudent Status to avoid to get the sum of more than 1 placement tests
		$qry .= "
				AND prestudent_id = (
					SELECT
						prestudent_id
					FROM
						public.tbl_rt_person
					JOIN
						public.tbl_prestudent USING(person_id)
					JOIN
						public.tbl_prestudentstatus USING (prestudent_id, studienplan_id)
					JOIN
						tbl_reihungstest ON (
							tbl_rt_person.rt_id = tbl_reihungstest.reihungstest_id
						)
					WHERE
						tbl_rt_person.person_id = ?
					AND
						tbl_rt_person.rt_id = ?
					AND
						tbl_prestudentstatus.status_kurzbz = 'Interessent'
					AND
						tbl_prestudentstatus.studiensemester_kurzbz = tbl_reihungstest.studiensemester_kurzbz
					ORDER BY tbl_reihungstest.datum DESC, tbl_prestudent.priorisierung ASC LIMIT 1
				)
		";
		array_push($parametersArray, $person_id);
		array_push($parametersArray, $reihungstest_id);

		$resultRtPerson = $this->execQuery($qry, $parametersArray);

		$ergebnis = 0;
		$summeGewicht = 0;

		foreach ($resultRtPerson->retval as $row)
		{
			$prozent = 0;
			if($row->punkte>=$row->maxpunkte)
			{
				$prozent = 100;
				$row->punkte = $row->maxpunkte;
			}
			else
				$prozent = (($row->punkte + $row->offsetpunkte)/($row->maxpunkte + $row->offsetpunkte))*100;

			if($punkte == 'true')
			{
				if($row->punkte)
				{
					$ergebnis += $row->punkte;
				}
			}
			else
			{
				if ($row->punkte)
				{
					$gew = isset($weightedArray[$row->gebiet_id]) ? $weightedArray[$row->gebiet_id] : 1;
					$ergebnis += $prozent * $gew;
					$summeGewicht += $gew;
				}
			}
		}
		$return = $summeGewicht > 0
					? number_format($ergebnis/$summeGewicht, 4, '.', '')
					: number_format($ergebnis, 4, '.', '');

		return $return;
	}

	/**
	 * returns Reihungstests for given studyplans and include_ids
	 *
	 * @param Array $studienplan_arr array of studienplaene
	 * @param Array $include_ids array of include_ids
	 * @return Array List of Reihungstests
	 */
	public function getReihungstestByStudyPlanAndIds($studienplan_arr, $include_ids = null)
	{
		$studienplan_ids_string = implode(',', $studienplan_arr);
		$studienplan_arr = explode(',', $studienplan_ids_string);

		$parametersArray = array($studienplan_arr);

		$qry = "
				SELECT
					distinct a.*,
					CASE EXTRACT(DOW FROM a.datum)
						WHEN 0 THEN 'So'
						WHEN 1 THEN 'Mo'
						WHEN 2 THEN 'Di'
						WHEN 3 THEN 'Mi'
						WHEN 4 THEN 'Do'
						WHEN 5 THEN 'Fr'
						WHEN 6 THEN 'Sa'
					  END AS wochentag,
				    sg.kurzbzlang as stg,
					(
						SELECT count(*) FROM public.tbl_rt_person
						WHERE rt_id = a.reihungstest_id
					) as angemeldete_teilnehmer
				FROM
					public.tbl_reihungstest a
					JOIN public.tbl_rt_studienplan USING(reihungstest_id)
					JOIN public.tbl_studiengang sg USING(studiengang_kz)
				WHERE studienplan_id IN ?";

		if($include_ids && is_array($include_ids) && count($include_ids) > 0)
		{
			$include_ids_string = implode(',', $include_ids);
			$include_ids = explode(',', $include_ids_string);

			array_push($parametersArray, $include_ids);

			$qry .= "OR reihungstest_id in ?";
		}
		$qry .= "ORDER BY a.datum DESC";

		return $this->execQuery($qry, $parametersArray);
	}
	/**
	 * returns Reihungstests for given studyplans and include_ids
	 *
	 * @param Integer $studiengang_kz
	 * @param $include_id optional (here null)
	 * @return Array List of Reihungstests
	 */
	public function getZukuenftigeReihungstestStg($studiengang_kz, $include_id = null)
	{
		$parametersArray = array($studiengang_kz, $studiengang_kz, $include_id);

		$qry = "
					SELECT *,
					CASE EXTRACT(DOW FROM a.datum)
						WHEN 0 THEN 'So'
						WHEN 1 THEN 'Mo'
						WHEN 2 THEN 'Di'
						WHEN 3 THEN 'Mi'
						WHEN 4 THEN 'Do'
						WHEN 5 THEN 'Fr'
						WHEN 6 THEN 'Sa'
					  END AS wochentag,
			(
				SELECT count(*) FROM public.tbl_prestudent
				WHERE reihungstest_id=a.reihungstest_id
			) as angemeldete_teilnehmer
		FROM
			(
			SELECT *, '1' as sortierung,
				(
					SELECT upper(typ || kurzbz) FROM public.tbl_studiengang
					WHERE studiengang_kz=tbl_reihungstest.studiengang_kz
				) as stg
			FROM
				public.tbl_reihungstest
			WHERE
				datum>=now()-'1 days'::interval AND studiengang_kz=?
			UNION
			SELECT *, '2' as sortierung,
				(
					SELECT upper(typ || kurzbz) FROM public.tbl_studiengang
					WHERE studiengang_kz=tbl_reihungstest.studiengang_kz
				) as stg
			FROM
				public.tbl_reihungstest
			WHERE datum>=now()-'1 days'::interval AND studiengang_kz!=?
			UNION
			SELECT *, '0' as sortierung,
				(
					SELECT upper(typ || kurzbz) FROM public.tbl_studiengang
					WHERE studiengang_kz=tbl_reihungstest.studiengang_kz
				) as stg
			FROM
				public.tbl_reihungstest
			WHERE reihungstest_id=?
			ORDER BY sortierung, stg, datum
					) a
			";

		return $this->execQuery($qry, $parametersArray);
	}
}
