<?php


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



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


//include '../functions.php';


if($_REQUEST['act'] == 'view_course_upload_students_list' )
{
	//collect the course id
	if(isset($_REQUEST['cid']))
	{
		$course_id = $_REQUEST['cid'];
	}


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





		$file = $_FILES['file']['tmp_name'];//PED-114-2017-2018
		
		
		ini_set('auto_detect_line_endings',TRUE);
		//$file = 'uploads/PED-114-2017-2018.csv';
		$handle = fopen($file, "r");
		$c = 1;
		
		while(($filesop = fgetcsv($handle)) !== false)
		{
			$name = $filesop[0];
			$number = $filesop[1];
			$ca_score = $filesop[2];
			$exam_score = $filesop[3];
			
			$total_score = $ca_score + $exam_score;//total score
			
			$grade = get_student_grade_by_score($total_score);
			
			$student_id = get_student_id_by_matric_no($number);
			
			
			//get candidates id
			
			//get session
			
			//get delete token
			
			//get course id
			
			//collect matric numbers and pass for check wallahi if not in originall list warning , if he tries it again lock him out
			
			//get the score and dont allow more than limit ca
			
			
			$sql = mysqli_query(dbc(),"UPDATE `students_courses_nce` 
								SET `ca_score`='$ca_score',`exam_score`='$exam_score',`total_score`='$total_score',`grade`='$grade',
								`result_date_added`=NOW(),`status`=0,`date_added`=NOW(),`added_by`='$added_by'
								
								WHERE session_id = '$session'
								AND semester = '$semester'
								AND md5(course_id) = '$course_id'
								AND student_id = '$student_id'");
										 
			
			$c = $c + 1;
		}
		
			ini_set('auto_detect_line_endings',FALSE);
		 	fclose($handle);
		
            
            if($sql)
            {
                $info_success['success'] = "Your database has imported successfully. You have inserted ". $c ." records";
                header("Location: gateway.php?act=view_course_assessments_students_list&cid=".$course_id."&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&error=".urlencode($errors['error'])."");
            }
            
			
	




exit();







	} //end of if post is action is given


?>



















