<?php



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



//check the id passed
if(isset($_POST['programmes_courses']))
{
	$token = $id =  $_POST['programmes_courses'];
}





//}

//validate the level
  if($_POST['level'] == '0')
  {
	   $errors[] = '<p> * </p>';
	 
   
  }
  else
  {
	  $level = mysqli_real_escape_string($dbc, $_POST['level']);
	  $_SESSION['level'] = $level;
	
  }	  

	  //validate the programme_type
  if($_POST['programme_type'] == '0')
  {
	   $errors[] = '<p> * </p>';
	 
   
  }
  else
  {
	  $programme_type = mysqli_real_escape_string($dbc, $_POST['programme_type']);
	  $_SESSION['programme_type'] = $programme_type;
	
  }


  //validate the search_department
  if($_POST['search_department'] == '0')
  {
	   $errors[] = '<p> * </p>';
	 
   
  }
  else
  {
	  $search_department = mysqli_real_escape_string($dbc, $_POST['search_department']);
	  $_SESSION['search_department'] = $search_department;
	
  }
	
	
	
  //validate the programme
  if($_POST['programmes'] == '0')
  {
	  
	  $errors[] = '<p> * </p>';
	  
	  // $errors[] = '<p> * </p>';
	 
   
  }
  else
  {
	  //$programme = mysqli_real_escape_string($dbc, $_POST['programmes']);
	  $programme = (!empty($_POST['programmes']))? (mysqli_real_escape_string($dbc,$_POST['programmes'])): NULL;
      $_SESSION['prog'] = mysqli_real_escape_string($dbc, $_POST['programmes']);
	
  }
  
  
  
  
  //validate the level2
  if($_POST['level2'] =='0')
  {
	   $errors[] = '<p> * </p>';
	 
   
  }
  else
  {
	  $level2 = mysqli_real_escape_string($dbc, $_POST['level2']);
	  $_SESSION['level2'] = $level2;
	
  }	
  
  
  
  
 
  
  
   //validate the department2
  if($_POST['department2'] =='0')
  {
	   $errors[] = '<p> * </p>';
	 
   
  }
  else
  {
	  $department2 = mysqli_real_escape_string($dbc, $_POST['department2']);
	  $_SESSION['department2'] = $department2;
	
  }	
  
  
  
  




//collect the status
$status = (!empty($_POST['status']))? (mysqli_real_escape_string($dbc,$_POST['status'])): 
 NULL;
$_SESSION['status'] = $status;



//finaly get security id
$user_id = get_user_id();


$security_id = get_security_id($user_id);


$current_session = get_current_session('1');

//$token = generate_token();


//get school id for a particular programme so to attached
$school_id = get_school_id_by_programme($programme);
 
 
 
if(empty($errors))
{
	//destroy session if there is no error
	
	unset($_SESSION['programme_type']);
    unset($_SESSION['programmes']);
    unset($_SESSION['search_department']);
	unset($_SESSION['department2']);
	unset($_SESSION['level']);
	unset($_SESSION['level2']);
	unset($_SESSION['status']);
		
	
	
	
	
	
	
		
	
	$id_array = $_POST['course']; // return array
	$id_count = count($_POST['course']); // count array
	
	for($i=0; $i < $id_count; $i++) {
		$cid = $id_array[$i];//collect the course by id
		
		
		$seen_as_elective = $_POST['seen_as_elective'.$cid];//collect seen as elective;
		
			  		
					  
	
		$q = "INSERT INTO `programmes_courses_nce` (`id`,`school_id`,`department_id`, `programme_type_id`, `programme_id`, `course_id`, `level`, `seen_as_elective`, `session_id`,`date_added`, `added_by` , `status` , `token` ) 
				VALUES (NULL, '$school_id','$search_department','$programme_type', '$programme', '$cid', '$level', '$seen_as_elective', '$current_session', NOW(), '$security_id' ,'$status' ,'$token')";
							
							
		$r = mysqli_query($dbc,$q);
	
		if($r)
		{
			$info_success['success'] = 'Successfully added!';
			header("Location: gateway.php?act=view_programmes_courses&is=".urlencode($info_success['success']).""); //is - info success
		
		}
		else
		{
			$errors['error'] =  'Courses not attached due to a system error. We apologize for any inconvenience.';
											
			header("Location: gateway.php?act=add_supplementry_programmes_courses&error=".urlencode($errors['error'])."");
		}
	}//end of loop
}
else
{//stay back in departments form


	$errors['error'] =  'Fill the form appropriately, take note of <span class="ast">*</span>.';
										
	header("Location: gateway.php?act=add_supplementry_programmes_courses&error=".urlencode($errors['error'])."");

}


?>