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

DisplayHeader(".:".ProgramTitle().":.");
$addition = array();
Widgets('course');
$exams_array = db_request(db_execute("SELECT ID,TITLE,SHORT_NAME,MAX_SCORE FROM EVALUATION_TESTS WHERE SCHOOL_YEAR='".get_school_year()."'"));

// $current_extra IS SET TO $addition SO IT WILL BE AFFECTED BY Widgets()
// $current_extra is the extra information for the query that will return the current_array data .. $addition produces the actual list
$current_extra = $addition;
$addition['columns_after'] = $addition['functions'] = array();

if(!$_REQUEST['student_id'] && !get_student_id())
{
	switch(substr($_REQUEST['score_id'],0,1))
	{
		case '':
			if(count($exams_array))
			{
				foreach($exams_array as $exam)
				{
					$addition['SELECT'] .= ',NULL AS SCORE_E'.$exam['ID'];
					$addition['columns_after'] += array('SCORE_E'.$exam['ID']=>$exam['SHORT_NAME']);
					$addition['functions'] += array('SCORE_E'.$exam['ID']=>'_makeScore');			
				}
	
				$current_extra['SELECT'] .= ',asco.TYPE'.CCAT.'asco.SCORE_ID AS SCORE_ID,asco.SCORE';
				$current_extra['FROM'] .= ',EVALUATION_MARKPOINTS asco,EVALUATION_TESTS ae ';
				$current_extra['WHERE'] .= " AND asco.STUDENT_ID=ssm.STUDENT_ID AND asco.SCORE_ID=ae.ID AND asco.TYPE='E' AND ae.SCHOOL_YEAR=ssm.SCHOOL_YEAR";
				$current_extra['group'] = array('STUDENT_ID','SCORE_ID');
			}
		break;
		
		case 'E':
			$exam_short_name = db_request(db_execute("SELECT SHORT_NAME FROM EVALUATION_TESTS WHERE ID='".substr($_REQUEST['score_id'],1)."'"));
			$exam_short_name = $exam_short_name[1]['SHORT_NAME'];
			
			$addition['SELECT'] .= ',NULL AS SCORE_'.$_REQUEST['score_id'];
			$addition['columns_after'] += array('SCORE_'.$_REQUEST['score_id']=>'<i>'.$exam_short_name.'</i> - Total');
			$addition['functions'] += array('SCORE_'.$_REQUEST['score_id']=>'_makeScore');
	
			$sections_array = db_request(db_execute("SELECT ID,TITLE,SHORT_NAME,MAX_SCORE FROM EVALUATION_TESTPARTS WHERE EXAM_ID='".substr($_REQUEST['score_id'],1)."'"));
			if(count($sections_array))
			{
				foreach($sections_array as $section)
				{
					$addition['SELECT'] .= ',NULL AS SCORE_S'.$section['ID'];
					$addition['columns_after'] += array('SCORE_S'.$section['ID']=>$section['SHORT_NAME']);
					$addition['functions'] += array('SCORE_S'.$section['ID']=>'_makeScore');			
				}
			}
	
			$current_extra['SELECT'] .= ',asco.TYPE'.CCAT.'asco.SCORE_ID AS SCORE_ID,asco.SCORE';
			$current_extra['FROM'] .= ',EVALUATION_MARKPOINTS asco,EVALUATION_TESTPARTS ae ';
			$current_extra['WHERE'] .= " AND asco.STUDENT_ID=ssm.STUDENT_ID AND ((asco.SCORE_ID=ae.ID AND asco.TYPE='S' AND ae.EXAM_ID='".substr($_REQUEST['score_id'],1)."') OR asco.SCORE_ID='".substr($_REQUEST['score_id'],1)."')";
			$current_extra['group'] = array('STUDENT_ID','SCORE_ID');
			
		break;
	}
}
else
{
	$addition['new'] = true;
	Search('student_id');
	$_REQUEST['stuid'] = get_student_id();

	DisplayHeader('Click "Back to Student List" to return to your previous search.');
	$_REQUEST['search_servicefunction'] = 'list';
	$sections_array = db_request(db_execute("SELECT sec.EXAM_ID,sec.ID,sec.TITLE,sec.MAX_SCORE FROM EVALUATION_TESTPARTS sec,EVALUATION_TESTS ae WHERE sec.EXAM_ID=ae.ID AND ae.SCHOOL_YEAR='".get_school_year()."'"),array(),array('EXAM_ID'));
	foreach($exams_array as $exam)
	{
		$columns['SCORE_E'.$exam['ID']] = '<i>'.$exam['SHORT_NAME'].'</i>';
		$addition['SELECT'] .= ',NULL AS SCORE_E'.$exam['ID'];
		$addition['functions']['SCORE_E'.$exam['ID']] = '_makeScore';
		foreach($sections_array[$exam['ID']] as $section)
		{
			$columns['SCORE_S'.$section['ID']] = $section['TITLE'];
			$addition['SELECT'] .= ',NULL AS SCORE_S'.$section['ID'];
			$addition['functions']['SCORE_S'.$section['ID']] = '_makeScore';
		}
	}

	$current_extra['SELECT'] .= ',asco.TYPE'.CCAT.'asco.SCORE_ID AS SCORE_ID,asco.SCORE';
	$current_extra['FROM'] .= ',EVALUATION_MARKPOINTS asco ';
	$current_extra['WHERE'] .= " AND asco.STUDENT_ID=ssm.STUDENT_ID AND asco.SCHOOL_YEAR=ssm.SCHOOL_YEAR";
	$current_extra['group'] = array('SCORE_ID');
}

