<?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'];
}








//validate the exam_no
if(!empty($_POST['exam_no']))
{
   $exam_no = mysqli_real_escape_string($dbc, $_POST['exam_no']);

   $_SESSION['exam_no'] = $exam_no;

}
else
{


   $errors[] = '<p> * </p>';
}

// //validate the card pin
// if(!empty($_POST['exam_card_1']))
// {
//    $exam_card_1 = mysqli_real_escape_string($dbc, $_POST['exam_card_1']);
//
//    $_SESSION['exam_card_1'] = $exam_card_1;
//
// }
// else
// {
//
//
//    $errors[] = '<p> * </p>';
// }

//validate the card serial no
// if(!empty($_POST['exam_card_SN_1']))
// {
//    $exam_card_SN_1 = mysqli_real_escape_string($dbc, $_POST['exam_card_SN_1']);
//
//    $_SESSION['exam_card_SN_1'] = $exam_card_SN_1;
//
// }
// else
// {
//
//
//    $errors[] = '<p> * </p>';
// }



//validate the olexam1
if(!empty($_POST['olexam1']))
{
   $olexam1 = mysqli_real_escape_string($dbc, $_POST['olexam1']);

   $_SESSION['olexam1'] = $olexam1;

}
else
{
   $errors[] = '<p> * </p>';
}


//validate the eyear1
if(!empty($_POST['eyear1']))
{
   $eyear1 = mysqli_real_escape_string($dbc, $_POST['eyear1']);

   $_SESSION['eyear1'] = $eyear1;

}
else
{
   $errors[] = '<p> * </p>';
}


//validate the emonth1
if(!empty($_POST['emonth1']))
{
   $emonth1 = mysqli_real_escape_string($dbc, $_POST['emonth1']);

   $_SESSION['emonth1'] = $emonth1;

}
else
{
   $errors[] = '<p> * </p>';
}

  //validate the sub1
  if($_POST['sub1'] == '0')
  {
	   $_SESSION['sub1'] = $_POST['sub1']; //pas to the parent page for feed back
	   $errors[] = '<p> * </p>';



  }
  else
  {
	  $sub1= mysqli_real_escape_string($dbc, $_POST['sub1']);
	  $_SESSION['sub1'] = $sub1;

  }



  //validate the grd1
  if($_POST['grd1'] == '0')
  {

	  $_SESSION['grd1'] =  $_POST['grd1'];
	   $errors[] = '<p> * </p>';


  }
  else
  {
	  $grd1 = mysqli_real_escape_string($dbc, $_POST['grd1']);
	  $_SESSION['grd1'] = $grd1;

  }



   //validate the sub2
  if($_POST['sub2'] == '0')
  {
	   $_SESSION['sub2'] = $_POST['sub2']; //pas to the parent page for feed back
	   $errors[] = '<p> * </p>';



  }
  else
  {
	  $sub2= mysqli_real_escape_string($dbc, $_POST['sub2']);
	  $_SESSION['sub2'] = $sub2;

  }



  //validate the grd2
  if($_POST['grd2'] == '0')
  {

	  $_SESSION['grd2'] =  $_POST['grd2'];
	   $errors[] = '<p> * </p>';


  }
  else
  {
	  $grd2 = mysqli_real_escape_string($dbc, $_POST['grd2']);
	  $_SESSION['grd2'] = $grd2;

  }




   //validate the sub3
  if($_POST['sub3'] == '0')
  {
	   $_SESSION['sub3'] = $_POST['sub3']; //pas to the parent page for feed back
	   $errors[] = '<p> * </p>';



  }
  else
  {
	  $sub3= mysqli_real_escape_string($dbc, $_POST['sub3']);
	  $_SESSION['sub3'] = $sub3;

  }



  //validate the grd3
  if($_POST['grd3'] == '0')
  {

	  $_SESSION['grd3'] =  $_POST['grd3'];
	   $errors[] = '<p> * </p>';


  }
  else
  {
	  $grd3 = mysqli_real_escape_string($dbc, $_POST['grd3']);
	  $_SESSION['grd3'] = $grd3;

  }

  //validate the sub4
  if($_POST['sub4'] == '0')
  {
	   $_SESSION['sub4'] = $_POST['sub4']; //pas to the parent page for feed back
	   $errors[] = '<p> * </p>';



  }
  else
  {
	  $sub4= mysqli_real_escape_string($dbc, $_POST['sub4']);
	  $_SESSION['sub4'] = $sub4;

  }



  //validate the grd4
  if($_POST['grd4'] == '0')
  {

	  $_SESSION['grd4'] =  $_POST['grd4'];
	   $errors[] = '<p> * </p>';


  }
  else
  {
	  $grd4 = mysqli_real_escape_string($dbc, $_POST['grd4']);
	  $_SESSION['grd4'] = $grd4;

  }


   //validate the sub5
  if($_POST['sub5'] == '0')
  {
	   $_SESSION['sub5'] = $_POST['sub5']; //pas to the parent page for feed back
	   $errors[] = '<p> * </p>';



  }
  else
  {
	  $sub5= mysqli_real_escape_string($dbc, $_POST['sub5']);
	  $_SESSION['sub5'] = $sub5;

  }



  //validate the grd5
  if($_POST['grd5'] == '0')
  {

	  $_SESSION['grd5'] =  $_POST['grd5'];
	   $errors[] = '<p> * </p>';


  }
  else
  {
	  $grd5 = mysqli_real_escape_string($dbc, $_POST['grd5']);
	  $_SESSION['grd5'] = $grd5;

  }



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


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


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


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



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


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

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


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


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



