<?php  
include ('../includes/connection.php');
include ('sms.php');
$ex_id=$_GET['Id_2'];
$getGradePoint="";


if(isset($_GET['id_A'])) {  
$id_class= $_GET['id_A'];
 // Fetch distinct students (limit 10)

$exam= mysqli_query($conn,"SELECT * FROM exam_srms,class_srms where exam_srms.class_id=class_srms.class_id AND exam_srms.class_id='$id_class' AND exam_srms.exam_id='$ex_id'")or die(mysqli_error());
$exam_1 = mysqli_fetch_array($exam);
$exam_name_1=$exam_1['exam_name']; 
$class_name1=$exam_1['nick']; 


if($class_name1=="PCM"){
    // $display_subjects = ['HISTORIA YA TANZANIA NA MAADILI','ACADEMIC COMMUNICATION','CIVICS / GENERAL STUDY', 'PHYSICS', 'CHEMISTRY', 'ADVANCED MATHEMATICS']; // Display in table
$display_subjects = [ 'CIVICS / GENERAL STUDY', 'PHYSICS', 'CHEMISTRY', 'ADVANCED MATHEMATICS']; // Display in table
$scoring_subjects = ['PHYSICS', 'CHEMISTRY', 'ADVANCED MATHEMATICS']; // Used for division calculation only                                
 }else{
    //  $display_subjects = ['HISTORIA YA TANZANIA NA MAADILI','ACADEMIC COMMUNICATION','CIVICS / GENERAL STUDY','BASIC APPLIED MATHEMATICS', 'PHYSICS', 'CHEMISTRY','BIOLOGY']; // Display in table
$display_subjects = [ 'CIVICS / GENERAL STUDY','BASIC APPLIED MATHEMATICS', 'PHYSICS', 'CHEMISTRY','BIOLOGY']; // Display in table
 $scoring_subjects = ['PHYSICS', 'CHEMISTRY', 'BIOLOGY']; // Used for division 
 
 } 

$students_sql = "SELECT DISTINCT student_srms.student_roll_no,student_srms.student_name,student_srms.p_mobile,student_srms.student_status FROM student_srms,marks_srms WHERE marks_srms.student_roll_no=student_srms.student_roll_no AND marks_srms.class_id='$id_class' AND marks_srms.exam_id='$ex_id' AND student_srms.student_status='Enable'";
 $students_result = $conn->query($students_sql);

 
// 5. Loop students
 while ($student = $students_result->fetch_assoc()) {
      $student_id = $student['student_roll_no'];
      $student_name = $student['student_name']; 
      $p_mobile = $student['p_mobile'];

    // Initialize subject data
    $subject_data = array_fill_keys($display_subjects, ['marks' => '-', 'grade' => '-', 'point' => 0]);
    $total_point = 0;
    $total_marks = 0;

    // Get subject data for student
    $subject_list = "'" . implode("','", $display_subjects) . "'";
    $sql = "SELECT general_subject.subject_name, marks_srms.grade, marks_srms.marks, marks_srms.point FROM marks_srms,general_subject WHERE marks_srms.subject_id=general_subject.sub_id AND marks_srms.student_roll_no = '$student_id' AND marks_srms.class_id='$id_class' AND marks_srms.exam_id='$ex_id' AND general_subject.subject_name IN ($subject_list) ORDER BY marks_srms.marks DESC";
    $result = $conn->query($sql);

    while ($row = $result->fetch_assoc()) {
        $sub = $row['subject_name'];
        $subject_data[$sub] = [
            'marks' => $row['marks'],
            'grade' => $row['grade'],
            'point' => $row['point']
        ];
        if (in_array($sub, $scoring_subjects)) {
            $total_point += (int)$row['point'];
            $total_marks += (int)$row['marks'];
        }
    }
 
     if ($total_point >= 3 && $total_point <= 9) {
        $division = "I";
        } elseif ($total_point >= 10 && $total_point <= 12) {
            $division = "II";
        } elseif ($total_point >= 13 && $total_point <= 17) {
            $division = "III";
        } elseif ($total_point >= 18 && $total_point <= 19) {
            $division = "IV";
        } else {
            $division = "0";
        }

    // Display row
    $line = "Ndugu mzazi wa $student_name , Matokeo ya $student_name katika mtihani wa $exam_name_1 ni:"; 
    foreach ($display_subjects as $subject) {
        $s = $subject_data[$subject]['marks'];
        $g = $subject_data[$subject]['grade'];
        $line .= "$subject: $s ($g) ";
    }
    $line .= "Jumla ya MARKS $total_marks Amepata DIVISION : $division ya POINT $total_point  ";
    $report .= $line;
    
    $s1_mobile=$p_mobile;
    $s2_mobile = substr($s1_mobile, 1);
    $s_mobile = "255$s2_mobile"; 
    send_message($s_mobile,$line); 
    
    // echo ($line);
    
  }
 
  
}

