<?php

include '../configuration.php';


include '../functions.php';

	//collect the course id
	if(isset($_GET['did'],$_GET['ses'],$_GET['sem']))
	{
		$course_id = $_GET['cid'];

		$did = $_GET['did']; //programme id
		$ses = $_GET['ses']; //session id passed
		$sem = $_GET['sem']; //semester id passed
		$level = $_GET['level']; //semester id passed
	}


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



$semester = get_current_semester(1); //deprecated since I am using semester passed from url


//get the  course ttitle
$code =  get_student_course_code($course_id);
$code = str_replace(' ','-',$code);


 $department_title = get_department_title($did);

 $department_code = get_department_code($did);

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






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

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

// output the column headings
fputcsv($output, array('USERNAME', 'FIRSTNAME', 'LASTNAME','PASSWORD','ROLE','COURES1','COURSE2','COURSE3','COURSE4','COURSE5','COURSE7','COURSE8','COURSE9','COURSE10','COURSE11','COURSE12','COURSE13','COURSE14','COURSE15'));


// $rows = mysql_query("SELECT   `student_id`, `total_score` ,`number`
// 					FROM `students_courses`
// 					WHERE session_id = '$ses' AND semester = '$sem'  AND md5(course_id) = '$course_id'
// 					ORDER BY `number`");


$rows = mysqli_query($dbc, "SELECT s.id AS sid, s.number, s.first_name, s.other_names

	FROM students s WHERE  s.level = '$level' AND s.department_id = '$did'
	ORDER BY s.`number`");

//SELECT      `ca_score`, `exam_score`   FROM `students_courses_nce`

// loop over the rows, outputting them
while ($row = mysqli_fetch_array($rows, MYSQLI_ASSOC))
{

	//fputcsv($output, $row);


	$name = str_replace(',',' ',get_student_fullname($row['sid']));

	//$gender = get_student_gender($row['student_id']);
	//
//	$department_title =  get_department_title($row['department_id']);
	//$programme_title =  get_actual_programme_title($row['actual_programme_id']);
	$registered_courses = get_student_registered_courses_jlearn($row['sid'], $level, $ses, $sem);
	$role = 'student';
	$password =md5(0000);
	$username= strtolower($row['number']);


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

	$line_data = array($username,strtolower($row['first_name']),strtolower($row['other_names']),$password, $role,$registered_courses); //get_student_matric_no(

	fputcsv($output, $line_data);

}






exit();









?>
