<?php
session_start();
include("../../../php_crud/my_class.php");
$obj=new my_class();
 
extract($_GET);
$shift_id;
 $medium_id;
 $class_id;
 $department_id;
 $student_type_id;
 $section_id;
 $exam_term_id;
 $exam_session;


 
 $det_in = $obj->Details_By_Cond("_int_institute_setup_vw","section_id='$section_id'");

$term_f = $obj->View_column_details_By_Cond("_int_exam_term","_id","_term_type='F' AND _branch_id='".$det_in['branch_id']."'");

$branch_id = isset($_SESSION['LOGIN_BRANCH']) ? $_SESSION['LOGIN_BRANCH']: '';
if($branch_id !=''){
	$brans_obj = $obj->View_branch_details_By_Cond($branch_id,'B');
	$inst_name = $brans_obj->insName;
	$ins_code_name = $brans_obj->insCodeName; 
	$extablishd_year = $brans_obj->_establish_year ;
	$ins_logo = $brans_obj->_logo_location;
	$ins_address = $brans_obj->_address;
	$inst_thanta = $brans_obj->thana_name ;
	$ins_district = $brans_obj->distrinc_name  ;
	$ins_division = $brans_obj->division_name  ;
	$ins_web = $brans_obj->_web_address !='' ? $brans_obj->_web_address : '' ;
	$ins_email = $brans_obj->_email_add !='' ? '|| '.$brans_obj->_email_add : '' ;	
}

 
	$im='../../../'.$ins_logo;
	if(file_exists($im)){
		$image = '<img src="../../../'.$ins_logo.'" style="top:20px;height:150px; float:left;margin-left: -12px;" title="logo" alt="" id="image" />';
				
	}else{
		$image = '<img src="../../../asset/custom/shadow.jpg"  />';								
	}

	// Include the main TCPDF library (search for installation path).
	require_once('../../../asset/custom/tcpdf/examples/tcpdf_include.php');
	require_once('../../../asset/custom/tcpdf/tcpdf.php');

	 $PDF_HEADER_LOGO = "logo_example.jpg";//any image file. check correct path.
	/*$PDF_HEADER_LOGO_WIDTH = "20";
	$PDF_HEADER_TITLE = "This is my Title";
	$PDF_HEADER_STRING = "Tel 1234567896 Fax 987654321\n"
	. "E abc@gmail.com\n"
	. "www.abc.com";
	$pdf->SetHeaderData($PDF_HEADER_LOGO, $PDF_HEADER_LOGO_WIDTH, $PDF_HEADER_TITLE, $PDF_HEADER_STRING); */

	// create new PDF document
	//$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
	$your_width=210;
	$your_height=297;
	$custom_layout = array($your_width, $your_height);

	class MYPDF extends TCPDF {			
		protected $image1 ;
		public $size ;
		public $type_id ;
		public function setImagesrc($sr) {
		 $this->image1 = $sr;
		}
		public $isLastPage = false;	
		public function Header() {		
			$this->SetAlpha(0.09);
			$this->Image($this->image1, 65, 95, 70, 100, '', '', '', true, 50);
			$this->SetAlpha(1);
		}
	

	
    public function Footer() {
        //$image_file = "";
        //$this->Image($image_file, 11, 241, 189, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
          
        $this->SetFont('helvetica', 'N', 10);	
		$this->Line(15, 290, 205, 290);		
		$this->SetY(-7);
		$this->SetX(15);
		$by = 'Generated By:'.$_SESSION["LOGIN_USER"];
        $this->SetFont('helvetica', 'N', 8);
        $this->Cell(0, 5,$by, 0, false, '', 0, '', 0, false, 'T', 'M');
		
		$this->SetY(-4);
		$this->SetX(15);
		$date = 'Printed Date:';
        $this->SetFont('helvetica', 'N', 6);
        $this->Cell(0, 5,$date.   date(" d/m/Y H\hi:s"), 0, false, '', 0, '', 0, false, 'T', 'M');
        $this->SetY(-5);
        $this->SetFont('helvetica', 'N', 10);
		$a = 'Powred by:'.$_SESSION["powred"];
        $this->Cell(0, 5, $a, 0, false, 'C', 0, '', 0, false, 'T', 'M');
		
		$this->SetY(-7);
    // Set font
    $this->SetFont('helvetica', 'I', 8);
    // Page number
    $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
    
/* 	 if($this->isLastPage)
{ */	
$style3 = array('width' => 0.4, 'cap' => 'round', 'join' => 'round', 'dash' => '1,1', 'color' => black);

$this->Line(15, 283, 50, 283, $style3);
$this->Line(85, 283, 130, 283, $style3);
$this->Line(170, 283, 205, 283, $style3);
$this->Text(15, 285, 'Parents Signature');
$this->Text(90, 285, 'approved by');
$this->Text(170, 285, 'Principle Signature');
//}
	
		
    }
	public function lastPage($resetmargins=false) {
    $this->setPage($this->getNumPages(), $resetmargins);
    $this->isLastPage = true;
}
}

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, $custom_layout, true, 'UTF-8', false);
/* logo */
$pdf->setImagesrc('../../../'.$ins_logo);
$pdf->size= $size;	
$pdf->type_id= $type_id;
//$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, $custom_layout, true, 'UTF-8', false);


