<?php
session_start();
if(isset($_SESSION['teacher']))
{ 
  $teacher_id=$_SESSION['teacher']['id'];
  if(isset($_GET['result_id']))
  {
     
    $result_id=htmlentities($_GET['result_id']);  
    include '../manage/function.php';

    include '../classes/students_class.php';
    include '../classes/classroom_class.php';
    include '../classes/class_subjects_class.php';
    include '../classes/subjects_class.php';
    include '../classes/assessment_class.php';
    include '../classes/session_class.php';
    include '../classes/result_class.php';

    $student=new Student("../manage/connection.php");
    $classroom=new ClassRoom("../manage/connection.php");
    $classSubject=new ClassSubject("../manage/connection.php");
    $subject=new Subject("../manage/connection.php");
    $assessment=new Assessment("../manage/connection.php");
    $class_session=new Session("../manage/connection.php");
    $result=new Result("../manage/connection.php");

      $student_id=$result->getResultById($result_id)['student_id'];
      $class_id=$result->getResultById($result_id)['class_id'];
      $getStud=$student->getStudent($student_id);
      $classInfo=$classroom->getClassRoom($class_id);
      $sutentCount=$result->studentByResult($result->getResultById($result_id)['session_id'], $result->getResultById($result_id)['class_id']);
      $curr_session=$class_session->getSession($result->getResultById($result_id)['session_id']);

      
      $class_id=$classInfo['id'];
      if($classInfo['teacher_id']==$teacher_id)
    {
        $totalSubjectMarks=0;
        $allsubjects=$classSubject->getAllClassSubjects($class_id);
        
        foreach($allsubjects as $classSub)
        {
            
            $subj_id=$classSub['subject_id'];
            $stud_id=$result->getResultById($result_id)['student_id'];
            $sess_id=$result->getResultById($result_id)['session_id'];
            $subjectResult=$result->getSubjectResult($stud_id, $sess_id, $class_id,$subj_id);
            
            $ca1=$assessment->getCA1Mark($stud_id, $sess_id, $class_id, $subj_id)['marks'];
            $ca2=$assessment->getCA2Mark($stud_id, $sess_id, $class_id, $subj_id)['marks'];
            $exam=$assessment->getExamMark($stud_id, $sess_id, $class_id, $subj_id)['marks'];
            $totalSubjectMarks+=($ca1+$ca2+$exam);
        }
?>
<!doctype html>
<html lang="en">

<head>
    <title>Aziza Academy School - <?php echo $getStud['reg_no'];?></title>
</head>

<body class="sidebar-menu-collapsed">

    <style> 
    #posInfo td {
        padding: 3px;
    }

    #printResult {
        border-spacing: 0px;
        border: 1px solid black;
    }

    #printResult td,
    th {
        padding: 3px;
    }

    #kinderHead td {
        padding: 3px;
    }

    #azizaTitle {
        font-size: 35px;
        font-weight: bold;
        color: black;
    }
    </style>

