<?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 department
  if($_POST['department'] == '0')
  {
	   $errors[] = '<p> * </p>';
	 
   
  }
  else
  {
	  $department= mysqli_real_escape_string($dbc, $_POST['department']);
	  $_SESSION['department'] = $department;
	
  }
  
  //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 programme
  if($_POST['programme'] =='0')
  {
	   $errors[] = '<p> * </p>';
	 
   
  }
  else
  {
	  $programme= mysqli_real_escape_string($dbc, $_POST['programme']);
	  $_SESSION['programme'] = $programme;
	
  }	
  
  
  
  //validate the level
  if($_POST['level'] == '0')
  {
	   $errors[] = '<p> * </p>';
	 
   
  }
  else
  {
	  $level = mysqli_real_escape_string($dbc, $_POST['level']);
	  $_SESSION['level'] = $level;
	
  }	  



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





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





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



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


//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);



 
 
 
if(empty($errors))
{
	//destroy session if there is no error
	
	unset($_SESSION['title']);
	unset($_SESSION['unit']);
	unset($_SESSION['programme']);
	unset($_SESSION['description']);
	unset($_SESSION['description1']);
	unset($_SESSION['type']);
	unset($_SESSION['code']);
	unset($_SESSION['department']);
	unset($_SESSION['programme_type']);
	unset($_SESSION['level']);
	unset($_SESSION['semester']);
	unset($_SESSION['status']);
		
	
	 //check if the query is for adding den add
	
	
		$q = "INSERT INTO `courses` (`id`, `title`, `code`, `unit`,`type_id`, `department_id`, `programme_id`,  `description`,`course_description`,`course_type`,`level`,`semester`, 
		`date_added`, `added_by` , `status` ) 
				VALUES (NULL,'$title', '$code', '$unit','$programme_type', '$department', '$programme', '$description', '$description1', '$type', '$level', '$semester', NOW(),
				'$security_id' ,'$status')";
							
							
		$r = mysqli_query($dbc,$q);
	
	if($r)
	{
		$info_success['success'] = 'Successfully added!';
		header("Location: gateway.php?act=view_courses&is=".urlencode($info_success['success']).""); //is - info success
		
	}
	else
	{
		$errors['error'] =  'Course not added due to a system error. We apologize for any inconvenience.';
										
		header("Location: gateway.php?act=add_courses&error=".urlencode($errors['error'])."");
	}
	
}
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_courses&error=".urlencode($errors['error'])."");

}



?>