<?php
		$user_id = 1;
		$class_teacher = "none";
		
		$result = $this->customlib->getUserData();
		$role  = strtolower($result["user_type"]);
		$role  = strtolower($role);
		$id    = $result["id"];

		$login_type = $role;
	
//		echo "<script>alert('$login_type $class_id,$section_id');</script>";
	
        if ($login_type == "teacher") {
			$user_id = $this->customlib->getTeacherSessionUserID();	
			$form_link = "teacher/";
			$js_link = "teacher/";
			include "application/scripts/class_teacher.php";		
			$class_teacher = get_teacher($class_id,$section_id);
			$user_id = $id;
        } else {
			$form_link  = "admin/";
			$js_link = "";	
        }


	
	
	///ALLOW Comment
	if($login_type == "admin" || $login_type == "super admin"){
		$allow_head_comment = "";
		$allow_teacher_comment = "";
		$allow_teacher_edit_term = "";
	}
	else{
		$allow_head_comment = "disabled";
		$allow_teacher_comment = "disabled";
		$allow_teacher_edit_term = " style='display:none;' ";
	//	if($user_id == $class_teacher)  //old code
		if(in_array($user_id, $class_teacher)){
			$allow_teacher_comment = "";
		//	echo "<script>alert('is class teacher');</script>";
		}else{
		//	echo "<script>alert('is not class teacher');</script>";
		}
	}	
	////END ALLOW COMMENT	
?>
<div class="content-wrapper" style="min-height: 946px;">
    <section class="content-header">
        <h1>
            <i class="fa fa-map-o"></i> <?php echo $this->lang->line('examinations'); ?> <small><?php echo $this->lang->line('student_fee1'); ?></small> </h1>
<?php 
	//		echo "<pre>"; 
		//	print_r($gradeList);
		//	echo "<hr />";
		//	print_r($className);
		//	echo "<h1>Class: $class_name | Section: $section_name</h1>";
	//		print_r($examSchedule);
		//	print_r($all_exams_get); 
//echo "<hr style='color:red' />";		
	//		print_r($annual_result); 		
		//	print_r($setting); 		
	//		echo "</pre>"; 
			if(isset($annual_result)){
				$_GLOBAL['annual_result'] = $annual_result;
			}
			$all_term_dossier = array();
			$all_annual_dossier = array();
			
			include "application/scripts/result_comment.php";