//$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
/* $pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 007');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide'); */

// set default header data
//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 007', PDF_HEADER_STRING);

//$PDF_HEADER_LOGO = "Untitled-1.jpg";//any image file. check correct path.
/* $PDF_HEADER_LOGO_WIDTH = "20";
$PDF_HEADER_TITLE = "This is my Title";
$PDF_HEADER_STRING = "Tel 1234567896 Fax 987654321\n"
. "E abc@gmail.com\n"
. "www.abc.com";
$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);
}

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


$empid;
$data_list_personal_info=$obj->Details_By_Cond('_pf_emp_personal_info',"_id='".$empid."'");

//$image = '$data_list_personal_info['_image_location']';

// set font
 $pdf->SetFont('times', '', 10);




 

foreach($obj->View_All_By_Cond(" _pf_std_basic_info ","_section_id = '$section_id'") as $value){
				extract($value);
				//echo $value['_pid'];

	$pdf->AddPage();
	
	/*  */
	$test = '<table style="width: 100%;height:10%;"  cellspacing="">';
$test1 = '</table>';
// create columns content
$test .= '<tr>
        <td style=" width: 20%;">
		'.$image.'
		                 
		               
						 
		</td>
        <td style=" width: 60%;">
						<h1 style="font-size: 25px;text-align: center;line-height: 10px;margin-bottom: 3px; margin-top: 20px;;"><b>  '.$inst_name.'</b>
						  ('.$ins_code_name.')
						</h1>
						<h5 style="text-align:center;font-size:14px;margin-top:6px"> '.$ins_address.' , '.$inst_thanta.' , '.$ins_district.' , '.$ins_division.'  </h5>
						 <h5 style="text-align:center;font-size:10px;line-height: 5px;">'.$ins_web.'  '.$ins_email.' </h5>
						 
						 <h2 style="font-size:22px;text-align:center; margin-top: 30px;padding-bottom: 1px;width:100%;">Final Mark Sheet</h2>
						
						
		</td>
        <td style=" width:20%;">
		
		
					
		
		</td>
        
    </tr>';






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

$pdf->writeHTML($test . $test1, false, false, false, false, '');

$pdf->Ln(20);





$details = $obj->View_column_details_By_Cond('_pf_std_personal_info','*','_id= "'.$value['_pid'].'"');





               




      $det =  '<table style="width:100%">';
				 
      $det1 = '</table >';
        $det .= '<tr>
		         <td style="width:70%">
				  <table border="1" style="width:100%">
					<tbody>';
					$term = $obj->Details_By_Cond("_int_exam_term","_id='$exam_term_id' and _branch_id='$branch_id'");
									$sec = $obj->Details_By_Cond("_int_institute_setup_vw","section_id='$section_id'");
									$st_type = $obj->Details_By_Cond("_int_institute_setup_vw","student_type_id='$student_type_id'");
									$dept = $obj->Details_By_Cond("_int_institute_setup_vw","department_id='$department_id'");
									$class = $obj->Details_By_Cond("_int_institute_setup_vw","class_id='$class_id'");
									$med = $obj->Details_By_Cond("_int_institute_setup_vw","medium_id='$medium_id'");
									$shift = $obj->Details_By_Cond("_int_institute_setup_vw","shift_id='$shift_id'");
									
					
					
						 $det .= ' <tr style="height:50px;">
						<td class="" id="td1" style="border: 1px solid #000000;width:25%;height:20px;"><b>Shift  </b></td>
						<td id="td2" style="border: 1px solid #000000;width:75%;height:20px;">'.$shift['shift'].' </td>
						</tr><tr>
						<td class="" id="td1" style="border: 1px solid #000000;width:25%;height:20px;"><b>Medium </b></td>
						<td id="td2" style="border: 1px solid #000000;width:75%;height:20px;">'.$med['medium'].' </td>
						</tr>
										
										
						<tr> 
							<td class="" id="td1" style="border: 1px solid #000000;width:25%;height:20px;"><b>Class  </b>  </td>
							<td id="td2" style="border: 1px solid #000000;width:75%;height:20px;">'.$class['class'].'</td>
						</tr><tr>	
							<td class="" id="td1" style="border: 1px solid #000000;width:25%;height:20px;"><b>Depertment  </b> </td>												
							<td id="td2" style="border: 1px solid #000000;width:75%;height:20px;">'. $dept['department'].'</td>	
						</tr>
						
						
						<tr> 
							<td class="" id="td1" style="border: 1px solid #000000;width:25%;height:20px;"><b>Student Type  </b> </td>
							<td id="td2" style="border: 1px solid #000000;width:75%;height:20px;">'.$st_type['student_type'].'</td>								
						</tr><tr>	
							<td class="" id="td1" style="border: 1px solid #000000;width:25%;height:20px;"><b>Section  </b> </td>
							<td id="td2" style="border: 1px solid #000000;width:75%;height:20px;">'.$sec['section'].'</td>
						</tr>
										
						<tr> 
										<td class="" id="td1" style="border: 1px solid #000000;width:25%;height:20px;"><b>Session  </b></td>
										<td id="td2" style="border: 1px solid #000000;width:75%;height:20px;">'.$exam_session.'</td>
										
						</tr><tr>				
										<td class="" id="td1" style="border: 1px solid #000000;width:25%;height:20px;"><b>Term exam   </b> </td>
										<td id="td2" style="border: 1px solid #000000;width:75%;height:20px;">'. $term['_term_name'].'</td>
										
										
										
						</tr>
										 <tr> 
										 <td class="" id="td1" style="border: 1px solid #000000;width:25%;height:20px;"><b>Name </b> </td>	
										 <td id="td2" style="border: 1px solid #000000;width:75%;height:20px;">'.$details['_full_name'].'</td>	
						</tr><tr>				 
										 <td class="" id="td1" style="border: 1px solid #000000;width:25%;height:20px;"><b>Section Roll  </b></td>	
										 <td id="td2" style="border: 1px solid #000000;width:75%;height:20px;">'.$details['_section_roll'].'</td>	
										 
																	
						</tr>
					</tbody>
				  </table>
				</td>
                <td style="width:10%"></td>
    
                <td style="width:20%">';
		$det .= '<table border="1" style="width:100%">
		            <thead  >
					<tr>
					<th  style="border: 1px solid #000000;font-size:11px;text-align:center; ">Letter Grade</th>
					<th style="border: 1px solid #000000;font-size:11px;text-align:center; ">Class Interval</th>
					<th  style="border: 1px solid #000000;font-size:11px;text-align:center; ">Grade Point</th>
					</tr>
					 </thead  >
				  <tbody>';
				  
					  //extract($term);
