<?php 
$params = $this->params;
?>
<style>
.listable .bb:last-child{border-bottom: 0;}
</style>
<div class="blocktitle"> Quiz Summary  </div>
<div class="tar borb mrb10">
	<?php echo $this->Html->link('Back',array('controller'=>'assignmentfeedbacks','action'=>'add',$params['pass'][0]),array('class'=>'btn btn-mini'))?>
</div>
<div class="w97p pdl10">
	

<?php 
	//pr($quizoptions);
	//pr(sizeof($quiztests)); 
	//pr($tot_number);
	//echo '==================';
	//pr($quizoptiondata);
	?>
<h4><?php echo $tot_number['Quizsetting']['title']?></h4>
<hr/>
<br/>
<div class="ritable borb">
<table width="100%" border="1" cellpadding="5" cellspacing="3" class="listable">
	<tr>
		<th>Name</th>
		<?php 
		$i = 0;
		foreach ( $quizoptions AS $key => $val){
			$i++;
			?>
			<th class="tac">
			<?php 
			echo $i;
			?>
			</th>
		<?php 
		}
		?>
	</tr>
	<?php 
		$str1 = $str2= $str2 = $str3 = ''; 
		//pr($quiztests);
		foreach ($quiztests AS $quiztest){
                  
			
		?>
		<tr>
			<td>
			<?php echo $quiztest['Profile']['fname'] . " ". $quiztest['Profile']['mname']. " ". $quiztest['Profile']['lname']?>
			</td>
			<?php 
			$individual__tot_marks = 0;$individual__tot_panelty = 0;
                        
                        $farr=array();
			foreach ($quiztest['Quiztestdetail'] as $quiztestdetail){
                            $arr[$quiztestdetail['quizoption_id']] = $quiztestdetail['isCorrect'];
                                
                                
                                $farr[$quiztestdetail['quizoption_id']] = $quiztestdetail['quizoption_id'];
                                
				$arr[$quiztestdetail['quizoption_id']] = $quiztestdetail['isCorrect']; 
				$individual__tot_marks = ($individual__tot_marks+$quiztestdetail['mark']);
				
				if($quiztestdetail['isCorrect'] != ACTIVE){
					$individual__tot_panelty = ($individual__tot_panelty + $quiztestdetail['penalty']);
				}
			}
			?>
			<?php 
			$icon_right = $this->Html->image('right.png');
			$icon_wrong = $this->Html->image('wrong.png');
                        
                         
			foreach ($quizoptions AS $key => $val){
				
			?>
				<td class="tac">
				<?php 
                                
				if(in_array($key,$farr)){
                                    
             
                                    $vl = $arr[$key] == '1'? $icon_right:$icon_wrong; 
                                    
                                    
                                }else{
                                    $vl =$icon_wrong; 
                                }
                                echo $vl;
                                
				?>
				</td>
			<?php 
			};
			?>
		</tr>
		<?php 
		// marks string
		//$str1 .= $individual__tot_marks.',';
		// panelty marks string
		//$str2 .= $individual__tot_panelty.',';
		
		// obtained marks string.
		$str3 .= ($individual__tot_marks-$individual__tot_panelty).',';
		};
		
	?>	
</table>

</div>

<div class="divline"></div>

