<?php
include_once('../../../db_connect/db.php');
include_once '../php/functions.php';





//get current session
$current_session = get_current_session($conn_1, $id = 1);//deprecatd for now
$session_title = get_session_title($conn_1,$current_session);//passed
$session_titlet = str_replace('/','-',$session_title);


//get the  course ttitle
$code =  "List of allocated students";
$code = str_replace(' ','-',$code);




$title = $code.'-'.$session_titlet;






header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='.$title.'.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings
fputcsv($output, array('STUDENT NAME', 'MATRIC NUMBER','GENDER','PROGRAMME','HOSTEL','BLOCK','ROOM'));




$rows = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE session_id='$current_session'
  ORDER BY campus_id ASC, block_id ASC, room_id ASC ");

while ($row = mysqli_fetch_array($rows, MYSQLI_ASSOC))
{

      $type_id = $row['type_id'];
      $student_id = $row['student_id'];
      $campus_id = $row['campus_id'];
      $block_id = $row['block_id'];
      $room_id = $row['room_id'];

      //get his room title
      $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_rooms WHERE id='$room_id' ") or die(mysqli_error($conn_1));
      $sql_room = mysqli_fetch_assoc($allocate);
      $Room_title = $sql_room['title'];

      //get his room title
      $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_campuses WHERE id='$campus_id' ") or die(mysqli_error($conn_1));
      $sql_site = mysqli_fetch_assoc($allocate);
      $Site_title = $sql_site['title'];

      //get his category title
      $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_blocks WHERE id='$block_id' ") or die(mysqli_error($conn_1));
      $sql_category = mysqli_fetch_assoc($allocate);
      $Block_title = $sql_category['title'];

      if ($type_id==1) {
        $programme = "PRENCE";
        //get students details
        $allocate = mysqli_query($conn_1, "SELECT  CONCAT_WS('',first_name,middle_name,other_names) AS name, number, gender FROM students_prence WHERE id='$student_id' ") or die(mysqli_error($conn_1));
        $sql = mysqli_fetch_assoc($allocate);
        $name = $sql['name'];
        $number = $sql['number'];
        $gender = $sql['gender'];
      }else if ($type_id==2) {
        $programme = "NCE";
        //get students details
        $allocate = mysqli_query($conn_1, "SELECT  CONCAT_WS('',first_name,middle_name,other_names) AS name, number, gender FROM students_nce WHERE id='$student_id' ") or die(mysqli_error($conn_1));
        $sql = mysqli_fetch_assoc($allocate);
        $name = $sql['name'];
        $number = $sql['number'];
        $gender = $sql['gender'];
      }else if ($type_id==3) {
        $programme = "DUAP(ABU)";
        //get students details
        $allocate = mysqli_query($conn_2, "SELECT  CONCAT_WS('',first_name,middle_name,other_names) AS name, number, gender FROM students_nce WHERE id='$student_id' ") or die(mysqli_error($conn_1));
        $sql = mysqli_fetch_assoc($allocate);
        $name = $sql['name'];
        $number = $sql['number'];
        $gender = $sql['gender'];
      }else if ($type_id==4) {
        $programme = "DUAP(UDUS)";
        //get students details
        $allocate = mysqli_query($conn_2, "SELECT  CONCAT_WS('',first_name,middle_name,other_names) AS name, number, gender FROM students_udus WHERE id='$student_id' ") or die(mysqli_error($conn_1));
        $sql = mysqli_fetch_assoc($allocate);
        $name = $sql['name'];
        $number = $sql['number'];
        $gender = $sql['gender'];
      }else if ($type_id==5) {
        $programme = "SUMMER";
        //get students details
        $allocate = mysqli_query($conn_1, "SELECT  CONCAT_WS('',first_name,middle_name,other_names) AS name, number, gender FROM summer_students WHERE id='$student_id' ") or die(mysqli_error($conn_1));
        $sql = mysqli_fetch_assoc($allocate);
        $name = $sql['name'];
        $number = $sql['number'];
        $gender = $sql['gender'];
      }else if ($type_id==6) {
        $programme = "LVSP";
        //get students details
        $allocate = mysqli_query($conn_2, "SELECT  CONCAT_WS('',first_name,middle_name,other_names) AS name, number, gender FROM students_lvsp WHERE id='$student_id' ") or die(mysqli_error($conn_1));
        $sql = mysqli_fetch_assoc($allocate);
        $name = $sql['name'];
        $number = $sql['number'];
        $gender = $sql['gender'];
      }


	//shorten gender
	//$gender = shorten_gender($gender);

	$line_data = array(strtoupper($name),strtoupper($number),$gender, $programme,strtoupper($Site_title),strtoupper($Block_title),strtoupper($Room_title)); //get_student_matric_no(
		//die($output);
	fputcsv($output, $line_data);

}






exit();









?>
