<?php //sleep(4); 
//LE 22-5-17 #6
ini_set("memory_limit","-1");
$baseurl = "../../../";
/* require_once($baseurl."epconfig/TaquaLB/Ajax/CGI/PHP/config.php");
LoadFile("pdfprint",$baseurl); */
$subDir = urldecode($_REQUEST['SubDir']);
$configdir = "../../../../../".$subDir;
require_once("../../../general/config.php");
require("../../../general/getinfo.php");
require_once "../../../general/TaquaLB/Elements/Elements.php";
require_once $configdir.$dbo->Config['Require']."PDF/mpdf60/mpdf.php";
require_once "../../../general/TaquaLB/Elements/Script/pdf.php";

$pdf = new PDFPrinter();
$pdf->SetBasePath($configdir."Files/");

extract($_GET);

$rstinfo = $dbo->RunQuery("SELECT f.FacName, d.DeptName, p.ProgName, p.YearOfStudy, p.Degree, s.SesName FROM fac_tb f, dept_tb d, programme_tb p, session_tb s WHERE f.FacID = d.FacID AND d.DeptID = p.DeptID AND s.SesID = $SesID AND p.ProgID = $ProgID LIMIT 1");

if(!is_array($rstinfo)){
    $pdf->Dump("Error Loading Result Info");
    $pdf->Finish();
    exit;
}
if($rstinfo[1] < 1){
    $pdf->Dump("No Result Info Found");
   // $pdf->_HTML();
    $pdf->Finish();
    exit;
}
$rstinfo = $rstinfo[0]->fetch_array();
$rstType = "MASTER MARK SHEET FOR THE AWARD OF {$rstinfo['Degree']} IN ".strtoupper($rstinfo['ProgName']);
if(trim($utitle) != "")$rstType = $utitle;
$topdet = '<div style="width:100%;margin-top:10px">

        <div style="width:33.3%;float:left;text-align:left; text-transform:uppercase">
          <b>FACULTY:</b> '.$rstinfo['FacName'].' <br/>
          <b>DEPARTMENT:</b> '.$rstinfo['DeptName'].' <br />
          <b>PROGRAMME:</b> '.$rstinfo['ProgName'].'
        </div>

        <div style="width:33.3%;float:left;text-align:center;font-weight:bold;text-decoration:underline">
        '.$rstType.'
        </div>

        <div style="width:33.3%;float:right;text-align:right">
        &nbsp;
      </div>
   <div style="clear:both"></div>
  </div> ';
$pdf->Header("MAIN CAMPUS/OBIO AKPA CAMPUS",array("LogoSize"=>"80px*80px","Style"=>"font-size:0.9em","WaterMark"=>"Abbr"),null,$topdet,array("Address"));

if(!isset($StudyID)){ 
    $pdf->Dump("Invalid Parameter");
      $pdf->Finish();
      exit;
  }
  if((int)$StudyID < 1){
    //get based on prog
    $sudobj = $dbo->SelectFirstRow("study_tb s, programme_tb p, dept_tb d, fac_tb f","s.ID","p.ProgID = $ProgID AND p.DeptID = d.DeptID AND d.FacID = f.FacID AND f.StudyID = s.ID");
    if(is_array($sudobj))$StudyID = $sudobj['ID'];
}
//get the programme details
$progDet = $dbo->SelectFirstRow("programme_tb","","ProgID=$ProgID");
$YearOfStudy = (int)$progDet['YearOfStudy'];

$validSes = [];
  //get all seesion starting from the students startses
  $AllSes = $dbo->Select("session_tb","","SesID >= $SesID Order By SesID LIMIT $YearOfStudy");
  
  //get valid sessions
  $yearCounter = 0;
  if(is_array($AllSes) && $AllSes[1] > 0){
    while($indSes= $AllSes[0]->fetch_assoc()){
      $validSes[$indSes['SesID']]=$indSes;
     $yearCounter++;
    }
  }
  $pdf->HTML();
  $tptStud = 0;
  ?>
<table style="width:100%;text-align:center;margin-top:10px">
    <thead>
<tr>
    <th rowspan="2">S/N</th><th rowspan="2">REGISTRATION NUMBER</th><th rowspan="2">NAME OF STUDENTS</th><th rowspan="2">SEX</th>
    <?php
     foreach($validSes as $SesIDid=>$sesDet){
        ?>
    <th colspan="3"><?=$sesDet['SesName']?></th>
        <?php
     }
    ?>
    <th colspan="3">CUMMULATIVE</th>
    <th rowspan="2">DEGREE</th>