<?php 
	function calculate_variance($aValues, $bSample = false) {
	    $fMean = array_sum($aValues) / count($aValues);
	    $fVariance = 0.0;
	    
	    foreach ($aValues as $i) {
	        $fVariance += pow($i - $fMean, 2);
	    }
	    $fVariance /= ( $bSample ? count($aValues) - 1 : count($aValues) );
	    return (float)$fVariance;
	}
	
	 $group_size = sizeof($quiztests);
	 $arr_obtained_marks = explode(',',substr($str3,0,-1));
	 $all_obtain_tot_marks =(array_sum($arr_obtained_marks));
	 $mean = ($all_obtain_tot_marks / $group_size);

	 $varience = calculate_variance($arr_obtained_marks);
	 $sd = sqrt($varience);
	 $h_ob_score = max($arr_obtained_marks);
	 $l_ob_score = min($arr_obtained_marks);
	 
	 // Percentile calculator.
	function mypercentile($data,$percentile){ 
	    if( 0 < $percentile && $percentile < 1 ) { 
	        $p = $percentile; 
	    }else if( 1 < $percentile && $percentile <= 100 ) { 
	        $p = $percentile * .01; 
	    }else { 
	        return ""; 
	    } 
	    $count = count($data); 
	    $allindex = ($count-1)*$p; 
	    $intvalindex = intval($allindex); 
	    $floatval = $allindex - $intvalindex; 
	    sort($data); 
	    if(!is_float($floatval)){ 
	        $result = $data[$intvalindex]; 
	    }else { 
	        if($count > $intvalindex+1) 
	            $result = $floatval*($data[$intvalindex+1] - $data[$intvalindex]) + $data[$intvalindex];
	        else 
	            $result = $data[$intvalindex]; 
	    } 
	    return $result; 
	} 
	
	 
	
?>

<br/>
<p class="bold">Overall Descriptive Statistics Summary</p>
<br/>
<table width="100%" border="1" cellpadding="3" cellspacing="5" class="listable i2tac">
  <tr>
    <td width="70%"><b>Group Size </b> </td>
    <td width="30%"><?php echo $group_size;?></td>
  </tr>
  
  <tr>
    <td width="30%"><b>  Mean </b> </td>
    <td width="70%"><?php echo round($mean, 2);?></td>
  </tr>
  
  <tr>
    <td width="30%"><b>  Standard Deviation </b> </td>
    <td width="70%"><?php echo round($sd, 2)?></td>
  </tr>
  <tr>
    <td width="30%"><b>  Variance </b> </td>
    <td width="70%"><?php echo round($varience,2);?></td>
  </tr>
  <tr>
    <td width="30%"><b>  Highest Posssible Score </b> </td>
    <td width="70%"><?php echo $tot_number[0]['tot_points']?></td>
  </tr>
  <tr>
    <td width="30%"><b> Highest Obtained Score </b> </td>
    <td width="70%"><?php echo $h_ob_score;?></td>
  </tr>
  <tr>
    <td width="30%"><b>  Lowest Obtained Score </b> </td>
    <td width="70%"><?php echo $l_ob_score?></td>
  </tr>
   <tr>
    <td width="30%"><b>  Range of Scores </b> </td>
    <td width="70%"><?php echo  ($h_ob_score -$l_ob_score);?></td>
  </tr>
  <tr>
    <td width="30%"><b>  90th Percentile Score </b> </td>
    <td width="70%"><?php echo  mypercentile($arr_obtained_marks,90);?></td>
  </tr>
  <tr>
    <td width="30%"><b>  75th Percentile Score </b> </td>
    <td width="70%"><?php echo  mypercentile($arr_obtained_marks,75);?></td>
  </tr>
  <tr>
    <td width="30%"><b>  25th Percentile Score </b> </td>
    <td width="70%"><?php echo  mypercentile($arr_obtained_marks,25);?></td>
  </tr>
  <tr>
    <td width="30%"><b>  10th Percentile Score </b> </td>
    <td width="70%"><?php echo  mypercentile($arr_obtained_marks,10);?></td>
  </tr>
</table>


<br/> 

<div class="rawlisting">
	Above is a table that represents the distribution of scrores in several different ways:
	<br/><br/>
	<p><b>Group Size: </b>It is the total number of student(s) attemped the quiz.</p>
	<p><b>Mean: </b> It is the average score obtained by the all the student(s) who attemped the quiz.</p>
	<p><b>Standard Deviation: </b>A quantity calculated to indicate the extent of deviation for a group as a whole.</p>
	<p><b>Veriance: </b>The average of the squared differences from the Mean.</p>
	<p><b>Range of Scores:</b> It is the difference between highest obtained score and lowest obtained score.</p>
	<p><b>nth Percentile: </b>It is the score at or below nth percent of the student(s) received.</p>
	
</div>

<hr/>
<br/>

