<?php


ob_start();
//set_time_limit(0);ini_set("memory_limit", "500M");


require_once 'includes/functions_.php';

require_once 'includes/students_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 = get_count_passed_students_per_programme_nce($pid, $level, $ses, $semester);

  $reoffers =  get_count_reoffers_students_per_programme_nce($pid, $level, $ses, $semester);

  $probation = get_count_probation_students_per_programme_nce($pid, $level, $ses, $semester);


  $acad_with = get_count_acad_with_students_per_programme_nce($pid, $level, $ses, $semester);

  $vol_with = get_count_vol_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('P', 'A4');


//$pdf->Cell(0, 0, 'A4 LANDSCAPE', 1, 1, 'C');
//
//
//
  //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 = '';

 $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> GRADUATION SUMMARY LIST '.$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.`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` = $semester 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"; // LIMIT 301, 650
    //sr.`session_id` = '$ses' AND sr.`semester` = $semester AND sr.`programme_id` = $pid AND sr.`level` = '$level'
    //here I i had to switch to s.level becaus eit was prinitng all level result,on the 8th of August , 2019
    //finally i had to equate level equals level before it worked


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


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



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


    $output  .= '          <thead>';
    // $output  .= '            <tr>';


    $output  .= '            <tr>';
    $output  .= '              <th width="5%" align="center" >S/NO.</th>';


    $output  .= '              <th width="15%" align="center" >MATRIC NO</th>';
    $output  .= '              <th width="25%" align="center" >NAMES</th>';

    $output  .= '              <th width="7%" align="center" >GENDER</th>';


    $output  .= '              <th width="35%" align="center" colspan = "5"> REMARK</th>';
    $output  .= '              <th width="13%" align="center">STATUS</th>';
    $output  .= '            </tr>';


    // $output  .= '              <td width="25%"></td>';
    //
    // $output  .= '              <td width="2%" align="center" > TP </td>';
    // $output  .= '              <td width="14%" align="center" colspan = "4"> EDU </td>';
    // $output  .= '              <td width="14%" align="center" colspan = "4"> GSE</td>';
    // $output  .= '              <td width="12%" align="center" colspan = "4">'.$first_debt.'</td>';
    // $output  .= '              <td width="12%" align="center" colspan = "4">'.$second_debt.'</td>';
    // $output  .= '              <td width="13%" align="center" colspan ="4">STATUS</td>';
    // $output  .= '              <td width="8%" align="center" ></td>';
    //
    //
    // $output  .= '            </tr>';


    $output  .= '          </thead>';







    $output  .= '          <tbody>';





    $i = 1;



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

     $sn = $i++; //




      $output .= '<tr>';
      $output .= '<td width="5%" align="center" >'.$sn.'</td>';
      $output .= '<td   width="15%" align="left">'.$row['number'].'</td>';
      $output .= '<td width="25%" align="left">  '.ucfirst($row['first_name'].' '.$row['middle_name'].' '.$row['other_names']).'</td>';
      $output .= '<td width="7%" >'.$row['gender'].'</td>';


      //tp out put


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

      if($tp_score == '')
      {
        $output .= '<td width="7%" align="center">F </td>';


      }
      else
      {
        $output .= '<td width="7%" align="center"> TP ('.get_student_grade_by_score($tp_score).') </td>';

      }


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



      $output .= '<td width="7%" align="center" > EDU ('.$edu_cgpa.') </td>';

      $output .= '<td width="7%" align="center" > GSE ('.$gst_cgpa.') </td>';


      $output .= '<td width="7%" align="center" > '.$first_debt.'('.$core1_cgpa.') </td>';

      if($second_debt != '')
      {


          $output .= '<td width="7%" align="center" >'.$second_debt.'('.$core2_cgpa.') </td>';


     }
     else
     {
          $output .= '<td width="7%" align="center" > </td>';
     }

      $output .= '<td width="13%" align="left" > <b>'.$row['remark2'].'</b></td>';

      $output .= '</tr>';

    }
  }
  else
  {//show the msg

      $output = 'No record found in the system.<br/>';


  }



  $output .=  '</tbody>';
  $output .=  '</table>';



$undergrade1 = 'REMARK: [4.50-5.00:A] [3.50-4.49:B] [2.40-3.49:C] [1.50-2.39:D] [1.00-1.49:E] [0.00-0.99:F]';

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

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

<br/>
<br/>
$output
<br/>
<br/>
<div align="center" style="font-size:9px;" > $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:9px;">


        <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');


?>
