<?php


ob_start();


require_once 'includes/functions_.php';

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

    // $pid = $_GET['pid'];

    // $ses = $_GET['ses']; 


    // $semester = $_GET['sem'];





   // $course_id = $_REQUEST['cid'];//course id
    $pid = $_REQUEST['pid'];//programme id
    $sid = $_REQUEST['sid'];//school id
    $ses = $_REQUEST['ses'];//session passed
    $semester = $_REQUEST['sem'];//semster passed
    $level = $_REQUEST['lvl'];//semster passed


  }
  else
  {
    header("Location: /autorize_message");

  }




  //get the  course ttitle
  $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
  $semester_title = get_semester_title($semester); 




  //school properties
  $school_id = get_school_id_using_programme_id($pid);
  $school_title = get_school_title($school_id);


  $programme_title = get_programme_title($pid);

  $programme_code = get_programme_code($pid);

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

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


  $course_title = strtoupper(get_course_title_by_md5($course_id));

  $course_code = get_course_code_by_md5($course_id);//get course using md5 


  $user_id = get_user_id();
  $staff_name = get_staff_name($user_id);


  //department
  //count the the aggregates belwo the table
  // $passed = count_passed_students_per_programme_nce($pid, $level, $ses, $semester);

  // $reoffers =  count_reoffers_students_per_programme_nce($pid, $level, $ses, $semester);

  // $probation = count_probation_students_per_programme_nce($pid, $level, $ses, $semester);


  // $acad_with = count_acad_with_students_per_programme_nce($pid, $level, $ses, $semester);

  

  


// Include the main TCPDF library (search for installation path).
require_once('tcpdf/tcpdf_include.php');


// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {

  //Page header
  public function Header() {
    // Logo
    $image_file = K_PATH_IMAGES.'logo.png';
    $this->Image($image_file, 10, 10, 15, '', 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);
    // Set font
    // $this->SetFont('helvetica', 'B', 14);
    // // Title
    // //$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M'); original commented 
    // $this->Cell(0, 15, 'IBBU CONSULT & SERVICES', 0, false, 'C', 0, '', 0, false, 'M', 'M');
  }

  // Page footer
  public function Footer() {
    // Position at 15 mm from bottom
    $this->SetY(-15);
    // Set font
    $this->SetFont('helvetica', 'I', 7);
    // Page number
    $this->Cell(0, 10, "SOURCE: www.coeminna.edu.ng  - ePortal,  Page ".$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
  }
}


// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);


$author ='_ COEMINNA';


// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor($author);
$pdf->SetTitle($title);
$pdf->SetSubject('HOD lecturers print outs');
$pdf->SetKeywords('COEMINNA, PDF');






// set default header data
//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);



// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
  require_once(dirname(__FILE__).'/lang/eng.php');
  $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set font
//$pdf->SetFont('times', 'BI', 12) original;
$pdf->SetFont('times', '', 8);

// add a page
//$pdf->AddPage();


$pdf->AddPage('L', 'A4');


//$pdf->Cell(0, 0, 'A4 LANDSCAPE', 1, 1, 'C');





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

 $output_ .= '<h4 >NIGER STATE COLLEGE OF EDUCATION MINNA</h4>';
 $output_ .= '<h4 >NIGERIA CERTIFICATE IN EDUCATION</h4>';
 $output_ .= '<h5 >'.$school_title.'</h5>';
 $output_  .= '<h5> '.$programme_title.'</h5>'; 
 $output_  .= '<h5  style="color:red; font-weight:normal;"  > '.strtoupper($semester_title).', '.$session_title.' SESSION </h5>'; 
  $output_  .= '<h5>EXAMINATION RESULT CUMMULATIVES - NCE '.$level.' FT </h5>'; 




 //pdf generated 
$pdf_generated = date('d-m-Y / h:i a'); 



$output = '';