if(isset($_GET['id_c']))
{ 
 
$id_class1= $_GET['id_c'];
 // Fetch distinct students (limit 10)
$exam= mysqli_query($conn,"SELECT * FROM exam_srms where class_id='$id_class1' AND exam_id='$ex_id'")or die(mysqli_error());
$exam_1 = mysqli_fetch_array($exam);
$exam_name_1=$exam_1['exam_name'];  
 
// Get distinct student names
$students_query ="SELECT DISTINCT student_srms.student_roll_no,student_srms.student_name,student_srms.p_mobile,student_srms.student_status FROM student_srms,marks_srms WHERE marks_srms.student_roll_no=student_srms.student_roll_no AND marks_srms.class_id='$id_class1' AND marks_srms.exam_id='$ex_id' AND student_srms.student_status='Enable' ";
$students_result = $conn->query($students_query);

while ($student = $students_result->fetch_assoc()) { 
    $student_roll_no = $student['student_roll_no'];
    $student_name = $student['student_name'];
    $p_mobile = $student['p_mobile'];

    $mark_query ="SELECT marks_srms.subject_id,general_subject.sub_id,general_subject.subject_name, marks_srms.grade, marks_srms.marks, marks_srms.point,marks_srms.student_roll_no,marks_srms.class_id,marks_srms.exam_id FROM marks_srms,general_subject WHERE marks_srms.subject_id=general_subject.sub_id AND marks_srms.student_roll_no = '$student_roll_no' AND marks_srms.class_id='$id_class1' AND marks_srms.exam_id='$ex_id' LIMIT 9";
    $data = $conn->query($mark_query);

    $subjects = [];
    $grades = [];
    $total_marks = 0; 


    while ($row = $data->fetch_assoc()) {
        $subject = strtoupper($row['subject_name']);
        $mark = $row['marks'];
        $total_marks += $row['marks'];
        $grade_letter = $row['grade'];
        $grade_point = $row['point'];

        $subjects[$subject] = [
            'marks' => $mark,
            'grade' => $grade_letter,
            'point' => $grade_point
        ];
        $grades[$subject] = $grade_point;
    } 
    // Get best 7 subjects based on grade point
    asort($grades);
    $best3 = array_slice($grades, 0, 7);
    $total_gp = array_sum($best3); 

    if ($total_gp >= 7 && $total_gp <= 17) {
      $division = "I";
      } elseif ($total_gp >= 17 && $total_gp <= 21) {
          $division = "II";
      } elseif ($total_gp >= 22 && $total_gp <= 25) {
          $division = "III";
      } elseif ($total_gp >= 26 && $total_gp <= 33) {
          $division = "IV";
      } else {
          $division = "0";
      } 
    // Display one-line report
       $line = "Ndugu mzazi wa $student_name , Matokeo ya $student_name katika mtihani wa $exam_name_1 ni:";
         foreach ($subjects as $sub => $mark) { 
           $line .= "$sub: {$mark['marks']} ({$mark['grade']}) ";
         } 
    
        $line .= "Jumla ya MARKS $total_marks Amepata DIVISION : $division ya POINT $total_gp\n";
        $report .= $line;
 
        $s1_mobile=$p_mobile;
        $s2_mobile = substr($s1_mobile, 1);
        $s_mobile = "255$s2_mobile"; 
        send_message($s_mobile,$line);
        
        //  echo ($line);
 
     }    
} 



if (isset($_POST['add_marks'])){
$student_roll_no = $_POST['student_roll_no']; 
$class_id = $_POST['class_id']; 
$exam_id = $_POST['exam_id']; 
$subject_id = $_POST['subject_id']; 
$marks = $_POST['marks'];
$year_of_study = $_POST['year_of_study'];  
$year=date("Y");  
$result_added_by = $_POST['result_added_by'];
$teacher_roll_no = $_POST['teacher_roll_no']; 
 
  
$sub_identity= mysqli_query($conn,"SELECT identity FROM general_subject where sub_id='$subject_id'")or die(mysqli_error());
$sub_i = mysqli_fetch_array($sub_identity);
$check_sub=$sub_i['identity'];

$Level= mysqli_query($conn,"SELECT * FROM class_srms where class_id='$class_id'")or die(mysqli_error());
$level_class = mysqli_fetch_array($Level);
$check_level=$level_class['level'];

if ($check_level=="O-level") {
if ($marks>= 75) {
        $T_point=1; 
      }elseif ($marks>= 65) {
        $T_point=2; 
      }elseif ($marks>= 45) {
        $T_point=3; 
      }elseif ($marks>= 30) {
        $T_point=4; 
      }else{
        $T_point=5; 
      }   
if ($marks>= 75) {
        $grade="A"; 
      }elseif ($marks>= 65) {
        $grade="B"; 
      }elseif ($marks>= 45) {
        $grade="C"; 
      }elseif ($marks>= 30) {
        $grade="D"; 
      }else{
        $grade="F"; 
}
 if ($marks>= 75) {
        $pass_marks="EXCELLENT"; 
      }elseif ($marks>= 65) {
        $pass_marks="VERY GOOD"; 
      }elseif ($marks>= 45) {
        $pass_marks="GOOD"; 
      }elseif ($marks>= 30) {
        $pass_marks="AVERAGE"; 
      }else{
        $pass_marks="FAIL"; 
 }
}else{
if ($marks>= 80) {
        $T_point=1; 
      }elseif ($marks>= 70) {
        $T_point=2; 
      }elseif ($marks>= 60) {
        $T_point=3; 
      }elseif ($marks>= 50) {
        $T_point=4; 
      }elseif ($marks>= 40) {
        $T_point=5; 
      }elseif ($marks>= 35) {
        $T_point=6; 
      }else{
        $T_point=7; 
      }   
if ($marks>= 80) {
        $grade="A"; 
      }elseif ($marks>= 70) {
        $grade="B"; 
      }elseif ($marks>= 60) {
        $grade="C"; 
      }elseif ($marks>= 50) {
        $grade="D"; 
      }elseif ($marks>= 40) {
        $grade="E"; 
      }elseif ($marks>= 35) {
        $grade="S"; 
      }else{
        $grade="F"; 
}
 if ($marks>= 80) {
        $pass_marks="EXCELLENT"; 
      }elseif ($marks>= 70) {
        $pass_marks="VERY GOOD"; 
      }elseif ($marks>= 60) {
        $pass_marks="GOOD"; 
      }elseif ($marks>= 50) {
        $pass_marks="AVERAGE"; 
      }elseif ($marks>= 40) {
        $pass_marks="SARTISFACTORY"; 
      }elseif ($marks>= 35) {
        $pass_marks="SUBSIDIARY"; 
      }else{
        $pass_marks="FAIL"; 
 }
}

$student= mysqli_query($conn,"SELECT subject_id FROM marks_srms WHERE exam_id='$exam_id' AND class_id='$class_id' and subject_id='$subject_id' and student_roll_no='$student_roll_no'")or die(mysqli_error());
$row1 = mysqli_fetch_array($student);
$check=$row1['subject_id'];

if ($check!=$subject_id) {

$query=mysqli_query($conn," INSERT INTO `marks_srms`(`teacher_roll_no`, `class_id`, `exam_id`, `grade`, `pass_marks`, `subject_id`, `student_roll_no`, `marks`,`identity_sub`, `point`, `result_added_by`, `year_of_study`, `Year`) VALUES ('$teacher_roll_no', '$class_id','$exam_id','$grade','$pass_marks','$subject_id','$student_roll_no','$marks','$check_sub','$T_point','$result_added_by','$year_of_study','$year')"); 
  if($query){  
         ?>
         <script type="text/javascript">
             alert("Result Success Fully Added");
         </script>
        <?php  
      }  
      else{
        ?>  
        <script type="text/javascript">
         alert("Result not Success please try again");
        </script>
        <?php
    }
    }else{
      ?>
         <script type="text/javascript">
             alert("Sorry Result Already added");
         </script>
    <?php 
  } 
  
}

