<?php

function get_school_term($mp,$column='TITLE')
{	global $_SYSTEM;

	if($column=='SCHOOL_TERM_ID')
		$column='TITLE';
//var_dump($_SESSION['get_school_term']);

	if(!$_SESSION['get_school_term'])
	{
		$_SESSION['get_school_term'] = array();
		$arr = db_request(db_execute("SELECT SCHOOL_TERM_ID,TITLE,SCHOOL_ID,SCHOOL_YEAR,".db_to_char('POST_START_DATE','YYYY-MM-DD')." AS POST_START_DATE,".db_to_char('POST_END_DATE','YYYY-MM-DD')." AS POST_END_DATE,".db_to_char('REGISTRATION_START_DATE', 'yyyy-mm-dd')." AS REGISTRATION_START_DATE,".db_to_char('REGISTRATION_END_DATE', 'yyyy-mm-dd')." AS REGISTRATION_END_DATE,'SCHOOL_QUARTERS' AS MP_TABLE,SORT_ORDER,SHORT_NAME,".db_to_char('START_DATE', 'yyyy-mm-dd')." AS START_DATE,".db_to_char('END_DATE', 'yyyy-mm-dd')." AS END_DATE,".db_to_char('START_DATE','YYYY-MM-DD')." AS POSTGRES_START_DATE,".db_to_char('END_DATE','YYYY-MM-DD')." AS POSTGRES_END_DATE FROM SCHOOL_QUARTERS"),array(),array('SCHOOL_TERM_ID'));
		if(is_array($arr))
			$_SESSION['get_school_term'] += $arr;

		$arr = db_request(db_execute("SELECT SCHOOL_TERM_ID,TITLE,SCHOOL_ID,SCHOOL_YEAR,".db_to_char('POST_START_DATE','YYYY-MM-DD')." AS POST_START_DATE,".db_to_char('POST_END_DATE','YYYY-MM-DD')." AS POST_END_DATE,".db_to_char('REGISTRATION_START_DATE', 'yyyy-mm-dd')." AS REGISTRATION_START_DATE,".db_to_char('REGISTRATION_END_DATE', 'yyyy-mm-dd')." AS REGISTRATION_END_DATE,'SCHOOL_SEMESTERS' AS MP_TABLE,SORT_ORDER,SHORT_NAME,".db_to_char('START_DATE', 'yyyy-mm-dd')." AS START_DATE,".db_to_char('END_DATE', 'yyyy-mm-dd')." AS END_DATE,".db_to_char('START_DATE','YYYY-MM-DD')." AS POSTGRES_START_DATE,".db_to_char('END_DATE','YYYY-MM-DD')." AS POSTGRES_END_DATE FROM SCHOOL_SEMESTERS"),array(),array('SCHOOL_TERM_ID'));
		if(is_array($arr))
			$_SESSION['get_school_term'] += $arr;

		$arr = db_request(db_execute("SELECT SCHOOL_TERM_ID,TITLE,SCHOOL_ID,SCHOOL_YEAR,".db_to_char('POST_START_DATE','YYYY-MM-DD')." AS POST_START_DATE,".db_to_char('POST_END_DATE','YYYY-MM-DD')." AS POST_END_DATE,".db_to_char('REGISTRATION_START_DATE', 'yyyy-mm-dd')." AS REGISTRATION_START_DATE,".db_to_char('REGISTRATION_END_DATE', 'yyyy-mm-dd')." AS REGISTRATION_END_DATE,'SCHOOL_YEARS' AS MP_TABLE,SORT_ORDER,SHORT_NAME,".db_to_char('START_DATE', 'yyyy-mm-dd')." AS START_DATE,".db_to_char('END_DATE', 'yyyy-mm-dd')." AS END_DATE,".db_to_char('START_DATE','YYYY-MM-DD')." AS POSTGRES_START_DATE,".db_to_char('END_DATE','YYYY-MM-DD')." AS POSTGRES_END_DATE FROM SCHOOL_YEARS"),array(),array('SCHOOL_TERM_ID'));
		if(is_array($arr))
			$_SESSION['get_school_term'] += $arr;

		$arr = db_request(db_execute("SELECT SCHOOL_TERM_ID,TITLE,SCHOOL_ID,SCHOOL_YEAR,".db_to_char('POST_START_DATE','YYYY-MM-DD')." AS POST_START_DATE,".db_to_char('POST_END_DATE','YYYY-MM-DD')." AS POST_END_DATE,'SCHOOL_WEEKS' AS MP_TABLE,SORT_ORDER,SHORT_NAME,".db_to_char('START_DATE', 'yyyy-mm-dd')." AS START_DATE,".db_to_char('END_DATE', 'yyyy-mm-dd')." AS END_DATE,".db_to_char('START_DATE','YYYY-MM-DD')." AS POSTGRES_START_DATE,".db_to_char('END_DATE','YYYY-MM-DD')." AS POSTGRES_END_DATE FROM SCHOOL_WEEKS"),array(),array('SCHOOL_TERM_ID'));
		if(is_array($arr))
			$_SESSION['get_school_term'] += $arr;
	}
	if(substr($mp,0,1)=='E')
	{
		$mp = substr($mp,1);
		if($column=='TITLE' || $column=='SHORT_NAME')
			$suffix = ' '._('Exam');
		if($column=='MP_TABLE')
			return 'EXAM';
	}
//	var_dump($_SESSION['get_school_term'][$mp]);
//echo $_SESSION['get_school_term']['336'][1]['SHORT_NAME'];
	if($mp==0 && $column=='TITLE')
		return _('Full Year');
	else
		return $_SESSION['get_school_term'][$mp][1][$column].$suffix;
}
function get_week($week)
{	
	global $_SYSTEM;
	switch($week)
	{
		case 'A':
			return _('Each');
		break;
		case 'E':
			return _('Even');
		break;
		case 'O':
			return _('Odd');
		break;
	}
}

