<?php

require_once($staticpath."modules/mod_portal/date_to_ru.php");
global $DBType;

$start_end_array = db_request(db_execute("SELECT TITLE,VALUE FROM SYSTEM_CONFIG WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."' AND PROGRAM='suitability' AND TITLE IN ('START_DAY','END_DAY')"));
if(count($start_end_array))
{
	foreach($start_end_array as $value)
		$$value['TITLE'] = $value['VALUE'];
}

switch(date('D'))
{
	case 'Mon':
	$today = 1;
	break;
	case 'Tue':
	$today = 2;
	break;
	case 'Wed':
	$today = 3;
	break;
	case 'Thu':
	$today = 4;
	break;
	case 'Fri':
	$today = 5;
	break;
	case 'Sat':
	$today = 6;
	break;
	case 'Sun':
	$today = 7;
	break;
}

$start = time() - ($today-$START_DAY)*60*60*24;
$end = time();

if(!$_REQUEST['start_date'])
{
	$start_time = $start;
	$start_date = strtoupper(date('d-M-y',$start_time));
	$end_date = strtoupper(date('d-M-y',$end));
}
else
{
	$start_time = $_REQUEST['start_date'];
	$start_date = strtoupper(date('d-M-y',$start_time));
	$end_date = strtoupper(date('d-M-y',$start_time+60*60*24*7));
}

$QI = db_execute("SELECT SCHOOL_PERIOD_ID,TITLE FROM SCHOOL_PERIODS WHERE SCHOOL_ID='".get_school_id()."' AND SCHOOL_YEAR='".get_school_year()."' ORDER BY SORT_ORDER ");
$periods_array = db_request($QI);

$period_select =  "<SELECT name=period><OPTION value=''>"._('All')."</OPTION>";
foreach($periods_array as $period)
	$period_select .= "<OPTION value=$period[SCHOOL_PERIOD_ID]".(($_REQUEST['period']==$period['SCHOOL_PERIOD_ID'])?' SELECTED':'').">".$period['TITLE']."</OPTION>";
$period_select .= "</SELECT>";

DisplayHeader(".:".ProgramTitle().":.");
echo "<FORM action=modloader.php?servicemod=$_REQUEST[servicemod] method=POST>";

if($DBType == 'mssql')
	$begin_year = db_request(db_execute("SELECT isnull(min(datediff(s, '1970-01-01 00:00:00', SCHOOL_DATE)), 0) as SCHOOL_DATE FROM SCHOOL_CALENDAR_DAYS WHERE SCHOOL_ID='".get_school_id()."' AND SCHOOL_YEAR='".get_school_year()."'"));
else
	$begin_year = db_request(db_execute("SELECT min(date_part('epoch',SCHOOL_DATE)) as SCHOOL_DATE FROM SCHOOL_CALENDAR_DAYS WHERE SCHOOL_ID='".get_school_id()."' AND SCHOOL_YEAR='".get_school_year()."'"));
$begin_year = $begin_year[1]['SCHOOL_DATE'];

if($start && $begin_year)
{
	if(Preferences('LANGUAGE')=='ru_RU')
		$date_select = "<OPTION value=$start>".change_date_ru('month_strreplace',date('M d, Y',$start)).' - '.change_date_ru('month_strreplace',date('M d, Y',$end)).'</OPTION>';
	else
		$date_select = "<OPTION value=$start>".change_date_ru('month_strreplace',date('M d, Y',$start)).' - '.change_date_ru('month_strreplace',date('M d, Y',$end)).'</OPTION>';
	if ($begin_year)
	{
		for($i=$start-(60*60*24*7);$i>=$begin_year;$i-=(60*60*24*7))
			if(Preferences('LANGUAGE')=='ru_RU')
				$date_select .= "<OPTION value=$i".(($i+86400>=$start_time && $i-86400<=$start_time)?' SELECTED':'').">".change_date_ru('month_strreplace',date('M d, Y',$i)).' - '.change_date_ru('month_strreplace',date('M d, Y',($i+1+(($END_DAY-$START_DAY))*60*60*24))).'</OPTION>';
			else
				$date_select .= "<OPTION value=$i".(($i+86400>=$start_time && $i-86400<=$start_time)?' SELECTED':'').">".date('M d, Y',$i).' - '.date('M d, Y',($i+1+(($END_DAY-$START_DAY))*60*60*24)).'</OPTION>';
	}
}

DisplayHeader('<SELECT name=start_date>'.$date_select.'</SELECT>'.$period_select,'<INPUT style="width:100px;" type=submit value='._('OK').'>');
echo '</FORM>';
$sql = "SELECT s.LAST_NAME".CCAT."', '".CCAT."s.FIRST_NAME AS FULL_NAME,sp.TITLE,cp.SCHOOL_PERIOD_ID,s.USER_ID 
		FROM USERS s,COURSE_SECTIONS cp,SCHOOL_PERIODS sp 
		WHERE 
			sp.SCHOOL_PERIOD_ID = cp.SCHOOL_PERIOD_ID
			AND cp.TEACHER_ID=s.USER_ID AND cp.SCHOOL_TERM_ID IN (".get_all_school_term('QTR',get_user_school_term()).")
			AND cp.SCHOOL_YEAR='".get_school_year()."' AND cp.SCHOOL_ID='".get_school_id()."' AND s.PROFILE='teacher'
			".(($_REQUEST['period'])?" AND cp.SCHOOL_PERIOD_ID='$_REQUEST[period]'":'')."
			AND NOT EXISTS (SELECT '' FROM SUITABILITY_TEACHERS_FILLED ac WHERE ac.USER_ID=cp.TEACHER_ID AND ac.SCHOOL_PERIOD_ID = sp.SCHOOL_PERIOD_ID AND ac.SCHOOL_DATE BETWEEN '$start_date' AND '$end_date')";

$array = db_request(db_execute($sql),array(),array('USER_ID','SCHOOL_PERIOD_ID'));

$i = 0;
if(count($array))
{
	foreach($array as $user_id=>$periods)
	{
		$i++;
		$user_array[$i]['FULL_NAME'] = $periods[key($periods)][1]['FULL_NAME'];
		foreach($periods as $school_period_id=>$period)
		{
			$user_array[$i][$school_period_id] = '<IMG SRC=images/x.gif>';
		}
	}
}
//die('hii');

$columns = array('FULL_NAME'=>_('Teacher Name'));

if(!$_REQUEST['period'])
{
	foreach($periods_array as $period)
		$columns[$period['SCHOOL_PERIOD_ID']] = $period['TITLE'];
}
else
{
	foreach($periods_array as $period)
		$periodsby[$period['SCHOOL_PERIOD_ID']] = $period['TITLE'];
		$columns[$_REQUEST['period']] = $periodsby[$_REQUEST['period']];
}
/*
echo "<pre>";
print_r ($user_array);
echo "</pre>";
*/
/*
echo "<pre>";
print_r($columns);
echo "</pre>";
*/
Gridoutput($user_array,$columns,_('Teachers who have not filled eligibility form'),_('Amount of teachers who have not filled eligibility form'));

?>