?>
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <!-- left column -->
            <div class="col-md-12">
                <!-- general form elements -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"><i class="fa fa-search"></i> <?php echo $this->lang->line('select_criteria'); ?></h3>
                        <div class="box-tools pull-right">
                            <?php if ($this->rbac->hasPrivilege('marks_register', 'can_add')) { ?>
                                <a href="<?php echo base_url(); ?>admin/mark/create" class="btn btn-primary btn-sm"  data-toggle="tooltip" title="<?php echo $this->lang->line('add'); ?>" >
                                    <i class="fa fa-plus"></i> <?php echo $this->lang->line('add'); ?>
                                </a>
                            <?php } ?>
                        </div>
                    </div>
                    <form action="<?php echo site_url('admin/mark') ?>"  method="post" accept-charset="utf-8" id="schedule-form">
                        <div class="box-body">
                            <div class="row">
                                <input type="hidden" name="save_exam" value="search" >                               
                                <?php echo $this->customlib->getCSRF(); ?>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="exampleInputEmail1"><?php echo $this->lang->line('exam_name'); ?></label>

                                        <select autofocus="" id="exam_id" name="exam_id" class="form-control" >
                                            <option value=""><?php echo $this->lang->line('select'); ?></option>
                                            <?php
                                            foreach ($examlist as $exam) {
                                                ?>
                                                <option value="<?php echo $exam['id'] ?>" <?php
                                                if ($exam_id == $exam['id']) {
                                                    echo "selected =selected";
													$current_term = $exam['name'];
                                                }
                                                ?>><?php echo $exam['name'] ?></option>
                                                        <?php
                                                        $count++;
                                                    }
                                                    ?>
                                        </select>
                                        <span class="text-danger"><?php echo form_error('exam_id'); ?></span>
                                    </div>
                                </div><!-- /.col -->
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="exampleInputEmail1"><?php echo $this->lang->line('class'); ?></label>
                                        <select  id="class_id" name="class_id" class="form-control" >
                                            <option value=""><?php echo $this->lang->line('select'); ?></option>
                                            <?php
                                            foreach ($classlist as $class) {
                                                ?>
                                                <option value="<?php echo $class['id'] ?>" <?php
                                                if ($class_id == $class['id']) {
                                                    echo "selected =selected";
													$current_class = $class['class'];
                                                }
                                                ?>><?php echo $class['class'] ?></option>

                                                <?php
                                                $count++;
                                            }
                                            ?>
                                        </select>
                                        <span class="text-danger"><?php echo form_error('class_id'); ?></span>
                                    </div>
                                </div><!-- /.col -->
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="exampleInputEmail1"><?php echo $this->lang->line('section'); ?></label>
                                        <select  id="section_id" name="section_id" class="form-control" >
                                            <option value=""><?php echo $this->lang->line('select'); ?></option>
                                        </select>
                                        <span class="text-danger"><?php echo form_error('section_id'); ?></span>
                                    </div>
                                </div><!-- /.col -->
                            </div><!-- /.row -->
                        </div><!-- /.box-body -->
                    </form>
                </div>
                <?php
                if (isset($examSchedule['status'])) {
                    ?>

                    <div class="box box-info">
                        <div class="box-header with-border">
                            <h3 class="box-title">
                                <i class="fa fa-list"></i> <?php echo $this->lang->line('marks_register'); ?></h3>
                        </div>
                        <div class="box-body">
						<?php 							
echo "
<div $allow_teacher_edit_term >
<h3 style='margin:0px;padding:0px;'>Term Settings</h3>";
include "application/scripts/term_setting.php";														
$term_setting = get_exam_settings($session_name,$exam_id);

	if(isset($term_setting['term_started'])){
		$term_started = $term_setting['term_started'];
		$term_ended = $term_setting['term_ended'];
		$next_begin = $term_setting['next_begin'];
		$foot_note = $term_setting['foot_note'];
	}

echo "
Term Started: <input type='date' name='get_term_started' id='get_term_started' value='$term_started' onchange='get_term_started=this.value;' /> &nbsp;
Term Ended: <input type='date' name='get_term_ended' id='get_term_ended' value='$term_ended' onchange='get_term_ended=this.value;'  /> &nbsp;
Next Term Begins: <input type='date' name='get_next_begin' id='get_next_begin' value='$next_begin' onchange='get_next_begin=this.value;' /> &nbsp;
Foot Note: <input type='text' name='get_foot_note' id='get_foot_note' value='$foot_note' onchange='get_foot_note=this.value;' /> &nbsp;
<input type='button' onclick=\"update_term('$session_name','$exam_id')\" value='Update' class='btn btn-primary btn-sm' />
</div>
";
?>												
                            <?php
                            if ($examSchedule['status'] == "yes") {
                                
								
								?>

                                <form role="form" id="" class="" method="post" action="<?php echo site_url('admin/mark/create') ?>">
                                    <?php echo $this->customlib->getCSRF(); ?>
                                    <input type="hidden" name="class_id" value="<?php echo $class_id; ?>">
                                    <input type="hidden" name="section_id" value="<?php echo $section_id; ?>">
                                    <input type="hidden" name="exam_id" value="<?php echo $exam_id; ?>">
                                    <div class="table-responsive">
                                    <div class="download_label"><?php echo $this->lang->line('marks_register'); ?></div>
                                        <table class="table table-striped table-bordered table-hover example">
                                            <thead>
                                                <tr>
                                                    <th>
                                                        <?php echo $this->lang->line('admission_no'); ?>
                                                    </th>
                                                    <th>
                                                        <?php echo $this->lang->line('roll_no'); ?>
                                                    </th>
                                                    <th>
                                                        <?php echo $this->lang->line('student'); ?>
                                                    </th>
                                                    <th>
                                                        <?php echo $this->lang->line('father_name'); ?>
                                                    </th>


                                                    <?php
                                                    $s = 0;
                                                    if ($examSchedule['status'] == "yes") {
                                                        foreach ($examSchedule['result'] as $key => $st) {
                                                            if ($s == 0) {
                                                                foreach ($st['exam_array'] as $key => $exam_schedule) {
                                                                    ?>
                                                                    <th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name'] . "<br/> (" . substr($exam_schedule['exam_type'], 0, 2) . ": " . $exam_schedule['passing_marks'] . "/" . $exam_schedule['full_marks'] . ") ";
                                                                        ?>
                                                                    </th>
                                                                    <?php
                                                                }
                                                            }
                                                            $s++;
                                                        }
                                                    } else {
                                                        ?>

                                                        <?php
                                                    }
                                                    ?>
                                                    <th><?php echo $this->lang->line('grand_total'); ?></th>
                                                    <th><?php echo $this->lang->line('percent') . ' (%)'; ?></th>
                                                    <th><?php echo $this->lang->line('result'); ?></th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                $s = 0;
                                                foreach ($examSchedule['result'] as $key => $student) {
                                                    ?>
                                                <input type="hidden" name="student[]" value="<?php echo $student['student_id'] ?>">

                                                <?php
                                                if (!empty($student['exam_array'])) {
                                                    if ($s == 0) {
                                                        foreach ($student['exam_array'] as $key => $exam_schedule) {
                                                            ?>
                                                            <input type="hidden" name="exam_schedule[]" value="<?php echo $exam_schedule['exam_schedule_id'] ?>">
                                                            <?php
                                                        }
                                                    }
                                                } else {
                                                    ?>

                                                    <?php
                                                }
                                                $s++;
                                            }
                                            ?>
                                            <?php
                                            foreach ($examSchedule['result'] as $key => $student) {
                                                $total_marks = 0;
                                                $obtain_marks = "0";
                                                $result = "Pass";
                                                ?>
                                                <tr>
                                                    <td>
                                                        <?php echo $student['admission_no'] ?>
                                                    </td>
                                                    <td>
                                                        <?php echo $student['roll_no'] ?>
                                                    </td>
                                                    <td>
                                                        <?php echo $student['firstname'] . " " . $student['lastname']; ?>
                                                    </td>
                                                    <td>
                                                        <?php echo $student['father_name'] ?>
                                                    </td>
                                                    <?php
                                                    if (!empty($student['exam_array'])) {
                                                        count($student['exam_array']);
                                                        $s = 0;
                                                        foreach ($student['exam_array'] as $key => $exam_schedule) {
                                                            $total_marks = (int) $total_marks + (int) $exam_schedule['full_marks'];
                                                            ?>
                                                            <td>
                                                                <?php
                                                                if (!isset($exam_schedule['attendence'])) {
                                                                    echo "N/A";
                                                                    $result = "N/A";
                                                                } else {
                                                                    if ($exam_schedule['attendence'] == "pre") {
                                                                        echo $get_marks_student = $exam_schedule['get_marks'];
                                                                        $passing_marks_student = $exam_schedule['passing_marks'];
                                                                        if ($result == "Pass") {
                                                                            if ($get_marks_student < $passing_marks_student) {
                                                                                $result = "Fail";
                                                                            }
                                                                        }
                                                                        $obtain_marks = $obtain_marks + $exam_schedule['get_marks'];
                                                                    } else {
                                                                        $result = "Fail";
                                                                        $s++;
                                                                        echo ($exam_schedule['attendence']);
                                                                    }
                                                                }
                                                                ?>
                                                            </td>
                                                            <?php
                                                        }
                                                        if ($s == count($student['exam_array'])) {
                                                            $obtain_marks = 0;
                                                        }
                                                        ?>
                                                        <td> <?php echo $obtain_marks . " /" . $total_marks; ?> </td>
                                                        <td> <?php
                                                            $per = $obtain_marks * 100 / $total_marks;
                                                            echo number_format($per, 2, '.', '');
                                                            ?>

                                                        </td>
                                                        <th><?php
                                                            if ($result == "Pass") {

                                                                echo "<label class='label label-success'>";
                                                            } else {

                                                                echo "<label class='label label-danger'>";
                                                            }
                                                            echo $result;
                                                            echo "<label/>";
                                                            ?></th>
                                                        <?php
                                                    } else {
                                                        ?>

                                                        <?php
                                                    }
                                                    ?>

                                                </tr>
                                                <?php
                                            }
                                            ?>

                                            </tbody>
                                        </table>
                                    </div>
                                </form>
                                <?php
                            } else {
                                ?>
                                <div class="alert alert-info"><?php echo $this->lang->line('no_record_found'); ?></div>
                                <?php
                            }
                            ?>

                        </div><!---./end box-body--->
                    </div>
                </div>
                <!-- right column -->
            </div>   <!-- /.row -->
            <?php
        } else {
            
        }
        ?>
    </section><!-- /.content -->
