<?php include("parent/callMe.php"); ?>
<?php  include("sessioncheck.php"); ?>


<?php

$sessionIDs=$_REQUEST['session'];
$termID=$_REQUEST['term_id'];
$subjectID=$_REQUEST['subjects'];
$branchID=$_SESSION['branch'];
$cls=$_REQUEST['cls_id'];

 if($_REQUEST['term_id']!=NULL){  
  
    if ($_REQUEST['cls_id']=="All") {
      $qrr="SELECT * FROM eduportal_exam_marksheet  WHERE session_id='$sessionIDs' and term_id='$termID' and subject_id='$subjectID' and branch_id='$branchID' LIMIT 50";
    } else {

      $qrr="SELECT * FROM eduportal_exam_marksheet  WHERE session_id='$sessionIDs' and term_id='$termID' and subject_id='$subjectID' and class_id='$cls' and branch_id='$branchID' LIMIT 50";
      
      }
       $ToperWiseDatas = $m->query($qrr);
    
  }



       // getting branch details

        $m->where("branch_id", $_SESSION['branch']);
        $branch=$m->get("edu_branch_config");
   
       


function ordinal($number) {
    $ends = array('th','st','nd','rd','th','th','th','th','th','th');
    if ((($number % 100) >= 11) && (($number%100) <= 13))
        return $number. 'th';
    else
        return $number. $ends[$number % 10];
}
 
     ?>


<!DOCTYPE html>
<html lang="en">
  <head>
    
    <title>Subject Wise Topper | EduPortal</title>
    <?php include('RootMe/css_links.php');?>
   <link href="<?php echo DOMAIN;?>Production/css/select2.min.css" type="text/css" rel="stylesheet">
   <link href="<?php echo DOMAIN;?>Production/css/bootstrap-select.min.css" type="text/css" rel="stylesheet">

   <!-- files for multiselect dropdown -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>  
<!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css" />
 
<!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> -->

<script>
      

        function getSubjects(id){
            
            var session=document.getElementById("session").value;
            var clas=document.getElementById("class_id").value;
            
            if (window.XMLHttpRequest) {

              // code for IE7+, Firefox, Chrome, Opera, Safari

              xmlhttp=new XMLHttpRequest();

            } else { // code for IE6, IE5

              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

            }

            xmlhttp.onreadystatechange=function() {

              if (xmlhttp.readyState==4 && xmlhttp.status==200) {

                document.getElementById("subjects").innerHTML=xmlhttp.responseText;
                //alert(xmlhttp.responseText);

              }

            }

            xmlhttp.open("GET","get_subjects.php?section="+id+"&class="+clas+"&session="+session,true);

            xmlhttp.send();




          }
                        



  function getsec(id){

      if (window.XMLHttpRequest) {

        // code for IE7+, Firefox, Chrome, Opera, Safari

        xmlhttp=new XMLHttpRequest();

      } else { // code for IE6, IE5

        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

      }

      xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState==4 && xmlhttp.status==200) {

          document.getElementById("txt_section").innerHTML=xmlhttp.responseText;
          // alert(xmlhttp.responseText);

        }

      }

      xmlhttp.open("GET","get_section_all.php?class_id="+id,true);

      xmlhttp.send();




    }
                  



    function show_message(){
      
      $("#msg").slideDown('slow').delay(4000).slideUp('slow');
      $("#error").slideDown('slow').delay(5000).slideUp('slow');

     

    }

</script>
  </head>
  




  <body class="nav-md">
    <div class="container body">
      <div class="main_container">
        <div class="col-md-3 left_col">
     
            <?php include('RootMe/sidebar.php');?>

              <?php include('RootMe/header.php');?>
     
        <!-- page content -->
          <div class="right_col" role="main">
          <div class="" style="background-color: white;">
   

         <a href="<?php echo $_SESSION['dashboard'];?>">Dashboard </a> &nbsp; <i style="" class="fa fa-angle-double-right"></i>  &nbsp; 
              
         <a href="examination.php">View Marksheets </a> &nbsp; <i style="" class="fa fa-angle-double-right"></i>  &nbsp; 
              
          Subject Wise Topper Report
          <br><br>

  <section class="add_sub_agent" id="table_sub_agent">
  <div class="container">

 <div >
      
        <div class="panel panel-default">
          <div class="panel-heading">

          <input type="hidden" value="<?php echo session_id().session_id().session_id().session_id().session_id(); ?>" name="session" />


