<?php
require_once($staticpath."modules/mod_portal/date_to_ru.php");
if($array['1']['LAST_NAME'])
{
    if(isset($_REQUEST['GridOutputCount']))
    {
	$tmp_REQUEST = $_REQUEST;
	unset($tmp_REQUEST['GridOutputCount']);
	unset($tmp_REQUEST['values']);
	unset($tmp_REQUEST['address_id']);
	unset($tmp_REQUEST['person_id']);
	unset($tmp_REQUEST['student_id']);
	unset($tmp_REQUEST['discipline_ir_id']);
	// search_servicefunction can probably be unset in any case
	//if($tmp_REQUEST['search_servicefunction']=='result')
	unset($tmp_REQUEST['search_servicefunction']);
	// since we need this for the referrals screen
	unset($tmp_REQUEST['stuid']);
	// since GrdPrintStudGrades has issues with the MP tabs ... this probably applies to any program, but why chance it...
	if($_REQUEST['servicemod']=='mod_grades/GrdPrintStudGrades.php')
	    unset($tmp_REQUEST['search_servicefunction']);

	if($_REQUEST['GridOutputCount']>1)
	{
	    $but_left =  '<A HREF='.SetRequestOptions($tmp_REQUEST).'&GridOutputCount='.($_REQUEST['GridOutputCount']-1).'><IMG SRC=images/arrow_left.gif border=0></A>';
	}
	if($_SESSION['student_list'] && ($_REQUEST['GridOutputCount']<count($_SESSION['student_list'])))
	{
	    $but_right =  '<A HREF='.SetRequestOptions($tmp_REQUEST).'&GridOutputCount='.($_REQUEST['GridOutputCount']+1).'><IMG SRC=images/arrow_right.gif border=0></A>';
	}
    }else{
	unset($_SESSION['student_list']);
    }
    get_student_info();
}
else
    get_student_info();

/*Widgets('activity');
Widgets('course');
Widgets('activity');
*/
Widgets('all');
Search('student_id',$addition);

if($_REQUEST['servicefunction']=='add' && AllowEdit())
{
	$current_array = db_request(db_execute("SELECT 'existing' AS EXISTING FROM STUDENT_ACTIVITIES_HISTORY WHERE STUDENT_ID='".get_student_id()."' AND ACTIVITY_ID='".$_REQUEST['new_activity']."' AND SCHOOL_YEAR='".get_school_year()."'"));
	if(count($current_array)<1)
		db_execute("INSERT INTO STUDENT_ACTIVITIES_HISTORY (STUDENT_ID,ACTIVITY_ID,SCHOOL_YEAR) values('".get_student_id()."','".$_REQUEST['new_activity']."','".get_school_year()."')");

	unset($_REQUEST['new_activity']);
	unset($_SESSION['_REQUEST_vars']['new_activity']);
	unset($_REQUEST['servicefunction']);
}

if(get_student_id() && !$_REQUEST['servicefunction'])
{
	DrawStudentHeader();
	$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));
		$start_date = strtoupper(date('Y-m-d',$start_time));
		$end_date = strtoupper(date('Y-m-d',$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));
		$start_date = strtoupper(date('Y-m-d',$start_time));
		$end_date = strtoupper(date('Y-m-d',$start_time+60*60*24*7));
	}
	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(Preferences('LANGUAGE')=='ru_RU')