</div>
<?php
  function addOrdinalNumberSuffix($num) {
    if (!in_array(($num % 100),array(11,12,13))){
      switch ($num % 10) {
        // Handle 1st, 2nd, 3rd
        case 1:  return $num.'st';
        case 2:  return $num.'nd';
        case 3:  return $num.'rd';
      }
    }
    return $num.'th';
  }
  
function highest($e_id, $exam){ 
$subject_total_array = array();
	for ($i=0;$i<count($exam['result']);$i++){
			for ($j=0;$j<count($exam['result'][$i]['exam_array']);$j++){
				
				$each_student = $exam['result'][$i]['exam_array'][$j];
				if($e_id == $each_student['exam_schedule_id']){
					if($each_student['attendence'] == "pre"){
						$each_student_total = $each_student['ca1'] + $each_student['ca2'] + $each_student['ca3'] + $each_student['get_marks'];	
						$subject_total_array[$i] = $each_student_total;	
					}
				}					
		}
	
	}
	return max($subject_total_array);
}


function lowest($e_id, $exam){ 
$subject_total_array = array();
	for ($i=0;$i<count($exam['result']);$i++){
			for ($j=0;$j<count($exam['result'][$i]['exam_array']);$j++){
				
				$each_student = $exam['result'][$i]['exam_array'][$j];
				if($e_id == $each_student['exam_schedule_id']){
					if($each_student['attendence'] == "pre"){
						$each_student_total = $each_student['ca1'] + $each_student['ca2'] + $each_student['ca3'] + $each_student['get_marks'];	
						$subject_total_array[$i] = $each_student_total;	
					}
				}					
		}
	
	}
	return min($subject_total_array);
}