<?php
$sesn_id=$result->getResultById($result_id)['session_id'];
$session_begin=$class_session->getSession($sesn_id)['begin'];
$session_end=$class_session->getSession($sesn_id)['end'];
?>
    <div class="card card_border mb-5">
        <!-- GET result By id -->
        <div class="card-body">
            <table border="0" id="kinderHead" align="center">
                <tr>
                    <td><img src="../images/aziza_logo.jpg" height="100px" width="100px" /></td>
                    <td align="center">
                        <div id="azizaTitle">AZIZA ACADEMY</div>
                        <b>NURSERY, PRIMARY, TAHFEEZ & SECONDARY</b>
                        <div><?php echo strtoupper($classInfo['section']); ?> SECTION </div>
                    </td>
                    <td><!--<img src="../images/<?php echo $getStud['image'];?>" height="80px" width="80px" />--></td>
                </tr>
            </table>
            <br>
            <table width="100%" border="0" style="border: 0px solid black;">
            <tr id="posInfo">
                    <td style="width: 1%; white-space: nowrap;"><b>Sudent's Name: </b></td>
                    <td><?php echo $getStud['firstname'].' '.$getStud['lastname'];?> </td>
                    <td style="width: 1%; white-space: nowrap;"><b>Registration Number: </b></td>
                    <td><?php echo $getStud['reg_no'];?></td>
                    <td style="width: 1%; white-space: nowrap;"><b>
                    <?php
                        // if(strtoupper($classInfo['section'])!="NURSERY" && strtoupper($classInfo['section'])!="pre-nursery")
                        // {
                        //     echo 'Position: ';
                        // }
                        // else
                        // {
                        //     echo 'Grade: ';
                        // }
                        echo 'Position: '
                    ?>    
                    </b></td>
                    <td><?php
                        // if(strtoupper($classInfo['section'])=="NURSERY" || strtoupper($classInfo['section'])=="PRE NURSERY")
                        // {
                        //     echo grade(($totalSubjectMarks)/count($allsubjects))['grade'];
                        // }
                        // else
                        // {
                        //     echo $result->getResultById($result_id)['grade'].'';
                        // }
                        echo  $result->getResultById($result_id)['grade'].'';
                    ?>  </td>
                </tr>
                <tr id="posInfo">
                    <td style="width: 1%; white-space: nowrap;"><b>Class: </b></td>
                    <td>
                        <?php echo strtoupper($classInfo['section']).' '.$classInfo['class_no'];?> </td>
                    <td style="width: 1%; white-space: nowrap;"><b>No. In Class: </b></td>
                    <td><?php echo $sutentCount;?> </td>
                    <td style="width: 1%; white-space: nowrap;"><b>Term: </b></td>
                    <td><?php 
                        if($curr_session['term']==1)
                        {
                            echo $curr_session['term']."<sup>st</sup>";
                        }
                        elseif($curr_session['term']==2)
                        {
                          echo $curr_session['term']."<sup>nd</sup>";
                        }
                        else
                        {
                          echo $curr_session['term']."<sup>rd</sup>";
                        }
                    ?> Term</td>
                </tr>
                <tr id="posInfo">
                    
                    <td style="width: 1%; white-space: nowrap;"><b>Class Average: </b></td>
                    <td><?php echo $result->getResultById($result_id)['average'];?> </td>
                    <td style="width: 1%; white-space: nowrap;"><b>Highest Ave.: </b></td>
                    <td>
                    <?php echo $result->classAverage($result->getResultById($result_id)['session_id'], $result->getResultById($result_id)['class_id'])['highest_average'];?>
                    </td>
                    <td style="width: 1%; white-space: nowrap;"><b>Lowest Ave. </b></td>
                    <td><?php echo $result->classAverage($result->getResultById($result_id)['session_id'], $result->getResultById($result_id)['class_id'])['lowest_average'];?> </td>
                     
                </tr>
            </table>

            <!--<div align="center">
            <br>
                <b>Attendance</b>
            </div>
            <table width="100%" border="0">
                <tr id="posInfo">
                    <td style="width: 1%; white-space: nowrap;"><b>Days of School Open: </b></td>
                    <td>
                    <?php echo daysInSchool($session_begin,$session_end);?>
                    </td>
                    <td style="width: 1%; white-space: nowrap;"><b>Day(s) Present: </b></td>
                    <td><?php echo $student->studentAttendance($getStud['id'],$sesn_id)['present'];?> </td>
                    <td style="width: 1%; white-space: nowrap;"><b>Days Absent:</b></td>
                    <td><?php echo (daysInSchool($session_begin,$session_end)-$student->studentAttendance($getStud['id'],$sesn_id)['present']);?></td>
                </tr>
            </table>-->
            <div align="center"><h2>ACADEMIC RECORD</h2></div>
        </div>
        <br> 

    </div>
    <table id="printResult" width="100%" border="1">
        <thead>
            <tr>
                <th>Subject Name</th>
                <th>CA</th>
                <!-- <th>CA 2</th> -->
                <th>Exam</th>
                <th>Total</th>
                <th>Grade</th>
                <!--<th>Position</th>
                <th>Out Of</th>-->
                <th>Comment</th>
            </tr>
        </thead>
        <tbody>
            <?php
                            $n=1;
                            foreach($allsubjects as $classSub)
                            {
                              $subj_id=$classSub['subject_id'];
                              $stud_id=$result->getResultById($result_id)['student_id'];
                              $sess_id=$result->getResultById($result_id)['session_id'];
                              $subjectResult=$result->getSubjectResult($stud_id, $sess_id, $class_id,$subj_id);
                              
                              $ca1=$assessment->getCA1Mark($stud_id, $sess_id, $class_id, $subj_id)['marks'];
                              $ca2=$assessment->getCA2Mark($stud_id, $sess_id, $class_id, $subj_id)['marks'];
                              $exam=$assessment->getExamMark($stud_id, $sess_id, $class_id, $subj_id)['marks'];
                              $totalMarks=($ca1+$ca2+$exam);
                              echo '
                              <tr> 
                                <td>'.$subject->getSubject($subj_id)['name'].'</td>
                                <td align="center">'.((($ca1!=null)?$ca1:0)+(($ca2!=null)?$ca2:0)).'</td>
                                <!--<td align="center">'.(($ca2!=null)?$ca2:0).'</td>-->
                                <td align="center">'.(($exam!=null)?$exam:0).'</td>
                                <td align="center">'.(($totalMarks!=null)?$totalMarks:0).'</td>
                                <td align="center">'.grade((($totalMarks!=null)?$totalMarks:0))['grade'].'</td>
                                <!--<td align="center">'.posInfo($subjectResult['position']).'</td>
                                <td align="center">'.$sutentCount.'</td>-->
                                <td align="center">'.grade((($totalMarks!=null)?$totalMarks:0))['comment'].'</td>
                              </tr>
                              ';
                            $n++;
                            }

                            ?>

        </tbody>
    </table>