if(isset($_GET['del']))
{
  
$id= $_GET['del'];
$q =mysqli_query($conn,"DELETE FROM `marks_srms` WHERE student_roll_no = '$id' AND exam_id='$ex_id'");
// header('location:admin.php?page=fgtutrs345gj789km231£xamUIYTR7ttdeewz34&Id_2='.$ex_id.'');
}

if(isset($_POST['update_r']))
{ 
$year_name = $_POST['year_name'];
$id = "1";
$sql =mysqli_query($conn,"update year_srms set year_name='$year_name' WHERE id='$id'");
header('location:admin.php?page=february"');
}

$query1= mysqli_query($conn,"SELECT year_name FROM `year_srms`")or die(mysqli_error());
$update_year = mysqli_fetch_array($query1);
$year_name=$update_year['year_name'];
 
?> 
 <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/jpg" href="../assets/img/mzumbe1.jpg">
  <title>SARMS | SIKIRARI ACADEMIC RESURT MANAGEMENT SYSTEM</title> 
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="../stp_con/fontawesome-free/css/all.min.css">
  <!-- daterange picker -->
  <link rel="stylesheet" href="../stp_con/daterangepicker/daterangepicker.css">
  <!-- iCheck for checkboxes and radio inputs -->
  <link rel="stylesheet" href="../stp_con/icheck-bootstrap/icheck-bootstrap.min.css">
  <!-- Bootstrap Color Picker -->
  <link rel="stylesheet" href="../stp_con/bootstrap-colorpicker/css/bootstrap-colorpicker.min.css">
  <!-- Tempusdominus Bootstrap 4 -->
  <link rel="stylesheet" href="../stp_con/tempusdominus-bootstrap-4/css/tempusdominus-bootstrap-4.min.css">
  <!-- Select2 -->
  <link rel="stylesheet" href="../stp_con/select2/css/select2.min.css">
  <link rel="stylesheet" href="stp_con/select2-bootstrap4-theme/select2-bootstrap4.min.css">
  <!-- Bootstrap4 Duallistbox -->
  <link rel="stylesheet" href="../stp_con/bootstrap4-duallistbox/bootstrap-duallistbox.min.css">
  <!-- BS Stepper -->
  <link rel="stylesheet" href="../stp_con/bs-stepper/css/bs-stepper.min.css">
  <!-- dropzonejs -->
  <link rel="stylesheet" href="../stp_con/dropzone/min/dropzone.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../assets/css/adminlte.min.css">
   <link rel="stylesheet" type="text/css" href="../stp_con/parsley/parsley.css"/>
