<?php

?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <?php
    include 'includes/styles.php';
    $conn = new Functions();
    ?>
    <title> Test Page 2 |  <?php echo $school_name; ?></title>
</head>
<body style="color: rgb(0,0,0)">

<section class="content">
    <div class="content-fluid">
        <div class="row">
            <section class="col-lg-12 connectedSortable">
                <?php
                $class = 'Year 7';//class ID
                $section = 'Lily';//section ID
                $exam = '3rd term';//exam term
                $exam_year = 2021; //exam year
                $student_name = "Obagho David";

                //fetch section name and class name

                $sql = "SELECT name FROM sections WHERE id = :id";
                $conn->query($sql);
                $conn->bind(":id", $section);
                $db_section = $conn->fetchColumn();

                //fetch class names
                $sql = "SELECT name FROM classes WHERE id = :id";
                $conn->query($sql);
                $conn->bind(":id", $class);
                $db_class = $conn->fetchColumn();

                $count = 1;
                $value = '';
                ?>
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title"> Manage Teachers </h3>
                    </div>
                    <!-- /.card-header -->
                    <div class="card-body">
                        <div class='col-md-12 jumbotron'>
                            <h3 class='text-center'>Score Details </h3>
                            <?php
                            //fetch scores
                            $sql = "SELECT * FROM student_class_positions WHERE class = :class 
                                            AND section =:section 
                                         AND exam =:exam AND exam_year =:exam_year ORDER BY name ASC";
                            $conn->query($sql);
                            $conn->bind(":class", $class);
                            $conn->bind(":section", $section);
                            $conn->bind(":exam", $exam);
                            $conn->bind(":exam_year", $exam_year);
                            // $conn->bind(":name", $name);

                            $scores = $conn->fetchMultiple();
                            $all_scores = [];
                            $row_count = $conn->rowCount();
                             $i = 1;
                            foreach ($scores as $details){
                                //$all_scores = $details->marks_obtained;
                                $all_scores[] = $details->marks_obtained;
                            }
                            rsort($all_scores);
                            $rank=1;
                            $prev=$all_scores[0];
                            foreach($all_scores as $number){
                                if($prev!=$number){
                                    $prev=$number;
                                    $rank++;
                                }
                                $ones = $rank % 10;
                                $tens = floor($rank / 10) % 10;
                                if ($tens == 1) {
                                    $suff = "th";
                                } else {
                                    switch ($ones) {
                                        case 1 : $suff = "st"; break;
                                        case 2 : $suff = "nd"; break;
                                        case 3 : $suff = "rd"; break;
                                        default : $suff = "th";
                                    }
                                }

//                                echo $number."=>Rank -".$rank. $suff . "<br>";
                                $sql = "UPDATE student_class_positions SET  position =:position 
                       WHERE  class =:class AND exam =:exam
                        AND section =:section  AND 
                          exam_year =:exam_year AND marks_obtained =:marks_obtained";
                                $conn->query($sql);
                                $conn->bind(":class", $class);
                                $conn->bind(":section", $section);
                                $conn->bind(":exam", $exam);
                                $conn->bind(":exam_year", $exam_year);
                                $conn->bind(":position", $rank.$suff);
                                $conn->bind(":marks_obtained", $number);

                                $execute = $conn->execute();
                                if($execute){
                                    echo "Successful";
                                }


                            }

                            ?>

                        </div>
                        <!-- /.card-body -->
                    </div>

            </section>
        </div>
    </div>
</section>

</body>
</html>
