<?php



//this page saves updated profile
//if the person is a new student


$errors = array();
$info = array();
$info_success = array();


//collect the qlk to pass it for fliping
if(isset($_POST['qlk']))
{
	$qlk = $_POST['qlk'];
}





	////collect the  optional tetiary school1
	$school1 = (!empty($_POST['school1']))? (mysqli_real_escape_string($dbc,$_POST['school1'])): NULL;
	$_SESSION['s5'] = $school1;
	
	
	  ////collect the  optional tetiary start year 1
	$sy1 = (!empty($_POST['start_year1']))? (mysqli_real_escape_string($dbc,$_POST['start_year1'])): NULL;
	$_SESSION['sy5'] = $sy1;
	
	
	  ////collect the  optional tetiary end year 1
	$ey1 = (!empty($_POST['end_year1']))? (mysqli_real_escape_string($dbc,$_POST['end_year1'])): NULL;
	$_SESSION['ey5'] = $ey1;
	
	
	
	  ////collect the  optional tetiary cert 
	$c1 = (!empty($_POST['cert1']))? (mysqli_real_escape_string($dbc,$_POST['cert1'])): NULL;
	$_SESSION['c5'] = $c1;
	
	//when NCE 
	if($c1 == 2){
		$sub_1 = (!empty($_POST['sub_1']))? (mysqli_real_escape_string($dbc,$_POST['sub_1'])): NULL;
		$_SESSION['sub_1'] = $sub_1;

		$grd_1 = (!empty($_POST['grd_1']))? (mysqli_real_escape_string($dbc,$_POST['grd_1'])): NULL;
		$_SESSION['grd_1'] = $grd_1;

		$sub_2 = (!empty($_POST['sub_2']))? (mysqli_real_escape_string($dbc,$_POST['sub_2'])): NULL;
		$_SESSION['sub_2'] = $sub_2;

		$grd_2 = (!empty($_POST['grd_2']))? (mysqli_real_escape_string($dbc,$_POST['grd_2'])): NULL;
		$_SESSION['grd_2'] = $grd_2;

		$sub_3 = (!empty($_POST['sub_3']))? (mysqli_real_escape_string($dbc,$_POST['sub_3'])): NULL;
		$_SESSION['sub_3'] = $sub_3;

		$grd_3 = (!empty($_POST['grd_3']))? (mysqli_real_escape_string($dbc,$_POST['grd_3'])): NULL;
		$_SESSION['grd_3'] = $grd_3;

		// $sub_4 = (!empty($_POST['sub_4']))? (mysqli_real_escape_string($dbc,$_POST['sub_4'])): NULL;
		// $_SESSION['sub_4'] = $sub_4;

		// $grd_4 = (!empty($_POST['grd_4']))? (mysqli_real_escape_string($dbc,$_POST['grd_4'])): NULL;
		// $_SESSION['grd_4'] = $grd_4;

	}
	
	
	  ////collect the  optional tetiary result 4
	$r1 = (!empty($_POST['result1']))? (mysqli_real_escape_string($dbc,$_POST['result1'])): NULL;
	$_SESSION['r5'] = $r1;
	
	
	
	
	
	
	////collect the  optional tetiary school2
	$school2 = (!empty($_POST['school2']))? (mysqli_real_escape_string($dbc,$_POST['school2'])): NULL;
	$_SESSION['s6'] = $school2;
	
	
	  ////collect the  optional tetiary start year 2
	$sy2 = (!empty($_POST['start_year2']))? (mysqli_real_escape_string($dbc,$_POST['start_year2'])): NULL;
	$_SESSION['sy6'] = $sy2;
	
	
	  ////collect the  optional tetiary end year 2
	$ey2 = (!empty($_POST['end_year2']))? (mysqli_real_escape_string($dbc,$_POST['end_year2'])): NULL;
	$_SESSION['ey6'] = $ey2;
	
	
	
	  ////collect the  optional tetiary cert 
	$c2 = (!empty($_POST['cert2']))? (mysqli_real_escape_string($dbc,$_POST['cert2'])): NULL;
	$_SESSION['c6'] = $c2;
	
	//when NCE 
	if($c2 == 2){
		$sub2_1 = (!empty($_POST['sub2_1']))? (mysqli_real_escape_string($dbc,$_POST['sub2_1'])): NULL;
		$_SESSION['sub2_1'] = $sub2_1;

		$grd2_1 = (!empty($_POST['grd2_1']))? (mysqli_real_escape_string($dbc,$_POST['grd2_1'])): NULL;
		$_SESSION['grd2_1'] = $grd2_1;

		$sub2_2 = (!empty($_POST['sub2_2']))? (mysqli_real_escape_string($dbc,$_POST['sub2_2'])): NULL;
		$_SESSION['sub2_2'] = $sub2_2;

		$grd2_2 = (!empty($_POST['grd2_2']))? (mysqli_real_escape_string($dbc,$_POST['grd2_2'])): NULL;
		$_SESSION['grd2_2'] = $grd2_2;

		$sub2_3 = (!empty($_POST['sub2_3']))? (mysqli_real_escape_string($dbc,$_POST['sub2_3'])): NULL;
		$_SESSION['sub2_3'] = $sub2_3;

		$grd2_3 = (!empty($_POST['grd2_3']))? (mysqli_real_escape_string($dbc,$_POST['grd2_3'])): NULL;
		$_SESSION['grd2_3'] = $grd2_3;

		// $sub2_4 = (!empty($_POST['sub2_4']))? (mysqli_real_escape_string($dbc,$_POST['sub2_4'])): NULL;
		// $_SESSION['sub2_4'] = $sub2_4;

		// $grd2_4 = (!empty($_POST['grd2_4']))? (mysqli_real_escape_string($dbc,$_POST['grd2_4'])): NULL;
		// $_SESSION['grd2_4'] = $grd2_4;

	}
	
	
	
	  ////collect the  optional tetiary result 4
	$r2 = (!empty($_POST['result2']))? (mysqli_real_escape_string($dbc,$_POST['result2'])): NULL;
	$_SESSION['r6'] = $r2;
	
	
	
	
	////collect the  optional tetiary school2
	$school3 = (!empty($_POST['school3']))? (mysqli_real_escape_string($dbc,$_POST['school3'])): NULL;
	$_SESSION['s7'] = $school3;
	
	
	  ////collect the  optional tetiary start year 2
	$sy3 = (!empty($_POST['start_year3']))? (mysqli_real_escape_string($dbc,$_POST['start_year3'])): NULL;
	$_SESSION['sy7'] = $sy3;
	
	
	  ////collect the  optional tetiary end year 2
	$ey3 = (!empty($_POST['end_year3']))? (mysqli_real_escape_string($dbc,$_POST['end_year3'])): NULL;
	$_SESSION['ey7'] = $ey3;
	
	
	
	  ////collect the  optional tetiary cert 
	$c3 = (!empty($_POST['cert3']))? (mysqli_real_escape_string($dbc,$_POST['cert3'])): NULL;
	$_SESSION['c7'] = $c3;
	
	