<p class="bold">Overall Total Score Distribution Table</p>
<br/> 
<?php 
	//pr($arr_obtained_marks);
	
	//$array = array('1'=>'24','2'=>'25','3'=>'25','4'=>'25','5'=>'24','6'=>'30','7'=>'31');
	$new_arr = array_count_values($arr_obtained_marks);
	krsort ($new_arr);
?>
<table width="100%" border="1" cellpadding="3" cellspacing="5" class="listable">
	<tr class="itac">
	    <th>Raw Score</th>
	    <th>Frequency</th>
	    <th>Cumulative Frequency</th>
	    <th>Percent</th>
	    <th>Cumulative Percent</th>
	    <th>Percentile Rank</th>
	    <!--<th>Standard Score</th> -->
	</tr>
  <?php 
  
  // for percentile rank;
  	//$rank_array = array('0'=>'98','1'=>'98', '2'=>'99','3'=>'94', '4'=>'96', '5'=>'88', '6'=>'88', '7'=>'90', '8'=>'84', '9'=>'84', '10'=>'85', '11'=>'86','12'=>'50', '13'=>'65', '14'=>'70', '15'=>'72', '16'=>'72', '17'=>'78','18'=>'80', '19'=>'82');
  	//$rank_array = array('0'=>'24.75','1'=>'4');
  	sort($arr_obtained_marks);
  	//$matches = array_keys($rank_array, 4);
  	//$pr = (($matches[0] + .5 * sizeof($matches))*100)/sizeof($rank_array);
  	//end
  	
  	$sum = array_sum($new_arr);
  	$i = 0;
  	$prev_val = $cumulativeF ='';
  	
  	foreach ($new_arr as $number => $frequency){
  		if($i==0){
  			$cumulativeF = $sum;
  		}else{
  			$cumulativeF = ($cumulativeF - $prev_val);
  		}
  		$i++;
  		$prev_val = $frequency;
  		// percent
  		$p = (($frequency/$group_size)*100);
  		// cumulative percent
  		$cp = (($cumulativeF/$group_size)*100);
  		
  		//percentile rank
  		$matches = array_keys($arr_obtained_marks, $number);
  		$pr = (($matches[0] + .5 * sizeof($matches))*100)/sizeof($arr_obtained_marks);
  		
  ?>
	<tr class="itac">
	    <td><?php echo $number ;?></td>
	    <td><?php echo $frequency ;?></td>
	    <td><?php echo $cumulativeF?></td>
	    <td><?php echo round($p, 1);?>%</td>
	    <td><?php echo round($cp,1)?>%</td>
	    <td><?php echo round($pr)?></td>
	    <!--<td>Row 1: Col 2</td>-->
	</tr>
  <?php }?>
</table>

<br/> 

<div class="rawlisting">
	Above is a table that represents the distribution of scrores in several different ways:
	<br/><br/>
	<p><b>Raw Score: </b>It is the number of items answered correctly.</p>
	<p><b>Mode: </b> It is the most frequentyl appearing score (this is identified in the Raw Score column).</p>
	<p><b>Frequency: </b>It is the number of students with a given score.</p>
	<p><b>Cumulative Frequency: </b>It is the percent of students at or below each score.</p>
	<p><b>Percent:</b> It is the percent of the number of students wisth a given score.</p>
	<p><b>Cumulative Percent: </b>It is the percent of students at or below each score.</p>
	<p><b>Percentile Rank: </b>It is the percent of students who cored below the mid-point of a given score.</p>
	<p><b>Standard Score: </b> It is computes by subtracting the population mean of the individual raw score and dividing the remainder by the standard deviation. The resulting scores have a mean of 50 and standart deviation of 10. This is also referred to as a z-score.</p>
	
</div>



<hr/>
<br/>


<br/> 
<?php
	echo $this->Html->script('site_js/highcharts');
	echo $this->Html->script('site_js/exporting'); 
