<?php


ob_start();

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


require_once 'includes/functions_.php';

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

    $pid = $_GET['pid'];

    $ses = $_GET['ses'];


    $semester = $_GET['sem'];
  }
  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





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







$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.'  <b>['.$course_title.' -'.$course_code.'] </b></h5>';
 $output_  .= '<h5  style="color:red; font-weight:normal;"  > '.strtoupper($semester_title).', '.$session_title.' SESSION </h5>';



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



$output = '';

$q = "SELECT s.id, CONCAT_WS(' ', s.first_name, s.other_names) as name,s.gender, s.number, s.level, s.validity_status,
            s.department_id, s.programme_id, s.school_id,
            sc.id as sc_id, sc.student_id, sc.course_id,sc.programme_id, sc.ca_score, sc.exam_score, (sc.ca_score + sc.exam_score) as total_score,  sc.grade, sc.add_drop_status


             FROM 	summer_students s, summer_students_courses_nce sc
             WHERE s.validity_status = '0'

             AND s.id = sc.student_id
             AND sc.add_drop_status != '2'
             AND md5(sc.course_id) = '$course_id'
             AND sc.programme_id = '$pid'
             AND sc.session_id = '$ses'


             ORDER BY s.number ASC";

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


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



    $output  .= '<table cellspacing="1"   cellpadding="2" border="0.5"  style="border:#000 solid 1px; font-size:11px;">';
    $output .=  '<thead>';

    $output .=  '

     <tr> <th width="5%">S/NO </th>
                    <th  width="17%">MATRIC NO. </th>
                    <th width="25%">NAMES</th>
                    <th width="5%">SEX</th>

                    <th width="7%">LEVEL</th>
                    <th width="8%"> C. A (40) </th>
                    <th width="12%"> EXAM (60) </th>
                    <th width="12%"> TOTAL (100)  </th>
                     <th width="9%">  GRADES </th>

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




    $i = 1;



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



        $number = $row['number'];
        $name = strtoupper($row['name']);
        $gender = shorten_gender($row['gender']);
        $level  = $row['level'];
        $ca_score = $row['ca_score'];
        $exam_score = $row['exam_score'];
        $total_score = $row['total_score'];
        $grade = get_student_grade_by_score($total_score);

        $sn = $i++; //




      $output .="" ;
      $output .= '<tr>
        <td width="5%">'.$sn.'</td>
        <td width="17%">'.$number.'</td>
        <td width="25%">'.$name.'</td>
        <td width="5%">'.$gender.'</td>
        <td width="7%">'.$level.'</td>
        <td width="8%">'.$ca_score.'</td>
        <td width="12%">'.$exam_score.'</td>
        <td width="12%">'.$total_score.'</td>
        <td width="9%">'.$grade.'</td>
       </tr >';

    }
  }
  else
  {//show the msg

      $output = 'No record found in the system.';


  }



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


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

<br/>
<br/>

$output

<!-- <h6 style="border-bottom:solid thin #000; margin-bottom:3px;">BASIC INFORMATION</h6> -->

 <table cellspacing="1"  border="0" cellpadding="3" style="font-size:8px;">


        <tr>
        <td width="35%">  </td>
        <td width="30%"></td>
        <td width="35%"> </td>


        </tr>


        <tr>
        <td width="35%"> <b> $staff_name </b></td>
        <td width="30%"></td>
        <td width="35%"> </td>


        </tr>

        <tr>
        <td width="35%"> <b>___________________________________________________</b> </td>
        <td width="30%"></td>
        <td width="35%"> <b>____________________________________________________</b></td>


        </tr>


         <tr>


       <td width="35%" align="center"> <b>LECTURER'S NAME, SIGNATURE & DATE. :</b> </td>
        <td width="30%"> </td>
          <td width="35%" 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, '');

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




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


?>