<div class="pull-right">         



</div>

<form  action="subject_wise_topper.php" enctype="multipart/form-data" method="get">

<input type="hidden" name="page" value="<?php echo $_REQUEST['page'];?>" />

          </span><div class="clearfix"></div> </div>
          <div class="panel-body">

            <div class="col-md-12" id="">


              <div class="col-md-2 col-xs-12" style="padding: 8px;">
                <div class="form-group ">
                  <label class="required">Session</label>
                    <select name="session" id="session" class="form-control" >

                        
                        <?php

 
                      $match;
                      if(!isset($_REQUEST['session'])){
                        $match=$_SESSION['current_session'];
                      }else { $match=$_REQUEST['session']; }
                     
   

                        $WorkAssignList = $m->get("edu_session_config");
                        foreach($WorkAssignList as $WorkAssignListVal)
                        { ?>

                           <option <?php if($match==$WorkAssignListVal['session_id']){echo 'selected';}?> value="<?php echo $WorkAssignListVal['session_id']; ?>"><?php echo $WorkAssignListVal['session_year']; ?></option>


                                <?php
                        } ?>

                    </select>
                </div>
              </div>


             <div class="col-md-2 col-xs-12" style="padding: 8px;">
                <div class="form-group ">
                  <label class="required">Term</label>

                     <select data-placeholder="Select" class="chosen-multi-select form-control" name="term_id" class="form-control"  >
                    <option value="">Select Term</option>

                    <?php
                    $m->where("session_id", $_SESSION['current_session']);
                    $m->where("branch_id", $_SESSION['branch']);
                    $m->orderBy("term_title","asc");
                    $WorkAssignList = $m->get("eduportal_exam_terms");
                    /*$query="select * from eduportal_exam_terms where branch_id=".$_SESSION['branch']." order by term_title asc ";
                    $WorkAssignList = $m->query($query);*/
                    foreach($WorkAssignList as $WorkAssignListVal)
                    { ?>

                       <option <?php if($_REQUEST['term_id']==$WorkAssignListVal['term_id']){echo 'selected';}?> value="<?php echo $WorkAssignListVal['term_id']; ?>"><?php echo $WorkAssignListVal['term_title']; ?></option>


                   <?php } ?>

                  </select> 

                  
                </div>
              </div> 


                <div class="col-md-2 col-xs-12" style="padding: 8px;">
                <div class="form-group ">
                  <label class="required">Class</label>

                     <select name="cls_id" id="cls_id"  data-placeholder="Select" class="chosen-multi-select inputfield form-control" onChange="getsec(this.value)"  >
                    <option value="All">All</option>

                    <?php
                    $m->where("branch_id", $_SESSION['branch']);
                    $WorkAssignList = $m->get("edu_class_config");
                    foreach($WorkAssignList as $WorkAssignListVal)
                    { ?>

                       <option <?php if($_REQUEST['cls_id']==$WorkAssignListVal['class_id']){echo 'selected';}?> value="<?php echo $WorkAssignListVal['class_id']; ?>"><?php echo $WorkAssignListVal['Class_Name']; ?></option>


                            <?php
                    } ?>

                </select> 

                  
                </div>
              </div>



               <div class="col-md-2 col-xs-12" style="padding: 8px;" >
                <div class="form-group ">
                  <label class="required">Subjects</label>
                    <select name="subjects" id="subjects" class="form-control"  >

                    	<option value="">Select Subject</option>
                      
                   <?php
                              
                              $section=$_REQUEST['section'];
                              $class=$_REQUEST['class_id'];
                              $branchID=$_SESSION['branch'];
  
                              $query="SELECT * FROM eduportal_subject_config where branch_id='$branchID' group by subject_id ";
                             
                              $secondLevel = $m->query($query);
                              
                              foreach($secondLevel as $secondLevel_val)
                              {
                              
                               ?>
                                <option <?php if($_REQUEST['subjects']==$secondLevel_val['subject_id']){echo "selected";} ?> value="<?php echo $secondLevel_val['subject_id']; ?>"><?php echo trim(utf8_encode_all($secondLevel_val['subject'])); ?></option>

                              <?php   
                              
                               } ?>

                    </select>

                  
                </div>
              </div> 

              <div class="col-md-1" style="padding:2px;" >
                <div class="form-group ">
                   <input type="hidden" name="addaccountAdmin" value="1">
                <button type="submit" class="btn btn-primary" style="margin-top: 29px;float: right;">Search</button>
                </div>
              </div>
              
               <?php if(isset($_GET['term_id']) ){ ?>
      
            <a href="print_subject_wise_topper.php?<?php echo session_id().session_id().session_id().session_id().session_id(); ?>&session=<?php echo $_REQUEST['session'];?>&term_id=<?php echo $_REQUEST['term_id'];?>&subjects=<?php echo $_REQUEST['subjects'];?>&cls_id=<?php echo $_REQUEST['cls_id'];?>"style="margin-top:31px;" class="btn btn-success"target="_blank" > Print Report </a>

            <?php } ?>
                      
              

           

             
            </div>
            <div class="clearfix"></div>
          </div>
        </div>
      </form>
      <div class="clearfix"></div>
    </div>
    <br>

     
          
              


      
