<?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";
	
////// CLASS RESULT LABELS	
	include "application/scripts/get_classlabel.php";
	$head_address = "";
	$teacher_address = "";
	$student_address= "";
	$get_classlabel_data = get_classlabel($class_id,$section_id);
	
	if(!empty($get_classlabel_data)){
		$head_address = $get_classlabel_data['head_address'];
		$teacher_address = $get_classlabel_data['teacher_address'];
		$student_address = $get_classlabel_data['student_address'];	
	}
	
	//echo "<pre>"; print_r($get_classlabel_data); echo "</pre>";			
?>

    </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 $student_address; ?>
                                                    </th>
                                                    <th>
                                                        Gender
                                                    </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/>" . "C. A 1";
                                                                        ?>
                                                                    </th>
                                                                    <th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . "C. A 2";
                                                                        ?>
                                                                    </th>																	
                                                                   <th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . "C. A 3";
                                                                        ?>
                                                                    </th> 
																	<th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . "EXAM";
                                                                        ?>
                                                                    </th>
                                                                    <th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name'] . "<br /> TOTAL";
                                                                        ?>
                                                                    </th>	
																	<th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . "HIGHEST";
                                                                        ?>
                                                                    </th>	
																	<th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . "LOWEST";
                                                                        ?>
                                                                    </th>
																	<th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . "AVERAGE";
                                                                        ?>
                                                                    </th>	
																	<th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . "POSITION";
                                                                        ?>
                                                                    </th>
																	<th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . "GRADE";
                                                                        ?>
                                                                    </th>																	
	
                                                                    <?php
                                                                }
                                                            }
                                                            $s++;
                                                        }
                                                    } else {
                                                        ?>

                                                        <?php
                                                    }
                                                    ?>
                                                    <th><?php echo $this->lang->line('grand_total'); ?></th>
                                                    <th>Grand Obtainable</th>
                                                    <th><?php echo $this->lang->line('percent') . ' (%)'; ?></th>
                                                    <th>CLASS AVERAGE</th>
                                                    <th>POSITION</th>
                                                    <th>EDIT</th>
                                                    <th>PRINT</th>
                                                    <th>DOWNLOAD</th>
                                                    <th>UPLOAD</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) {
												$dossier = "<div style='margin:30px;'><table border='1' style='width:100%;border-spacing: 0;border-collapse: collapse;border-width: thin;border-style: inset;'>";
												
												$dossier .= "
												<tr style='font-weight:bold;'>
												<td>SUBJECTS</td>
												<td>CA 1</td>
												<td>CA 2</td>
												<td>CA 3</td> 
												<td>EXAM</td>
												<td>TOTAL</td>
												<td>HIGHEST</td>
												<td>LOWEST</td>
												<td>AVERAGE</td>
												<td>POSITION</td>
												<td>GRADE</td>
												<td>REMARK</td>
												</tr>
												";
												
																								
                                                $total_marks = 0;
                                                $obtain_marks = "0";
                                                $result = "Pass";
                                                ?>
                                                <tr>
                                                    <td>
                                                        <?php echo $student['admission_no'] ?>
                                                    </td>
                                                    <td>
                                                        <?php echo $student['roll_no'] ?>
                                                    </td>
                                                    <td>
                                                    <a href="<?php echo base_url();?>/student/view/<?php echo $student['student_id'];?>" target="_blank">    <?php echo $student['firstname'] . " " . $student['lastname']; ?> </a>
                                                    </td>
                                                    <td>
                                                        <?php echo $student['gender'] ?>
													<?php
													///OTHER STUDENT DETAILS 
												//	echo "<pre>";
												//	echo $student['gender'];
												//	print_r($student['current_details']);
												//	echo $student['current_details']['class'] . $student['current_details']['section'];
												//	echo "</pre>";
													///END OTHER STUDENT DETAILS	
				//echo "<img src='" . base_url() . "/" . $student['current_details']['image'] ."' width='100px'>";
													?>															
                                                    </td>
                                                    <?php
                                                    if (!empty($student['exam_array'])) {
														$grand_total_mark = 0;
                                                        count($student['exam_array']);
                                                        $s = 0;
														$subject_offered = 0;
                                                        foreach ($student['exam_array'] as $key => $exam_schedule) {
															$dossier_ori = $dossier;
															$dossier = "";																
															
															$dossier .= "<tr>";
															$dossier .= "<td>". $exam_schedule['exam_name'] . "</td>";															
                                                            $total_marks = (int) $total_marks + (int) $exam_schedule['full_marks'];
                                                            ?>
															<td>
															<?php echo $exam_schedule['ca1']; ?>
															<?php $dossier .= "<td>". $exam_schedule['ca1'] . "</td>"; ?>
															</td>
															<td>
															<?php echo $exam_schedule['ca2']; ?>
															<?php $dossier .= "<td>". $exam_schedule['ca2'] . "</td>"; ?>
															</td>
															<td>
															<?php echo $exam_schedule['ca3']; ?>
															<?php 
															$dossier .= "<td>". $exam_schedule['ca3'] . "</td>"; 
															?>
															</td>
														
															<td>
															<?php echo $exam_schedule['get_marks']; ?>
															<?php $dossier .= "<td>". $exam_schedule['get_marks'] . "</td>"; ?>
															</td>															
                                                            <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'];
                                                                        echo $get_marks_student = $exam_schedule['ca1'] +  $exam_schedule['ca2'] +  $exam_schedule['ca3'] +  $exam_schedule['get_marks'];
																		///SET MARK GRADE
																		$grade_name = "";
																		$grade_desc = "";
																		foreach($gradeList as $grade => $grade_details){
																			if(($get_marks_student >= $grade_details['mark_from']) && ($get_marks_student <= $grade_details['mark_upto'])){
																			//	echo $grade_details['name'];
																				$grade_name = $grade_details['name'];
																				$grade_desc = $grade_details['description'];
																			}
																			
																			if(($get_marks_student > 100) || ($get_marks_student < 0)){
																			//	echo $grade_details['name'];
																				$grade_name = "<label class='label label-danger'>Out of range</label>";
																				$grade_desc = "<label class='label label-danger'>Out of range</label>";
																			}
																			
																			
																		}
																		
																		///END SET MARK GRADE
																		
																		$grand_total_mark += $get_marks_student;
                                                                        $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 $dossier .= "<td>". $get_marks_student . "</td>"; ?>
															<td>
															<?php 
															$highest = highest($exam_schedule['exam_schedule_id'], $examSchedule);
															echo $highest;
															?>
															<?php $dossier .= "<td>". $highest . "</td>"; ?>
															</td>	
															<td>
															<?php 
															$lowest = lowest($exam_schedule['exam_schedule_id'], $examSchedule);
															echo $lowest;
															?>
															<?php $dossier .= "<td>". $lowest . "</td>"; ?>															
															</td>	
															<td>
															<?php 															
															$average = average($exam_schedule['exam_schedule_id'], $examSchedule); 
															echo $average;
															?>
															<?php $dossier .= "<td>". $average . "</td>"; ?>	
															</td>	
															<td>
															<?php 
															$position = position($exam_schedule['exam_schedule_id'], $examSchedule, $student['student_id']);
															if($position !=""){
																echo $position;
															}
															$position = addOrdinalNumberSuffix($position);
															?>
															<?php $dossier .= "<td>". $position . "</td>"; ?>
															</td>
															<td>
															<?php echo $grade_name; ?>
															</td>
															<?php $dossier .= "<td>". $grade_name . "</td>"; ?>
															<?php $dossier .= "<td>". $grade_desc . "</td>"; ?>
                                                            <?php
															
														$dossier .= "</tr>";	
														
															if($exam_schedule['attendence'] == "pre"){
																$dossier = $dossier_ori . $dossier;	
																$subject_offered += 1;
															}else{
																$dossier = $dossier_ori;
															}
                                                        }
                                                        if ($s == count($student['exam_array'])) {
                                                            $obtain_marks = 0;
                                                        }
                                                        ?><?php

													$dossier .= "</table></div>";	
													
													?>
														
                                                     <!--   <td> <?php echo $obtain_marks . " /" . $total_marks; ?> </td> -->													 
                                                        <td> <?php echo $grand_total_mark; ?> </td>													
                                                        <td> <?php echo $total_marks; ?> </td>																									
                                                        <td> 
														<?php
                                                            $per = $grand_total_mark/$subject_offered;
                                                            $average = number_format($per, 2, '.', '');
                                                            echo $average;
                                                         ?>
														</td>
														<td>														
														<?php 
														$class_average = class_average($examSchedule);
														echo $class_average;
														?>
														</td>														
														</td>
														<td>
														<?php 
														$class_position = class_position($examSchedule, $student['student_id']); 
														if($class_position !=""){
														echo $class_position;
														}
														$class_position = addOrdinalNumberSuffix($class_position);
														?>														
														</td>
														<td><button class='btn btn-primary btn-sm'>EDIT</button></td>
														<?php 
														$student_count = count($examSchedule['result']);
														$subject_count = count($examSchedule['result'][0]['exam_array']);
														$student_admission_no = $student['admission_no'];
														$student_firstname = $student['firstname'];
														$student_lastname = $student['lastname'];
														$x_student_id = $student['student_id'];
														$student_class = $class_name;
														$student_section = $section_name;
														$student_session = $student['session_name'];
													//	$student_sex = $examSchedule['result'][$x_student_id]['current_details']['gender'];
														$student_sex = $student['gender'];
														$student_image = $student['current_details']['image'];
														$setting_name = $setting['name'];
														$setting_email = $setting['email'];
														$setting_phone = $setting['phone'];
														$setting_address = $setting['address'];
														$setting_image = $setting['image'];
														$student_c1 = $student['character']['c1'];
														$student_c2 = $student['character']['c2'];
														$student_c3 = $student['character']['c3'];
														$student_c4 = $student['character']['c4'];
														$student_c5 = $student['character']['c5'];
														$student_c6 = $student['character']['c6'];
														$student_c7 = $student['character']['c7'];
														$student_c8 = $student['character']['c8'];
														$student_c9 = $student['character']['c9'];
														$student_c10 = $student['character']['c10'];
														$student_c11 = $student['character']['c11'];
														$student_c12 = $student['character']['c12'];
														$student_c13 = $student['character']['c13'];
														$student_c14 = $student['character']['c14'];
														$student_c15 = $student['character']['c15'];
														$student_c16 = $student['character']['c16'];
														$student_c17 = $student['character']['c17'];
														$student_c18 = $student['character']['c18'];
														$student_c19 = $student['character']['c19'];
														$student_c20 = $student['character']['c20'];
														$student_c21 = $student['character']['c21'];
														$student_c22 = $student['character']['c22'];
														$student_c23 = $student['character']['c23'];
														$student_c24 = $student['character']['c24'];
													//	$student_image = "<img src='" . base_url() . "/" . $student['current_details']['image'] ."' width='100px'>";													
														?>
														<?php
														$school_logo = get_base_64(base_url() . "/uploads/school_content/logo/" . $setting_image);	
														$student_image = get_base_64(base_url() . "/" . $student_image);	
														$school_head_signature = get_base_64(base_url() . "/uploads/signatures_images/sign_0". "_" . ".jpg");	
														$class_teacher_signature = get_base_64(base_url() ."/uploads/signatures_images/sign_$class_id"."_$section_id". ".jpg");	
														
														$this_student_id = $student['student_id'];
														$comments = get_comment($student_session,$exam_id,$this_student_id);
													//	$comments = array("head_comment" => "Head Comment", "teacher_comment" => "Teacher Comment");
														
														$head_comment = $comments['head_comment'];
														$teacher_comment = $comments['teacher_comment'];
														
														$dossier_header = "
														<div style='margin:30px; padding:10px; border:1px black solid;'>
														<table style='width:100%;text-align:center;'>
														<tr>
														<td width='15%'>
														<img src='$school_logo' width='100px' border='1' />
														</td>
														<td width='70%'>
														<h1 style='margin:0px;padding:0px;'>$setting_name</h1>
														<h3 style='margin:0px;padding:0px;'>$setting_address</h3>
														<h4 style='margin:0px;padding:0px;'>$setting_phone</h4>
														<h4 style='margin:0px;padding:0px;'>$setting_email</h4>
														
														</td>
														<td width='15%'>
														<img src='$student_image' width='120px' border='1' />
														</td>
														</tr>														
														</table>
														</div>
														<center><h2 style='margin:0px;padding:0px;'>Termly Continuous Assessment Dossier</h2></center>
														";
														
														
														$dossier_top = "
														<div style='margin:30px; padding:10px; border:1px black solid;'>
														<strong>Admission No:</strong> $student_admission_no &nbsp;&nbsp;&nbsp;
														<strong>$student_address's Name:</strong> $student_lastname $student_firstname &nbsp;&nbsp;&nbsp;
														<br />
														<br />
														<strong>Term:</strong> $current_term &nbsp;&nbsp;&nbsp;
														<strong>Class:</strong> $student_class$student_section&nbsp;&nbsp;&nbsp;
														<strong>Session:</strong> $student_session&nbsp;&nbsp;&nbsp;
														<strong>Sex:</strong> $student_sex&nbsp;&nbsp;&nbsp;
														
														</div>
														";
														

														$dossier .= "
														<div style='margin:30px'>
														<strong>Obtainable marks:</strong> $total_marks &nbsp;&nbsp;&nbsp; 
														<strong>Score obtained:</strong> $grand_total_mark &nbsp;&nbsp;&nbsp; 
														<strong>Average:</strong> $average &nbsp;&nbsp;&nbsp; 
														<strong>Class Average:</strong> $class_average &nbsp;&nbsp;&nbsp; 
														<strong>Position:</strong> $class_position Out of $student_count &nbsp;&nbsp;&nbsp; 
														<br />
														<strong>Subjects:</strong> $subject_offered &nbsp;&nbsp;&nbsp;
														<br />
														
														<div style='text-align:center;'>
														<strong>Term Started: </strong><span class='term_started'>$term_started</span> &nbsp;&nbsp;&nbsp;&nbsp;<strong>Term Ended:</strong><span class='term_ended'>$term_ended</span>&nbsp;&nbsp;&nbsp;&nbsp;<strong>Next Term Begins:</strong><span class='next_begin'>$next_begin</span>
														</div>
														</div>														
														
														";	
														
														$dossier .= "
														<div style='margin:30px'>
														<div style='margin:30px;border:1px solid black;text-align:center;'>
														<strong>
														Rating: 5 - Excellent, 4 - Very good, 3 - Good, 2 - Fair, 1 - poor
														</strong>
														</div>
<table border='1px' style='width:100%;border-spacing: 0;border-collapse: collapse;border-width: thin;border-style: inset;'>	
<tr><td>Punctuality</td><td>$student_c1</td><td>Neatness</td><td>$student_c2</td><td>Peer relations</td><td>$student_c3</td><td>Hand writing</td><td>$student_c4</td><td>Attitude to study</td><td>$student_c5</td></tr>	
<!--												
<tr><td>Attitude to study</td><td>$student_c6</td><td>Initiative</td><td>$student_c7</td><td>Spirit of Co-operation</td><td>$student_c8</td><td>Musical Skill</td><td>$student_c9</td><td>Craft</td><td>$student_c10</td></tr>													
<tr><td>Politeness</td><td>$student_c11</td><td>Relationship with Teacher</td><td>$student_c12</td><td>Tolerance</td><td>$student_c13</td><td>Hand Writing</td><td>$student_c14</td><td>Fluency</td><td>$student_c15</td></tr>													
<tr><td>Attentiveness</td><td>$student_c16</td><td>Respect for others</td><td>$student_c17</td><td>Self Control</td><td>$student_c18</td><td>Courage</td><td>$student_c19</td><td>Humility</td><td>$student_c20</td></tr>													
<tr><td>Leadership</td><td>$student_c21</td><td>Honesty</td><td>$student_c22</td><td>Openness</td><td>$student_c23</td><td>Kindness</td><td>$student_c24</td>
</tr>
-->													
</table>
<br />														
<div style='border:1px solid black;text-align:center;'>
<strong>Term Total Grading:</strong> A+ (95-100), A (80-94), B+ (70 -79), B (60-69), C (50-59), D (40-49), E (0-39)
</div>														
														</div>														
														
														";	
														
														$dossier .= "

														<div style='margin-left:30px;margin-right:30px;text-align:;'>
														<table style='width:100%;'>
														<tr>
														<td style='width:50%;'>
														<strong>
														$head_address's Comment and Sign
														</strong>	
														<br />
														<input $allow_head_comment list='comments' style=\"font-style:italic; width:100%; border:0px;\" 
														onchange=\"termComment('$student_session','$exam_id','$this_student_id','head',this.value)\" 
														id='term_head_$this_student_id' class='term_head_$this_student_id'
														value=\"$head_comment\">
														<br />														
														<img src='$school_head_signature' width='80px' />
														<br />
														</td>
														
														<td>
														
														<strong>
														$teacher_address's Comment and Sign
														</strong>
														<br />
														<input $allow_teacher_comment list='comments' style=\"font-style:italic; width:100%; border:0px;\" 
														onchange=\"termComment('$student_session','$exam_id','$this_student_id','teacher',this.value)\" 
														id='term_teacher_$this_student_id' class='term_teacher_$this_student_id'
														value=\"$teacher_comment\">
														<br />	
														<img src='$class_teacher_signature' width='80px' />
														<br />
														</td>
														</tr>
														</table>
														</div>
														<div class='foot_note' style='text-align:center; font-style:italic;'>
														$foot_note
														</div>
														";
														?>
														
														
														
														
														
														<?php
														
													
														
														
														$display_id = $student['student_id']. "_term";
														$draw_dossier = "
														<div>
														$dossier_header
														$dossier_top
														$dossier
														
														<center>
														<input type='button' class='btn btn-primary btn-sm print_button_class' style='' id='print_button' value='PRINT' onclick=\"if(document.getElementById('$display_id')){}else{this.style.display = 'none';window.print();} content = document.getElementById('$display_id').innerHTML; x=window.open(); x.document.open;  x.document.write(content + '<script>window.print();</script>'); x.document.close(); \">
														</center>
														
														<script>
														
														if(document.getElementById('$display_id'))
														{

														}else{
															print_buttons = document.getElementsByClassName('print_button_class');
														for(pc in print_buttons){
																print_buttons[pc].style.display = 'none';
															//	alert(pc);
															}
														}	
														</script>
														</div>
														";
														
														echo "
														<td>
														<div style='display:none;' id='$display_id'>
														$draw_dossier
														</div>
														<input type='button' class='btn btn-primary btn-sm' value='VIEW' onclick=\"dialog('',document.getElementById('$display_id').innerHTML);\">		
														</td>
														<td>
<button class='btn btn-primary btn-sm' onclick=\"content = document.getElementById('$display_id').innerHTML; download(content,'$student_lastname $student_firstname $student_class$student_section $current_term  RESULT.html','text/html'); return false; \">DOWNLOAD</button>
														</td>														
														<td>
														<input $allow_teacher_comment type='button' class='btn btn-primary btn-sm' value='UPLOAD' 
														onclick=\"upload_result('$student_admission_no','$student_lastname $student_firstname','$display_id','$student_session','$student_class','$student_section','$exam_id'); \">															
														</td>
														";
////ALL DOSSSIER DETAILS
$all_term_dossier["$student_admission_no"] = 	$draw_dossier;	
$all_pin["$student_admission_no"] = "<td>$student_admission_no</td><td>$student_lastname $student_firstname </td><td>" . base64_encode($student_admission_no) . "</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>
<?php 
$all_term_dossier_print = implode("<div style='page-break-after: always'><span style='display:none'>&nbsp;</span></div>", $all_term_dossier);

echo "<div id='all_term_dossier_print' style='display:none;'>$all_term_dossier_print</div>";

?>		
							
<button class='btn btn-primary btn-sm' onclick="content = document.getElementById('all_term_dossier_print').innerHTML; x=window.open(); x.document.open;  x.document.write('<html><body>' + content + '<script>window.print();</script>' + '</body></html>'); x.document.close(); return false;">PRINT ALL TERM RESULT</button>
<button class='btn btn-primary btn-sm' onclick="content = document.getElementById('all_term_dossier_print').innerHTML; download(content,'<?php echo "$student_class$student_section"."_RESULT.html"; ?>','text/html'); return false; ">DOWNLOAD ALL RESULT</button>
<div id='all_pins' style='display:none;'>
<center><h3>Online Result Pins for <?php echo "$student_class$student_section"; ?></h3></center>
<table border='1' width='100%'>
<tr>
<th>Admission No.</th>
<th>Name</th>
<th>Pin</th>
</tr>
<tr>
<?php echo implode('</tr><tr>', $all_pin); ?>
</tr>
</table>
<center><button onclick='window.print();'>PRINT</button></center>
</div>

<button class='btn btn-primary btn-sm' onclick="content = document.getElementById('all_pins').innerHTML; x=window.open(); x.document.open;  x.document.write('<html><body>' + content + '<script>window.print();</script>' + '</body></html>'); x.document.close(); return false;">PRINT ALL PINS</button>


										
<!--- Annual -->  <!--- Annual --> <!--- Annual --> <!--- Annual --> <!--- Annual -->
                        <div class="box-header with-border">
                            <h3 class="box-title">
                                <i class="fa fa-list"></i> ANNUAL REPORT</h3>
                        </div>
							<div class="table-responsive">
									<div class="download_label">Annual Report</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 $student_address; ?>
                                                    </th>
                                                    <th>
                                                        <?php echo $this->lang->line('gender'); ?>
                                                    </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/>" . "1ST TERM";
                                                                        ?>
                                                                    </th>
                                                                    <th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . "2ND TERM";
                                                                        ?>
                                                                    </th>																	
                                                                    <th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . "3RD TERM";
                                                                        ?>
                                                                    </th>
                                                                    <th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name'] . "<br /> ANNUAL TOTAL";
                                                                        ?>
                                                                    </th>	
																	<th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . " ANNUAL HIGHEST";
                                                                        ?>
                                                                    </th>	
																	<th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . "ANNUAL LOWEST";
                                                                        ?>
                                                                    </th>
																	<th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . " ANNUAL AVERAGE";
                                                                        ?>
                                                                    </th>	
																	<th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . " ANNUAL POSITION";
                                                                        ?>
                                                                    </th>
																	<th>
                                                                        <?php
                                                                        echo $exam_schedule['exam_name']. "<br/>" . " ANNUAL GRADE";
                                                                        ?>
                                                                    </th>																	
																	
                                                                    <?php
                                                                }
                                                            }
                                                            $s++;
                                                        }
                                                    } else {
                                                        ?>

                                                        <?php
                                                    }
                                                    ?>
                                                    <th>ANNUAL GRAND TOTAL</th>
                                                    <th>ANNUAL Grand Obtainable</th>
                                                    <th><?php echo $this->lang->line('percent') . ' (%)'; ?></th>
                                                    <th>CLASS AVERAGE</th>
                                                    <th>POSITION</th>
                                                    <th>EDIT</th>
                                                    <th>DOSSSIER</th>
                                                    <th>DOWNLOAD</th>
                                                    <th>UPLOAD</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) {
												
												$dossier = "<div style='margin:30px;'><table border='1' style='width:100%; border-spacing: 0;border-collapse: collapse;border-width: thin;border-style: inset;'>";
												
												$dossier .= "
												<tr style='font-weight:bold;'>
												<td>SUBJECTS</td>
												<td>Term 1</td>
												<td>Term 2</td>
												<td>Term 3</td>
												<td>Annual Total</td>
												<td>Highest in Class</td>
												<td>Lowest in Class</td>
												<td>Average</td>
												<td>Position</td>
												<td>Grade</td>
												<td>Remark</td>
												</tr>
												";
												
                                                $total_marks = 0;
                                                $obtain_marks = "0";
                                                $result = "Pass";
                                                ?>
                                                <tr>
                                                    <td>
                                                        <?php echo $student['admission_no'] ?>
                                                    </td>
                                                    <td>
                                                        <?php echo $student['roll_no'] ?>
                                                    </td>
                                                    <td>
														<a href="<?php echo base_url();?>/student/view/<?php echo $student['student_id'];?>" target="_blank">    <?php echo $student['firstname'] . " " . $student['lastname']; ?> </a>
                                                    </td>
                                                    <td>
                                                        <?php echo $student['gender'] ?>
													<?php
													///OTHER STUDENT DETAILS
												//	echo "<pre>";
												//	echo $student['gender'];
												//	print_r($student['current_details']);
												//	echo $student['current_details']['class'] . $student['current_details']['section'];
												//	echo "</pre>";
													///END OTHER STUDENT DETAILS	
					//	echo "<img src='" . base_url() . "/" . $student['current_details']['image'] ."' width='100px'>";
													?>									
                                                    </td>
                                                    <?php
													
													$each_student_id = $student['student_id'];
                                                    if (!empty($student['exam_array'])) {
														$grand_total_mark = 0; 
                                                        count($student['exam_array']);
                                                        $s = 0;
                                                        foreach ($student['exam_array'] as $key => $exam_schedule) {
															
															$dossier .= "<tr>";
															$dossier .= "<td>". $exam_schedule['exam_name'] . "</td>";
															
                                                            $total_marks = (int)$total_marks + (int)$exam_schedule['full_marks'];
                                                            ?>
															<td>															
															<?php 																														
																$sub_term_total1 = subject_term_total($annual_result, 0, $each_student_id, $key); 															
																echo $sub_term_total1; 
																$dossier .= "<td>". $sub_term_total1 . "</td>";
															?>
															<?php  ?>	
															</td>
															<td>
															<?php 																														
																$sub_term_total2 = subject_term_total($annual_result, 1, $each_student_id, $key); 															
																echo $sub_term_total2; 
																$dossier .= "<td>". $sub_term_total2 . "</td>";
															?>
															<?php  ?>	
															
															</td>
															<td>
															<?php 																														
																$sub_term_total3 = subject_term_total($annual_result, 2, $each_student_id, $key); 															
																echo $sub_term_total3; 
																$dossier .= "<td>". $sub_term_total3 . "</td>";
															?>
															</td>
                                                            <td>
															<?php
															$sub_annual_total = subject_annual_total($annual_result, $each_student_id, $key); 															
															echo $sub_annual_total;
															$dossier .= "<td>". $sub_annual_total . "</td>";
															?>
															<?php  ?>			
                                                            </td>															
															<td>
															<?php																
															$subject_range = subject_highest($annual_result, $key, "max"); 															
															echo $subject_range;
															$dossier .= "<td>". $subject_range . "</td>";
															?>
															<?php  ?>																
															</td>	
															<td>
															<?php																
															$subject_range = subject_highest($annual_result, $key, "min"); 															
															echo $subject_range;
															$dossier .= "<td>". $subject_range . "</td>";
															?>
															<?php  ?>									
															</td>	
															<td>
															<?php

															$sub_avg = subject_average($annual_result, $key);															
															$sub_avg = number_format($sub_avg, 2, '.', '');
															echo $sub_avg;
															$dossier .= "<td>". $sub_avg . "</td>";
															?>
															</td>	
															<td>
															<?php 
															//subject_position
															//
															$sub_postion = subject_position($annual_result, $each_student_id, $key);			
															if($sub_postion !=""){
																echo $sub_postion;
															}
															$sub_postion = addOrdinalNumberSuffix($sub_postion);
															
															
															$dossier .= "<td>". $sub_postion . "</td>";													
															?>
															</td>
															<td>
															<?php 
																///SET MARK GRADE
																$e_annual = $sub_annual_total/3;
																		$grade_name = "";
																		$grade_desc = "";
																		foreach($gradeList as $grade => $grade_details){
																			if(($e_annual >= $grade_details['mark_from']) && ($e_annual <= $grade_details['mark_upto'])){
																			//	echo $grade_details['name'];
																				$grade_name = $grade_details['name'];
																				$grade_desc = $grade_details['description'];
																			}
																			
																			if(($e_annual > 100) || ($e_annual < 0)){
																			//	echo $grade_details['name'];
																				$grade_name = "<label class='label label-danger'>Out of range</label>";
																				$grade_desc = "<label class='label label-danger'>Out of range</label>";
																			}
																			
																			
																		}
																echo 	$grade_name;
																$dossier .= "<td>". $grade_name . "</td>";																											
																		///END SET MARK GRADE

															?>
															</td>
															
															<?php										
															$dossier .= "<td>". $grade_desc . "</td>";
															?>
														
															
                                                            <?php
															
														$dossier .= "</tr>";	
                                                        }
                                                        if ($s == count($student['exam_array'])) {
                                                            $obtain_marks = 0;
                                                        }
                                                        ?>
														
													<?php
														$dossier .= "</table></div>";
													?>
														
                                                     <!--   <td> <?php echo $obtain_marks . " /" . $total_marks; ?> </td> -->
													 
                                                        <td>
														<?php 
														//annual_total
														$annual_total = annual_total($annual_result, $each_student_id);			
														echo $annual_total;														

														?> 
														
														</td>
														
                                                        <td> 														
														<?php 
														$annual_obtainable = count($annual_result[0][0]['exam_array']) * 100 * 3;
														echo $annual_obtainable;
														
														?> 
														
														</td>
														
                                                        <td> 
														<?php
                                                            $annual_per = $annual_total * 100 / $annual_obtainable;
                                                            $annual_per = number_format($annual_per, 2, '.', '');
                                                            echo $annual_per;
                                                            ?>

														<td>														
														<?php 
														
														//annual_class_average														
														$annual_class_average = annual_class_average($annual_result);
														$annual_class_average_percent = $annual_class_average/$annual_obtainable * 100;
														$annual_class_average_percent = number_format($annual_class_average_percent, 2, '.', '');															
														echo $annual_class_average_percent;															
														?>
														</td>
														
														</td>
														<td>
														<?php 
															$annual_position = annual_position($annual_result, $each_student_id);
															if($annual_position !=""){
																echo $annual_position;
															}
															$annual_position = addOrdinalNumberSuffix($annual_position);
														?>
														</td>
														
														<td><button class='btn btn-primary btn-sm'>EDIT</button></td>
																												<?php 
														$student_count = count($examSchedule['result']);
														$subject_count = count($examSchedule['result'][0]['exam_array']);
														$student_admission_no = $student['admission_no'];
														$student_firstname = $student['firstname'];
														$student_lastname = $student['lastname'];
														$x_student_id = $student['student_id'];
														$student_class = $class_name;
														$student_section = $section_name;
														$student_session = $student['session_name'];
														$student_sex = $student['gender'];
														$student_image = $student['current_details']['image'];
														$setting_name = $setting['name'];
														$setting_email = $setting['email'];
														$setting_phone = $setting['phone'];
														$setting_address = $setting['address'];
														$setting_image = $setting['image'];
													
														?>
														<?php
														$school_logo = get_base_64(base_url() . "/uploads/school_content/logo/" . $setting_image);	
														$student_image = get_base_64(base_url() . "/" . $student_image);	
														$school_head_signature = get_base_64(base_url() . "/uploads/signatures_images/sign_0". "_" . ".jpg");	
														$class_teacher_signature = get_base_64(base_url() ."/uploads/signatures_images/sign_$class_id"."_$section_id". ".jpg");	
														
														$this_student_id = $student['student_id'];
														$acomments = get_comment($student_session,'ANNUAL',$this_student_id);
														
														$ahead_comment = $acomments['head_comment'];
														$ateacher_comment = $acomments['teacher_comment'];
														$student_add_u = strtoupper($student_address);
														$dossier_header = "
														<div style='margin:30px; padding:10px; border:1px black solid;'>
														<table style='width:100%;text-align:center;'>
														<tr>
														<td width='15%'>
														<img src='$school_logo' width='100px' border='1' />
														</td>
														<td width='70%'>
														<h1 style='margin:0px;padding:0px;'>$setting_name</h1>
														<h3 style='margin:0px;padding:0px;'>$setting_address</h3>
														<h4 style='margin:0px;padding:0px;'>$setting_phone</h4>
														<h4 style='margin:0px;padding:0px;'>$setting_email</h4>
														
														</td>
														<td width='15%'>
														<img src='$student_image' width='100px' border='1' />
														</td>
														</tr>														
														</table>
														</div>
														<center><h2 style='margin:0px;padding:0px;'> $student_add_u ANNUAL REPORT </h2></center>
														";
														
														
														$dossier_top = "
														<div style='margin:30px; padding:10px; border:1px black solid;'>
														<strong>Admission No:</strong> $student_admission_no &nbsp;&nbsp;&nbsp;
														<strong>$student_address's Name:</strong> $student_lastname $student_firstname &nbsp;&nbsp;&nbsp;
														<br />
														<br />
														<strong>Term:</strong> Annual &nbsp;&nbsp;&nbsp;
														<strong>Class:</strong> $student_class$student_section&nbsp;&nbsp;&nbsp;
														<strong>Session:</strong> $student_session&nbsp;&nbsp;&nbsp;
														<strong>Sex:</strong> $student_sex&nbsp;&nbsp;&nbsp;
														
														</div>
														";														

														$dossier .= "
														<div style='margin:30px'>
														<strong>Obtainable marks:</strong> $annual_obtainable &nbsp;&nbsp;&nbsp; 
														<strong>Score obtained:</strong> $annual_total &nbsp;&nbsp;&nbsp; 
														<strong>Average:</strong> $annual_per &nbsp;&nbsp;&nbsp; 
														<strong>Class Average:</strong> $annual_class_average_percent &nbsp;&nbsp;&nbsp; 
														<strong>Position:</strong> $annual_position Out of $student_count &nbsp;&nbsp;&nbsp; 
														<br />
														<strong>Subjects:</strong> $subject_count &nbsp;&nbsp;&nbsp;
														</div>														
														
														";	
$dossier .= "

														<div style='margin-left:30px;margin-right:30px;text-align:;'>
														<table style='width:100%;'>
														<tr>
														<td style='width:50%;'>
														<strong>
														$head_address's Comment and Signature
														</strong>														
														<br />
														<input $allow_head_comment list='comments' style=\"font-style:italic; width:100%; border:0px;\" 
														onchange=\"termComment('$student_session','ANNUAL','$this_student_id','head',this.value)\" 
														id='aterm_head_$this_student_id' class='aterm_head_$this_student_id'
														value=\"$ahead_comment\">
														<br />															
														<img src='$school_head_signature' width='80px' />
														<br />
														</td>
														
														<td>
														
														<strong>
														$teacher_address's Comment and Sign
														</strong>
														<br />
														<input $allow_teacher_comment list='comments' style=\"font-style:italic; width:100%; border:0px;\" 
														onchange=\"termComment('$student_session','ANNUAL','$this_student_id','teacher',this.value)\" 
														id='aterm_teacher_$this_student_id' class='aterm_teacher_$this_student_id'
														value=\"$ateacher_comment\">
														<br />
														<img src='$class_teacher_signature' width='80px' />
														<br />
														</td>
														</tr>
														</table>
														</div>
														";		

													
														?>


													
														<?php
														$display_id = $student['student_id']. "_annual";
														
														$print_dossier = "<div>														
														$dossier_header
														$dossier_top
														$dossier
														<center>
														<input type='button' class='btn btn-primary btn-sm print_button_class_a' value='PRINT' onclick=\"if(document.getElementById('$display_id')){}else{this.style.display = 'none';window.print();} content = document.getElementById('$display_id').innerHTML; x=window.open(); x.document.open;  x.document.write(content + '<script>window.print();</script>'); x.document.close(); \">
														</center>
														
														<script>													
														if(document.getElementById('$display_id'))
														{

														}else{
															print_buttons = document.getElementsByClassName('print_button_class_a');
														for(pc in print_buttons){
																print_buttons[pc].style.display = 'none';
															//	alert(pc);
															}
														}	
														</script>
														</div>";
														
														echo "
														<td>
														<div style='display:none;' id='$display_id'>
														$print_dossier
														</div>
														<input type='button' class='btn btn-primary btn-sm' value='VIEW' onclick=\"dialog('',document.getElementById('$display_id').innerHTML);\">
														</td>
														<td>
<button class='btn btn-primary btn-sm' onclick=\"content = document.getElementById('$display_id').innerHTML; download(content,'$student_lastname $student_firstname $student_class$student_section ANNUAL RESULT.html','text/html'); return false; \">DOWNLOAD</button>
														</td>															
														<td>														
														<input $allow_teacher_comment type='button' class='btn btn-primary btn-sm' value='UPLOAD' 
														onclick=\"upload_result('$student_admission_no','$student_lastname $student_firstname','$display_id','$student_session','$student_class','$student_section','ANNUAL'); \">															
														</td>														
														";
$all_annual_dossier[$student_admission_no]	 = $print_dossier;															
														?>

														<!--
                                                        <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>
<?php 
$all_annual_dossier_print = implode("<div style='page-break-after: always'><span style='display:none'>&nbsp;</span></div>", $all_annual_dossier);

echo "<div id='all_annual_dossier_print' style='display:none;'>$all_annual_dossier_print</div>";

?>		
							
<button class='btn btn-primary btn-sm' onclick="content = document.getElementById('all_annual_dossier_print').innerHTML; x=window.open(); x.document.open;  x.document.write('<html><body>' + content + '<script>window.print();</script>' + '</body></html>'); x.document.close(); return false;">PRINT ALL ANNUAL RESULT</button>									
<button class='btn btn-primary btn-sm' onclick="content = document.getElementById('all_annual_dossier_print').innerHTML; download(content,'<?php echo "$student_class$student_section"."_ANNUAL_RESULT.html"; ?>','text/html'); return false; ">DOWNLOAD ALL RESULT</button>
<!--- End Annual -->									
                                </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){
	if(isset($annual_result[$term_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;
			}
			
		}
	} else {
		return 0;
	}
}




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){
		if(isset($annual_result[$term_key])){		
			foreach($annual_result[$term_key] as $student_key => $student_details){
				//if($student_details['student_id'] == $this_student_id){	
				$studentx_id  = $student_details['student_id'];
				$scores_array[$studentx_id] = 0;
					$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[$studentx_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){	
			$studentx_id = $student_details['student_id'];
			$scores_array[$studentx_id] = 0;
				$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[$studentx_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){	
			$studentx_id = $student_details['student_id'];
			$scores_array[$studentx_id] = 0;
				$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[$studentx_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){
			$studentx_id = $student_details['student_id'];
			$scores_array[$studentx_id] = 0;
				$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[$studentx_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(){
		var update_url = base_url + 'application/scripts/update_result.php';
				  $.post(update_url,{
					  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>