<?php


ob_start();
set_time_limit(0);ini_set("memory_limit", "256M");


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.'A2';


  $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_prence($pid, $level, $ses, $semester);

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

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


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

  $vol_with = get_count_vol_with_students_per_programme_prence($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');

//query to count how many headings we are expecting
$total_courses_per_programme = get_total_courses_per_semester_prence($pid,$semester,$level);

//get the headers in spilited format
$qh = "SELECT GROUP_CONCAT(pc.course_id)  AS coded

      FROM `programmes_courses_nce` pc , courses c

      WHERE pc.level = '$level' AND pc.programme_id = '$pid'
      AND c.id = pc.course_id AND c.semester = '$semester'";

$rh = mysqli_query(dbc(),$qh);

if(mysqli_num_rows($rh) > 0)
{


  while($rowh = mysqli_fetch_array($rh,MYSQLI_ASSOC))
  {


     //="YES";

       $couse_codes_h = $rowh['coded'];
  }

}



$output = '';

$q = "SELECT  DISTINCT(sr.`number`), sr.`student_id`

    FROM `students_courses_prence` sr

    WHERE

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

    ORDER BY sr.`number` ASC";

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


 //declare course hedares array
 $course_code_array = array();




 //the collected array

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



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


    $output  .= '          <thead>';

//////////////////////////////////////////////////////////////
////////////////////// first header

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


    // $output  .= '              <td width="25%" colspan="3"></td>';


    // $output  .= '              <td width="3%" align="center" > PREV CGPA  </td>';



    // $output .=  '              <td width="45%" align="center" >   </td>';



    // $output  .= '              <td width="3%" align="center" > TCUR</td>';
    // $output  .= '              <td width="24%" align="center"  colspan ="4">REMARK</td>';
    // $output  .= '            </tr>';

///////////////////////////////////////////////////////////
/////////////////////// second header


    $output  .= '            <tr>';


    $output  .= '              <td width="25%" colspan="3"></td>';


    $output  .= '              <td width="3%" align="center" > </td>';

    //insert the course headers
    //
    //
    ////explode and pass for print

    for ($i = 0; $i < count($couse_codes_h);$i++)
    {
       $course_codeh = explode(',', $couse_codes_h);


    }

    //pass array for output
    foreach ($course_codeh as $item)
    {
      $output .=  '<td  width="3%" align="center" > <span style="transform: rotate(90deg);
transform-origin: left top 0;
float: left;">'.get_course_code($item).'</span>  </td>';
    }


    $output  .= '              <td width="3%" align="center" > </td>';
    $output  .= '              <td width="24%" align="center"  colspan ="4"></td>';
    $output  .= '            </tr>';




//////////////////////////////////////////
///
////////////////////// third header
    $output  .= '            <tr>';


    $output  .= '              <td width="25%" colspan="3"></td>';


    $output  .= '              <td width="3%" align="center" >PREV</td>';

    //insert the course headers
    //
    //
    ////explode and pass for print



    //pass array for output
    foreach ($course_codeh as $item)
    {
      $output .=  '<td  width="3%" align="center" > <span>'.get_course_unit($item).'</span>  </td>';
    }


    $output  .= '              <td width="3%" align="center" > </td>';
    $output  .= '              <td width="24%" align="center"  colspan ="4"></td>';
    $output  .= '            </tr>';



//////////////////////////////////////////
///
////////////////////// fourth header
    $output  .= '            <tr>';


     $output  .= '            <th width="3%" align="center" >S/NO.</th>';
     $output  .= '            <th width="9%" align="center" >MATRIC NO</th>';
     $output  .= '            <th width="13%" align="center" >NAMES</th>';


    $output  .= '             <th width="3%" align="center" > CGPA  </th>';

    //insert the course headers
    //
    //
    ////explode and pass for print


    //pass array for output
    foreach ($course_codeh as $item)
    {
      $output .=  '<th  width="3%" align="center" > <span>'.get_course_type_string(get_course_type($item)).'</span>  </th>';
    }


    $output  .= '              <th width="3%" align="center" > TCUR</th>';
    $output  .= '              <th width="24%" align="center"  colspan ="4">REMARK</th>';
    $output  .= '            </tr>';

    $output  .= '          </thead>';

    $output  .= '          <tbody>';





    $i = 1;



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

     $sn = $i++; //

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

        $output .= '<td width="3%" align="center" >'.$sn.'</td>';
        $output .= '<td   width="9%" align="center" style="font-size:8px;">'.$row['number'].'</td>';
        $output .= '<td width="13%" align="left"   style="font-size:10px;">  '.ucfirst(get_student_fullname_prence($row['student_id'])).'</td>';

        $output .= '<td width="3%" align="center" >'.get_cum_prev_cgpa_students_per_semester_prence($row['student_id'],$pid, $level, $ses, $semester).'</td>';




          //insert the course headers

        //pass array for output
        foreach ($course_codeh as $item)
        {
          $output .=  '<td  width="3%" align="center" > '.get_total_score_per_student_per_course_prence($row['student_id'],$pid, $level, $ses, $semester,$item).' </td>'; //tem is the course id '.get_total_score_per_student_per_course_nce($row['student_id'],$pid, $level, $ses, $semester,$item).'
        }


        $output .= '<td width="3%" align="center" >'.get_tcur_students_per_semester_prence($row['student_id'],$pid, $level, $ses, $semester).'</td>';
        $output .= '<td width="24%" align="left" >'.get_remark2_students_per_semester_prence($row['student_id'],$pid, $level, $ses, $semester).' '.get_remark1_students_per_semester_nce($row['student_id'],$pid, $level, $ses, $semester).'</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 = $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




 <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">  </td>
       <td width="12.5%"> </td>
        <td width="25%" align="center"> <b>HOD'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');


?>