?>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<?php 
	//pr($arr_obtained_marks);
	//pr(array_count_values($arr_obtained_marks));
	$str_score = $str_count = '';
	$str_score2 = $str_count2 = '';
	$c = 0;
	foreach ( array_count_values($arr_obtained_marks) as $key => $val){
		$arr_number_of_student[round($key)][$c] = $val;
		$c++;
	}
	
	$initial = round($l_ob_score); 
	$limit = round($h_ob_score); 
	for ($i=$initial;$i<=$limit;$i++){
		$str_score .= "'$i'".',';
		if(!empty($arr_number_of_student[$i])){
			//$str_count .= sizeof($arr_number_of_student[$i]).',';
			$str_count .= array_sum($arr_number_of_student[$i]).',';
		}else{
			$str_count .= '0,';
		}
		
	}
	

$obtain_scores =substr($str_score,0,-1);
$count = substr($str_count,0,-1);
//pr($str_count);
foreach ($arr_number_of_student as $k => $v){
	$str_score2 .=  $k.',' ;
	foreach ($v as $key => $val){
		$str_count2 .= $val . ',';
	}
}
$obtain_scores2 =substr($str_score2,0,-1);
$count2 = substr($str_count2,0,-1);

?>
<style> 
.highcharts-legend{display: none;}
</style>
<script type="text/javascript">

$(function () {
    $('#container').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: 'Student Test Score Distribution'
        },
        xAxis: {
            categories: [<?php  echo $obtain_scores2;//echo $obtain_scores; ?>]
        },
        credits: {
            enabled: false
        },
        series: [{
        	name: 'Frequency',
            data: [<?php echo $count2; //echo $count ;  ?>]
        }]
    });
});
</script>

<br/> 

<div class="rawlisting">
	Above is a bar that represents the frequency of student at each scores
	<br/><br/>
	<p><b>Frequency: </b>It is the total number of student(s) at given level.</p>
	<p><b>Score: </b>It is the total score obtained by student(s) at given level.</p>
	
</div>
<br/>
<hr/>


<br/>

<?php 
	//pr($quizoptions);
	//pr($quiztests); 
	//pr($tot_number);
	$countquiztestarr = sizeof($quiztests);
	if($countquiztestarr % 2 == 0){
		$item = $countquiztestarr/2;	
	}else{
		$item = round($countquiztestarr/2,0,PHP_ROUND_HALF_DOWN);
		unset($quiztests[$item]);
	}
	
	$divide_quiztests = array_chunk($quiztests,$item);
	// for upper group
	$arr_upper_group ='';
	$u = 0;
	//pr($divide_quiztests[0]);
	foreach ($divide_quiztests[0] AS $data){
		
		foreach ($data['Quiztestdetail'] as $datum){
			if($datum['isCorrect'] == ACTIVE){
				$arr_upper_group[$datum['quizoption_id']][$u] =  $datum['isCorrect'];
				
			}
			
			
			
		}
		$u++;
	}
	
	// for lower group
	$l = 0;
	foreach ($divide_quiztests[1] AS $data){
		
		foreach ($data['Quiztestdetail'] as $datum){
			if($datum['isCorrect'] == ACTIVE){
				$arr_lower_group[$datum['quizoption_id']] [$l]=  $datum['isCorrect'];
			}
			
		}
		$l++;
	}
	
	//pr($arr_upper_group);
	