//when NCE 
	if($c3 == 2){
		$sub3_1 = (!empty($_POST['sub3_1']))? (mysqli_real_escape_string($dbc,$_POST['sub3_1'])): NULL;
		$_SESSION['sub3_1'] = $sub3_1;

		$grd3_1 = (!empty($_POST['grd3_1']))? (mysqli_real_escape_string($dbc,$_POST['grd3_1'])): NULL;
		$_SESSION['grd3_1'] = $grd3_1;

		$sub3_2 = (!empty($_POST['sub3_2']))? (mysqli_real_escape_string($dbc,$_POST['sub3_2'])): NULL;
		$_SESSION['sub3_2'] = $sub3_2;

		$grd3_2 = (!empty($_POST['grd3_2']))? (mysqli_real_escape_string($dbc,$_POST['grd3_2'])): NULL;
		$_SESSION['grd3_2'] = $grd3_2;

		$sub3_3 = (!empty($_POST['sub3_3']))? (mysqli_real_escape_string($dbc,$_POST['sub3_3'])): NULL;
		$_SESSION['sub3_3'] = $sub3_3;

		$grd3_3 = (!empty($_POST['grd3_3']))? (mysqli_real_escape_string($dbc,$_POST['grd3_3'])): NULL;
		$_SESSION['grd3_3'] = $grd3_3;

		// $sub3_4 = (!empty($_POST['sub3_4']))? (mysqli_real_escape_string($dbc,$_POST['sub3_4'])): NULL;
		// $_SESSION['sub3_4'] = $sub3_4;

		// $grd3_4 = (!empty($_POST['grd3_4']))? (mysqli_real_escape_string($dbc,$_POST['grd3_4'])): NULL;
		// $_SESSION['grd3_4'] = $grd3_4;

	}

	
	  ////collect the  optional tetiary result 4
	$r3 = (!empty($_POST['result3']))? (mysqli_real_escape_string($dbc,$_POST['result3'])): NULL;
	$_SESSION['r7'] = $r3;
	
	
	
		



////finaly get security id
 $user_id = get_user_id();
