<?php
require_once("includes/initialize.php");
global $database;
date_default_timezone_set('Africa/Lagos');

$id = $session->user_id;
$user_data = Userdata::find_by_id($session->user_id);
$biodata = Biodata::find_by_id($user_data->formno, $user_data->email);
$session_class = SESSIONCLASS::session_authenticate();
$user_data = Userdata::find_by_id($session->user_id);




$year=$_SESSION['year'];
$id = $session->user_id;
$user_data = Userdata::find_by_id($session->user_id);
 $array_semester=array();



//$session_first = SESSIONCLASS::session_authenticate1();

$user_data = User::find_by_id($session->user_id);


//echo "Student Process ID: ".$user_data->std_process_id;
 if (substr($user_data->formno,2,2)>=20){
      
       Switch(substr($user_data->formno,2,2))
      {
          case '20':$table='form_data';
          break;
          
          case '21': $table='form_data21';
          break;
          
          case '22': $table='form_data22';
      }
      
       $biodata = Biodata2::find_by_id($table,$user_data->formno, $user_data->email);
       
      
        $new_dp = DEPARTMENTCLASS::new_admin_dept($biodata->formno);
        
          if (!isset($new_dp->department)){
           $biodata->courses ='Nil';   
          }else{
        $biodata->courses = $new_dp->department;
          }
     $level = $biodata->level;
       
       
      $department =$new_dp->department;
  
    if ($department=='Microbiology (Option)'){
        $department='(Microbiology (Option)';
    }
        // $pixf = substr($biodata->passport, 10);
       $dp = DEPARTMENTCLASS::department_authenticate($biodata->department);
       $pix = "http://admission.oscotechesaoke.edu.ng/passport22/".trim($biodata->passport);
    
     
    
       
  }else{
     
     $biodata = Biodata::find_by_id($user_data->formno, $user_data->email); 


    $dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);
    $display_course = COURSECLASS::fetch_all_course($biodata->courses, $biodata->programme, $session_class->semester);
    $alldp = DEPARTMENTCLASS::find_all($dp->faculty);
    $pix = "../Oscotech-Wallet/passport/".$pixf;
    $level = $biodata->programme;
    $department = $biodata->courses;
    $formno = $biodata->formno;
}





if (substr($user_data->formno,2,2)>=20){
      
        Switch(substr($user_data->formno,2,2))
      {
          case '20':$table='form_data';
          break;
          
          case '21': $table='form_data21';
          break;
          
          case '22': $table='form_data22';
      }
      
       $biodata = Biodata2::find_by_id($table,$user_data->formno, $user_data->email);
       
      
       $new_dp = DEPARTMENTCLASS::new_admin_dept($biodata->formno);
        
          if (!isset($new_dp->department)){
           $biodata->courses ='Nil';   
          }else{
        $biodata->courses = $new_dp->department;
        
        if ( $biodata->courses=='Microbiology (Option)'){
         $biodata->courses='(Microbiology (Option)';
    }
          }
     $level = $biodata->level;
       
       
      $department =$new_dp->department;
      
      if ( $department=='Microbiology (Option)'){
         $department='(Microbiology (Option)';
    }
  
        // $pixf = substr($biodata->passport, 10);
       $dp = DEPARTMENTCLASS::department_authenticate($biodata->department);
       $pix = "http://admission.oscotechesaoke.edu.ng/passport22/".trim($biodata->passport);
    
     
    
     
    
       
  }else{
     
     $biodata = Biodata::find_by_id($user_data->formno, $user_data->email);
     if (substr($user_data->formno,2,2)=='19'){

 $new_dp = DEPARTMENTCLASS::new_admin_dept($user_data->formno);
   $biodata->courses = $new_dp->department;
  $biodata->programme = $biodata->level; 
  
   $dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);
    $pixf = substr($biodata->pix, 11);
   $pix = "../application_account/passport/".$pixf;
   
 }else {


//   $biodata = Biodata::find_by_id($user_data->formno, $user_data->email);
  
$dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);

//print_r($session_class);

//$display_course = COURSECLASS::fetch_all_course($biodata->courses, $biodata->programme, $session_class->semester);
$alldp = DEPARTMENTCLASS::find_all($dp->faculty);
 $pixf = substr($biodata->pix, 11);
$pix = "../Oscotech-Wallet/passport/".$pixf;


