<?php


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



//include '../configuration.php';


//include '../functions_.php';


if($_REQUEST['act'] == 'compute_by_school_nce_new_' )
{
	//collect the course id
	//if(isset($_REQUEST['cid']))
	//{
	//
	     echo "Working on it ... <br/>";

		//$course_id = $_REQUEST['cid'];//course id
		$pid = $_REQUEST['pid'];//programme id
		$sid = $_REQUEST['sid'];//school id
		$ses = $_REQUEST['ses'];//session passed
		$sem = $_REQUEST['sem'];//semster passed
		$level = $_REQUEST['level'];//semster passed
		$action_type = $_REQUEST['type'];//semster passed


		//get a security id to know who computed the result
        $security = get_security_id($user_id);




        //set counters for probation, passed ,  acad with drawal  and vol with drawal
        $count_probation = 0;
        $count_passed = 0;
        //$count_reoffers = 0;
        $count_oc = 0; //reoffers
        $count_acad_with = 0;
        $count_vol_with = 0;
				$count_graduating_students = 0;

		//get a token to allow processing by batch
		//token
		//
		if($action_type == 'do')
		{//begin fresh computing for the semester

				$token = result_entry_token();//get a mapped token


				// INSERT INTO students_nce_test (student_id) SELECT DISTINCT(std_nce.id) FROM students_nce std_nce, students_courses_nce cs_nce WHERE std_nce.entry_year = '2018' AND cs_nce.student_id = std_nce.id AND cs_nce.student_id = std_nce.id AND NOT EXISTS( SELECT * FROM students_nce_test WHERE students_nce_test.student_id = std_nce.id)


				$q = "SELECT distinct (student_id) FROM students_courses_nce WHERE programme_id = $pid AND session_id = $ses AND level = '$level' LIMIT 30"; //  ORDER BY student_id LIMIT 0, 50
				$r = mysqli_query($dbc,$q);

				if($r)
				{
					while($row = mysqli_fetch_array($r,MYSQLI_ASSOC))
					{
						echo $row['student_id'];




						//check if a particular matric number is on the system
						echo $student = $row['student_id'];




						//start with assuming the id of student


						//$dept_id = get_student_department_id_nce($student);
						//$school_id = get_student_school_id_nce($student);
						$programme_id = $pid;//pass programme idsince you have passed it initially get_student_programme_id_nce($student); //get course if
						// $level = 'I';//get_student_level_nce($student);
					echo 	$preev_remark = get_previous_remark2($student,$programme_id, $level, $ses, $sem);



						//check if not wihdrawan already
						if( $preev_remark != 'ACAD WITH' || $preev_remark != 'VOL WITH')
						{



                    //check if the total score is not updated and update
                    update_students_courses_db_nce($student, $programme_id, $level, $ses, $sem);

					//echo "I should work nw";
					//previous total credit unit registered
						$prev_tcur_edu = get_total_credit_unit_registered_previously_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem);



						 //present  per session per semester
						 $tcur_eduo = get_total_credit_unit_registered_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem) ; //add previous tcur edu
						 $tcur_edu = $tcur_eduo + $prev_tcur_edu;



						//previous
						 $prev_tcur_gst = get_total_credit_unit_registered_previously_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem);


						 //present
						 $tcur_gsto = get_total_credit_unit_registered_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem);

						 $tcur_gst = $tcur_gsto + $prev_tcur_gst;





						 //declare an array to hold the pices of the sum
						 $prev_tcur_core_array = array(); //declared for previous usage

						 //declare an array to hold the pices of the sum declared fore present
						 $tcur_core_array = array();


						//get previous summed credit unit registered for credit registered
						 $prev_tcur_core0 = get_total_credit_unit_registered_previously_nce($student,$programme_id, $course_category = 0, $level, $ses,$sem);


						 //get the value returned for the single major
						 $tcur_core0 = get_total_credit_unit_registered_nce($student,$programme_id, $course_category = 0, $level, $ses,$sem);



						 ///pass the previously core 0 for total
						 if(strpos($prev_tcur_core0, ',') !== false)//check
						 {

						 		for ($i = 0; $i < count($prev_tcur_core0);$i++)
								{
									 $prev_tcur_core_array = explode(',', $prev_tcur_core0);


								}


								//pass the array to the two variables
								$prev_tcur_core1 =  $prev_tcur_core_array[0]; //first summed

								$prev_tcur_core2 = $prev_tcur_core_array[1]; //second summed

						 }
						 else
						 {
						 	//just collect the single department id
						 	$prev_tcur_core1 = $prev_tcur_core0;
						 }




						 ///pass the core 0 fresh present total
						 if(strpos($tcur_core0, ',') !== false)//check
						 {

						 		for ($i = 0; $i < count($tcur_core0);$i++)
								{
									 $tcur_core_array = explode(',', $tcur_core0);


								}


								//pass the array to the two variables
								$tcur_core1o =  $tcur_core_array[0]; //first summed
								$tcur_core1 = $tcur_core1o + $prev_tcur_core1;

								$tcur_core2o = $tcur_core_array[1]; //second summed
								$tcur_core2 = $tcur_core2o + $prev_tcur_core2;

						 }
						 else
						 {
						 	//just collect the single department id
						 	$tcur_core1o = $tcur_core0;
						 	$tcur_core1 = $tcur_core1o + $prev_tcur_core1;

						 }


						 //previous before present
						 $prev_tcur_cummulative = $prev_tcur_edu + $prev_tcur_gst + $prev_tcur_core1 + $prev_tcur_core2;

						 //present
						 //get cumulative credit unit registereds
						 $tcur_cummulativeo =  $tcur_edu + $tcur_gst + $tcur_core1 +$tcur_core2 ;

						 $tcur_cummulative  = $tcur_cummulativeo ;//+ $prev_tcur_cummulative


						 //get tottal credit unit earned previously from edu
						 //get total credit unit
					    $prev_tcu_earned_edu = get_total_credit_unit_earned_previously_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem);

					    //get total credit unit  present  from edu
					    $tcu_earned_eduo = get_total_credit_unit_earned_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem);
					    $tcu_earned_edu = $tcu_earned_eduo + $prev_tcu_earned_edu;


					    //previously total credit unit earned frome gst
					   $prev_tcu_earned_gst = get_total_credit_unit_earned_previously_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem);

						//present totact credit earned from gstS
					    $tcu_earned_gsto = get_total_credit_unit_earned_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem);
					    $tcu_earned_gst = $tcu_earned_gsto + $prev_tcu_earned_gst;





					    //declare an array to hold the pices of the sum for previous earned core
						 $prev_tcu_earned_core_array = array();

						 //declare an array to hold the pices of the sum for present
						 $tcu_earned_core_array = array();

						 //get the value returned for the single major previously
						 $prev_tcu_earned_core0 = get_total_credit_unit_earned_previously_nce($student,$programme_id, $course_category = 0, $level, $ses,$sem);


						 //get the value returned for the single major  for present
						 $tcu_earned_core0 = get_total_credit_unit_earned_nce($student,$programme_id, $course_category = 0, $level, $ses,$sem);



						 //for previous incase of single major or double major
						 if(strpos($prev_tcu_earned_core0, ',') !== false)//check
						 {

						 		for ($i = 0; $i < count($prev_tcu_earned_core0);$i++)
								{
									 $prev_tcu_earned_core_array = explode(',', $prev_tcu_earned_core0);


								}


								//pass the array to the two variables
								$prev_tcu_earned_core1 =  $prev_tcu_earned_core_array[0]; //first summed

								$prev_tcu_earned_core2 = $prev_tcu_earned_core_array[1]; //second summed

						 }
						 else
						 {
						 	//just collect the single department id
						 	$prev_tcu_earned_core1 = $prev_tcu_earned_core0;
						 }//end for collection for previously

						 //for present single major
						 if(strpos($tcu_earned_core0, ',') !== false)//check
						 {

						 		for ($i = 0; $i < count($tcu_earned_core0);$i++)
								{
									 $tcu_earned_core_array = explode(',', $tcu_earned_core0);


								}


								//pass the array to the two variables
								$tcu_earned_core1o =  $tcu_earned_core_array[0]; //first summed
								$tcu_earned_core1 = $tcu_earned_core1o + $prev_tcu_earned_core1;

								$tcu_earned_core2o = $tcu_earned_core_array[1]; //second summed
								$tcu_earned_core2 = $tcu_earned_core2o + $prev_tcu_earned_core2;

						 }
						 else
						 {
						 	//just collect the single department id
						 	$tcu_earned_core1o = $tcu_earned_core0;
						 	$tcu_earned_core1 = $tcu_earned_core1o + $prev_tcu_earned_core1;

						 }



						 //get cummulative credit unit earned previously
						$prev_tcu_earned_cummulative =  $prev_tcu_earned_edu + $prev_tcu_earned_gst + $prev_tcu_earned_core1 + $prev_tcu_earned_core2;

						 //get cummulative credit unit earned in present
						 $tcu_earned_cummulativeo =  $tcu_earned_edu + $tcu_earned_gst + $tcu_earned_core1 + $tcu_earned_core2;
						 $tcu_earned_cummulative = $tcu_earned_cummulativeo ;//+ $prev_tcu_earned_cummulative



					//////////////////////######################### work on total grade point #################################///////



						///previously earned total grade point
						$prev_tgp_earned_edu = get_total_grade_point_earned_previously_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem);

						///present total frade point earned
						 $tgp_earned_eduo = get_total_grade_point_earned_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem);
						 $tgp_earned_edu = $tgp_earned_eduo + $prev_tgp_earned_edu;


						 //previously earned grade point on gst
						 $prev_tgp_earned_gst = get_total_grade_point_earned_previously_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem);

						 //presently earned grade point on all the courses
						 $tgp_earned_gsto = get_total_grade_point_earned_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem);
						 $tgp_earned_gst = $tgp_earned_gsto + $prev_tgp_earned_gst;



						 //previously earned from coure courses
						 $prev_tgp_earned_core0 = get_total_grade_point_earned_previously_nce($student,$programme_id,$course_category = 0, $level, $ses,$sem);

						 //presently earned from coure courses
						 $tgp_earned_core0 = get_total_grade_point_earned_nce($student,$programme_id,$course_category = 0, $level, $ses,$sem);

						 //$tgp_earned_core0 = $tgp_earned_core0o + $prev_tgp_earned_core0;



						 //declared to hold for previously
						 $prev_tgp_earned_core_array = array();


						 //declared to hold for present
						 $tgp_earned_core_array = array();



						 //previously  retreived single major or double major earned total grade point
						 if(strpos($prev_tgp_earned_core0, ',') !== false)//check
						 {

						 		for ($i = 0; $i < count($prev_tgp_earned_core0);$i++)
								{
									 $prev_tgp_earned_core_array = explode(',', $prev_tgp_earned_core0);


								}


								//pass the array to the two variables
								$prev_tgp_earned_core1 =  $prev_tgp_earned_core_array[0]; //first summed

								$prev_tgp_earned_core2 = $prev_tgp_earned_core_array[1]; //second summed

						 }
						 else
						 {
						 	//just collect the single department id
						 	$prev_tgp_earned_core1 = $prev_tgp_earned_core0;
						 }//end of previously retreived total grade point earned

						 //presently  retreived single major or double major earned total grade point
						 if(strpos($tgp_earned_core0, ',') !== false)//check
						 {

						 		for ($i = 0; $i < count($tgp_earned_core0);$i++)
								{
									 $tgp_earned_core_array = explode(',', $tgp_earned_core0);


								}


								//pass the array to the two variables
								$tgp_earned_core1o =  $tgp_earned_core_array[0]; //first summed
								$tgp_earned_core1 = $tgp_earned_core1o + $prev_tgp_earned_core1;

								$tgp_earned_core2o = $tgp_earned_core_array[1]; //second summed
								$tgp_earned_core2 = $tgp_earned_core2o +$prev_tgp_earned_core2;


						 }
						 else
						 {
						 	//just collect the single department id
						 	$tgp_earned_core1o = $tgp_earned_core0;
						 	$tgp_earned_core1 = $tgp_earned_core1o + $prev_tgp_earned_core1;

						 }





						 //get grade point earned previously
						  $prev_tgp_earned_cummulative =  $prev_tgp_earned_edu + $prev_tgp_earned_gst + $prev_tgp_earned_core1 + $prev_tgp_earned_core2;

						 	//get grade point earned
						 $tgp_earned_cummulativeo =  $tgp_earned_edu + $tgp_earned_gst + $tgp_earned_core1 + $tgp_earned_core2;
						 $tgp_earned_cummulative = $tgp_earned_cummulativeo ; //+ $prev_tgp_earned_cummulative



					      echo '<br/>';

					      //get previous cgpa
					      @$prev_gpa_edu = $prev_tgp_earned_edu / $prev_tcur_edu;


					      //get present gpa edu
					   	  @$gpa_edu = $tgp_earned_edu / $tcur_edu; /// $tcur_edu;

						  echo "Test PREV GPA EDU:" .number_format($prev_gpa_edu,2).'<br/>';
						  echo "Test GPA EDU:" .number_format($gpa_edu,2).'<br/>';


						  //previous gpa gst
						  @$prev_gpa_gst = $prev_tgp_earned_gst / $prev_tcur_gst;


						  //present gpa gst
						  @$gpa_gst = $tgp_earned_gst / $tcur_gst;

						  echo "Test PREV GPA GST:" .number_format($prev_gpa_gst,2).'<br/>';


						  echo "Test GPA GST:" .number_format($gpa_gst,2).'<br/>';



						  //previous gpa core 1
						  @$prev_gpa_core1 = $prev_tgp_earned_core1 / $prev_tcur_core1;

						  //present gpa core 1
						   @$gpa_core1 = $tgp_earned_core1 / $tcur_core1;


						  echo "Test PREV GPA CORE 1:" .number_format($prev_gpa_core1,2).'<br/>';

						  echo "Test GPA CORE 1:" .number_format($gpa_core1,2).'<br/>';


						    //previous computed core 2 gpa
						  	 @$prev_gpa_core2 = $prev_tgp_earned_core2 / $prev_tcur_core2;

						     //present computed core 2
						     @$gpa_core2 = $tgp_earned_core2 / $tcur_core2;

						  echo "Test PREV GPA CORE 2:" .number_format($prev_gpa_core2,2).'<br/>';

						  echo "Test GPA CORE 2:" .number_format($gpa_core2,2).'<br/>';


						  	 @$prev_cgpa_cum = $prev_tgp_earned_cummulative / $prev_tcur_cummulative;

						  echo "PREV CGPA:" .number_format($prev_cgpa_cum,2).'<br/>';

						     @$cgpa_cum = $tgp_earned_cummulative / $tcur_cummulative;

						  echo "CGPA:" .number_format($cgpa_cum,2).'<br/>';

						  //logic to get current gpa for a particular semester
						  ///////////////////////////////////////////////////////
						  //////////////////////////////////////////////////////
						  @$current_gpa =  $tgp_earned_cummulativeo / $tcur_cummulativeo;
						  //////////////////////////////////////////////////////////
						  ///////////////////////////////////////////////////////////




						  //get outstanding courses remark per student if there is or not and if there is count
						  $oc_remark = get_remark_for_cummulative_per_student_nce($student,$programme_id,$level,$ses,$sem);

						  //count outstanding remark passed
						 if($oc_remark == 'PASSED'){ $count_passed = $count_passed + 1;}

						 //count outstanding remark for oc
						 if($oc_remark == 'OC'){$count_oc = $count_oc + 1;}




						 //collect the remarks for different criteria
						  $probation_remark = get_probation_remark_per_semester_nce($current_gpa,$level,$ses,$sem);

						  //count probation
						 if($probation_remark == 'PROB')
						 {
						 	$count_probation = $count_probation + 1;

						 }




		  				 ///////////////////////////////////////////////////////
		  				 /// start to work on probation remark
						 //before the next this get previous  gpa stored for a student
						 $pre_gpa = get_student_previous_gpa_nce($student,$programme_id,$level,$ses,$sem);
						  //check if the pre gpa is false


						 //get acad remark
						 if($pre_gpa == 'false') //
						 {
						  	$acad_with_remark = '';
						 }
						 else
						 {
						  	//get remark for academic withdrwal
						 	$acad_with_remark = get_acad_with_remark_per_semester_nce($student, $pre_gpa,$cgpa_cum,$level,$ses,$sem);//$cgpa_cumkeep for future is

						 }


						 //count acad with
						 if($acad_with_remark == 'ACAD WITH')
						 {
						 	$count_acad_with = $count_acad_with + 1;

						 	//hide
						 	$probation_remark = '';
						 	$oc_remark = '';
						 }

						 //////////////////////////////////////////////////////////////////////////////
						 //////////////////////////////////////////////////////////////////////////////
						 /////////////////// end of previous cga and acad withdrawal //////////////////


						 /////////////////////////////////////////////////////////////////////////////
						 ////////////// before we go lets get how many carry overs gotten by the
						 ////////////// candidates

						 //count the ocs
		  				 $no_oc = count_carry_overs_per_student_nce($student, $programme_id,$level,$ses, $sem);



		  				 $vol_with_remark = get_vol_with_remark_per_semester_nce($current_gpa); //$cgpa_cum keep for future use

		  				 //count vol with and hide acad with,
						 if($vol_with_remark == 'VOL WITH')
						 {
							 	$count_vol_with = $count_vol_with + 1;

							 	//hide
							 	$acad_with_remark = '';
							 	$probation_remark = '';
							 	$oc_remark = '';
						 }