$per = '';
				   $termssss = $obj->View_all_data_row_column_By_Cond("_int_grading_system","*","_branch_id='$branch_id'");
				   foreach($termssss AS $term){
				
					$per .= '<tr>
							 <td class=""  style="border: 1px solid #000000;"> '.$term['_grade'].' </td>
							 <td class=""  style="border: 1px solid #000000;">'.$term['_from_mark'].'-'.$term['_to_mark'].'</td>
							 <td class=""  style="border: 1px solid #000000;"> '.$term['_gpa'].' </td>
							</tr>';
						}	
 $det .= $per .'</tbody>
				</table>
				</td>
                </tr>';				  
 						   


$pdf->writeHTML($det . $det1, true, true, false, false, '');

$pdf->Ln(20);


$test = '<p style="text-align:center;font-size:25px;"><b>Subject-Wise Grade/ Mark Sheet</b></p>';
$pdf->writeHTML($test, true, true, false, false, '');

$pdf->Ln(10);




/*============================= for mark ============================*/
/* total row count */
$total_obj = $obj->num_of_rows("_int_subject","_dept_id='".$department_id."' ");
	    $total_row =$total_obj->num ;
		
		$sub = $obj->Details_By_Cond("_int_subject_select","_std_id='".$value['_pid']."'  AND _session='$exam_session' AND _section_id='$section_id'");
		
		   $sum = 0;

		   if($sub['_combined_sub_ids']){
		   		$fail_cmb_sub_count = $obj->Total_Count_By_Cond("_ex_mark_sheet_entry","_term_id='".$term_f['_id']."' AND _session='$exam_session' AND _section_id='$section_id' AND _std_id='".$value['_pid']."' AND _subject_id IN (".$sub['_combined_sub_ids'].") AND _gpa='0'");
           }else{$fail_cmb_sub_count ='0';}

           if($sub['_main_sub_ids']){
           		$fail_main_sub_count =$obj->Total_Count_By_Cond("_ex_mark_sheet_entry","_term_id='".$term_f['_id']."' AND _session='$exam_session' AND _section_id='$section_id' AND _std_id='".$value['_pid']."' AND _subject_id IN (".$sub['_main_sub_ids'].") AND _gpa='0'");
		   }else{$fail_main_sub_count ='0';}

		   if($sub['_3rd_sub_id']){
		   		$fail_3rd_sub_count =$obj->Total_Count_By_Cond("_ex_mark_sheet_entry","_term_id='".$term_f['_id']."' AND _session='$exam_session' AND _section_id='$section_id' AND _std_id='".$value['_pid']."' AND _subject_id IN (".$sub['_3rd_sub_id'].") AND _gpa='0'");
		   }else{$fail_3rd_sub_count ='0';}

		   if($sub['_4th_sub_id']){
		   		$count_4th_sub =$obj->Total_Count_By_Cond("_ex_mark_sheet_entry","_term_id='".$term_f['_id']."' AND _session='$exam_session' AND _section_id='$section_id' AND _std_id='".$value['_pid']."' AND _subject_id IN (".$sub['_4th_sub_id'].")");
		   }else{$count_4th_sub ='0';}
		   $total_failed_sub = $fail_cmb_sub_count+$fail_main_sub_count+$fail_3rd_sub_count;
			
	   		if($sub['_combined_sub_ids']){
 				$cmb_total = ($obj->Total_Count_By_Cond("_ex_mark_sheet_entry","_term_id='".$term_f['_id']."' AND _session='$exam_session' AND _section_id='$section_id' AND _std_id='".$value['_pid']."' AND _subject_id IN (".$sub['_combined_sub_ids'].") AND _subject_type='C'"))/2;
		 	}else{$cmb_total = '0';}

		 	foreach($obj->View_std_trabulation("std._id='".$value['_pid']."' AND sub_s._section_id='$section_id' AND sub_s._session='$exam_session' AND sub_s._status='A'") AS $value_cm){
			    $total_countable_sub =($obj->Total_Count_By_Cond("_ex_mark_sheet_entry","_term_id='".$term_f['_id']."' AND _session='$exam_session' AND _section_id='$section_id' AND _std_id='".$value['_pid']."' AND _subject_id IN(".$value_cm['_main_3rd_sub_ids'].")"))-$cmb_total;
			    $total_gpa = $total_countable_sub*5;
			}
			
       		foreach($obj->View_std_trabulation("sub_s._section_id='$section_id' AND sub_s._session='$exam_session' AND sub_s._status='A'") as $valu){
				
						$qr = "_term_id='".$term_f['_id']."' AND _session='$exam_session' AND _section_id='$section_id' AND _std_id='".$value['_pid']."' AND _status='A' AND _subject_id IN(". $valu['_all_sub_ids'] .")" ; 
				$asent_sub_count = $obj->Total_Count_By_Cond("_ex_mark_sheet_entry", $qr); 
            } 

			// =============================
				
				if($sub['_combined_sub_ids']){
					foreach($obj->Combined_sub_total("m_sh._session='$exam_session' AND m_sh._section_id='$section_id' AND m_sh._std_id='".$value['_pid']."' AND m_sh._subject_id IN (".$sub['_combined_sub_ids'].")") AS $comb_mrk){
						 $comb_tot_mark = $comb_mrk['_comb_total'];					
						 $comb_tot_gpa = $comb_mrk['_comb_gpa'];
					}
				}
				else{
					$comb_tot_mark='0';
					$comb_tot_gpa='0';
				}
				
				
				if($value_cm['_main_all_sub_ids']){
					foreach($obj->Main_3rd_sub_total("m_sh._session='$exam_session' AND m_sh._section_id='$section_id' AND m_sh._std_id='".$value['_pid']."' AND m_sh._subject_id IN (".$value_cm['_main_all_sub_ids'].")") AS $main_mrk){
						$main_tot_mark .= $main_mrk['_main_3rd_total'];					
						$main_tot_gpa .= $main_mrk['_main_3rd_gpa'];				
					}
				}
				else{
					$main_tot_mark='0';
					$main_tot_gpa='0';
				}
				
				if($sub['_4th_sub_id']){
					foreach($obj->Ssub_total_4th("m_sh._session='$exam_session' AND m_sh._section_id='$section_id' AND m_sh._std_id='".$value['_pid']."' AND m_sh._subject_id IN (".$sub['_4th_sub_id'].")") AS $mrk_4th){
						$tot_mark_4th = $mrk_4th['_4th_total'];					
						$tot_gpa_4th = $mrk_4th['_4th_gpa'];
					}
				}
				else{
					$tot_mark_4th='0';
					$tot_gpa_4th='0';
				}
				
				
				if($tot_gpa_4th>3){ $ex_4th_gpa = $tot_gpa_4th-3;}
			    $total_mark_witht_4th = $comb_tot_mark+$main_tot_mark+$tot_mark_4th;
				$total_mark_without_4th = $comb_tot_mark+$main_tot_mark;


				$obtain_gpa = $comb_tot_gpa+$main_tot_gpa;
				if(($comb_tot_gpa+$main_tot_gpa)<$total_gpa){
					if(($comb_tot_gpa+$main_tot_gpa+$ex_4th_gpa)<=$total_gpa){$obtain_gpa_4th = $comb_tot_gpa+$main_tot_gpa+$ex_4th_gpa;}
					elseif(($comb_tot_gpa+$main_tot_gpa+$ex_4th_gpa)>$total_gpa){$obtain_gpa_4th = $total_gpa;}
				}
				else{$obtain_gpa_4th = $comb_tot_gpa+$main_tot_gpa;}

				$cgpa_4th = round(($obtain_gpa_4th/$total_countable_sub), 2);
				$cgpa = round(($obtain_gpa/$total_countable_sub), 2);

		      
		
		$thf =  '<table style="width:100%">';
		$thf .='<thead  >
		        <tr>
				<th  style="border: 1px solid #000000;font-size:11px;text-align:center; ">Sl</th>
			
				<th  style="border: 1px solid #000000;font-size:11px;text-align:center; ">Subject Name</th>
				
				<th style="border: 1px solid #000000;font-size:11px;text-align:center; " >Marks</th>
				<th style="border: 1px solid #000000;font-size:11px;text-align:center; ">GPA</th>
				<th style="border: 1px solid #000000;font-size:11px;text-align:center; ">Grade</th>
				    
				<th style="border: 1px solid #000000;font-size:11px;text-align:center; ">CGPA</th>';
				 if($count_4th_sub!='0')
					{
				$thf .='<th style="border: 1px solid #000000;font-size:11px;text-align:center; ">CGPA(4th)</th>';
					}
				$thf .='<th style="border: 1px solid #000000;font-size:11px;text-align:center; ">Avarage Grade</th>';
				if($count_4th_sub!='0')
					{
				$thf .='<th style="border: 1px solid #000000;font-size:11px;text-align:center; ">Avarage Grade(4th)</th>';
				    }
				
				
				
			   $thf .=' </tr>
			
			
	    </thead><tbody>'; 
		
		$i=0;
		
		
    /* combined */
