<?php


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



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


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


if($_REQUEST['act'] == 'compute_rst_nce' )
{
	//collect the course id
	//if(isset($_REQUEST['cid']))
	//{
		$course_id = $_REQUEST['cid'];//course id
		$pid = $_REQUEST['pid'];//programme id
		$ses = 3; //$_REQUEST['ses'];//session passed
		$sem = 1; //$_REQUEST['sem'];//semster passed





//check if a particular matric number is on the system



	//start with assuming the id of student
	$student = 2551;

	$dept_id = get_student_department_id_nce($student);
	$school_id = get_student_school_id_nce($student);
	$programme_id = get_student_programme_id_nce($student); //get course if
	$level = 'I';//get_student_level_nce($student);


//echo "I should work nw";

	 $tcur_edu = get_total_credit_unit_registered_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem).'<br/>';

	 echo 'TCUR EDU = '.$tcur_edu;

	 $tcur_gst = get_total_credit_unit_registered_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem).'<br/>';

	 echo 'TCUR GST = '.$tcur_gst;


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

	 //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);

	 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_core1 =  $tcur_core_array[0]; //first summed

			$tcur_core2 = $tcur_core_array[1]; //second summed

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



	 echo 'TCUR CORE 1 = '.$tcur_core1.'<br/>';

	  echo 'TCUR CORE 2 = '.$tcur_core2.'<br/>';





	 $tcur_cummulative =  $tcur_edu + $tcur_gst + $tcur_core1 +$tcur_core2;

	 echo 'TCUR CORE CUM. = '.$tcur_cummulative.'<br/>';





//get total credit unit
$tcu_earned_edu = get_total_credit_unit_earned_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem).'<br/>';

	 echo 'TCUR EARNED EDU = '.$tcu_earned_edu;

	 $tcu_earned_gst = get_total_credit_unit_earned_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem).'<br/>';

	 echo 'TCUR _earned GST = '.$tcu_earned_gst;


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

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

	 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_core1 =  $tcu_earned_core_array[0]; //first summed

			$tcu_earned_core2 = $tcu_earned_core_array[1]; //second summed

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



	 echo 'TCU _earned CORE 1 = '.$tcu_earned_core1.'<br/>';

	  echo 'TCU _earned CORE 2 = '.$tcu_earned_core2.'<br/>';





	 $tcu_earned_cummulative =  $tcu_earned_edu + $tcu_earned_gst + $tcu_earned_core1 +$tcu_earned_core2;

	 echo 'TCU _earned CORE CUM. = '.$tcu_earned_cummulative.'<br/>';




//////////////////////######################### work on total grade point #################################///////
	 $tgp_earned_edu = get_total_grade_point_earned_nce($student,$programme_id,$course_category = 2, $level, $ses,$sem);


	$tgp_earned_gst = get_total_grade_point_earned_nce($student,$programme_id,$course_category = 1, $level, $ses,$sem);

	 $tgp_earned_core0 = get_total_grade_point_earned_nce($student,$programme_id,$course_category = 0, $level, $ses,$sem);

	  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_core1 =  $tgp_earned_core_array[0]; //first summed

			$tgp_earned_core2 = $tgp_earned_core_array[1]; //second summed

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



	 echo 'TGP CORE 1 = '.$tgp_earned_core1.'<br/>';

	  echo 'TGP CORE 2 = '.$tgp_earned_core2.'<br/>';





     echo $gpa_edu = $tgp_earned_edu/$tcur_edu; /// $tcur_edu;

	  echo "Test GPA EDU:" .$gpa_edu.'<br/>';

	  //echo "Test GPA GST:" .$tgp_earned_gst / $tcur_gst.'<br/>';