//print_r($display_course);

}

    $level = $biodata->programme;
    $department = $biodata->courses;
    
    
    if ( $department=='Microbiology (Option)'){
         $department='(Microbiology (Option)';
    }
   
}












//echo "<br>: ". $user_data->std_process_id.' '.$level;
 $s= find_semester($user_data->std_process_id);
      $clevel= find_report($user_data->std_process_id,$level);
      
    //   echo "<br>Clevel: ".$clevel;
    //   echo "<br>Sem: ".$s;

if (($clevel=='ND I') OR ($clevel=='HND I')){
    
    $prs = '20'.substr($user_data->formno,2,2);
     $se = $prs.'/'.($prs+1);
     
     
     
}else if (($clevel=='ND II') OR ($clevel=='HND II')){
     $prs = '20'.(substr($user_data->formno,2,2)+1);
     $se = $prs.'/'.($prs+1);
}

//echo "Session: ".$se.'   '.$s;

//$status = SESSIONCLASS::session_authenticate($se, $s);
 //$ses_sem_id = $status->ses_sem_id;

$year = $_SESSION['year'];
 //echo "Year: ".$year;
 //echo "Level: ".$clevel;
// echo "Session: ".$se;
 $array_semester = SESSIONCLASSNEW::fetch_semester_id($year,$clevel);
  // echo  "<br>GKIIIF: ". $matricno = $user_data->formno;
//print_r($array_semester);
 

foreach ($array_semester as $sem){
    
    
    if ($sem->sem_id==1){
        $sem1=$sem->sem_id;
        $semester1='First';
    }else if ($sem->sem_id==2){
        $sem2=$sem->sem_id;
        $semester2='Second';
    }else if ($sem->sem_id==3){
        $sem1=$sem->sem_id;
        $semester1='First';
    }else if ($sem->sem_id==4){
        $sem2=$sem->sem_id;
        $semester2='Semester';
    }
    
}





        if ((($semester1=='First') OR ($ssemester2=='Second')) AND (($clevel=='ND I') OR ($clevel=='HND I'))){
        
       $matricno = $user_data->formno;
        
    }else if ((($clevel=='ND II') OR ($clevel=='HND II')) && ($user_data->matricno!=0)){
       // echo '<br> Matric Set: '.$user_data->matricno;
        $matricno=$user_data->matricno;
    }






//echo "<br>Matricno: ".$matricno;

//echo "<br>Sem Id: ".$ses_sem_id;

//echo "Semester: ".$s;

// if ($level == 'ND I' OR $level == 'HND I')
// { $level = $level."I"; }else { $level = $level." "."I"; }

// $current_session = $session_class->session;
// $start_session = $user_data->start_session;



    if ((($clevel=='ND I') OR ($clevel=='HND I')))
    {
       $msg='FORM NO';
       
    }else {
        $msg = 'MATRIC NO:';
    }
    //   if ((($clevel=='ND I') OR ($clevel=='HND I')) AND ($s=='First') AND ($status->status == 'active') AND ($user_data->std_process_id==4))
    //   {
    //     $act_level = $user_data->std_process_id + 1;
    //     $update_process = Userdata::update($id, $act_level);
    //   }else if ((($clevel=='ND I') OR ($clevel=='HND I')) AND ($s=='Second') AND ($status->status == 'active') AND ($user_data->std_process_id==5))
    //   {
    //       $act_level = $user_data->std_process_id + 1;
    //     $update_process = Userdata::update($id, $act_level);
    
    
    // }else if ((($clevel=='ND II') OR ($clevel=='HND II')) AND ($s=='First') AND ($status->status == 'active') AND ($user_data->std_process_id==7))
    //   {
    //       $act_level = $user_data->std_process_id + 1;
    //     $update_process = Userdata::update($id, $act_level);
       
    // }else if ((($clevel=='ND II') OR ($clevel=='HND II')) AND ($s=='Second') AND ($status->status == 'active') AND ($user_data->std_process_id==8))
    //   {
    //       $act_level = $user_data->std_process_id + 1;
    //     $update_process = Userdata::update($id, $act_level);
    //   }
    


 

// echo $matricno;
// echo $clevel;
// echo $se;
 
$surname = $biodata->surname;
$othernames = $biodata->othernames;

$output = '';


class coursePDF extends FPDF
{
    public $matricno;
    public $department;
    public $semester1;
    public $semester2;
    public $sem1;
    public $sem2;
    public $se;
    public $clevel;
    public $ses_sem_id;
    