$current_array = get_student_list($current_extra);

if($_REQUEST['values'] && $_POST['values'])
{
	foreach($_REQUEST['values'] as $student_id=>$scores)
	{
		foreach($scores as $score_id=>$score)
		{
			$sql = false;
			$type = substr($score_id,0,1);
			$score_type_id = $score_id;
			$score_id = substr($score_id,1);
			
			if($current_array[$student_id][$score_type_id])
				$sql = "UPDATE EVALUATION_MARKPOINTS SET SCORE='".ereg_replace('[^0-9]','',$score)."' WHERE STUDENT_ID='$student_id' AND SCORE_ID='$score_id' AND TYPE='$type'";
			elseif($score!=null)
				$sql = "INSERT INTO EVALUATION_MARKPOINTS (STUDENT_ID,SCORE_ID,TYPE,SCORE) values('$student_id','$score_id','$type','".$score."')";
			if($sql)
				db_execute($sql);
		}
	}
	unset($_REQUEST['values']);
	unset($_SESSION['_REQUEST_vars']['values']);
	$current_array = get_student_list($current_extra);
}

if($_REQUEST['search_servicefunction']=='list')
{
	$select = '<SELECT name=score_id onchange="this.form.submit();"><OPTION value="">All Exams</OPTION>';
	
	if(count($exams_array))
	{
		foreach($exams_array as $exam)
			$select .= '<OPTION value=E'.$exam['ID'].(($_REQUEST['score_id']=='E'.$exam['ID'])?' SELECTED':'').'>'.$exam['TITLE'].' ('.$exam['SHORT_NAME'].')</OPTION>';
	}
	$select .= '</SELECT>';
	
	echo "<FORM action=".SetRequestOptions().' method=POST>';
	DisplayHeader($select,$submitButton->createIButton('Update'));
}

Search('student_id',$addition);

if($_REQUEST['search_servicefunction']=='list')
{
	echo '<CENTER>'.$submitButton->createIButton('Update').'</CENTER>';
	echo '</FORM>';
}

function _makeScore($value,$column)
{	global $THIS_array,$current_array;

	return TextInput($current_array[$THIS_array['STUDENT_ID']][substr($column,6)][1]['SCORE'],'values['.$THIS_array['STUDENT_ID'].']['.substr($column,6).']','',' size=4 maxlength=7 tabindex='.$tabindex);
}

?>