if($sub['_combined_sub_ids']){
foreach($obj->View_All_By_Cond("_int_subject","_id IN (".$sub['_combined_sub_ids'].") GROUP BY _combined_sub_id") AS $value_c){		
extract($value_c);
$subject = $value_c['_subject_full_name'];
$search = '-1';
$trimmed = str_replace($search, '', $subject);
$i++;
/* CGPA and AVERAGE grade */
$t_row =''; 
$t1_row =''; 
$tl_row ='';
$tl1_row ='';
			   
$cgp = '';
if($asent_sub_count=='0')
{ if($total_failed_sub=='0')
{ $cgp .= $cgpa;}else{$cgp .= $cgpa = '0';}}
else{$cgp .= 'Withheld';}

$cgp_4th = '';
			

$cgrade = '';
if($asent_sub_count=='0')
{ if($total_failed_sub=='0')
{ $cgrade .=$obj->Grade_check("_gpa<='$cgp'");}
else $cgrade .=$obj->Grade_check("_gpa<='$cgp'");}
else{$cgrade .='Withheld';}

$cgrade_4th = '';
			 
if($i==1)
{
$space = '';
for($j=0;$j<($total_row/2);$j++)
{
$space .= '<br/>';
}


$t_row .= '<td rowspan= "'.$total_row.'" style="vertical-align:middle;text-align:center;font-weight: bold;border: 1px solid #000000;">'.$space.$cgp.'</td>';


if($count_4th_sub!='0')
{ $t1_row .= '<td rowspan= "'.$total_row.'" style="vertical-align:middle;text-align:center;font-weight: bold;border: 1px solid #000000;">';
		 if($asent_sub_count=='0')
		{ if($total_failed_sub=='0')
		{ $cgp_4th .= $cgpa_4th;}
		else{$cgp_4th .=  $cgpa_4th = '0';}}
			else{$cgp_4th .=  'Withheld';}

$t1_row .=$space.$cgp_4th."</td>";
}



$tl_row .= '<td rowspan= "'.$total_row.'" style="vertical-align:middle;text-align:center;font-weight: bold;border: 1px solid #000000;">'.$space.$cgrade.'</td>';



if($count_4th_sub!='0'){ $tl1_row .= '<td rowspan= "'.$total_row.'" style="vertical-align:middle;text-align:center;font-weight: bold;border: 1px solid #000000;">';if($asent_sub_count=='0')
{ if($total_failed_sub=='0')
{ $cgrade_4th .=$obj->Grade_check("_gpa<='$cgp_4th'");
}
else $cgrade_4th .="F";}
else{$cgrade_4th .= 'Withheld';}
$tl1_row .=$space.$cgrade_4th."</td>";
}
}

/* for marks */

// echo   $value_c['_combined_sub_id'];
$s_res_cm = $obj->Combined_Mark("m_sh._session='$exam_session' AND m_sh._section_id='$section_id' AND m_sh._std_id='".$value['_pid']."' AND m_sh._combined_sub_id='".$value_c['_combined_sub_id']."'");
$s_status = $obj->View_column_details_By_Cond("_ex_mark_sheet_entry","_status","_term_id='".$term_f['_id']."' AND _session='$exam_session' AND _section_id='$section_id' AND _subject_id='".$value_c['_combined_sub_id']."' AND _std_id='".$value['_pid']."'");
$r = $obj->View_column_details_By_Cond("_int_grading_system","_gpa,_grade","_branch_id='".$det_in['branch_id']."' AND ('".$s_res_cm['_total_mark_avg']."' BETWEEN _from_mark AND _to_mark)");


$t_mark='';
$t_gpa='';
$t_grade='';
if($s_status['_status']=='P')
{  $t_mark .=$s_res_cm['_total_mark_avg']; 
$t_gpa .= $r['_gpa']; 
$t_grade .= $r['_grade'];} 
elseif($s_status['_status']=='A')
{echo 'Absent';}

$cd_res = $obj->Combined_Mark("m_sh._session='$exam_session' AND m_sh._section_id='$section_id' AND m_sh._std_id='".$value['_pid']."' AND m_sh._combined_sub_id='".$value_c['_combined_sub_id']."'");
				
				//echo $cd_res['_obj_marks_avg'];
				
				$thf .=' <tr> 
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$i.'</td>	
					<td style="border: 1px solid #000000;font-size:11px;text-align:center; height:20px;">'.$trimmed.'</td>
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$cd_res['_total_mark_avg'].'</td>
					   	
                    
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$t_gpa.'</td>	
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$t_grade.'</td>';	
                    $thf .= $t_row;	
                    $thf .= $t1_row;				   
                    $thf .= $tl_row;				   
                    $thf .= $tl1_row;				   
					$thf .='</tr>';
}
							
}		
		
		/* main sub */
