<?php
$sid=$this->uri->segment('3');
$class='';
$section='';
$get_student_data=$this->ExamModel->get_student_data($sid);
$acedemic_session=$this->session->userdata('acedemic_session');
$get_student_course_data=$this->ExamModel->get_student_course_info($sid,$acedemic_session);
if($get_student_course_data){
    foreach($get_student_course_data as $student_course_data){
        $class=$student_course_data['class'];
        $section=$student_course_data['section'];
    }
}
?>

<div class="container-fluid mt-3 mb-3">
<div class="row">
<div class="col-sm-12">

<div class="input-section">
<h3 class="text-center">SESSION 2020 - 21</h3>
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>NAME</th>
<!--<th>ROLL NO.</th>-->
<th>CLASS-SEC</th>
<th>SCHOLAR NO.</th>
<th>D.O.B</th>
<th>HOUSE</th>
<th>ATTENDANCE</th>
</tr>
</thead>
<tbody>
<?php 
if($get_student_data){
foreach($get_student_data as $student_data){
// $class=$student_data['class'];
?>
<tr>
<td><?php echo $student_data['student_name'];?></td>

<td><?php echo $class; echo "-"; echo $section;?></td>
<td><?php echo $student_data['sch_no'];?></td>
<td><?php echo $student_data['dob'];?></td>
<td><?php echo $student_data['house'];?></td>
<td>
<?php
$exam_result_data=$this->ExamModel->get_exam_result_data($sid); 
if($exam_result_data){
foreach($exam_result_data as $result_data){
if($result_data['result_type'] == 'yearly'){

$working_days=$result_data['annual_working_days'];
$present_days=$result_data['annual_attendance'];
echo  $present_days; echo " / "; echo $working_days; echo " ( ".round($present_days*100/$working_days,2)." % ) ";
}


}
}
?>
    
</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>

<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>

<tr>

<td colspan="14" class="text-center font-weight-bold">Annual Exam</td>

</tr>

<tr>
<td>S. No.</td>
<th>Subjects</th>
<th>MM</th>
<th>Half Yearly Marks</th>
<?php
$get_exam_heads=$this->ExamModel->get_exam_heads($class);
if($get_exam_heads){
foreach($get_exam_heads as $exam_heads){
if($exam_heads['exam_mode'] =='yearly'){
if($exam_heads['mm'] ==0 ||  $exam_heads['mm'] ==50){
?>

<?php
}else{
?>
<th><?php echo $exam_heads['exam_head']; ?></th>
<?php    
}

}
}
}
?>



<th>TOTAL (100)</th>
<th>GRAND TOTAL(200)</th>
<th>YEARLY AVERAGE</th>
<th>Action</th>
</tr>
    
</thead>
<tbody>
<?php 
 
$count=0;
$total_mm=0;
$grand_total=0;

    $half_yearly_total=0;
    $annual_total=0;
    $mm_total=0;

$get_subjects=$this->ExamModel->get_active_subject_data($class);
if($get_subjects){
foreach($get_subjects as $subjects){
$count= $count + 1;
$subject_id=$subjects['id'];
$exam_type=$subjects['exam_type'];
$max_marks=$subjects['MM'];
$total_mm=$total_mm + $max_marks;
$total1=0;
$total=0;
?>
<tr>
<td><?php echo $count; ?></td>
    
<td><?php echo $subjects['subject']; ?></td>
<td><?php  if($exam_type=='marks'){echo $subjects['MM']; }else if($exam_type=='grade'){echo "Grd";}?></td>
<td>
<?php
if($exam_type=='marks'){
$all_heads=$this->ExamModel->get_exam_heads($class);

if($all_heads){
foreach($all_heads as $exam_heads){
if($exam_heads['exam_mode'] ==''){
$ehid=$exam_heads['id'];

if($exam_heads['mm'] !=0 && $exam_heads['mm'] !=50){
    
$get_all_marks_total_data=$this->ExamModel->student_exam_log($sid,$ehid,$subject_id);
if($get_all_marks_total_data){
foreach($get_all_marks_total_data as $total_data){
$total1 = $total1 + $total_data['obtained_marks'];
}
}

}else{ 
$get_all_marks_total_data=$this->ExamModel->student_exam_log($sid,$ehid,$subject_id);
if($get_all_marks_total_data){
foreach($get_all_marks_total_data as $total_data){
$total1 = $total1 + $total_data['obtained_marks'];
}
}
}

}

}
}
echo  $total1;
// $grand_total=$grand_total + $total;
if($subjects['subject_type'] =='main_subject'){
$half_yearly_total=$half_yearly_total + $total1;
}
}else if($exam_type=='grade'){
if($all_heads){
foreach($all_heads as $exam_heads){
if($exam_heads['exam_mode'] ==''){
$ehid=$exam_heads['id'];    
if($exam_heads['mm'] ==0 || $exam_heads['mm'] ==50){
$get_marks=$this->ExamModel->student_exam_log($sid,$ehid,$subject_id);
if($get_marks){foreach($get_marks as $get_marks_data){
echo $get_marks_data['grade'];    
}
}
}
}
}
    
}

    
}

?>      
</td>


<?php 
$all_heads=$this->ExamModel->get_exam_heads($class);
if($all_heads){
foreach($all_heads as $exam_heads){
if($exam_heads['exam_mode'] =='yearly'){
$exam_head_id=$exam_heads['id'];
// if($exam_type == 'marks' && $exam_heads['mm'] !=0 &&  $exam_heads['mm'] !=50){
if($exam_type =='marks' && $exam_heads['mm'] !=0){


$exam_head_id=$exam_heads['id'];
$student_log_data=$this->ExamModel->student_exam_log($sid,$exam_head_id,$subject_id);
if($student_log_data){
foreach($student_log_data as $student_log){
?>
<td><?php
echo $student_log['obtained_marks'];
?></td>
<?php
}
}else{
?>
<td>...</td>
<?php
}

}

else if($exam_type == 'grade' && $exam_heads['mm'] !=0 ){
// else if($exam_type == 'grade' ){
$student_log_data=$this->ExamModel->student_exam_log($sid,$exam_head_id,$subject_id);
if($student_log_data){
foreach($student_log_data as $student_log){
?>
<td><?php
echo $student_log['grade']; 
?></td>
<?php
}
}else{
?>
<td>...</td>
<?php
}
}


}
}
}
?>


<td>
<?php


if($exam_type=='marks'){

if($all_heads){
foreach($all_heads as $exam_heads){
$ehid=$exam_heads['id'];
if($exam_heads['exam_mode'] =='yearly'){
if($exam_heads['mm'] !=0 && $exam_heads['mm'] !=50){
    
$get_all_marks_total_data=$this->ExamModel->student_exam_log($sid,$ehid,$subject_id);
if($get_all_marks_total_data){
foreach($get_all_marks_total_data as $total_data){
$total = $total + $total_data['obtained_marks'];
}
}

}else{ 
$get_all_marks_total_data=$this->ExamModel->student_exam_log($sid,$ehid,$subject_id);
if($get_all_marks_total_data){
foreach($get_all_marks_total_data as $total_data){
$total = $total + $total_data['obtained_marks'];
}
}

}


}
}
}
echo  $total;
$annual_total=$annual_total + $total;

}else if($exam_type=='grade'){
    
if($all_heads){
foreach($all_heads as $exam_heads){
$ehid=$exam_heads['id'];    
    
if($exam_heads['mm'] ==0 || $exam_heads['mm'] ==50){

$get_marks=$this->ExamModel->student_exam_log($sid,$ehid,$subject_id);
if($get_marks){foreach($get_marks as $get_marks_data){
echo $get_marks_data['grade'];     
}}

}
    
}
    
}

    
}
?>    

</td>
<td><?php echo $annual_grand_total=$total1 + $total; ?></td>
<td><?php echo $annual_average=($total1 + $total)/2; ?></td>
<td><a href="<?php echo base_url();?>Exam/add_exam_log/<?php echo $sid; echo "/"; echo $subjects['id']; ?>" class="btn btn-sm btn-warning">Edit</a></td>
</tr>
<?php
}
}
?>    

<tr>
<td colspan="2"></td>
<td colspan="1"><?php echo $total_mm;?></td>
<td><?php echo $half_yearly_total;?></td>
<td colspan="6"></td>
<td><?php echo $annual_total; ?></td>
<td><?php echo ($half_yearly_total + $annual_total); ?></td>
<td><?php echo ($half_yearly_total + $annual_total)/2; ?></td>
<td></td>
</tr>
<tr>
<td colspan="7">REMARKS :
<?php

$get_remarks_data=$this->ExamModel->get_student_remark($sid);
if($get_remarks_data){
foreach($get_remarks_data as $remarks_data){
if($remarks_data['quote_for']=='yearly'){
?>
<?php echo $remarks_data['quote']; ?>
<?php
}
}
}
$avg_total=($half_yearly_total + $annual_total)/2;
?>
</td>
<td colspan="7">PERCENTAGE :<?php echo ($avg_total*100)/$total_mm ?></td>
</tr>
</tbody>
</table>
</div>


</div>

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

