<?php

//so check if confirm courses is passed

if(($_POST['act']== 'confirm_edited_rc')) {


$student_id = $user_id;
$school_id = get_user_school_id($user_id);
$dept_id = get_user_department_id($user_id);
$programme_id = get_user_programme_id($user_id); //get programme id if for a user
$level = get_user_level($user_id); //get user level 

$session = get_current_session_4($id = '4'); //current session is always
$mat_no = get_user_mat_no($user_id);

	//insert unique token incase of delete or update
	$token = delete_course_token();
	
	$id_array = $_SESSION['course']; // return array
	//print_r($id_array);
	$id_count = count($_SESSION['course']); // count array
	
	for($i=0; $i < $id_count; $i++) {
			 $id = $id_array[$i];
			
			 $course_unit = get_course_unit($id); //collect unit to add
		     $course_semester = get_course_semester($id);//collect course semester for showing base on semester
			 
			 $course_dept_id = get_course_dept_id($id);
			 $course_category = get_course_category($id);

 			 //get course level from designed programme courses table by passing users programme_id, and courses he choose to register
			  $course_level = get_course_level_from_pc($programme_id, $id); 
			 
			 
			 //get seen_as_elective from designed programme courses table by passing users programme_id, and courses he choose to register
			 $seen_as_elective = get_seen_as_elective_from_pc($programme_id, $id); 
	
			 $course_token = get_course_token($id, $course_level,$student_id);
			
	
	
	  //delete all the course id 
	  $q6 = "DELETE FROM students_courses_nce WHERE delete_token = '$course_token' AND session_id = '$session' AND student_id = '$student_id'";
	  $r6 = mysqli_query($dbc,$q6);
	  if(!$r6) { die("<span class='btn btn-danger'>SERVER ERROR: Please retry again</span>"); }
		
	
		
	//INSERT INTO perm TB	
			
			//check if course already exist
		 $cs_exist = course_exist_on_students_courses_nce($student_id,$id,$level ,$session);
		
		if (!$cs_exist) {

			//INSERT INTO perm TB	
			$q5 = "INSERT INTO students_courses_nce ( student_id, programme_id, course_id, course_unit,seen_as_elective,session_id, semester, level, delete_token, date_added,
				`number`,course_category,course_department_id) 
			
				  VALUES ('$student_id','$programme_id','$id','$course_unit','$seen_as_elective','$session','$course_semester', '$level','$token',NOW(),'$mat_no','$course_category','$course_dept_id')";
				
			$r5 = mysqli_query($dbc,$q5);
			
			//if insertin is successful
			//clear the session 'course'
			unset($_SESSION['course']);
			unset($_SESSION['cs']);
			
			
			if(!$r5) { die("<span class='btn btn-danger'>SERVER ERROR: Please retry again</span>"); }
		}
	
		
	}//end of for
	
		
		if($r5  || $cs_exist)
		{
			//echo '<script type="text/javascript">jQuery(function($) { window.location="'.'gateway.php'.'";})';
            header("Location: dashboard.php?act=course_registration0&is=Course resgistration is updated, please click on course registration history to re-print&qlk=".md5(3));
		}
	

}//end of confirm rc

?>