<div style="">

	<?php 

if(isset($_GET['term_id']) ){
   
  ?>

<h2>Subject Wise Topper Students</h2>
    <table class="table table-bordered"  style="width:100%;font-size:10px;">
          
          <thead>
            <tr>
                <th width="3%" class=" center">S.No</th>


                <th width="20%" class=" center">Student Name</th>
                <th width="7%" class=" center">Father Name</th>                
                <th width="8%" class=" center"> Class/Section </th>  
                <th width="8%" class=" center"> Total Marks </th>  
                <th width="8%" class=" center"> Obtained Marks</th>
                <th width="8%" class=" center"> Percentage</th>
                <th width="8%" class=" center" > Grade </th>  
                <th width="5%" class=" center"> Position </th>               
  

            </tr>
        </thead>
        <tbody>
   
     
       
     <?php 
     $count=1;
     $SR=1;
     $new_array = array();

      $sbs_marks="";
      $sbs_tmarks="";
      $sbs_title="";
      $sbs_msg1;

     // getting all the marks of students and other data
 
      foreach($ToperWiseDatas as $WorkAssignListVal)
      { 
        $total_marks;
        $total_obt;
            
          $m->where("branch_id", $_SESSION['branch']);
          $m->where("student_id", $WorkAssignListVal['student_id']);         
          $StudentsData = $m->get("edu_student_details");

   //  echo $WorkAssignListVal['student_id'];


                $total_marks=$WorkAssignListVal['total_marks'];
                $total_obt=$WorkAssignListVal['obtained_marks'];

                $sbs_marks=$WorkAssignListVal['obtained_marks'];              
                

                  if($WorkAssignListVal['obtained_marks']!=NULL && $WorkAssignListVal['obtained_marks']!=""){
                  
                  /*$val=mb_substr($teacher_sb['subject'], 0, 3, "UTF-8")." ".$marks[0]['obtained_marks']."/".$marks[0]['total_marks'];
                  $sbs_msg1=$val.",".$sbs_msg1; */             
    
                  }


             $avg=($total_obt/$total_marks)*100;
             $avg=$avg+0;
             // getting average of total marks
             

             $query="SELECT * FROM eduportal_exam_school_grades WHERE percantage_start_range <=".round($avg)." and percantage_end_range >=".round($avg)." and branch_id=".$_SESSION['branch'];
     
             $average =  $m->query($query);

             
            
   
        
               $new_array['name_cell']=$StudentsData[0]['first_name'];
               $new_array['last_name']=$StudentsData[0]['last_name'];
               $new_array['roll']=$StudentsData[0]['candidate_roll_no'];
               $new_array['ref_no']=$StudentsData[0]['ref_no'];
               $new_array['class_id']=$StudentsData[0]['class_id'];
               $new_array['section_id']=$StudentsData[0]['section_id'];
               $new_array['sub_marks']=$sbs_marks;
               $new_array['total_marks']=$total_marks;
               $new_array['obt_marks']=$total_obt;
               $new_array['percantage']=number_format($avg,2);
               $new_array['grade']=$average[0]['grade_title'];
               $new_array['branch_id']=$_SESSION['branch'];
               $new_array['session']=session_id();             
               $new_array['student_id']=$WorkAssignListVal['student_id'];
               $new_array['cell_no']=$StudentsData[0]['cell_no'];
              // $new_array['sbs_total']=$sbs_msg1;

        
        
            //  print_r($new_array)."<br><br>";            
               $m->insert("eduportal_subjectwise_toper_temp",$new_array); 

 $total_marks=0; $total_obt=0;  $sbs_msg1="";
  }



if($_REQUEST['term_id']!=NULL){  
     

   $query="select * from eduportal_subjectwise_toper_temp where branch_id=".$_SESSION['branch']." order by percantage desc";
   $std_data=$m->query($query); 
  }
  // echo $query;


  $position;
           


   for($i=0;$i<count($std_data);$i++){
     
     
        $m->where("class_id", $std_data[$i]['class_id']);
        $class=$m->get("edu_class_config");

        $m->where("section_id", $std_data[$i]['section_id']);
        $section=$m->get("edu_adm_section_detail");

?>  
	<tr>
		
		<td class=" center"> <?php echo $SR;  $SR++;?> </td>
        <td style="padding-left:1%;"><?php echo $std_data[$i]['name_cell'];  ?></td>
        <td class="center"> <?php echo $std_data[$i]['last_name'];?> </td>
        <td class="center"> <?php echo $class[0]['Class_Name']." / ".$section[0]['section_name'];?> </td>
        <td class="center"> <?php echo $std_data[$i]['total_marks'];?> </td>
        <td class="center"> <?php echo $std_data[$i]['obt_marks'];?> </td>
        <td class="center"> <?php echo $std_data[$i]['percantage']."%";?> </td>
        <td class="center"> <?php echo $std_data[$i]['grade'];?> </td>
        <td class=" center" > <?php 

             $postition=ordinal($count);


             if($std_data[$i]['obt_marks']==$std_data[$i-1]['obt_marks']){
                $postition=ordinal($count-1);

              }else {
                
                $count+=1;
              }    


                   echo $postition; 



          $sms=$std_data[$i]['name_cell']." (".$class[0]['Class_Name']."-".trim($terms[0]['term_title']).") ".$std_data[$i]['sbs_total']." TM:".$std_data[$i]['obt_marks']."/".$std_data[$i]['total_marks']." Position:".$postition."
          ".$brid[0]['msg_name'];   
              // echo $sms."<br>";

                   ?> 


                   </td>


	</tr>

<?php }

$m->delete("eduportal_subjectwise_toper_temp"); 

 ?>
        </tbody>
        
    </table>

  <?php  } else {?>
<br>
  <div  style="display:block;" class="alert alert-danger alert-dismissible">
      <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
       <strong>Warning !</strong> Please select Term, Subject and serarch to View Report  !!!
  </div>
  <?php }?>
 


</div>

<br><br>


  </div>
  <div class="clearfix"></div>
</section>          
    

           
          </div>
        </div>

            </div>
          </div>
        </div>
        <!-- /page content -->
  


       <!-- jQuery -->
    <script src="js/ajax_functions.js"></script>  
    
  <?php include('RootMe/js_linksscript.php');?>


    <!-- searchable dropdown -->
    
    <link href="autosearch_select/select2.css" rel="stylesheet">
    <link href="autosearch_select/select2-default.css" rel="stylesheet">
    <script src="autosearch_select/select2.min.js"></script>
    
    <script>
    $(function() {
    $('.chosen-select').select2( { width:'100%', maximumSelectionSize: 1 } );
    $(document).ready(function() {
    $(".chosen-multi-select").select2( { width:'100%', } ); }); }); function slideout(){ setTimeout(function(){
    $(".alert-success").fadeOut("slow", function () { });
    $(".alert-danger").fadeOut("slow", function () { }); }, 4000);}
    </script>

  </body>
</html>