$added_by = get_user_id();
$session = get_current_session(1);
$semester = get_current_semester(1);

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




 if(isset($_FILES['file']))
 {
	 //check for the file uploads

	  $file_name = $_FILES['file']['name'];
      $file_size =$_FILES['file']['size'];
      $file_tmp =$_FILES['file']['tmp_name'];
      $file_type=$_FILES['file']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));

      $expensions= array("csv");


	  if(in_array($file_ext,$expensions)=== false)
	  {
         $errors[1]="File not allowed, please choose a CSV file.";
		 header("Location:gateway.php?act=view_course_assessments_students_list&cid=".$course_id."&pid=".$pid."&ses=".$ses."&sem=".$sem."&error=".$errors[1]);
      }

      if($file_size > 500000)
	  {
         $errors[2]='File size must be exatly 50KB';
		 header("Location:gateway.php?act=view_course_assessments_students_list&cid=".$course_id."&pid=".$pid."&ses=".$ses."&sem=".$sem."&error=".urlencode($errors[2]));


      }



      if(empty($errors)==true)
	  {


			  //cretae the temporary table
			 $q0 = "CREATE TEMPORARY TABLE ".$tb_name."(
			  number VARCHAR(200)  NOT NULL,
			  name VARCHAR(300) NOT NULL,
			  gender VARCHAR(10) NOT NULL,
			  ca_score  DOUBLE(10,2)  NOT NULL,
			  exam_score  DOUBLE(10,2)  NOT NULL)" ;

			$r0 = mysqli_query($dbc,$q0);


		 	$file_name = $now.$file_name;

			$file_path = "uploads/";


         	move_uploaded_file($file_tmp,$file_path.$file_name);
			//$q=mysqli_query(dbc(),"UPDATE students_prence SET image='$file_name' WHERE id='$user_id'") ;
			//if($q)
			//header('location:dashboard.php?is=image uploaded succesfully');


			$file = $file_path.$file_name;


			$q1 = "LOAD DATA LOCAL INFILE '".$file."' INTO TABLE ".$tb_name."  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
			LINES TERMINATED BY '\n' IGNORE 1 LINES
			(number,name,gender,ca_score,exam_score);";
			$r1 = mysqli_query($dbc,$q1);

			if($r1)
			{
				//echo "it works";
			}

			$grade = get_student_grade_by_score($total_score);

			$q2 = "UPDATE students_courses_nce  JOIN ".$tb_name." on ";
			$q2 .= " students_courses_nce.number = ".$tb_name.".number";
			$q2 .= " set ";
			$q2 .= $ca = " students_courses_nce.ca_score = ".$tb_name.".ca_score ";
			$q2 .= " ,students_courses_nce.exam_score = ".$tb_name.".exam_score,";
			$q2 .= " students_courses_nce.total_score = ".$tb_name.".ca_score + ".$tb_name.".exam_score ";
			//
//			$gr = get_student_grade_by_score($ts);
//
//			if($ca > 30)
//			{
//				$errors['error'] =  'Ensure you comply with rule.';
//
//				header("Location: gateway.php?act=view_course_assessments_students_list&error=".urlencode($errors['error'])."");
//
//			}
//
//
			//$grade = get_student_grade_by_score("students_courses_nce.");


			//$q2 .= " students_courses_nce.grade = '".$gr."'";

			$q2 .= " WHERE ";
			$q2 .= " md5(students_courses_nce.course_id) = '$course_id' AND students_courses_nce.session_id = '$session'";
			$q2 .= " AND students_courses_nce.semester = '$sem'";


			$r2 = mysqli_query($dbc,$q2);
			$sn = 0 ;

			// done
			if($r2)
			{
			  $info_success['success'] = "Your database is imported successfully. You have updated the records";
			  header("Location: gateway.php?act=view_course_assessments_students_list&cid=".$course_id."&pid=".$pid."&ses=".$ses."&sem=".$sem."&is=".urlencode($info_success['success']).""); //is - info success

			}
			else
			{
				die();

				$errors['error'] =  'DB not updated due to a system error. We apologize for any inconvenience.';

				header("Location: gateway.php?act=view_course_assessments_students_list&pid=".$pid."&ses=".$ses."&sem=".$sem."&error=".urlencode($errors['error'])."");
			}


		  # Delete file
        unlink($file_path.$file_name);



	    # Delete temporary table
		$ql="DROP TEMPORARY TABLE ".$tb_name.";";
		$rl = mysqli_query($dbc,$ql);







	  }//end of if no error in file uploaded


 }//end of if file has value








	} //end of if post is action is given


?>
