<?php


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



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


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


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


$added_by = get_user_id();
$session = $ses;// 5;//get_current_session(1);
$semester = $sem; //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 summer_students_courses_nce  JOIN ".$tb_name." on ";
			$q2 .= " summer_students_courses_nce.number = ".$tb_name.".number";
			$q2 .= " set ";
			$q2 .= $ca = " summer_students_courses_nce.ca_score = ".$tb_name.".ca_score";
			$q2 .= " ,summer_students_courses_nce.exam_score = ".$tb_name.".exam_score,";
			$q2 .= " summer_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(summer_students_courses_nce.course_id) = '$course_id' AND summer_students_courses_nce.session_id = '$session'";
			$q2 .= " AND summer_students_courses_nce.semester = '$sem'";


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

			// done
			if($r2)
			{


				//track scores entered by staffs
				track_staff_score_entry($added_by, $course_id, $session, $sem,$pid);



			  $info_success['success'] = "Your database is imported successfully. You have updated the records";
			  header("Location: gateway.php?act=view_course_assessments_students_list_summer&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_summer&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


?>
