<?php
session_start();
// ============
include("../../asset/theme/css/myfunc.php");
include("../../php_crud/my_class.php");
$obj=new my_class();
// ============

extract($_GET);

$section_id;
$ct_id;
$session;
$exam_term;


$det_in = $obj->Details_By_Cond("_int_institute_setup_vw","section_id='$section_id'");

//$data = $obj->View_all_data_row_column_By_Cond("_qz_quize_subject qs","_id , _sub_full_name",1);
$data = $obj->View_all_data_row_column_By_Cond("_ct_mark_sheet ms left join _int_subject qs on ms._subject_id =qs._id","ms._subject_id , qs._subject_full_name,qs._subject_short_name","ms._ct_id=$ct_id AND _exam_term=$exam_term AND _exam_session=$session AND qs._status ='A' group by ms._subject_id ORDER BY qs._id ASC");
extract($data);


?>

<style type="text/css">
	.sub-select {
	    border: 2px solid red;
	    color: blue;
	    font-size: 16px;
	}
</style>


<div class="row-fluid" style="margin-top:10px;">
	<legend>
		<div class="row-fluid">
			<div class="span12">
				<?php echo 'Shift: '.$det_in['shift'].', Medium: '.$det_in['medium'].', Class: '.$det_in['class'].', Department: '.$det_in['department'].', Student Type: '.$det_in['student_type'].', Section: '.$det_in['section'].', Quiz Type: '.$qt['_quize_type'].', Session: '.$session; ?>.


				<a target="blank" href="module/ct/pdf_ct_report.php?section_id=<?php echo $section_id;?>&ct_id=<?php echo $ct_id;?>&session=<?php echo $session;?>&exam_term=<?php echo $exam_term;?>" ><button class="btn btn-small btn-primary" type="button" style="float:right;text-align:center;"><i class='icon-print '></i>Print</button></a>

			</div>
		</div>

	</legend>
</div>


<div class="row-fluid">
	<div class="row-fluid">
		<div  class="row-fluid" id="form_data">
			<!-- <table id="sample-table-121" class="table table-striped table-bordered table-hover">	class="span12 " -->			
			<table id="sample_table" class="table table-striped table-bordered table-hover">		
				<tr>
					<th><?php echo "SL" ?></th>
					<!--<th><?php echo "Publish Date"; ?></th>-->
					<th><?php echo "Student roll" ?></th>
					<th><?php echo "Student name" ?></th>
					<?php
						//$where = "inv.section_id='$section_id' AND (ms._date BETWEEN '".$from."' AND '".$to."') GROUP BY ms._std_id ORDER BY SUM(ms._mark) DESC";				
					$i=0;
					foreach($data as $column1){
					?>
						<th><?php echo $column1['_subject_short_name']; ?></th>
						
					<?php
						$subject_id_array[$i] = $column1['_subject_id'];
						$i++;
					}		
					?>
					<th><?php echo "Total"; ?></th>
					<th><?php echo "Average"; ?></th>
					<th><?php echo "Position"; ?></th>
						
				</tr>
				<?php
				$where_cond = "ms._exam_session='$session' AND _ct_id=$ct_id AND ms._exam_term=$exam_term AND inv.section_id=$section_id GROUP BY ms._std_id ORDER BY SUM(ms._mark) DESC, ms._std_id ASC";
				$student_roll_2darray = $obj->CT_Merit_List($where_cond);  
				extract($student_roll_2darray);
				$j=0;
				$student_position=1;
				foreach($student_roll_2darray as $student_roll_array){
				
					$student_roll = $student_roll_array['_section_roll'];	
					$student_uniq_id = $student_roll_array['_uniq_id'];	
					$student_id = $student_roll_array['_std_id'];	
					$student_name = $student_roll_array['_full_name'];	
					$student_total_mark = $student_roll_array['_total_mark'];
					$student_avg = $student_roll_array['_total_mark']/$i;
					//$student_publish_date = $student_roll_array['_publish_date'];	
					//$student_position = $student_roll_array['_position'];	
					$total =0;
				?>
					<tr>
						<td><?php echo $j+1; ?></td>
						<!--<td><?php echo $student_publish_date; ?></td>-->
						<td><?php echo $student_roll; ?></td>
						<td><?php echo $student_name; ?></td>
					<?php 
						$k =0;
						while($k<$i){ // here $i is total subject counted number
							
							$subject_id = $subject_id_array[$k];
							//echo '<br>';
							//echo "select _subject_id ,sum(_mark) mark from _qz_mark_sheet ms where ms._subject_id = $subject_id AND ms._std_id = $student_roll AND (ms._date BETWEEN '".$from."' AND '".$to."') group by _subject_id";
							//if($quiz_type_id == 'all'){
								//$subject_mark_2darray = $obj->View_all_data_row_column_By_Cond("_qz_mark_sheet ms","_subject_id ,sum(_mark) mark"," ms._subject_id = $subject_id AND ms._status ='A' AND ms._std_id = $student_id AND ms._section_id =$section_id  AND (ms._date BETWEEN '".$from."' AND '".$to."') group by _subject_id ");
							//}else{
								$subject_mark_2darray = $obj->View_all_data_row_column_By_Cond("_ct_mark_sheet ms","_subject_id ,sum(_mark) mark"," ms._subject_id = $subject_id AND ms._status ='A' AND ms._std_id = $student_id  AND ms._section_id =$section_id AND ms._ct_id =$ct_id group by _subject_id ");
							//}
							
							//if(!$subject_mark_2darray){
								 //echo '<td>n/a</td>';
							//}else{							
								extract($subject_mark_2darray);
								?>
								<td>
									<?php 
										$total = $total + $subject_mark_2darray[0]['mark'];	
										echo $subject_mark_2darray[0]['mark'];
									?>
								</td>
								<?php
								
							//}?>
							
						<?php
						$k++;
						}
						 //echo '<td>'.'('.$student_total_mark.')'.$total.'</td>';
						 echo '<td>'.$student_total_mark.'</td>';
						 echo '<td>'.number_format((float)$student_avg, 2, '.', '').'</td>';
						 echo '<td>'.$student_position.'</td>';
						?>
					</tr>
					<?php
					$j++;
					$student_position++;
				}
				
				?>
			</table>	
		</div>
	</div>
</div>
