<?php
$button = new Button();
$grad_subjects_array = db_request(db_execute("SELECT ID,TITLE,CREDITS,'' AS REMAINING_CREDITS FROM GRADUATION_REQ_SUBJECTS WHERE SCHOOL_ID='".get_school_id()."'"));
$grad_subjects_ID_array = db_request(db_execute("SELECT ID,TITLE,CREDITS,'' AS REMAINING_CREDITS FROM GRADUATION_REQ_SUBJECTS WHERE SCHOOL_ID='".get_school_id()."'"),null,array('ID'));

/*
$subject_select =  "<SELECT name=subject_id><OPTION value=''>"._('All Subjects')."</OPTION>";
foreach($grad_subjects_array as $subject)
	$subject_select .= "<OPTION value=$subject[ID]".(($_REQUEST['subject_id']==$subject['ID'])?' SELECTED':'').">".$subject['TITLE']."</OPTION>";
$subject_select .= "</SELECT>";
*/

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

$tmp_PHP_SELF = SetRequestOptions('',array('school_term_sn'));
$school_terms = array('SCHOOL_YEARS'=>_('Full-Year Grades'),'SCHOOL_SEMESTERS'=>_('Semester Grades'),'SCHOOL_QUARTERS'=>_('Quarter Grades'));
if(get_user_school_term())
{
	$sem = get_parent_school_term('SEM',get_user_school_term());
        if(!get_school_term($sem,'START_DATE'))
        		unset($school_terms['SCHOOL_SEMESTERS']);
        elseif(!$_REQUEST['school_term_sn'])
		$_REQUEST['school_term_sn'] = 'SCHOOL_SEMESTERS';
	$fy = get_parent_school_term('FY',$sem);
	if(!get_school_term($fy,'START_DATE'))
		unset($school_terms['SCHOOL_YEARS']);
	elseif(!$_REQUEST['school_term_sn'])
		$_REQUEST['school_term_sn'] = 'SCHOOL_YEARS';
	if(!get_school_term(get_user_school_term(),'POST_START_DATE'))
		unset($school_terms['SCHOOL_QUARTERS']);
	elseif(!$_REQUEST['school_term_sn'])
		$_REQUEST['school_term_sn'] = 'SCHOOL_QUARTERS';
}
else
	$_REQUEST['school_term_sn'] = 'SCHOOL_SEMESTERS';
foreach($school_terms as $table=>$mp_title)
{
	if($_REQUEST['school_term_sn']!==$table)
		$header .= $button->createText($mp_title,$tmp_PHP_SELF.'&amp;school_term_sn='.$table);
	else
		$header .= $button->createButton($mp_title,$tmp_PHP_SELF.'&amp;school_term_sn='.$table);
}
echo '<table align=center><tr><td>'.TabsHeader($header).'</td></tr></table>';
echo '<HR>';
$mps = GetAllTerms($_REQUEST['school_term_sn']);

//echo "<FORM action=modloader.php?servicemod=$_REQUEST[servicemod] method=POST>";
//DisplayHeader($subject_select.' <INPUT type=submit value='._('Go').'>');
//echo '</FORM>';

Widgets('all');
$addition['singular'] = _('Students with credits to be taken for graduation');
$addition['plural'] = _('Amount of students with credits to be taken for graduation');
foreach($grad_subjects_array as $subject)
{
	$addition['SELECT'] .= ',NULL AS S'.$subject['ID'];
	$addition['columns_after']['S'.$subject['ID']] = $subject['TITLE'].' ('.$subject['CREDITS'].')';
	$addition['functions']['S'.$subject['ID']] = '_makeCredits';
}

if(get_student_id())
{
	$addition2['WHERE'] = $addition3['WHERE'] = $addition4['WHERE'] = "AND ssm.STUDENT_ID='".get_student_id()."' ";
}

