<?php

$result = $this->customlib->getUserData();
$role  = strtolower($result["user_type"]);
$role  = strtolower($role);
$id    = $result["id"];

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

?>
<div class="content-wrapper" style="min-height: 946px;">
    <!-- Content Header (Page header) -->
    <section class="content-header">
		<pre>
	<?php
	//print_r($subject_data);
	//print_r($character);
	//print_r($character_x);

	?>
	</pre>
		<style>
	.character {    
	
    -webkit-flex-wrap: nowrap; /* Safari 6.1+ */
    display: inline;  
    flex-wrap: nowrap;}

	.character input{    
	width:30px;
	display:inline;
	}	
	
	.ccon{
	max-width:200px;	
	}
	
	</style>
        <h1>
            <i class="fa fa-map-o"></i> <?php echo $this->lang->line('examinations'); ?> <small><?php echo $this->lang->line('student_fee1'); ?></small>  </h1>
    </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>
                    <form action="<?php echo site_url('admin/mark/create') ?>"  method="post" accept-charset="utf-8" id="schedule-form">
                        <div class="box-body">
                            <?php if ($this->session->flashdata('msg')) { ?>
                                <?php echo $this->session->flashdata('msg') ?>
                            <?php } ?>
                            <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";
                                                }
                                                ?>><?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";
                                                }
                                                ?>><?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)) {
/////GENERATE SCORESHEET	
		$score_sheet = "";
		$score_sheet .= "";
		$score_sheet .= "<center><h2>E-Score sheet form<h2/></center>";
		$trait_sheet = "";
		$trait_sheet .= "<center><h2>Student Trait Form<h2/></center>";
		
		$score_sheet .= "Session:_____________________________  Term:_____________________ Class:_________________________ Arm:_________________________________ 
						 Subject Name:_____________________ Subject Code:________________ Number of Students in Class:__________________________
						  Name of Teacher:_______________________ Phone number of teacher:______________________";
		$score_sheet .= "<table class='score_sheet' border='1' style='width:100%;' cellspacing='0px'>
						<tr style='font-weight:bold;'>
						<td>S/N</td>
						<td>Admission. No</td>
						<td>Student Name</td>
						<td>C.A 1</td>
						<td>C.A 2</td>
						<td>C.A 3</td>
						<td>EXAM</td>
						</tr>
						";
		$trait_sheet .= "Session:_____________________________  Term:_____________________ Class:_________________________ Arm:_________________________________ 
						 Number of Students in Class:__________________________
						  Name of Teacher:_______________________ Phone number of teacher:______________________";
		$trait_sheet .= "<table class='score_sheet' border='1' style='width:100%;' cellspacing='0px'>
						<tr style='font-weight:bold;'>
						<td>S/N</td>
						<td>Admission. No</td>
						<td>Student Name</td>
						<td>Punctuality</td>
						<td>Neatness</td>
						<td>Peer relations</td>
						<td>Hand writing</td>
						<td>Attitude to study</td>
						</tr>
						";
						
		$sn = 0;				
		  foreach ($examSchedule as $key => $student) {
			$student_admin_no = $student['admission_no'];
			$student_roll = $student['roll_no'];
			$student_fname = $student['firstname'];
			$student_lname = $student['lastname'];
			$sn += 1;	
			$score_sheet .= "
							<tr>
							<td>$sn</td>
							<td>$student_admin_no</td>
							<td>$student_fname $student_lname</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							</tr>
							";
			$trait_sheet .= "
							<tr>
							<td>$sn</td>
							<td>$student_admin_no</td>
							<td>$student_fname $student_lname</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							</tr>
							";
		  }
		  
		  for($s = 0; $s < 10; $s++){
			$sn += 1;	
			$score_sheet .= "
							<tr>
							<td>$sn</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							</tr>
							";	
			$trait_sheet .= "
							<tr>
							<td>$sn</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							</tr>
							";	
		}
		  
		 $score_sheet .= "</table>";
		 $score_sheet .= "<style> .score_sheet td {border-collapse:collapse;margin:0px;border-spacing: 0px 0px;cell-spacing:none;cell-padding:none;}</style>";
		 
		 $trait_sheet .= "</table>";
		 $trait_sheet .= "<style> .score_sheet td {border-collapse:collapse;margin:0px;border-spacing: 0px 0px;cell-spacing:none;cell-padding:none;}</style>";
		 
		 echo "<div id='score_sheet_div' style='display:none;'>$score_sheet</div>";
		 
		 echo "<div id='trait_sheet_div' style='display:none;'>$trait_sheet</div>";

///////GENERATE CSV
		$csv = "";
		$csv .= "";
		$csv .= "S/N,Admission. No,Student Name,C.A 1,C.A 2,C.A 3,EXAM,ATTEDANCE \n";
						
		$sn = 0;				
		  foreach ($examSchedule as $key => $student) {
			$student_admin_no = $student['admission_no'];
			$student_roll = $student['roll_no'];
			$student_fname = $student['firstname'];
			$student_lname = $student['lastname'];
			$sn += 1;	
			$csv .= "$sn,@$student_admin_no,$student_fname $student_lname,,,,,1 \n";
		  }
		   
		 
		 echo "<div id='csv_div' style='display:none;'>$csv</div>";		 
								
					
                    ?>
<button class='btn btn-primary btn-sm' onclick="content = document.getElementById('score_sheet_div').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 SCORE SHEET</button>	
<button class='btn btn-primary btn-sm' onclick="content = document.getElementById('trait_sheet_div').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 TRAIT SHEET</button>	
								
<select id='exam_list' name='exam_list'>

</select>

<input type="file" id="csv_file" name="csv_file" style='display:inline;opacity:1;' placeholder='select csv' accept="text/csv" >

<button class='btn btn-primary btn-sm' id="upload_csv" onclick="upload_csv()" >IMPORT SCORE(CSV)</button>
<button class='btn btn-primary btn-sm' onclick="content = document.getElementById('csv_div').innerHTML; download(content,'score_sheet.csv','text/csv');">	EXPORT SCORE(CSV)</button>		



                    <div class="box box-info">
                        <div class="box-header with-border">
                            <h3 class="box-title"><i class="fa fa-users"></i> <?php echo $this->lang->line('fill_mark'); ?></h3>
                        </div>
                        <div class="box-body">
                            <?php
                            if (!empty($examSchedule)) {
                                ?>
                                <form role="form" id=""  class="addmarks-form"  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">
                                        <table class="table table-striped table-bordered table-hover">
                                            <thead>
                                                <tr>
                                                    <th>
                                                        <?php echo $this->lang->line('admission_no'); ?>
                                                    </th>
                                                    <th><?php echo $this->lang->line('roll_no'); ?></th>
                                                    <th>
                                                        <?php echo $this->lang->line('student'); ?>
                                                    </th>
													<?php 
													if($user_id != 1){
														$show_subject = 'none';
													//	if($user_id == $class_teacher){
														if(in_array($user_id, $class_teacher)){
															$show_subject = '';
														}
													}
													else {

														$show_subject = '';
													}
													?>
													
													<th  style="display:<?php echo $show_subject; ?>;">
                                                        Character/Trait 
                                                    </th>	
                                                    <?php
                                                    $s = 0;
                                                    foreach ($examSchedule as $key => $student) {
                                                        if (!empty($student['exam_array'])) {
                                                            if ($s == 0) {
																$allowed_exam_array = array();																
                                                                foreach ($student['exam_array'] as $key => $exam_schedule) {
																/////ASSIGNED TEACHER	
																	if($user_id != 1){
																		$show_subject = 'none';	
																		foreach($subject_data as $subject_data_key => $subject_data_detail){
																			if($subject_data_detail['id'] == $exam_schedule['teacher_subject_id']){
																				if($subject_data_detail['teacher_id'] == $user_id){
																					$show_subject = '';
																					$es_id = $exam_schedule['exam_schedule_id'];
																					$allowed_exam_array["$es_id"] = $exam_schedule['exam_name'];
																					$subject_csv[$es_id] = array();
																				}
																			}																		
																		}
																	}else{
																		$show_subject = '';
																		$es_id = $exam_schedule['exam_schedule_id'];
																		$allowed_exam_array["$es_id"] = $exam_schedule['exam_name'];
																		$subject_csv[$es_id] = array();
																	}
																/////END ASSIGNED TEACHER
                                                                    ?>
                                                                    <th style="display:<?php echo $show_subject; ?>;" id="<?php echo $exam_schedule['exam_name'] . '_col'; ?>" onclick="highlight('<?php echo $exam_schedule['exam_name'] . '_col'; ?>')">
                                                                        <?php
                                                                        echo $exam_schedule['exam_name'] . " (" . substr($exam_schedule['exam_type'], 0, 2) . ": " . $exam_schedule['passing_marks'] . "/" . $exam_schedule['full_marks'] . ") ";
                                                                        ?>
                                                                    </th>
                                                                    <?php
                                                                }
                                                            }
                                                        } else {
                                                            ?>

                                                            <?php
                                                        }
                                                        $s++;
                                                    }
                                                    ?>

                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                $s = 0;
                                                foreach ($examSchedule 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 as $key => $student) {
                                                ?>

                                                <tr>
                                                    <td>     <?php echo $student['admission_no'] ?></td>
                                                    <td>     <?php echo $student['roll_no'] ?></td>
                                                    <td>        <?php echo $student['firstname'] . " " . $student['lastname']; ?> </td>
													<?php 
													if($user_id != 1){
														$show_subject = 'none';
														if($user_id == $class_teacher){
															$show_subject = '';
														}
													}
													else {

														$show_subject = '';
													}
													?>	
													<td style="display:<?php echo $show_subject; ?>;">
													<button onclick="$('#<?php echo $student['student_id']; ?>_triat').toggle();return false;">Edit traits</button>
													<div class='ccon' id='<?php echo $student['student_id']; ?>_triat' style='display:none;'>
														<span class="character">  Punctuality: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c1"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c1"; ?>" value="<?php echo $student['character']['c1'] ?>" placeholder=""> </span>
												<br />	<span class="character">  Neatness: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c2"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c2"; ?>" value="<?php echo $student['character']['c2'] ?>" placeholder=""> </span>
												<br />	<span class="character">  Pear relation: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c3"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c3"; ?>" value="<?php echo $student['character']['c3'] ?>" placeholder=""> </span>
												<br />	<span class="character">  Hand writing: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c4"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c4"; ?>" value="<?php echo $student['character']['c4'] ?>" placeholder=""> </span>
												<br />	<span class="character">  Attitude to study: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c5"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c5"; ?>" value="<?php echo $student['character']['c5'] ?>" placeholder=""> </span>
													<span style='display:none;'>	<span class="character">  Neatness: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c6"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c6"; ?>" value="<?php echo $student['character']['c6'] ?>" placeholder=""> </span>
														<span class="character">  Initiative: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c7"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c7"; ?>" value="<?php echo $student['character']['c7'] ?>" placeholder=""> </span>
														<span class="character">  Spirit of Co-operation: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c8"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c8"; ?>" value="<?php echo $student['character']['c8'] ?>" placeholder=""> </span>
														<span class="character">  Musical Skill: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c9"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c8"; ?>" value="<?php echo $student['character']['c9'] ?>" placeholder=""> </span>
														<span class="character">  Craft: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c10"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c10"; ?>" value="<?php echo $student['character']['c10'] ?>" placeholder=""> </span>
														<span class="character">  Politeness: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c11"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c11"; ?>" value="<?php echo $student['character']['c11'] ?>" placeholder=""> </span>
														<span class="character">  Relationship with Teacher: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c12"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c12"; ?>" value="<?php echo $student['character']['c12'] ?>" placeholder=""> </span>
														<span class="character">  Tolerance: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c13"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c13"; ?>" value="<?php echo $student['character']['c13'] ?>" placeholder=""> </span>
														<span class="character">  Hand Writing: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c14"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c14"; ?>" value="<?php echo $student['character']['c14'] ?>" placeholder=""> </span>
														<span class="character">  Fluency: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c15"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c15"; ?>" value="<?php echo $student['character']['c15'] ?>" placeholder=""> </span>
														<span class="character">  Attentiveness: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c16"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c16"; ?>" value="<?php echo $student['character']['c16'] ?>" placeholder=""> </span>
														<span class="character">  Respect for Others: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c17"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c17"; ?>" value="<?php echo $student['character']['c17'] ?>" placeholder=""> </span>
														<span class="character">  Self Control: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c18"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c18"; ?>" value="<?php echo $student['character']['c18'] ?>" placeholder=""> </span>
														<span class="character">  Courage: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c19"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c19"; ?>" value="<?php echo $student['character']['c19'] ?>" placeholder=""> </span>
														<span class="character">  Humility: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c20"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c20"; ?>" value="<?php echo $student['character']['c20'] ?>" placeholder=""> </span>
														<span class="character">  Leadership: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c21"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c21"; ?>" value="<?php echo $student['character']['c21'] ?>" placeholder=""> </span>
														<span class="character">  Honesty: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c22"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c22"; ?>" value="<?php echo $student['character']['c22'] ?>" placeholder=""> </span>
														<span class="character">  Openness: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c23"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c23"; ?>" value="<?php echo $student['character']['c23'] ?>" placeholder=""> </span>
														<span class="character">  Kindness: <input type="text" name="student_c_<?php echo $student['student_id'] . "_c24"; ?>" class="form-control input-sm" id="c_<?php echo $student['student_id'] . "_c24"; ?>" value="<?php echo $student['character']['c24'] ?>" placeholder=""> </span>
													</span>
													</div>
						<!--		-->					  
													  <br />
													
													</td>	
                                                    <?php
                                                    if (!empty($student['exam_array'])) {
                                                        $n = 0;
                                                        $class = "";
                                                        $check = "";
                                                        foreach ($student['exam_array'] as $key => $exam_schedule) {
																/////ASSIGNED TEACHER	
																	if($user_id != 1){
																		$show_subject_score = 'none';	
																		foreach($subject_data as $subject_data_key => $subject_data_detail){
																			if($subject_data_detail['id'] == $exam_schedule['teacher_subject_id']){
																				if($subject_data_detail['teacher_id'] == $user_id){
																					$show_subject_score = '';

																				}
																			}																		
																		}
																	}else{
																		$show_subject_score = '';

																	}
																/////END ASSIGNED TEACHER																

                                                            if (!empty($teacher_subjects) && (array_key_exists($n, $teacher_subjects))) {
                                                                $class = "readonly";
                                                                $check = "disabled";
                                                                if ($teacher_subjects[$n]["subject_id"] == $exam_schedule["subject_id"]) {

                                                                    $class = "";
                                                                    $check = "";
                                                                    $n++;
                                                                }
                                                            }
                                                            ?>
												
															
                                                            <td style="display:<?php echo $show_subject_score; ?>;" class="<?php echo $exam_schedule['exam_name'] . '_col'; ?>">
                                                                <div class="form-group">
                                                                    <div class="checkbox">
                                                                        <label><input type="checkbox" <?php echo $check; ?> name="student_absent<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id']; ?>" value="ABS" <?php if ($exam_schedule['attendence'] == "ABS") echo "checked"; ?>>Abs</label>
                                                                    </div>
                                                                    <input type="hidden" name="subject_id" value="<?php echo $exam_schedule["subject_id"] ?>">
                                                                    CA1 <input type="text" name="student_number<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca1"; ?>" class="form-control input-sm" id="subject_<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca1"; ?>" value="<?php echo $exam_schedule['ca1'] ?>" placeholder="Enter Marks"><br />
                                                                    CA2 <input type="text" name="student_number<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca2"; ?>" class="form-control input-sm" id="subject_<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca2"; ?>" value="<?php echo $exam_schedule['ca2'] ?>" placeholder="Enter Marks"><br />
                                                                    CA3 <input type="text" name="student_number<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca3"; ?>" class="form-control input-sm" id="subject_<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca3"; ?>" value="<?php echo $exam_schedule['ca3'] ?>" placeholder="Enter Marks"><br />
                                                                <span style='display:none;'>    CA4 <input type="text" name="student_number<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca4"; ?>" class="form-control input-sm" id="subject_<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca4"; ?>" value="<?php echo $exam_schedule['ca4'] ?>" placeholder="Enter Marks"><br />
                                                                    CA5 <input type="text" name="student_number<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca5"; ?>" class="form-control input-sm" id="subject_<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca5"; ?>" value="<?php echo $exam_schedule['ca5'] ?>" placeholder="Enter Marks"><br />
                                                                    CA6 <input type="text" name="student_number<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca6"; ?>" class="form-control input-sm" id="subject_<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca6"; ?>" value="<?php echo $exam_schedule['ca6'] ?>" placeholder="Enter Marks"><br />
                                                                    CA7 <input type="text" name="student_number<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca7"; ?>" class="form-control input-sm" id="subject_<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca7"; ?>" value="<?php echo $exam_schedule['ca7'] ?>" placeholder="Enter Marks"><br />
                                                                    CA8 <input type="text" name="student_number<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca8"; ?>" class="form-control input-sm" id="subject_<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca8"; ?>" value="<?php echo $exam_schedule['ca8'] ?>" placeholder="Enter Marks"><br />
                                                                    CA9 <input type="text" name="student_number<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca9"; ?>" class="form-control input-sm" id="subject_<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca9"; ?>" value="<?php echo $exam_schedule['ca9'] ?>" placeholder="Enter Marks"><br />
                                                                </span>    Exam <input type="text" <?php echo $class; ?> name="student_number<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id']; ?>" class="form-control input-sm" id="subject_<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id']; ?>" value="<?php echo $exam_schedule['get_marks'] ?>" placeholder="Enter Marks">
                                                                </div>
                                                            </td>
																														<?php
															///WRITE SUBJECT CSV
															//$csv .= "S/N,Admission. No,Student Name,C.A 1,C.A 2,C.A 3,EXAM,ATTEDANCE \n";
															if ($exam_schedule['attendence'] == "ABS"){
																$attd = 0;
															}
															else{
																$attd = 1;
															}
															$es_idx = $exam_schedule['exam_schedule_id'];
															//$csv_string = "S/N," . $student['admission_no'] . "," . $student['firstname'] . " " . $student['lastname'] . "," .  $exam_schedule['ca1'] . "," . $exam_schedule['ca2'] . $exam_schedule['ca3'] . "," . $exam_schedule['get_marks'] . "," . $attd . "\n";
															$csv_string = "@" . $student['admission_no'] . "," . $student['firstname'] . " " . $student['lastname'] . "," .  $exam_schedule['ca1'] . "," . $exam_schedule['ca2'] . "," . $exam_schedule['ca3'] . "," . $exam_schedule['get_marks'] . "," . $attd . "\n";
															$subject_csv[$es_idx][] = $csv_string;
															?>
                                                            <?php
                                                        }
                                                    }else {
                                                        ?>

                                                        <?php
                                                    }
                                                    ?>

                                                </tr>
                                                <?php
                                            }
                                            ?>
                                            </tbody>
                                        </table>
                                    </div>
                                    <button type="submit" class="btn btn-primary pull-right" name="save_exam" value="save_exam"><?php echo $this->lang->line('save'); ?></button>
                                </form>
                                <?php
                            } else {
                                ?>

                                <div class="alert alert-info">
                                    <?php echo $this->lang->line('no_record_found'); ?>
                                </div>


                                <?php
                            }
                            ?>
                        </div><!---./end box-body--->
                    </div>
                </div>            

            </div>   <!-- /.row -->
            <?php
        } else {
            
        }
        ?>

    </section><!-- /.content -->
