<?php

include '../configuration.php';


include '../functions_.php';

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

		$pid = $_GET['pid']; //programme id
		$ses = $_GET['ses']; //session id passed
		$sem = $_GET['sem']; //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);



$programme_code = get_programme_code($pid);


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






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('number', 'name','gender', 'ca_score', 'exam_score'));


$rows = mysqli_query(dbc(),"SELECT   `student_id`,`ca_score`, `exam_score` ,`number`
					FROM `students_courses_prence`
					WHERE session_id = '$ses' AND semester = '$sem' AND programme_id = '$pid' AND md5(course_id) = '$course_id'
					ORDER BY `number`");
//$rows = mysqli_query(dbc(),"SELECT s.id, CONCAT_WS(' ', s.first_name, s.other_names) as name, s.number, s.level  FROM students_nce s WHERE s.validity_status = '0'");

//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['student_id']));

	$gender = get_student_gender($row['student_id']);


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

	$line_data = array($row['number'],strtoupper($name),$gender,$row['ca_score'],$row['exam_score']); //get_student_matric_no(

	fputcsv($output, $line_data);

}






exit();









?>