function average($e_id, $exam){
	$subject_total_array = array();
	for ($i=0;$i<count($exam['result']);$i++){
			for ($j=0;$j<count($exam['result'][$i]['exam_array']);$j++){
				
				$each_student = $exam['result'][$i]['exam_array'][$j];
				if($e_id == $each_student['exam_schedule_id']){
					if($each_student['attendence'] == "pre"){
						$each_student_total = $each_student['ca1'] + $each_student['ca2'] + $each_student['ca3'] + $each_student['get_marks'];	
						$subject_total_array[$i] = $each_student_total;	
					}
				}					
		}
	
	}
	$average = array_sum($subject_total_array)/count($subject_total_array);
	return number_format($average, 2, '.', '');
	
	}
	
	
function position($e_id, $exam, $s_id){
	$subject_total_array = array();
	for ($i=0;$i<count($exam['result']);$i++){
			for ($j=0;$j<count($exam['result'][$i]['exam_array']);$j++){
				
				$each_student = $exam['result'][$i]['exam_array'][$j];
				if($e_id == $each_student['exam_schedule_id']){
					if($each_student['attendence'] == "pre"){
						$each_student_total = $each_student['ca1'] + $each_student['ca2'] + $each_student['ca3'] + $each_student['get_marks'];	
						$subject_total_array[$i] = $each_student_total;	
						$stu_id = (string) $exam['result'][$i]['student_id'];	
						$stu_total = (string) $each_student_total;	
						$student_total[$stu_id] = $stu_total;	
					}
				}					
		}
	
	}
/*		
		echo "$s_id -- <br />";
		echo "<pre>";
		print_r($student_total);
		natsort($student_total);
		print_r($student_total);
		
*/	
	
	$student_total_reset_key = array_values($student_total);
	
	rsort($student_total_reset_key);
	
//	print_r($student_total_reset_key);	

	foreach($student_total as $sid => $stotal){
		$check_position = array_search($stotal, $student_total_reset_key);
		if(($stotal == $student_total_reset_key[$check_position]) && ($sid == $s_id)){
			$position = $check_position + 1;			
			return $position;	
		}
	}
	
}



