<?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
//	$start_date = '01-'.strtoupper(date('M-y'));
	$start_date = date('Y-m').'-01';

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

if($_REQUEST['month_values'] && $_POST['month_values'])
{
	foreach($_REQUEST['month_values'] as $column=>$value)
	{
		$_REQUEST['values'][$column] = $_REQUEST['year_values'][$column].'-'.$value.'-'.$_REQUEST['day_values'][$column];
		if($_REQUEST['values'][$column]=='--')
			$_REQUEST['values'][$column] = '';
	}
	$_POST['values'] = $_REQUEST['values'];
}

if($_REQUEST['values'] && $_POST['values'])
{
	$sql = "INSERT INTO STUDENT_DISCIPLINE_REPORTS ";
	
	$fields = "ID,SCHOOL_YEAR,SCHOOL_ID,STUDENT_ID,";
	$values = db_seq_nextval('STUDENT_DISCIPLINE_REPORTS_SEQ').",'".get_school_year()."','".get_school_id()."','".get_student_id()."',";

	$go = 0;

	foreach($_REQUEST['values'] as $column=>$value)
	{
		if($value)
		{
			$fields .= $column.',';
			if(!is_array($value))
				$values .= "'".str_replace("\'","''",str_replace('&quot;','"',$value))."',";
			else
			{
				$values .= "'||";
				foreach($value as $val)
				{
					if($val)
						$values .= str_replace('&quot;','"',$val).'||';
				}
				$values .= "',";
			}
			$go = true;
		}
	}

	$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
	db_execute($sql);
	if(get_user('PROFILE')!='admin')
		$note = '<IMG SRC=images/check.gif> '._('This discipline incident has been successfully referred to an administrator.');
	else
		$note = '<IMG SRC=images/check.gif> '._('This discipline referral has been successfully added.');
	unset($_REQUEST['values']);
	unset($_SESSION['_REQUEST_vars']['values']);

	if($_REQUEST['return']=='student_violation')
	{
		echo '<script language=javascript>document.location.href="modloader.php?servicemod=mod_discipline/DisListSubmittedReports.php&student_header=true&stuid='.get_student_id().'&student_id='.get_student_id().'&GridOutputCount='.$_REQUEST['GridOutputCount'].'&search_servicefunction=list";</script>';
		exit;
	}

	unset($_REQUEST['student_id']);
	unset($_SESSION['student_id']);
}

DisplayHeader(".:".ProgramTitle().":.");
if(strlen($note)>0)
	DisplayHeader($note);

//if(!$_REQUEST['student_id'])
	$addition['new'] = true;


if($_REQUEST['student_id'])
	echo '<BR>';
Widgets('all');
Search('student_id',$addition);

