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

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>Malika School</title>

    <!-- Template CSS -->
    <link rel="stylesheet" href="assets/css/style-starter.css">

    <link rel="stylesheet" href="assets/vendors/simple-datatables/style.css">
    <link rel="stylesheet" href="assets/vendors/perfect-scrollbar/perfect-scrollbar.css">
    <!-- google fonts -->
    <link href="//fonts.googleapis.com/css?family=Nunito:300,400,600,700,800,900&display=swap" rel="stylesheet">
</head>

<body class="sidebar-menu-collapsed">
    <div class="se-pre-con"></div>
    <section>
        <!-- sidebar menu start -->
        <?php
  include 'navigation.php';
  ?>
        <!-- //sidebar menu end -->
        <!-- header-starts -->
        <?php
  include 'header.php';
  ?>
        <style>
        #posInfo td {
            padding: 10px;
        }
        </style>
        <!-- //header-ends -->
        <!-- main content start -->
        <div class="main-content">

            <!-- content -->
            <div class="container-fluid content-top-gap">

                <!-- breadcrumbs -->
                <nav aria-label="breadcrumb" class="mb-4">
                    <ol class="breadcrumb my-breadcrumb">
                        <li class="breadcrumb-item"><a href="index.html">Home</a></li>
                        <!-- <li class="breadcrumb-item"><a href="#">Elements</a></li> -->
                        <li class="breadcrumb-item active" aria-current="page">View Result</span></li>
                    </ol>
                </nav>
                <!-- //breadcrumbs -->
                <?php
          if(isset($_GET['message']))
          {
            echo '<div align="center" id="status"><h4>'.$_GET['message'].'</h4></div>';
          }
        ?>
                <div id="status" align="center"></div>


                <div class="card card_border mb-5">
                    <!-- GET result By id -->
                    <div class="card-body">
                        <table width="100%">
                            <tr id="posInfo">
                                <td width="50%" align="right"><b>Sudent's Name: </b></td>
                                <td width="50%" align="left">
                                    <?php echo $getStud['firstname'].' '.$getStud['lastname'];?> </td>
                            </tr>
                            <tr id="posInfo">
                                <td width="50%" align="right"><b>Registration Number: </b></td>
                                <td width="50%" align="left"><?php echo $getStud['reg_no'];?></td>
                            </tr>
                            <tr id="posInfo">
                                <td width="50%" align="right"><b>Class Name: </b></td>
                                <td width="50%" align="left">
                                    <?php echo strtoupper($classInfo['section']).' '.$classInfo['class_no'];?> </td>
                            </tr>
                            <tr id="posInfo">
                                <td width="50%" align="right"><b>Stuents In Class: </b></td>
                                <td width="50%" align="left"><?php echo $sutentCount;?> </td>
                            </tr>
                            <tr id="posInfo">
                                <td width="50%" align="right"><b>Position In Class: </b></td>
                                <td width="50%" align="left">
                                    <?php echo posInfo($result->getResultById($result_id)['position']);?> Position </td>
                            </tr>
                            <tr id="posInfo">
                                <td width="50%" align="right"><b>Grade In Class: </b></td>
                                <td width="50%" align="left">
                                    <?php echo $result->getResultById($result_id)['grade'];?> </td>
                            </tr>
                            <tr id="posInfo">
                                <td width="50%" align="right"><b>Average In Class: </b></td>
                                <td width="50%" align="left">
                                    <?php echo $result->getResultById($result_id)['average'];?> </td>
                            </tr>
                        </table>

                        <?php
                        // echo 'Highest Average '.$result->classAverage($result->getResultById($result_id)['session_id'], $result->getResultById($result_id)['class_id'])['highest_average'].'<br>';
                        // echo 'Lowest Average '.$result->classAverage($result->getResultById($result_id)['session_id'], $result->getResultById($result_id)['class_id'])['lowest_average'].'<br>';
                        ?>
                    </div>

                </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">
                                <div class="container-fluid">
                                    <div class="row">
                                        <div class="col-sm-10">

                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="card-body">
                                <table class='table table-striped' id="table1">
                                    <thead>
                                        <tr>
                                            <th>S/N</th>
                                            <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>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                            $n=1;
                            $class_id=$classInfo['id'];
                            foreach($classSubject->getAllClassSubjects($class_id) 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>'.$n.'</td>
                                <td>'.$subject->getSubject($subj_id)['name'].'</td>
                                <td>'.(($ca1!=null)?$ca1:0).'</td>
                                <td>'.(($ca2!=null)?$ca2:0).'</td>
                                <td>'.(($exam!=null)?$exam:0).'</td>
                                <td>'.(($totalMarks!=null)?$totalMarks:0).'</td>
                                <td>'.grade((($totalMarks!=null)?$totalMarks:0))['grade'].'</td>
                                <td>'.$subjectResult['position'].'</td>
                                <td>'.$sutentCount.'</td>
                              </tr>
                              ';
                            $n++;
                            }

                            ?>

                                    </tbody>
                                </table>
                                <div align="center">
                                    <div class="row">
                                        <div class="col-6">
                                            <button type="button" class="btn btn-primary" data-toggle="modal"
                                                data-target="#reateStud">Add Remarks
                                            </button>
                                            <div class="modal fade" id="reateStud" tabindex="-1" role="dialog"
                                                aria-labelledby="reateStudTitle" aria-hidden="true">
                                                <div class="modal-dialog modal-dialog-centered" role="document">
                                                    <form method="post" id="remarksForm">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <h5 class="modal-title" id="exampleModalLongTitle">Add
                                                                    Remark</h5>
                                                                <button type="button" class="close" data-dismiss="modal"
                                                                    aria-label="Close">
                                                                    <span aria-hidden="true">&times;</span>
                                                                </button>
                                                            </div>
                                                            <div class="modal-body">
                                                                <div id="remarks_status" align="center"></div>

                                                                <div class="container">
                                                                    <table width="100%">
                                                                        <tr>
                                                                            <td width="40%">Form Teacher's Remark</td>
                                                                            <td width="60%"><textarea name="t_remark"
                                                                                    class="form-control"><?php echo $result->getRemarks($result_id)['f_teacher'];?></textarea>
                                                                                <input type="radio" name="result_id"
                                                                                    value="<?php echo $result_id;?>"
                                                                                    style="display: none;" checked />
                                                                            </td>
                                                                        </tr>
                                                                        <tr>
                                                                            <td width="40%">Head Teacher's Remark</td>
                                                                            <td width="60%"><br><textarea
                                                                                    name="ht_remark"
                                                                                    class="form-control"><?php echo $result->getRemarks($result_id)['h_teacher'];?></textarea>
                                                                            </td>
                                                                        </tr>
                                                                    </table>
                                                                </div>
                                                            </div>
                                                            <div class="modal-footer">
                                                                <button type="button" class="btn btn-danger"
                                                                    data-dismiss="modal">Close</button>
                                                                <input type="submit" name="submit"
                                                                    class="btn btn-success" value="Save changes" />
                                                            </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-6">
                                        <button type="button" class="btn btn-primary" data-toggle="modal"
                                            data-target="#studRemark">Add Rating
                                        </button>
                                        <div class="modal fade" id="studRemark" tabindex="-1" role="dialog"
                                            aria-labelledby="studRemarkTitle" aria-hidden="true">
                                            <div class="modal-dialog modal-dialog-centered" role="document">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h5 class="modal-title" id="exampleModalLongTitle">Add
                                                            Rating</h5>
                                                        <button type="button" class="close" data-dismiss="modal"
                                                            aria-label="Close">
                                                            <span aria-hidden="true">&times;</span>
                                                        </button>
                                                    </div>
                                                    <div id="rate_status" align="center"></div>
                                                    <form method="post" name="add_rate">
                                                        <div class="modal-body">
                                                            <div id="rate_success" align="center"></div>
                                                            <div class='row'>
                                                                <div class="col-sm-4 col-6">
                                                                    <span>Punctuality</span>
                                                                    <input type="number" class="form-control"
                                                                        name="puntuality"
                                                                        value="<?php echo $result->resultRate($result_id, 'puntuality')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Attendance</span>
                                                                    <input type="number" class="form-control"
                                                                        name="attendance"
                                                                        value="<?php echo $result->resultRate($result_id, 'attendance')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Reliability</span>
                                                                    <input type="number" class="form-control"
                                                                        name="reliability"
                                                                        value="<?php echo $result->resultRate($result_id, 'reliability')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Neatness</span>
                                                                    <input type="number" class="form-control"
                                                                        name="neatness"
                                                                        value="<?php echo $result->resultRate($result_id, 'neatness')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Politeness</span>
                                                                    <input type="number" class="form-control"
                                                                        name="politeness"
                                                                        value="<?php echo $result->resultRate($result_id, 'politeness')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Honesty</span>
                                                                    <input type="number" class="form-control"
                                                                        name="honesty"
                                                                        value="<?php echo $result->resultRate($result_id, 'honesty')['rate'];?>"
                                                                        required />
                                                                </div>
                                                                <div class="col-sm-4 col-6">
                                                                    <span>Staff Relationship</span>
                                                                    <input type="number" class="form-control"
                                                                        name="staff_relationship"
                                                                        value="<?php echo $result->resultRate($result_id, 'staff_relationship')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Peers Relationship</span>
                                                                    <input type="number" class="form-control"
                                                                        name="peers_relationship"
                                                                        value="<?php echo $result->resultRate($result_id, 'peers_relationship')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Self Control</span>
                                                                    <input type="number" class="form-control"
                                                                        name="self_control"
                                                                        value="<?php echo $result->resultRate($result_id, 'self_control')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Spirit Of Coopp</span>
                                                                    <input type="number" class="form-control"
                                                                        name="spirit_corpration"
                                                                        value="<?php echo $result->resultRate($result_id, 'spirit_corpration')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Willigness to learn</span>
                                                                    <input type="number" class="form-control"
                                                                        name="learn_willingness"
                                                                        value="<?php echo $result->resultRate($result_id, 'learn_willingness')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Initiative</span>
                                                                    <input type="number" class="form-control"
                                                                        name="initiative"
                                                                        value="<?php echo $result->resultRate($result_id, 'initiative')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Perseverence</span>
                                                                    <input type="number" class="form-control"
                                                                        name="perseverence"
                                                                        value="<?php echo $result->resultRate($result_id, 'perseverence')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Physchomotor</span>
                                                                    <input type="number" class="form-control"
                                                                        name="physchomotor"
                                                                        value="<?php echo $result->resultRate($result_id, 'physchomotor')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Handrwiting</span>
                                                                    <input type="number" class="form-control"
                                                                        name="handwriting"
                                                                        value="<?php echo $result->resultRate($result_id, 'handwriting')['rate'];?>"
                                                                        required />
                                                                </div>
                                                                <div class="col-sm-4 col-6">
                                                                    <span>Games</span>
                                                                    <input type="number" class="form-control"
                                                                        name="games"
                                                                        value="<?php echo $result->resultRate($result_id, 'games')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Sports</span>
                                                                    <input type="number" class="form-control"
                                                                        name="sports"
                                                                        value="<?php echo $result->resultRate($result_id, 'sports')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Drawing/Painting</span>
                                                                    <input type="number" class="form-control"
                                                                        name="drawing_paint"
                                                                        value="<?php echo $result->resultRate($result_id, 'drawing_paint')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-2 col-0"></div>
                                                                <div class="col-sm-4 col-6">
                                                                    <span>Craft</span>
                                                                    <input type="number" class="form-control"
                                                                        name="craft"
                                                                        value="<?php echo $result->resultRate($result_id, 'craft')['rate'];?>"
                                                                        required />
                                                                </div>

                                                                <div class="col-sm-4 col-6">
                                                                    <span>Musical SKills</span>
                                                                    <input type="number" class="form-control"
                                                                        name="musicals"
                                                                        value="<?php echo $result->resultRate($result_id, 'musicals')['rate'];?>"
                                                                        required />
                                                                </div>
                                                            </div>
                                                        </div>
                                                        <input type="radio" name="result_id"
                                                            value="<?php echo $result_id;?>" style="display: none;"
                                                            checked />
                                                        <div class="modal-footer">
                                                            <button type="button" class="btn btn-danger"
                                                                data-dismiss="modal">Close</button>
                                                            <button type="submit" name="submit"
                                                                class="btn btn-success">Save
                                                                changes</button>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-12"><a href="print_result.php?result_id=<?php echo $result_id;?>"
                                            class="btn btn-success">Print Result</a>
                                    </div>
                                </div>
                            </div>
                        </div>
            </div>
    </section>
    <!-- //teams1 -->

    <!-- //people cards style 2 -->

    </section>
    <!-- //people -->

    </div>
    <!-- //content -->
    </div>
    <!-- main content end-->
    </section>
    <!--footer section start-->
    <?php
