<?php



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









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


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


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

//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'] = $department;
	
  }



  //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['programme'] == '')
  {
	   $programme = (!empty($_POST['sub_programme']))? (mysqli_real_escape_string($dbc,$_POST['sub_programme'])): NULL;
       $_SESSION['prog'] = mysqli_real_escape_string($dbc, $_POST['sub_programme']);
	  // $errors[] = '<p> * </p>';
	 
   
  }
  else
  {
	  $programme = mysqli_real_escape_string($dbc, $_POST['programme']);
	  $_SESSION['prog'] = $programme;
	
  }
  
  
  
  
  //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 actual programme id 
 $actual_programme_id = get_programme_actual_programme_id($programme);

 
 
 
if(empty($errors))
{
	//destroy session if there is no error
	
	unset($_SESSION['programme_type']);
	unset($_SESSION['prog']);
    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` (`id`,`department_id`, `programme_type_id`, `programme_id`,`actual_programme_id`, `course_id`, `level`, `seen_as_elective`, `session_id`,`date_added`, `added_by` , `status` , `token` ) 
				VALUES (NULL, '$search_department', '$programme_type', '$programme', '$actual_programme_id', '$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_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_programmes_courses&error=".urlencode($errors['error'])."");

}



?>