foreach($obj->View_All_By_Cond("_int_subject","_id IN (".$sub['_main_sub_ids'].") ") AS $value_m){		

extract($value_m);
$value_m['_id'];


$i++;


/* CGPA and AVERAGE grade */
$t_row =''; 
$t1_row =''; 
$tl_row ='';
$tl1_row ='';
			   
$cgp = '';
if($asent_sub_count=='0')
{ if($total_failed_sub=='0')
{ $cgp .= $cgpa;}else{$cgp .= $cgpa = '0';}}
else{$cgp .= 'Withheld';}

$cgp_4th = '';
			

$cgrade = '';
if($asent_sub_count=='0')
{ if($total_failed_sub=='0')
{ $cgrade .=$obj->Grade_check("_gpa<='$cgp'");}
else $cgrade .=$obj->Grade_check("_gpa<='$cgp'");}
else{$cgrade .='Withheld';}

$cgrade_4th = '';
			 
if($i==1)
{
$space = '';
for($j=0;$j<($total_row/2);$j++)
{
$space .= '<br/>';
}


$t_row .= '<td rowspan= "'.$total_row.'" style="vertical-align:middle;text-align:center;font-weight: bold;border: 1px solid #000000;">'.$space.$cgp.'</td>';


if($count_4th_sub!='0')
{ $t1_row .= '<td rowspan= "'.$total_row.'" style="vertical-align:middle;text-align:center;font-weight: bold;border: 1px solid #000000;">';
		 if($asent_sub_count=='0')
		{ if($total_failed_sub=='0')
		{ $cgp_4th .= $cgpa_4th;}
		else{$cgp_4th .=  $cgpa_4th = '0';}}
			else{$cgp_4th .=  'Withheld';}

$t1_row .=$space.$cgp_4th."</td>";
}



$tl_row .= '<td rowspan= "'.$total_row.'" style="vertical-align:middle;text-align:center;font-weight: bold;border: 1px solid #000000;">'.$space.$cgrade.'</td>';



if($count_4th_sub!='0'){ $tl1_row .= '<td rowspan= "'.$total_row.'" style="vertical-align:middle;text-align:center;font-weight: bold;border: 1px solid #000000;">';if($asent_sub_count=='0')
{ if($total_failed_sub=='0')
{ $cgrade_4th .=$obj->Grade_check("_gpa <= '$cgp_4th'");
}
else $cgrade_4th .="F";}
else{$cgrade_4th .= 'Withheld';}
$tl1_row .=$space.$cgrade_4th."</td>";
}
}

/////////////////////
$md_res = $obj->Others_sub_marks("m_sh._session='$exam_session' AND m_sh._section_id='$section_id' AND m_sh._std_id='".$value['_pid']."' AND m_sh._subject_id='".$value_m['_id']."'");
$sm_status = $obj->View_column_details_By_Cond("_ex_mark_sheet_entry","_status","_term_id='".$term_f['_id']."' AND _session='$exam_session' AND _section_id='$section_id' AND _subject_id='".$value_m['_id']."' AND _std_id='".$value['_pid']."'");
$rm = $obj->View_column_details_By_Cond("_int_grading_system","_gpa,_grade","_branch_id='".$det_in['branch_id']."' AND ('".$md_res['_total_mark_avg']."' BETWEEN _from_mark AND _to_mark)");


$t_gpa='';
$t_grade='';
if($sm_status['_status']=='P')
{ 

$t_gpa .= $rm['_gpa']; 
$t_grade .= $rm['_grade'];} 
elseif($sm_status['_status']=='A')
{echo 'Absent';}
				
		
		
		
		
		    $thf .=' <tr> 
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$i.'</td>	
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$value_m['_subject_full_name'].'</td>
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$md_res['_total_mark_avg'].'</td>
					<td style="border: 1px solid #000000;font-size:11px;text-align:center; height:20px;">'.$t_gpa.'</td>	
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$t_grade.'</td>';	
                   $thf .= $t_row;	
                    $thf .= $t1_row;				   
                    $thf .= $tl_row;				   
                    $thf .= $tl1_row;				   
					$thf .='</tr>';
				//}
}
				
				
				
				
				
						
		/* 3rd sub */
		
		
