<?php

include_once '../configuration.php';


include_once '../functions_.php';

include_once '../students_functions.php';


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

		//$_GET['cid'],

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


//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_title = get_programme_title($pid);

 $programme_code = get_programme_code($pid);

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




 //split the departments
 //work on the short code for different core courses
	 $debt_comb_array =array();

	 //get department comb of programme combination
	 $debt_comb = get_department_comb_using_programme_id($pid);

		 //get headings for the core departments


		 //
		 if(strpos($debt_comb, ',') !== false )
		 {

			 for ($i = 0; $i < count($debt_comb);$i++)
			 {
					$debt_comb_array = explode(',', $debt_comb);


			 }

				$first_debt = $debt_comb_array[0];

				$second_debt = $debt_comb_array[1];


			 $first_debt = get_department_code($first_debt);

			 $second_debt = get_department_code($second_debt);

		 }
		 else
		 {

			 //get department id using programme id
			$first_debt =  get_department_using_programme_id($pid);

			$first_debt = get_department_code($first_debt);
			$second_debt = '';



			 //get short code for department

		 }





//get TP course ID
 $tp_course_id = get_tp_course_id();



 $output_ = '';
 $output = '';

  $output1_ .= 'NIGER STATE COLLEGE OF EDUCATION MINNA';
  $output2_ .= 'NIGERIA CERTIFICATE IN EDUCATION';
  $output3_ .= ''.$school_title.'';
  $output4_  .= ''.$programme_title.'';
  $output5_  .= ' '.strtoupper($semester_title).', '.$session_title.' SESSION';
  $output6_  .= 'GRADUATION SUMMARY LIST - LEVEL '.$level.'';
	$output7_  .= '';





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

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


fputcsv($output, array($output1_));
fputcsv($output, array($output2_));
fputcsv($output, array($output3_));
fputcsv($output, array($output4_));
fputcsv($output, array($output5_));
fputcsv($output, array($output6_));
fputcsv($output, array($output7_));




// output the column headings
fputcsv($output, array('S/NO', 'MATRIC NO.','NAMES', 'GENDER', 'TP','EDU','GSE',$first_debt,$second_debt,'STATUS'));


$rows = mysqli_query(dbc(),"SELECT sr.`id`, sr.`student_id`, sr.`programme_id`, sr.`session_id`, sr.`semester`, sr.`level`,
       sr.`result_date_added`, sr.`status`, sr.`date_trashed`, sr.`date_added`, sr.`date_modified`,
      sr.`added_by`, sr.`modified_by`, sr.`edu_ptcur`, sr.`edu_tcur`, sr.`edu_ptcue`, sr.`edu_tcue`, sr.`edu_ptgp`,
      sr.`edu_tgp`, sr.`edu_ptgpe`, sr.`edu_tgpe`, sr.`edu_pcgpa`, sr.`edu_cgpa`, sr.`gst_ptcur`, sr.`gst_tcur`,
      sr.`gst_ptcue`, sr.`gst_tcue`, sr.`gst_ptgp`, sr.`gst_tgp`, sr.`gst_ptgpe`, sr.`gst_tgpe`, sr.`gst_pcgpa`,
      sr.`gst_cgpa`, sr.`core1_ptcur`, sr.`core1_tcur`,sr.`core1_ptcue`, sr.`core1_tcue`, sr.`core1_ptgp`, sr.`core1_tgp`,
      sr.`core1_ptgpe`, sr.`core1_tgpe`, sr.`core1_pcgpa`, sr.`core1_cgpa`, sr.`core2_ptcur`, sr.`core2_tcur`,
      sr.`core2_ptcue`, sr.`core2_tcue`, sr.`core2_ptgp`, sr.`core2_tgp`, sr.`core2_ptgpe`, sr.`core2_tgpe`,
      sr.`core2_pcgpa`, sr.`core2_cgpa`, sr.`cum_ptcur`, sr.`cum_tcur`, sr.`cum_ptcue`, sr.`cum_tcue`, sr.`cum_ptgpe`,
      sr.`cum_tgpe`, sr.`cum_pcgpa`, sr.`cum_cgpa`,sr.`remark2`, s.`id`, s.`number`, s.`first_name`,s.`other_names`,s.`gender`



    FROM `students_results_nce` sr, `students_nce` s


    WHERE sr.`student_id` = s.`id` AND


  sr.`session_id` = $ses AND sr.`semester` = $sem AND sr.`programme_id` = $pid AND sr.`level` = '$level'


 AND

 (sr.`remark2` LIKE '%LOWER PASS%'
 OR sr.`remark2` LIKE '%PASS%'
 OR sr.`remark2` LIKE '%MERIT%'
 OR sr.`remark2` LIKE '%CREDIT%'
 OR sr.`remark2` LIKE '%DISTINCTION%')

 ORDER BY s.`number` ASC");
//$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`


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

	//fputcsv($output, $row);
	 $sn = $i++; //serial no
	//
	 $matric_number = $row['number'];
	 $names = ucfirst($row['first_name'].' '.$row['middle_name'].' '.$row['other_names']);
	 $gender = $row['gender'];

	 $tp_score = get_student_tp_score($row['student_id'], $tp_course_id, $ses);

	 if($tp_score == '')
	 {
		 $tp = 'F';


	 }
	 else
	 {
		 $tp =  ' TP ('.get_student_grade_by_score($tp_score).')';

	 }
	//
	//
	//
	 //collect the the cgpas and pass
	 $edu_cgpa = get_grade_by_various_subjects($row['edu_cgpa']);
	 $gst_cgpa = get_grade_by_various_subjects($row['gst_cgpa']);
	 $core1_cgpa  = get_grade_by_various_subjects($row['core1_cgpa']);
	 $core2_cgpa  = get_grade_by_various_subjects($row['core2_cgpa']);



	 $edu = 'EDU ('.$edu_cgpa.')';

	 $gse  = 'GSE ('.$gst_cgpa.')';


	 $core1 = $first_debt.'('.$core1_cgpa.')';

	 if($second_debt != '')
	 {


			 $core2 = $second_debt.'('.$core2_cgpa.')';


	}
	else
	{
			$core2 = '';
	}

	 $remark =  $row['remark2'];


	// $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($sn,$matric_number,strtoupper($names),$gender,$tp,); //get_student_matric_no(

   $line_data = array($sn,$matric_number, strtoupper($names),$gender,$tp,$edu, $gse,$core1, $core2, $remark); //get_student_matric_no(

	fputcsv($output, $line_data);

}






exit();









?>