?>
<b>Item Analysis</b>
<br/><br/>
<table width="100%" border="1" cellpadding="3" cellspacing="5" class="listable itac">
	<tr>
		<th>Item</th>
		<th>Question Type</th>
		<th>#Correct (Upper group)</th>
		<th>#Correct (Lower group)</th>
		<th>Difficulty Percent</th>
		<th>Difficulty level</th>
		<th>Discrimination Index</th>
	</tr>
	
	<!-- DYNAMIC CONTENT WILL BE PLACED -->
	<?php 
		//pr($arr_upper_group);
		//pr($arr_lower_group);
	?>
	<?php 
	$i = 1;
	foreach ($quizoptiondata as $optiondata):
		if(!empty($arr_upper_group[$optiondata['Quizoption']['id']])){
			$upper = sizeof($arr_upper_group[$optiondata['Quizoption']['id']]);
		}else{
			$upper = '';	
		}
		if(!empty($arr_lower_group[$optiondata['Quizoption']['id']])){
			$lower = sizeof($arr_lower_group[$optiondata['Quizoption']['id']]);
		}else{
			$lower = '';
		}
		$size = sizeof($quiztests);
		$dp = ((($upper + $lower)/$size) * 100);
		
		switch ($dp){
			case $dp <= 10:
				$dl = 'Extremely Difficult';
				break;
			case $dp > 10 && $dp <= 20:
				$dl = 'Extremely Difficult';
				break;
			case $dp > 10 && $dp <= 20:
				$dl = 'Very Difficult';
				break;
			case $dp > 20 && $dp <= 40:
				$dl = 'Difficult';
				break;
			case $dp > 40 && $dp <= 60:
				$dl = 'Okay';
				break;
			case $dp > 60 && $dp <= 80:
				$dl = 'Easy';
				break;
			case $dp > 80 && $dp <= 90:
				$dl = 'Very Easy';
				break;
			case $dp > 90 && $dp <= 100:
				$dl = 'Extremely Easy';
				break;
			
		}
		$di = (($upper - $lower)/$item);
		
	?>
	
	<tr>
		<td><?php echo $i;?></td>
		<td><?php echo $optiondata['Questiontype']['title']?></td>
		<td><?php echo  $upper;?></td>
		<td><?php echo  $lower;?></td>
                <td> <?php  echo round($dp,2) .'%'; ?> </td>
		<td><?php echo $dl;?></td>
                <td><?php echo round($di,2);?></td>
	</tr>
	<?php $i++; endforeach;?>
</table>
<br/> 

<div class="rawlisting">
	Above is a table that represents the distribution of item analysis in several different ways:
	<br/><br/>
	<p><b>Difficulty Percentage: </b>It is the percentage of student's who selected the specific answer choice. The higher the value, the easier the item.</p>
	<p><b>Difficulty Level: </b>It is dirived from the difficulty percentage.</p>
	<p><b>90% or more: </b>It indicate very easy items that you should not use in subsequent tests. If almost all students responded correctly, an item addresses a concept probably not worth testing.</p>
	<p><b>20% or less: </b>indicate very difficult items. If almost all students responded incorrectly, either an item is flawed or students did not understand the concept.</p>
	<br/>
	<p>
	<b>Discrimination Index: </b>
	<br/>
	<br/>
	
	</p>
	<table>
		<tr>
		<td>0.40 or higher:</td>
		<td>Very good items</td>
		</tr>
		
		<tr>
		<td>0.30 to 0.39:</td>
		<td>Good items</td>
		</tr>
		
		<tr>
		<td>0.20 to 0.2:9</td>
		<td>Fairly good items</td>
		</tr>
		
		<tr>
		<td>0.19 or less:</td>
		<td>Poor items</td>
		</tr>
	</table>
	