//		$date_select = "<OPTION value=$start>".change_date_ru('eligibity',date('M d, Y',$start)).' - '.change_date_ru('eligibity',date('M d, Y',$end)).'</OPTION>';
//	else
//		$date_select = "<OPTION value=$start>".change_date_ru(date('M d, Y',$start),'month_strreplace').' - '.date('M d, Y',$end).'</OPTION>';
		$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('eligibity',date('M d, Y',$i)).' - '.change_date_ru('eligibity',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':'').">".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>';
		}
	}
	echo "<FORM action=modloader.php?servicemod=$_REQUEST[servicemod]&GridOutputCount=".$_REQUEST['GridOutputCount']." method=POST>";
	DisplayHeader('<b>'._('Choose a period of time:').'</b>'.'<SELECT name=start_date onchange="this.form.submit();">'.$date_select.'</SELECT>');//,'<INPUT type=submit value='._('Refresh').'>'
	echo '</FORM>';

	echo '<TABLE border=0 width=100%><TR><TD width=100% valign=top>';
	
	$array = db_request(db_execute("SELECT em.STUDENT_ID,em.ACTIVITY_ID,ea.TITLE,ea.START_DATE,ea.END_DATE FROM ACTIVITY_COURSES ea,STUDENT_ACTIVITIES_HISTORY em WHERE em.SCHOOL_YEAR='".get_school_year()."' AND em.STUDENT_ID='".get_student_id()."' AND em.SCHOOL_YEAR=ea.SCHOOL_YEAR AND em.ACTIVITY_ID=ea.ID ORDER BY ea.START_DATE"),array('START_DATE'=>'ProperDate','END_DATE'=>'ProperDate'));

	$activities_array = db_request(db_execute("SELECT ID,TITLE FROM ACTIVITY_COURSES WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'"));
	if(count($activities_array))
	{
		foreach($activities_array as $value)
			$activities[$value['ID']] = $value['TITLE'];
	}

	$link['remove']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=remove&start_date=$_REQUEST[start_date]&GridOutputCount=".$_REQUEST['GridOutputCount'];
	$link['remove']['variables'] = array('activity_id'=>'ACTIVITY_ID');
	$link['add']['html']['TITLE'] = '<TABLE border=0 cellpadding=0 cellspacing=0><TR><TD>'.SelectInput('','new_activity','',$activities).'</TD><TD><INPUT type=submit value='._('Add').'></TD></TR></TABLE>';
	$link['add']['html']['remove'] = button('add');

	echo "<FORM action=modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=add&start_date=$_REQUEST[start_date]&GridOutputCount=".$_REQUEST['GridOutputCount']." method=POST>";
	/*
	$columns = array('TITLE'=>_('Activity Course'),'START_DATE'=>_('Starts'),'END_DATE'=>_('Ends'));
	Gridoutput($array,$columns,_('Activity Courses'),_('Amount of Activity Courses'),$link);
	echo '</FORM>';

	echo '</TD>';
	*/
	//echo '<TD width=100% valign=top>';
//	echo "SELECT e.STUDENT_SUITABILITY_GRADES_CODE,c.TITLE as COURSE_TITLE FROM STUDENT_SUITABILITY_GRADES e,COURSES c,COURSE_SECTIONS cp WHERE e.STUDENT_ID='".get_student_id()."' AND e.SCHOOL_YEAR='".get_school_year()."' AND e.COURSE_SECTION_ID=cp.COURSE_SECTION_ID AND cp.COURSE_ID=c.COURSE_ID AND e.SCHOOL_DATE BETWEEN '$start_date' AND '$end_date'";
	$array = db_request(db_execute("SELECT e.STUDENT_SUITABILITY_GRADES_CODE,c.TITLE as COURSE_TITLE FROM STUDENT_SUITABILITY_GRADES e,COURSES c,COURSE_SECTIONS cp WHERE e.STUDENT_ID='".get_student_id()."' AND e.SCHOOL_YEAR='".get_school_year()."' AND e.COURSE_SECTION_ID=cp.COURSE_SECTION_ID AND cp.COURSE_ID=c.COURSE_ID AND e.SCHOOL_DATE BETWEEN '$start_date' AND '$end_date'"),array('STUDENT_SUITABILITY_GRADES_CODE'=>'_makeLower'));
	$columns = array('COURSE_TITLE'=>_('Course Name'),'STUDENT_SUITABILITY_GRADES_CODE'=>_('Progress Grade'));
	Gridoutput($array,$columns,_('Courses'),_('Amount of Courses'));
	
//	echo '</TD></TR></TABLE>';
	echo '</TR></TABLE>';
}

function _makeLower($word)
{
	return ucwords(strtolower($word));
}
?>