/*
function class_average($exam){
	$subject_total_array = array();
	$each_student_total = 0;
	$exam_count = count($exam['result'][0]['exam_array']);
	$average_array = array();
	
	for ($i=0;$i<count($exam['result']);$i++){
		$exam_total = 0;
		$subject_count = 0;
			for ($j=0;$j<count($exam['result'][$i]['exam_array']);$j++){				
				$each_student = $exam['result'][$i]['exam_array'][$j];			
				if($each_student['attendence'] == "pre"){
					$each_student_total += $each_student['ca1'] + $each_student['ca2'] + $each_student['ca3'] + $each_student['get_marks'];	
					$exam_total += $each_student['ca1'] + $each_student['ca2'] + $each_student['ca3'] + $each_student['get_marks'];	
					$subject_total_array[$i] = $each_student_total;
					$subject_count += 1;
				}					
			}
		$student_average = $exam_total/$subject_count;	
		$average_array[$i] = $student_average;
	}

	//$average = $each_student_total/(100*$exam_count*count($exam['result'])) * 100;
	$average = array_sum($average_array)/count($exam['result']);

	return number_format($average, 2, '.', '');

	
}

*/

function class_average($exam){
	$subject_total_array = array();
	$all_average_array = array();
	$subject_count = 0;
	for ($i=0;$i<1;$i++){
			for ($j=0;$j<count($exam['result'][$i]['exam_array']);$j++){
				
				$each_student = $exam['result'][$i]['exam_array'][$j];
				$exam_schedule_id = $each_student['exam_schedule_id'];
				$each_average = average($exam_schedule_id, $exam);
				$all_average_array[$exam_schedule_id] = $each_average;
				$subject_count += 1;			
		}

	}
	
	$all_average = array_sum($all_average_array)/$subject_count;
	return number_format($all_average, 2, '.', '');
	
	}

//class_position
	
function class_position($exam, $s_id){
	$subject_total_array = array();
	for ($i=0;$i<count($exam['result']);$i++){
		$each_student_total = 0;
			for ($j=0;$j<count($exam['result'][$i]['exam_array']);$j++){
				
				$each_student = $exam['result'][$i]['exam_array'][$j];
			//	if($e_id == $each_student['exam_schedule_id']){
				if($each_student['attendence'] == "pre"){
				$each_student_total += $each_student['ca1'] + $each_student['ca2'] + $each_student['ca3'] + $each_student['get_marks'];	
				$subject_total_array[$i] = $each_student_total;	
				$stu_id = (string) $exam['result'][$i]['student_id'];	
				$stu_total = (string) $each_student_total;	
				$student_total[$stu_id] = $stu_total;	
				}
			//	}					
		}
	
	}
/*		
		echo "$s_id -- <br />";
		echo "<pre>";
		print_r($student_total);
		natsort($student_total);
		print_r($student_total);
		
*/	
	
	$student_total_reset_key = array_values($student_total);
	
	rsort($student_total_reset_key);
	
//	print_r($student_total_reset_key);	

	foreach($student_total as $sid => $stotal){
		$check_position = array_search($stotal, $student_total_reset_key);
		if(($stotal == $student_total_reset_key[$check_position]) && ($sid == $s_id)){
			$position = $check_position + 1;			
			return $position;	
		}
	}
	
}



//////ANNUAL_CALCULATIONS

function subject_term_total($annual_result, $term_key, $this_student_id, $subject_key){
	foreach($annual_result[$term_key] as $student_key => $student_details){
		if($student_details['student_id'] == $this_student_id){
			foreach($student_details['exam_array'] as $sub_key => $sub_details){
				if($sub_key == $subject_key){
					if($sub_details['attendence'] == "pre"){
					$total_subject_score = $sub_details['ca1'] + $sub_details['ca2'] + $sub_details['ca3'] + $sub_details['get_marks'];
					return $total_subject_score;
					}
				}
				
			}	
			break;
		}
		
	}
	
}




function subject_annual_total($annual_result, $this_student_id, $subject_key){
	$total_subject_score = 0;
	foreach($annual_result as $term_key => $term_details){
		foreach($annual_result[$term_key] as $student_key => $student_details){
			if($student_details['student_id'] == $this_student_id){			
				foreach($student_details['exam_array'] as $sub_key => $sub_details){
					if($sub_key == $subject_key){
						$total_subject_score += $sub_details['ca1'] + $sub_details['ca2'] + $sub_details['ca3'] + $sub_details['get_marks'];						
					}					
				}	
				break;
			}
		}	
	}


return $total_subject_score;
	
}