//validate the card pin
// if(!empty($_POST['exam_card_2']))
// {
//    $exam_card_2 = mysqli_real_escape_string($dbc, $_POST['exam_card_2']);
//
//    $_SESSION['exam_card_2'] = $exam_card_2;
//
// }
//
// //validate the card serial no
// if(!empty($_POST['exam_card_SN_2']))
// {
//    $exam_card_SN_2 = mysqli_real_escape_string($dbc, $_POST['exam_card_SN_2']);
//
//    $_SESSION['exam_card_SN_2'] = $exam_card_SN_2;
//
// }




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



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


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


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


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


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


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


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


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



  //collect the s_sub4
$s_sub4 = (!empty($_POST['s_sub3']))? (mysqli_real_escape_string($dbc,$_POST['s_sub4'])):
 NULL;
$_SESSION['s_sub4'] = $s_sub4;


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


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


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

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


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


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


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



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


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

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


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




////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['exam_no']); //clear exam_no
	unset($_SESSION['olexam1']); //clear olexam1
	unset($_SESSION['eyear1']); //clear eyear1
	unset($_SESSION['emonth1']); //clear emonth1
	unset($_SESSION['sub1']); //clear sub1
	unset($_SESSION['grd1']); //clear grd1
	unset($_SESSION['sub2']); //clear sub1
	unset($_SESSION['grd2']); //clear grd1
	unset($_SESSION['sub3']); //clear sub1
	unset($_SESSION['grd3']); //clear grd1
	unset($_SESSION['sub4']); //clear sub1
	unset($_SESSION['grd4']); //clear grd1
	unset($_SESSION['sub5']); //clear sub1
	unset($_SESSION['grd5']); //clear grd1
	unset($_SESSION['sub6']); //clear sub1
	unset($_SESSION['grd6']); //clear grd1
	unset($_SESSION['sub7']); //clear sub1
	unset($_SESSION['grd7']); //clear grd1
	unset($_SESSION['sub8']); //clear sub1
	unset($_SESSION['grd8']); //clear grd1
	unset($_SESSION['sub9']); //clear sub1
	unset($_SESSION['grd9']); //clear grd1


	unset($_SESSION['exam_no2']); //clear olexam1
	unset($_SESSION['olexam2']); //clear olexam1
	// unset($_SESSION['exam_card_1']); //clear exam_card_1
	// unset($_SESSION['exam_card_SN_1']); //clear exam_card_SN_1
	// unset($_SESSION['exam_card_2']); //clear exam_card_1
	// unset($_SESSION['exam_card_SN_2']); //clear exam_card_SN_1
	unset($_SESSION['eyear2']); //clear eyear1
	unset($_SESSION['emonth2']); //clear emonth1
	unset($_SESSION['s_sub1']); //clear sub1
	unset($_SESSION['s_grd1']); //clear grd1
	unset($_SESSION['s_sub2']); //clear sub1
	unset($_SESSION['s_grd2']); //clear grd1
	unset($_SESSION['s_sub3']); //clear sub1
	unset($_SESSION['s_grd3']); //clear grd1
	unset($_SESSION['s_sub4']); //clear sub1
	unset($_SESSION['s_grd4']); //clear grd1
	unset($_SESSION['s_sub5']); //clear sub1
	unset($_SESSION['s_grd5']); //clear grd1
	unset($_SESSION['s_sub6']); //clear sub1
	unset($_SESSION['s_grd6']); //clear grd1
	unset($_SESSION['s_sub7']); //clear sub1
	unset($_SESSION['s_grd7']); //clear grd1
	unset($_SESSION['s_sub8']); //clear sub1
	unset($_SESSION['s_grd8']); //clear grd1
	unset($_SESSION['s_sub9']); //clear sub1
	unset($_SESSION['s_grd9']); //clear grd1


	//check if the user has id in the table to be updated
	if(if_student_is_available($user_id))
	{
		//$type  = 1;
		for($i = 1 ; $i <=2 ; $i++)
		{

			switch($i)
			{

				case 1:

				$q = "UPDATE `applicants_olevel_nce` SET
					`exam_type`='$olexam1',`exam_year`='$eyear1',`exam_month`='$emonth1',`subject1`='$sub1',`grade1`='$grd1',
					`subject2`='$sub2',`grade2`='$grd2',`subject3`='$sub3',`grade3`='$grd3',`subject4`='$sub4',`grade4`='$grd4',
					`subject5`='$sub5',`grade5`='$grd5',`subject6`='$sub6',`grade6`='$grd6',`subject7`='$sub7',`grade7`='$grd7',
					`subject8`='$sub8',`grade8`='$grd8',`subject9`='$sub9',`grade9`='$grd9',`exam_no`='$exam_no'
					 WHERE `type`='1'  AND applicant_id = '$user_id'";

					 $r = mysqli_query($dbc,$q) or die("System Error: Please go back");

					 break;



				case 2:

				  $q = "UPDATE `applicants_olevel_nce` SET
				  `exam_type`='$olexam2',`exam_year`='$eyear2',`exam_month`='$emonth2',`subject1`='$s_sub1',`grade1`='$s_grd1',
				  `subject2`='$s_sub2',`grade2`='$s_grd2',`subject3`='$s_sub3',`grade3`='$s_grd3',`subject4`='$s_sub4',`grade4`='$s_grd4',
				  `subject5`='$s_sub5',`grade5`='$s_grd5',`subject6`='$s_sub6',`grade6`='$s_grd6',`subject7`='$s_sub7',`grade7`='$s_grd7',
				  `subject8`='$s_sub8',`grade8`='$s_grd8',`subject9`='$s_sub9',`grade9`='$s_grd9',`exam_no`='$exam_no2'
				   WHERE `type`='2'  AND applicant_id = '$user_id'";

				   $r = mysqli_query($dbc,$q) or die("System Error: Please go back");

				break;

				default:



			}


			//increment the counter
			//$type++;


		}

	}
	else
	{


		   $q = "INSERT INTO `applicants_olevel_nce` (`id`, `applicant_id`, `exam_type`,`card_pin`,`card_sno`,
		   		 `exam_year`, `exam_month`, `subject1`,
		        `grade1`, `subject2`, `grade2`, `subject3`, `grade3`, `subject4`, `grade4`, `subject5`, `grade5`, `subject6`, `grade6`,
		        `subject7`, `grade7`, `subject8`, `grade8`, `subject9`, `grade9`, `exam_no`, `type`)
		         VALUES

				(NULL,'$user_id','$olexam1','$exam_card_1','$exam_card_SN_1','$eyear1','$emonth1','$sub1','$grd1','$sub2','$grd2','$sub3','$grd3','$sub4',
				'$grd4','$sub5','$grd5','$sub6','$grd6','$sub7','$grd7','$sub8','$grd8','$sub9','$grd9','$exam_no','1'),

				(NULL,'$user_id','$olexam2','$exam_card_2','$exam_card_SN_2','$eyear2','$emonth2','$s_sub1','$s_grd1','$s_sub2','$s_grd2','$s_sub3','$s_grd3','$s_sub4',
				'$s_grd4','$s_sub5','$s_grd5','$s_sub6','$s_grd6','$s_sub7','$s_grd7','$s_sub8','$s_grd8','$s_sub9','$s_grd9','$exam_no2','2')";


				$r = mysqli_query($dbc,$q) or die("System Error: Please go back");

	}



	if($r)
	{

		$olevel1_str = '';
		$olevel2_str = '';
		$all_olevel = array();

		//initializing variables to prevent errors in array
		for ($i = 1 ; $i < 10; $i++) {
			//using a dynamic variables to generate variables from 1 to 9
			//first olevel
			${ 'ol1_sb' . $i} =  '';
			${ 'ol1_gr' . $i} =  '';
			//passing values to new variable
			${ 'ol1_sb' . $i} =  ${ 'sub' . $i};
			${ 'ol1_gr' . $i} =  ${ 'grd' . $i};
			//populating the olevel_1 array
			//$arr_olvl1 =  ${ 'ol1_sb' . $i}

			//validating pass grade
			if ( ${ 'ol1_gr' . $i} > 0 &&   ${ 'ol1_gr' . $i} < 7) {
				//array_push( $olevel_1 , $arr_olvl1);
				$olevel1_str .=  ${ 'ol1_sb' . $i}.',';
			}

			//second olevel
			${ 'ol2_sb' . $i} =  '';
			${ 'ol2_gr' . $i} =  '';

			${ 'ol2_sb' . $i} =  ${ 's_sub' . $i};
			${ 'ol2_gr' . $i} =  ${ 's_grd' . $i};

			//validating pass grade
			if ( ${ 'ol2_gr' . $i} > 0 &&   ${ 'ol2_gr' . $i} < 7) {
				//array_push( $olevel_2 , $arr_olvl2);
				$olevel2_str .=  ${ 'ol2_sb' . $i}.',';
			}


		}

		//creating the array with valid subjects
		$olevel_1 =  explode( ',' , $olevel1_str);
		$olevel_2 =  explode( ',' , $olevel2_str);



		/*
		print_r($olevel_1);
		print_r($olevel_2);*/

		//removing the last empty data due to the last , comma
		for ($i = 0; $i < count($olevel_1); $i++) {
			if ($olevel_1[$i] !== '') {
				//when subject not added already
				if ( ! in_array($olevel_1[$i], $all_olevel)) {
					//merging all olevel
					array_push( $all_olevel , $olevel_1[$i]);
				}
					# code...
			}

		}
		//removing the last empty data due to the last , comma
		for ($i = 0; $i < count($olevel_2); $i++) {
			if ($olevel_2[$i] !== '') {
				//when subject not added already
				if ( ! in_array($olevel_2[$i], $all_olevel)) {
					//merging all olevel
					array_push( $all_olevel , $olevel_2[$i]);
				}
			}

		}


	/*	foreach ($all_olevel as $olv ) {
			echo show_subjects_title($olv).'<br>';
		}
		//print_r($all_olevel);
		exit();*/
		//validaing subjects combination

		$qi = 0;
		$qualified = check_applicant_required_olevel($all_olevel, $user_id);
		if ($qualified == true) {
			$qi = 1;
		}

		//activating applicant
		set_applicant_qualified($user_id, $qi );


		if (isset($_SESSION['course_sugg'])) {
			unset($_SESSION['course_sugg']);
		}

		if (isset($_SESSION['sugg_err'])) {
			unset($_SESSION['sugg_err']);
		}

		// if not qualified display suggestion
		if ($qi == 0) {


			get_course_suggestion($all_olevel,$user_id);

		}else{

			/*unset($_SESSION['course_sugg']);
			unset($_SESSION['sugg_err']);*/
		}


		//print_r(check_applicant_required_olevel($all_olevel, $user_id) );

		//when resubmite wipe previous reason
		//if(is_applicant_admitted($user_id) && is_applicant_not_cleared($user_id)){
		delete_applicant_cleared_reasons($user_id);
		//}

		$info_success['success'] = 'O-Level is successfully updated..';
		header("Location: dashboard.php?act=qualification_info&is=".urlencode($info_success['success'])."&qlk=".$encripted_qlk.""); //is - info success


	}
	else
	{
		$errors['error'] =  'O-Level not updated due to a system error. We apologize for any inconvenience.';

		header("Location: dashboard.php?act=qualification_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=qualification_info&error=".urlencode($errors['error'])."&qlk=".$encripted_qlk."");

}



?>