    function Head($department)
    {
// 		$this->department = trim(strtoupper($department));
        $this->Image('images/icon.png', 10, 15);
        $this->SetFont('Arial', 'B', 14);
        $this->Cell(189, 5, 'OSUN STATE COLLEGE OF TECHNOLOGY, ESA-OKE.', 0, 0, 'C');
        $this->Ln();
        $this->SetFont('Arial', 'B', 12);
        $this->Cell(189, 8, 'DEPARTMENT OF ' . trim(strtoupper($department)), 0, 0, 'C');
        // $this->Cell(189,8, $department,0,0,'L');
        $this->Ln();
        $this->SetFont('Times', '', 12);
        $this->Cell(189, 5, 'P.M.B 1011, ESA-OKE, OSUN STATE.', 0, 0, 'C');
        $this->Ln(20);
        
       
    }
    
    function Head2($department)
    {
// 		$this->department = trim(strtoupper($department));
        $this->Image('images/icon.png', 10, 15);
        $this->SetFont('Arial', 'B', 14);
        $this->Cell(189, 5, 'OSUN STATE COLLEGE OF TECHNOLOGY, ESA-OKE.', 0, 0, 'C');
        $this->Ln();
        $this->SetFont('Arial', 'B', 12);
        $this->Cell(189, 8, 'DEPARTMENT OF ' . trim(strtoupper($department)), 0, 0, 'C');
        // $this->Cell(189,8, $department,0,0,'L');
        $this->Ln();
        $this->SetFont('Times', '', 12);
        $this->Cell(189, 5, 'P.M.B 1011, ESA-OKE, OSUN STATE.', 0, 0, 'C');
        $this->Ln(20);
        
       
    }

    
    
    function Background()
    {
        $this->Image('images/img2.png', 0, 0, 210, 300);
    }

    function footer()
    {
        $this->SetY(-15);
        $this->SetFont('Times', 'BI', 11);
        $this->SetTextColor(248,0,0);
        $this->Cell(0, 2, 'You are requested to report to your Faculty Officer immediately and complete your registration formalities to obtain', 0, 1, 'L');
        $this->Ln(3);
        
        $this->Cell(0, 2, ' your Certificate of Registration (C.O.R) within two weeks from the date of printing this course form.', 0, 0, 'L');
         $this->Ln();
        $this->SetFont('Arial', '', 8);
        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
    }

    function headerTable($database, $matricno, $semester, $clevel, $sem1, $department)
    {
        // echo "<br>Level: ".$clevel;
        // echo "<br>Sem: ".$semester;
        // echo "<br>Sem Id: ".$sem1;
        // echo "<br>Matricno: ".$matricno;

        if (($clevel == "ND II") &&($semester=='First')) {
            $qry = "SELECT courses,regDate FROM ndii_tbl 
			WHERE matricno ='$matricno' and ses_sem_id = '$sem1'
			";
        } else if (($clevel == "ND I")&&($semester=='First')) {
            $qry = "SELECT courses, regDate FROM ndi_tbl 
			WHERE matricno ='$matricno' and ses_sem_id = '$sem1'
			";
        }else if (($clevel == "PGD I")&&($semester=='First')) {
            $qry = "SELECT courses, regDate FROM pgdi_tbl 
			WHERE matricno ='$matricno' and ses_sem_id = '$sem1'
			";
        } 
        else if (($clevel == "HND I")&&($semester=='First')) {
            $qry = "SELECT courses, regDate FROM hndi_tbl 
			WHERE matricno ='$matricno' and ses_sem_id = '$sem1'
			";
        } else if (($clevel == "HND II")&&($semester=='First')) {
            $qry = "SELECT courses, regDate FROM hndii_tbl 
			WHERE matricno ='$matricno' and ses_sem_id = '$sem1'
			";
        }

        $result = $database->query($qry);
        //$result = mysql_query($qry);
        $count = 0;
        while ($row = mysqli_fetch_array($result)) {
            //while ($row= mysql_fetch_array($result)){
            $code = $row{"courses"};
            $date = $row{"regDate"};
        }
        $RegCourses = explode(',', $code);
        $this->SetFont('Times', 'BU', 14);
        $this->Cell(189, 20, 'COURSE REGISTRATION FORM', 0, 0, 'C');
        $this->Ln(10);
        $this->SetFont('Times', '', 12);
        $this->Cell(189, 10, 'Reg.Date: ' . $date, 0, 1, 'R');
        $this->SetFont('Times', 'B', 12);
        $this->SetFillColor(150, 255, 150);
        $this->Cell(20, 10, 'S/N', 1, 0, 'C', true);
        $this->Cell(30, 10, "Course Codes", 1, 0, "C", true);
        $this->Cell(120, 10, "Course Titles", 1, 0, "C", true);
        $this->Cell(19, 10, "Units", 1, 1, "C", true);
        $total = 0;
        foreach ($RegCourses as $course_code) {

            $query = "SELECT * FROM tbl_courses 
			WHERE course_code = '$course_code' AND department = '$department'
			";
            $sql = $database->query($query);
            //while($rows = mysqli_fetch_array($sql,MYSQLI_ASSOC))
            while ($rows = mysqli_fetch_array($sql)) {
                $count += 1;
                $code = $rows["course_code"];
                $title = strtoupper($rows["course_title"]);
                $unit = $rows["course_unit"];
                $total = $total + $unit;

                $this->SetFont('Times', '', 12);
                $this->Cell(20, 10, $count, 1, 0, 'C');
                $this->Cell(30, 10, $code, 1, 0);
                $this->Cell(120, 10, $title, 1, 0);
                $this->Cell(19, 10, $unit, 1, 1, 'C');

            }
        }
        $this->SetFont('Times', 'B', 14);
        $this->Cell(170, 10, 'Total Units:' . $total, 0, 0, 'R');
        $this->Ln(10);
    }