</div>
<br/>
<b>Item Analysis2</b>
<br/><br/>
<table width="100%" border="1" cellpadding="3" cellspacing="5" class="listable itac">
	<tr>
		<th>Item</th>
		<th>Question Type</th>
		<th>Answer Choice</th>
		<th>Frequency</th>
	</tr>
	<?php
	//pr($quizoptiondata);
	//pr(array_merge($arr_upper_answer,$arr_lower_answer));
	$a=0;
	foreach ($quiztests as $quiztest){
		foreach ($quiztest['Quiztestdetail'] as $datum){
			$arr_answers[$datum['quizoption_id']][$a] =  $datum['result'];
			$arr_iscorrect[$datum['quizoption_id']][$a] =  $datum['isCorrect'];
		}
		$a++;
	}
	$j = 1;
	//pr($arr_iscorrect);
	//pr($arr_answers);
	foreach ($quizoptiondata as $optiondata): 
	$count_total_correct = $count_total_wrong = 0;
	$occurences = '';
	?>
	<tr>
		<td><?php echo $j;?></td>
		<td><?php echo $optiondata['Questiontype']['title']?></td>
	<?php switch ($optiondata['Questiontype']['id']){
			case 1:
			case 2:
			case 5:
				//pr($arr_iscorrect[$optiondata['Quizoption']['id']]);
				$occurences = array_count_values($arr_iscorrect[$optiondata['Quizoption']['id']]);
				//pr($occurences);
				$count_total_correct = (!empty($occurences[1])) ? $occurences[1] : '0';
				$count_total_wrong = (!empty($occurences[2])) ? $occurences[2] : '0';
				?>
			<td><div class="bb">Correct</div><div >Wrong</div></td>
			
			<td><div class="bb"><?php echo $count_total_correct;?></div><div><?php echo $count_total_wrong;?></div></td>
			<?php
				break; 
			case 3: 
			case 7:
				$option_radio = $frequency_mc = '';
				//pr($arr_answers[$optiondata['Quizoption']['id']]);
				//pr($optiondata['Optiondetail']);
				foreach ($optiondata['Optiondetail'] as $opdetail):
					$x = 0;
					$y = 0;
					foreach ($arr_answers[$optiondata['Quizoption']['id']] as $answer){
						//pr($answer);
						if($opdetail['id'] == $answer){
								$x = 1;
							}else{
								$x = 0;
							}
					
						$y = $y + $x ; 
					}
					$option_radio .= "<div class='bb'>".$opdetail['answeroption']."</div>";
					$frequency_mc .= "<div class='bb'>".$y."</div>";
				endforeach;
			?>
			<td> <?php echo $option_radio;?></td>
		
			<td><?php echo $frequency_mc ;?></td>
			<?php 
				break;
			case 4:
				//pr($arr_iscorrect[$optiondata['Quizoption']['id']]);
				$occurences = array_count_values($arr_iscorrect[$optiondata['Quizoption']['id']]);
				//pr($occurences);
				$count_total_correct = (!empty($occurences[1])) ? $occurences[1] : '0';
				$count_total_wrong = (!empty($occurences[2])) ? $occurences[2] : '0';
			?>
                        <td><div class="bb">True</div><div style="text-align:left;">False</div></td>
		
			<td><div class="bb"><?php echo $count_total_correct;?></div><div style="text-align:left;"><?php echo $count_total_wrong;?></div></td>
			<?php
				break;
			case 6: 
			?>
			<?php 
				
				//pr($arr_upper_answer);
				//echo '==================================================================';
				//pr($arr_lower_answer);
				
				//pr($optiondata['Optiondetail']);
				//pr($arr_answers[$optiondata['Quizoption']['id']]);
				
				$option_chk = '';
				$frequency_c = '';
				foreach ($optiondata['Optiondetail'] as $opdetail):
					//pr($arr_upper_answer[$optiondata['Quizoption']['id']]);
					$c = 0;
					$t = 0;
					foreach ($arr_answers[$optiondata['Quizoption']['id']] as $test){
						$abc = explode(',',substr($test,1));
						
						foreach ($abc as $ab){
							if($opdetail['id'] == $ab){
								$c = 1;
								break;
							}else{
								$c = 0;
							}
						}
						$t = $t + $c; 
					}
					$option_chk .= "<div class='bb'>".$opdetail['answeroption']."</div>";
					$frequency_c .= "<div class='bb'>".$t."</div>";
				endforeach;?>
			<td><?php echo $option_chk;?></td>
		
			<td><?php echo $frequency_c;?></td>
			<?php 
				break;
			default :
				//pr($optiondata['Optiondetail']);
				//pr($arr_answers[$optiondata['Quizoption']['id']]);
				$option_scale = $frequency_s = '';
				
				for($i=1;$i<=10;$i++){
					$xx = 0;
					$yy = 0;
					foreach ($arr_answers[$optiondata['Quizoption']['id']] as $answer){
						//pr($answer);
						if($i == $answer){
								$xx = 1;
							}else{
								$xx = 0;
							}
					
						$yy = $yy + $xx ; 
					}
					$option_scale .= "<div class='bb'>". $i ."</div>"; 
					$frequency_s .= "<div class='bb'>". $yy ."</div>"; 
					
				}
				
			?>
			<td><?php echo $option_scale;?></td>
			<td><?php echo $frequency_s;?></td>
	<?php 	break;}?>
		
		
		
		
		
		
	</tr>
	<?php $j++; endforeach;?>
	</table>
	
	<?php 
	//pr($optiondata['Optiondetail']);
	//pr($arr_upper_answer[33]);
	?>
	
</div>
<br/>
<br/>
<br/>