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

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

if(get_user('PROFILE')=='teacher')
	$_SYSTEM['allow_edit'] = true;

if($_REQUEST['tables'] && $_POST['tables'] && AllowEdit())
{
	foreach($_REQUEST['tables'] as $table=>$values)
	{
		foreach($values as $id=>$columns)
		{
			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
			{
				if($table=='GRADES_CARD_TESTS' && !$columns['TITLE'])
					continue;

				$sql = "INSERT INTO $table ";
				
				//$new_id = db_request(db_execute("SELECT ".db_seq_nextval($table.'_SEQ').' AS ID'.FROM_DUAL));
				//$new_id = $new_id[1]['ID'];
				
				$fields = 'ID,SCHOOL_ID,SCHOOL_YEAR,SUBJECT_ID,COURSE_ID,COURSE_CREDIT_TITLE,';
				$values = "".db_seq_nextval($table.'_SEQ').",'".get_school_id()."','".get_school_year()."','".$_REQUEST['subject_id']."','".$_REQUEST['course_id']."','".$_REQUEST['course_credits']."',";
				
				if($table=='GRADES_CARD_EVALUATION')
				{
					$fields .= 'STANDARD_ID,';
					$values .= "'".$_REQUEST['standard_id']."',";
				}
				//else
					//$_REQUEST['standard_id'] = $new_id;

				$go = 0;
				foreach($columns as $column=>$value)
				{
					if($value)
					{
						$fields .= $column.',';
						$values .= "'".str_replace("\'","''",$value)."',";
						if($column!='SORT_ORDER')
							$go = true;
					}
				}
				$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
				
				if($go)
				{
					db_execute($sql);
					///Now get the id generated. Right way for mssql.
					if($table!='GRADES_CARD_EVALUATION')
						$_REQUEST['standard_id'] = db_seq_currval($table.'_SEQ');
				}
			}
		}
	}
	unset($_REQUEST['tables']);
	unset($_SESSION['_REQUEST_vars']['tables']);
}

if($_REQUEST['servicefunction']=='delete')
{
	if($_REQUEST['table']=='GRADES_CARD_TESTS')
	{
		if(DeletePrompt(_('Standard and all Benchmarks within the Standard')))
		{
			$benchmarks_array = db_request(db_execute("SELECT ID FROM GRADES_CARD_EVALUATION WHERE STANDARD_ID='".$_REQUEST['standard_id']."'"));
			foreach($benchmarks_array as $benchmark)
			{
				db_execute("DELETE FROM GRADES_CARD_EVALUATION WHERE ID='".$benchmark['ID']."'");
				db_execute("DELETE FROM STUDENT_GRADES_CARD_EVALUATIONS WHERE BENCHMARK_ID='".$benchmark['ID']."'");
			}
			db_execute("DELETE FROM GRADES_CARD_TESTS WHERE ID='".$_REQUEST['standard_id']."'");
			unset($_REQUEST['servicefunction']);
			unset($_REQUEST['standard_id']);
			unset($_SESSION['_REQUEST_vars']['standard_id']);
		}
	}
	else
	{
		if(DeletePrompt(_('Benchmark')))
		{
			db_execute("DELETE FROM GRADES_CARD_EVALUATION WHERE ID='".$_REQUEST['id']."'");
			db_execute("DELETE FROM STUDENT_GRADES_CARD_EVALUATIONS WHERE BENCHMARK_ID='".$_REQUEST['id']."'");
			unset($_REQUEST['servicefunction']);
		}
	}
}