if($sub['_3rd_sub_id']){		
foreach($obj->View_All_By_Cond("_int_subject","_id IN (".$sub['_3rd_sub_id'].") ") AS $value_3rd){		

extract($value_3rd);
$value_3rd['_id'];


$d3_res = $obj->Others_sub_marks("m_sh._session='$exam_session' AND m_sh._section_id='$section_id' AND m_sh._std_id='".$value['_pid']."' AND m_sh._subject_id='".$value_3rd['_id']."'");


$s3_status = $obj->View_column_details_By_Cond("_ex_mark_sheet_entry","_status","_term_id='".$term_f['_id']."' AND _session='$exam_session' AND _section_id='$section_id' AND _subject_id='".$value_3rd['_id']."' AND _std_id='".$value['_pid']."'");
$r3 = $obj->View_column_details_By_Cond("_int_grading_system","_gpa,_grade","_branch_id='".$det_in['branch_id']."' AND ('".$d3_res['_total_mark_avg']."' BETWEEN _from_mark AND _to_mark)");

$t_gpa='';
$t_grade='';
if($s3_status['_status']=='P')
{ 

$t_gpa .= $r3['_gpa']; 
$t_grade .= $r3['_grade'];} 
elseif($s3_status['_status']=='A')
{echo 'Absent';}
$i++;

/* CGPA and AVERAGE grade */
$t_row =''; 
$t1_row =''; 
$tl_row ='';
$tl1_row ='';
			   
$cgp = '';
if($asent_sub_count=='0')
{ if($total_failed_sub=='0')
{ $cgp .= $cgpa;}else{$cgp .= $cgpa = '0';}}
else{$cgp .= 'Withheld';}

$cgp_4th = '';
			

$cgrade = '';
if($asent_sub_count=='0')
{ if($total_failed_sub=='0')
{ $cgrade .=$obj->Grade_check("_gpa <= '$cgp'");}
else $cgrade .=$obj->Grade_check("_gpa <= '$cgp'");}
else{$cgrade .='Withheld';}

$cgrade_4th = '';
			 
if($i==1)
{
$space = '';
for($j=0;$j < ($total_row/2);$j++)
{
$space .= '<br/>';
}


$t_row .= '<td rowspan= "'.$total_row.'" style="vertical-align:middle;text-align:center;font-weight: bold;border: 1px solid #000000;">'.$space.$cgp.'</td>';


if($count_4th_sub!='0')
{ $t1_row .= '<td rowspan= "'.$total_row.'" style="vertical-align:middle;text-align:center;font-weight: bold;border: 1px solid #000000;">';
		 if($asent_sub_count=='0')
		{ if($total_failed_sub=='0')
		{ $cgp_4th .= $cgpa_4th;}
		else{$cgp_4th .=  $cgpa_4th = '0';}}
			else{$cgp_4th .=  'Withheld';}

$t1_row .=$space.$cgp_4th."</td>";
}



$tl_row .= '<td rowspan= "'.$total_row.'" style="vertical-align:middle;text-align:center;font-weight: bold;border: 1px solid #000000;">'.$space.$cgrade.'</td>';



if($count_4th_sub!='0'){ $tl1_row .= '<td rowspan= "'.$total_row.'" style="vertical-align:middle;text-align:center;font-weight: bold;border: 1px solid #000000;">';if($asent_sub_count=='0')
{ if($total_failed_sub=='0')
{ $cgrade_4th .=$obj->Grade_check("_gpa<='$cgp_4th'");
}
else $cgrade_4th .="F";}
else{$cgrade_4th .= 'Withheld';}
$tl1_row .=$space.$cgrade_4th."</td>";
}
}

/////////////////////
		    $thf .=' <tr> 
					<td style="border: 1px solid #000000;font-size:11px;text-align:center; height:20px;">'.$i.'</td>	
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$value_3rd['_subject_full_name'].'</td>
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$d3_res['_total_mark_avg'].'</td>
					   	
                    
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$t_gpa.'</td>	
					<td style="border: 1px solid #000000;font-size:11px;text-align:center; height:20px;">'.$t_grade.'</td>';	
                    $thf .= $t_row;	
                    $thf .= $t1_row;				   
                    $thf .= $tl_row;				   
                    $thf .= $tl1_row;				   
					$thf .='</tr>';
				//}
}
}				
				
				/* 4th */
