<?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 $student_data['roll_no'];?></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="3"></td>-->
<!--<td colspan="2">Unit Test 1</td>-->
<!--<td colspan="2">Unit Test 2</td>-->
<!--<td colspan="3"></td>-->
<!--<td></td>-->
<!--</tr>-->

<tr>
<td>S. No.</td>
<th>Subjects</th>
<th>MM</th>
<th>Half Yearly Total</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 ){
?>

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

}
}
}


?>


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

$grand_total=0;
$half_yearly_grand_total=0;

if($class !=9){
$total_mm=0;    
}else{
$total_mm=-100;    
}


$get_subjects=$this->ExamModel->get_active_subject_data($class);
if($get_subjects){
foreach($get_subjects as $subjects){
$total=0;
$total1=0;

$count= $count + 1;
$subject_id=$subjects['id'];
$exam_type=$subjects['exam_type'];
// if($exam_type =='marks'){
    
// $max_marks=$subjects['MM'];
// $total_mm=$total_mm + $max_marks;

// }

if($subjects['subject_type'] =='main_subject'){
$max_marks=$subjects['MM'];
$total_mm=$total_mm + $max_marks;
}
?>
<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);
// $total1=0;
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_grand_total=$half_yearly_grand_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'){
    
// if($exam_type == 'marks' && $exam_heads['mm'] !=0 &&  $exam_heads['mm'] !=50){

if($exam_type == 'marks'){

$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'){
?>

<td>
<?php
$exam_head_id=$exam_heads['id'];

if($exam_heads['mm'] !=50){
echo "";
}else if($exam_heads['mm'] ==50){
$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){
echo "".$student_log['grade']."";
}
}
}
?>
</td>
<?php

}

}

}
}

?>
<td>
    
<?php
// $total=0;
// if($all_heads){
// foreach($all_heads as $exam_heads){
// $ehid=$exam_heads['id'];

// $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'];
// }
// }

// }
// }
// if($exam_type=='marks'){echo  $total;}else {}

if($exam_type=='marks'){
// $total=0;
if($all_heads){
foreach($all_heads as $exam_heads){
if($exam_heads['exam_mode'] =='yearly'){
$ehid=$exam_heads['id'];

// if($exam_heads['mm'] !=0 && $exam_heads['mm'] !=50){
if($exam_heads['mm'] !=0 ){
$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;
// $grand_total=$grand_total + $total;
if($subjects['subject_type'] =='main_subject'){
$grand_total=$grand_total + $total;
}
}else if($exam_type == 'grade'){
if($all_heads){
foreach($all_heads as $exam_heads){
if($exam_heads['exam_mode'] =='yearly'){
$ehid=$exam_heads['id'];    
    
// if($exam_heads['mm'] ==0 || $exam_heads['mm'] ==50){
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 ($total + $total1); ?></td>
<td><?php echo $annual_average= ($total + $total1)/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
}
}
?>    
<?php
if($student_data['class'] ==8){
?>
<tr>
<td colspan="2"></td>
<td><?php echo $total_mm; ?></td>
<td><?php echo $half_yearly_grand_total; ?></td>
<td colspan="5"></td>
<td><?php echo $grand_total; ?></td>
<td><?php 
echo ($grand_total + $half_yearly_grand_total);
// echo "("; echo round(($grand_total * 100)/$total_mm,2); echo "%)";
?></td>
<td>
<?php echo $avg_total=($grand_total + $half_yearly_grand_total)/2;?>
</td>
<td></td>
</tr> 
<?php
}else{
?>
<tr>
<td colspan="2"></td>
<td><?php echo $total_mm; ?></td>
<td><?php echo $half_yearly_grand_total; ?></td>
<td colspan="7"></td>
<td><?php echo $grand_total; ?></td>
<td><?php 
echo ($grand_total + $half_yearly_grand_total);
// echo "("; echo round(($grand_total * 100)/$total_mm,2); echo "%)";
?></td>
<td>
<?php echo $avg_total=($grand_total + $half_yearly_grand_total)/2;?>
</td>
<td></td>
</tr> 
<?php 
}
?>
<?php 
if($student_data['class'] ==8){
?>
<tr>
<td colspan="8">
REMARK:
<?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
}
}
}
?>
</td>
<td colspan="3"></td>
<td colspan="">PERCENTAGE: <?php echo round(($avg_total * 100)/$total_mm,2); ?></td>
<td></td>
</tr>
<?php
}else{

?>
<tr>
<td colspan="8">
REMARK:
<?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
}
}
}
?>
</td>
<td colspan="5"></td>
<td colspan="">PERCENTAGE: <?php echo round(($avg_total * 100)/$total_mm,2); ?></td>
<td></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>


</div>

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