$q = "SELECT sr.`id`, sr.`student_id`, sr.`programme_id`, sr.`session_id`, sr.`semester`, sr.`level`, 
      sr.`delete_token`, 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`



    FROM `students_results_nce` sr, `students_nce` s


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


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

    ORDER BY s.`number` ASC";

  $r =mysqli_query(dbc(),$q);

  
  if(mysqli_num_rows($r) > 0)
  {//show table
  
  

    $output  .= '<table border="0.5" width="100%"  cellspacing="1"   style="font-size:9px;">';
        

    $output  .= '          <thead>';
    $output  .= '            <tr>';
                
                    
    $output  .= '              <td colspan="3"></td>';
         
    
    $output  .= '              <td align="center" colspan = "4"> EDUCATION </td>';
    $output  .= '              <td align="center" colspan = "4"> GENERAL STUDIES</td>';
    $output  .= '              <td align="center" colspan = "4">CORE 1</td>';
    $output  .= '              <td align="center" colspan = "4">CORE 2</td>';
    $output  .= '              <td> TP </td>';
    $output  .= '              <td align="center" colspan ="4">CUMMULATIVE</td>';
    $output  .= '              <td>REMARK</td>';
                    
                  
    $output  .= '            </tr>';





    $output  .= '            <tr>';
    $output  .= '              <th >S/NO.</th>';


    $output  .= '              <th >MATRIC NO</th>';
    $output  .= '              <th >NAMES</th>';
                  
    $output  .= '              <th>TCUR</th>';
    $output  .= '              <th>TCUE</th>';
    $output  .= '              <th>TGP</th>';
    $output  .= '              <th>CGPA</th>';
                
    $output  .= '              <th>TCUR</th>';
    $output  .= '              <th>TCUE</th>';
    $output  .= '              <th>TGP</th>';
    $output  .= '              <th>CGPA</th>';

    $output  .= '              <th>TCUR</th>';
    $output  .= '              <th>TCUE</th>';
    $output  .= '              <th>TGP</th>';
    $output  .= '              <th>CGPA</th>';

    $output  .= '              <th>TCUR</th>';
    $output  .= '              <th>TCUE</th>';
    $output  .= '              <th>TGP</th>';
    $output  .= '              <th>CGPA</th>';

    $output  .= '              <th> </th>';

    $output  .= '              <th>TCUR</th>';
    $output  .= '              <th>TCUE</th>';
    $output  .= '              <th>TGP</th>';
    $output  .= '              <th>CGPA</th>';

    $output  .= '              <th>REMARK</th>';
    $output  .= '            </tr>';

 
    $output  .= '          </thead>';
              


      
      
        
      
    $output  .= '          <tbody>';
   




    $i = 1;

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

     $sn = $i++; //

      


      $output .= "<tr>";
      $output .= "<td >".$sn."</td>";
      $output .= "<td width='25%' >".$row['number']."</td>";
      $output .= "<td>".strtoupper($row['first_name']." ".$row['other_names'])."</td>";

      $output .= "<td>".$row['edu_tcur']."</td>";
      $output .= "<td>".$row['edu_tcue']."</td>";
      $output .= "<td>".$row['edu_tgpe']."</td>";
      $output .= "<td>".$row['edu_cgpa']."</td>";

      $output .= "<td>".$row['gst_tcur']."</td>";
      $output .= "<td>".$row['gst_tcue']."</td>";
      $output .= "<td>".$row['gst_tgpe']."</td>";
      $output .= "<td>".$row['gst_cgpa']."</td>";


      $output .= "<td>".$row['core1_tcur']."</td>";
      $output .= "<td>".$row['core1_tcue']."</td>";
      $output .= "<td>".$row['core1_tgpe']."</td>";
      $output .= "<td>".$row['core1_cgpa']."</td>";


      $output .= "<td>".$row['core2_tcur']."</td>";
      $output .= "<td>".$row['core2_tcue']."</td>";
      $output .= "<td>".$row['core2_tgpe']."</td>";
      $output .= "<td>".$row['core2_cgpa']."</td>";

      //tp out put
      $output .= "<td></td>";


      $output .= "<td>".$row['cum_tcur']."</td>";
      $output .= "<td>".$row['cum_tcue']."</td>";
      $output .= "<td>".$row['cum_tgpe']."</td>";
      $output .= "<td>".$row['cum_cgpa']."</td>";

      $output .= "<td>".$row['remark2']."</td>";
      
      $output .= "</tr>";

    }
  }
  else
  {//show the msg 
  
      $output = 'No record found in the system.';
    
    
  }
  
  
  
  $output .=  '</tbody>';
  $output .=  '</table>';



$undergrade1 = 'GRADING KEYS: TCUR: TOTAL CREDIT UNIT REGISTER; TCUE: TOTAL CREDIT UNIT EARNED; 
              TGP: TOTAL GRADE POINTS; TP: TEACHING PRACTICE; E-P: ELECTIVE PASSED; CGPA: CUMMULATIVE GRADE POINTS AVERAGE';

$undergrade2 = "PASSED = $passed RE-OFFERS = $reoffers PROB = $probation ACAD WITH = 0 VOL WITH = 0  EXPELLED = 0 
                SUSPENDED = 0 DEFERRED = 0 WITHDRAWN = 0"; 

// set some text to print
$txt = <<<EOD
<div align="center"> $output_ </div> 

<br/>
<br/>
$output

<div align="center"> $undergrade1 <br/>  $undergrade2 </div> 


<!-- <h6 style="border-bottom:solid thin #000; margin-bottom:3px;">BASIC INFORMATION</h6> -->
 
 <table cellspacing="1"  border="0" cellpadding="3" style="font-size:12px;">
       
 
        <tr>
        <td width="25%">  </td>
        <td width="12.5%"></td>
        <td width="25%"> </td>
        <td width="12.5%"></td>
        <td width="25%"> </td>
        
        
        </tr> 


        <tr>
         <td width="25%">  </td>
        <td width="12.5%"></td>
        <td width="25%"> </td>
        <td width="12.5%"></td>
        <td width="25%"> </td>
        
        
        </tr> 

        <tr>
        <td width="25%"> <b>________________________________________</b> </td>
        <td width="12.5%"></td>
        <td width="25%"> <b>________________________________________</b></td>
        
        <td width="12.5%"></td>
        <td width="25%"> <b>________________________________________</b></td>
        
        </tr> 


         <tr>
      

       <td width="25%" align="center"> <b>EXAM-OFFICER'S NAME, SIGNATURE & DATE</b> </td>
       <td width="12.5%"> </td>
       <td width="25%" align="center"> <b>HOD'S NAME , SIGNATURE & DATE</b> </td>
       <td width="12.5%"> </td>
        <td width="25%" align="center"> <b>DEAN OF SCHOOL'S NAME , SIGNATURE & DATE</b> </td>
        
        
        </tr> 





      
        
      
       
        
       
    
 </table>


 Date printed : $pdf_generated
 

EOD;

// print a block of text using Write()
//$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0);

//$pdf->writeHTML($txt, true, false, false, false, '');

// ---------------------------------------------------------




$pdf->writeHTML($txt, true, false, false, false, '');

// ---------------------------------------------------------


//ob_end_flush();

//Close and output PDF document
//$pdf->Output('example_003.pdf', 'I');
$pdf->Output($title.'.pdf', 'I');

          
?>