if($sub['_4th_sub_id']){
foreach($obj->View_All_By_Cond("_int_subject","_id IN (".$sub['_4th_sub_id'].") ") AS $value_4th){		

extract($value_4th);
$value_4th['_id'];				
$d4_res = $obj->Others_sub_marks("m_sh._session='$exam_session' AND m_sh._section_id='$section_id' AND m_sh._std_id='".$value['_pid']."' AND m_sh._subject_id='".$value_4th['_id']."'");
$s4_status = $obj->View_column_details_By_Cond("_ex_mark_sheet_entry","_status","_term_id='".$term_f['_id']."' AND _session='$exam_session' AND _section_id='$section_id' AND _subject_id='".$value_4th['_id']."' AND _std_id='".$value['_pid']."'");
$r4 = $obj->View_column_details_By_Cond("_int_grading_system","_gpa,_grade","_branch_id='".$det_in['branch_id']."' AND ('".$d4_res['_total_mark_avg']."' BETWEEN _from_mark AND _to_mark)");



$t_gpa='';
$t_grade='';
if($s4_status['_status']=='P')
{ 

$t_gpa .= $r4['_gpa']; 
$t_grade .= $r4['_grade'];} 
elseif($s4_status['_status']=='A')
{echo 'Absent';}

$i++;

/* CGPA and AVERAGE grade */
$t_row =''; 
$t1_row =''; 
$tl_row ='';
$tl1_row ='';
			   
$cgp = '';
if($asent_sub_count=='0')
{ if($total_failed_sub=='0')
{ $cgp .= $cgpa;}else{$cgp .= $cgpa = '0';}}
else{$cgp .= 'Withheld';}

$cgp_4th = '';
			

$cgrade = '';
if($asent_sub_count=='0')
{ if($total_failed_sub=='0')
{ $cgrade .=$obj->Grade_check("_gpa<='$cgp'");}
else $cgrade .=$obj->Grade_check("_gpa<='$cgp'");}
else{$cgrade .='Withheld';}

$cgrade_4th = '';
			 
if($i==1)
{
$space = '';
for($j=0;$j<($total_row/2);$j++)
{
$space .= '<br/>';
}


$t_row .= '<td rowspan= "'.$total_row.'" style="vertical-align:middle;text-align:center;font-weight: bold;border: 1px solid #000000;">'.$space.$cgp.'</td>';


if($count_4th_sub!='0')
{ $t1_row .= '<td rowspan= "'.$total_row.'"  style="vertical-align:bottom;text-align:center;font-weight: bold;border: 1px solid #000000;">';
		 if($asent_sub_count=='0')
		{ if($total_failed_sub=='0')
		{ $cgp_4th .= $cgpa_4th;}
		else{$cgp_4th .=  $cgpa_4th = '0';}}
			else{$cgp_4th .=  'Withheld';}

$t1_row .=$space.$cgp_4th."</td>";
}



$tl_row .= '<td rowspan= "'.$total_row.'"  style="vertical-align:bottom;text-align:center;font-weight: bold;border: 1px solid #000000;">'.$space.$cgrade.'</td>';



if($count_4th_sub!='0'){ $tl1_row .= '<td rowspan= "'.$total_row.'" style="vertical-align:middle;text-align:center;font-weight: bold;border: 1px solid #000000;">';if($asent_sub_count=='0')
{ if($total_failed_sub=='0')
{ $cgrade_4th .=$obj->Grade_check("_gpa<='$cgp_4th'");
}
else $cgrade_4th .="F";}
else{$cgrade_4th .= 'Withheld';}
$tl1_row .=$space.$cgrade_4th."</td>";
}
}

/////////////////////
		      
		$thf .=' <tr> 
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$i.'</td>	
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$value_4th['_subject_full_name'].'</td>
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$d4_res['_total_mark_avg'].'</td>
					   	
                    
					<td style="border: 1px solid #000000;font-size:11px;text-align:center;height:20px; ">'.$t_gpa.'</td>	
					<td style="border: 1px solid #000000;font-size:11px;text-align:center; height:20px;">'.$t_grade.'</td>';	
                    $thf .= $t_row;	
                    $thf .= $t1_row;				   
                    $thf .= $tl_row;				   
                    $thf .= $tl1_row;				   
					 		 	
                            							
			$thf .='</tr>';
}
}
$thf .='</tbody>';
$thf1 = '</table >';
				

$pdf->writeHTML($thf.$thf1, true, true, false, false, '');
/*=============================end for mark ============================*/



	
}






// set color for background
$pdf->SetFillColor(255, 255, 200);

// set color for text
$pdf->SetTextColor(0, 63, 127);

// write the first column


// set color for background
$pdf->SetFillColor(215, 235, 255);

// set color for text
$pdf->SetTextColor(127, 31, 0);

// write the second column


// reset pointer to the last page
$pdf->lastPage();


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

//Close and output PDF document
   if (file_exists(__DIR__."/final_mark_sheet.pdf"))
                unlink(__DIR__."/final_mark_sheet.pdf"); 

   $pdf->Output(__DIR__."/final_mark_sheet.pdf", 'F');
 //$fileatt = $pdf->Output('mark_sheet.pdf', 'D');