</head>
<body class="hold-transition sidebar-mini layout-fixed top-nav">
<!-- Site wrapper -->
<div class="wrapper"> 
   <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header bg-primary" style=" color: white;">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <a><strong>Level For Examination Result</strong></a>
              
          </div> 
        </div>
      </div><!-- /.container-fluid -->
    </section>
    <br><br>
   <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row"> 
          <!-- /.col -->
          <div class="col-md-12">
            <div class="card">
              <div class="card-header p-2">
                <ul class="nav nav-pills ">
                  <?php   
                    $query= mysqli_query($conn,"SELECT * FROM exam_srms,class_srms where exam_srms.class_id=class_srms.class_id AND exam_srms.exam_id='$ex_id'")or die(mysqli_error());
                      $class1 = mysqli_fetch_array($query);
                      $class_id=$class1['class_id']; 
                    ?>    
                    <li class="nav-item "><a class="nav-link active" href="#class_one" data-toggle="tab"><?php echo $class1['exam_name']; ?>-<?php echo $class1['class_name']; ?></a></li>   
                </ul>
              </div>

              <!-- /.card-header --> 
              <div class="card-body"> 
              <div class="tab-content">
              <!-- /.tab-pane -->
              <div class="active tab-pane" id="class_one">
              <div class="container-fluid">
                <div class="row">
                  <div class="col-sm-12">  
                    <div class="card"> 
                      <div class="card-header">
                      <h3 class="card-title">Examination Result List</h3>
                      <div class="float-right mb-2">
                          <a data-toggle="modal" href="#exel" class="btn btn-success btn-xs float-right ml-2" >
                            <i class="fa fa-upload"></i> Upload CSV File
                        </a>
                        <a data-toggle="modal" href="#modal-lg" class="btn btn-info btn-xs float-right ml-2" >
                            <i class="fa fa-plus"></i> Add Result Here
                        </a> 
                         
                        <?php 
                        $Level_1= mysqli_query($conn,"SELECT * FROM class_srms where class_id='$class_id'")or die(mysqli_error());
                        $level_class = mysqli_fetch_array($Level_1);
                        $check_level=$level_class['level'];
                        if ($check_level=="Advance") {
                        ?> 
                        <a  href="admin.php?page=view_all_ad_report&Id_2=<?php echo $ex_id; ?>" class="btn btn-info btn-xs float-right ml-2" >  <i class="fa fa-edit"></i> View Report
                        </a>
                         <a href="admin.php?page=fgtutrs345gj789km231ExamUIYTR7ttdeewz34&Id_2=<?php echo $ex_id ?>&id_A=<?php echo $class_id ?>" onclick="return confirm('Are you sure! Do you want to Send Result to their Parent?');" class="btn btn-danger btn-xs float-right ml-2"> 
                            <i class="fa fa-upload"></i> Send Result
                        </a>
                        <?php }else{
                          ?>
                           
                           <a  href="admin.php?page=view_all_report&Id_2=<?php echo $ex_id; ?>" class="btn btn-info btn-xs float-right ml-2" >  <i class="fa fa-edit"></i> View Report
                        </a>
                         <a href="admin.php?page=fgtutrs345gj789km231ExamUIYTR7ttdeewz34&Id_2=<?php echo $ex_id ?>&id_c=<?php echo $class_id ?>" onclick="return confirm('Are you sure! Do you want to Send Result to their Parent?');" class="btn btn-danger btn-xs float-right ml-2"> 
                            <i class="fa fa-upload"></i> Send Result
                        </a>
                          <?php } ?>
                       
                        </div>
                        </div>  
                       <!-- /.card-header -->
                        <div class="card-body">
                        <div class="table-responsive"> 
                            <table id="example1" class="table table-striped display table-bordered">
                                <thead>
                                  <tr>
                                      <th>#</th>
                                      <th>ADM NUMBER</th> 
                                      <th>Student Name</th> 
                                      <th>Tatal marks</th>
                                      <th>Average</th>
                                      <th>Point</th> 
                                      <th>DIV</th> 
                                      <th>YEAR</th> 
                                      <th>Added By</th> 
                                      <th>Report</th>
                                      <th>Action</th>
                                  </tr>
                                  </thead>
                          <tbody>  
                        <?php 
                         // Fetch distinct students
                        $students_query = "SELECT DISTINCT marks_srms.result_added_by,marks_srms.Year,marks_srms.student_roll_no, student_srms.student_name, marks_srms.exam_id FROM student_srms,marks_srms WHERE marks_srms.student_roll_no=student_srms.student_roll_no  AND  marks_srms.class_id='$class_id' AND marks_srms.exam_id='$ex_id'";
                        $students_result = $conn->query($students_query);

                        if ($check_level=="Advance") { 
                        // Function to get grade points
                         
                        $i = 1;
                        while ($student = $students_result->fetch_assoc()) {
                            $student_id = $student['student_roll_no'];
                            $student_name = $student['student_name'];
                            $Year = $student['Year']; 
                            $result_added_by = $student['result_added_by'];
                            $result_id = $student['exam_id']; 
                            // Fetch marks for the student
                            $marks_query = "SELECT * FROM marks_srms WHERE student_roll_no = '$student_id' AND class_id='$class_id' AND  exam_id='$ex_id' AND identity_sub !='BAM' AND identity_sub !='CIV' ORDER BY marks DESC LIMIT 3";
                            $marks_result = $conn->query($marks_query); 
                           

                            $total_marks = 0;
                            $total_grade_points = 0;

                            while ($mark = $marks_result->fetch_assoc()) {
                                $total_marks += $mark['marks'];
                                $total_grade_points +=  $mark['point'];
                               
                                
                            }

                           $avr = round($total_marks/3, 1);
                            // Determine division based on grade points

                           if ($total_grade_points >= 3 && $total_grade_points <= 9) {
                                    $division = "I";
                                    } elseif ($total_grade_points >= 10 && $total_grade_points <= 12) {
                                        $division = "II";
                                    } elseif ($total_grade_points >= 13 && $total_grade_points <= 17) {
                                        $division = "III";
                                    } elseif ($total_grade_points >= 18 && $total_grade_points <= 19) {
                                        $division = "IV";
                                    } else {
                                        $division = "0";
                                    }
 
                             ?> 
                           <tr>
                            <td class="text-center"><?php echo $i++ ?></td>
                            <td><?php echo $student_id ?></td>
                            <td><?php echo $student_name ?></td>
                            <td><?php echo $total_marks ?></td>
                            <td><?php echo $avr ?></td>
                            <td><?php echo $total_grade_points ?></td>
                            <td><?php echo $division ?></td>
                            <td><?php echo $Year ?></td>
                            <td><?php echo $result_added_by ?></td> 
                            <td> 
                              <div class='btn-group'>
                              <a  href='admin.php?page=view_A_student_result&id=<?php echo $student_id ?>&id_4=<?php echo $ex_id ?>' class='btn btn-secondary btn-flat'><i class='fa fa-eye'></i>
                              </a> 
                            </div> 
                            </td> 
                            <td>
                            <div class='btn-group'>
                              <a href='admin.php?page=edit_february&id=<?php echo $student_id ?>&id_4=<?php echo $ex_id ?>' class='btn btn-primary btn-flat manage_class'>
                                <i class='fas fa-edit'></i> 
                              </a>
                               <a href='admin.php?page=fgtutrs345gj789km231£xamUIYTR7ttdeewz34&Id_2=<?php echo $ex_id ?>&del=<?php echo $student_id ?>' onclick='return confirm('Are you sure you want to delete?');' class='btn btn-danger btn-flat delete_student'>
                                <i class='fas fa-trash'></i>
                              </a>   
                          </div> 
                        </td>
                        </tr>
                      <?php } }else{ 
                       
                        
                        $i = 1;
                        while ($student = $students_result->fetch_assoc()) {
                            $student_id = $student['student_roll_no'];
                            $student_name = $student['student_name'];
                            $Year = $student['Year']; 
                            $result_added_by = $student['result_added_by'];
                            $result_id = $student['exam_id']; 
                            // Fetch marks for the student
                            $marks_query = "SELECT * FROM marks_srms WHERE student_roll_no = '$student_id' AND class_id='$class_id' AND  exam_id='$ex_id' ORDER BY marks DESC LIMIT 7";
                            $marks_result = $conn->query($marks_query);

                            $total_marks = 0;
                            $total_grade_points = 0;

                            while ($mark = $marks_result->fetch_assoc()) {
                                $total_marks += $mark['marks'];
                                $total_grade_points += $mark['point'];
                               
                                
                            }

                           $avr = round($total_marks/7, 1);
                            // Determine division based on grade points

                            if ($total_grade_points <= 17) {
                                $division = "I";
                            } elseif ($total_grade_points <= 22) {
                                $division = "II";
                            } elseif ($total_grade_points <= 25) {
                                $division = "III";
                            } elseif ($total_grade_points <= 33) {
                                $division = "IV";
                            } else {
                                $division = "Fail";
                            }
 
                             ?> 
                           <tr>
                            <td class="text-center"><?php echo $i++ ?></td>
                            <td><?php echo $student_id ?></td>
                            <td><?php echo $student_name ?></td>
                            <td><?php echo $total_marks ?></td>
                            <td><?php echo $avr ?></td>
                            <td><?php echo $total_grade_points ?></td>
                            <td><?php echo $division ?></td>
                            <td><?php echo $Year ?></td>
                            <td><?php echo $result_added_by ?></td> 
                            <td>
                              <?php 
                              if ($check_level=="Advance") {?> 
                              <div class='btn-group'>
                              <a  href='admin.php?page=view_A_student_result&id=<?php echo $student_id ?>&id_4=<?php echo $ex_id ?>' class='btn btn-secondary btn-flat'><i class='fa fa-eye'></i>
                              </a> 
                            </div>
                             <?php }else{ ?>
                              <div class='btn-group'>
                              <a  href='admin.php?page=view_student_result&id=<?php echo $student_id ?>&id_4=<?php echo $ex_id ?>' class='btn btn-info btn-flat'><i class='fa fa-eye'></i>
                              </a> 
                            </div>
                             <?php }?> 
                            </td> 
                            <td>
                            <div class='btn-group'>
                              <a href='admin.php?page=edit_february&id=<?php echo $student_id ?>&id_4=<?php echo $ex_id ?>' class='btn btn-primary btn-flat manage_class'>
                                <i class='fas fa-edit'></i> 
                              </a>
                               <a href='admin.php?page=fgtutrs345gj789km231£xamUIYTR7ttdeewz34&Id_2=<?php echo $ex_id ?>&del=<?php echo $student_id ?>' onclick='return confirm('Are you sure you want to delete?');' class='btn btn-danger btn-flat delete_student'>
                                <i class='fas fa-trash'></i>
                              </a>   
                          </div> 
                        </td>
                        </tr>

                        <?php }}?>
                      </tbody> 
                           <tfoot>
                             <tr> 
                                <th>#</th>
                                <th>ADM NUMBER</th> 
                                <th>Student Name</th> 
                                <th>Total marks</th>
                                <th>Average</th>
                                <th>Point</th> 
                                <th>DIV</th> 
                                <th>YEAR</th> 
                                <th>Added By</th> 
                                <th>Report</th>
                                <th>Action</th>
                            </tr> 
                          </tfoot> 
                        </table>
                            <!-- /.add class one result -->
                       <div id="class_1" class="modal fade">
                        <div class="modal-dialog modal-lg">
                          <form method="post" action="" id="login_form">
                              <div class="modal-content">
                                <div class="modal-header">
                                    <h4 class="modal-title" id="modal_title">Upload Result</h4>
                                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                                </div> 
                                <div class="modal-body"> 
                                <?php  
                                  $query = "SELECT * FROM class_srms where class_id='$class_id' AND  class_status='Enable' ORDER BY class_name ASC"; 
                                  $result = $conn->query($query);      
                                   ?>
                                   
                                    <div class="form-group">
                                        <label>Class <span style="color: red;">*</span></label>
                                         <?php 
                                          if($result->num_rows > 0){ 
                                              while($row = $result->fetch_assoc()){  
                                                  echo '
                                                  <input type="hidden" name="class_id" value="'.$row['class_id'].'" class="form-control" required>
                                                  <input type="text" value="'.$row['class_name'].'" class="form-control" disabled>'; 
                                              } 
                                          }else{ 
                                              echo '<input type="text" disabled class="form-control" placeholder="Class not available Contuct to admin">'; 
                                          } 
                                        ?>
                                      </div> 
                                      <div class="form-group">
                                              <label>Year of study</label>
                                              <div class="select2-purple">  
                                              <select name="year_of_study" id="year_of_study" class="form-control select2" data-dropdown-css-class="Select a year" style="width: 100%;" required>
                                                  <option value="">Select Year of study</option> 
                                                  <option value="2020">2020</option> 
                                                  <option value="2021">2021</option> 
                                                  <option value="2022">2022</option>
                                                  <option value="2023">2023</option> 
                                                  <option value="2024">2024</option> 
                                                  <option value="2025">2025</option>
                                                  <option value="2026">2026</option>
                                                  <option value="2027">2027</option>
                                                  <option value="2028">2028</option> 
                                                  <option value="2028">2029</option>   
                                            </select>
                                          </div>
                                        </div>
                                        <div class="form-group">
                                            <label>Student Name<span style="color: red;">*</span></label>
                                            <div class="select2-purple"> 
                                            <?php  
                                              $query = "SELECT * FROM student_srms where class_id= '$class_id' and student_status ='Enable' ORDER BY student_name ASC"; 
                                              $result = $conn->query($query);         
                                               ?> 

                                              <select name="student_roll_no" class="form-control select2" data-dropdown-css-class="Select a Student" style="width: 100%;" required>
                                                  <option>Select Student</option>
                                                  <?php 
                                                  if($result->num_rows > 0){
                                                   
                                                      while($row = $result->fetch_assoc()){  
                                                          echo '<option value="'.$row['student_roll_no'].'">'.$row['student_name'].' | '.$row['student_roll_no'].'</option>';

                                                      }
                                                     
                                                  }else{ 
                                                      echo '<option value="">Student not available</option>'; 
                                                  } 
                                                ?>
                                            </select>
                                          </div>
                                        </div>  
                                        <div class="form-group">
                                            <label>Subject Name<span style="color: red;">*</span></label>
                                            <div class="select2-purple"> 
                                            <?php  
                                              $i = 1; 
                                              $query = "SELECT * FROM subject,class_srms,general_subject WHERE subject.class_id=class_srms.class_id AND subject.sub_id=general_subject.sub_id AND subject.class_id= '$class_id' AND subject.program_stutus='Enable' ORDER BY general_subject.subject_name ASC"; 
                                              $result = $conn->query($query);         
                                               ?> 
                                              <select name="subject_id" class="form-control select2" data-dropdown-css-class="Select a Student" style="width: 100%;" required>
                                                  <option>Select Subject</option>
                                                  <?php 
                                                  if($result->num_rows > 0){ 
                                                      while($row = $result->fetch_assoc()){  
                                                          echo '<option value="'.$row['sub_id'].'">'.$i++.' | '.$row['subject_name'].'</option>';

                                                      }
                                                     
                                                  }else{ 
                                                      echo '<option value="">Subject not available</option>'; 
                                                  } 
                                                ?>
                                            </select>
                                          </div>
                                        </div> 

                                        <?php   
                                         $query= mysqli_query($conn,"select * from teacher_srms where user_contact_no = '$session_id'")or die(mysqli_error());
                                        $q1 = mysqli_fetch_array($query);     
                                        ?>  
                                        <input type="hidden" name="teacher_roll_no" value="<?php echo $q1['teacher_roll_no']; ?>">            
                                        <input type="hidden" name="result_added_by" value="<?php echo $q1['user_name']; ?>"> 
                                        <input type="hidden" name="exam_id" value="<?php echo $ex_id; ?>"> 
                                          <div class="form-group">
                                          <label>SCORE <span style="color: red;">*</span></label>
                                           <input type="text" name="marks" id="marks" class="form-control" required data-parsley-max="100" required data-parsley-type="integer"  data-parsley-trigger="keyup"  placeholder="Hint: Marks <= 100"/>
                                          </div> 
                                      </div>
                                    <div class="modal-footer">  
                                    <button type="submit" name="add_marks" class="btn btn-info">ADD RESULT</button>
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                </div>
                              </div> 
                          </form>
                        </div>
                    </div>
                <div class="modal fade" id="modal-lg">
        <div class="modal-dialog modal-lg">
          <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title">Large Modal</h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
              <p>One fine body&hellip;</p>
            </div>
            <div class="modal-footer justify-content-between">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              <button type="button" class="btn btn-primary">Save changes</button>
            </div>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
                
                <div id="exel" class="modal fade">
                        <div class="modal-dialog">
                          <form action="views/import.php" method="post" name="upload_excel" enctype="multipart/form-data">
                              <div class="modal-content">
                                <div class="modal-header">
                                    <h4 class="modal-title" id="modal_title">Upload Result</h4>
                                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                                </div> 
                                <div class="modal-body"> 
                                <?php  
                                  $query = "SELECT * FROM class_srms where class_id='$class_id' AND  class_status='Enable' ORDER BY class_name ASC"; 
                                  $result = $conn->query($query);      
                                   ?>
                                   
                                   <div class="form-group">
                                        <label>Class <span style="color: red;">*</span></label>
                                         <?php 
                                          if($result->num_rows > 0){ 
                                              while($row = $result->fetch_assoc()){  
                                                  echo '
                                                  <input type="hidden" name="class_id" value="'.$row['class_id'].'" class="form-control" required>
                                                  <input type="text" value="'.$row['class_name'].'" class="form-control" disabled>'; 
                                              } 
                                          }else{ 
                                              echo '<input type="text" disabled class="form-control" placeholder="Class not available Contuct to admin">'; 
                                          } 
                                        ?>
                                      </div> 
                                      <div class="form-group">
                                              <label>Year of study</label>
                                              <div class="select2-purple">  
                                              <select name="year_of_study" id="year_of_study" class="form-control select2" data-dropdown-css-class="Select a year" style="width: 100%;" required>
                                                  <option value="">Select Year of study</option> 
                                                  <option value="2020">2020</option> 
                                                  <option value="2021">2021</option> 
                                                  <option value="2022">2022</option>
                                                  <option value="2023">2023</option> 
                                                  <option value="2024">2024</option> 
                                                  <option value="2025">2025</option>
                                                  <option value="2026">2026</option>
                                                  <option value="2027">2027</option>
                                                  <option value="2028">2028</option> 
                                                  <option value="2028">2029</option>   
                                            </select>
                                          </div>
                                        </div>   
                                        <div class="form-group">
                                            <label>Subject Name<span style="color: red;">*</span></label>
                                            <div class="select2-purple"> 
                                            <?php  
                                              $i = 1; 
                                              $query = "SELECT * FROM subject,class_srms,general_subject WHERE subject.class_id=class_srms.class_id AND subject.sub_id=general_subject.sub_id AND subject.class_id= '$class_id' AND subject.program_stutus='Enable' ORDER BY general_subject.subject_name ASC"; 
                                              $result = $conn->query($query);         
                                               ?> 
                                              <select name="subject_id" class="form-control select2" data-dropdown-css-class="Select a Student" style="width: 100%;" required>
                                                  <option>Select Subject</option>
                                                  <?php 
                                                  if($result->num_rows > 0){ 
                                                      while($row = $result->fetch_assoc()){  
                                                          echo '<option value="'.$row['sub_id'].'">'.$i++.' | '.$row['subject_name'].'</option>';

                                                      }
                                                     
                                                  }else{ 
                                                      echo '<option value="">Subject not available</option>'; 
                                                  } 
                                                ?>
                                            </select>
                                          </div>
                                        </div> 
                                        <div class="form-group">
                                          <label>Choose File <span style="color: red;">*</span></label>
                                        
                                          <input type="file" name="file" accept=".xls,.xlsx,.CSV" id="file" class="form-control" placeholder="Select excel file.." required>
                                        </div>
                                        <?php   
                                         $query= mysqli_query($conn,"select * from teacher_srms where user_contact_no = '$session_id'")or die(mysqli_error());
                                        $q1 = mysqli_fetch_array($query);     
                                        ?>  
                                        <input type="hidden" name="teacher_roll_no" value="<?php echo $q1['teacher_roll_no']; ?>">            
                                        <input type="hidden" name="result_added_by" value="<?php echo $q1['user_name']; ?>"> 
                                        <input type="hidden" name="exam_id" value="<?php echo $ex_id; ?>"> 
                                         
                                    <div class="modal-footer">  
                                    <button type="submit" id="submit" name="Import" class="btn btn-success">UPLOAD AND IMPORT</button>
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                </div>
                              </div> 
                          </form>
                        </div>
                    </div>
                

                 </div>

                 </div>
                 </div> 
              </div> <!-- end card body-->
            </div> <!-- end card --> 
            </div>
            <!-- /.card -->
          </div>
          </div>
          <!-- END OF CLASS ONE --->
    
 

             </div>
                <!-- /.tab-content -->
             </div><!-- /.card-body -->
            </div>
            <!-- /.card -->
          </div>
          <!-- /.col -->
        </div>
        <!-- /.row -->
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  <script src="../stp_con/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="../stp_con/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- DataTables  & Plugins -->
<script src="../stp_con/datatables/jquery.dataTables.min.js"></script>
<script src="../stp_con/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="../stp_con/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="../stp_con/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
<script src="../stp_con/datatables-buttons/js/dataTables.buttons.min.js"></script>
<script src="../stp_con/datatables-buttons/js/buttons.bootstrap4.min.js"></script>
<script src="../stp_con/jszip/jszip.min.js"></script> 
<script src="../stp_con/pdfmake/pdfmake.min.js"></script>
<script src="../stp_con/pdfmake/vfs_fonts.js"></script> 
<script src="../stp_con/datatables-buttons/js/buttons.html5.min.js"></script>
<script src="../stp_con/datatables-buttons/js/buttons.print.min.js"></script>
<script src="../stp_con/datatables-buttons/js/buttons.colVis.min.js"></script>
 