function subject_highest($annual_result, $subject_key, $min_max){
	
	$scores_array = array();
	
	
	foreach($annual_result as $term_key => $term_details){
		
		foreach($annual_result[$term_key] as $student_key => $student_details){
			//if($student_details['student_id'] == $this_student_id){	
				$total_subject_score = 0;
				foreach($student_details['exam_array'] as $sub_key => $sub_details){
					if($sub_key == $subject_key){
						$total_subject_score += $sub_details['ca1'] + $sub_details['ca2'] + $sub_details['ca3'] + $sub_details['get_marks'];
						$scores_array[$student_details['student_id']] += $total_subject_score;						
					}					
				}	
			//	break;
			//}
		}	
		
	
	}


if($min_max == "max")
{return max($scores_array);}
if($min_max == "min")
{return min($scores_array);}
	
}


function subject_average($annual_result, $subject_key){
	$scores_array = array();	
	foreach($annual_result as $term_key => $term_details){
		
		foreach($annual_result[$term_key] as $student_key => $student_details){
			//if($student_details['student_id'] == $this_student_id){	
				$total_subject_score = 0;
				foreach($student_details['exam_array'] as $sub_key => $sub_details){
					if($sub_key == $subject_key){
						$total_subject_score += $sub_details['ca1'] + $sub_details['ca2'] + $sub_details['ca3'] + $sub_details['get_marks'];
						$scores_array[$student_details['student_id']] += $total_subject_score;						
					}					
				}	
			//	break;
			//}
		}	
		
	
	}
//////DIVIDE BY UPLOADED SUBJECTS
	//$average = array_sum($scores_array)/count($scores_array)/count($annual_result);
	$average = array_sum($scores_array)/count($scores_array)/3;
	return $average;

	
}

function subject_position($annual_result, $this_student_id, $subject_key){
	
	$scores_array = array();
	
	
	foreach($annual_result as $term_key => $term_details){
		
		foreach($annual_result[$term_key] as $student_key => $student_details){
			//if($student_details['student_id'] == $this_student_id){	
				$total_subject_score = 0;
				foreach($student_details['exam_array'] as $sub_key => $sub_details){
					if($sub_key == $subject_key){
						$total_subject_score += $sub_details['ca1'] + $sub_details['ca2'] + $sub_details['ca3'] + $sub_details['get_marks'];
						$scores_array[$student_details['student_id']] += $total_subject_score;						
					}					
				}	
			//	break;
			//}
		}	
		
	
	}
	
	$student_total = $scores_array;
	
	
	$student_total_reset_key = array_values($student_total);
	
	rsort($student_total_reset_key);
	
//	print_r($student_total_reset_key);	

	foreach($student_total as $sid => $stotal){
		$check_position = array_search($stotal, $student_total_reset_key);
		if(($stotal == $student_total_reset_key[$check_position]) && ($sid == $this_student_id)){
			$position = $check_position + 1;			
			return $position;
			break;
		}
	}	



	
}




function annual_total($annual_result, $this_student_id){
	$total_subject_score = 0;
	foreach($annual_result as $term_key => $term_details){
		foreach($annual_result[$term_key] as $student_key => $student_details){
			if($student_details['student_id'] == $this_student_id){			
				foreach($student_details['exam_array'] as $sub_key => $sub_details){
				//	if($sub_key == $subject_key){
						$total_subject_score += $sub_details['ca1'] + $sub_details['ca2'] + $sub_details['ca3'] + $sub_details['get_marks'];						
				//	}					
				}	
				break;
			}
		}	
	}


return $total_subject_score;
	
}

function annual_class_average($annual_result){
	$total_subject_score = 0;
	foreach($annual_result as $term_key => $term_details){
		foreach($annual_result[$term_key] as $student_key => $student_details){
				foreach($student_details['exam_array'] as $sub_key => $sub_details){
						$total_subject_score += $sub_details['ca1'] + $sub_details['ca2'] + $sub_details['ca3'] + $sub_details['get_marks'];										
				}	
		}	
	}


return ($total_subject_score/count($annual_result[0]));
}



