<?php
echo GetBaseJavaFunc();
include_once($staticpath.'classes/InputButton.php');
$submitButton = new InputButton;

if($_REQUEST['tables'] && $_POST['tables'] && AllowEdit())
{
	foreach($_REQUEST['tables'] as $table=>$values)
	{
		foreach($values as $id=>$columns)
		{
			if($columns['DEFAULT_SCALE'] == 'Y')
				db_execute("UPDATE GRADES_CARD_TYPES SET DEFAULT_SCALE=NULL WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'");

			if($columns['DEFAULT_BREAKOFF'])
				$columns['DEFAULT_BREAKOFF'] = $columns['DEFAULT_BREAKOFF'];
			if($id!='new')
			{
				$sql = "UPDATE $table SET ";
								
				foreach($columns as $column=>$value)
				{
					$sql .= $column."='".str_replace("\'","''",$value)."',";
				}
				$sql = substr($sql,0,-1) . " WHERE ID='$id'";
				db_execute($sql);
			}
			else
			{
				$sql = "INSERT INTO $table ";
	
				if($columns['TITLE'])
				{
					//$new_id = db_request(db_execute("SELECT ".db_seq_nextval($table.'_SEQ').' AS ID'));
					//$new_id = $new_id[1]['ID'];
	
					$fields = 'ID,SCHOOL_ID,SCHOOL_YEAR,';
					$values = "".db_seq_nextval($table.'_SEQ').",'".get_school_id()."','".get_school_year()."',";
					
					if($table=='GRADES_CARD_SETTINGS')
					{
						$fields .= 'SCALE_ID,';
						$values .= "'".$_REQUEST['scale_id']."',";
					}
				}

				$go = 0;
				if ($columns) {

					foreach($columns as $column=>$value)
					{
						if($value || $value==='0')
						{
							$fields .= $column.',';
							$values .= "'".str_replace("\'","''",$value)."',";
							
							if($column!='CREDITS'){
									$go = true;
								}
							}
					}
					if($go){
						$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
						db_execute($sql);
						if($table=='GRADES_CARD_TYPES')
							$_REQUEST['scale_id'] = db_seq_currval($table.'_SEQ');
					}

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

unset($_SESSION['_REQUEST_vars']['scale_id']);

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


if($_REQUEST['servicefunction']=='remove')
{
	if($_REQUEST['table']=='GRADES_CARD_SETTINGS')
		$code_type = _('this report card grade');
	elseif($_REQUEST['table']=='GRADES_CARD_COMMENTS')
		$code_type = _('this report card comment');
	elseif($_REQUEST['table']=='GRADES_CARD_TYPES')
		$code_type = _('this grading scale');
	
	if($_REQUEST['table']=='GRADES_CARD_TYPES')
	{
		if(DeletePrompt($code_type))
		{
			db_execute("DELETE FROM GRADES_CARD_SETTINGS WHERE SCALE_ID='".$_REQUEST['scale_id']."'");			
			db_execute("DELETE FROM GRADES_CARD_TYPES WHERE ID='".$_REQUEST['scale_id']."'");
			unset($_REQUEST['scale_id']);
			unset($_REQUEST['servicefunction']);
		}
	}
	else
	{
		if(DeletePrompt($code_type))
		{
			db_execute("DELETE FROM ".$_REQUEST['table']." WHERE ID='".$_REQUEST['id']."'");
			unset($_REQUEST['servicefunction']);
		}
	}
}

if(!isset($_REQUEST['scale_id']))
{
	$scales_array = db_request(db_execute("SELECT ID FROM GRADES_CARD_TYPES WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."' ORDER BY COALESCE(DEFAULT_SCALE,'N') DESC"));
	if(count($scales_array))
		$_REQUEST['scale_id'] = $scales_array[1]['ID'];
	else
		$_REQUEST['scale_id'] = 'new';
}

if($_REQUEST['servicefunction']!='remove')
{
	echo "<FORM action=modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=update&scale_id=$_REQUEST[scale_id] method=POST>";
//	DisplayHeader('',$submitButton->createSButton(_('Save')));
	echo '<BR>';
	echo '<TABLE width=100%><TR><TD width=50% align='.ALIGN_LEFT.' valign=top>';
	
	//DisplayHeader(_('Report Card Grades'));
	echo '<BR>';
	if($_REQUEST['scale_id']!=='new')
	{
		$sql = "SELECT ID,TITLE,DESCRIPTION,SORT_ORDER,GPA_VALUE,WEIGHTED_GPA_VALUE,CREDITS,DEFAULT_BREAKOFF FROM GRADES_CARD_SETTINGS WHERE SCALE_ID='".$_REQUEST['scale_id']."' AND SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."' ORDER BY SORT_ORDER";
		$QI = db_execute($sql);
		$array = db_request($QI,array('TITLE'=>'_makeGradesInput','DESCRIPTION'=>'_makeGradesInput','SORT_ORDER'=>'_makeGradesInput','GPA_VALUE'=>'_makeGradesInput','WEIGHTED_GPA_VALUE'=>'_makeGradesInput','CREDITS'=>'_makeCheckbox','DEFAULT_BREAKOFF'=>'_makeGradesInput'));
	}

	$columns = array('TITLE'=>_('Grade'),
			 'DESCRIPTION'=>_('Description of Grade'),
			 'GPA_VALUE'=>_('GPA Value'),
			 'WEIGHTED_GPA_VALUE'=>_('Weighted GPA Value'),
			 'SORT_ORDER'=>_('Sort Order'),
			 'CREDITS'=>_('Credit'),
			 'DEFAULT_BREAKOFF'=>_('Default Breakoff'));
	if(SystemPreferences('WEIGHTED_GPA')!='Y')
		unset($columns['WEIGHTED_GPA_VALUE']);
	if(SystemPreferences('USE_CREDITS')!='Y')
		unset($columns['CREDITS']);
	$link['add']['html'] = array('TITLE'=>_makeGradesInput('','TITLE'),'DESCRIPTION'=>_makeGradesInput('','DESCRIPTION'),'GPA_VALUE'=>_makeGradesInput('','GPA_VALUE'),'WEIGHTED_GPA_VALUE'=>_makeGradesInput('','WEIGHTED_GPA_VALUE'),'CREDITS'=>_makeCheckbox('','CREDITS'),'SORT_ORDER'=>_makeGradesInput('','SORT_ORDER'),'DEFAULT_BREAKOFF'=>_makeGradesInput('','DEFAULT_BREAKOFF'));
	$link['remove']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=remove&table=GRADES_CARD_SETTINGS&scale_id=".$_REQUEST['scale_id'];
	$link['remove']['variables'] = array('id'=>'ID');
	
	$categories_array = db_request(db_execute("SELECT ID,TITLE FROM GRADES_CARD_TYPES WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'"));
	foreach($categories_array as $category)
		$tabs[] = array('title'=>$category['TITLE'],'link'=>"modloader.php?servicemod=$_REQUEST[servicemod]&scale_id=".$category['ID'],'align'=>'left');

	if($_REQUEST['scale_id']==='new')
		$tabs[] = array('title'=>button('white_add'),'link'=>"modloader.php?servicemod=$_REQUEST[servicemod]&scale_id=new");
	else
		$tabs[] = array('title'=>button('add'),'link'=>"modloader.php?servicemod=$_REQUEST[servicemod]&scale_id=new");
	
	$_REQUEST['showTitle'] = 'no';
		
	if($_REQUEST['scale_id']!=='new')
	{
		if(count($array)!=0)
		{
			$_SYSTEM['selected_tab'] = "modloader.php?servicemod=$_REQUEST[servicemod]&scale_id=$_REQUEST[scale_id]";
			echo PopTable('header');
			Gridoutput($array,$columns,_('Grades'),_('Amount of Grades'),$link,array(),array('download'=>false,'search'=>false,'xscroll'=>false,'middle'=>WrapTabs($tabs,"modloader.php?servicemod=$_REQUEST[servicemod]&scale_id=$_REQUEST[scale_id]")));
			if($_REQUEST['scale_id']!=0)
				echo '<CENTER>'.button('remove',_('Delete this grading scale'),"modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=remove&scale_id=$_REQUEST[scale_id]&table=GRADES_CARD_TYPES");
		}
		else
		{
			$options = array('search'=>false,'xscroll'=>false);
			$options['middle'] = WrapTabs($tabs,"modloader.php?servicemod=$_REQUEST[servicemod]&scale_id=$_REQUEST[scale_id]");
			echo PopTable('header');
			Gridoutput($array,$columns,'','',$link,array(),$options);
			if($_REQUEST['scale_id']!=0)
				echo '<CENTER>'.button('remove',_('Delete this grading scale'),"modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=remove&scale_id=$_REQUEST[scale_id]&table=GRADES_CARD_TYPES");
		}
	}
	else
	{
		$options = array('search'=>false,'xscroll'=>false);
		$options['middle'] = WrapTabs($tabs,"modloader.php?servicemod=$_REQUEST[servicemod]&scale_id=$_REQUEST[scale_id]");
		$categories_array = db_request(db_execute("SELECT ID,TITLE,DEFAULT_SCALE FROM GRADES_CARD_TYPES WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."' ORDER BY TITLE"),array('TITLE'=>'_makeCategoryTitle','DEFAULT_SCALE'=>'_makeDefault'));
		$columns = array('TITLE'=>_('Grade Scale Title'),'DEFAULT_SCALE'=>_('Default'));
		$link = array();
		$link['add']['html'] = array('TITLE'=>_makeCategoryTitle('','TITLE'),'DEFAULT_SCALE'=>_makeDefault('','DEFAULT_SCALE'));
		$link['remove']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=remove&table=GRADES_CARD_TYPES";
		$link['remove']['variables'] = array('scale_id'=>'ID');
		echo PopTable('header');
		Gridoutput($categories_array,$columns,'','',$link,array(),$options);
	}
	unset($_REQUEST['showTitle']);
	
	echo '</TD><TD  bgcolor=white style="border:1px solid #'.Preferences('HIGHLIGHT').';" width=50% align='.ALIGN_LEFT.' valign=top>';
	
	$sql = "SELECT ID,TITLE,SORT_ORDER FROM GRADES_CARD_COMMENTS WHERE SCHOOL_ID='".get_school_id()."' AND SCHOOL_YEAR='".get_school_year()."' ORDER BY SORT_ORDER";
	$QI = db_execute($sql);
	$array = db_request($QI,array('TITLE'=>'_makeCommentsInput','SORT_ORDER'=>'_makeCommentsInput'));
	
	$columns = array('TITLE'=>_('Comment'),'SORT_ORDER'=>_('Sort Order'));
	$link['add']['html'] = array('TITLE'=>_makeCommentsInput('','TITLE'),'SORT_ORDER'=>_makeCommentsInput('','SORT_ORDER'));
	$link['remove']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=remove&table=GRADES_CARD_COMMENTS";
	$link['remove']['variables'] = array('id'=>'ID');
	
	Gridoutput($array,$columns,_('Comments'),_('Amount of Comments'),$link,array(),array('center'=>false));
	echo PopTable('footer');
	echo '<CENTER>'.$submitButton->createSButton(_('Save')).'</CENTER></TD></TR></TABLE>';
	echo '</FORM>';
}

function _makeCategoryTitle($value,$name)
{	global $THIS_array;

	if($THIS_array['ID'])
		$id = $THIS_array['ID'];
	else
		$id = 'new';

	return TextInput($value,'tables[GRADES_CARD_TYPES]['.$id.']['.$name.']','',$addition);
}

function _makeDefault($value,$name)
{	global $THIS_array;

	if($THIS_array['ID'])
		$id = $THIS_array['ID'];
	else
	{
		$id = 'new';
		$new = true;
	}
	
	return CheckBoxInput($value,'tables[GRADES_CARD_TYPES]['.$id.']['.$name.']','','',$new);
}

function _makeGradesInput($value,$name)
{	global $THIS_array;
	
	if($THIS_array['ID'])
	{
		$id = $THIS_array['ID'];
		$div = true;
	}
	else
		$id = 'new';
	
	if($name=='DEFAULT_BREAKOFF')
	{
		$addition = ' size=3 maxlength=5';
		$suffix = '%';
	}
	elseif($name=='DESCRIPTION')
	{
		$addition = 'size=10 maxlength=100';
		$div = 'force';
	}
	elseif($name!='GPA_VALUE' && $name!='WEIGHTED_GPA_VALUE')
		$addition = 'size=5 maxlength=3';
	else
		$addition = 'size=5 maxlength=6';

	return TextInput(array($value,$value.$suffix),'tables[GRADES_CARD_SETTINGS]['.$id.']['.$name.']','',$addition,$div);
}

function _makeCommentsInput($value,$name)
{	global $THIS_array;
	
	if($THIS_array['ID'])
		$id = $THIS_array['ID'];
	else
		$id = 'new';
	
	return TextInput($value,'tables[GRADES_CARD_COMMENTS]['.$id.']['.$name.']','',$addition);
}

function _makeCheckbox($value,$column)
{	global $THIS_array;

	if($THIS_array['ID'])
		$id = $THIS_array['ID'];
	else
	{
		$id = 'new';
		$value = 'Y';
		$new = true;
	}

	return CheckboxInput($value,'tables[GRADES_CARD_SETTINGS]['.$id.']['.$column.']','','',$new);
}

?>