<?php
//require_once("../../../../general/TaquaLB/Elements/Elements.php");
$configdir = "../../../../../../".$_POST['SubDir'];
require_once("../../../../general/config.php");
//require("../../../../general/getinfo.php");
 
 function Error($title,$text = ""){
    echo '<div class="defaultTabText">';
    echo '<div style="color:red"';echo '<i class="fa fa-exclamation-triangle fa-3x"></i>';echo '</div>';
    echo '<div>';echo $title;echo '</div>';  
    echo '<div>';echo $text;echo '</div>';  
    echo '</div>';
  
  }
  //check priv
  AllowUser("MMS");
LoadFile("basic","../../../../");

extract($_POST);
// StudyID=5&ProgID=23&SesID=10&ClassID=1
if(!isset($StudyID)){ 
  exit("No Result Found");
}
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'];
}

//headers
$headers = ["*RegNo"=>"REG.NO","*StudName"=>"NAME","*Sex"=>"SEX"];
$ReadOnly = 'RegNo;StudName;Sex';
//get the programme details
$progDet = $dbo->SelectFirstRow("programme_tb","","ProgID=$ProgID");
$YearOfStudy = (int)$progDet['YearOfStudy'];
//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()){
    // if($YearOfStudy === $yearCounter) break;
    $headers["*Ses_".$indSes['SesID']] = $indSes['SesName'];
    $ReadOnly .= ";Ses_".$indSes['SesID'];
    $validSes[$indSes['SesID']]=$indSes;
   $yearCounter++;
  }
}

$headers["*Cumm"] = "CUMMULATIVE";
$headers["*Degree"] = "DEGREE";
$ReadOnly .= ";Cumm;Degree";


//get all student in the programm by start ses
$allStud = $dbo->Select("studentinfo_tb","","ProgID=$ProgID AND StartSes=$SesID AND ClassID=$ClassID");
$dump = [];
if(is_array($allStud) && $allStud[1] > 0){
  while($indStud = $allStud[0]->fetch_assoc()){
    $regNo = trim($indStud['RegNo']) == ''?$indStud['JambNo']:$indStud['RegNo'];
     $rec = [$regNo, strtoupper($indStud['SurName'])." ".$indStud['FirstName']." ".$indStud['OtherNames'], $indStud['Gender']];
     $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);
        }
      }
      $pass = 1;
      $succ = $pass == 0?"dangerre":"successse";
      $RstHtml .= '<strong class="Labels '.$succ.'">&sum;CH: '.$TCH."</strong>";
      $RstHtml .= '<strong class="Labels '.$succ.'">&sum;GP: '.$TGP."</strong>";
      $RstHtml .= '<strong class="Labels '.$succ.'">GPA: '.number_format($GPA,2)."</strong>";
      $rec[]=trim($GPA . '') != ''?$RstHtml:"";
    }
    if(!$rstSeen)continue;
    $RstHtmlc .= '<strong class="Labels '.$succ.'">&sum;CH: '.$CCH."</strong>";
      $RstHtmlc .= '<strong class="Labels '.$succ.'">&sum;GP: '.$CGP."</strong>";
      if($CCH > 0){
        $CGPA = number_format(round($CGP/$CCH,2),2);
      }
      $RstHtmlc .= '<strong class="Labels '.$succ.'">GPA: '.$CGPA."</strong>";
    $rec= array_merge($rec,[$RstHtmlc,$COP]);
    $dump[] = $rec;
  }
}

Box("class=ep-animate-opacity");

SpreadSheet("rowselect=false,style=width:calc(100% - 12px);margin:auto;margin-top:0px;margin-bottom:6px,id=sprstrpt,multiselect=false,readonly=$ReadOnly,dynamiccolumn=false,dynamicrow=false,minrow=-1,rowfilter=true,filtertitle=FILTER RESULT SHEET,filterstyle=width:calc(100% - 12px);margin:auto,rowdelete=false,trimtext=<strong class\=\"Labels successse\">;</strong>;<strong class\=\"Labels\">;<strong class\=\"Labels dangerre\">;<b>;</b>",$headers,$dump);
_Box();
$sch = GetSchool("SemLabel");
$semlabel = trim($sch['SemLabel']);
$rstinfoa = $dbo->RunQuery("SELECT f.FacName, d.DeptName, p.ProgName, p.YearOfStudy,s.SesName, c.Name as ClassName FROM fac_tb f, dept_tb d, programme_tb p, session_tb s, studentclass_tb c WHERE f.FacID = d.FacID AND d.DeptID = p.DeptID AND s.SesID = $SesID AND p.ProgID = $ProgID AND p.ProgID = c.ProgID AND c.ID = $ClassID LIMIT 1");
$info = "MASTER RESULT SHEET";
if(is_array($rstinfoa)){
  if($rstinfoa[1] > 0){
      $rsstii = $rstinfoa[0]->fetch_array();
      // $LevelName = LevelName($LevelID, $StudyID);
      $info .= " - ".$rsstii['SesName']." SET | ".$rsstii['ProgName']." | ".$rsstii['ClassName']." | ".$semlabel ;
  }
}
Hidden("mstinfobs",$info);
?>