         function headerTable2($database, $matricno,$semester, $clevel, $sem2, $department)
    {
        //echo "<br>".$clevel;
        // echo "<br>".$semester;
        //  echo "<br>".$sem2;
        // echo "<br>".$department;
        // echo "<br>".$matricno;
        
        

        if ($clevel == "ND II") {
            $qry = "SELECT courses,regDate FROM ndii_tbl 
			WHERE matricno ='$matricno' and ses_sem_id = '$sem2'
			";
        } else if ($clevel == "ND I") {
            $qry = "SELECT courses, regDate FROM ndi_tbl 
			WHERE matricno ='$matricno' and ses_sem_id = '$sem2'
			";
        }else if (($clevel == "PGD I") &&($semester=='Second')) {
            $qry = "SELECT courses, regDate FROM pgdi_tbl 
			WHERE matricno ='$matricno' and ses_sem_id = '$sem2'
			";
        } 
        else if ($clevel == "HND I") {
            $qry = "SELECT courses, regDate FROM hndi_tbl 
			WHERE matricno ='$matricno' and ses_sem_id = '$sem2'
			";
        }else if ($clevel == "HND II")  {
            
           
            
            $qry = "SELECT courses, regDate FROM hndii_tbl 
			WHERE matricno ='$matricno' and ses_sem_id = '$sem2'
			";
        }

        $result = $database->query($qry);
        //$result = mysql_query($qry);
        $count = 0;
        while ($row = mysqli_fetch_array($result)) {
            //while ($row= mysql_fetch_array($result)){
            $code = $row{"courses"};
            $date = $row{"regDate"};
        }
        $RegCourses = explode(',', $code);
        $this->SetFont('Times', 'BU', 14);
        $this->Cell(189, 20, 'COURSE REGISTRATION FORM', 0, 0, 'C');
        $this->Ln(10);
        $this->SetFont('Times', '', 12);
        $this->Cell(189, 10, 'Reg.Date: ' . $date, 0, 1, 'R');
        $this->SetFont('Times', 'B', 12);
        $this->SetFillColor(150, 255, 150);
        $this->Cell(20, 10, 'S/N', 1, 0, 'C', true);
        $this->Cell(30, 10, "Course Codes", 1, 0, "C", true);
        $this->Cell(120, 10, "Course Titles", 1, 0, "C", true);
        $this->Cell(19, 10, "Units", 1, 1, "C", true);
        $total = 0;
        foreach ($RegCourses as $course_code) {

            $query = "SELECT * FROM tbl_courses 
			WHERE course_code = '$course_code' AND department = '$department'
			";
            $sql = $database->query($query);
            //while($rows = mysqli_fetch_array($sql,MYSQLI_ASSOC))
            while ($rows = mysqli_fetch_array($sql)) {
                $count += 1;
                $code = $rows["course_code"];
                $title = $rows["course_title"];
                $unit = $rows["course_unit"];
                $total = $total + $unit;

                $this->SetFont('Times', '', 12);
                $this->Cell(20, 10, $count, 1, 0, 'C');
                $this->Cell(30, 10, $code, 1, 0);
                $this->Cell(120, 10, $title, 1, 0);
                $this->Cell(19, 10, $unit, 1, 1, 'C');

            }
        }
        $this->SetFont('Times', 'B', 14);
        $this->Cell(170, 10, 'Total Units:' . $total, 0, 0, 'R');
        $this->Ln(10);
    }










        

