diff --git a/main/exercice/exercise.lib.php b/main/exercice/exercise.lib.php index b7a2b8a9d0..6ac74bd706 100755 --- a/main/exercice/exercise.lib.php +++ b/main/exercice/exercise.lib.php @@ -1260,7 +1260,7 @@ function get_exercise_result_ranking($my_score, $my_exe_id, $exercise_id, $cours foreach($user_list as $user_data) { $user_id = $user_data['user_id']; - $best_attempts[$user_id]= get_best_attempt_score_by_user($user_id,$exercise_id,$course_code,$session_id); + $best_attempts[$user_id]= get_best_attempt_by_user($user_id,$exercise_id,$course_code,$session_id); } $position_data = array(); @@ -1307,7 +1307,7 @@ function get_exercise_result_ranking($my_score, $my_exe_id, $exercise_id, $cours /** * Gets the position of the score based in a given score (result/weight) and the exe_id based in all attempts - * (NO Exercises in LPs ) old funcionality + * (NO Exercises in LPs ) old funcionality by attempt * @param float user score to be compared attention => score/weight * @param int exe id of the exercise (this is necesary because if 2 students have the same score the one with the minor exe_id will have a best position, just to be fair and FIFO) * @param int exercise id @@ -1368,7 +1368,7 @@ function get_exercise_result_ranking_by_attempt($my_score, $my_exe_id, $exercise * Get the best score in a exercise (NO Exercises in LPs ) */ -function get_best_attempt_score($exercise_id, $course_code, $session_id) { +function get_best_attempt_in_course($exercise_id, $course_code, $session_id) { $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id); $best_score_data = array(); $best_score = 0; @@ -1376,7 +1376,7 @@ function get_best_attempt_score($exercise_id, $course_code, $session_id) { foreach($user_results as $result) { if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) { $score = $result['exe_result']/$result['exe_weighting']; - if ($score > $best_score) { + if ($score >= $best_score) { $best_score = $score; $best_score_data = $result; } @@ -1389,22 +1389,22 @@ function get_best_attempt_score($exercise_id, $course_code, $session_id) { /* * Get the best score in a exercise (NO Exercises in LPs ) */ -function get_best_attempt_score_by_user($user_id, $exercise_id, $course_code, $session_id) { +function get_best_attempt_by_user($user_id, $exercise_id, $course_code, $session_id) { $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id); $best_score_data = array(); $best_score = 0; - if (!empty($user_results)) { + if (!empty($user_results)) { foreach($user_results as $result) { if ($result['exe_user_id'] != $user_id) continue; if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) { - $score = $result['exe_result']/$result['exe_weighting']; - if ($score > $best_score) { + $score = $result['exe_result']/$result['exe_weighting']; + if ($score >= $best_score) { $best_score = $score; $best_score_data = $result; } } } - } + } return $best_score_data; } diff --git a/main/inc/lib/events.lib.inc.php b/main/inc/lib/events.lib.inc.php index 8af6c663ab..7a7a3a48ea 100755 --- a/main/inc/lib/events.lib.inc.php +++ b/main/inc/lib/events.lib.inc.php @@ -674,8 +674,7 @@ function get_all_exercise_results($exercise_id, $course_code, $session_id = 0) { $exercise_id = intval($exercise_id); $session_id = intval($session_id); - $sql = "SELECT * FROM $TABLETRACK_EXERCICES WHERE status = '' AND exe_cours_id = '$course_code' AND exe_exo_id = '$exercise_id' AND session_id = $session_id AND orig_lp_id =0 AND orig_lp_item_id = 0 ORDER BY exe_id"; - + $sql = "SELECT * FROM $TABLETRACK_EXERCICES WHERE status = '' AND exe_cours_id = '$course_code' AND exe_exo_id = '$exercise_id' AND session_id = $session_id AND orig_lp_id =0 AND orig_lp_item_id = 0 ORDER BY exe_id"; $res = Database::query($sql); $list = array(); while($row = Database::fetch_array($res,'ASSOC')) { @@ -686,60 +685,9 @@ function get_all_exercise_results($exercise_id, $course_code, $session_id = 0) { $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q; } } - //echo '
'; print_r($list);
 	return $list;
 }
 
-/**
- * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
- * @param   int     exercise id
- * @param   string  course code
- * @param   int     session id
- * @return  array   with the results
- * 
- */
-function get_all_best_exercise_results_by_user($exercise_id, $course_code, $session_id = 0) {
-	$table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
-	$table_track_attempt   = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
-	$course_code           = Database::escape_string($course_code);
-	$exercise_id           = intval($exercise_id);
-	$session_id            = intval($session_id);
-	
-	$sql = "SELECT * FROM $table_track_exercises WHERE status = ''  AND exe_cours_id = '$course_code' AND exe_exo_id = '$exercise_id' AND session_id = $session_id  AND orig_lp_id =0 AND orig_lp_item_id = 0 ORDER BY exe_id";
-	
-	$res = Database::query($sql);
-	$list = array();
-	while($row = Database::fetch_array($res,'ASSOC')) {		
-		$list[$row['exe_id']] = $row;		
-		$sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
-		$res_question = Database::query($sql);
-		while($row_q = Database::fetch_array($res_question,'ASSOC')) {
-			$list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
-		}		
-	}
-	/*
-	echo count($list);
-	echo '
'; - echo '
'; print_r($list);*/
-	//Getting the best results of every student	
-	$best_score_return = array();
-	
-	foreach($list as $student_result) {	    
-	    $user_id = $student_result['exe_user_id'];	    
-	    $current_best_score[$user_id] = $student_result['exe_result'];	    
-	    //echo $current_best_score[$user_id].' - '.$best_score_return[$user_id]['exe_result'].'
'; - if ($current_best_score[$user_id] > $best_score_return[$user_id]['exe_result']) { - $best_score_return[$user_id] = $student_result; - } - } - /* - echo count($best_score_return); - echo '
'; print_r($best_score_return);*/
-	return $best_score_return;
-}
-
-
-
 
 /**
  * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
@@ -795,7 +743,7 @@ function get_all_exercise_results_by_user($user_id, $exercise_id, $course_code,
     $session_id = intval($session_id);
     $user_id    = intval($user_id);
     
-    $sql = "SELECT * FROM $table_track_exercises WHERE status = ''  AND exe_cours_id = '$course_code' AND exe_exo_id = $exercise_id AND session_id = $session_id AND orig_lp_id = 0 AND orig_lp_item_id = 0 AND exe_user_id = $user_id  ORDER by exe_id";    
+    $sql = "SELECT * FROM $table_track_exercises WHERE status = ''  AND exe_user_id = $user_id AND exe_cours_id = '$course_code' AND exe_exo_id = $exercise_id AND session_id = $session_id AND orig_lp_id = 0 AND orig_lp_item_id = 0   ORDER by exe_id";    
     
     $res = Database::query($sql);
     $list = array();    
@@ -812,6 +760,84 @@ function get_all_exercise_results_by_user($user_id, $exercise_id, $course_code,
 }
 
 
+
+/**
+ * Count exercise attempts (NO Exercises in LPs ) from a given exercise id, course, session
+ * @param   int     exercise id
+ * @param   string  course code
+ * @param   int     session id
+ * @return  array   with the results
+ * 
+ */
+function count_exercise_attempts_by_user($user_id, $exercise_id, $course_code, $session_id = 0) {
+	$TABLETRACK_EXERCICES  = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
+	$TBL_TRACK_ATTEMPT     = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
+	$course_code           = Database::escape_string($course_code);
+	$exercise_id           = intval($exercise_id);
+	$session_id            = intval($session_id);
+	$user_id               = intval($user_id);
+	
+	$sql = "SELECT count(*) as count FROM $TABLETRACK_EXERCICES WHERE status = ''  AND exe_user_id = '$user_id' AND exe_cours_id = '$course_code' AND exe_exo_id = '$exercise_id' AND session_id = $session_id  AND orig_lp_id =0 AND orig_lp_item_id = 0 ORDER BY exe_id";
+	//echo '
'; + + $res = Database::query($sql); + $result = 0; + if (Database::num_rows($res) > 0 ) { + $row = Database::fetch_array($res,'ASSOC'); + $result = $row['count']; + } + return $result; +} + +/** + * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user + * @param int exercise id + * @param string course code + * @param int session id + * @return array with the results + * + */ +function get_best_exercise_results_by_user($exercise_id, $course_code, $session_id = 0) { + $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES); + $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); + $course_code = Database::escape_string($course_code); + $exercise_id = intval($exercise_id); + $session_id = intval($session_id); + + $sql = "SELECT * FROM $table_track_exercises WHERE status = '' AND exe_cours_id = '$course_code' AND exe_exo_id = '$exercise_id' AND session_id = $session_id AND orig_lp_id =0 AND orig_lp_item_id = 0 ORDER BY exe_id"; + + $res = Database::query($sql); + $list = array(); + while($row = Database::fetch_array($res,'ASSOC')) { + $list[$row['exe_id']] = $row; + $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}"; + $res_question = Database::query($sql); + while($row_q = Database::fetch_array($res_question,'ASSOC')) { + $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q; + } + } + /* + echo count($list); + echo '
'; + echo '
'; print_r($list);*/
+	//Getting the best results of every student	
+	$best_score_return = array();
+	
+	foreach($list as $student_result) {	    
+	    $user_id = $student_result['exe_user_id'];	    
+	    $current_best_score[$user_id] = $student_result['exe_result'];	    
+	    //echo $current_best_score[$user_id].' - '.$best_score_return[$user_id]['exe_result'].'
'; + if ($current_best_score[$user_id] > $best_score_return[$user_id]['exe_result']) { + $best_score_return[$user_id] = $student_result; + } + } + /* + echo count($best_score_return); + echo '
'; print_r($best_score_return);*/
+	return $best_score_return;
+}
+
+
 /**
  * Gets all exercise events from a Learning Path within a Course 	nd Session
  * @param	int		exercise id
diff --git a/main/inc/lib/tracking.lib.php b/main/inc/lib/tracking.lib.php
index 938a8f2204..528b66bb73 100755
--- a/main/inc/lib/tracking.lib.php
+++ b/main/inc/lib/tracking.lib.php
@@ -2198,7 +2198,7 @@ class Tracking {
             $html .= '                
                 '.get_lang('Exercices').'
                 '.get_lang('Attempts').'                    
-                '.get_lang('LatestAttempt').'
+                '.get_lang('BestAttempt').'
                 '.get_lang('Ranking').'
                 '.get_lang('BestResultInCourse').'
                 '.get_lang('Statistics').' '.Display :: return_icon('info3.gif', get_lang('OnlyBestResultsPerStudent'), array('align' => 'absmiddle', 'hspace' => '3px')).'                                        
@@ -2212,39 +2212,14 @@ class Tracking {
             $result_exercices = Database::query($sql_exercices);
             $to_graph_exercise_result = array();
             if (Database::num_rows($result_exercices) > 0) {
-                
+                $score = $weighting = $exe_id = 0;
                 while ($exercices = Database::fetch_array($result_exercices)) {
                     //if ($exercices['id'] != 3) continue;
-                    $score = $weighting = $attempts = 0;                        
-                    $exercise_stats      = get_all_exercise_results($exercices['id'], $course_info['code'], $session_id);                    
-                    $best_exercise_stats = get_all_best_exercise_results_by_user($exercices['id'], $course_info['code'], $session_id);
-                   
-                    
-                    //User attempts we assume the latest item in the loop is the latest attempt
-                    if (!empty($exercise_stats)) {
-                        //$best_score = 0; $best_score_array = array();
-                        foreach($exercise_stats as $exercise_stat) {
-                            if ($exercise_stat['exe_user_id'] == $user_id) {
-                                
-                               //Always getting the latest attempt
-                               $score          = $exercise_stat['exe_result'];
-                               $weighting      = $exercise_stat['exe_weighting'];                               
-                               $exe_id         = $exercise_stat['exe_id'];
-                               
-                               //Use this to take the average
-                               //$score          = $score + $exercise_stat['exe_result'];
-                               //$weighting      = $weighting + $exercise_stat['exe_weighting'];
-                               
-                               //Getting my best score
-                               /*
-                               if ($score > $best_score ) {
-                                   $best_score = $score;
-                                   $best_score_array = $exercise_stat;
-                               }*/                       
-                               $attempts++;
-                            }
-                        }
-                    }
+                    $score = $weighting = $attempts = 0;
+                    //Getting count of attempts by user
+                    $attempts      = count_exercise_attempts_by_user(api_get_user_id(), $exercices['id'], $course_info['code'], $session_id);   
+                    //For graphics                 
+                    $best_exercise_stats = get_best_exercise_results_by_user($exercices['id'], $course_info['code'], $session_id);
                     $to_graph_exercise_result[$exercices['id']] = array('title'=>$exercices['title'], 'data'=>$best_exercise_stats);
                     
                     $html .= '';
@@ -2259,21 +2234,30 @@ class Tracking {
                         $graph = $normal_graph = null;
 
                         //Getting best results 
-                        $best_score_data = get_best_attempt_score($exercices['id'], $course_info['code'], $session_id);     
+                        $best_score_data = get_best_attempt_in_course($exercices['id'], $course_info['code'], $session_id);     
                         $best_score      = show_score($best_score_data['exe_result'], $best_score_data['exe_weighting']);                       
                                                
                         if ($attempts > 0) {
-                            //$latest_attempt_url .= ' '.Display::return_icon('quiz.gif', get_lang('Quiz')).' ';
-                            $latest_attempt_url .= '../exercice/exercise_show.php?origin=myprogress&id='.$exe_id.'&cidReq='.$course_info['code'].'&id_session='.$session_id;
-                            $percentage_score_result = Display::url(show_score($score, $weighting), $latest_attempt_url);
-                            $my_score = 0;                            
-                            if (!empty($weighting)) {                                                           
-                                $my_score = $score/$weighting;
-                            }                            
-                            $position = get_exercise_result_ranking($my_score, $exe_id, $exercices['id'], $course_info['code'], $session_id);
+                            $exercise_stat = get_best_attempt_by_user(api_get_user_id(), $exercices['id'], $course_info['code'], $session_id);                       
+                            if (!empty($exercise_stat)) {
                             
-                            $graph         = self::generate_exercise_result_thumbnail_graph($to_graph_exercise_result[$exercices['id']]);                        
-                            $normal_graph  = self::generate_exercise_result_graph($to_graph_exercise_result[$exercices['id']]);                            
+                                //Always getting the BEST attempt
+                                $score          = $exercise_stat['exe_result'];
+                                $weighting      = $exercise_stat['exe_weighting'];                               
+                                $exe_id         = $exercise_stat['exe_id'];
+                                   
+                                //$latest_attempt_url .= ' '.Display::return_icon('quiz.gif', get_lang('Quiz')).' ';
+                                $latest_attempt_url .= '../exercice/exercise_show.php?origin=myprogress&id='.$exe_id.'&cidReq='.$course_info['code'].'&id_session='.$session_id;
+                                $percentage_score_result = Display::url(show_score($score, $weighting), $latest_attempt_url);
+                                $my_score = 0;
+                                if (!empty($weighting) && intval($weighting) != 0) {                                                        
+                                    $my_score = $score/$weighting;
+                                }                            
+                                $position = get_exercise_result_ranking($my_score, $exe_id, $exercices['id'], $course_info['code'], $session_id);                                
+                                
+                                $graph         = self::generate_exercise_result_thumbnail_graph($to_graph_exercise_result[$exercices['id']]);                        
+                                $normal_graph  = self::generate_exercise_result_graph($to_graph_exercise_result[$exercices['id']]);     
+                            }                       
                         }
                                                 
                         echo Display::div($normal_graph, array('id'=>'main_graph_'.$exercices['id'],'class'=>'dialog', 'style'=>'display:none') );
diff --git a/main/session/index.php b/main/session/index.php
index 52bc5ff9b9..09a0584332 100644
--- a/main/session/index.php
+++ b/main/session/index.php
@@ -155,7 +155,7 @@ foreach($final_array as $session_data) {
         if (!empty($course_data['exercises'])) {
             //Exercises            
             foreach ($course_data['exercises'] as $my_exercise_id => $exercise_data) {
-                $best_score_data = get_best_attempt_score($my_exercise_id, $my_course_code, $session_id);     
+                $best_score_data = get_best_attempt_in_course($my_exercise_id, $my_course_code, $session_id);     
                 $best_score      = show_score($best_score_data['exe_result'], $best_score_data['exe_weighting']);
                 //Exercise results                              
                 $counter = 1;