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

    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");
    
    if($classroom->getClassRoom($class_id)['teacher_id']==$teacher_id)
    {
?>
<!doctype html>
<html lang="en">

<head>

    <title>Aziza Academy - <?php echo strtoupper($classroom->getClassRoom($class_id)['section']).' '.$classroom->getClassRoom($class_id)['class_no'].' - ';
    if($class_session->getSession($session_id)['term']==1)
    {
      
      echo $class_session->getSession($session_id)['term'].'st term ';
    }
    elseif($class_session->getSession($session_id)['term']==2)
    {
      
      echo $class_session->getSession($session_id)['term'].'nd term ';
    }
    else 
    {
      
      echo $class_session->getSession($session_id)['term'].'rd term ';
    } 
    echo ' - '.$class_session->getSession($session_id)['year'].'';
     
    ?>
   - Student's Result
  </title>

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

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

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

    #kinderHead td {
        padding: 10px;
    }

    #kinderTitle {
        font-size: 35px;
        font-weight: bold;
        color: blue;
    }
/* NEw Css */
#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: blue;
    }
    /* Print per page */
    @media print
    {
        p
        {
            page-break-after: always;
        }
    }

    </style>


</head>

<body class="sidebar-menu-collapsed">
    <!-- main content start -->
    <div id="status" align="center"></div>
    <!-- people -->
    <section class="people">
        <section class="w3l-team-block">
            <!-- //people cards style 1 -->
            <div class="card card_border mb-5">
                <div class="cards__heading">
                <?php
                     $n=1;
                     $studentsCount=count($result->getAllResult($class_id, $session_id));
                     foreach($result->getAllResult($class_id, $session_id) as $resultInfo)
                     {            
                        $sesn_id=$result->getResultById($resultInfo['id'])['session_id'];
                        $session_begin=$class_session->getSession($session_id)['begin'];
                        $session_end=$class_session->getSession($session_id)['end'];
                        $result_id=$resultInfo['id'];
                        // Getting all Subjects for class
                        $allsubjects=$classSubject->getAllClassSubjects($class_id);
                         ?>
                         <!-- p was here -->
                         <table border="0" id="kinderHead" align="center">
                            <tr>
                                <td><img src="../images/aziza_logo.jpg" height="80px" width="80px" /></td>
                                <td align="center">
                                    <div id="azizaTitle">AZIZA ACADEMY</div>
                                <b>NURSERY, PRIMARY, TAHFEEZ & SECONDARY</b>
                                <div><?php echo strtoupper($classroom->getClassRoom($class_id)['section']); ?> SECTION </div>
                                </td>
                                <td><img src="../images/<?php echo $student->getStudent($resultInfo['student_id'])['image'];?>" height="80px" width="80px" /></td>
                            </tr>
                        </table>
                        <br>
            <!-- Student Info -->
                         <table width="100%" border="0" style="border: 1px solid black;">
                            <tr id="posInfo">
                                    <td style="width: 1%; white-space: nowrap;"><b>Sudent's Name: </b></td>
                                    <td><?php echo $student->getStudent($resultInfo['student_id'])['firstname'].' '.$student->getStudent($resultInfo['student_id'])['lastname'];?></td>
                                    <td style="width: 1%; white-space: nowrap;"><b>Registration Number: </b></td>
                                    <td><?php echo $student->getStudent($resultInfo['student_id'])['reg_no'];?></td>
                                    <td style="width: 1%; white-space: nowrap;"><b>
                                    Position   
                                    </b></td>
                                    <td>
                                        <?php echo posInfo($resultInfo['grade']); ?>
                                        </td>
                                </tr>
                                <tr id="posInfo">
                                    <td style="width: 1%; white-space: nowrap;"><b>Class: </b></td>
                                    <td>
                                        <?php echo strtoupper($classroom->getClassRoom($class_id)['section']).' '.$classroom->getClassRoom($class_id)['class_no'];?></b>
                                    </td>
                                    <td style="width: 1%; white-space: nowrap;"><b>No. In Class: </b></td>
                                    <td><?php echo $studentsCount;?> </td>
                                    <td style="width: 1%; white-space: nowrap;"><b>Term: </b></td>
                                    <td>
                                    <?php
                                    if($class_session->getSession($session_id)['term']==1)
                                    {
                                    
                                    echo $class_session->getSession($session_id)['term'].'st term ';
                                    }
                                    elseif($class_session->getSession($session_id)['term']==2)
                                    {
                                    
                                    echo $class_session->getSession($session_id)['term'].'nd term ';
                                    }
                                    else 
                                    {
                                    
                                    echo $class_session->getSession($session_id)['term'].'rd term ';
                                    } 
                                    // echo ' '.$class_session->getSession($session_id)['year'].'';
                                
                                ?>  
                                 </td>
                                </tr>
                                <tr id="posInfo">
                                    
                                    <td style="width: 1%; white-space: nowrap;"><b>Class Average: </b></td>
                                    <td><?php echo $result->getResultById($resultInfo['id'])['average'];?></td>
                                    <td style="width: 1%; white-space: nowrap;"><b>Highest Ave.: </b></td>
                                    <td>
                                    <?php echo $result->classAverage($result->getResultById($resultInfo['id'])['session_id'], $result->getResultById($resultInfo['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($resultInfo['id'])['session_id'], $result->getResultById($resultInfo['id'])['class_id'])['lowest_average'];?></td>
                                    
                                </tr>
                            </table>
                            <!-- Attendance Section -->
                            <!-- <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($resultInfo['student_id'],$session_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($resultInfo['student_id'],$session_id)['present']);?></td>
                                </tr>
                            </table> -->

                            
                            <div align="center"><h2>ACADEMIC RECORD</h2></div>
                            <br/>
                            <!-- Result Section -->
                            <table id="printResult" width="100%" border="1">
                                <thead>
                                    <tr>
                                        <th>Subject Name</th>
                                        <th>CA 1</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
                                         
                                         
                                        // Getting All Result
                                        $n=1;
                                        $totalCaOne=0;
                                        $totalCaTwo=0;
                                        $totalExam=0;
                                        $totalScore=0;
                                        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);
                                        
                                        
                                        // echo 'STUDENT ID -'.$stud_id.' Sess '.$sess_id.' Class '.$class_id.' SUBJ '.$subj_id.'<br>';
                                        
                                        $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++;
                                        
                                        $totalCaOne+=$ca1;
                                        $totalCaTwo+=$ca2;
                                        $totalExam+=$exam;
                                        $totalScore+=$totalMarks;
                                        }

                                        echo ' <tr> 
                                                <td> </td>
                                                <td align="center">'.$totalCaOne.'</td>
                                                <td align="center">'.$totalCaTwo.'</td>
                                                <td align="center">'.$totalExam.'</td>
                                                <td align="center">'.$totalScore.'</td>
                                                <td align="center"> </td>
                                                <!--<td align="center"> </td>
                                                <td align="center"> </td>-->
                                                <td align="center"></td>
                                            </tr>';
                                    ?>

                                </tbody>
                            </table>
                            <br>
                            <!-- Grades -->
                            <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>

                        <?php
                        echo '<p></p>';
                        }
                        ?> 
                        <div class="container-fluid">
                            <div class="row">
                                <div align="center">
                                    <!--  -->
                                </div>
                            </div>
                        </div>
                </div>
                <div class="card-body">
                     <!-- Table -->
                </div>
                <!-- //teams1 -->

                <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>
 

    <!-- close script -->
       
    <script>
    $(window).load(function() {
      window.print();
      setTimeout(function()
      {
        window.location="results.php?class_id=<?php echo $class_id;?>&session_id=<?php echo $session_id;?>";
      },1000);
    });
    </script>
    <!--// loading-gif Js -->
    
    <script src="assets/js/bootstrap.min.js"></script>



</body>

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