function annual_position($annual_result, $this_student_id){
	$scores_array = array();
	foreach($annual_result as $term_key => $term_details){
		
		foreach($annual_result[$term_key] as $student_key => $student_details){
				$total_subject_score = 0;
				foreach($student_details['exam_array'] as $sub_key => $sub_details){
						$total_subject_score += $sub_details['ca1'] + $sub_details['ca2'] + $sub_details['ca3'] + $sub_details['get_marks'];
															
				}
			$scores_array[$student_details['student_id']] += $total_subject_score;	
		}	
		
	
	}
	
	$student_total = $scores_array;	
	
	$student_total_reset_key = array_values($student_total);
	
	rsort($student_total_reset_key);
	
//	print_r($student_total_reset_key);	

	foreach($student_total as $sid => $stotal){
		$check_position = array_search($stotal, $student_total_reset_key);
		if(($stotal == $student_total_reset_key[$check_position]) && ($sid == $this_student_id)){
			$position = $check_position + 1;			
			return $position;
			break;
		}
	}		
}

echo "
<script>alert('";
var_dump($className);
echo "
');</script>
";



?>


<?php
function get_base_64($url){
	$file_content = file_get_contents($url);
	$file_content_base64 = base64_encode($file_content);
	return "data:image/png;base64," . $file_content_base64;
	//return $url;
}




?>

<style>
#dialog {
position:fixed;
top:0px;
left:0px;
width:100%;
height:100%;
background: url('<?php echo base_url()?>/backend/images/fading.png');
display:none;
z-index:100000;
}

#dialog_box {
width:50%;
height:50%;
background:white;
overflow: auto;
position:absolute;
border-radius:10px 10px;
border:4px green dotted;
z-index:100002;
}

#dialog_head{
opacity:1;
}
</style>

<div id="dialog">

<div id="dialog_box">
<h1 style="margin:0px;text-align:center;" id="dialog_head"></h1>
<div id="dialog_body">

</div>
<center><button class='btn btn-primary btn-sm' onclick="close_dialog()">CLOSE</button></center>
</div>

</div>

<div id='result_response'>

</div>


<div id='comment_response' style='display:none;'></div>
<div id='term_response' style='display:none;'></div>

<datalist id="comments">
  <option value="Keep it up!">
  <option value="Good job!">
  <option value="Try harder next time!">
  <option value="Sit up for better performance!">
  <option value="Good result, can do better.">
  <option value="Congratulations, can be better still.">
  <option value="Good result, can do better.">
  <option value="You can do better!">
  <option value="Excellent result!">
  <option value="Congratulations!">
  <option value="Can do better!">
  <option value="Bravo!">
  <option value="Wonderful Performance!">
 
<?php

	include "application/scripts/comment.php";
?> 
</datalist> 

<script>
function termComment(student_session,current_term,this_student_id,type,this_comment){
	var base_url = '<?php echo base_url() ?>';
	var update_url = base_url + 'application/scripts/update_comment.php';
	$(document).ready(function(){

	  $.post(update_url,{student_session:student_session,current_term:current_term,this_student_id:this_student_id,type:type,this_comment:this_comment},function(result){
	   $("#comment_response").html(result);
		
	  });

	});
	
}
</script>


<script>
function update_term(student_session,current_term){
	var base_url = '<?php echo base_url() ?>';
	var update_url = base_url + 'application/scripts/update_term.php';
	$(document).ready(function(){
	get_term_started = $('#get_term_started').val();
	get_term_ended = $('#get_term_ended').val();
	get_next_begin = $('#get_next_begin').val();
	get_foot_note = $('#get_foot_note').val();
		
	//alert('Updating Term' + term_started + term_ended + next_begin + foot_note);
	
	  $.post(update_url,{student_session:student_session,current_term:current_term,term_started:get_term_started,term_ended:get_term_ended,next_begin:get_next_begin,foot_note:get_foot_note},function(result){
	   $("#term_response").html(result);
	   
	 //  alert('Updated term details');
		
	  });	
	  
	});
	
}
</script>
<script>
function dialog(head,body){
//$("#dialog").slideDown();
$("#dialog").fadeIn();
$("#dialog_head").html(head);
$("#dialog_body").html(body);
//$("#dialog_box").css("width", "200px");
//alert(document.body.getBoundingClientRect().width);
window_width = document.body.getBoundingClientRect().width;
box_width = Math.round(7/10 * window_width);
box_position_x = Math.round(window_width-box_width)/2;


window_height = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;

//window_height = document.body.getBoundingClientRect().height;
box_height = Math.round(9/10 * window_height);
box_position_y = Math.round(window_height-box_height)/2;


if(window_width < 400){box_width = window_width; box_position_x = 0;}
if(window_height < 300){box_height = window_height; box_position_y = 0;}

//$("#dialog").css("display", "");
$("#dialog_box").css("position", "fixed");

$("#dialog_box").css("width", box_width + "px");
$("#dialog_box").css("left", box_position_x + "px");

$("#dialog_box").css("height", box_height + "px");
$("#dialog_box").css("top", box_position_y + "px");


}

