<?php

if($_REQUEST['day_start'] && $_REQUEST['month_start'] && $_REQUEST['year_start'])
{
	while(!VerifyDate($start_date = $_REQUEST['year_start'].'-'.$_REQUEST['month_start'].'-'.$_REQUEST['day_start']))
		$_REQUEST['day_start']--;
}
else
{
	$min_date = db_request(db_execute("SELECT min(SCHOOL_DATE) AS MIN_DATE FROM SCHOOL_CALENDAR_DAYS WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'"));
	if($min_date[1]['MIN_DATE'])
		$start_date = $min_date[1]['MIN_DATE'];
	else
//		$start_date = '01-'.strtoupper(date('M-y'));
		$start_date = date('Y-m').'-01';
}

if($_REQUEST['day_start'] && $_REQUEST['month_start'] && $_REQUEST['year_start'])
{
	while(!VerifyDate($end_date = $_REQUEST['year_end'].'-'.$_REQUEST['month_end'].'-'.$_REQUEST['day_end']))
		$_REQUEST['day_end']--;
}
else
	$end_date = get_db_date();

if($_REQUEST['day_end'] && $_REQUEST['month_end'] && $_REQUEST['year_end'])
	$end_date = $_REQUEST['year_end'].'-'.$_REQUEST['month_end'].'-'.$_REQUEST['day_end'];
else
	$end_date = get_db_date();

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

if(!$_REQUEST['min_referrals'])
	$_REQUEST['min_referrals'] = 1;

if($_REQUEST['search_servicefunction']=='list' && !$_SYSTEM['modules_search'])
{
	echo '<FORM action='.SetRequestOptions().' method=POST>';
	$select = '<SELECT name=min_referrals onchange="this.form.submit();">';
	for($i=0;$i<=20;$i++)
		$select .= '<OPTION value='.$i.(($_REQUEST['min_referrals']==$i)?' SELECTED':'').'>'.$i.' '.makePlural($i,_('referral '),_('referrals')).'</OPTION>';
	$select .= '</SELECT>';
	DisplayHeader(' &nbsp; &nbsp; <B>'._('Display Students with at least ').'</B>'.$select);
	
	DisplayHeader(' &nbsp; &nbsp; <B>'._('Report Timeframe').': </B>'.PrepareDate($start_date,'_start').' - '.PrepareDate($end_date,'_end').$advanced_link,'<INPUT type=submit value='._('Go').'>');
	echo '</FORM>';
}

if(!$_REQUEST['GridSortingBy'])
{
	$_REQUEST['GridSortingBy'] = 'REF_COUNT';
	$_REQUEST['GridOrder'] = -1;
}

Widgets('all');

$addition['SELECT'] = ",COALESCE((SELECT COUNT(*) AS COUNT FROM STUDENT_DISCIPLINE_REPORTS d WHERE ssm.STUDENT_ID=d.STUDENT_ID AND d.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND d.ENTRY_DATE BETWEEN '".$start_date."' AND '".$end_date."'),0) AS REF_COUNT
,(SELECT MAX(ENTRY_DATE) FROM STUDENT_DISCIPLINE_REPORTS d WHERE ssm.STUDENT_ID=d.STUDENT_ID AND d.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND d.ENTRY_DATE BETWEEN '".$start_date."' AND '".$end_date."') AS MOST_RECENT";
$addition['WHERE'] = "AND (SELECT COUNT(*) AS COUNT FROM STUDENT_DISCIPLINE_REPORTS d WHERE ssm.STUDENT_ID=d.STUDENT_ID AND d.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND d.ENTRY_DATE BETWEEN '".$start_date."' AND '".$end_date."')>='".$_REQUEST['min_referrals']."'";


$addition['columns_after'] = array('REF_COUNT'=>_('Referrals'),'MOST_RECENT'=>_('Most Recent Incident Reports'));
$addition['functions'] = array('MOST_RECENT'=>'ProperDate');
$addition['dont_default_customised_view'] = true;

if(AllowUse('mod_discipline/DisListSubmittedReports.php'))
{
	$addition['link']['FULL_NAME']['link'] = "modloader.php?servicemod=mod_discipline/DisListSubmittedReports.php&student_header=true&search_servicefunction=list";
	$addition['link']['FULL_NAME']['variables'] = array('student_id'=>'STUDENT_ID','stuid'=>'STUDENT_ID','GridOutputCount'=>'index');
}
$addition['new'] = true;
$addition['singular'] = _('Problem Students');
$addition['plural'] = _('Amount of Problem Students');


Search('student_id',$addition);
?>