<script src="../assets/js/adminlte.min.js"></script> 

<!-- Select2 -->
<script src="../stp_con/select2/js/select2.full.min.js"></script>
 
<script src="../stp_con/tempusdominus-bootstrap-4/js/tempusdominus-bootstrap-4.min.js"></script>
 
</body>

</html>

 <script>
  $(function () {
    $("#example1").DataTable({
      "responsive": false, "lengthChange": true, "autoWidth": false,
      "buttons": ["copy", "csv", "excel", "pdf", "print", "colvis"]
    }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)'); 
  });
</script>
 
 <script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2').select2()

    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })

    //Datemask dd/mm/yyyy
    $('#datemask').inputmask('dd/mm/yyyy', { 'placeholder': 'dd/mm/yyyy' })
    //Datemask2 mm/dd/yyyy
    $('#datemask2').inputmask('mm/dd/yyyy', { 'placeholder': 'mm/dd/yyyy' })
    //Money Euro
    $('[data-mask]').inputmask()

    //Date picker
    $('#reservationdate').datetimepicker({
        format: 'L'
    }); 
    //Date range picker
    $('#reservation').daterangepicker()
    //Date range picker with time picker
    $('#reservationtime').daterangepicker({
      timePicker: true,
      timePickerIncrement: 30,
      locale: {
        format: 'MM/DD/YYYY hh:mm A'
      }
    })
    //Date range as a button
    $('#daterange-btn').daterangepicker(
      {
        ranges   : {
          'Today'       : [moment(), moment()],
          'Yesterday'   : [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
          'Last 7 Days' : [moment().subtract(6, 'days'), moment()],
          'Last 30 Days': [moment().subtract(29, 'days'), moment()],
          'This Month'  : [moment().startOf('month'), moment().endOf('month')],
          'Last Month'  : [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        },
        startDate: moment().subtract(29, 'days'),
        endDate  : moment()
      },
      function (start, end) {
        $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'))
      }
    )

    //Timepicker
    $('#timepicker').datetimepicker({
      format: 'LT'
    })

    //Bootstrap Duallistbox
    $('.duallistbox').bootstrapDualListbox()

    //Colorpicker
    $('.my-colorpicker1').colorpicker()
    //color picker with addon
    $('.my-colorpicker2').colorpicker()

    $('.my-colorpicker2').on('colorpickerChange', function(event) {
      $('.my-colorpicker2 .fa-square').css('color', event.color.toString());
    })
  })
  // BS-Stepper Init
  document.addEventListener('DOMContentLoaded', function () {
    window.stepper = new Stepper(document.querySelector('.bs-stepper'))
  })

  // DropzoneJS Demo Code Start
  Dropzone.autoDiscover = false

  // Get the template HTML and remove it from the doumenthe template HTML and remove it from the doument
  var previewNode = document.querySelector("#template")
  previewNode.id = ""
  var previewTemplate = previewNode.parentNode.innerHTML
  previewNode.parentNode.removeChild(previewNode)

  var myDropzone = new Dropzone(document.body, { // Make the whole body a dropzone
    url: "/target-url", // Set the url
    thumbnailWidth: 80,
    thumbnailHeight: 80,
    parallelUploads: 20,
    previewTemplate: previewTemplate,
    autoQueue: false, // Make sure the files aren't queued until manually added
    previewsContainer: "#previews", // Define the container to display the previews
    clickable: ".fileinput-button" // Define the element that should be used as click trigger to select files.
  })

  myDropzone.on("addedfile", function(file) {
    // Hookup the start button
    file.previewElement.querySelector(".start").onclick = function() { myDropzone.enqueueFile(file) }
  })

  // Update the total progress bar
  myDropzone.on("totaluploadprogress", function(progress) {
    document.querySelector("#total-progress .progress-bar").style.width = progress + "%"
  })

  myDropzone.on("sending", function(file) {
    // Show the total progress bar when upload starts
    document.querySelector("#total-progress").style.opacity = "1"
    // And disable the start button
    file.previewElement.querySelector(".start").setAttribute("disabled", "disabled")
  })


    $('.toastrDefaultSuccess').click(function() {
      toastr.success('Lorem ipsum dolor sit amet, consetetur sadipscing elitr.')
    });
    $('.toastrDefaultInfo').click(function() {
      toastr.info('Lorem ipsum dolor sit amet, consetetur sadipscing elitr.')
    });
    $('.toastrDefaultError').click(function() {
      toastr.error('Lorem ipsum dolor sit amet, consetetur sadipscing elitr.')
    });
    $('.toastrDefaultWarning').click(function() {
      toastr.warning('Lorem ipsum dolor sit amet, consetetur sadipscing elitr.')
    });

  // Hide the total progress bar when nothing's uploading anymore
  myDropzone.on("queuecomplete", function(progress) {
    document.querySelector("#total-progress").style.opacity = "0"
  })

  // Setup the buttons for all transfers
  // The "add files" button doesn't need to be setup because the config
  // `clickable` has already been specified.
  document.querySelector("#actions .start").onclick = function() {
    myDropzone.enqueueFiles(myDropzone.getFilesWithStatus(Dropzone.ADDED))
  }
  document.querySelector("#actions .cancel").onclick = function() {
    myDropzone.removeAllFiles(true)
  }
  // DropzoneJS Demo Code End
  </script>
<script>
$(document).ready(function(){
    $('#login_form').parsley();
     $('#login_form2').parsley();
     $('#login_form3').parsley();
     $('#login_form4').parsley();
     $('#login_form5').parsley();
     $('#login_form6').parsley(); 
});

</script>