function get_days($value)
{
	global $_SYSTEM,$THIS_array;
        $days_str = '';
        if(Preferences('CALENDAR') == 'Mon')
	        $days = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
        else
                $days = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
        $empty_day = _('No days were chosen');
        foreach($days as $day)
        {
                if(strpos($value,$day)!==false)
                {
                        $empty_day = '';
                        $days_str .= _($day).' ';
                }
        }
        return $days_str;
}

function get_school_termTable($mp_table)
{
	switch($mp_table)
	{
		case 'SCHOOL_SEMESTERS':
			return 'SEM';
		break;
		case 'SCHOOL_QUARTERS':
			return 'QTR';
		break;
		case 'SCHOOL_WEEKS':
			return 'PRO';
		break;
		default:
			return 'FY';
		break;
	}
}

function get_current_school_term($mp,$date,$error=true)
{	global $_SYSTEM;

	switch($mp)
	{
		case 'FY':
			$table = 'SCHOOL_YEARS';
		break;

		case 'SEM':
			$table = 'SCHOOL_SEMESTERS';
		break;

		case 'QTR':
			$table = 'SCHOOL_QUARTERS';
		break;

		case 'PRO':
			$table = 'SCHOOL_WEEKS';
		break;
	}
	if(!$_SYSTEM['get_current_school_term'][$date][$mp])
	 	$_SYSTEM['get_current_school_term'][$date][$mp] = db_request(db_execute("SELECT SCHOOL_TERM_ID FROM $table WHERE '$date' BETWEEN START_DATE AND END_DATE AND SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'"));
	//this need if dates in school terms are not set. getting one and put it as get_user_school_term
	if(!$_SYSTEM['get_current_school_term'][$date][$mp])
	 	$_SYSTEM['get_current_school_term'][$date][$mp] = db_request(db_execute(db_limit("SELECT SCHOOL_TERM_ID FROM $table WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'",1)));

	if($_SYSTEM['get_current_school_term'][$date][$mp][1]['SCHOOL_TERM_ID'])
		return $_SYSTEM['get_current_school_term'][$date][$mp][1]['SCHOOL_TERM_ID'];
	elseif(strpos($_SERVER['PHP_SELF'],'Panel.php')===false && $error==true)
//		echo ErrorMessage(array(_("You are not currently in a marking period")),'fatal');
	{
//		DisplayHeader(".:".ProgramTitle().":.");
		echo ErrorMessage(array(_('Current date is out of existent school terms. You can not use this function.')),'fatal');
	}
}
?>