function close_dialog(){
$("#dialog").fadeOut();
}
</script>
<script type="text/javascript">
    function getSectionByClass(class_id, section_id) {
        if (class_id != "" && section_id != "") {
            $('#section_id').html("");
            var base_url = '<?php echo base_url() ?>';
            var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
            $.ajax({
                type: "GET",
                url: base_url + "sections/getByClass",
                data: {'class_id': class_id},
                dataType: "json",
                success: function (data) {
                    $.each(data, function (i, obj)
                    {
                        var sel = "";
                        if (section_id == obj.section_id) {
                            sel = "selected";
                        }
                        div_data += "<option value=" + obj.section_id + " " + sel + ">" + obj.section + "</option>";
                    });
                    $('#section_id').append(div_data);
                }
            });
        }
    }

    $(document).ready(function () {
        $(document).on('change', '#class_id', function (e) {
            $('#section_id').html("");
            var class_id = $(this).val();
            var base_url = '<?php echo base_url() ?>';
            var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
            $.ajax({
                type: "GET",
                url: base_url + "sections/getByClass",
                data: {'class_id': class_id},
                dataType: "json",
                success: function (data) {
                    $.each(data, function (i, obj)
                    {
                        div_data += "<option value=" + obj.section_id + ">" + obj.section + "</option>";
                    });
                    $('#section_id').append(div_data);
                }
            });
        });
        var class_id = $('#class_id').val();
        var section_id = '<?php echo set_value('section_id') ?>';
        getSectionByClass(class_id, section_id);
        $(document).on('change', '#feecategory_id', function (e) {
            $('#feetype_id').html("");
            var feecategory_id = $(this).val();
            var base_url = '<?php echo base_url() ?>';
            var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
            $.ajax({
                type: "GET",
                url: base_url + "feemaster/getByFeecategory",
                data: {'feecategory_id': feecategory_id},
                dataType: "json",
                success: function (data) {
                    $.each(data, function (i, obj)
                    {
                        div_data += "<option value=" + obj.id + ">" + obj.type + "</option>";
                    });
                    $('#feetype_id').append(div_data);
                }
            });
        });
    });
    $(document).on('change', '#section_id', function (e) {
        $("form#schedule-form").submit();
    });
</script>
<script>

$(document).ready(function(){
    $("tr").dblclick(function(){
	   $("tr").css('background-color', '');
	   $(this).css('background-color', 'lime');
    });
}); 

</script>

<script>			
    function upload_result(admission_no,student_name,result_data_id,session,sclass,arm,term) {
	//alert('hello');
	dialog('Upload result','<center>Uploading' + term + ' result for' + admission_no + ' ...' + '</center>'); 
	var result_data = document.getElementById(result_data_id).innerHTML;
	//	$(document).ready(function(){
				  $.post("https://fajudeacademy.com/update_result/",{
					  admission_no:admission_no,
					  student_name:student_name,
					 sclass:sclass,
					 arm:arm,
					 session:session,
					 term:term,
					  result_data:result_data
					  },function(result){
					$("#result_response").html(result);
				  });
	//	});
	
	}
</script>			
<script>
function download(content,filename,filetype){
var file_type = "data:" + filetype +";base64,";	
var encode_content = btoa(content);
var url_link = file_type + encode_content;
var element = document.createElement('a');
element.setAttribute('href', url_link);
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
</script>