<?php

DisplayHeader(".:".ProgramTitle().":.");

if(!$_REQUEST['school_term_sn'])
{

	//print_r(get_all_school_term('QTR',get_user_school_term()));die;
	//print_r(str_replace('E','',$_REQUEST['school_term_sn']));die;
	if(!$_REQUEST['school_term_sn'] && get_school_term(get_user_school_term(),'POST_START_DATE'))
		$_REQUEST['school_term_sn'] = get_user_school_term();
	elseif($_REQUEST['school_term_sn'] && strpos(get_all_school_term('QTR',get_user_school_term()),str_replace('E','',$_REQUEST['school_term_sn']))===false && strpos(get_child_school_term('PRO',get_user_school_term()),"'".$_REQUEST['school_term_sn']."'")===false && get_school_term(get_user_school_term(),'POST_START_DATE'))
		$_REQUEST['school_term_sn'] = get_user_school_term();
	
	if(!$_REQUEST['school_term_sn'] && get_school_term(get_parent_school_term('SEM',get_user_school_term()),'POST_START_DATE'))
		$_REQUEST['school_term_sn'] = get_parent_school_term('SEM',get_user_school_term());	
}

$sem = get_parent_school_term('SEM',get_user_school_term());
$fy = get_parent_school_term('FY',$sem);
$pro = get_child_school_term('PRO',get_user_school_term());
$pros = explode(',',str_replace("'",'',$pro));
$pro_grading = false;
$pro_select = '';
foreach($pros as $pro)
{
	if(get_school_term($pro,'POST_START_DATE'))
	{
		if(!$_REQUEST['school_term_sn'])
			$_REQUEST['school_term_sn'] = $pro;
		$pro_grading = true;
		$pros_arr[] = $pro;
	}
}

if(get_school_term(get_user_school_term(),'POST_START_DATE'))
{
	$mps_arr[] = get_user_school_term();
	if(SystemPreferences('QTR_EXAMS')=='Y')
		$mps_arr[] = 'E'.get_user_school_term();
}
elseif($_REQUEST['school_term_sn']==get_user_school_term())
	$_REQUEST['school_term_sn'] = $sem;

if(get_school_term($sem,'POST_START_DATE'))
{
	$mps_arr[] = $sem;
	if(SystemPreferences('FINAL_EXAMS')=='Y')
		$mps_arr[] = 'E'.$sem;
}
if(get_school_term($fy,'POST_START_DATE'))
{
	$mps_arr[] = $fy;
	if(SystemPreferences('FY_EXAMS')=='Y')
		$mps_arr[] = 'E'.$fy;
}

if($pro_grading)
{
	foreach($pros_arr as $mp)
		$mps_arr[] = $mp;
}

if(is_array($mps_arr))
{
	foreach($mps_arr as $mp)
	{
		if($_REQUEST['school_term_sn']!==$mp)
//		if($_REQUEST['ext_school_term']!==$mp)
		{
			$tabcolor = 'DFDFDF';
			$textcolor = '666666';
			$header .= '<TD width=10></TD<TD><A HREF='.SetRequestOptions(null,array('school_term_sn')).'&amp;school_term_sn='.$mp.'&amp;GridOutputCount='.$_REQUEST['GridOutputCount'].' style="text-decoration:none;font-weight:bold;color:#666666;" onmouseover="this.style.color=\'#000000\';" onmouseout="this.style.color=\'#666666\';">'.get_school_term($mp).'</A></TD>';
		}
		else
		{
			$tabcolor = 'CCCCCC';
			$textcolor = 'ff0000';
			$header .= '<TD width=10></TD><TD><B>'.DrawTab(get_school_term($mp),SetRequestOptions(null,array('school_term_sn')).'&amp;school_term_sn='.$mp.'&amp;GridOutputCount='.$_REQUEST['GridOutputCount'],$tabcolor,$textcolor,'_circle').'</B></TD>';
		}
	}
}
else
	$header .= _('There are no school terms available for taking grades.');

echo TabsHeader($header);

Widgets('all');

$addition['SELECT'] .= ',gc.TITLE AS COURSE_TITLE,gcp.TEACHER_ID,gcp.SCHOOL_PERIOD_ID,gss.START_DATE,gss.END_DATE';
$addition['FROM'] .= ',STUDENT_SCHEDULE gss,COURSES gc,COURSE_SECTIONS gcp';
$addition['WHERE'] .= " AND gcp.DOES_GRADES='Y' AND gcp.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND gc.COURSE_ID=gss.COURSE_ID AND gcp.COURSE_SECTION_ID=gss.COURSE_SECTION_ID AND 
			gss.STUDENT_ID=ssm.STUDENT_ID AND gss.SCHOOL_TERM_ID IN (".get_all_school_term('QTR',get_user_school_term()).") AND NOT EXISTS 
			(SELECT '' FROM STUDENT_GRADES_CARD gsrcg WHERE gss.STUDENT_ID=gsrcg.STUDENT_ID AND gss.COURSE_SECTION_ID=gsrcg.COURSE_SECTION_ID AND 
			gsrcg.SCHOOL_TERM_ID='".$_REQUEST['school_term_sn']."')";

if(get_school_term($_REQUEST['school_term_sn'],'POSTGRES_END_DATE'))
	$addition['WHERE'] .= " AND ('".get_school_term($_REQUEST['school_term_sn'],'POSTGRES_END_DATE')."' BETWEEN gss.START_DATE AND gss.END_DATE OR (gss.END_DATE IS NULL AND gss.START_DATE<='".get_school_term($_REQUEST['school_term_sn'],'POSTGRES_END_DATE')."'))";

$addition['columns_after'] = array('COURSE_TITLE'=>_('Course Name'),'TEACHER_ID'=>_('Teacher Name'),'SCHOOL_PERIOD_ID'=>_('School Period'),'START_DATE'=>_('Course Start Date'),'END_DATE'=>_('Course End Date'));

$addition['functions']['TEACHER_ID'] = 'getTeacher';
$addition['functions']['SCHOOL_PERIOD_ID'] = 'get_period';
$addition['functions']['START_DATE'] = $addition['functions']['END_DATE'] = 'ProperDate';
$addition['link']['FULL_NAME'] = false;

$addition['singular'] = _('Missing grades');
$addition['plural'] = _('Amount of missing grades');
$addition['new'] = true;

Search('student_id',$addition);

?>