    function body()
    {
        $this->SetFont('Times', 'B', 12);
        $this->SetFillColor(255, 0, 0);
        $this->Cell(276, 5, 'Welcome $surname', 0, 1);
        $this->Cell(50, 5, $matricno, 0, 0, "C");
        $this->Cell(50, 5, $semester, 0, 0, "C");
        $this->Cell(50, 5, $se, 0, 0, "C");
        $this->Ln(20);
    }
}


$pdf = new coursePDF();
$pdf->SetTitle('Course Form '.$clevel,'UTF-8');
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->Head($department);

$pdf->Background();
$pdf->SetFont('Times', 'BU', 12);
$pdf->SetFillColor(255, 0, 0);
$pdf->Cell(120, 10, 'Name: ' . strtoupper($surname) . ', ' . $othernames, 0, 1, 'R');
$pdf->Ln(1);
$pdf->Image($pix, 170, 15, 30,30);
$pdf->Ln(1);
$pdf->Cell(70, 10, $msg. ' ' . $matricno, 0, 0, 'L');

$pdf->Cell(60, 10, $semester1 . '  Semester:  ' . $clevel, 0, 0, 'L');

$pdf->Cell(20, 10, 'Session:' . $se, 0, 0, 'L');
$pdf->Ln(8);
$pdf->headerTable($database, $matricno, $semester1, $clevel, $sem1, $department);
$pdf->Ln(20);
$pdf->SetFont('Times', 'B', 10);
$pdf->Cell(10, 5, '--------------------------', 0, 0, 'L');
$pdf->Cell(170, 5, '--------------------------', 0, 1, 'R');
$pdf->Cell(5, 3, "HOD's Signature and Date", 0, 0, 'L');
$pdf->Cell(175, 3, "Bursar's Signature and Date", 0, 0, 'R');
$pdf->Ln(20);
$pdf->Cell(10, 5, '---------------------------------------------', 0, 0, 'L');
$pdf->Cell(170, 5, '---------------------------------------------', 0, 1, 'R');
$pdf->Cell(5, 3, "Academics Officer's Signature and Date", 0, 0, 'L');
$pdf->Cell(175, 3, "Faculty Officer's Signature and Date", 0, 0, 'R');






$pdf->SetTitle('Course Form '.$clevel,'UTF-8');
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->Head2($department);

$pdf->Background();
$pdf->SetFont('Times', 'BU', 12);
$pdf->SetFillColor(255, 0, 0);
$pdf->Cell(120, 10, 'Name: ' . strtoupper($surname) . ', ' . $othernames, 0, 1, 'R');
$pdf->Ln(1);
$pdf->Image($pix, 170, 15, 30,30);
$pdf->Ln(1);
$pdf->Cell(70, 10, $msg. ' ' . $matricno, 0, 0, 'L');

$pdf->Cell(60, 10, 'Second' . '  Semester:  ' . $clevel, 0, 0, 'L');

$pdf->Cell(20, 10, 'Session:' . $se, 0, 0, 'L');
$pdf->Ln(8);
$pdf->headerTable2($database, $matricno,$semester2, $clevel, $sem2, $department);
$pdf->Ln(20);
$pdf->SetFont('Times', 'B', 10);
$pdf->Cell(10, 5, '--------------------------', 0, 0, 'L');
$pdf->Cell(170, 5, '--------------------------', 0, 1, 'R');
$pdf->Cell(5, 3, "HOD's Signature and Date", 0, 0, 'L');
$pdf->Cell(175, 3, "Bursar's Signature and Date", 0, 0, 'R');
$pdf->Ln(20);
$pdf->Cell(10, 5, '---------------------------------------------', 0, 0, 'L');
$pdf->Cell(170, 5, '---------------------------------------------', 0, 1, 'R');
$pdf->Cell(5, 3, "Academics Officer's Signature and Date", 0, 0, 'L');
$pdf->Cell(175, 3, "Faculty Officer's Signature and Date", 0, 0, 'R');















$pdf->Output($surname.'-'.$clevel.'.pdf','I');
?>