Julio Montoya 13 rokov pred
rodič
commit
305f341ab0

+ 67 - 39
main/exercice/exercise.class.php

@@ -19,7 +19,7 @@ define('EXERCISE_FEEDBACK_TYPE_EXAM',       2); //NoFeedback 	 - Show score only
 
 define('EXERCISE_MAX_NAME_SIZE',            80);
 
-$debug = 0; //All exercise scripts should depend in this debug variable
+$debug = true; //All exercise scripts should depend in this debug variable
 
 require_once dirname(__FILE__).'/../inc/lib/exercise_show_functions.lib.php';
 
@@ -1818,18 +1818,18 @@ class Exercise {
 	public function manage_answer($exeId, $questionId, $choice, $from = 'exercise_show', $exerciseResultCoordinates = array(), $saved_results = true, $from_database = false, $show_result = true, $propagate_neg = 0, $hotspot_delineation_result = array()) {
 		global $_configuration, $feedback_type, $debug;		
 		require_once api_get_path(LIBRARY_PATH).'geometry.lib.php';
-
-		if ($debug) error_log('manage_answer called exe_id '.$exeId);
-		if ($debug) error_log('manage_answer $from  '.$from);
-		if ($debug) error_log('manage_answer $saved_results  '.$saved_results);
-		if ($debug) error_log('manage_answer $from_database '.$from_database);
-		if ($debug) error_log('manage_answer $show_result '.$show_result);
-		if ($debug) error_log('manage_answer $propagate_neg '.$propagate_neg);
-		if ($debug) error_log('manage_answer $hotspot_delineation_result '.print_r($hotspot_delineation_result, 1));
+        
+        if ($debug) error_log("<------ manage_answer ------> ");
+		if ($debug) error_log('manage_answer called exe_id: '.$exeId);
+		if ($debug) error_log('manage_answer $from:  '.$from);
+		if ($debug) error_log('manage_answer $saved_results: '.$saved_results);
+		if ($debug) error_log('manage_answer $from_database: '.$from_database);
+		if ($debug) error_log('manage_answer $show_result: '.$show_result);
+		if ($debug) error_log('manage_answer $propagate_neg: '.$propagate_neg);
+		if ($debug) error_log('manage_answer $hotspot_delineation_result: '.print_r($hotspot_delineation_result, 1));
 			
 		$extra_data = array();
-		$html = '';
-		 
+				 
 		$questionId   = intval($questionId);
 		$exeId        = intval($exeId);
 		$TBL_TRACK_ATTEMPT      = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
@@ -1868,9 +1868,8 @@ class Exercise {
 		$objAnswerTmp = new Answer($questionId);
 		$nbrAnswers = $objAnswerTmp->selectNbrAnswers();
 
-		if ($debug) error_log('Count of answers :'.$nbrAnswers);
-		if ($debug) error_log('$answerType : '.$answerType);
-
+		if ($debug) error_log('Count of answers: '.$nbrAnswers);
+		if ($debug) error_log('$answerType: '.$answerType);
 
 		if ($answerType == FREE_ANSWER || $answerType == ORAL_EXPRESSION) {
 			$nbrAnswers = 1;
@@ -1916,18 +1915,26 @@ class Exercise {
 		$organs_at_risk_hit = 0;
 
 		$questionScore = 0;
-		 
+		if ($debug) error_log('Start answer loop ');
+        
+        $answer_correct_array = array();
+        
 		for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
 			$answer             = $objAnswerTmp->selectAnswer($answerId);			
 			$answerComment      = $objAnswerTmp->selectComment($answerId);
 			$answerCorrect      = $objAnswerTmp->isCorrect($answerId);
 			$answerWeighting    = $objAnswerTmp->selectWeighting($answerId);
 			$numAnswer          = $objAnswerTmp->selectAutoId($answerId);
+            
+            $answer_correct_array[$answerId] = (bool)$answerCorrect;
+            
+            if ($debug) error_log("answer auto id: $numAnswer ");
+            if ($debug) error_log("answer correct : $answerCorrect ");
 
 			//delineation
 			$delineation_cord   = $objAnswerTmp->selectHotspotCoordinates(1);
 			$answer_delineation_destination=$objAnswerTmp->selectDestination(1);
-
+            
 			switch ($answerType) {
 				// for unique answer
 				case UNIQUE_ANSWER :
@@ -1982,32 +1989,34 @@ class Exercise {
 						//if no result then the user just hit don't know
 						$studentChoice = 3;
 						$questionScore  +=  $doubt_score;
-					}
-					
+					}					
 					$totalScore = $questionScore;
 					break;
-				case MULTIPLE_ANSWER :
+				case MULTIPLE_ANSWER : //2                    
 					if ($from_database) {
-						$choice=array();
-						$queryans = "SELECT answer FROM ".$TBL_TRACK_ATTEMPT." where exe_id = '".$exeId."' and question_id= '".$questionId."'";
+						$choice = array();
+						$queryans = "SELECT answer FROM ".$TBL_TRACK_ATTEMPT." WHERE exe_id = '".$exeId."' AND question_id= '".$questionId."'";                        
 						$resultans = Database::query($queryans);
 						while ($row = Database::fetch_array($resultans)) {
 							$ind = $row['answer'];
 							$choice[$ind] = 1;
 						}
-						$numAnswer=$objAnswerTmp->selectAutoId($answerId);
-						$studentChoice=$choice[$numAnswer];
-						if ($studentChoice) {
-							$questionScore  +=$answerWeighting;
-							$totalScore     +=$answerWeighting;
+						$numAnswer = $objAnswerTmp->selectAutoId($answerId);
+						$studentChoice = $choice[$numAnswer];  
+                        $real_answers[$answerId] = (bool)$studentChoice;
+						if ($studentChoice == $answerCorrect) {
+							$questionScore  +=$answerWeighting;							                            
 						}
 					} else {
-						$studentChoice=$choice[$numAnswer];
-						if ($studentChoice) {
+						$studentChoice = $choice[$numAnswer];
+						if ($studentChoice == $answerCorrect) {
 							$questionScore  +=$answerWeighting;
-							$totalScore     +=$answerWeighting;
 						}
+                        $real_answers[$answerId] = (bool)$studentChoice;
 					}
+                    $totalScore     +=$answerWeighting;
+                    
+                    if ($debug) error_log("studentChoice: $studentChoice");
 					break;
 				case MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE:
 					if ($from_database) {
@@ -2336,10 +2345,8 @@ class Exercise {
 						break;
 					}
 					// for hotspot with no order
-				case HOT_SPOT :
-					
-					if ($from_database) {
-						
+				case HOT_SPOT :					
+					if ($from_database) {						
 						if ($show_result) {
 							$TBL_TRACK_HOTSPOT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
 							$query = "SELECT hotspot_correct FROM ".$TBL_TRACK_HOTSPOT." WHERE hotspot_exe_id = '".$exeId."' and hotspot_question_id= '".$questionId."' AND hotspot_answer_id='".Database::escape_string($answerId)."'";
@@ -2405,11 +2412,14 @@ class Exercise {
 					$_SESSION['hotspot_dest'][1]	= $answer_delineation_destination;
 					break;
 			} // end switch Answertype
+            
 
-			global $origin, $debug;
+			global $origin;
 
 			if ($show_result) {
-
+                
+                if ($debug) error_log('show result '.$show_result);
+                
 				if ($from == 'exercise_result') {
 					if ($debug) error_log('Showing questions $from '.$from);
 					//display answers (if not matching type, or if the answer is correct)
@@ -2821,7 +2831,10 @@ class Exercise {
 					}
 				}
 			}
+            if ($debug) error_log(' ------ ');
 		} // end for that loops over all answers of the current question
+        
+        if ($debug) error_log('-- end answer loop --');
 
 		
 		// destruction of Answer
@@ -2833,7 +2846,7 @@ class Exercise {
 		}
 
 		$final_answer = true;
-		foreach($real_answers as $my_answer) {
+		foreach ($real_answers as $my_answer) {
 			if (!$my_answer) {
 				$final_answer = false;
 			}
@@ -2848,6 +2861,19 @@ class Exercise {
 				$totalScore         += $answerWeighting;
 			}
 		}
+        
+        //Fixes multiple answer question in order to be exact 
+        if ($answerType == MULTIPLE_ANSWER) {
+            $diff = @array_diff($answer_correct_array, $real_answers);
+            
+            if ($debug) error_log(" answer_correct_array: ".print_r($answer_correct_array, 1)."");
+            if ($debug) error_log(" real_answers: ".print_r($real_answers, 1)."");
+            if ($debug) error_log(" diff: $diff");
+            
+            if (!empty($diff)) {
+                $questionScore = 0;
+            }
+        } 
 
 		$extra_data = array('final_overlap' => $final_overlap, 'final_missing'=>$final_missing, 'final_excess'=> $final_excess,
         					'overlap_color' => $overlap_color, 'missing_color'=>$missing_color, 'excess_color'=> $excess_color,
@@ -3015,14 +3041,15 @@ class Exercise {
 		// Store results directly in the database
 		// For all in one page exercises, the results will be
 		// stored by exercise_results.php (using the session)
-
+        
 		if ($saved_results) {
+            if ($debug) error_log("Save question results $saved_results");
+            if ($debug) error_log(print_r($choice ,1 ));
+            
 			if (empty($choice)) {
 				$choice = 0;
 			}
 			if ($answerType ==  MULTIPLE_ANSWER_TRUE_FALSE || $answerType ==  MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE ) {
-				
-				if ($debug) error_log(print_r($choice ,1 ));
 				if ($choice != 0) {
 					$reply = array_keys($choice);
 					for ($i = 0; $i < sizeof($reply); $i++) {
@@ -3036,6 +3063,7 @@ class Exercise {
 			} elseif ($answerType == MULTIPLE_ANSWER) {
 				if ($choice != 0) {
 					$reply = array_keys($choice);
+                    if ($debug) error_log("reply ".print_r($reply, 1)."");
 					for ($i = 0; $i < sizeof($reply); $i++) {
 						$ans = $reply[$i];
 						exercise_attempt($questionScore, $ans, $quesId, $exeId, $i, $this->id);

+ 2 - 1
main/exercice/exercise_result.php

@@ -45,7 +45,7 @@ api_protect_course_script(true);
 // Database table definitions
 $main_admin_table       = Database::get_main_table(TABLE_MAIN_ADMIN);
 
-if($debug>0){error_log('Entered exercise_result.php: '.print_r($_POST,1));}
+if ($debug){ error_log('Entering exercise_result.php: '.print_r($_POST,1));}
 
 // general parameters passed via POST/GET
 if ( empty ( $origin ) ) {                  $origin                 = Security::remove_XSS($_REQUEST['origin']);}
@@ -165,6 +165,7 @@ $total_score = $total_weight = 0;
 
 // Loop over all question to show results for each of them, one by one
 if (!empty($question_list)) {
+    if ($debug){ error_log('Loopinf question_list '.print_r($question_list,1));}
 	foreach ($question_list as $questionId) {
 	    // destruction of the Question object
 		unset($objQuestionTmp);

+ 2 - 2
main/exercice/exercise_show.php

@@ -42,11 +42,11 @@ $TBL_QUESTIONS         	= Database::get_course_table(TABLE_QUIZ_QUESTION);
 $TBL_REPONSES          	= Database::get_course_table(TABLE_QUIZ_ANSWER);
 $main_user_table 		= Database::get_main_table(TABLE_MAIN_USER);
 $main_course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
-$TBL_TRACK_EXERCICES	= Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
+$TBL_TRACK_EXERCICES    	= Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
 $TBL_TRACK_ATTEMPT		= Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
 
 // General parameters passed via POST/GET
-if($debug) { error_log('Entered exercise_result.php: '.print_r($_POST,1)); }
+if ($debug) { error_log('Entered exercise_result.php: '.print_r($_POST,1)); }
 
 if ( empty ( $formSent ) ) {            $formSent       = $_REQUEST['formSent']; }
 if ( empty ( $exerciseResult ) ) {      $exerciseResult = $_SESSION['exerciseResult'];}

+ 56 - 78
main/exercice/exercise_submit.php

@@ -159,7 +159,6 @@ if ($time_control) {
 	$current_expired_time_key = generate_time_control_key($objExercise->id);
 }
 
-
 $show_clock = true;
 $user_id = api_get_user_id();
 if ($objExercise->selectAttempts() > 0) {
@@ -220,42 +219,44 @@ if ($objExercise->selectAttempts() > 0) {
 
 
 
-if ($debug) { error_log("4. Setting the exe_id $exe_id");} ;
+if ($debug) { error_log("4. Setting the exe_id: $exe_id");} ;
 
 //5. Getting user exercise info (if the user took the exam before) - generating exe_id
 //var_dump($safe_lp_id.' - '.$safe_lp_item_id.' - '.$safe_lp_item_view_id);
 $exercise_stat_info = $objExercise->get_stat_track_exercise_info($safe_lp_id, $safe_lp_item_id, $safe_lp_item_view_id);
 
 if (empty($exercise_stat_info)) {
+    if ($debug)  error_log('5  $exercise_stat_info is empty ');
 	$total_weight = 0;
 	$questionList = $objExercise->get_validated_question_list();	
-	foreach($questionList as $question_id) {
+	foreach ($questionList as $question_id) {
 		$objQuestionTmp = Question::read($question_id);
 		$total_weight += floatval($objQuestionTmp->weighting);
 	}
 	$clock_expired_time = '';
+    
 	if ($time_control) {
 		$expected_time = $current_timestamp + $total_seconds;
 		
-		if ($debug)  error_log('6.3. $current_timestamp '.$current_timestamp);
-		if ($debug)  error_log('6.4. $expected_time '.$expected_time);
+		if ($debug)  error_log('5.1. $current_timestamp '.$current_timestamp);
+		if ($debug)  error_log('5.2. $expected_time '.$expected_time);
 		
 		$clock_expired_time 	= api_get_utc_datetime($expected_time);
-		if ($debug) error_log('6.5. $expected_time '.$clock_expired_time);
+		if ($debug) error_log('5.3. $expected_time '.$clock_expired_time);
 		
 		//Sessions  that contain the expired time
 		$_SESSION['expired_time'][$current_expired_time_key] 	 = $clock_expired_time;
-		if ($debug) {
-			error_log('6.6. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key] );
-		};
+		if ($debug) { error_log('5.4. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key] ); };
 	}
 	$exe_id = $objExercise->save_stat_track_exercise_info($clock_expired_time, $safe_lp_id, $safe_lp_item_id, $safe_lp_item_view_id, $questionList, $total_weight);
 	$exercise_stat_info = $objExercise->get_stat_track_exercise_info($safe_lp_id, $safe_lp_item_id, $safe_lp_item_view_id);	
+    if ($debug)  error_log("5.5  exercise_stat_info[] exists getting exe_id $exe_id");
 } else {
 	$exe_id = $exercise_stat_info['exe_id'];
+    if ($debug)  error_log("5  exercise_stat_info[] exists getting exe_id $exe_id ");
 }
 
-if ($debug) { error_log('5. $objExercise->get_stat_track_exercise_info function called::  '.print_r($exercise_stat_info, 1)); };
+if ($debug) { error_log('6. $objExercise->get_stat_track_exercise_info function called::  '.print_r($exercise_stat_info, 1)); };
 
 if (!empty($exercise_stat_info['questions_to_check'])) {
 	$my_remind_list = $exercise_stat_info['questions_to_check'];
@@ -264,29 +265,30 @@ if (!empty($exercise_stat_info['questions_to_check'])) {
 }
 
 $params = 'exe_id='.$exe_id.'&exerciseId='.$exerciseId.'&origin='.$origin.'&learnpath_id='.$learnpath_id.'&learnpath_item_id='.$learnpath_item_id.'&learnpath_item_view_id='.$learnpath_item_view_id;
-if ($debug) { error_log("5.1 params: ->  $params"); };
+if ($debug) { error_log("6.1 params: ->  $params"); };
 
 if ($reminder == 2 && empty($my_remind_list)) {	
+    if ($debug) { error_log("6.2 calling the exercise_reminder.php "); };
 	header('Location: exercise_reminder.php?'.$params);
 	exit;
 }
 
 /*
- * 6. Loading Time control parameters
+ * 7. Loading Time control parameters
  * If the expired time is major that zero(0) then the expired time is compute on this time. Disable for learning path
  */
 
 if ($time_control) {
-	if ($debug) error_log('6.1. Time control is enabled');	
-	if ($debug) {error_log('6.2. $current_expired_time_key  '.$current_expired_time_key); };
-	if ($debug) { error_log('6.2. $_SESSION[expired_time][$current_expired_time_key]  '.$_SESSION['expired_time'][$current_expired_time_key]); };
+	if ($debug) error_log('7.1. Time control is enabled');	
+	if ($debug) error_log('7.2. $current_expired_time_key  '.$current_expired_time_key);
+	if ($debug) error_log('7.3. $_SESSION[expired_time][$current_expired_time_key]  '.$_SESSION['expired_time'][$current_expired_time_key]);
 	
     if (!isset($_SESSION['expired_time'][$current_expired_time_key])) {
         //Timer - Get expired_time for a student        
         if (!empty($exercise_stat_info)) {
-        	if ($debug) {error_log('6.3 Seems that the session ends and the user want to retake the exam'); };        	
+        	if ($debug) {error_log('7.4 Seems that the session ends and the user want to retake the exam'); };        	
 	        $expired_time_of_this_attempt = $exercise_stat_info['expired_time_control'];
-			if ($debug) {error_log('$expired_time_of_this_attempt: '.$expired_time_of_this_attempt); }			
+			if ($debug) {error_log('7.5 $expired_time_of_this_attempt: '.$expired_time_of_this_attempt); }			
 	        //Get the last attempt of an exercice
 	    	$last_attempt_date = get_last_attempt_date_of_exercise($exercise_stat_info['exe_id']);
 	    	
@@ -299,31 +301,33 @@ if ($time_control) {
 	    		$diff = $current_timestamp - api_strtotime($last_attempt_date,'UTC');
 	    		$last_attempt_date = api_get_utc_datetime(api_strtotime($last_attempt_date,'UTC') + $diff);
 	    	}
-	        if ($debug) {error_log('6.4. $last_attempt_date: '.$last_attempt_date); }
+	        if ($debug) {error_log('7.6. $last_attempt_date: '.$last_attempt_date); }
 	        	
 	        //New expired time - it is due to the possible closure of session
 	        $new_expired_time_in_seconds = api_strtotime($expired_time_of_this_attempt, 'UTC') - api_strtotime($last_attempt_date,'UTC');
-	        if ($debug) {error_log('6.5. $new_expired_time_in_seconds: '.$new_expired_time_in_seconds); }	        
+	        if ($debug) {error_log('7.7. $new_expired_time_in_seconds: '.$new_expired_time_in_seconds); }	        
 	        
 	        $expected_time	= $current_timestamp + $new_expired_time_in_seconds;
-	        if ($debug) {error_log('6.6. $expected_time1: '.$expected_time); }
+	        if ($debug) {error_log('7.8. $expected_time1: '.$expected_time); }
 	        	        			
 	        $clock_expired_time  = api_get_utc_datetime($expected_time);
-	        if ($debug) {error_log('6.7. $clock_expired_time: '.$clock_expired_time); }
+	        if ($debug) {error_log('7.9. $clock_expired_time: '.$clock_expired_time); }
 	        	              		
 			// First we update the attempt to today
 			// How the expired time is changed into "track_e_exercices" table,then the last attempt for this student should be changed too,so
 	        $sql_track_e_exe = "UPDATE $exercice_attemp_table SET tms = '".api_get_utc_datetime()."' WHERE exe_id = '".$exercise_stat_info['exe_id']."' AND tms = '".$last_attempt_date."' ";
-	        if ($debug) {error_log('6.8. $sql_track_e_exe2: '.$sql_track_e_exe); }
+	        if ($debug) {error_log('7.10. $sql_track_e_exe2: '.$sql_track_e_exe); }
 	        Database::query($sql_track_e_exe);
 	        	
 	        //Sessions  that contain the expired time
 	        $_SESSION['expired_time'][$current_expired_time_key] 		= $clock_expired_time;
-	        if ($debug) {error_log('6.9. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key] ); };
+	        if ($debug) {error_log('7.11. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key] ); };
         }
     } else {
         $clock_expired_time =  $_SESSION['expired_time'][$current_expired_time_key];
     }
+} else {
+    if ($debug) { error_log("7 No time control"); };
 }
 
 // Get time left for exipiring time
@@ -337,24 +341,6 @@ if ($time_control) { //Sends the exercice form when the expired time is finished
 	$htmlHeadXtra[] = $objExercise->show_time_control_js($time_left);
 }
 
-/*
-if ($objExercise->type == ONE_PER_PAGE && $objExercise->feedbacktype != EXERCISE_FEEDBACK_TYPE_DIRECT) {	
-	if (!empty($exercise_stat_info)) {
-        $exe_id = $exercise_stat_info['exe_id'];
-        if ($_SERVER['REQUEST_METHOD'] != 'POST') {            
-            $recorded['questionList'] = explode(',', $exercise_stat_info['data_tracking']);            
-            $query = 'SELECT * FROM ' . $exercice_attemp_table . ' WHERE exe_id = ' . $exercise_stat_info['exe_id'] . ' ORDER BY tms ASC';            
-            $result = Database::query($query);
-            while ($row = Database :: fetch_array($result,'ASSOC')) {
-                $recorded['exerciseResult'][$row['question_id']] = 1;
-            }
-            $exerciseResult = $_SESSION['exerciseResult'] = $recorded['exerciseResult'];            
-            $current_question = count($recorded['exerciseResult']);
-            $current_question++;
-            $questionList = $_SESSION['questionList'] = $recorded['questionList'];
-        }
-    }
-}*/
 // if the user has submitted the form
 
 $exercise_title			= $objExercise->selectTitle();
@@ -378,7 +364,7 @@ if (!isset($_SESSION['questionList'])) {
 	}
 }
 
-if ($debug) error_log('7 Question list loaded '.print_r($questionList, 1));
+if ($debug) error_log('8. Question list loaded '.print_r($questionList, 1));
 
 $quizStartTime = time();
 api_session_register('quizStartTime');
@@ -390,7 +376,7 @@ if (!empty($questionList)) {
 }
 
 if ($formSent && isset($_POST)) {
-    if ($debug > 0) { error_log('8. $formSent was set'); }
+    if ($debug) { error_log('9. $formSent was set'); }
 
     // Initializing
     if (!is_array($exerciseResult)) {
@@ -406,13 +392,13 @@ if ($formSent && isset($_POST)) {
     
     // if the user has answered at least one question
     if (is_array($choice)) {
-        if ($debug) { error_log('8.1. $choice is an array '.print_r($choice, 1)); } 	
+        if ($debug) { error_log('9.1. $choice is an array '.print_r($choice, 1)); } 	
         // Also store hotspot spots in the session ($exerciseResultCoordinates
         // will be stored in the session at the end of this script)
 
         if (isset($_POST['hotspot'])) {
             $exerciseResultCoordinates = $_POST['hotspot'];
-            if ($debug) { error_log('8.2. $_POST[hotspot] data '.print_r($exerciseResultCoordinates, 1)); }     
+            if ($debug) { error_log('9.2. $_POST[hotspot] data '.print_r($exerciseResultCoordinates, 1)); }     
         }        	
         if ($objExercise->type == ALL_ON_ONE_PAGE) {
             // $exerciseResult receives the content of the form.
@@ -433,15 +419,15 @@ if ($formSent && isset($_POST)) {
                     $choice = $exerciseResult[$questionId];                    
                     if (isset($exe_id)) {
                     	//Manage the question and answer attempts
-                        if ($debug > 0) { error_log('8.3. manage_answer exe_id: '.$exe_id.' - $questionId: '.$questionId.' Choice'.print_r($choice,1)); }
+                        if ($debug) { error_log('8.3. manage_answer exe_id: '.$exe_id.' - $questionId: '.$questionId.' Choice'.print_r($choice,1)); }
                     	$objExercise->manage_answer($exe_id, $questionId, $choice,'exercise_show',$exerciseResultCoordinates, true, false,false, $objExercise->propagate_neg);
                     }
                     //END of saving and qualifying
                 }
             }
         }
-        if ($debug > 0) { error_log('8.3.  $choice is an array - end'); }
-        if ($debug > 0) { error_log('8.4.  $exerciseResult '.print_r($exerciseResult,1)); }
+        if ($debug) { error_log('9.3.  $choice is an array - end'); }
+        if ($debug) { error_log('9.4.  $exerciseResult '.print_r($exerciseResult,1)); }
     }
     
     
@@ -452,11 +438,11 @@ if ($formSent && isset($_POST)) {
 
     // if all questions on one page OR if it is the last question (only for an exercise with one question per page)
     
-    if (($objExercise->type == ALL_ON_ONE_PAGE || $current_question >= $question_count)) {       
+    if (($objExercise->type == ALL_ON_ONE_PAGE || $current_question >= $question_count)) {          
         if (api_is_allowed_to_session_edit()) {
             // goes to the script that will show the result of the exercise
             if ($objExercise->type == ALL_ON_ONE_PAGE) {
-                if ($debug) { error_log('Exercise ALL_ON_ONE_PAGE -> Redirecting to exercise_result.php'); }
+                if ($debug) { error_log('10. Exercise ALL_ON_ONE_PAGE -> Redirecting to exercise_result.php'); }
                 
                 //We check if the user attempts before sending to the exercise_result.php                
                 if ($objExercise->selectAttempts() > 0) {
@@ -492,23 +478,20 @@ if ($formSent && isset($_POST)) {
                     if ($debug) { error_log('Updating track_e_exercises '.$update_query); }                    
                     Database::query($update_query);*/
                 }                
-                if ($debug) { error_log('Redirecting to exercise_show.php'); }
+                if ($debug) { error_log('10. Redirecting to exercise_show.php'); }
                 //header("Location: exercise_show.php?id=$exe_id&origin=$origin&learnpath_id=$safe_lp_id&learnpath_item_id=$safe_lp_item_id&learnpath_item_view_id=$safe_lp_item_view_id");
                 header("Location: exercise_result.php?exe_id=$exe_id&origin=$origin&learnpath_id=$safe_lp_id&learnpath_item_id=$safe_lp_item_id&learnpath_item_view_id=$safe_lp_item_view_id");
                 exit;
             }            
         } else {
-            if ($debug) { error_log('Redirecting to exercise_submit.php'); }
+            if ($debug) { error_log('10. Redirecting to exercise_submit.php'); }
             header("Location: exercise_submit.php?exerciseId=$exerciseId&origin=$origin");
             exit;            
         }
     }
-    
-    if (!empty($remind_list)) {
-         //header("Location: exercise_reminder.php?origin=$origin&learnpath_id=$safe_lp_id&learnpath_item_id=$safe_lp_item_id&learnpath_item_view_id=$safe_lp_item_view_id");
-         exit;
-    }
-    if ($debug > 0) { error_log('$formSent was set - end'); }
+    if ($debug) { error_log('11. $formSent was set - end'); }
+} else {
+    if ($debug) { error_log('9. $formSent was NOT sent'); }
 }
 
 // if questionNum comes from POST and not from GET
@@ -520,12 +503,13 @@ if (!$current_question || $_REQUEST['num']) {
         $current_question++;
     }
 }
+
 if ($question_count != 0) {
 	if (($objExercise->type == ALL_ON_ONE_PAGE || $current_question > $question_count)) {       
 	    if (api_is_allowed_to_session_edit()) {
 	        // goes to the script that will show the result of the exercise
 	        if ($objExercise->type == ALL_ON_ONE_PAGE) {
-	            if ($debug) { error_log('Exercise ALL_ON_ONE_PAGE -> Redirecting to exercise_result.php'); }
+	            if ($debug) { error_log('12. Exercise ALL_ON_ONE_PAGE -> Redirecting to exercise_result.php'); }
 	            
 	            //We check if the user attempts before sending to the exercise_result.php
 	            if ($objExercise->selectAttempts() > 0) {
@@ -554,10 +538,10 @@ if ($question_count != 0) {
 	            
 	                if ($check) {
 	                	$sql_exe_result = "";                    	
-	                    if ($debug) { error_log('exercise_time_control_is_valid is valid'); }
+	                    if ($debug) { error_log('12. exercise_time_control_is_valid is valid'); }
 	                } else {
 	                	$sql_exe_result = ", exe_result = 0";
-	                    if ($debug) { error_log('exercise_time_control_is_valid is NOT valid then exe_result = 0 '); }
+	                    if ($debug) { error_log('12. exercise_time_control_is_valid is NOT valid then exe_result = 0 '); }
 	                }
 	                /*
 	                //Clean incomplete - @todo why setting to blank the status?                  
@@ -626,12 +610,6 @@ if (api_is_course_admin() && $origin != 'learnpath') {
     }
     echo '</div>';
 }
-/*
-$exercise_header = Display::div($exercise_title, array('class'=>'exercise_title'));
-if (!empty($exercise_description)) {
-	$exercise_header .= Display::div($exercise_description, array('class'=>'exercise_description'));
-}
-echo Display::div($exercise_header, array('class'=>'exercise_header'));*/
 
 $is_visible_return = $objExercise->is_visible($learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
 if ($is_visible_return['value'] == false) {
@@ -644,7 +622,7 @@ if ($is_visible_return['value'] == false) {
 
 $limit_time_exists = (($objExercise->start_time != '0000-00-00 00:00:00') || ($objExercise->end_time != '0000-00-00 00:00:00')) ? true : false;
 
-if ($limit_time_exists) {	    
+if ($limit_time_exists) {    
     $exercise_start_time 	= api_strtotime($objExercise->start_time, 'UTC');
     $exercise_end_time 		= api_strtotime($objExercise->end_time, 'UTC');
     $time_now 				= time();
@@ -699,16 +677,15 @@ if ($origin != 'learnpath') {
    echo '<div id="highlight-plugin" class="glossary-content">';
 }
 
-//var_dump('$remind_question_id . '.$remind_question_id);
-
 if ($reminder == 2)  {
+    if ($debug) { error_log('. $reminder == 2'); }
  
     $data_tracking  = $exercise_stat_info['data_tracking'];
     $data_tracking  = explode(',', $data_tracking);
       
     $current_question = 1; //set by default the 1st question    
     
-    if (!empty($my_remind_list)) { 
+    if (!empty($my_remind_list)) {
     	//Checking which questions we are going to call from the remind list      
 		for ($i = 0; $i < count($data_tracking); $i++) {
 			for($j = 0; $j < count($my_remind_list); $j++) {				
@@ -741,8 +718,9 @@ if ($reminder == 2)  {
 				}
 			}  
         }
-    } else {    	
+    } else {
     	if ($objExercise->review_answers) {
+            if ($debug) { error_log('. redirecting to exercise_reminder.php '); }
 	    	header("Location: exercise_reminder.php?$params");
 	    	exit;
     	}
@@ -847,10 +825,8 @@ if (!empty($error)) {
                             
            		// Only for the first time
            		
-           		
-           		
           		$("#save_for_now_"+question_id).html("'.addslashes(Display::return_icon('loading1.gif')).'");                                
-                    $.ajax({ 
+                    $.ajax({
                         url: "'.api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?a=save_exercise_by_now",
                         data: "'.$params.'&type=simple&question_id="+question_id+"&"+my_choice+"&"+hotspot+"&"+remind_list,                                 
                         success: function(return_value) {
@@ -867,10 +843,12 @@ if (!empty($error)) {
 								} else {
 									url = "exercise_submit.php?'.$params.'&num='.$current_question.'&remind_question_id='.$remind_question_id.'";
 								}								
-								window.location = url;
-								
+								window.location = url;								
                         	}
-                        },                                 
+                        },
+                        error: function() {
+                            $("#save_for_now_"+question_id).html("'.addslashes(Display::return_icon('error.png', get_lang('Error'), array(), ICON_SIZE_SMALL)).'");
+                        }
                     });   
                 return false;
             }

+ 32 - 14
main/inc/ajax/exercise.ajax.php

@@ -15,6 +15,8 @@ api_protect_course_script(true);
 $action = $_REQUEST['a'];
 $course_id = api_get_course_int_id();
 
+if ($debug) error_log("$action ajax call");
+
 switch ($action) {
     case 'get_live_stats':
         if (!api_is_allowed_to_edit(null, true)) {
@@ -121,8 +123,8 @@ switch ($action) {
             Display::display_confirmation_message(get_lang('Saved'));
         }
         break;
-    case 'add_question_to_reminder':    	    	
-    	$objExercise             = $_SESSION['objExercise'];    	
+    case 'add_question_to_reminder':          
+    	$objExercise  = $_SESSION['objExercise'];    	
     	if (empty($objExercise)) {
     		echo 0;
     		exit;
@@ -135,19 +137,24 @@ switch ($action) {
         if (api_is_allowed_to_session_edit()) {
         	            
             //"all" or "simple" strings means that there's one or all questions           
-            $type                    = $_REQUEST['type'];            
+            $type                   = $_REQUEST['type'];            
             
             //Normal questions choices
-            $choice                  = $_REQUEST['choice'];
+            $choice                 = $_REQUEST['choice'];
             
             //All Hotspot coordinates from all questions 
-            $hot_spot_coordinates    = $_REQUEST['hotspot'];
-            
+            $hot_spot_coordinates   = $_REQUEST['hotspot'];            
             
             //There is a reminder?
-            $remind_list             = isset($_REQUEST['remind_list']) && !empty($_REQUEST['remind_list'])? array_keys($_REQUEST['remind_list']) : null;
+            $remind_list            = isset($_REQUEST['remind_list']) && !empty($_REQUEST['remind_list'])? array_keys($_REQUEST['remind_list']) : null;
                
-            $exe_id = $_REQUEST['exe_id'];
+            $exe_id                 = $_REQUEST['exe_id'];
+            
+            if ($debug) error_log("exe_id = $exe_id ");
+            if ($debug) error_log("type = $type ");
+            if ($debug) error_log("choice = ".print_r($choice, 1)." ");
+            if ($debug) error_log("hot_spot_coordinates = $hot_spot_coordinates ");
+            if ($debug) error_log("remind_list = $remind_list ");            
             
             //Exercise information            
             $question_id             = intval($_REQUEST['question_id']);            
@@ -157,7 +164,8 @@ switch ($action) {
             if (empty($question_list) || empty($objExercise)) {
                 echo 0;
                 exit;
-            }            
+            }
+            
             //Getting information of the current exercise    
             $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);           
             
@@ -232,12 +240,16 @@ switch ($action) {
             
             //Looping the question list
             
-            foreach($question_list as $my_question_id) {                
+            foreach ($question_list as $my_question_id) {
+                if ($debug) error_log("Saving question_id = $my_question_id ");                
+                
                 if ($type == 'simple' && $question_id != $my_question_id) {
                     continue;
                 }             
                  
                 $my_choice = $choice[$my_question_id];
+                
+                if ($debug) error_log("my_choice = ".print_r($my_choice, 1)."");
                                 
                // creates a temporary Question object
             	$objQuestionTmp        = Question::read($my_question_id);
@@ -255,7 +267,8 @@ switch ($action) {
             	$hotspot_delineation_result = $_SESSION['hotspot_delineation_result'][$objExercise->selectId()][$my_question_id];
             	
             	// Deleting old attempt
-                if (isset($attempt_list) && !empty($attempt_list[$my_question_id])) {                    
+                if (isset($attempt_list) && !empty($attempt_list[$my_question_id])) {          
+                    if ($debug) error_log("delete_attempt  exe_id : $exe_id, my_question_id: $my_question_id");
                     delete_attempt($exe_id, api_get_user_id() , api_get_course_id(), api_get_session_id(), $my_question_id);
                     if ($objQuestionTmp->type  == HOT_SPOT) {            	        
             	        delete_attempt_hotspot($exe_id, api_get_user_id() , api_get_course_id(), $my_question_id);
@@ -266,13 +279,18 @@ switch ($action) {
             	// We're inside *one* question. Go through each possible answer for this question
             	$result = $objExercise->manage_answer($exe_id, $my_question_id, $my_choice,'exercise_result', $hot_spot_coordinates, true, false, $show_results, $objExercise->selectPropagateNeg(), $hotspot_delineation_result, true);
             	  	
-                $total_score     += $result['score'];    
-              	
+                $total_score     += $result['score'];
+                
+                if ($debug) error_log("total_score: $total_score ");
+                if ($debug) error_log("total_weight: $total_weight ");
+                                
                 update_event_exercice($exe_id, $objExercise->selectId(), $total_score, $total_weight, api_get_session_id(), $exercise_stat_info['orig_lp_id'], $exercise_stat_info['orig_lp_item_id'], $exercise_stat_info['orig_lp_item_view_id'], $exercise_stat_info['exe_duration'], $question_list, 'incomplete', $remind_list);
                 
                  // Destruction of the Question object
             	unset($objQuestionTmp); 
-            }            
+                if ($debug) error_log(" -- end question -- ");
+            }   
+            if ($debug) error_log(" ------ end ajax call ------- ");            
         }
         
         if ($objExercise->type == ONE_PER_PAGE) {

+ 39 - 22
main/inc/lib/events.lib.inc.php

@@ -331,8 +331,7 @@ function update_event_exercice($exeid, $exo_id, $score, $weighting,$session_id,$
 		if (!exercise_time_control_is_valid($exo_id)) {
 			$score = 0;		
 	    }
-	    $now = time();
-              
+	                  
         /*  start_date wouldn't be updated
         $start_date_condition = '';        
 	    //Validation in case of wrong start_date
@@ -377,6 +376,9 @@ function update_event_exercice($exeid, $exo_id, $score, $weighting,$session_id,$
 				 WHERE exe_id = '".Database::escape_string($exeid)."'";
 		$res = Database::query($sql);
         
+        if ($debug) error_log('update_event_exercice called ');
+        if ($debug) error_log("$sql");
+        
         //Deleting control time session track		
 		//exercise_time_control_delete($exo_id);
 		return $res;        
@@ -448,56 +450,71 @@ function create_event_exercice($exo_id) {
  * @param	integer	Position
  * @return	boolean	Result of the insert query
  */
-function exercise_attempt($score, $answer, $quesId, $exeId, $j, $exercise_id = 0, $nano = null) {	
+function exercise_attempt($score, $answer, $question_id, $exe_id, $position, $exercise_id = 0, $nano = null) {	
     require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
-	$score 	= Database::escape_string($score);
-	$answer = Database::escape_string($answer);
-	$quesId = Database::escape_string($quesId);
-	$exeId 	= Database::escape_string($exeId);
-	$j 		= Database::escape_string($j);
+    global $debug;
+	$score          = Database::escape_string($score);
+	$answer         = Database::escape_string($answer);
+	$question_id    = Database::escape_string($question_id);
+	$exe_id          = Database::escape_string($exe_id);
+	$position 		= Database::escape_string($position);
+    $now            = api_get_utc_datetime();
+	$user_id        = api_get_user_id();
 		
 	$TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
+    
+    if ($debug) error_log("----- entering exercise_attempt function ------");
+    
+    if ($debug) error_log("answer: $answer");
+    if ($debug) error_log("score: $score");
+    if ($debug) error_log("question_id : $question_id");
+    if ($debug) error_log("position: $position");
 
-	//Validation in case of fraud with actived control time	
-	
+	//Validation in case of fraud with actived control time	    	
     if (!exercise_time_control_is_valid($exercise_id)) {
+        if ($debug) error_log("exercise_time_control_is_valid is false");
     	$score = 0;
     	$answer = 0;
     	$j = 0;
 	}
-	$reallyNow 	= api_get_utc_datetime();
-	$user_id 	= api_get_user_id();
+	
+    
 	if (!empty($user_id)) {
 		$user_id = "'".$user_id."'";
 	} else {
 		// anonymous
 		$user_id = api_get_anonymous_id();
 	}		
-	$file = '';
-	
+    
+	$file = '';	
 	if (isset($nano)) {	
-		$file = basename($nano->load_filename_if_exists(false));
+		$file = Database::escape_string(basename($nano->load_filename_if_exists(false)));
 	}
     
 	$sql = "INSERT INTO $TBL_TRACK_ATTEMPT (exe_id, user_id, question_id, answer, marks, course_code, session_id, position, tms, filename)
 			  VALUES (
-			  ".$exeId.",
+			  ".$exe_id.",
 			  ".$user_id.",
-			   '".$quesId."',
+			   '".$question_id."',
 			   '".$answer."',
 			   '".$score."',
 			   '".api_get_course_id()."',
 			   '".api_get_session_id()."',
-			   '".$j."',
-			   '".$reallyNow."',
+			   '".$position."',
+			   '".$now."',
 			   '".$file."'
 			)";		
-    //error_log($sql);
-	if (!empty($quesId) && !empty($exeId) && !empty($user_id)) {
+    
+    if ($debug) error_log("Saving question attempt: ");
+    if ($debug) error_log($sql);
+    if ($debug) error_log("");
+    
+	if (!empty($question_id) && !empty($exe_id) && !empty($user_id)) {
 		$res = Database::query($sql);		
 		if (defined('ENABLED_LIVE_EXERCISE_TRACKING')){
 			$recording_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
-			$recording_changes = "INSERT INTO $recording_table (exe_id, question_id, marks, insert_date, author, session_id) VALUES ('$exeId','$quesId','$score','".api_get_utc_datetime()."','', '".api_get_session_id()."') ";
+            if ($debug) error_log("Saving e attempt recording ");
+			$recording_changes = "INSERT INTO $recording_table (exe_id, question_id, marks, insert_date, author, session_id) VALUES ('$exe_id','$question_id','$score','".api_get_utc_datetime()."','', '".api_get_session_id()."') ";
 			Database::query($recording_changes);			
 		}		
 		return $res;