<?php

$categories_array = db_request(db_execute("SELECT f.ID,u.TITLE,u.SELECT_OPTIONS,f.DATA_TYPE,u.SORT_ORDER FROM DISCIPLINE_REPORT_FIELDS f,DISCIPLINE_CUSTOM_FIELDS_ASG u WHERE f.DATA_TYPE!='multiple_checkbox' AND u.DISCIPLINE_FIELD_ID=f.ID ORDER BY ".db_case(array('DATA_TYPE',"'textarea'","'1'","'0'")).",SORT_ORDER"),array(),array('ID'));

$addition['new'] = true;
$addition['second_col'] .= '<fieldset style="border:1px solid gray;"><legend>'._('Info that will be included into Incident Card').':</legend><TABLE>';

$addition['second_col'] .= '<TR><TD><INPUT type=checkbox name=elements[ENTRY_DATE] value=Y CHECKED>'._('Entry Date').'</TD>';
$addition['second_col'] .= '<TD><INPUT type=checkbox name=elements[USER_ID] value=Y CHECKED>'._('Reporter').'</TD></TR>';
foreach($categories_array as $id=>$category)
{
	if($i%2==0)
		$addition['second_col'] .= '</TR><TR>';
	$addition['second_col'] .= '<TD><INPUT type=checkbox name=elements[CATEGORY_'.$id.'] value=Y'.($category[1]['DATA_TYPE']=='textarea'?' CHECKED':'').'>'.$category[1]['TITLE'].'</TD>';
	$i++;
}
$addition['second_col'] .= '</TABLE></fieldset>';

/*
$templates_array = db_request(db_execute("SELECT ID,TITLE FROM LH_TEMPLATES ORDER BY TITLE"));
foreach($templates_array as $template)
	$options .= "<OPTION value=$template[ID]>".$template['TITLE'].'</OPTION>';
$addition['second_col'] .= '<TABLE><TR><TD width=100> &nbsp; </TD><TD align='.ALIGN_RIGHT.'>'._('Letterhead Template').' </TD><TD><SELECT name=_template_id><OPTION value="">'._('None').'</OPTION>'.$options.'</SELECT></TD></TR></TABLE>';
*/


Widgets('all');
Widgets('document_template');
$addition['force_search'] = true;
$addition['action'] .= "&CREATEPDF=true";


if(!$_REQUEST['search_servicefunction'] || $_REQUEST['search_servicefunction']=='search' || $_SYSTEM['modules_search'])
{
	DisplayHeader(".:".ProgramTitle().":.");
	
	Search('student_id',$addition);
}
else
{
	if($_REQUEST['month_discipline_entry_begin'] && $_REQUEST['day_discipline_entry_begin'] && $_REQUEST['year_discipline_entry_begin'])
	{
		$start_date = $_REQUEST['year_discipline_entry_begin'].'-'.$_REQUEST['month_discipline_entry_begin'].'-'.$_REQUEST['day_discipline_entry_begin'];
		if(!VerifyDate($start_date))
			unset($start_date);
		$end_date = $_REQUEST['year_discipline_entry_end'].'-'.$_REQUEST['month_discipline_entry_end'].'-'.$_REQUEST['day_discipline_entry_end'];
		if(!VerifyDate($end_date))
			unset($end_date);
	}

	if(!$_REQUEST['CREATEPDF'])
	{
		DisplayHeader(".:".ProgramTitle().":.");
		echo '<BR><BR>';
	}
	
	foreach($_REQUEST['elements'] as $column=>$Y)
	{
		$addition['SELECT'] .= ',r.'.$column;
	}

	$addition['FROM'] .= ',STUDENT_DISCIPLINE_REPORTS r ';
	$addition['WHERE'] .= " AND r.STUDENT_ID=ssm.STUDENT_ID AND r.SCHOOL_YEAR=ssm.SCHOOL_YEAR ";
	if(strpos($addition['FROM'],'STUDENT_DISCIPLINE_REPORTS dr')!==false)
		$addition['WHERE'] .= ' AND r.ID=dr.ID';
	
	$addition['group'] = array('STUDENT_ID');
	$addition['ORDER'] = ',r.ENTRY_DATE';
	
	$array = get_student_list($addition);

	if(count($array))
	{
		$handle = PDFStart();
		foreach($array as $student_id=>$referrals)
		{
			unset($_SYSTEM['DisplayHeader']);
			$_REQUEST['DrawAlign'] = 'LEFT';
			DisplayHeader(_('Discipline Incident Card'),null,null,null,null,null,null,true);
			unset($_REQUEST['DrawAlign']);
			DisplayHeader($referrals[1]['FULL_NAME'].' ('.$referrals[1]['STUDENT_ID'].')');
			DisplayHeader(get_school(get_school_id()),$courses[1]['CLASSLEVEL_ID']);
			if($start_date && $end_date)
				DisplayHeader(ProperDate($start_date).' - '.ProperDate($end_date));
			else
				DisplayHeader(_('School Year').': '.get_school_year().' - '.(get_school_year()+1));
			echo '<BR>';
			unset($_REQUEST['DrawAlign']);
			foreach($referrals as $referral)
			{
				echo '<TABLE cellpadding=5><TR>';
				if($_REQUEST['elements']['ENTRY_DATE'])
					echo '<TD><small><font color=gray>'._('Date').': </font></small><b>'.ProperDate($referral['ENTRY_DATE']).'</b></TD>';
				if($_REQUEST['elements']['USER_ID'])
					echo '<TD><small><font color=gray>'._('Reporter').': </font></small><b>'.GetTeacher($referral['USER_ID']).'</b></TD>';

				$end_tr = false;
				foreach($_REQUEST['elements'] as $column=>$Y)
				{
					if($column=='ENTRY_DATE' || $column=='USER_ID')
						continue;

					if($categories_array[substr($column,9)][1]['DATA_TYPE']=='textarea' && !$end_tr)
					{
						$end_tr = true;
						echo '</TR></TABLE>';
					}
					elseif($categories_array[substr($column,9)][1]['DATA_TYPE']=='textarea')
						echo '<BR>';
					
					if($categories_array[substr($column,9)][1]['DATA_TYPE']!='textarea')
					{
					    if($referral[$column]) $referral[$column] = _($referral[$column]);
						echo '<TD><small><font color=gray>'.$categories_array[substr($column,9)][1]['TITLE'].': </font></small><b> '.$referral[$column].'</b></TD>';
					}
					else
					{
					    if($referral[$column]) $referral[$column] = _($referral[$column]);
						echo $referral[$column].'<BR>';
					}
				}
				echo '<HR>';
			}
			echo '<BR>';
			DrawTemplateHeaders($student_id,'footer');
			echo '<!-- NEW PAGE -->';
		}
		PDFStop($handle);
	}
	else
		BackPrompt(_('No Students were found.'));
}
?>