include 'footer.php';
?>
    <!--footer section end-->
    <!-- move top -->
    <button onclick="topFunction()" id="movetop" class="bg-primary" title="Go to top">
        <span class="fa fa-angle-up"></span>
    </button>
    <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>

    <!-- chart js -->
    <script src="assets/js/Chart.min.js"></script>
    <script src="assets/js/utils.js"></script>
    <!-- //chart js -->

    <!-- Different scripts of charts.  Ex.Barchart, Linechart -->
    <script src="assets/js/bar.js"></script>
    <script src="assets/js/linechart.js"></script>
    <!-- //Different scripts of charts.  Ex.Barchart, Linechart -->


    <script src="assets/js/jquery.nicescroll.js"></script>
    <script src="assets/js/scripts.js"></script>
    <script src="assets/vendors/perfect-scrollbar/perfect-scrollbar.min.js"></script>
    <script src="assets/vendors/simple-datatables/simple-datatables.js"></script>
    <script src="assets/js/vendors.js"></script>

    <!-- close script -->
    <script>
    var closebtns = document.getElementsByClassName("close-grid");
    var i;

    for (i = 0; i < closebtns.length; i++) {
        closebtns[i].addEventListener("click", function() {
            this.parentElement.style.display = 'none';
        });
    }
    </script>
    <!-- //close script -->

    <!-- disable body scroll when navbar is in active -->
    <script>
    // $('input').val(3);
    $(function() {
        $('.sidebar-menu-collapsed').click(function() {
            $('body').toggleClass('noscroll');
        })
    });
    </script>
    <!-- disable body scroll when navbar is in active -->

    <!-- loading-gif Js -->
    <script src="assets/js/modernizr.js"></script>
    <script>
    $(window).load(function() {
        // Animate loader off screen
        $(".se-pre-con").fadeOut("slow");
    });
    </script>
    <!--// loading-gif Js -->

    <!-- Bootstrap Core JavaScript -->
    <script src="assets/js/bootstrap.min.js"></script>
    <!-- remarks_status -->
    <script>
    $(document).on('submit', '#remarksForm', function(e) {
        e.preventDefault();
        // alert("WORKIN");
        var formData = new FormData(this);

        $('#remarks_status').html('<h4 class="card-title">Adding</h4>');
        // alert(pres);
        $.ajax({
            url: "../control/add_remarks.php", // Url to which the request is send
            type: "POST", // Type of request to be send, called as method
            data: formData, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
            contentType: false, // The content type used when sending data to the server.
            cache: false, // To unable request pages to be cached
            processData: false, // To send DOMDocument or non processed data file it is set to false
            success: function(data) // A function to be called if request succeeds
            {
                $('#remarks_status').html('<h4 class="card-title">' + data + '</h4>');
                // alert(data);

            }
        });
    });

    // Rateing
    $(document).on('submit', 'form[name=add_rate]', function(e) {
        e.preventDefault();
        //    alert("WORKIN");
        var formData = new FormData(this);

        $('#rate_status').html('<h4 class="card-title">Adding</h4>');
        // alert(pres);
        $.ajax({
            url: "../control/add_rating.php", // Url to which the request is send
            type: "POST", // Type of request to be send, called as method
            data: formData, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
            contentType: false, // The content type used when sending data to the server.
            cache: false, // To unable request pages to be cached
            processData: false, // To send DOMDocument or non processed data file it is set to false
            success: function(data) // A function to be called if request succeeds
            {
                $('#rate_status').html('<h4 class="card-title">' + data + '</h4>');
                // alert(data);

            }
        });
    });
    </script>
</body>

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