// $minimum_requirement fulfilment
								//check if Level is III or spilling
								if($level == 'III' && $sem == '2' && $oc_remark == 'PASSED')
								{
									$minimum_req_fulfilment = check_minimum_requirments_nce($student, $programme_id,$level,$ses, $sem);
									$graduation_remark = get_graduation_remark_nce($student, $programme_id,$level,$ses, $sem, $cgpa_cum);

											// // //count passed graduating students and other remarks
											 if($minimum_req_fulfilment == 'Pass')
											 {
											    $count_graduating_students = $count_graduating_students + 1;
											//
											//  //hide
											  $acad_with_remark = '';
											  $probation_remark = '';
											  $oc_remark = '';
                        $vol_with_remark = '';

											//
											//
											}
											else
											{
											// 	// = '';
											 	$not_graduation_remark = $minimum_req_fulfilment; //set outanding remark to be problems found on minimum required
											//	$graduation_remark = ''; //not graduating so set graduting remark free

											//hide
											 $acad_with_remark = '';
											 $probation_remark = '';
											 $oc_remark = '';
											 $vol_with_remark = '';
											 $graduation_remark ='';



											//
											}

											//	exit();

								}







						 //gen remark for cummulative
						 //$gen_remark_compounded = $acad_with_remark.' ; '.$probation_remark.' ; '.$oc_remark.'-'.$no_oc;
							$gen_remark_compounded = get_compounded_remark_nce($vol_with_remark, $acad_with_remark,$repeat_with_remark,$probation_remark,$oc_remark,$no_oc,$minimum_req_fulfilment,$graduation_remark, $not_graduation_remark);


						 //get remark for different courses
						 $distinct_remark_oc = get_oc_courses_nce($student,$programme_id, $level, $ses, $sem);



					//insert with a unique token by session and by semester
					//write a fucntion to insert the student course nce result table


					//
					// //check if not wihdrawan already
					// if(get_previous_remark2($student,$programme_id, $level, $ses, $sem) != 'ACAD WITH')
					// {



							// //check if inserted already using token and other parameters
							if(!check_if_student_computatation_exist($student, $ses, $sem, $level))
							{


								$q5 = "INSERT INTO `students_results_nce`(`student_id`, `programme_id`,
													`session_id`, `semester`, `level`, `delete_token`, `result_date_added`, `status`,
													 `date_added`, `added_by`,
													`edu_ptcur`, `edu_tcur`, `edu_ptcue`, `edu_tcue`, `edu_ptgpe`, `edu_tgpe`
													,`edu_pcgpa`,`edu_cgpa`,
													`gst_ptcur`, `gst_tcur`, `gst_ptcue`, `gst_tcue`, `gst_ptgpe`, `gst_tgpe`
													,`gst_pcgpa`,`gst_cgpa`,
													`core1_ptcur`, `core1_tcur`, `core1_ptcue`, `core1_tcue`, `core1_ptgpe`, `core1_tgpe`,`core1_pcgpa`,`core1_cgpa`,
													`core2_ptcur`, `core2_tcur`, `core2_ptcue`, `core2_tcue`,
													`core2_ptgpe`, `core2_tgpe`,`core2_pcgpa`,`core2_cgpa`,
													`cum_ptcur`, `cum_tcur`, `cum_ptcue`,`cum_tcue`,
													`cum_ptgpe`, `cum_tgpe`,`cum_pcgpa`,`cum_cgpa`,
													`prev_gpa`,`cur_gpa`,`remark1`,`remark2`)

									  VALUES ('$student','$programme_id','$ses','$sem', '$level','$token',NOW(),1,NOW(), '$security',
											  '$prev_tcur_edu','$tcur_edu','$prev_tcu_earned_edu','$tcu_earned_edu','$prev_tgp_earned_edu','$tgp_earned_edu','$prev_gpa_edu','$gpa_edu',
											  '$prev_tcur_gst','$tcur_gst','$prev_tcu_earned_gst','$tcu_earned_gst','$prev_tgp_earned_gst','$tgp_earned_gst','$prev_gpa_gst','$gpa_gst',
											  '$prev_tcur_core1','$tcur_core1','$prev_tcu_earned_core1','$tcu_earned_core1','$prev_tgp_earned_core1','$tgp_earned_core1','$prev_gpa_core1','$gpa_core1',
											  '$prev_tcur_core2','$tcur_core2','$prev_tcu_earned_core2','$tcu_earned_core2','$prev_tgp_earned_core2','$tgp_earned_core2','$prev_gpa_core2','$gpa_core2',
											  '$prev_tcur_cummulative','$tcur_cummulative','$prev_tcu_earned_cummulative','$tcu_earned_cummulative','$prev_tgp_earned_cummulative','$tgp_earned_cummulative','$prev_cgpa_cum','$cgpa_cum',
											  '$pre_gpa','$current_gpa','$distinct_remark_oc','$gen_remark_compounded')"; // i changed $pre_gpa to $prev_cgpa_cum

								$r5 = mysqli_query($dbc,$q5);

								if($r5) { /*die("<span class='btn btn-danger'>SERVER ERROR: Please retry again</span>");*/ }
						 	}
						 	else
						 	{



						 		$existing_token = get_existing_result_token($pid,$ses,$sem,$level);//get exisitng result token

						 		$q6 = "UPDATE  `students_results_nce`  SET  `result_date_added` = NOW() , `status` = 1,
													`date_added` = NOW() , `added_by` = '$security',
													`edu_ptcur` = '$prev_tcur_edu', `edu_tcur` = '$tcur_edu',
													`edu_ptcue` ='$prev_tcu_earned_edu' , `edu_tcue` = '$tcu_earned_edu',
													`edu_ptgpe` = '$prev_tgp_earned_edu' , `edu_tgpe`='$tgp_earned_edu',
													`edu_pcgpa` ='$prev_gpa_edu',`edu_cgpa` = '$gpa_edu',

												`gst_ptcur`= '$prev_tcur_gst', `gst_tcur` ='$tcur_gst', `gst_ptcue` = '$prev_tcu_earned_gst', `gst_tcue` = '$tcu_earned_gst', `gst_ptgpe` = '$prev_tgp_earned_gst', `gst_tgpe` = '$tgp_earned_gst'
													,`gst_pcgpa` = '$prev_gpa_gst',`gst_cgpa`= '$gpa_gst',


												`core1_ptcur` = '$prev_tcur_core1', `core1_tcur` = '$tcur_core1', `core1_ptcue` = '$prev_tcu_earned_core1' , `core1_tcue` = '$tcu_earned_core1', `core1_ptgpe` = '$prev_tgp_earned_core1', `core1_tgpe`='$tgp_earned_core1',`core1_pcgpa` = '$prev_gpa_core1' , `core1_cgpa`='$gpa_core1',

												`core2_ptcur` = '$prev_tcur_core2', `core2_tcur` = '$tcur_core2', `core2_ptcue` = '$prev_tcu_earned_core2', `core2_tcue` = '$tcu_earned_core2',
													`core2_ptgpe` = '$prev_tgp_earned_core2', `core2_tgpe` = '$tgp_earned_core2',`core2_pcgpa` = '$prev_gpa_core2' ,`core2_cgpa` = '$gpa_core2',

												`cum_ptcur` = '$prev_tcur_cummulative', `cum_tcur` = '$tcur_cummulative', `cum_ptcue` = '$prev_tcu_earned_cummulative' ,`cum_tcue` = '$tcu_earned_cummulative',
													`cum_ptgpe` = '$prev_tgp_earned_cummulative', `cum_tgpe` =
													'$tgp_earned_cummulative',
													`cum_pcgpa` = '$prev_cgpa_cum',`cum_cgpa` = '$cgpa_cum',
													`prev_gpa` = '$pre_gpa',`cur_gpa` = '$current_gpa',
												`remark1` = '$distinct_remark_oc',`remark2`= '$gen_remark_compounded'


										WHERE `student_id` = '$student' AND  `programme_id` = '$programme_id'  AND
											  `session_id` = '$ses' AND `semester` = '$sem' AND `level` = '$level'";

								$r6 = mysqli_query($dbc,$q6);

								if($r6) { /*die("<span class='btn btn-danger'>SERVER ERROR: Please retry again</span>");*/ }
						 	}


					 	}//end of if widthdrawn


						}//end of while looping tru students
					}//end of if result


							if($r5)
							{


							 	//check if ras is true before switching


					  //         // header("Location: gateway.php?act=course_registration0&is=Course resgistration is done, please click on course registration history to print&qlk=".md5(3));
							 }


							 //check if aggregate summary exist
								if(!check_if_programme_aggregate_summary_exist($programme_id, $ses, $sem, $level))
								{
									// if not added insert
									$qas = "INSERT INTO `students_results_programme_remark_nce`
											(`programme_id`, `session_id`, `semester`, `level`, `delete_token`,
											`result_date_added`,  `date_added`,
											`added_by`,  `passed`, `reoffers`, `probation`, `acad_with`,
											 `vol_with`)

									VALUES ('$programme_id','$ses','$sem','$level','$token',NOW(),NOW(),'$security',
										   '$count_passed','$count_oc','$count_probation','$count_acad_with',
											'$count_vol_with')";

									$ras = mysqli_query($dbc,$qas);

									if(!$ras)
								{ /*die("<span class='btn btn-danger'>SERVER ERROR: Error Inserting Second Table</span>");*/ }

								}
								else
								{
									$qas = "UPDATE `students_results_programme_remark_nce` SET
											`date_modified`=NOW(), `modified_by`='$security',`passed`='$count_passed',
											`reoffers`='$count_oc',`probation`='$count_probation',`acad_with`=
											'$count_acad_with',`vol_with`='$count_vol_with'

											WHERE `programme_id` = '$programme_id' AND `session_id`='$ses'
												  AND `semester`= '$sem' AND `level`='$level'";
									$ras = mysqli_query($dbc,$qas);


								}


					           //check
							 	echo "Done PROB:- $count_probation RE-OFFERS $count_oc PASSED:  $count_passed ACAD WITH: $count_acad_with VOL WITH:  $count_vol_with";





		} //end of if freshe computing
		else
		{//begin recomputation by deleting exiting record then insert


		
			//delete using token
			//get token from existing
			$existing_token = '5053370357738192929021623325152';//get_existing_result_token($pid,$ses,$sem,$level);


			//delete the old computation and redo kindly no thats here i just added level to search criteria not to allow searching session only
// 			$qd = "DELETE FROM `students_results_nce` WHERE programme_id = '$pid' AND session_id = '$ses' AND semester = '$sem' AND level = '$level'";
// 			$rd = mysqli_query($dbc,$qd);

// 			if($rd)
// 			{

				// $token = result_entry_token();//get a mapped token 5053370357738192929021623325152
				
					$token = '777023321162259000821121211212';


				//get total number in batch format
				/////////////////////////////////////////////////////////////////////////
				///////////////////////////////////////////////////////////////////////////////
				////////////////////////////////////////////////////////////////////////////


				// $q0 = "SELECT COUNT(DISTINCT(student_id)) AS num FROM students_courses_nce WHERE programme_id = $pid AND session_id = $ses AND level = '$level'"; //  ORDER BY student_id LIMIT 0, 50
				// $r0 = mysqli_query($dbc,$q0); // or die(mysql_error());
				// $row0 = mysqli_fetch_array($r0);
				// $itemcount0 = $row0['num'];
				//
				// echo "num:".$itemcount0."<br/>";
				//
				$offset = 80;
				$limit = 40;
				//
				//
			  // $batches0 = $itemcount0 / $limit; // Number of while-loop calls - around 120.
				//
				// echo "bch:".$batches0."<br/>";
				//
				//
				//
				// //start a batch loo
				// for ($counter = 0; $counter <= $batches0; $counter++)
				// {
				// 	$offset = $counter * $limit; // MySQL Limit offset number
				// 	////////////////////////////////////////////////////////////////////////////////////////
				// 	////////////////////////////////////////////////////////////////////////////////////////////
				//


				$q = "SELECT distinct (student_id) FROM students_courses_nce WHERE programme_id = $pid AND session_id = $ses AND level = '$level' LIMIT $limit  OFFSET $offset"; // LIMIT $limit  OFFSET $offset
				$r = mysqli_query($dbc,$q);

				if($r)
				{
					while($row = mysqli_fetch_array($r,MYSQLI_ASSOC))
					{
						echo $row['student_id'];




						//check if a particular matric number is on the system
						$student = $row['student_id'];



						//start with assuming the id of student


						//$dept_id = get_student_department_id_nce($student);
						//$school_id = get_student_school_id_nce($student);
						$programme_id = $pid;//pass programme idsince you have passed it initially get_student_programme_id_nce($student); //get course if
						// $level = 'I';//get_student_level_nce($student);
						//

						echo 	$preev_remark = get_previous_remark2($student,$programme_id, $level, $ses, $sem);


						//exit();

							//check if not wihdrawan already
							if( $preev_remark != 'ACAD WITH' || $preev_remark != 'VOL WITH')
							{


						// //check if the total score is not updated and update
                        update_students_courses_db_nce($student, $programme_id, $level, $ses, $sem);



					//echo "I should work nw";
						//previous total credit unit registered
						$prev_tcur_edu = get_total_credit_unit_registered_previously_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem);



						 //present  per session per semester
						 $tcur_eduo = get_total_credit_unit_registered_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem) ; //add previous tcur edu
						 $tcur_edu = $tcur_eduo + $prev_tcur_edu;




						//previous
						 $prev_tcur_gst = get_total_credit_unit_registered_previously_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem);


						 //present
						 $tcur_gsto = get_total_credit_unit_registered_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem);

						 $tcur_gst = $tcur_gsto + $prev_tcur_gst;





						 //declare an array to hold the pices of the sum
						 $prev_tcur_core_array = array(); //declared for previous usage

						 //declare an array to hold the pices of the sum declared fore present
						 $tcur_core_array = array();


						//get previous summed credit unit registered for credit registered
						 $prev_tcur_core0 = get_total_credit_unit_registered_previously_nce($student,$programme_id, $course_category = 0, $level, $ses,$sem);


						 //get the value returned for the single major
						 $tcur_core0 = get_total_credit_unit_registered_nce($student,$programme_id, $course_category = 0, $level, $ses,$sem);



						 ///pass the previously core 0 for total
						 if(strpos($prev_tcur_core0, ',') !== false)//check
						 {

						 		for ($i = 0; $i < count($prev_tcur_core0);$i++)
								{
									 $prev_tcur_core_array = explode(',', $prev_tcur_core0);


								}


								//pass the array to the two variables
								$prev_tcur_core1 =  $prev_tcur_core_array[0]; //first summed

								$prev_tcur_core2 = $prev_tcur_core_array[1]; //second summed

						 }
						 else
						 {
						 	//just collect the single department id
						 	$prev_tcur_core1 = $prev_tcur_core0;
						 }




						 ///pass the core 0 fresh present total
						 if(strpos($tcur_core0, ',') !== false)//check
						 {

						 		for ($i = 0; $i < count($tcur_core0);$i++)
								{
									 $tcur_core_array = explode(',', $tcur_core0);


								}


								//pass the array to the two variables
								$tcur_core1o =  $tcur_core_array[0]; //first summed
								$tcur_core1 = $tcur_core1o + $prev_tcur_core1;

								$tcur_core2o = $tcur_core_array[1]; //second summed
								$tcur_core2 = $tcur_core2o + $prev_tcur_core2;

						 }
						 else
						 {
						 	//just collect the single department id
						 	$tcur_core1o = $tcur_core0;
						 	$tcur_core1 = $tcur_core1o + $prev_tcur_core1;

						 }






						 //previous before present
						 $prev_tcur_cummulative = $prev_tcur_edu + $prev_tcur_gst + $prev_tcur_core1 + $prev_tcur_core2;

						 //present
						 //get cumulative credit unit registereds
						 $tcur_cummulativeo =  $tcur_edu + $tcur_gst + $tcur_core1 +$tcur_core2 ;

						 $tcur_cummulative  = $tcur_cummulativeo ; //+ $prev_tcur_cummulative





						 //get tottal credit unit earned previously from edu
						 //get total credit unit
					    $prev_tcu_earned_edu = get_total_credit_unit_earned_previously_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem);

					    //get total credit unit  present  from edu
					    $tcu_earned_eduo = get_total_credit_unit_earned_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem);
					    $tcu_earned_edu = $tcu_earned_eduo + $prev_tcu_earned_edu;





					    //previously total credit unit earned from gst
					   $prev_tcu_earned_gst = get_total_credit_unit_earned_previously_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem);

						//present totact credit earned from gstS
					    $tcu_earned_gsto = get_total_credit_unit_earned_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem);
					   $tcu_earned_gst = $tcu_earned_gsto + $prev_tcu_earned_gst;





					    //declare an array to hold the pices of the sum for previous earned core
						 $prev_tcu_earned_core_array = array();

						 //declare an array to hold the pices of the sum for present
						 $tcu_earned_core_array = array();

						 //get the value returned for the single major previously
						 $prev_tcu_earned_core0 = get_total_credit_unit_earned_previously_nce($student,$programme_id, $course_category = 0, $level, $ses,$sem);


						 //get the value returned for the single major  for present
						 $tcu_earned_core0 = get_total_credit_unit_earned_nce($student,$programme_id, $course_category = 0, $level, $ses,$sem);



						 //for previous incase of single major or double major
						 if(strpos($prev_tcu_earned_core0, ',') !== false)//check
						 {

						 		for ($i = 0; $i < count($prev_tcu_earned_core0);$i++)
								{
									 $prev_tcu_earned_core_array = explode(',', $prev_tcu_earned_core0);


								}


								//pass the array to the two variables
								$prev_tcu_earned_core1 =  $prev_tcu_earned_core_array[0]; //first summed

								$prev_tcu_earned_core2 = $prev_tcu_earned_core_array[1]; //second summed

						 }
						 else
						 {
						 	//just collect the single department id
						 	$prev_tcu_earned_core1 = $prev_tcu_earned_core0;
						 }//end for collection for previously

						 //for present single major
						 if(strpos($tcu_earned_core0, ',') !== false)//check
						 {

						 		for ($i = 0; $i < count($tcu_earned_core0);$i++)
								{
									 $tcu_earned_core_array = explode(',', $tcu_earned_core0);


								}


								//pass the array to the two variables
								$tcu_earned_core1o =  $tcu_earned_core_array[0]; //first summed
								$tcu_earned_core1 = $tcu_earned_core1o + $prev_tcu_earned_core1;

								$tcu_earned_core2o = $tcu_earned_core_array[1]; //second summed
								$tcu_earned_core2 = $tcu_earned_core2o + $prev_tcu_earned_core2;

						 }
						 else
						 {
						 	//just collect the single department id
						 	$tcu_earned_core1o = $tcu_earned_core0;
						 	$tcu_earned_core1 = $tcu_earned_core1o + $prev_tcu_earned_core1;

						 }


						 //get cummulative credit unit earned previously
						$prev_tcu_earned_cummulative =  $prev_tcu_earned_edu + $prev_tcu_earned_gst + $prev_tcu_earned_core1 + $prev_tcu_earned_core2;

						 //get cummulative credit unit earned in present
						 $tcu_earned_cummulativeo =  $tcu_earned_edu + $tcu_earned_gst + $tcu_earned_core1 + $tcu_earned_core2;
						 $tcu_earned_cummulative = $tcu_earned_cummulativeo ;//+ $prev_tcu_earned_cummulative



					//////////////////////######################### work on total grade point #################################///////



						///previously earned total grade point
						$prev_tgp_earned_edu = get_total_grade_point_earned_previously_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem);

						///present total frade point earned
						 $tgp_earned_eduo = get_total_grade_point_earned_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem);
						 $tgp_earned_edu = $tgp_earned_eduo + $prev_tgp_earned_edu;


						 //previously earned grade point on gst
						 $prev_tgp_earned_gst = get_total_grade_point_earned_previously_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem);

						 //presently earned grade point on all the courses
						 $tgp_earned_gsto = get_total_grade_point_earned_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem);
						 $tgp_earned_gst = $tgp_earned_gsto + $prev_tgp_earned_gst;



						 //previously earned from coure courses
						 $prev_tgp_earned_core0 = get_total_grade_point_earned_previously_nce($student,$programme_id,$course_category = 0, $level, $ses,$sem);

						 //presently earned from coure courses
						 $tgp_earned_core0 = get_total_grade_point_earned_nce($student,$programme_id,$course_category = 0, $level, $ses,$sem);

						 //$tgp_earned_core0 = $tgp_earned_core0o + $prev_tgp_earned_core0;



						 //declared to hold for previously
						 $prev_tgp_earned_core_array = array();


						 //declared to hold for present
						 $tgp_earned_core_array = array();



						 //previously  retreived single major or double major earned total grade point
						 if(strpos($prev_tgp_earned_core0, ',') !== false)//check
						 {

						 		for ($i = 0; $i < count($prev_tgp_earned_core0);$i++)
								{
									 $prev_tgp_earned_core_array = explode(',', $prev_tgp_earned_core0);


								}


								//pass the array to the two variables
								$prev_tgp_earned_core1 =  $prev_tgp_earned_core_array[0]; //first summed

								$prev_tgp_earned_core2 = $prev_tgp_earned_core_array[1]; //second summed

						 }
						 else
						 {
						 	//just collect the single department id
						 	$prev_tgp_earned_core1 = $prev_tgp_earned_core0;
						 }//end of previously retreived total grade point earned

						 //presently  retreived single major or double major earned total grade point
						 if(strpos($tgp_earned_core0, ',') !== false)//check
						 {

						 		for ($i = 0; $i < count($tgp_earned_core0);$i++)
								{
									 $tgp_earned_core_array = explode(',', $tgp_earned_core0);


								}


								//pass the array to the two variables
								$tgp_earned_core1o =  $tgp_earned_core_array[0]; //first summed
								$tgp_earned_core1 = $tgp_earned_core1o + $prev_tgp_earned_core1;

								$tgp_earned_core2o = $tgp_earned_core_array[1]; //second summed
								$tgp_earned_core2 = $tgp_earned_core2o +$prev_tgp_earned_core2;


						 }
						 else
						 {
						 	//just collect the single department id
						 	$tgp_earned_core1o = $tgp_earned_core0;
						 	$tgp_earned_core1 = $tgp_earned_core1o + $prev_tgp_earned_core1;

						 }





						 //get grade point earned previously
						  $prev_tgp_earned_cummulative =  $prev_tgp_earned_edu + $prev_tgp_earned_gst + $prev_tgp_earned_core1 + $prev_tgp_earned_core2;

						 	//get grade point earned
						 $tgp_earned_cummulativeo =  $tgp_earned_edu + $tgp_earned_gst + $tgp_earned_core1 + $tgp_earned_core2;
						 $tgp_earned_cummulative = $tgp_earned_cummulativeo ;//+ $prev_tgp_earned_cummulative



					      echo '<br/>';

					      //get previous cgpa
					      @$prev_gpa_edu = $prev_tgp_earned_edu / $prev_tcur_edu;


					      //get present gpa edu
					   	  @$gpa_edu = $tgp_earned_edu / $tcur_edu; /// $tcur_edu;

						  echo "Test PREV GPA EDU:" .number_format($prev_gpa_edu,2).'<br/>';


						  echo "Test GPA EDU:" .number_format($gpa_edu,2).'<br/>';





						  //previous gpa gst
						  @$prev_gpa_gst = $prev_tgp_earned_gst / $prev_tcur_gst;


						  //present gpa gst
						  @$gpa_gst = $tgp_earned_gst / $tcur_gst;

						  echo "Test PREV GPA GST:" .number_format($prev_gpa_gst,2).'<br/>';


						  echo "Test GPA GST:" .number_format($gpa_gst,2).'<br/>';



						  //previous gpa core 1
						  @$prev_gpa_core1 = $prev_tgp_earned_core1 / $prev_tcur_core1;

						  //present gpa core 1
						   @$gpa_core1 = $tgp_earned_core1 / $tcur_core1;


						echo "Test PREV GPA CORE 1:" .number_format($prev_gpa_core1,2).'<br/>';

						  echo "Test GPA CORE 1:" .number_format($gpa_core1,2).'<br/>';


						    //previous computed core 2 gpa
						  	 @$prev_gpa_core2 = $prev_tgp_earned_core2 / $prev_tcur_core2;

						     //present computed core 2
						     @$gpa_core2 = $tgp_earned_core2 / $tcur_core2;

						  echo "Test PREV GPA CORE 2:" .number_format($prev_gpa_core2,2).'<br/>';

						  echo "Test GPA CORE 2:" .number_format($gpa_core2,2).'<br/>';




						  	 @$prev_cgpa_cum = $prev_tgp_earned_cummulative / $prev_tcur_cummulative;

						  echo "PREV CGPA:" .number_format($prev_cgpa_cum,2).'<br/>';

						     @$cgpa_cum = $tgp_earned_cummulative / $tcur_cummulative;

						  echo "CGPA:" .number_format($cgpa_cum,2).'<br/>';





						  //logic to get current gpa for a particular semester
						  ///////////////////////////////////////////////////////
						  //////////////////////////////////////////////////////
						  @$current_gpa =  $tgp_earned_cummulativeo / $tcur_cummulativeo;
						  //////////////////////////////////////////////////////////
						  ///////////////////////////////////////////////////////////




						  //get outstanding courses remark per student if there is or not and if there is count
						  $oc_remark = get_remark_for_cummulative_per_student_nce($student,$programme_id,$level,$ses,$sem);

						  //count outstanding remark passed
						 if($oc_remark == 'PASSED'){ $count_passed = $count_passed + 1;}

						 //count outstanding remark for oc
						 if($oc_remark == 'OC'){$count_oc = $count_oc + 1;}




						 //collect the remarks for different criteria
						  $probation_remark = get_probation_remark_per_semester_nce($current_gpa,$level,$ses,$sem);

						  //count probation
						 if($probation_remark == 'PROB')
						 {
						 	$count_probation = $count_probation + 1;

						 }




		  				 ///////////////////////////////////////////////////////
		  				 /// start to work on probation remark
						 //before the next this get previous  gpa stored for a student
						 $pre_gpa = get_student_previous_gpa_nce($student,$programme_id,$level,$ses,$sem);
						  //check if the pre gpa is false


						 //get acad remark
						 if($pre_gpa == 'false') //
						 {
						  	$acad_with_remark = '';
						 }
						 else
						 {
						  	//get remark for academic withdrwal
						 	$acad_with_remark = get_acad_with_remark_per_semester_nce($student,$pre_gpa,$cgpa_cum,$level,$ses,$sem);//$cgpa_cumkeep for future is

						 }


						 //count acad with
						 if($acad_with_remark == 'ACAD WITH')
						 {
						 	$count_acad_with = $count_acad_with + 1;

						 	//hide
						 	$probation_remark = '';
						 	$oc_remark = '';
						 }

						 //////////////////////////////////////////////////////////////////////////////
						 //////////////////////////////////////////////////////////////////////////////
						 /////////////////// end of previous cga and acad withdrawal //////////////////


						 /////////////////////////////////////////////////////////////////////////////
						 ////////////// before we go lets get how many carry overs gotten by the
						 ////////////// candidates

						 //count the ocs
		  				 $no_oc = count_carry_overs_per_student_nce($student, $programme_id,$level,$ses, $sem);



		  				 $vol_with_remark = get_vol_with_remark_per_semester_nce($current_gpa); //$cgpa_cum keep for future use

		  				 //count vol with and hide acad with,
						 if($vol_with_remark == 'VOL WITH')
						 {
						 	$count_vol_with = $count_vol_with + 1;

						 	//hide
						 	$acad_with_remark = '';
						 	$probation_remark = '';
						 	$oc_remark = '';
						 }

						 //get remark for graduation

						    // $minimum_requirement fulfilment
								//check if Level is III or spilling
								if($level == 'III' && $sem == '2' && $oc_remark == 'PASSED')
								{
									$minimum_req_fulfilment = check_minimum_requirments_nce($student, $programme_id,$level,$ses, $sem);
									$graduation_remark = get_graduation_remark_nce($student, $programme_id,$level,$ses, $sem, $cgpa_cum);

											// // //count passed graduating students and other remarks
											 if($minimum_req_fulfilment == 'Pass')
											 {
											    $count_graduating_students = $count_graduating_students + 1;
											//
													 ////hide
													  $acad_with_remark = '';
													  $probation_remark = '';
													  $oc_remark = '';
													  $vol_with_remark = '';



											}
											else
											{
												// 	// = '';
													$not_graduation_remark = $minimum_req_fulfilment; //set outanding remark to be problems found on minimum required

													//clear others
													$acad_with_remark = '';
													$probation_remark = '';
													$oc_remark = '';
													$vol_with_remark = '';
													$graduation_remark ='';





											}

											//	exit();

								}







						 //gen remark for cummulative
						 //$gen_remark_compounded = $acad_with_remark.' ; '.$probation_remark.' ; '.$oc_remark.'-'.$no_oc;
						  $gen_remark_compounded = get_compounded_remark_nce($vol_with_remark, $acad_with_remark,$repeat_with_remark,$probation_remark,$oc_remark,$no_oc,$minimum_req_fulfilment,$graduation_remark,$not_graduation_remark);

						 //get remark for different courses
						 $distinct_remark_oc = get_oc_courses_nce($student,$programme_id, $level, $ses, $sem);


					//insert with a unique token by session and by semester
					//write a fucntion to insert the student course nce result table


					// //check if inserted already using token and other parameters
							if(!check_if_student_computatation_exist($student, $ses, $sem, $level))
							{


								$q5 = "INSERT INTO `students_results_nce`(`student_id`, `programme_id`,
													`session_id`, `semester`, `level`, `delete_token`, `result_date_added`, `status`,
													 `date_added`, `added_by`,
													`edu_ptcur`, `edu_tcur`, `edu_ptcue`, `edu_tcue`, `edu_ptgpe`, `edu_tgpe`
													,`edu_pcgpa`,`edu_cgpa`,
													`gst_ptcur`, `gst_tcur`, `gst_ptcue`, `gst_tcue`, `gst_ptgpe`, `gst_tgpe`
													,`gst_pcgpa`,`gst_cgpa`,
													`core1_ptcur`, `core1_tcur`, `core1_ptcue`, `core1_tcue`, `core1_ptgpe`, `core1_tgpe`,`core1_pcgpa`,`core1_cgpa`,
													`core2_ptcur`, `core2_tcur`, `core2_ptcue`, `core2_tcue`,
													`core2_ptgpe`, `core2_tgpe`,`core2_pcgpa`,`core2_cgpa`,
													`cum_ptcur`, `cum_tcur`, `cum_ptcue`,`cum_tcue`,
													`cum_ptgpe`, `cum_tgpe`,`cum_pcgpa`,`cum_cgpa`,
													`prev_gpa`,`cur_gpa`,`remark1`,`remark2`)

									  VALUES ('$student','$programme_id','$ses','$sem', '$level','$token',NOW(),1,NOW(), '$security',
											  '$prev_tcur_edu','$tcur_edu','$prev_tcu_earned_edu','$tcu_earned_edu','$prev_tgp_earned_edu','$tgp_earned_edu','$prev_gpa_edu','$gpa_edu',
											  '$prev_tcur_gst','$tcur_gst','$prev_tcu_earned_gst','$tcu_earned_gst','$prev_tgp_earned_gst','$tgp_earned_gst','$prev_gpa_gst','$gpa_gst',
											  '$prev_tcur_core1','$tcur_core1','$prev_tcu_earned_core1','$tcu_earned_core1','$prev_tgp_earned_core1','$tgp_earned_core1','$prev_gpa_core1','$gpa_core1',
											  '$prev_tcur_core2','$tcur_core2','$prev_tcu_earned_core2','$tcu_earned_core2','$prev_tgp_earned_core2','$tgp_earned_core2','$prev_gpa_core2','$gpa_core2',
											  '$prev_tcur_cummulative','$tcur_cummulative','$prev_tcu_earned_cummulative','$tcu_earned_cummulative','$prev_tgp_earned_cummulative','$tgp_earned_cummulative','$prev_cgpa_cum','$cgpa_cum',
											  '$pre_gpa','$current_gpa','$distinct_remark_oc','$gen_remark_compounded')";

								$r5 = mysqli_query($dbc,$q5);

								if($r5) { /*die("<span class='btn btn-danger'>SERVER ERROR: Please retry again</span>");*/ }
						 	}
						 	else
						 	{

						 		$q6 = "UPDATE  `students_results_nce`  SET  `result_date_added` = NOW() , `status` = 1,
													`date_added` = NOW() , `added_by` = '$security',
													`edu_ptcur` = '$prev_tcur_edu', `edu_tcur` = '$tcur_edu',
													`edu_ptcue` ='$prev_tcu_earned_edu' , `edu_tcue` = '$tcu_earned_edu',
													`edu_ptgpe` = '$prev_tgp_earned_edu' , `edu_tgpe`='$tgp_earned_edu',
													`edu_pcgpa` ='$prev_gpa_edu',`edu_cgpa` = '$gpa_edu',

												`gst_ptcur`= '$prev_tcur_gst', `gst_tcur` ='$tcur_gst', `gst_ptcue` = '$prev_tcu_earned_gst', `gst_tcue` = '$tcu_earned_gst', `gst_ptgpe` = '$prev_tgp_earned_gst', `gst_tgpe` = '$tgp_earned_gst'
													,`gst_pcgpa` = '$prev_gpa_gst',`gst_cgpa`= '$gpa_gst',


												`core1_ptcur` = '$prev_tcur_core1', `core1_tcur` = '$tcur_core1', `core1_ptcue` = '$prev_tcu_earned_core1' , `core1_tcue` = '$tcu_earned_core1', `core1_ptgpe` = '$prev_tgp_earned_core1', `core1_tgpe`='$tgp_earned_core1',`core1_pcgpa` = '$prev_gpa_core1' , `core1_cgpa`='$gpa_core1',

												`core2_ptcur` = '$prev_tcur_core2', `core2_tcur` = '$tcur_core2', `core2_ptcue` = '$prev_tcu_earned_core2', `core2_tcue` = '$tcu_earned_core2',
													`core2_ptgpe` = '$prev_tgp_earned_core2', `core2_tgpe` = '$tgp_earned_core2',`core2_pcgpa` = '$prev_gpa_core2' ,`core2_cgpa` = '$gpa_core2',

												`cum_ptcur` = '$prev_tcur_cummulative', `cum_tcur` = '$tcur_cummulative', `cum_ptcue` = '$prev_tcu_earned_cummulative' ,`cum_tcue` = '$tcu_earned_cummulative',
													`cum_ptgpe` = '$prev_tgp_earned_cummulative', `cum_tgpe` =
													'$tgp_earned_cummulative',
													`cum_pcgpa` = '$prev_cgpa_cum',`cum_cgpa` = '$cgpa_cum',
													`prev_gpa` = '$pre_gpa',`cur_gpa` = '$current_gpa',
												`remark1` = '$distinct_remark_oc',`remark2`= '$gen_remark_compounded'


										WHERE `student_id` = '$student' AND  `programme_id` = '$programme_id'  AND
											  `session_id` = '$ses' AND `semester` = '$sem' AND `level` = '$level'";


								 $r6 = mysqli_query($dbc,$q6);

								if($r6) { /*die("<span class='btn btn-danger'>SERVER ERROR: Please retry again</span>");*/ }

						 	}


						}//end of if on acad with from last semesterS

						}//end of while looping tru students
					}//end of if result


							if($r5)
							{



							 	//check if ras is true before switching


					  //         // header("Location: gateway.php?act=course_registration0&is=Course resgistration is done, please click on course registration history to print&qlk=".md5(3));
							 }


							 //check if aggregate summary exist
								if(!check_if_programme_aggregate_summary_exist($programme_id, $ses, $sem, $level))
								{
									// if not added insert
									$qas = "INSERT INTO `students_results_programme_remark_nce`
											(`programme_id`, `session_id`, `semester`, `level`, `delete_token`,
											`result_date_added`,  `date_added`,
											`added_by`,  `passed`, `reoffers`, `probation`, `acad_with`,
											 `vol_with`)

									VALUES ('$programme_id','$ses','$sem','$level','$token',NOW(),NOW(),'$security',
										   '$count_passed','$count_oc','$count_probation','$count_acad_with',
											'$count_vol_with')";

									$ras = mysqli_query($dbc,$qas);

									if(!$ras) { /*die("<span class='btn btn-danger'>SERVER ERROR: Error Inserting Second Table</span>"); */}

								}
								else
								{
									$qas = "UPDATE `students_results_programme_remark_nce` SET
											`date_modified`=NOW(), `modified_by`='$security',`passed`='$count_passed',
											`reoffers`='$count_oc',`probation`='$count_probation',`acad_with`=
											'$count_acad_with',`vol_with`='$count_vol_with'

											WHERE `programme_id` = '$programme_id' AND `session_id`='$ses'
												  AND `semester`= '$sem' AND `level`='$level'";
									$ras = mysqli_query($dbc,$qas);


								}


					           //check
							 	echo "Done PROB:- $count_probation RE-OFFERS $count_oc PASSED:  $count_passed ACAD WITH: $count_acad_with VOL WITH:  $count_vol_with";





		//	}//end of result is avilable




		}//end of



//prevent overwritten
// $now = date('YmdHis');
// $tb_name = 'temp'.$now;//get table name

mysqli_close(dbc());

	} //end of if post is action is given


?>
