<?php session_start();
include("connect.php");


if(isset($_SESSION['stureg_id'])){
	$ddd=mysqli_query($con,"select * from student where id='".$_SESSION['stureg_id']."'");
	$me=mysqli_fetch_assoc($ddd);
	
	$bam=mysqli_query($con,"select * from `programme` where id='".$me['programme_id']."'");
	$pro=mysqli_fetch_assoc($bam);
	
	$bxm=mysqli_query($con,"select * from `department` where id='".$me['dept_id']."'");
	$dep=mysqli_fetch_assoc($bxm);
	
	$bm=mysqli_query($con,"select * from `faculty` where id='".$dep['fac_id']."'");
	$fac=mysqli_fetch_assoc($bm);
	
	$bc=mysqli_query($con,"select * from `semester` where programme_id='".$me['programme_id']."' AND registration_control=1 AND session !='' order by id desc limit 1");
	$sem=mysqli_fetch_assoc($bc);
}


if(isset($_POST['submit_registration'])){
	$bb=mysqli_query($con,"select * from `semester` where programme_id='".$me['programme_id']."' AND registration_control=1 AND session !='' order by id desc limit 1");
	$res=mysqli_fetch_assoc($bb);
	
		if(isset($_POST['select_course'])){
			
			$count=count($_POST['select_course']);
			if($count>0){
				$mm=$unit_count=0;
			$exp=explode(",",$_POST['show_selected']);

for($i=0; $i<=count($exp); $i++){
	if($exp[$i]!=''){
		$cid=str_ireplace(",","",$exp[$i]);
$nan=mysqli_query($con,"select * from course where id='$cid'");
$cos=mysqli_fetch_assoc($nan);


$ban=mysqli_query($con,"select * from reg_course where sid='".$me['id']."' AND matric_no='".$me['matric_no']."' AND level='".$_POST['submit_registration']."' AND programme_id='".$me['programme_id']."' AND programme='".$pro['name']."' AND session='".$sem['session']."' AND LCASE(course_code)='".strtolower($cos['course_code'])."'");

if(mysqli_num_rows($ban)==0){
	
	$sql=mysqli_query($con,"insert into reg_course(sid,matric_no,level,dep_id,course_id,course_code,description,unit,status,programme_id,programme,programme_description,semester_num,semester_description,session,date) values('".$me['id']."','".$me['matric_no']."','".$_POST['submit_registration']."','".$me['dept_id']."','".$cos['id']."','".$cos['course_code']."','".$cos['description']."','".$cos['unit']."','".$cos['status']."','".$pro['id']."','".$pro['name']."','".$pro['description']."','".$sem['semester']."','".$sem['description']."','".$sem['session']."','$date')");
	
	if($sql){
		$mm=$mm+1;
		$unit_count=$unit_count+$cos['unit'];
	}else{
		$mm=$mm+0;
		$unit_count=$unit_count+0;
	}	
}
}
}
		if($mm>0){
			
			if(!isset($_SESSION['extra_year_student']) || $_SESSION['extra_year_student']!='extra_year_student'){	// non-extra year student
			
		if($unit_count < $res['min_tnu']){		
				
		mysqli_query($con,"delete from reg_course where sid='".$me['id']."' AND matric_no='".$me['matric_no']."' AND level='".$_POST['submit_registration']."' AND dep_id='".$me['dept_id']."' AND programme_id='".$pro['id']."' AND programme='".$pro['name']."' AND session='".$sem['session']."' AND semester_num='".$sem['semester']."' AND semester_description='".$sem['description']."'");	
				
		echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh No!</strong> Total number of units cannot be lesser than '.$res['min_tnu'].'.
              </div>';
			  
		}else if($unit_count > $res['max_tnu']){
				
		mysqli_query($con,"delete from reg_course where sid='".$me['id']."' AND matric_no='".$me['matric_no']."' AND level='".$_POST['submit_registration']."' AND dep_id='".$me['dept_id']."' AND programme_id='".$pro['id']."' AND programme='".$pro['name']."' AND session='".$sem['session']."' AND semester_num='".$sem['semester']."' AND semester_description='".$sem['description']."'");	
				
		echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh No!</strong> Total number of units cannot be more than '.$res['max_tnu'].'.
              </div>';
			  
			}else{
				
			unset($_SESSION['grab_course']);
			unset($_SESSION['student_level']);
			
			echo '<script>
			alert("Course Registeration Successful!")
			location.href="course_form.php"
			</script>';
				
			}
			
		}else{
			
			if($unit_count > $res['max_tnu']){
				
		mysqli_query($con,"delete from reg_course where sid='".$me['id']."' AND matric_no='".$me['matric_no']."' AND level='".$_POST['submit_registration']."' AND dep_id='".$me['dept_id']."' AND programme_id='".$pro['id']."' AND programme='".$pro['name']."' AND session='".$sem['session']."' AND semester_num='".$sem['semester']."' AND semester_description='".$sem['description']."'");	
				
		echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh No!</strong> Total number of units cannot be more than '.$res['max_tnu'].'.
              </div>';
			  
			}else{
				
			unset($_SESSION['grab_course']);
			unset($_SESSION['student_level']);
			unset($_SESSION['extra_year_student']);
			echo '<script>
			alert("Course Registeration Successful!")
			location.href="course_form.php"
			</script>';
			
			}
			
			
			
		}
			
		}else{
			echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh Sorry!</strong> Unable to complete operation, kindly check request and try again.
              </div>';
		}
			}else{
				echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh No!</strong> No course selected.
              </div>';
			}
			
		}else{
			echo '<div class="alert alert-dismissible alert-danger">
				<strong>Oh No!</strong> No course selected.
              </div>';
		}
	
}








?>













 