//
//
//$security_id = get_security_id($user_id);
//


 
 
 
if(empty($errors))
{
	//destroy session if there is no error
	
	unset($_SESSION['s5']); //clear school 5
	unset($_SESSION['sy5']); //clearschool year 5
	unset($_SESSION['ey5']); //clear school end year 5
	unset($_SESSION['c5']); //clear school cert 5
	unset($_SESSION['r5']); //clear school result 5
	
	
	unset($_SESSION['s6']); //clear school 6
	unset($_SESSION['sy6']); //clearschool year 6
	unset($_SESSION['ey6']); //clear school end year 6
	unset($_SESSION['c6']); //clear school cert 6
	unset($_SESSION['r6']); //clear school result 6
	
	
	unset($_SESSION['s7']); //clear school 7
	unset($_SESSION['sy7']); //clearschool year 7
	unset($_SESSION['ey7']); //clear school end year 7
	unset($_SESSION['c7']); //clear school cert 7
	unset($_SESSION['r7']); //clear school result 7
	
	
	
	/* $q = "UPDATE `applicants_institutions` 
			       SET 
				   `school`='$school1',`cert1`='$c1',`start_year`='$sy1',`end_year1`='$ey1',`result1`='$r1',
				   `school2`='$school2',`cert2`='$c2',`start_year2`='$sy2',`end_year2`='$ey2',`result2`='$r2',
				   `school3`='$school3',`cert3`='$c3',`result3`='$r3',`start_year3`='$sy3',`end_year3`='$ey3'
				  
			 WHERE user_id = '$user_id'";*/

		// when higher qual does not exist
		if (user_id_exist_app_instutions($user_id)) {
		 //where empty
			delete_user_records_app_instutions(0);
			//clear previous records
			delete_user_records_app_instutions($user_id);

		}
			
		//add new records when  nce
		//if ($c1 == 2 || $c2 !== 2  || $c3 !== 2) {
			

		// compute result
		//add new records when nce
		if ($c1 == 2 ) {
			
			// compute result
			$res1 = 5;
			$q1 = "INSERT INTO 
						applicants_institutions ( user_id, school, cert, start_year,end_year,
						result, nce_type, nce_subject_1, nce_grade_1,nce_subject_2, nce_grade_2,
						nce_subject_3, nce_grade_3,nce_subject_4, nce_grade_4,date_added)
						
						VALUES ('$user_id', '$school1','$c1','$sy1','$ey1','$res1', '1', '$sub_1', 
						'$grd_1', '$sub_2','$grd_2', '$sub_3',	'$grd_3', '$sub_4','$grd_4',NOW())";

			
			$r = mysqli_query($dbc,$q1);
		}
		//add new records when not nce
		
		elseif ($c1 !== 2 ) {

			$res1 = $r1;

			$q1 = "INSERT INTO 
						applicants_institutions ( user_id, school, cert, start_year,end_year,
						result,nce_type,date_added)
						
						
						VALUES ('$user_id', '$school1','$c1','$sy1','$ey1','$res1',1,NOW())";

			
			$r = mysqli_query($dbc,$q1);
		}
		

		//add new records when nce
		if ($c2 == 2 ) {
			
			// compute result
			$res2 = 3;
			$q1 = "INSERT INTO 
						applicants_institutions ( user_id, school, cert, start_year,end_year,
						result, nce_type, nce_subject_1, nce_grade_1,nce_subject_2, nce_grade_2,
						nce_subject_3, nce_grade_3,nce_subject_4, nce_grade_4,date_added)
						
						VALUES ('$user_id', '$school2','$c2','$sy2','$ey2','$res2', '2', '$sub2_1', 
						'$grd2_1', '$sub2_2','$grd2_2', '$sub2_3',	'$grd2_3', '$sub2_4','$grd2_4',NOW())";

			
			$r = mysqli_query($dbc,$q1);
		}elseif ($c2 !== 2 ) {

			$res2 = $r2;

			$q1 = "INSERT INTO 
						applicants_institutions ( user_id, school, cert, start_year,end_year,
						result,nce_type,date_added)
						
						
						VALUES ('$user_id', '$school2','$c2','$sy2','$ey2','$res2',2,NOW())";

			
			$r = mysqli_query($dbc,$q1);
		}

		//add new records when nce
		if ($c3 == 2 ) {
			
			// compute result
			$res3 = 4;
			$q1 = "INSERT INTO 
						applicants_institutions ( user_id, school, cert, start_year,end_year,
						result, nce_type, nce_subject_1, nce_grade_1,nce_subject_2, nce_grade_2,
						nce_subject_3, nce_grade_3,nce_subject_4, nce_grade_4,date_added)
						
						VALUES ('$user_id', '$school3','$c3','$sy3','$ey3','$res3', '3', '$sub3_1', 
						'$grd3_1', '$sub3_2','$grd3_2', '$sub3_3',	'$grd3_3', '$sub3_4','$grd3_4',NOW())";

			
			$r = mysqli_query($dbc,$q1);
		}elseif ($c3 !== 2 ) {

			$res3 = $r3;
			$q1 = "INSERT INTO 
						applicants_institutions ( user_id, school, cert, start_year,end_year,
						result,nce_type,date_added)
						
						
						VALUES ('$user_id', '$school3','$c3','$sy3','$ey3','$res3',3,NOW())";

			
			$r = mysqli_query($dbc,$q1);
		}
		//}

								
							
		//$r = mysqli_query($dbc,$q);
	
		if($r)
		{
			$info_success['success'] = 'Successfully updated..';
		    header("Location: dashboard.php?act=institution_info&is=".urlencode($info_success['success'])."&qlk=".$encripted_qlk.""); //is - info success
		
				
		}
		else
		{
			$errors['error'] =  'Account not updated due to a system error. We apologize for any inconvenience.';
										
			header("Location: dashboard.php?act=institution_info&error=".urlencode($errors['error'])."&qlk=".$encripted_qlk."");
		}
	
	
	
}
else
{//stay back in update  form


	$errors['error'] =  'Fill the form appropriately, take note of <span class="ast">*</span>.';
										
	header("Location: dashboard.php?act=institution_info&error=".urlencode($errors['error'])."&qlk=".$encripted_qlk."");

}



?>