<br>
    <b>Grade Details:</b>
    <div>A = 70 - 100, B = 60 - 69, C = 50 - 59, D = 45 - 49, E = 40-44, F = 0 - 39</div>
    <!-- <br><br> -->
    <table width="100%">
        <td width="33%">
            <table width="100%" border="1" id="printResult">
                <tr>
                    <th width="90%">Development Assessment</th>
                    <th>Rate</th>
                </tr>
                <?php
            $n=0;
            foreach($result->allResultRate($result_id) as $rate)
            {
                if($n<6)
                {
                    echo '<tr>
                    <td width="90%">'.$rate['title'].'</td>
                    <td>'.$rate['rate'].'</td>
                </tr>';
                }
                $n++;
            }
            ?>
            </table>
        </td>

        <td width="33%">
            <table width="100%" border="1" id="printResult">

                <?php
            $n=0;
            foreach($result->allResultRate($result_id) as $rate)
            {
                if($n>6 && $n<14)
                {
                    echo '<tr>
                    <td width="90%">'.$rate['title'].'</td>
                    <td>'.$rate['rate'].'</td>
                </tr>';
                }
                $n++;
            }
            ?>
            </table>
        </td>

        <td width="33%">
            <table width="100%" border="1" id="printResult">
                <?php
            $n=0;
            foreach($result->allResultRate($result_id) as $rate)
            {
                if($n>=14 && $n<=21)
                {
                    echo '<tr>
                    <td width="90%">'.$rate['title'].'</td>
                    <td>'.$rate['rate'].'</td>
                </tr>';
                }
                $n++;
            }
            ?>
            <tr>
                    <td width="90%"> - </td>
                    <td> </td>
                </tr>
            </table>
        </td>
    </table>

    <?php
    $htRem='';
    $tcRem='';
    if(strtoupper($result->getResultById($result_id)['grade'])=='A' || strtoupper($result->getResultById($result_id)['grade'])=='A1' ||strtoupper($result->getResultById($result_id)['grade'])=='A2' ||strtoupper($result->getResultById($result_id)['grade'])=='A3')
    {
    $htRem='An excellent performance, keep it up.';
    $tcRem='An excellent performance, keep it up';
    }
    else if(strtoupper($result->getResultById($result_id)['grade'])=='B' || strtoupper($result->getResultById($result_id)['grade'])=='B1' ||strtoupper($result->getResultById($result_id)['grade'])=='B2' ||strtoupper($result->getResultById($result_id)['grade'])=='B3')
    {
    $htRem='A very good performance, keep trying';
    $tcRem='very good performance, keep it up';
    }
    else if(strtoupper($result->getResultById($result_id)['grade'])=='C' || strtoupper($result->getResultById($result_id)['grade'])=='C1' ||strtoupper($result->getResultById($result_id)['grade'])=='C2' ||strtoupper($result->getResultById($result_id)['grade'])=='C3')
    {
    $htRem='You can do better next term';
    $tcRem='A good performance, put more effort';
    }
    else 
    {
    $htRem='An average performance, keep trying.';
    $tcRem='An average performance,you need to work harder.'; 
    }
    ?>
    <br> 
    <table width="100%" border="0">
        <tr id="posInfo">
            <td style="width: 1%; white-space: nowrap;"><b>CLASS TEACHER'S REMARK: </b></td>
            <td>
                <font size="3px"><i><?php echo $tcRem; ?></i></font>
                <!--<?php echo $result->getRemarks($result_id)['f_teacher'];?>-->
                </td>
        </tr>
        <tr id="posInfo">
            <td style="width: 1%; white-space: nowrap;"><b>HEAD TEACHER'S REMARK: </b></td>
            <td>
                
                <font size="3px"><i><?php echo $htRem; ?></i></font>
                <!--<?php echo $result->getRemarks($result_id)['h_teacher'];?>-->
            </td>
        </tr>
        <tr id="posInfo">
            <td style="width: 1%; white-space: nowrap;"><b>NEXT TERM BEGINS: </b></td>
            <td><?php 
            $session_end=$class_session->getSession($result->getResultById($result_id)['session_id'])['end'];
            echo date('d/m/Y', strtotime($result->nextTerm($session_end))); ?> </td>
        </tr>
    </table>

    <script>
    // When the user scrolls down 20px from the top of the document, show the button
    window.onscroll = function() {
        scrollFunction()
    };

    function scrollFunction() {
        if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
            document.getElementById("movetop").style.display = "block";
        } else {
            document.getElementById("movetop").style.display = "none";
        }
    }

    // When the user clicks on the button, scroll to the top of the document
    function topFunction() {
        document.body.scrollTop = 0;
        document.documentElement.scrollTop = 0;
    }
    </script>
    <!-- /move top -->


    <script src="assets/js/jquery-3.3.1.min.js"></script>
    <script src="assets/js/jquery-1.10.2.min.js"></script>
    <script>
     window.print();
     setTimeout(function(){
         window.location="view_result.php?result_id=<?php echo $result_id;?>";
      },2000);
    </script>
</body>

</html>
<?php
    }
    else
    {
      header('location:assessments.php');
    }
  }
  else
  {
    header("location:classes.php");
  }
}
else
{
    header("location:../index.php");
}
?>