<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class AnnualExam extends CI_Controller {
    public function __construct(){
        parent::__construct();
        $this->load->helper('text');
        $this->load->helper(array('form','url'));
        date_default_timezone_set('Asia/Kolkata');
        $this->load->model('StudentModel');
        $this->load->model('NewExamModel');
        $this->load->model('WelcomeModel');
        $this->load->model('NewFeeModel');
        $this->load->model('FeeModel');
        $this->load->model('SchoolModel');
        $this->load->helper('date');
        $this->load->database();    
        $this->load->library('form_validation');
        $this->load->helper(array('form', 'url'));
        $this->load->library('session');
         $this->load->library('Pdf');
    }
    
    public function generate_result_lower_classes(){
        $class=$_POST['exam_class'];
        $section=$_POST['section'];
        $exam_id=$_POST['exam_id'];
        $acedemic_session=$_POST['acedemic_session'];
        
        // $class="4";
        // $section="A";
        // $exam_id=3;
        // $acedemic_session=3;
        $exam_result_table="exam_result";
        $all_students=$this->NewExamModel->get_students_ac_session_by_course_and_class($acedemic_session,$class,$section);
        
        if($all_students){
            foreach($all_students as $students){
                $student_hy_total=0;
                $result_mm=0;
                
                $course_id=$students['course_id'];
                $sid=$students['sid'];
                $student_subjects_table="student_subjects";
                $get_student_subjects=$this->NewExamModel->get_student_subjects($sid,$course_id,$acedemic_session);
                
                
                
                
                $subjects_table="exam_subjects_".$acedemic_session;
                $get_course_subjects=$this->NewExamModel->get_course_subjects($course_id,$subjects_table);
                $exam_log_table="exam_log_".$acedemic_session;
                $get_student_exam_log_data=$this->NewExamModel->students_exam_log($sid,$acedemic_session,$exam_log_table);
                
                $exam_heads_table="exam_heads_".$acedemic_session;
                $get_exam_heads=$this->NewExamModel->get_exam_heads($exam_heads_table);
                $subject_failure_count=0;
                foreach($get_course_subjects as $course_subjects){
                    // $hic_subject_id=$course_subjects['hic_subject_id'];
                    // $hic_subject=$course_subjects['hic_subject'];
                    
                    $subject_id=$course_subjects['id'];
                    $subject=$course_subjects["subject"];
                    $subject_mm=$course_subjects['MM'];
                    // $subject_mm=100;
                    $subject_type=$course_subjects['subject_type'];
                    $exam_type=$course_subjects['exam_type'];
                    $subject_marks_obtained=0;
                    
                    if($exam_type == "Marks" && $subject_type == "Main Subject"){
                        
                        $student_subject=0;
                        if($get_student_subjects){
                            foreach($get_student_subjects as $subjectss){
                                if($subjectss['subject_id'] == $subject_id){
                                    $student_subject=$student_subject + 1;    
                                }
                                            
                            }
                        }
                        
                        
                            $result_mm=$result_mm + $subject_mm;
                        foreach($get_exam_heads as $exam_heads){
                            $ehid=$exam_heads['id'];
                            if($exam_heads['exam_mode'] == "SecondTerm"){
                            if($get_student_exam_log_data){
                                foreach($get_student_exam_log_data as $exam_log){
                                    if($ehid == $exam_log['exam_head'] && $subject_id == $exam_log['subject_id']){
                                        $subject_marks_obtained =$subject_marks_obtained + $exam_log['obtained_marks'];
                                }else{
                                    
                                }
                            }
                            }
                        }
                    }
                    $cat=date('Y-m-d H:i:s');
                    $tid=$this->session->userdata('tid');
                    $student_hy_total=$student_hy_total + $subject_marks_obtained;
                    $subject_percentage=($subject_marks_obtained/$subject_mm)* 100;
                    $sp1=round($subject_percentage,0);
                    $p_p=33;
                    if($sp1 < $p_p){
                        // echo $subject_percentage."<br>";
                        $subject_failure_count=$subject_failure_count + 1;
                        // echo $sp1."<br>";
                    }
                    

                    
                    // $data_array_update=array("hic_subject_id"=>$hic_subject_id,"hic_subject"=>$hic_subject,"subject"=>$subject,"subject_mm"=>$subject_mm,"hy_total_marks"=>$subject_marks_obtained,"uat"=>$cat,"upd_by"=>$tid,"class"=>$class,"section"=>$section);
                    
                    // $data_array=array("sid"=>$sid,"acedemic_session"=>$acedemic_session,"course_id"=>$course_id,"subject_id"=>$subject_id,"hy_total_marks"=>$subject_marks_obtained,"subject"=>$subject,"subject_mm"=>$subject_mm,"cat"=>$cat,"cat_by"=>$tid,"hic_subject_id"=>$hic_subject_id,"hic_subject"=>$hic_subject);
                    
                    // $data_array_update=array("subject"=>$subject,"second_term_subject_mm"=>$subject_mm,"second_term_marks"=>$subject_marks_obtained,"uat"=>$cat,"upd_by"=>$tid,"class"=>$class,"section"=>$section);
                    $data_array_update=array("second_term_subject_mm"=>$subject_mm,"second_term_marks"=>$subject_marks_obtained,"uat"=>$cat,"upd_by"=>$tid);
                    $data_array=array("sid"=>$sid,"acedemic_session"=>$acedemic_session,"course_id"=>$course_id,"subject_id"=>$subject_id,"second_term_marks"=>$subject_marks_obtained,"subject"=>$subject,"second_term_subject_mm"=>$subject_mm,"cat"=>$cat,"cat_by"=>$tid);
                    
                    
                        if($student_subject >= 1){
                            $update_subject_data=$this->NewExamModel->update_student_subjects_data($sid,$subject_id,$course_id,$acedemic_session,$student_subjects_table,$data_array_update);
                    
                        }else{
                            $add_subject_data=$this->NewExamModel->insert_data($data_array,$student_subjects_table);
                        }
                    }
                }
                
                $cat=date('Y-m-d H:i:s');
                $tid=$this->session->userdata('tid');
                $percentage1=0;
                if($result_mm !=0){
                    $percentage1=($student_hy_total * 100)/$result_mm; 
                }
                
                $percentage=number_format((float)$percentage1, 2, '.', '');
                
                
                $y1=$this->session->userdata("y1");
                $y2=$this->session->userdata("y2");
                $result_status="";
                if($subject_failure_count == 0){
                        $result_status="Pass";
                }else{
                    $result_status="Fail";    
                }
                
                $result_data_array=array("exam_id"=>$exam_id,"sid"=>$sid,"y1"=>$y1,"y2"=>$y2,"acedemic_session"=>$acedemic_session,"course_id"=>$course_id,"class"=>$class,"section"=>$section,"MM"=>$result_mm,"total_obtained_marks"=>$student_hy_total,"percentage"=>$percentage,"cat"=>$cat,"cat_by"=>$tid,"result_status"=>$result_status,"no_of_failure_subjects"=>$subject_failure_count);
                
                $result_data_array_update=array("class"=>$class,"section"=>$section,"MM"=>$result_mm,"total_obtained_marks"=>$student_hy_total,"percentage"=>$percentage,"uat"=>$cat,"updated_by"=>$tid,"result_status"=>$result_status,"no_of_failure_subjects"=>$subject_failure_count);
                
                
                // echo $result_status."-".$subject_failure_count."<br><br>";
                // var_dump($result_data_array_update)."<br><br>";
                
                $get_student_result_data=$this->NewExamModel->student_result($sid,$acedemic_session,$exam_id,$course_id,$exam_result_table);
                if($get_student_result_data){
                    $update_student_result_data=$this->NewExamModel->update_student_exam_data($sid,$exam_id,$course_id,$result_data_array_update,$exam_result_table);
                }else{
                    $add_exam_result=$this->NewExamModel->insert_data($result_data_array,$exam_result_table);
                }
            }
        }
        
        $rank_count=0;
        $get_course_result=$this->NewExamModel->get_class_section_result($exam_id,$acedemic_session,$class,$section,$exam_result_table);
        if($get_course_result){
            foreach($get_course_result as $result){
                $id=$result['id'];
                if($result['result_status'] == "Pass"){
                    $rank_count=$rank_count + 1;        
                    $cat=date("Y-m-d H:i:s");
                    $tid=$this->session->userdata('tid');
                    $data_array_update=array("rank_in_class"=>$rank_count,"uat"=>$cat,"updated_by"=>$tid);
                
                    $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                }else{
                    $cat=date("Y-m-d H:i:s");
                    $tid=$this->session->userdata('tid');
                    $data_array_update=array("rank_in_class"=>"","uat"=>$cat,"updated_by"=>$tid);
                
                    $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                }
            }
        }
        echo "success";
        
    }
    
    
    public function generate_result_hsc(){
        $class=$_POST['exam_class'];
        $section=$_POST['section'];
        $exam_id=$_POST['exam_id'];
        $acedemic_session=$_POST['acedemic_session'];
        $exam_result_table="exam_result";
        $all_students=$this->NewExamModel->get_students_ac_session_by_course_and_class_new($acedemic_session,$class,$section);
        
        if($all_students){
            foreach($all_students as $students){
                $student_hy_total=0;
                $result_mm=0;
                
                $course_id=$students['course_id'];
                $sid=$students['sid'];
                $student_subjects_table="student_subjects";
                $get_student_subjects=$this->NewExamModel->get_student_subjects($sid,$course_id,$acedemic_session);
                
                
                
                $subject_failure_count=0;
                $subjects_table="exam_subjects_".$acedemic_session;
                $get_course_subjects=$this->NewExamModel->get_course_subjects($course_id,$subjects_table);
                $exam_log_table="exam_log_".$acedemic_session;
                $get_student_exam_log_data=$this->NewExamModel->students_exam_log($sid,$acedemic_session,$exam_log_table);
                
                $exam_heads_table="exam_heads_".$acedemic_session;
                $get_exam_heads=$this->NewExamModel->get_exam_heads($exam_heads_table);
                
                foreach($get_course_subjects as $course_subjects){
                    $hic_subject_id=$course_subjects['hic_subject_id'];
                    $hic_subject=$course_subjects['hic_subject'];
                    
                    $subject_id=$course_subjects['id'];
                    $subject=$course_subjects["subject"];
                    $subject_mm=$course_subjects['MM'];
                    $subject_type=$course_subjects['subject_type'];
                    $exam_type=$course_subjects['exam_type'];
                    $subject_marks_obtained=0;
                    
                    if($exam_type == "Marks" && $subject_type == "Main Subject"){
                        
                        $student_subject=0;
                        if($get_student_subjects){
                            foreach($get_student_subjects as $subjectss){
                                if($subjectss['subject_id'] == $subject_id){
                                    $student_subject=$student_subject + 1;    
                                }
                                            
                            }
                        }
                        
                        if($student_subject >= 1){
                            
                            
                            $result_mm=$result_mm + $subject_mm;
                        foreach($get_exam_heads as $exam_heads){
                            $ehid=$exam_heads['id'];
                            if($exam_heads['exam_mode'] == "SecondTerm"){
                                if($get_student_exam_log_data){
                                    foreach($get_student_exam_log_data as $exam_log){
                                        if($ehid == $exam_log['exam_head'] && $subject_id == $exam_log['subject_id']){
                                            $subject_marks_obtained =$subject_marks_obtained + $exam_log['obtained_marks'];
                                        }else{
                                    
                                        }
                                    }
                                }
                            }
                        }
                        
                        
                    $cat=date('Y-m-d H:i:s');
                    $tid=$this->session->userdata('tid');
                    $student_hy_total=$student_hy_total + $subject_marks_obtained;
                    
                    $sub_percentage=($subject_marks_obtained*100)/$subject_mm;
                    
                    $sub_p_r=round($sub_percentage,0);
                    
                    if($sub_p_r <33){
                        $subject_failure_count=$subject_failure_count + 1;
                    }
                    // $data_array_update=array("hic_subject_id"=>$hic_subject_id,"hic_subject"=>$hic_subject,"subject"=>$subject,"subject_mm"=>$subject_mm,"hy_total_marks"=>$subject_marks_obtained,"uat"=>$cat,"upd_by"=>$tid,"class"=>$class,"section"=>$section);
                    
                    // $data_array=array("sid"=>$sid,"acedemic_session"=>$acedemic_session,"course_id"=>$course_id,"subject_id"=>$subject_id,"hy_total_marks"=>$subject_marks_obtained,"subject"=>$subject,"subject_mm"=>$subject_mm,"cat"=>$cat,"cat_by"=>$tid,"hic_subject_id"=>$hic_subject_id,"hic_subject"=>$hic_subject);
                    
                    
                    $data_array_update=array("second_term_marks"=>$subject_marks_obtained,"second_term_subject_mm"=>$subject_mm,"uat"=>$cat,"upd_by"=>$tid,"class"=>$class,"section"=>$section);
                    $update_subject_data=$this->NewExamModel->update_student_subjects_data($sid,$subject_id,$course_id,$acedemic_session,$student_subjects_table,$data_array_update);
                    
                    
                    
                        }
                    }
                }
                
                $cat=date('Y-m-d H:i:s');
                $tid=$this->session->userdata('tid');
                $percentage1=0;
                if($result_mm !=0){
                    $percentage1=($student_hy_total * 100)/$result_mm; 
                }
                
                // $percentage=number_format((float)$percentage1, 2, '.', '');
                $percentage=round($percentage1,2);
                
                
                $y1=$this->session->userdata("y1");
                $y2=$this->session->userdata("y2");
                
                $result_status="";
                if($subject_failure_count == 0){
                        $result_status="Pass";
                }else{
                    $result_status="Fail";    
                }
                
                $result_data_array=array("exam_id"=>$exam_id,"sid"=>$sid,"y1"=>$y1,"y2"=>$y2,"acedemic_session"=>$acedemic_session,"course_id"=>$course_id,"class"=>$class,"section"=>$section,"MM"=>$result_mm,"total_obtained_marks"=>$student_hy_total,"percentage"=>$percentage,"cat"=>$cat,"cat_by"=>$tid,"result_status"=>$result_status,"no_of_failure_subjects"=>$subject_failure_count);
                
                $result_data_array_update=array("class"=>$class,"section"=>$section,"MM"=>$result_mm,"total_obtained_marks"=>$student_hy_total,"percentage"=>$percentage,"uat"=>$cat,"updated_by"=>$tid,"result_status"=>$result_status,"no_of_failure_subjects"=>$subject_failure_count);
                
                $get_student_result_data=$this->NewExamModel->student_result($sid,$acedemic_session,$exam_id,$course_id,$exam_result_table);
                if($get_student_result_data){
                    $update_student_result_data=$this->NewExamModel->update_student_exam_data($sid,$exam_id,$course_id,$result_data_array_update,$exam_result_table);
                }else{
                    $add_exam_result=$this->NewExamModel->insert_data($result_data_array,$exam_result_table);
                }
            }
        }
        
        $rank_count=0;
        // $get_course_result=$this->NewExamModel->get_course_result($exam_id,$acedemic_session,$course_id,$exam_result_table);
        
        $get_course_result=$this->NewExamModel->get_class_section_result($exam_id,$acedemic_session,$class,$section,$exam_result_table);
        if($get_course_result){
            foreach($get_course_result as $result){
                $id=$result['id'];
                // $rank_count=$rank_count + 1;        
                // $cat=date("Y-m-d H:i:s");
                // $tid=$this->session->userdata('tid');
                // $data_array_update=array("rank_in_class"=>$rank_count,"uat"=>$cat,"updated_by"=>$tid);
                
                // $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                
                
                if($result['result_status'] == "Pass"){
                    $rank_count=$rank_count + 1;        
                    $cat=date("Y-m-d H:i:s");
                    $tid=$this->session->userdata('tid');
                    $data_array_update=array("rank_in_class"=>$rank_count,"uat"=>$cat,"updated_by"=>$tid);
                
                    $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                }else{
                    $cat=date("Y-m-d H:i:s");
                    $tid=$this->session->userdata('tid');
                    $data_array_update=array("rank_in_class"=>"","uat"=>$cat,"updated_by"=>$tid);
                
                    $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                }
            }
        }
        echo "success";
        
    }
    
    
    
    public function generate_result_hsc_class_11_annual(){
        $class=$_POST['exam_class'];
        $section=$_POST['section'];
        $exam_id=$_POST['exam_id'];
        $acedemic_session=$_POST['acedemic_session'];
        $exam_result_table="exam_result";
        $all_students=$this->NewExamModel->get_students_ac_session_by_course_and_class($acedemic_session,$class,$section);
        $student_subjects_table="student_subjects";
        if($all_students){
            foreach($all_students as $students){
                $student_hy_total=0;
                $annual_grand_total=0;
                $result_mm=0;
                
                $course_id=$students['course_id'];
                $sid=$students['sid'];
                
                $get_student_subjects=$this->NewExamModel->get_student_subjects($sid,$course_id,$acedemic_session);
                
                
                
                $subject_failure_count=0;
                $subjects_table="exam_subjects_".$acedemic_session;
                $get_course_subjects=$this->NewExamModel->get_course_subjects($course_id,$subjects_table);
                $exam_log_table="exam_log_".$acedemic_session;
                $get_student_exam_log_data=$this->NewExamModel->students_exam_log($sid,$acedemic_session,$exam_log_table);
                
                $exam_heads_table="exam_heads_".$acedemic_session;
                $get_exam_heads=$this->NewExamModel->get_exam_heads($exam_heads_table);
                
                foreach($get_course_subjects as $course_subjects){
                    $hic_subject_id=$course_subjects['hic_subject_id'];
                    $hic_subject=$course_subjects['hic_subject'];
                    
                    $subject_id=$course_subjects['id'];
                    $subject=$course_subjects["subject"];
                    $subject_mm=$course_subjects['MM'];
                    $subject_type=$course_subjects['subject_type'];
                    $exam_type=$course_subjects['exam_type'];
                    $subject_marks_obtained=0;
                    $first_term_subject_marks_obtained=0;
                    if($exam_type == "Marks" && $subject_type == "Main Subject"){
                        
                        $student_subject=0;
                        if($get_student_subjects){
                            foreach($get_student_subjects as $subjectss){
                                if($subjectss['subject_id'] == $subject_id){
                                    $student_subject=$student_subject + 1;    
                                }
                                            
                            }
                        }
                        
                        if($student_subject >= 1){
                            $result_mm=$result_mm + $subject_mm;
                        foreach($get_exam_heads as $exam_heads){
                            $ehid=$exam_heads['id'];
                            if($exam_heads['exam_mode'] == "AnnualExam"){
                                if($get_student_exam_log_data){
                                    foreach($get_student_exam_log_data as $exam_log){
                                        if($ehid == $exam_log['exam_head'] && $subject_id == $exam_log['subject_id']){
                                            $subject_marks_obtained =$subject_marks_obtained + $exam_log['obtained_marks'];
                                        }else{
                                    
                                        }
                                    }
                                }
                            }
                            
                            if($exam_heads['exam_mode'] == "FirstTerm"){
                                if($get_student_exam_log_data){
                                    foreach($get_student_exam_log_data as $exam_log){
                                        if($ehid == $exam_log['exam_head'] && $subject_id == $exam_log['subject_id']){
                                            $first_term_subject_marks_obtained =$first_term_subject_marks_obtained + $exam_log['obtained_marks'];
                                        }else{
                                    
                                        }
                                    }
                                }
                            }
                            
                        }
                    $cat=date('Y-m-d H:i:s');
                    $tid=$this->session->userdata('tid');
                    $student_hy_total=$student_hy_total + $subject_marks_obtained;
                    // $subject_marks_obtained=$subject_marks_obtained;
                    $sub_percentage=($subject_marks_obtained*100)/$subject_mm;
                    
                    $annual_grand_total=$annual_grand_total + $first_term_subject_marks_obtained + $subject_marks_obtained;
                    
                    
                    $sub_annual_total=$first_term_subject_marks_obtained + $subject_marks_obtained;
                    $sub_annual_avg_total=$sub_annual_total/2;
                    $sub_per_ann_new=($subject_marks_obtained*100)/$subject_mm;
                    
                    
                    
                    $sub_p_r=round($sub_percentage,0);
                    
                    if($sub_per_ann_new < 33){
                        $subject_failure_count=$subject_failure_count + 1;
                    }
                    // $data_array_update=array("hic_subject_id"=>$hic_subject_id,"hic_subject"=>$hic_subject,"subject"=>$subject,"subject_mm"=>$subject_mm,"hy_total_marks"=>$subject_marks_obtained,"uat"=>$cat,"upd_by"=>$tid,"class"=>$class,"section"=>$section);
                    
                    // $data_array=array("sid"=>$sid,"acedemic_session"=>$acedemic_session,"course_id"=>$course_id,"subject_id"=>$subject_id,"hy_total_marks"=>$subject_marks_obtained,"subject"=>$subject,"subject_mm"=>$subject_mm,"cat"=>$cat,"cat_by"=>$tid,"hic_subject_id"=>$hic_subject_id,"hic_subject"=>$hic_subject);
                    
                    
                    $data_array_update=array("annual_total_marks"=>$subject_marks_obtained,"annual_subject_mm"=>$subject_mm,"uat"=>$cat,"upd_by"=>$tid,"class"=>$class,"section"=>$section);
                    $update_subject_data=$this->NewExamModel->update_student_subjects_data($sid,$subject_id,$course_id,$acedemic_session,$student_subjects_table,$data_array_update);
                    
                    
                    
                        }
                    }
                }
                
                
                $annual_grand_total=$annual_grand_total / 2;
                
                $cat=date('Y-m-d H:i:s');
                $tid=$this->session->userdata('tid');
                $percentage1=0;
                if($result_mm !=0){
                    $percentage1=($student_hy_total * 100)/$result_mm; 
                }
                
                // $percentage=number_format((float)$percentage1, 2, '.', '');
                $percentage=round($percentage1,2);
                
                $y1=$this->session->userdata("y1");
                $y2=$this->session->userdata("y2");
                
                $result_status="";
                if($subject_failure_count == 0){
                        $result_status="Pass";
                }else{
                    $result_status="Fail";    
                }
                
                $result_data_array=array("exam_id"=>$exam_id,"sid"=>$sid,"y1"=>$y1,"y2"=>$y2,"acedemic_session"=>$acedemic_session,"course_id"=>$course_id,"class"=>$class,"section"=>$section,"MM"=>$result_mm,"total_obtained_marks"=>$student_hy_total,"percentage"=>$percentage,"cat"=>$cat,"cat_by"=>$tid,"result_status"=>$result_status,"no_of_failure_subjects"=>$subject_failure_count);
                $result_data_array_update=array("class"=>$class,"section"=>$section,"MM"=>$result_mm,"total_obtained_marks"=>$student_hy_total,"percentage"=>$percentage,"uat"=>$cat,"updated_by"=>$tid,"result_status"=>$result_status,"no_of_failure_subjects"=>$subject_failure_count);
                
                $get_student_result_data=$this->NewExamModel->student_result($sid,$acedemic_session,$exam_id,$course_id,$exam_result_table);
                if($get_student_result_data){
                    $update_student_result_data=$this->NewExamModel->update_student_exam_data($sid,$exam_id,$course_id,$result_data_array_update,$exam_result_table);
                }else{
                    $add_exam_result=$this->NewExamModel->insert_data($result_data_array,$exam_result_table);
                }
            }
        }
        
        $rank_count=0;
        // $get_course_result=$this->NewExamModel->get_course_result($exam_id,$acedemic_session,$course_id,$exam_result_table);
        
        
        
        // $first_term_exam_id=3;
        
        // $first_term_result_status="";
        // $get_course_result=$this->NewExamModel->get_class_section_result($first_term_exam_id,$acedemic_session,$class,$section,$exam_result_table);
        // if($get_course_result){
        //     foreach($get_course_result as $result){
        //         $first_term_result_status=$result['result_status'];
        //     }
        // }
        
        
        $get_course_result=$this->NewExamModel->get_class_section_result($exam_id,$acedemic_session,$class,$section,$exam_result_table);
        if($get_course_result){
            foreach($get_course_result as $result){
                $id=$result['id'];
                // $rank_count=$rank_count + 1;        
                // $cat=date("Y-m-d H:i:s");
                // $tid=$this->session->userdata('tid');
                // $data_array_update=array("rank_in_class"=>$rank_count,"uat"=>$cat,"updated_by"=>$tid);
                
                // $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                
                
                if($result['result_status'] == "Pass"){
                    $rank_count=$rank_count + 1;        
                    $cat=date("Y-m-d H:i:s");
                    $tid=$this->session->userdata('tid');
                    $data_array_update=array("rank_in_class"=>$rank_count,"uat"=>$cat,"updated_by"=>$tid);
                
                    $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                }else{
                    $cat=date("Y-m-d H:i:s");
                    $tid=$this->session->userdata('tid');
                    $data_array_update=array("rank_in_class"=>"","uat"=>$cat,"updated_by"=>$tid);
                
                    $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                }
            }
        }
        echo "success";
        
    }
    
    
    
    
    
    public function generate_result_1_to_9(){
        $class=$_POST['exam_class'];
        $section=$_POST['section'];
        $exam_id=$_POST['exam_id'];
        $acedemic_session=$_POST['acedemic_session'];
        $exam_result_table="exam_result";
        $all_students=$this->NewExamModel->get_students_ac_session_by_course_and_class($acedemic_session,$class,$section);
        $student_subjects_table="student_subjects";
        if($all_students){
            foreach($all_students as $students){
                $student_annual_total=0;
                $annual_grand_total=0;
                $result_mm=0;
                
                $course_id=$students['course_id'];
                $sid=$students['sid'];
                
                $get_student_subjects=$this->NewExamModel->get_student_subjects($sid,$course_id,$acedemic_session);
                
                
                
                $subject_failure_count=0;
                $subjects_table="exam_subjects_".$acedemic_session;
                $get_course_subjects=$this->NewExamModel->get_course_subjects($course_id,$subjects_table);
                $exam_log_table="exam_log_".$acedemic_session;
                $get_student_exam_log_data=$this->NewExamModel->students_exam_log($sid,$acedemic_session,$exam_log_table);
                
                $exam_heads_table="exam_heads_".$acedemic_session;
                $get_exam_heads=$this->NewExamModel->get_exam_heads($exam_heads_table);
                
                foreach($get_course_subjects as $course_subjects){
                    $hic_subject_id=$course_subjects['hic_subject_id'];
                    $hic_subject=$course_subjects['hic_subject'];
                    
                    $subject_id=$course_subjects['id'];
                    $subject=$course_subjects["subject"];
                    $subject_mm=$course_subjects['MM'];
                    $subject_type=$course_subjects['subject_type'];
                    $exam_type=$course_subjects['exam_type'];
                    
                    $annual_marks_obtained=0;
                    $first_term_subject_marks_obtained=0;
                    $second_term_subject_marks_obtained=0;
                    
                    if($exam_type == "Marks" && $subject_type == "Main Subject"){
                        
                        $student_subject=0;
                        if($get_student_subjects){
                            foreach($get_student_subjects as $subjectss){
                                if($subjectss['subject_id'] == $subject_id){
                                    $student_subject=$student_subject + 1;    
                                }
                                            
                            }
                        }
                        
                        if($student_subject >= 1){
                            
                            
                            $result_mm=$result_mm + $subject_mm;
                            foreach($get_exam_heads as $exam_heads){
                                $ehid=$exam_heads['id'];
                                
                            
                                if($exam_heads['exam_mode'] == "FirstTerm"){
                                    if($get_student_exam_log_data){
                                        foreach($get_student_exam_log_data as $exam_log){
                                            if($ehid == $exam_log['exam_head'] && $subject_id == $exam_log['subject_id']){
                                                $first_term_subject_marks_obtained =$first_term_subject_marks_obtained + $exam_log['obtained_marks'];
                                            }else{
                                    
                                            }
                                        }
                                    }
                                }
                            
                                if($exam_heads['exam_mode'] == "SecondTerm"){
                                    if($get_student_exam_log_data){
                                        foreach($get_student_exam_log_data as $exam_log){
                                            if($ehid == $exam_log['exam_head'] && $subject_id == $exam_log['subject_id']){
                                                $second_term_subject_marks_obtained =$second_term_subject_marks_obtained + $exam_log['obtained_marks'];
                                            }else{
                                    
                                            }
                                        }
                                    }
                                }
                            
                                if($exam_heads['exam_mode'] == "AnnualExam"){
                                    if($get_student_exam_log_data){
                                        foreach($get_student_exam_log_data as $exam_log){
                                            if($ehid == $exam_log['exam_head'] && $subject_id == $exam_log['subject_id']){
                                                $annual_marks_obtained =$annual_marks_obtained + $exam_log['obtained_marks'];
                                            }else{
                                    
                                            }
                                        }
                                    }
                                }
                            
                        }
                    $cat=date('Y-m-d H:i:s');
                    $tid=$this->session->userdata('tid');
                    
                    
                    // $sub_total=($first_term_subject_marks_obtained + $annual_marks_obtained + $second_term_subject_marks_obtained)/3;
                    
                    
                    
                    $sub_annual_total=$first_term_subject_marks_obtained + $second_term_subject_marks_obtained + $annual_marks_obtained;
                    $sub_annual_avg_total=$sub_annual_total/3;
                    $annual_grand_total=$annual_grand_total + $sub_annual_avg_total;
                    
                    
                    $sub_per_ann_new=($sub_annual_avg_total*100)/$subject_mm;
                    $student_annual_total=$student_annual_total + $sub_annual_avg_total;
               
                    
                    if($sub_per_ann_new < 33){
                        $subject_failure_count=$subject_failure_count + 1;
                    }
                    // $data_array_update=array("hic_subject_id"=>$hic_subject_id,"hic_subject"=>$hic_subject,"subject"=>$subject,"subject_mm"=>$subject_mm,"hy_total_marks"=>$subject_marks_obtained,"uat"=>$cat,"upd_by"=>$tid,"class"=>$class,"section"=>$section);
                    
                    // $data_array=array("sid"=>$sid,"acedemic_session"=>$acedemic_session,"course_id"=>$course_id,"subject_id"=>$subject_id,"hy_total_marks"=>$subject_marks_obtained,"subject"=>$subject,"subject_mm"=>$subject_mm,"cat"=>$cat,"cat_by"=>$tid,"hic_subject_id"=>$hic_subject_id,"hic_subject"=>$hic_subject);
                    
                    
                            $data_array_update=array("annual_total_marks"=>$annual_marks_obtained,"annual_subject_mm"=>$subject_mm,"uat"=>$cat,"upd_by"=>$tid,"class"=>$class,"section"=>$section);
                            $update_subject_data=$this->NewExamModel->update_student_subjects_data($sid,$subject_id,$course_id,$acedemic_session,$student_subjects_table,$data_array_update);
                    
                    
                    
                        }
                    }
                }
                
                
                // $annual_grand_total=$annual_grand_total / 3;
                
                $cat=date('Y-m-d H:i:s');
                $tid=$this->session->userdata('tid');
                $percentage1=0;
                if($result_mm !=0){
                    $percentage1=($student_annual_total * 100)/$result_mm; 
                }
                
                // $percentage=number_format((float)$percentage1, 2, '.', '');
                $percentage=round($percentage1,2);
                
                
                $y1=$this->session->userdata("y1");
                $y2=$this->session->userdata("y2");
                
                $result_status="";
                if($subject_failure_count == 0){
                        $result_status="Pass";
                }else{
                    $result_status="Fail";    
                }
                
                $result_data_array=array("exam_id"=>$exam_id,"sid"=>$sid,"y1"=>$y1,"y2"=>$y2,"acedemic_session"=>$acedemic_session,"course_id"=>$course_id,"class"=>$class,"section"=>$section,"MM"=>$result_mm,"total_obtained_marks"=>$student_annual_total,"percentage"=>$percentage,"cat"=>$cat,"cat_by"=>$tid,"result_status"=>$result_status,"no_of_failure_subjects"=>$subject_failure_count);
                $result_data_array_update=array("class"=>$class,"section"=>$section,"MM"=>$result_mm,"total_obtained_marks"=>$student_annual_total,"percentage"=>$percentage,"uat"=>$cat,"updated_by"=>$tid,"result_status"=>$result_status,"no_of_failure_subjects"=>$subject_failure_count);
                
                $get_student_result_data=$this->NewExamModel->student_result($sid,$acedemic_session,$exam_id,$course_id,$exam_result_table);
                if($get_student_result_data){
                    $update_student_result_data=$this->NewExamModel->update_student_exam_data($sid,$exam_id,$course_id,$result_data_array_update,$exam_result_table);
                }else{
                    $add_exam_result=$this->NewExamModel->insert_data($result_data_array,$exam_result_table);
                }
            }
        }
        
        $rank_count=0;
        // $get_course_result=$this->NewExamModel->get_course_result($exam_id,$acedemic_session,$course_id,$exam_result_table);
        
        
        
        // $first_term_exam_id=3;
        
        // $first_term_result_status="";
        // $get_course_result=$this->NewExamModel->get_class_section_result($first_term_exam_id,$acedemic_session,$class,$section,$exam_result_table);
        // if($get_course_result){
        //     foreach($get_course_result as $result){
        //         $first_term_result_status=$result['result_status'];
        //     }
        // }
        
        
        $get_course_result=$this->NewExamModel->get_class_section_result($exam_id,$acedemic_session,$class,$section,$exam_result_table);
        if($get_course_result){
            foreach($get_course_result as $result){
                $id=$result['id'];
                // $rank_count=$rank_count + 1;        
                // $cat=date("Y-m-d H:i:s");
                // $tid=$this->session->userdata('tid');
                // $data_array_update=array("rank_in_class"=>$rank_count,"uat"=>$cat,"updated_by"=>$tid);
                
                // $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                
                
                if($result['result_status'] == "Pass"){
                    $rank_count=$rank_count + 1;        
                    $cat=date("Y-m-d H:i:s");
                    $tid=$this->session->userdata('tid');
                    $data_array_update=array("rank_in_class"=>$rank_count,"uat"=>$cat,"updated_by"=>$tid);
                
                    $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                }else{
                    $cat=date("Y-m-d H:i:s");
                    $tid=$this->session->userdata('tid');
                    $data_array_update=array("rank_in_class"=>"","uat"=>$cat,"updated_by"=>$tid);
                
                    $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                }
            }
        }
        echo "success";
        
    }
    
    
    
    public function generate_result_class_9(){
        $class=$_POST['exam_class'];
        $section=$_POST['section'];
        $exam_id=$_POST['exam_id'];
        $acedemic_session=$_POST['acedemic_session'];
        $exam_result_table="exam_result";
        $all_students=$this->NewExamModel->get_students_ac_session_by_course_and_class($acedemic_session,$class,$section);
        $student_subjects_table="student_subjects";
        if($all_students){
            foreach($all_students as $students){
                $student_annual_total=0;
                
                $annual_exam_total=0;
                $annual_grand_total=0;
                $result_mm=0;
                
                $course_id=$students['course_id'];
                $sid=$students['sid'];
                
                $get_student_subjects=$this->NewExamModel->get_student_subjects($sid,$course_id,$acedemic_session);
                
                
                
                $subject_failure_count=0;
                $subjects_table="exam_subjects_".$acedemic_session;
                $get_course_subjects=$this->NewExamModel->get_course_subjects($course_id,$subjects_table);
                $exam_log_table="exam_log_".$acedemic_session;
                $get_student_exam_log_data=$this->NewExamModel->students_exam_log($sid,$acedemic_session,$exam_log_table);
                
                $exam_heads_table="exam_heads_".$acedemic_session;
                $get_exam_heads=$this->NewExamModel->get_exam_heads($exam_heads_table);
                
                foreach($get_course_subjects as $course_subjects){
                    $hic_subject_id=$course_subjects['hic_subject_id'];
                    $hic_subject=$course_subjects['hic_subject'];
                    
                    $subject_id=$course_subjects['id'];
                    $subject=$course_subjects["subject"];
                    $subject_mm=$course_subjects['MM'];
                    $subject_type=$course_subjects['subject_type'];
                    $exam_type=$course_subjects['exam_type'];
                    
                    $annual_marks_obtained=0;
                    $first_term_subject_marks_obtained=0;
                    $second_term_subject_marks_obtained=0;
                    
                    if($exam_type == "Marks" && $subject_type == "Main Subject"){
                        
                        $student_subject=0;
                        if($get_student_subjects){
                            foreach($get_student_subjects as $subjectss){
                                if($subjectss['subject_id'] == $subject_id){
                                    $student_subject=$student_subject + 1;    
                                }
                                            
                            }
                        }
                        
                        if($student_subject >= 1){
                            
                            
                            $result_mm=$result_mm + $subject_mm;
                            foreach($get_exam_heads as $exam_heads){
                                $ehid=$exam_heads['id'];
                                
                            
                                if($exam_heads['exam_mode'] == "FirstTerm"){
                                    if($get_student_exam_log_data){
                                        foreach($get_student_exam_log_data as $exam_log){
                                            if($ehid == $exam_log['exam_head'] && $subject_id == $exam_log['subject_id']){
                                                $first_term_subject_marks_obtained =$first_term_subject_marks_obtained + $exam_log['obtained_marks'];
                                            }else{
                                    
                                            }
                                        }
                                    }
                                }
                            
                                if($exam_heads['exam_mode'] == "SecondTerm"){
                                    if($get_student_exam_log_data){
                                        foreach($get_student_exam_log_data as $exam_log){
                                            if($ehid == $exam_log['exam_head'] && $subject_id == $exam_log['subject_id']){
                                                $second_term_subject_marks_obtained =$second_term_subject_marks_obtained + $exam_log['obtained_marks'];
                                            }else{
                                    
                                            }
                                        }
                                    }
                                }
                            
                                if($exam_heads['exam_mode'] == "AnnualExam"){
                                    if($get_student_exam_log_data){
                                        foreach($get_student_exam_log_data as $exam_log){
                                            if($ehid == $exam_log['exam_head'] && $subject_id == $exam_log['subject_id']){
                                                $annual_marks_obtained =$annual_marks_obtained + $exam_log['obtained_marks'];
                                            }else{
                                    
                                            }
                                        }
                                    }
                                }
                            
                            }
                            $cat=date('Y-m-d H:i:s');
                            $tid=$this->session->userdata('tid');
                            $annual_exam_total=$annual_exam_total + $annual_marks_obtained;
                    
                            // $sub_total=($first_term_subject_marks_obtained + $annual_marks_obtained + $second_term_subject_marks_obtained)/3;
                    
                    
                    
                            $sub_annual_total=$first_term_subject_marks_obtained + $second_term_subject_marks_obtained + $annual_marks_obtained;
                            $sub_annual_avg_total=$sub_annual_total/3;
                            $annual_grand_total=$annual_grand_total + $sub_annual_avg_total;
                    
                    
                            $sub_per_ann_new=($annual_marks_obtained*100)/$subject_mm;
                            $student_annual_total=$student_annual_total + $sub_annual_avg_total;
               
                    
                            if($sub_per_ann_new < 33){
                                $subject_failure_count=$subject_failure_count + 1;
                            }
                            // $data_array_update=array("hic_subject_id"=>$hic_subject_id,"hic_subject"=>$hic_subject,"subject"=>$subject,"subject_mm"=>$subject_mm,"hy_total_marks"=>$subject_marks_obtained,"uat"=>$cat,"upd_by"=>$tid,"class"=>$class,"section"=>$section);
                    
                            // $data_array=array("sid"=>$sid,"acedemic_session"=>$acedemic_session,"course_id"=>$course_id,"subject_id"=>$subject_id,"hy_total_marks"=>$subject_marks_obtained,"subject"=>$subject,"subject_mm"=>$subject_mm,"cat"=>$cat,"cat_by"=>$tid,"hic_subject_id"=>$hic_subject_id,"hic_subject"=>$hic_subject);
                    
                    
                            $data_array_update=array("annual_total_marks"=>$annual_marks_obtained,"annual_subject_mm"=>$subject_mm,"uat"=>$cat,"upd_by"=>$tid,"class"=>$class,"section"=>$section);
                            $update_subject_data=$this->NewExamModel->update_student_subjects_data($sid,$subject_id,$course_id,$acedemic_session,$student_subjects_table,$data_array_update);
                    
                    
                    
                        }
                    }
                }
                
                
                // $annual_grand_total=$annual_grand_total / 3;
                
                $cat=date('Y-m-d H:i:s');
                $tid=$this->session->userdata('tid');
                $percentage1=0;
                if($result_mm !=0){
                    $percentage1=($annual_exam_total * 100)/$result_mm; 
                }
                
                // $percentage=number_format((float)$percentage1, 2, '.', '');
                $percentage=round($percentage1,2);
                
                
                $y1=$this->session->userdata("y1");
                $y2=$this->session->userdata("y2");
                
                $result_status="";
                if($subject_failure_count == 0){
                        $result_status="Pass";
                }else{
                    $result_status="Fail";    
                }
                
                $result_data_array=array("exam_id"=>$exam_id,"sid"=>$sid,"y1"=>$y1,"y2"=>$y2,"acedemic_session"=>$acedemic_session,"course_id"=>$course_id,"class"=>$class,"section"=>$section,"MM"=>$result_mm,"total_obtained_marks"=>$annual_exam_total,"percentage"=>$percentage,"cat"=>$cat,"cat_by"=>$tid,"result_status"=>$result_status,"no_of_failure_subjects"=>$subject_failure_count);
                $result_data_array_update=array("class"=>$class,"section"=>$section,"MM"=>$result_mm,"total_obtained_marks"=>$annual_exam_total,"percentage"=>$percentage,"uat"=>$cat,"updated_by"=>$tid,"result_status"=>$result_status,"no_of_failure_subjects"=>$subject_failure_count);
                
                $get_student_result_data=$this->NewExamModel->student_result($sid,$acedemic_session,$exam_id,$course_id,$exam_result_table);
                if($get_student_result_data){
                    $update_student_result_data=$this->NewExamModel->update_student_exam_data($sid,$exam_id,$course_id,$result_data_array_update,$exam_result_table);
                }else{
                    $add_exam_result=$this->NewExamModel->insert_data($result_data_array,$exam_result_table);
                }
            }
        }
        
        $rank_count=0;
        // $get_course_result=$this->NewExamModel->get_course_result($exam_id,$acedemic_session,$course_id,$exam_result_table);
        
        
        
        // $first_term_exam_id=3;
        
        // $first_term_result_status="";
        // $get_course_result=$this->NewExamModel->get_class_section_result($first_term_exam_id,$acedemic_session,$class,$section,$exam_result_table);
        // if($get_course_result){
        //     foreach($get_course_result as $result){
        //         $first_term_result_status=$result['result_status'];
        //     }
        // }
        
        
        $get_course_result=$this->NewExamModel->get_class_section_result($exam_id,$acedemic_session,$class,$section,$exam_result_table);
        if($get_course_result){
            foreach($get_course_result as $result){
                $id=$result['id'];
                // $rank_count=$rank_count + 1;        
                // $cat=date("Y-m-d H:i:s");
                // $tid=$this->session->userdata('tid');
                // $data_array_update=array("rank_in_class"=>$rank_count,"uat"=>$cat,"updated_by"=>$tid);
                
                // $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                
                
                if($result['result_status'] == "Pass"){
                    $rank_count=$rank_count + 1;        
                    $cat=date("Y-m-d H:i:s");
                    $tid=$this->session->userdata('tid');
                    $data_array_update=array("rank_in_class"=>$rank_count,"uat"=>$cat,"updated_by"=>$tid);
                
                    $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                }else{
                    $cat=date("Y-m-d H:i:s");
                    $tid=$this->session->userdata('tid');
                    $data_array_update=array("rank_in_class"=>"","uat"=>$cat,"updated_by"=>$tid);
                
                    $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                }
            }
        }
        echo "success";
        
    }
    
    public function generate_result_class_1_to_5(){
        $class=$_POST['exam_class'];
        $section=$_POST['section'];
        $exam_id=$_POST['exam_id'];
        $acedemic_session=$_POST['acedemic_session'];
        $exam_result_table="exam_result";
        $all_students=$this->NewExamModel->get_students_ac_session_by_course_and_class($acedemic_session,$class,$section);
        $student_subjects_table="student_subjects";
        if($all_students){
            foreach($all_students as $students){
                $student_annual_total=0;
                $annual_grand_total=0;
                $result_mm=0;
                
                $course_id=$students['course_id'];
                $sid=$students['sid'];
                
                $get_student_subjects=$this->NewExamModel->get_student_subjects($sid,$course_id,$acedemic_session);
                
                
                
                $subject_failure_count=0;
                $subjects_table="exam_subjects_".$acedemic_session;
                $get_course_subjects=$this->NewExamModel->get_course_subjects($course_id,$subjects_table);
                $exam_log_table="exam_log_".$acedemic_session;
                $get_student_exam_log_data=$this->NewExamModel->students_exam_log($sid,$acedemic_session,$exam_log_table);
                
                $exam_heads_table="exam_heads_".$acedemic_session;
                $get_exam_heads=$this->NewExamModel->get_exam_heads($exam_heads_table);
                
                foreach($get_course_subjects as $course_subjects){
                    $hic_subject_id=$course_subjects['hic_subject_id'];
                    $hic_subject=$course_subjects['hic_subject'];
                    
                    $subject_id=$course_subjects['id'];
                    $subject=$course_subjects["subject"];
                    $subject_mm=$course_subjects['MM'];
                    $subject_type=$course_subjects['subject_type'];
                    $exam_type=$course_subjects['exam_type'];
                    
                    $annual_marks_obtained=0;
                    $first_term_subject_marks_obtained=0;
                    $second_term_subject_marks_obtained=0;
                    
                    if($exam_type == "Marks" && $subject_type == "Main Subject"){
                        
                        $student_subject=0;
                        if($get_student_subjects){
                            foreach($get_student_subjects as $subjectss){
                                if($subjectss['subject_id'] == $subject_id){
                                    $student_subject=$student_subject + 1;    
                                }
                                            
                            }
                        }
                        
                        if($student_subject >= 1){
                            
                            
                            $result_mm=$result_mm + $subject_mm;
                            foreach($get_exam_heads as $exam_heads){
                                $ehid=$exam_heads['id'];
                                
                            
                                if($exam_heads['exam_mode'] == "FirstTerm"){
                                    if($get_student_exam_log_data){
                                        foreach($get_student_exam_log_data as $exam_log){
                                            if($ehid == $exam_log['exam_head'] && $subject_id == $exam_log['subject_id']){
                                                $first_term_subject_marks_obtained =$first_term_subject_marks_obtained + $exam_log['obtained_marks'];
                                            }else{
                                    
                                            }
                                        }
                                    }
                                }
                            
                                if($exam_heads['exam_mode'] == "SecondTerm"){
                                    if($get_student_exam_log_data){
                                        foreach($get_student_exam_log_data as $exam_log){
                                            if($ehid == $exam_log['exam_head'] && $subject_id == $exam_log['subject_id']){
                                                $second_term_subject_marks_obtained =$second_term_subject_marks_obtained + $exam_log['obtained_marks'];
                                            }else{
                                    
                                            }
                                        }
                                    }
                                }
                            
                                if($exam_heads['exam_mode'] == "AnnualExam"){
                                    if($get_student_exam_log_data){
                                        foreach($get_student_exam_log_data as $exam_log){
                                            if($ehid == $exam_log['exam_head'] && $subject_id == $exam_log['subject_id']){
                                                $annual_marks_obtained =$annual_marks_obtained + $exam_log['obtained_marks'];
                                            }else{
                                    
                                            }
                                        }
                                    }
                                }
                            
                        }
                    $cat=date('Y-m-d H:i:s');
                    $tid=$this->session->userdata('tid');
                    
                    
                    // $sub_total=($first_term_subject_marks_obtained + $annual_marks_obtained + $second_term_subject_marks_obtained)/3;
                    
                    $first_term_subject_marks_obtained_avg_per=$first_term_subject_marks_obtained*100/70;
                    
                    $sub_annual_total=$first_term_subject_marks_obtained_avg_per + $second_term_subject_marks_obtained + $annual_marks_obtained;
                    $sub_annual_avg_total=$sub_annual_total/3;
                    $annual_grand_total=$annual_grand_total + $sub_annual_avg_total;
                    
                    
                    $sub_per_ann_new=($sub_annual_avg_total*100)/$subject_mm;
                    $student_annual_total=$student_annual_total + $sub_annual_avg_total;
               
                    
                    if($sub_per_ann_new < 33){
                        $subject_failure_count=$subject_failure_count + 1;
                    }
                    // $data_array_update=array("hic_subject_id"=>$hic_subject_id,"hic_subject"=>$hic_subject,"subject"=>$subject,"subject_mm"=>$subject_mm,"hy_total_marks"=>$subject_marks_obtained,"uat"=>$cat,"upd_by"=>$tid,"class"=>$class,"section"=>$section);
                    
                    // $data_array=array("sid"=>$sid,"acedemic_session"=>$acedemic_session,"course_id"=>$course_id,"subject_id"=>$subject_id,"hy_total_marks"=>$subject_marks_obtained,"subject"=>$subject,"subject_mm"=>$subject_mm,"cat"=>$cat,"cat_by"=>$tid,"hic_subject_id"=>$hic_subject_id,"hic_subject"=>$hic_subject);
                    
                    
                        $data_array_update=array("annual_total_marks"=>$annual_marks_obtained,"annual_subject_mm"=>$subject_mm,"uat"=>$cat,"upd_by"=>$tid,"class"=>$class,"section"=>$section);
                        $update_subject_data=$this->NewExamModel->update_student_subjects_data($sid,$subject_id,$course_id,$acedemic_session,$student_subjects_table,$data_array_update);
                    
                    
                    
                        }
                    }
                }
                
                
                // $annual_grand_total=$annual_grand_total / 3;
                
                $cat=date('Y-m-d H:i:s');
                $tid=$this->session->userdata('tid');
                $percentage1=0;
                if($result_mm !=0){
                    $percentage1=($student_annual_total * 100)/$result_mm; 
                }
                
                // $percentage=number_format((float)$percentage1, 2, '.', '');
                $percentage=round($percentage1,2);
                
                
                $y1=$this->session->userdata("y1");
                $y2=$this->session->userdata("y2");
                
                $result_status="";
                if($subject_failure_count == 0){
                        $result_status="Pass";
                }else{
                    $result_status="Fail";    
                }
                
                $result_data_array=array("exam_id"=>$exam_id,"sid"=>$sid,"y1"=>$y1,"y2"=>$y2,"acedemic_session"=>$acedemic_session,"course_id"=>$course_id,"class"=>$class,"section"=>$section,"MM"=>$result_mm,"total_obtained_marks"=>$student_annual_total,"percentage"=>$percentage,"cat"=>$cat,"cat_by"=>$tid,"result_status"=>$result_status,"no_of_failure_subjects"=>$subject_failure_count);
                $result_data_array_update=array("class"=>$class,"section"=>$section,"MM"=>$result_mm,"total_obtained_marks"=>$student_annual_total,"percentage"=>$percentage,"uat"=>$cat,"updated_by"=>$tid,"result_status"=>$result_status,"no_of_failure_subjects"=>$subject_failure_count);
                
                $get_student_result_data=$this->NewExamModel->student_result($sid,$acedemic_session,$exam_id,$course_id,$exam_result_table);
                if($get_student_result_data){
                    $update_student_result_data=$this->NewExamModel->update_student_exam_data($sid,$exam_id,$course_id,$result_data_array_update,$exam_result_table);
                }else{
                    $add_exam_result=$this->NewExamModel->insert_data($result_data_array,$exam_result_table);
                }
            }
        }
        
        $rank_count=0;
        // $get_course_result=$this->NewExamModel->get_course_result($exam_id,$acedemic_session,$course_id,$exam_result_table);
        
        
        
        // $first_term_exam_id=3;
        
        // $first_term_result_status="";
        // $get_course_result=$this->NewExamModel->get_class_section_result($first_term_exam_id,$acedemic_session,$class,$section,$exam_result_table);
        // if($get_course_result){
        //     foreach($get_course_result as $result){
        //         $first_term_result_status=$result['result_status'];
        //     }
        // }
        
        
        $get_course_result=$this->NewExamModel->get_class_section_result($exam_id,$acedemic_session,$class,$section,$exam_result_table);
        if($get_course_result){
            foreach($get_course_result as $result){
                $id=$result['id'];
                // $rank_count=$rank_count + 1;        
                // $cat=date("Y-m-d H:i:s");
                // $tid=$this->session->userdata('tid');
                // $data_array_update=array("rank_in_class"=>$rank_count,"uat"=>$cat,"updated_by"=>$tid);
                
                // $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                
                
                if($result['result_status'] == "Pass"){
                    $rank_count=$rank_count + 1;        
                    $cat=date("Y-m-d H:i:s");
                    $tid=$this->session->userdata('tid');
                    $data_array_update=array("rank_in_class"=>$rank_count,"uat"=>$cat,"updated_by"=>$tid);
                
                    $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                }else{
                    $cat=date("Y-m-d H:i:s");
                    $tid=$this->session->userdata('tid');
                    $data_array_update=array("rank_in_class"=>"","uat"=>$cat,"updated_by"=>$tid);
                
                    $update_hic=$this->NewExamModel->update_item_data($id,$data_array_update,$exam_result_table);
                }
            }
        }
        echo "success";
    }
    
    
    
}