<?php

ob_start();


//include my own function
include 'includes/functions_.php';



//customly collect the id and session
if(isset($_GET['u']))
{
	$u = $_GET['u'];
	$s = $_GET['s'];
	$sem = $_GET['se'];
}
else
{
	header("Location:dashboard.php?act=check_results0&qlk=".md5(3));
}
//collect other dinfo
$dept_id = get_user_department_id($u);
$school_id = get_user_school_id($u);
$programme_id = get_user_programme_id($u); //get programme if for a suer
$level = get_user_level($u); //get user level
$programme_type = get_user_programme_type_id($user); //programme type


$session = $s ;//get_current_session($id = '1'); //current session is always



$validity_status = get_user_validity_status($student_id);//get studenrt validity whther expelled or not






// 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.jpg';
		$this->Image($image_file, 10, 10, 15, '', 'JPG', '', '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, 'NIGER STATE COLLEGE OF EDUCATION MINNA ', 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  Copyright (c) (".date('Y').") NIGER STATE COEMINNA, ALL RIGHT RESERVED 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);


//customly collect the title
$title = str_replace ('/','_',get_user_mat_no($u)). '-CRF';
$title =strtoupper($title);
$author = get_user_fullname($u).'_ COE MINNA';


// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor($author);
$pdf->SetTitle($title);
$pdf->SetSubject('Course Form');
$pdf->SetKeywords('COEMINNA, PDF, course, slip, '.$s.'');


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

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




//collect other dinfo
$dept_id = get_user_department_id($u);
$school_id = get_user_school_id($u);

$level = get_user_level($u); //get user level
$name = strtoupper (get_user_fullname($u));
$mat_no = strtoupper (get_user_mat_no($u));


//collect academic info
$pt = strtoupper(show_programme_type(get_user_programme_type($u)));
$study_mode = strtoupper(show_study_mode(get_user_study_mode($u)));
$shool = strtoupper(get_school_title(get_user_school_id($u)));
$department = strtoupper(get_department_title(get_user_department_id($u)));
$programme = strtoupper(get_programme_title(get_user_programme_id($u)));

$st =  get_session_title($session);
$semt = get_semester_title($sem);

$passport = get_user_image($u);

$passport = '<img src="../uploads/'.$passport.'" height = "120" width="100" />';


//set the content //this is my own function
$output0 = '';
  //$output = '<div class="text-center"><img src="../images/logo.png" height="80"  width="80"/></div>';


 $output0 .= '<br/><h5  style="color:#00000; font-weight:normal" ><i>P.M.B 39, MINNA, NIGER STATE</i></h5>';
 $output0  .= '<h6   style="color:#00000; font-weight:normal"> '. $st .' SESSION '.strtoupper($semt).'</h6>';
 $output0  .= '<h5  style="color:#0000; font-weight:normal;"  >STATEMENT OF RESULT -  RS</h5>';



  $output ='';
  $outputr = '';


  //view the temporary table and show the preview for first semester
  $q3 = "SELECT id, student_id, programme_id, course_id, course_unit,session_id,  level, semester,total_score, seen_as_elective
			   FROM  students_courses_nce
			   WHERE semester = '$sem'
			   AND student_id ='$u'
			   AND session_id = '$s'
			   ORDER BY course_category";

	  $r3 = mysqli_query(dbc(),$q3);
	  $sn = 0 ;
	  if(mysqli_num_rows($r3) > 0)
	  {//show table





            //$output .= '<h6 id="list" class="btn btn-primary">FIRST SEMESTER</h6>';


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


                                        <tr>
										<th width="5%" align="center">S/N</th>
										<th width="10%" align="center">CODE</th>
										<th width="50%" align="center">COURSE NAME</th>
										<th width="10%" align="center" >TYPE</th>
										<th width="5%" align="center">UNIT </th>
										<th width="10%" align="center" >GRADE </th>
										<th width="10%" align="center">POINT </th>

										</tr>

							 </thead>
							<tbody>';

		  while($row = mysqli_fetch_array($r3, MYSQLI_ASSOC))
		  {
			   //level to show
	  			$level_to_show = $row['level'];

			  $output .= '<tr>';
			  $output .= '<td width="5%" >'.$sn = $sn+1 .'</td>';
			  $output .= '<td width="10%"> '.get_course_code($row['course_id']).'</td>';
			  $output .= '<td width="50%"> '.get_course_name($row['course_id']).'</td>';
			  $output .= '<td width="10%" align="center">'.seen_as_elective_for_pdf($row['seen_as_elective']).'</td>';
			  $output .= '<td width="5%" align="center">'.get_course_unit($row['course_id']).'</td>';
			  $output .= '<td width="10%" align="center" >'.get_student_grade_by_score($row['total_score']).'</td>';

			  $output .= '<td width="10%" align="center">'.get_student_grade_point_score($row['total_score']) * get_course_unit($row['course_id']).'</td>';

			  $output .= '</tr>';



		  }

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


			$outputr .=
				  '<br/><table  cellspacing="1" cellpadding="1" border="0"  style="font-size:12px;">

					<tbody>';


					$outputr .= "<tr>";
					$outputr .= "<td width='50%'>";
					$outputr .= "</td><td width='50%' ></td>";
					$outputr .= "</tr>";

					$outputr .= "<tr>";

					$outputr .= "<td width='50%'><b>";
					$outputr .= "PREVIOUS";
					$outputr .= "</b></td><td></td>";

					$outputr .= "</tr>";

					$outputr .= "<tr>";

					$outputr .= "<td>";
					$outputr .= "TOTAL CREDIT UNITS REGISTERED: ".get_total_prev_credit_unit_registered_per_semester($u, $s, $sem)." <br/>

					TOTAL GRADE POINTS: ". get_total_prev_grade_points_per_semester($u, $s, $sem);
					$outputr .= "</td>";


					$outputr .= "<td>";
					$outputr .= "TOTAL CREDIT UNITS EARNED:  ". get_total_credit_units_earned_per_semester($u, $s, $sem)."<br/>
					CUMMULATIVE GRADE POINTS: ".get_total_prev_gpa_per_semester($u, $s, $sem);
					$outputr .= "</td>";

					$outputr .= "</tr>";


					$outputr .= "<tr>";

					$outputr .= "<td><b>";
					$outputr .= "PRESENT";
					$outputr .= "</b></td><td></td>";

					$outputr .= "</tr>";


					$outputr .= "<tr>";

					$outputr .= "<td>";
					$outputr .= "TOTAL CREDIT UNITS REGISTERED: ".get_total_present_credit_unit_registered_per_semester($u, $s, $sem)." <br/>

					TOTAL GRADE POINTS: ".get_total_present_grade_points_per_semester($u, $s, $sem);
					$outputr .= "</td>";


					$outputr .= "<td>";
					$outputr .= "TOTAL CREDIT UNITS EARNED:  ". get_total_present_credit_units_earned_per_semester($u, $s, $sem)."<br/>
					CUMMULATIVE GRADE POINTS:
					".get_total_present_gpa_per_semester($u, $s, $sem);
					$outputr .= "</td>";

					$outputr .= "</tr>";



					$outputr .= "<tr>";

					$outputr .= "<td><b>";
					$outputr .= "CUMMULATIVE";
					$outputr .= "</b></td><td></td>";

					$outputr .= "</tr>";


					$outputr .= "<tr>";

					$outputr .= "<td>";
					$outputr .= "TOTAL CREDIT UNITS REGISTERED:  ". get_total_cummulative_credit_unit_registered_per_semester($u, $s, $sem)."<br/>

					TOTAL GRADE POINTS: ".get_total_cummulative_grade_points_per_semester($u, $s, $sem);
					$outputr .= "</td>";


					$outputr .= "<td>";
					$outputr .= "TOTAL CREDIT UNITS EARNED:  ".get_total_cummulative_credit_units_earned_per_semester($u, $s, $sem)."<br/>
					CUMMULATIVE GRADE POINTS:
					".get_total_cummulative_gpa_per_semester($u, $s, $sem);
					$outputr .= "</td>";

					$outputr .= "</tr>";



					if($sem == 1)
					{
						$outputr .= "<tr>";

						$outputr .= '<td colspan="2">';
						$outputr .= "<b>REMARK</b>: ".get_remark2_per_semester($u, $s, $sem)." : ".get_remark1_per_semester($u, $s, $sem =1);
						$outputr .= "</td>";

						$outputr .= "</tr>";

					}
					else
					{
							$outputr .= "<tr>";

							$outputr .= '<td colspan="2">';
							$outputr .= "<b>REMARK</b>: ".get_remark2_per_semester($u, $s, $sem)." : ".get_remark1_per_semester($u, $s, $sem=1).', '.get_remark1_per_semester($u, $s, $sem =2);
							$outputr .= "</td>";

							$outputr .= "</tr>";
					}


					$outputr .='</tbody>
							  </table>';
	  }
	  else
	  {//show the msg

			  $output .='No record found.';


	  }





$date = date("l jS \of F Y h:i:s A");


//$string_ =  get_session_title($s).' Course Form';


// set some text to print
$txt = <<<EOD

<div align="center"> $output0 </div>

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

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

			  <tr>
			  <td width="30%"> <b>MATRIC No. :</b> $mat_no</td>
			  <td width="40%"> <b>NAME :</b> $name</td>
			   <td width="20%"> <b>LEVEL :</b>  $level_to_show</td>
			   <td width="10%" rowspan="3"> $passport </td>
			  </tr>


			  <tr>
			   <td width="20%"><b>SESSION:</b> $st</td>
			  <td width="40%"><b>PROGRAMME TYPE :</b> $pt</td>
			  <td width="30%" colspan="2"> <b>STUDY MODE. :</b> $study_mode</td>

			  </tr>

			   <tr>


			   <td width="50%"> <b>SCHOOL:</b> $shool</td>
			   <td width="40%" colspan="2"> <b>PROGRAMME :</b> $programme</td>


			  </tr>



 </table>


<br/><br/><br/>

$output

<br/>

$outputr

<br/><br/><br/>



<table style="font-size:7px;" border="0" cellspacing="0" cellpadding="1">
<tr>
<td width="100%">
..................................................................................................................................................................................................................................................................................................................<br>
_______________________ HOD'S SIGNATURE & STAMP ______________________________________________________ __ DATE _______________________________
</td>
</tr>

<tr>
<td width="100%">
Date: $date
</td>
</tr>


</table>




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

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






// -- set new background ---

//// get the current page break margin
//$bMargin = $pdf->getBreakMargin();
//// get current auto-page-break mode
//$auto_page_break = $pdf->getAutoPageBreak();
//// disable auto-page-break
//$pdf->SetAutoPageBreak(false, 0);
//// set bacground image
//$img_file = K_PATH_IMAGES.'bg.jpg';
//$pdf->Image($img_file, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
//// restore auto-page-break status
//$pdf->SetAutoPageBreak($auto_page_break, $bMargin);
//// set the starting point for the page content
//$pdf->setPageMark();


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

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



//temporarily use it
ob_end_clean();


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

//============================================================+
// END OF FILE
//============================================================+