</div>
<script type="text/javascript">
    $(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>';
            var url = "<?php
        $userdata = $this->customlib->getUserData();
        if (($userdata["role_id"] == 2)) {
            echo "getClassTeacherSection";
        } else {
            echo "getByClass";
        }
        ?>";
            $.ajax({
                type: "GET",
                url: base_url + "sections/" + url,
                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);
                }
            });
        });
        $(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 src="<?php echo base_url(); ?>backend/custom/jquery.validate.min.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>backend/custom/bootstrap-datepicker.js"></script>
<script>
    $('.sandbox-container').datepicker({
        autoclose: true,
        format: "dd-mm-yyyy"
    });
    $(function () {
        $('.addmarks-form').validate({

            submitHandler: function (form) {
                form.submit();
            }
        });

        $('input[id^="subject_X"]').each(function () { ////remove require with 'X'
            $(this).rules('add', {
                required: true,
                messages: {
                    required: "Required"
                }
            });
        });
    });
    var class_id = $('#class_id').val();
    var section_id = '<?php echo set_value('section_id') ?>';
    getSectionByClass(class_id, section_id);
    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>';
            var url = "<?php
        $userdata = $this->customlib->getUserData();
        if (($userdata["role_id"] == 2)) {
            echo "getClassTeacherSection";
        } else {
            echo "getByClass";
        }
        ?>";
            $.ajax({
                type: "GET",
                url: base_url + "sections/" + url,
                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);
                }
            });
        }
    }