$addition2['DisableSearchResDisplay'] = true;
$addition2['SELECT_ONLY'] = 'ssm.STUDENT_ID,gs.TITLE,gs.ID,COALESCE(sum(srcg.CREDITS),0) AS CREDITS';
$addition2['FROM'] = 'LEFT OUTER JOIN GRADUATION_REQ_SUBJECTS gs ON (gs.SCHOOL_ID=ssm.SCHOOL_ID) LEFT OUTER JOIN STUDENT_GRADES_CARD srcg ON (gs.SCHOOL_ID=srcg.SCHOOL_ID AND gs.ID=srcg.GRAD_SUBJECT_ID AND ssm.STUDENT_ID=srcg.STUDENT_ID AND srcg.CARRIES_CREDITS=\'Y\' AND srcg.SCHOOL_TERM_ID IN ('.$mps.'))';
$addition2['FROM'] = 'LEFT OUTER JOIN GRADUATION_REQ_SUBJECTS gs ON (gs.SCHOOL_ID=ssm.SCHOOL_ID) LEFT OUTER JOIN
(SELECT * from STUDENT_GRADES_CARD WHERE CARRIES_CREDITS=\'Y\' AND SCHOOL_TERM_ID IN ('.$mps.')) srcg
ON (gs.SCHOOL_ID=srcg.SCHOOL_ID AND gs.ID=srcg.GRAD_SUBJECT_ID AND ssm.STUDENT_ID=srcg.STUDENT_ID)';
$addition2['GROUP'] = 'ssm.STUDENT_ID,gs.ID,gs.TITLE,gs.CREDITS';
$addition2['group'] = array('STUDENT_ID','ID');
$credits_array = get_student_list($addition2);

$addition3['DisableSearchResDisplay'] = true;
$addition3['SELECT'] = ',srcg.COURSE_TITLE,srcg.GRADE_TITLE,srcg.CREDITS,srcg.GRAD_SUBJECT_ID';
$addition3['FROM'] = ',STUDENT_GRADES_CARD srcg';
$addition3['WHERE'] .= 'AND srcg.STUDENT_ID=ssm.STUDENT_ID AND srcg.CARRIES_CREDITS=\'Y\' AND srcg.SCHOOL_ID=ssm.SCHOOL_ID AND srcg.SCHOOL_TERM_ID IN ('.$mps.')';
$addition3['group'] = array('STUDENT_ID','GRAD_SUBJECT_ID');
$completed_array = get_student_list($addition3);

if($curr_mp = get_current_school_term('QTR',get_db_date(),false))
{
	$addition4['DisableSearchResDisplay'] = true;
	$addition4['SELECT'] = ',c.TITLE,cw.CREDITS,c.GRAD_SUBJECT_ID';
	$addition4['FROM'] = ',STUDENT_SCHEDULE sched,COURSES c,COURSE_CREDITS cw';
	$addition4['WHERE'] .= 'AND c.GRAD_SUBJECT_ID IS NOT NULL AND sched.COURSE_ID=c.COURSE_ID AND sched.COURSE_CREDIT_TITLE=cw.COURSE_CREDIT_TITLE AND c.COURSE_ID=cw.COURSE_ID AND sched.STUDENT_ID=ssm.STUDENT_ID AND sched.SCHOOL_ID=ssm.SCHOOL_ID 
		AND sched.SCHOOL_TERM_ID IN ('.get_all_school_term('QTR',$curr_mp).') AND NOT EXISTS (SELECT \'\' FROM STUDENT_GRADES_CARD srcg WHERE srcg.STUDENT_ID=ssm.STUDENT_ID AND srcg.COURSE_SECTION_ID=sched.COURSE_SECTION_ID AND srcg.SCHOOL_TERM_ID IN ('.$mps.'))';
	$addition4['group'] = array('STUDENT_ID','GRAD_SUBJECT_ID');
	$current_array = get_student_list($addition4);
}

if ($current_array) {
	foreach($current_array as $student_id=>$subjects)
	{
		foreach($subjects as $subject_id=>$courses)
		{
			foreach($courses as $course)
			{
				$current_credits_array[$student_id][$subject_id] += $course['CREDITS'];
			}
		}
	}
}


$addition['dont_default_customised_view'] = true;
Search('student_id',$addition);

if(get_student_id())
{
	$THIS_array['STUDENT_ID'] = get_student_id();
	foreach($grad_subjects_array as $i=>$subject)
	{
		$grad_subjects_array[$i]['REMAINING_CREDITS'] = _makeCredits(null,'S'.$subject['ID']);
		$grad_subjects_array[$i]['CURRENT_CREDITS'] = _makeCurrentCredits(null,'S'.$subject['ID']);
	}
	
	$columns = array('TITLE'=>_('Name of Subject'),'CREDITS'=>_('Credits Required to Graduate'),'REMAINING_CREDITS'=>_('Credits Remaining to be Taken'),'CURRENT_CREDITS'=>_('Currently Earned Credits'));
	Gridoutput($grad_subjects_array,$columns,_('Subjects with required credits for graduation'),_('Amount of subjects with required credits for graduation'));
}

function _makeCredits($value,$column,$col='')
{	global $credits_array,$THIS_array,$grad_subjects_ID_array,$completed_array,$current_array;

	$subject_id = substr($column,1);
	if($credits_array[$THIS_array['STUDENT_ID']][$subject_id][1]['CREDITS']>=$grad_subjects_ID_array[$subject_id][1]['CREDITS'])
		$return = '<IMG SRC=images/check.gif>';
	else
		$return = $grad_subjects_ID_array[$subject_id][1]['CREDITS'] - $credits_array[$THIS_array['STUDENT_ID']][$subject_id][1]['CREDITS'];

	$table .= _('Completed:').'<BR>';
	if(!empty($completed_array[$THIS_array['STUDENT_ID']][$subject_id]) && count($completed_array[$THIS_array['STUDENT_ID']][$subject_id]))
	{
		$table .= '<TABLE border=0>';
		$table .= '<TR><TD width=15></TD><TD><b>'._('Course').'</b></TD><TD><b>'._('Grade').'</b></TD><TD><b>'._('Credits').'</b></b></TD></TR>';
		foreach($completed_array[$THIS_array['STUDENT_ID']][$subject_id] as $course)
		{
			$table .= '<TR><TD width=25></TD><TD>'.str_replace("'","&rsquo;",$course['COURSE_TITLE']).'</TD><TD align=right>'.$course['GRADE_TITLE'].'</TD><TD align=right>'.$course['CREDITS'].'</TD></TR>';
		}
		$table .= '</TABLE>';
	}
	else
		$table .= '&nbsp; &nbsp; <small>'._('None').'</small>';
		
	$table .= '<BR>';

	$table .= _('Currently Earned:').'<BR>';
	
	if(!empty($current_array[$THIS_array['STUDENT_ID']][$subject_id]) && count($current_array[$THIS_array['STUDENT_ID']][$subject_id]))
	{
		$table .= '<TABLE border=0>';
		$table .= '<TR><TD width=15></TD><TD><b>'._('Course').'</b></TD><TD><b>'._('Credits').'</b></b></TD></TR>';
		foreach($current_array[$THIS_array['STUDENT_ID']][$subject_id] as $course)
		{
			$table .= '<TR><TD width=25></TD><TD>'.str_replace("'","&rsquo;",$course['TITLE']).'</TD><TD align=right>'.$course['CREDITS'].'</TD></TR>';
		}
		$table .= '</TABLE>';
	}
	else
		$table .= '&nbsp; &nbsp; <small>'._('None').'</small>';
	
	return '<DIV onMouseOver=\'stm(["'._('Courses Meeting this Requirement').'","'.$table.'"],["white","#333366","","","",,"black","#e8e8ff","","","",,,,2,"#333366",2,,,,,"",,,,]);\' onMouseOut=\'htm()\'>'.$return.'</DIV>';
}

function _makeCurrentCredits($value,$column)
{	global $current_credits_array,$THIS_array,$current_array;

	$subject_id = substr($column,1);
	$return = $current_credits_array[$THIS_array['STUDENT_ID']][$subject_id];

	if(!empty($current_array[$THIS_array['STUDENT_ID']][$subject_id]) && count($current_array[$THIS_array['STUDENT_ID']][$subject_id]))
	{
		//$table .= _('Currently Enrolled:').'<BR>';
		
		if(!empty($current_array[$THIS_array['STUDENT_ID']][$subject_id]) && count($current_array[$THIS_array['STUDENT_ID']][$subject_id]))
		{
			$table .= '<TABLE border=0>';
			$table .= '<TR><TD width=15></TD><TD><b>'._('Course').'</b></TD><TD><b>'._('Credits').'</b></b></TD></TR>';
			foreach($current_array[$THIS_array['STUDENT_ID']][$subject_id] as $course)
			{
				$table .= '<TR><TD width=25></TD><TD>'.str_replace("'","&rsquo;",$course['TITLE']).'</TD><TD align=right>'.$course['CREDITS'].'</TD></TR>';
			}
			$table .= '</TABLE>';
		}
		else
			$table .= '&nbsp; &nbsp; <small>'._('None').'</small>';
		return '<DIV onMouseOver=\'stm(["'._('Scheduled Courses Meeting this Requirement').'","'.$table.'"],["white","#333366","","","",,"black","#e8e8ff","","","",,,,2,"#333366",2,,,,,"",,,,]);\' onMouseOut=\'htm()\'>'.$return.'</DIV>';
	}
	else
		return $return;	
}

?>