if($_REQUEST['servicefunction']!='delete')
{
	DisplayHeader('',$submitButton->createSButton(_('Save')));
	$_REQUEST['servicefunction'] = 'choose_course';
	if(get_user('PROFILE')=='teacher')
	{
		$course_section_id = get_user_coursesection();
		$course_id = db_request(db_execute("SELECT cp.COURSE_ID,cp.COURSE_CREDIT_TITLE,c.SUBJECT_ID,cp.TITLE,c.TITLE AS COURSE_TITLE,c.SHORT_NAME AS COURSE_NUM,cw.CREDITS,cp.DOES_GRADES,cp.GRADE_TYPE_ID,cp.DOES_GPA as AFFECTS_GPA FROM COURSE_SECTIONS cp,COURSES c,COURSE_CREDITS cw WHERE cw.COURSE_ID=cp.COURSE_ID AND cw.COURSE_CREDIT_TITLE=cp.COURSE_CREDIT_TITLE AND c.COURSE_ID=cp.COURSE_ID AND cp.COURSE_SECTION_ID='".get_user_coursesection()."'"));
		if($course_id[1]['DOES_GRADES']!='Y')
			DisplayHeader(ErrorMessage(array(_('You can not enter grades for this school period.')),'error'),' &nbsp;');
		/*
		$grade_scale_id = $course_id[1]['GRADE_TYPE_ID'];
		$cp_title = $course_id[1]['TITLE'];
		$course_title = $course_id[1]['COURSE_TITLE'];
		$course_num = $course_id[1]['COURSE_NUM'];
		$credits = $course_id[1]['CREDITS'];
		$affects_gpa = $course_id[1]['AFFECTS_GPA'];
		$course_credits = $course_id[1]['COURSE_CREDIT_TITLE'];
		$course_id = $course_id[1]['COURSE_ID'];
		*/
		$_REQUEST['course_id'] = $course_id[1]['COURSE_ID'];
		$_REQUEST['course_credits'] = $course_id[1]['COURSE_CREDIT_TITLE'];
		$_REQUEST['subject_id'] = $course_id[1]['SUBJECT_ID'];
	}
	else
		include($staticpath.'modules/mod_curriculum/ScdCourseProperties.php');

	if(isset($_REQUEST['course_credits']))
	{
		$standards_array = db_request(db_execute("SELECT ID,TITLE FROM GRADES_CARD_TESTS WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."' AND SUBJECT_ID='".$_REQUEST['subject_id']."' AND COURSE_ID='".$_REQUEST['course_id']."' AND COURSE_CREDIT_TITLE='".$_REQUEST['course_credits']."'"));
		if(count($standards_array)==0)
		{
			$_REQUEST['standard_id'] = 'new';
			$help_message = _('Create at least one standard for this class. In a math class, an example of a standard might be "Addition." If this class doesn\'t have multiple groups of benchmarks, just create one standard using the same title as the name of the course. Once you create a standard, you will be able to create benchmarks within this standard.');
		}
		elseif(!$_REQUEST['standard_id'])
			$_REQUEST['standard_id'] = $standards_array[1]['ID'];

		if($_REQUEST['standard_id']!='new')
		{
			$functions = array('TITLE'=>'_makeInput','SORT_ORDER'=>'_makeInput','SUBTITLE'=>'_makeInput');
			$benchmarks_array = db_request(db_execute("SELECT ID,TITLE,SUBTITLE,SORT_ORDER FROM GRADES_CARD_EVALUATION WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."' AND SUBJECT_ID='".$_REQUEST['subject_id']."' AND COURSE_ID='".$_REQUEST['course_id']."' AND COURSE_CREDIT_TITLE='".$_REQUEST['course_credits']."' AND STANDARD_ID='".$_REQUEST['standard_id']."' ORDER BY SORT_ORDER, TITLE"),$functions);
			$count_benchmarks = count($benchmarks_array);
			if($count_benchmarks==0)
			{
				$help_message = _('Create benchmarks in this standard by typing the title in the "Benchmark" field and pressing "Save."  You can change this standard or create new standards by clicking the tab labeled with a plus sign.');
			}
		}

		if(get_user('PROFILE')!='teacher')
			echo '<TD width=25% valign=top>';
		else
		{
			if($help_message)
				DisplayHeader('<TABLE style="display:inline;" width=800><TR><TD>'.$help_message.'</TD></TR></TABLE>');
			echo '<BR>';
		}
		echo '<FORM action=modloader.php?servicemod='.$_REQUEST['servicemod'].'&subject_id='.$_REQUEST['subject_id'].'&course_id='.$_REQUEST['course_id'].'&course_credits='.$_REQUEST['course_credits'].'&standard_id='.$_REQUEST['standard_id'].' method=POST>';
		$link = array();
		$link['add']['html'] = array('TITLE'=>_makeInput('','TITLE'),'SUBTITLE'=>_makeInput('','SUBTITLE'),'SORT_ORDER'=>_makeInput('','SORT_ORDER'));
		$link['remove']['link'] = "modloader.php?servicemod=".$_REQUEST['servicemod'].'&subject_id='.$_REQUEST['subject_id'].'&course_id='.$_REQUEST['course_id'].'&course_credits='.$_REQUEST['course_credits'].'&servicefunction=delete';
		$link['remove']['variables'] = array('id'=>'ID');
		$columns = array('TITLE' =>_('Benchmark'),
			    'SUBTITLE'=>_('Subtitle'),
			    'SORT_ORDER'=>_('Sort Order'));

		foreach($standards_array as $category)
			$tabs[] = array('title'=>$category['TITLE'],'link'=>"modloader.php?servicemod=$_REQUEST[servicemod]&subject_id=$_REQUEST[subject_id]&course_id=$_REQUEST[course_id]&course_credits=$_REQUEST[course_credits]&standard_id=".$category['ID']);
	
		if($_REQUEST['standard_id']==='new')
		{
			$tabs[] = array('title'=>button('white_add'),'link'=>"modloader.php?servicemod=$_REQUEST[servicemod]&subject_id=$_REQUEST[subject_id]&course_id=$_REQUEST[course_id]&course_credits=$_REQUEST[course_credits]&standard_id=new");
			$_SYSTEM['selected_tab'] = "modloader.php?servicemod=$_REQUEST[servicemod]&subject_id=$_REQUEST[subject_id]&course_id=$_REQUEST[course_id]&course_credits=$_REQUEST[course_credits]&standard_id=new";
		}
		else
			$tabs[] = array('title'=>button('add'),'link'=>"modloader.php?servicemod=$_REQUEST[servicemod]&subject_id=$_REQUEST[subject_id]&course_id=$_REQUEST[course_id]&course_credits=$_REQUEST[course_credits]&standard_id=new");
	
		if($_REQUEST['standard_id']!=='new')
		{
			$_SYSTEM['selected_tab'] = "modloader.php?servicemod=$_REQUEST[servicemod]&subject_id=$_REQUEST[subject_id]&course_id=$_REQUEST[course_id]&course_credits=$_REQUEST[course_credits]&standard_id=$_REQUEST[standard_id]";
			if(count($benchmarks_array)==0)
			{
				echo PopTable('header',$tabs);
				Gridoutput($benchmarks_array,$columns,_('Benchmarks'),_('Amount of Benchmarks'),$link,array(),array('download'=>false,'search'=>false));
				echo '<CENTER>'.button('remove',_('Delete this Standard'),"modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=delete&subject_id=$_REQUEST[subject_id]&course_id=$_REQUEST[course_id]&course_credits=$_REQUEST[course_credits]&standard_id=$_REQUEST[standard_id]&table=GRADES_CARD_TESTS");
				echo '<BR><CENTER>'.$submitButton->createSButton(_('Save')).'</CENTER>';
				echo PopTable('footer');
			}
			else
			{
				$options = array('save'=>false,'search'=>false);
				$options['middle'] = WrapTabs($tabs,$_SYSTEM['selected_tab']);
				Gridoutput($benchmarks_array,$columns,'','',$link,array(),$options);
				echo '<BR><CENTER>'.$submitButton->createSButton(_('Save')).'</CENTER>';
			}
		}
		else
		{
			$options = array('search'=>false);
			$options['middle'] = WrapTabs($tabs,$_SYSTEM['selected_tab']);
			$functions['TITLE'] = $functions['SUBTITLE'] = $functions['SORT_ORDER'] = '_makeInput';
			$categories_array = db_request(db_execute("SELECT ID,TITLE,SORT_ORDER,SUBTITLE FROM GRADES_CARD_TESTS WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."' AND SUBJECT_ID='".$_REQUEST['subject_id']."' AND COURSE_ID='".$_REQUEST['course_id']."' AND COURSE_CREDIT_TITLE='".$_REQUEST['course_credits']."' ORDER BY TITLE"),$functions);
			$count_benchmarks = count($categories_array);
			$columns = array('TITLE'=>_('Standard Title'),'SUBTITLE'=>_('Subtitle'),'SORT_ORDER'=>_('Sort Order'));
			$link = array();
			$link['add']['html'] = array('TITLE'=>_makeInput('','TITLE'),'SUBTITLE'=>_makeInput('','SUBTITLE'),'SORT_ORDER'=>_makeInput('','SORT_ORDER'));
			$link['remove']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=delete&subject_id=$_REQUEST[subject_id]&course_id=$_REQUEST[course_id]&course_credits=$_REQUEST[course_credits]&table=GRADES_CARD_TESTS";
			$link['remove']['variables'] = array('standard_id'=>'ID');
			Gridoutput($categories_array,$columns,'','',$link,array(),$options);
			echo '<BR><CENTER>'.$submitButton->createSButton(_('Save')).'</CENTER>';

			/*
			echo PopTable('header',$tabs);
			echo '<CENTER>'._('New Standard Title').' <INPUT type=text name=new_category_title></CENTER>';
			echo '<BR><CENTER>'.$submitButton->createSButton(_('Save')).'</CENTER>';
			echo PopTable('footer');
			*/
		}


		//Gridoutput($benchmarks_array,$columns,_('Benchmark'),_('Benchmarks'),$link,'',$Gridoutput_options);
		echo '</FORM>';
		if(get_user('PROFILE')!='teacher')
			echo '</TD>';
	}
	
	if(get_user('PROFILE')!='teacher')
		echo '</TR></TABLE>';
}

function _makeInput($value,$name)
{	global $THIS_array,$count_benchmarks,$_SYSTEM;

	if($_REQUEST['standard_id']=='new')	
		$table = 'GRADES_CARD_TESTS';
	else
		$table = 'GRADES_CARD_EVALUATION';

	if($THIS_array['ID'])
	{
		$id = $THIS_array['ID'];
		$div = 'force';
	}
	else
	{
		$id = 'new';
		$addition = 'size=15';
		if($name=='SORT_ORDER')
		{
			$div = false;
			$value = $count_benchmarks+1;
		}
	}

	if($name=='SORT_ORDER')
		$addition = 'size=3';
		
	if($name=='TITLE' || $name=='SUBTITLE')
		$addition .= ' class=GridOutput_fieldint';

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


?>