<?php
if(is_apply_school() == 'Y')
{
	DisplayHeader('.:'.ProgramTitle().':.');
	DisplayHeader(ErrorMessage(array(_('Student addition is not allowed when school in admission mode.')),'fatal'),' &nbsp;');
}
//die('kkkkkk');
unset($tmp_REQUEST);
unset($_SESSION['_REQUEST_vars']);
unset($_SESSION['user_id']);
unset($_SESSION['customValues']);
$_REQUEST['student_id'] = 'new';
$_REQUEST['include'] = 'stugeneralinfotab';
//Checking if student already exist and adding an enrollment record for him if he is not already enrolled.
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
$_REQUEST['CREATEPDF'] = true;
	if (!isset($_GET['enrollment']))
	{
//		$student_exist_array = db_request(db_execute("SELECT STUDENT_ID, ".FormatNames()." AS FULL_NAME, CFIELD_50004 AS BirthDate, CFIELD_50003 AS SSN FROM STUDENTS s WHERE FIRST_NAME = '".$_REQUEST['first_name']."' AND LAST_NAME = '".$_REQUEST['last_name']."'"));
		$selected_student = simplify_array($student_exist_array);

		if (!empty($student_exist_array))
		{
			//BOF STUDENT IMAGE
			if($StudentPicturesColumn && $StudentPicturesColumn!='STUDENT_ID')
			{
				$photo_name = db_request(db_execute("SELECT ".$StudentPicturesColumn." AS NAME FROM STUDENTS WHERE STUDENT_ID='".$selected_student['STUDENT_ID']."'"));
				$student_exist_array[1]['PICTURES_COLUMN'] = $photo_name = $photo_name[1]['NAME'];

				$title = db_request(db_execute("SELECT TITLE FROM STUDENT_FIELDS WHERE ID = '".substr($StudentPicturesColumn,7)."'"));
				$title = $title[1]['TITLE'];
			}
			else
				$photo_name = $selected_student['STUDENT_ID'];

			if($StudentPicturesPath && ($file = @fopen($StudentPicturesPath.($school_year = get_school_year()).'/'.$photo_name.($jpg = '.JPG'),'r') || $file = @fopen($StudentPicturesPath.($school_year = get_school_year()).'/'.$photo_name.($jpg = '.jpg'),'r') || $file = @fopen($StudentPicturesPath.($school_year = (get_school_year()-1)).'/'.$photo_name.($jpg = '.JPG'),'r') || $file = @fopen($StudentPicturesPath.($school_year = (get_school_year()-1)).'/'.$photo_name.($jpg = '.jpg'),'r')))
			{
				if($_REQUEST['CREATEPDF'] && substr($StudentPicturesPath,0,7)!='http://')
				{
					if($_SERVER['HTTPS']=='on')
						$http = 'https://';
					else
						$http = 'http://';

					if(!$_SERVER['SCRIPT_URI'])
						$uri = $http.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
					else
						$uri = $_SERVER['SCRIPT_URI'];
					$uri = substr($uri,0,strrpos($uri,'/')).'/';
				}
				else
					$uri = '';

				$student_exist_array[1]['STUDENT_PHOTO'] = $uri.$StudentPicturesPath.$school_year.'/'.$photo_name.$jpg;
				fclose($file);
			}
			//EOF STUDENT IMAGE

			$column_name = array('STUDENT_ID'=>'ID number','FULL_NAME'=>'Full Name','BIRTHDATE'=>'Date of Birth','SSN'=>'Social Security');

			if ($student_photo)
				$column_name['STUDENT_PHOTO'] = 'STUDENT_PHOTO';

			if ($title)
				$column_name['PICTURES_COLUMN'] = $title;

			$school_enrollment = db_request(db_execute("SELECT s.TITLE FROM SCHOOLS s, STUDENT_REGISTRATION se WHERE se.STUDENT_ID = '".$selected_student['STUDENT_ID']."' AND se.SCHOOL_ID = s.ID AND se.SCHOOL_YEAR = '".get_school_year()."' AND ('".get_db_date()."' BETWEEN se.START_DATE AND se.END_DATE OR se.END_DATE IS NULL)"));
			if (!empty($school_enrollment))
			{
				$student_exist_array[1]['ENROLLED_IN'] = $school_enrollment[1]['TITLE'];
				$column_name['ENROLLED_IN'] = 'Enrolled in';
			}
			else
			{
				$column_name['ENROLL'] = 'Enroll';
				$student_exist_array[1]['ENROLL'] = get_school(get_school_id());
			}

			//Formating birthdate
			$student_exist_array[1]['BIRTHDATE'] = ProperDate($selected_student['BIRTHDATE']);
			$column_name['HEADER'] = 'header';
			$student_exist_array[1]['HEADER'] = _('A student with this name is already enrolled in the system');
			AJAXGridoutput($student_exist_array,$column_name,'','',false,false);
		}
	}
	else
	{
		db_execute("INSERT INTO STUDENT_REGISTRATION(ID, SCHOOL_YEAR, SCHOOL_ID, STUDENT_ID, START_DATE) VALUES(".db_seq_nextval('STUDENT_REGISTRATION_SEQ').",'".get_school_year()."', '".get_school_id()."', '".$_GET['ID']."', '".get_db_date()."')");

		while(@ob_end_clean());

		echo '<?xml version="1.0" standalone="yes"?>';
		echo '<xml>';
		echo '<result>success</result>';
		echo '</xml>';
	}
	exit();
}
echo '<script type="text/javascript" src="js/addstud.js"></script>';
echo '<script type="text/javascript">chkusr_addHandlers();</script>';
include($staticpath.'modules/mod_students/Students.php');
?>