if(get_student_id() && $_REQUEST['student_id'])
{
	echo "<FORM action=modloader.php?servicemod=$_REQUEST[servicemod]&student_id=".get_student_id()."&return=".$_REQUEST['return']."&GridOutputCount=".$_REQUEST['GridOutputCount']." method=POST>";
	echo '<BR>';
	PopTable('header',ProgramTitle());
	
	if(get_user('PROFILE')=='teacher')
		$teacher_extra = " AND du.USER_TYPE='teacher' ";
	
	$categories_array = db_request(db_execute("SELECT df.ID,df.DATA_TYPE,du.TITLE,du.SELECT_OPTIONS FROM DISCIPLINE_REPORT_FIELDS df,DISCIPLINE_CUSTOM_FIELDS_ASG du WHERE du.SCHOOL_YEAR='".get_school_year()."' AND du.SCHOOL_ID='".get_school_id()."' AND du.DISCIPLINE_FIELD_ID=df.ID ".$teacher_extra." ORDER BY du.SORT_ORDER"));

	 $linestyle='style="border-bottom:1px inset #'.Preferences('HIGHLIGHT').';"';

	echo '<TABLE>';
	echo '<TABLE width=100% cellpadding=5 cellspacing=0>';
	echo '<TR><TD '.$linestyle.'align=left valign=top width=200><font color=gray>'._('Student Name').'</font></TD><TD '.$linestyle.'>';
	$name = db_request(db_execute("SELECT FIRST_NAME,LAST_NAME,MIDDLE_NAME,NAME_SUFFIX FROM STUDENTS WHERE STUDENT_ID='".get_student_id()."'"));
	echo $name[1]['FIRST_NAME'].'&nbsp;'.($name[1]['MIDDLE_NAME']?$name[1]['MIDDLE_NAME'].' ':'').$name[1]['LAST_NAME'].'&nbsp;'.$name[1]['NAME_SUFFIX'];
	echo '</TD></TR>';

	echo '<TR><TD align=left valign=top width=200 '.$linestyle.'><font color=gray>'._('Reporter').'</font></TD><TD '.$linestyle.'>';
	$users_array = db_request(db_execute("SELECT USER_ID,FIRST_NAME,LAST_NAME,MIDDLE_NAME FROM USERS WHERE SCHOOLS LIKE '%,".get_school_id().",%' AND PROFILE IN ('admin','teacher') ORDER BY LAST_NAME,FIRST_NAME,MIDDLE_NAME"));
	
	echo '<SELECT name=values[USER_ID]>';

	if(get_user('PROFILE') == 'admin')
		echo "<OPTION value=''></OPTION>";

	foreach($users_array as $user)
		echo '<OPTION value='.$user['USER_ID'].((get_user('PROFILE') != 'admin' && get_user('USER_ID')==$user['USER_ID'])?' SELECTED':'').'>'.$user['LAST_NAME'].', '.$user['FIRST_NAME'].' '.$user['MIDDLE_NAME'].'</OPTION>';
	echo '</SELECT>';
	echo '</TD></TR>';
	echo '<TR><TD align=left valign=top '.$linestyle.'><font color=gray>'._('Incident Date').'</font></TD><TD '.$linestyle.'>';
	echo PrepareDate(get_db_date(),'_values[ENTRY_DATE]');
	echo '</TD></TR>';
	foreach($categories_array as $category)
	{
/*		if($bgcolor==Preferences('HIGHLIGHT_BACKGROUND'))
			$bgcolor = 'ffffff';
		else
			$bgcolor = Preferences('HIGHLIGHT_BACKGROUND');*/

//        echo '<TR BGCOLOR=#'.$bgcolor.'><td colspan=2 background=themes/'.Preferences('HIGHLIGHT_FRAME').'/bottom_shadow.png height=20> </td></tr>';

		echo '<TR>';

		echo '<TD align=left valign=top width=150  '.$linestyle.'><font color=gray>'.$category['TITLE'].'</font></TD><TD '.$linestyle.'>';
		switch($category['DATA_TYPE'])
		{
			case 'text':
				echo '<INPUT type=TEXT name=values[CATEGORY_'.$category['ID'].'] maxlength=255>';
			break;
	
			case 'numeric':
				echo '<INPUT type=TEXT name=values[CATEGORY_'.$category['ID'].'] size=4 maxlength=10>';
			break;
	
			case 'textarea':
				echo '<TEXTAREA name=values[CATEGORY_'.$category['ID'].'] rows=4 cols=30></TEXTAREA>';
			break;
	
			case 'checkbox':
				echo '<INPUT type=CHECKBOX name=values[CATEGORY_'.$category['ID'].'] value=Y>';
			break;
			
			case 'date'://get_db_date()
				echo PrepareDate('','_values[CATEGORY_'.$category['ID'].']',true);
			break;
			
			case 'multiple_checkbox':
				$category['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$category['SELECT_OPTIONS']));
				$options = explode("\r",$category['SELECT_OPTIONS']);
				
				echo '<TABLE border=0 cellpadding=2><TR>';
				$i = 0;
				foreach($options as $option)
				{
					if($i%3==0)
						echo '</TR><TR>';
					echo '<TD><INPUT type=checkbox id="mcvalues[CATEGORY_'.$category['ID'].']'.$i.'" name=values[CATEGORY_'.$category['ID'].'][] value="'.str_replace('"','&quot;',$option).'"><small><label for="mcvalues[CATEGORY_'.$category['ID'].']'.$i.'">'.$option.'</label></small></TD>';
					$i++;
				}
				echo '</TR></TABLE>';      
			break;
			
			case 'multiple_radio':
				$category['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$category['SELECT_OPTIONS']));
				$options = explode("\r",$category['SELECT_OPTIONS']);
				
				echo '<TABLE border=0 cellpadding=3><TR>';
				$i = 0;
				foreach($options as $option)
				{
					if($i%3==0)
						echo '</TR><TR>';
					echo '<TD><INPUT type=radio id="mrvalues[CATEGORY_'.$category['ID'].']'.$i.'" name=values[CATEGORY_'.$category['ID'].'] value="'.str_replace('"','&quot;',$option).'"><small><label for="mrvalues[CATEGORY_'.$category['ID'].']'.$i.'">'.str_replace('<','&lt;',$option).'</label></small></TD>';
					$i++;
				}
				echo '</TR></TABLE>';
			break;

			case 'select':
				$category['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$category['SELECT_OPTIONS']));
				$options = explode("\r",$category['SELECT_OPTIONS']);
				
				echo '<SELECT name=values[CATEGORY_'.$category['ID'].']><OPTION value="">N/A';
				foreach($options as $option)
				{
					echo '<OPTION value="'.str_replace('"','&quot;',$option).'">'.$option.'</OPTION>';
				}
			break;
		}
		echo '</TD></TR>';
	}
	echo '</TABLE>';
	if (get_user('PROFILE') == 'teacher')
	    echo '<CENTER><INPUT type=submit value='._('Refer&nbsp;to&nbsp;Administrator').'></CENTER>';
	else
	    echo '<CENTER><INPUT type=submit value='._('Submit').'></CENTER>';	
	PopTable('footer');
	echo '</FORM>';
}
?>