</tr>
<tr>
    <?php
        echo str_repeat('<th>&sum;CH</th><th>&sum;GP</th><th>GPA</th>',count($validSes) + 1)
    ?>
</tr>
    </thead>
    <tbody>
<?php
//get all student in the programm by start ses
$allStud = $dbo->Select("studentinfo_tb","","ProgID=$ProgID AND StartSes=$SesID AND ClassID=$ClassID");

if(is_array($allStud) && $allStud[1] > 0){
    $tptStud = $allStud[1];
    $cnt = 0;
  while($indStud = $allStud[0]->fetch_assoc()){
    $dump = '';
    $cnt++;
    $regNo = trim($indStud['RegNo']) == ''?$indStud['JambNo']:$indStud['RegNo'];
    
     $dump .= "<tr>
     <td>$cnt</td><td style=\"text-align:left\">$regNo</td><td style=\"text-align:left\">".strtoupper($indStud['SurName'])." ".ucwords(strtolower($indStud['FirstName']))." ".ucwords(strtolower($indStud['OtherNames']))."</td><td>{$indStud['Gender']}</td>";
     $CCH = 0; $CGP = 0; $CGPA = '0.00';
     $RstHtmlc = '';
     $COP = '';
     $rstSeen = false;
     foreach($validSes as $SesIDi => $SesDet){
      $RstHtml = '';
      //get the student result by session
      $stidRst = $dbo->Select("result_tb","","RegNo='$regNo' AND SesID=$SesIDi ORDER BY GroupID DESC, Lvl ASC, Sem ASC");
      $TCH = ''; $TGP = ''; $GPA = '';$SeenRst = [];
      if(is_array($stidRst) && $stidRst[1] > 0){
        $rstSeen = true;
        $TCH = 0; $TGP = 0; 
        while($indStudRst = $stidRst[0]->fetch_assoc()){
          $rstSig = $indStudRst['Lvl'].$indStudRst['Sem'].$indStudRst['SesID'];
          if(in_array($rstSig,$SeenRst))continue;
          $SeenRst[] = $rstSig;
          $TCH += (int)$indStudRst['TCH'];
          $TGP += (int)$indStudRst['TGP'];
          $COP = $indStudRst['COP'];
        }
        $CCH += $TCH;
        $CGP += $TGP;
        if($TCH > 0){
          $GPA = round($TGP/$TCH,2);
        }
      }
      $dump .= "<td>$TCH</td>
      <td>$TGP</td>
      <td>".number_format($GPA,2)."</td>";
      
if($CCH > 0){
    $CGPA = number_format(round($CGP/$CCH,2),2);
  }
    }
    if(!$rstSeen)continue;
    $dump .= "<td>$CCH</td>
    <td>$CGP</td>
    <td>$CGPA</td>
    <td style=\"text-align:left\">$COP</td>
         </tr>";
   echo $dump;
  }
}
?>
    </tbody>
</table>
<div style="text-align: center;margin-top:10px">
   <strong>SENATE IS PRAYED TO AWARD <?=$rstinfo['Degree'] ?> DEGREE IN <?=strtoupper($rstinfo['ProgName']) ?> TO THE ABOVE STUDENT<?=$tptStud>1?"S":""?></strong> 
</div>


<?php
// {$rstinfo['Degree']} IN {$rstinfo['ProgName']}
$pdf->_HTML();
$sigarr = ["HEAD OF DEPARTMENT","DEAN","CHAIRMAN, SERVC"];
if(isset($ReportID)){
  //check if already signed
  //StudyID=5&ProgID=23&LevelID=1&SemID=1&SesID=8&ReportID=2
  $sigparam = $dbo->DataString(json_decode($sigparam,true));
  $sigs = $dbo->SelectFirstRow("reportsig_tb","","Param='".$sigparam."'");
if(!is_array($sigs))$sigs = $dbo->SelectFirstRow("report_tb","","ID=".$ReportID);
   if(is_array($sigs)){
    $sigarr = $sigs['Signatories'];
   }
}
//$pdf->Dump($sigarr);
//$pdf->Finish();
  $pdf->FooterNote("ERRS ( ".$rstinfo['ProgName']." | ".$rstinfo['SesName']." SET )",$pdf->Signataries($sigarr,"font-size:1.2em",false),array("Developer"));
  $pdf->Finish();
  exit();
  
  

  
  
  



?>