</script>
<script>
function highlight(col_class){
$("[class*='_col']").css("background", "");	
$("[class*='"+ col_class +"']").css("background", "lime");		
}

</script>
<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="subject_csv_data" style="display:none;">
<?php 
/*
echo "<pre>";
print_r($subject_csv);
echo "</pre>";
*/
foreach($subject_csv as $sub_key => $each_student){
	echo "<div id='subject_csv_$sub_key'>";
	$sn = 1;
	echo "S/N,Admission. No,Student Name,C.A 1,C.A 2,C.A 3,EXAM,ATTEDANCE \n";
		foreach($each_student as $score_key => $score_data){
			$sn += 1;
			echo "$sn,$score_data";
		}
	
	echo "</div>\n";
}


?>
</div>
<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>
function download(content,filename,filetype){

var exam_id = $("#exam_id").val();
var class_id = $("#class_id").val();
var section_id = $("#section_id").val();
var exam_list = $("#exam_list").val();

if(exam_list != ""){
//alert(this_class_name + this_section_name + subject_list["subject_" + exam_list]);
filename = this_class_name + this_section_name + subject_list["subject_" + exam_list] + ".csv";
content = $("#subject_csv_" + exam_list).html()
//return false;
}else{
alert("Please select a Subject");
return false;
}
	
	
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>


<div id="csv_response" style='display:none;'></div>
<?php
//print_r($allowed_exam_array);
$exam_list_options = "";
$exam_list_options .= "<option value=''>SELECT</option>";	
foreach($allowed_exam_array as $key => $value){
$exam_list_options .= "<option value='$key'>$value</option>";	
}

$subject_list_js = "<script> subject_list = {";
foreach($allowed_exam_array as $key => $value){
$subject_list_js .= "subject_$key: \"$value\",";
}
$subject_list_js .= "};</script>";
echo $subject_list_js;
?>


<script>
var this_class_name = "<?php echo $class_name; ?>";
var this_section_name = "<?php echo $section_name; ?>";
</script>

<script>
document.getElementById('exam_list').innerHTML = "<?php echo $exam_list_options; ?>";
</script>
<script>
csv_content_set = "";
$(document).ready(function() {
	$("#csv_file").change(function(){
		// prepare HTML5 FileReader
		var oFReader = new FileReader();
		oFReader.readAsDataURL(document.getElementById("csv_file").files[0]);

		oFReader.onload = function (oFREvent) {
			csv_content_set = oFREvent.target.result;
			//alert(oFREvent.target.result);
		};
	});	
});
</script>
<script>
	function client_effect(exam_schedule_id,student_id,ca1,ca2,ca3,get_marks,att){
		//="subject_<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id'] . "_ca1"; ?>"
		document.getElementById('subject_' + student_id + '_' + exam_schedule_id + '_ca1').value = ca1;
		document.getElementById('subject_' + student_id + '_' + exam_schedule_id + '_ca2').value = ca2;
		document.getElementById('subject_' + student_id + '_' + exam_schedule_id + '_ca3').value = ca3;
		document.getElementById('subject_' + student_id + '_' + exam_schedule_id).value = get_marks;
		
		//id="student_absent<?php echo $student['student_id'] . "_" . $exam_schedule['exam_schedule_id']; ?>"
		if(att == 1){
			$( "#student_absent" + student_id + "_" +  exam_schedule_id).prop( "checked", false );
		}
		if(att == 0 || att == ''){
			$( "#student_absent" + student_id + "_" +  exam_schedule_id).prop( "checked", true );
		}
		
		
		
		
	//	alert(exam_schedule_id + "-" + student_id + "-" + ca1  + "-" + ca2 + "-" + ca3 + "-" + get_marks + "-" + att);
	//alert('Hello');
	}
</script>
<script>
function upload_csv(){
	
		var base_url = '<?php echo base_url() ?>';
		var url = base_url + 'application/scripts/upload_csv.php';
		$(document).ready(function(){
			csv_content = csv_content_set;
			exam_schedule_id = $('#exam_list').val();
				if(exam_schedule_id != ""){
					var proper_name = this_class_name + this_section_name + subject_list["subject_" + exam_schedule_id];
					var csv_filename = $('#csv_file').val();	
					
						if(csv_filename.indexOf(proper_name) > 0){
							//alert("CSV File name is valid!");
						}
						else{
							alert("CSV File name is invalid!");
							return false;
						}
						
						if(csv_content != ""){					
								alert("Uploading CSV .....");
							//	alert(exam_schedule_id);
							//	alert(csv_content);
								  $.post(url,{csv_content:csv_content,exam_schedule_id:exam_schedule_id},function(result){
								   $("#csv_response").html(result);
								   
								   alert('CSV UPLOADED');
									
								  });
						}else{
							alert("SELECT A CSV FILE!");
						}  
					} else{		
					alert("SELECT A SUBJECT!");
					}	  
		});

}
</script>