Browse Source

Fixing exercise sessions

Julio Montoya 11 years ago
parent
commit
e4ccd1cff1

+ 2 - 1
main/exercice/exercise.class.php

@@ -2658,6 +2658,7 @@ class Exercise
                         send_form();
                     }
                 });
+
                 $('#clock_warning').dialog('open');
 
                 $('#counter_to_redirect').epiclock({
@@ -2674,7 +2675,7 @@ class Exercise
                 if ($('#exercise_form').length) {
                     $('#exercise_form').submit();
                 } else {
-                    //In reminder
+                    // In reminder
                     final_submit();
                 }
             }

+ 1 - 1
main/exercice/exercise_reminder.php

@@ -111,7 +111,7 @@ echo '<script>
 		lp_data = $.param({"learnpath_id": '.$learnpath_id.', "learnpath_item_id" : '.$learnpath_item_id.', "learnpath_item_view_id": '.$learnpath_item_view_id.'});
 
         function final_submit() {
-        	//Normal inputs
+        	// Normal inputs
         	window.location = "'.$urlMainExercise.'exercise_result.php?origin='.$origin.'&exe_id='.$exe_id.'&" + lp_data;
 		}
 

+ 105 - 57
main/exercice/exercise_submit.php

@@ -43,8 +43,11 @@ $current_course_tool  = TOOL_QUIZ;
 $nameTools = get_lang('Quiz');
 
 $this_section = SECTION_COURSES;
-
-if ($debug) { error_log('--- Enter to the exercise_submit.php ---- '); error_log('0. POST variables : '.print_r($_POST,1)); }
+$debug = true;
+if ($debug) {
+    error_log('--- Enter to the exercise_submit.php ---- ');
+    error_log('0. POST variables : '.print_r($_POST,1));
+}
 
 // Notice for unauthorized people.
 api_protect_course_script(true);
@@ -311,35 +314,48 @@ $(function(){
 
 // General parameters passed via POST/GET
 
-$learnpath_id 			= isset($_REQUEST['learnpath_id']) ? intval($_REQUEST['learnpath_id']) : 0;
-$learnpath_item_id 		= isset($_REQUEST['learnpath_item_id']) ? intval($_REQUEST['learnpath_item_id']) : 0;
-$learnpath_item_view_id	= isset($_REQUEST['learnpath_item_view_id']) ? intval($_REQUEST['learnpath_item_view_id']) : 0;
-$origin 				= isset($_REQUEST['origin']) ? Security::remove_XSS($_REQUEST['origin']) : '';
-$reminder 				= isset($_REQUEST['reminder']) ? intval($_REQUEST['reminder']) : 0;
-$remind_question_id 	= isset($_REQUEST['remind_question_id']) ? intval($_REQUEST['remind_question_id']) : 0;
+/*
+     <input type="hidden" name="formSent"				value="1" />
+     <input type="hidden" name="exerciseId" 			value="'.$exerciseId . '" />
+     <input type="hidden" name="num" 					value="'.$current_question.'" id="num_current_id" />
+     <input type="hidden" name="exe_id" 				value="'.$exe_id . '" />
+     <input type="hidden" name="origin" 				value="'.$origin . '" />
+     <input type="hidden" name="learnpath_id" 			value="'.$learnpath_id . '" />
+     <input type="hidden" name="learnpath_item_id" 		value="'.$learnpath_item_id . '" />
+     <input type="hidden" name="learnpath_item_view_id" value="'.$learnpath_item_view_id . '" />';*/
+
+// @todo check get and posts
+$learnpath_id 			= isset($_GET['learnpath_id']) ? intval($_GET['learnpath_id']) : 0;
+$learnpath_item_id 		= isset($_GET['learnpath_item_id']) ? intval($_GET['learnpath_item_id']) : 0;
+$learnpath_item_view_id	= isset($_GET['learnpath_item_view_id']) ? intval($_GET['learnpath_item_view_id']) : 0;
+$origin 				= isset($_GET['origin']) ? Security::remove_XSS($_GET['origin']) : '';
+$reminder 				= isset($_GET['reminder']) ? intval($_GET['reminder']) : 0;
+$remind_question_id 	= isset($_GET['remind_question_id']) ? intval($_GET['remind_question_id']) : 0;
 $exerciseId				= isset($_REQUEST['exerciseId']) ? intval($_REQUEST['exerciseId']) : 0;
-$formSent               = isset($_REQUEST['formSent']) ? $_REQUEST['formSent'] : null;
-$exerciseResult         = isset($_REQUEST['exerciseResult']) ? $_REQUEST['exerciseResult'] : null;
-$choice                 = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : null;
-$choice                 = empty($choice) ? isset($_REQUEST['choice2']) ? $_REQUEST['choice2'] : null : null;
-$exerciseResultCoordinates = isset($_REQUEST['exerciseResultCoordinates']) ? $_REQUEST['exerciseResultCoordinates'] : null;
+$formSent               = isset($_POST['formSent']) ? $_POST['formSent'] : null;
+$exerciseResult         = isset($_GET['exerciseResult']) ? $_GET['exerciseResult'] : null;
+$choice                 = isset($_GET['choice']) ? $_GET['choice'] : null;
+$choice                 = empty($choice) ? isset($_GET['choice2']) ? $_GET['choice2'] : null : null;
+$exerciseResultCoordinates = isset($_GET['exerciseResultCoordinates']) ? $_GET['exerciseResultCoordinates'] : null;
 $current_question       = isset($_REQUEST['num']) ? intval($_REQUEST['num']) : null;
 
 //Error message
 $error = '';
 
-/*  Teacher takes an exam and want to see a preview, we delete the objExercise from the session in order to get the latest changes in the exercise */
+/*  Teacher takes an exam and want to see a preview,
+we delete the objExercise from the session in order to get the latest changes in the exercise */
 if (api_is_allowed_to_edit(null,true) && isset($_GET['preview']) && $_GET['preview'] == 1) {
     Session::erase('objExercise');
 }
+$exerciseInSession = Session::read('objExercise');
 
 // 1. Loading the $objExercise variable
-if (!isset($_SESSION['objExercise']) || isset($_SESSION['objExercise']) && ($_SESSION['objExercise']->id != $_REQUEST['exerciseId'])) {
+if (!isset($exerciseInSession) || isset($exerciseInSession) && ($exerciseInSession->id != $_GET['exerciseId'])) {
     // Construction of Exercise
     /** @var Exercise $objExercise */
     $objExercise = new Exercise();
     if ($debug) {error_log('1. Setting the $objExercise variable'); };
-    unset($_SESSION['questionList']);
+    Session::erase('questionList');
 
     // if the specified exercise doesn't exist or is disabled
     if (!$objExercise->read($exerciseId) || (!$objExercise->selectStatus() && !$is_allowedToEdit && ($origin != 'learnpath'))) {
@@ -349,21 +365,25 @@ if (!isset($_SESSION['objExercise']) || isset($_SESSION['objExercise']) && ($_SE
     } else {
         // Saves the object into the session
         Session::write('objExercise', $objExercise);
-        if ($debug) { error_log('1.1. $_SESSION[objExercise] was unset - set now - end'); };
+        if ($debug) {
+            error_log('1.1. $exerciseInSession was unset - set now - end');
+        };
     }
 }
 
 // $objExercise = new Exercise(); $objExercise->read($exerciseId);
 
 //2. Checking if $objExercise is set
-if (!isset($objExercise) && isset($_SESSION['objExercise'])) {
+if (!isset($objExercise) && isset($exerciseInSession)) {
 	if ($debug) { error_log('2. Loading $objExercise from session'); };
-    $objExercise = $_SESSION['objExercise'];
+    $objExercise = $exerciseInSession;
 }
 
 //3. $objExercise is not set, then return to the exercise list
 if (!is_object($objExercise)) {
-	if ($debug) {error_log('3. $objExercise was not set, kill the script'); };
+	if ($debug) {
+        error_log('3. $objExercise was not set, kill the script');
+    };
     header('Location: '.$urlMainExercise.'exercice.php');
     exit;
 }
@@ -374,7 +394,7 @@ if ($objExercise->review_answers) {
         $paramsReminder = "exerciseId=$exerciseId&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id&".api_get_cidreq();
         header('Location: '.$urlMainExercise.'exercise_reminder.php?'.$paramsReminder);
         exit;
-	}
+    }
 }
 
 $current_timestamp 	= time();
@@ -388,10 +408,15 @@ if ($objExercise->expired_time != 0) {
 //Generating the time control key for the user
 $current_expired_time_key = ExerciseLib::get_time_control_key($objExercise->id, $learnpath_id, $learnpath_item_id);
 if ($debug) {
-    error_log("4. current_expired_time_key: $current_expired_time_key ");
+    error_log("4. current_expired_time_key: $current_expired_time_key");
 }
 
-$_SESSION['duration_time'][$current_expired_time_key] = $current_timestamp;
+$durationTime = array(
+    $current_expired_time_key => $current_timestamp
+);
+Session::write('duration_time', $durationTime);
+
+//$_SESSION['duration_time'][$current_expired_time_key] = $current_timestamp;
 
 if ($time_control) {
     // Get the expired time of the current exercise in track_e_exercices.
@@ -407,20 +432,33 @@ if ($objExercise->selectAttempts() > 0) {
     $attempt_html = null;
     $attempt_count = get_attempt_count($user_id, $exerciseId, $learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
 
+    $warningMessage = Display::return_message(
+        sprintf(get_lang('ReachedMaxAttempts'),
+        $exercise_title,
+        $objExercise->selectAttempts()),
+        'warning',
+        false
+    );
+
 	if ($attempt_count >= $objExercise->selectAttempts()) {
 		$show_clock = false;
 		if (!api_is_allowed_to_edit(null,true)) {
 
 			if ($objExercise->results_disabled == 0 && $origin != 'learnpath') {
 
-				//Showing latest attempt according with task BT#1628
-				$exercise_stat_info = getExerciseResultsByUser($user_id, $exerciseId, api_get_course_int_id(), api_get_session_id());
+				// Showing latest attempt according with task BT#1628.
+				$exercise_stat_info = getExerciseResultsByUser(
+                    $user_id,
+                    $exerciseId,
+                    api_get_course_int_id(),
+                    api_get_session_id()
+                );
 
 				if (!empty($exercise_stat_info)) {
 					$max_exe_id = max(array_keys($exercise_stat_info));
 					$last_attempt_info = $exercise_stat_info[$max_exe_id];
 					$attempt_html .= Display::div(get_lang('Date').': '.api_get_local_time($last_attempt_info['exe_date']), array('id'=>''));
-					$attempt_html .= Display::return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
+					$attempt_html .= $warningMessage;
 
 					if (!empty($last_attempt_info['question_list'])) {
 						foreach($last_attempt_info['question_list'] as $question_data) {
@@ -435,13 +473,13 @@ if ($objExercise->selectAttempts() > 0) {
 					$score = ExerciseLib::show_score($last_attempt_info['exe_result'], $last_attempt_info['exe_weighting']);
 					$attempt_html .= Display::div(get_lang('YourTotalScore').' '.$score, array('id'=>'question_score'));
 				} else {
-					$attempt_html .= Display::return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
+					$attempt_html .= $warningMessage;
 				}
 			} else {
-				$attempt_html .= Display::return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
+				$attempt_html .= $warningMessage;
 			}
 		} else {
-			$attempt_html .= Display :: return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
+			$attempt_html .= $warningMessage;
 		}
 
 		if ($origin == 'learnpath') {
@@ -462,7 +500,8 @@ if ($objExercise->selectAttempts() > 0) {
 $exercise_stat_info = $objExercise->getStatTrackExerciseInfo($learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
 
 //if (1) {
-if (!isset($_SESSION['questionList'])) {
+$questionListInSession = Session::read('questionList');
+if (!isset($questionListInSession)) {
     // Selects the list of question ID
     $questionList = $objExercise->getQuestionList();
 
@@ -476,7 +515,7 @@ if (!isset($_SESSION['questionList'])) {
     Session::write('questionList', $questionList);
     if ($debug > 0) { error_log('$_SESSION[questionList] was set'); }
 } else {
-	if (isset($objExercise) && isset($_SESSION['objExercise'])) {
+	if (isset($objExercise) && isset($exerciseInSession)) {
         $questionList = Session::read('questionList');
 	}
 }
@@ -503,12 +542,15 @@ if (empty($exercise_stat_info)) {
 		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);
+		$clock_expired_time = api_get_utc_datetime($expected_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('5.4. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key] ); };
+		$expiredTime = array(
+            $current_expired_time_key => $clock_expired_time
+        );
+        Session::write('expired_time', $expiredTime);
+		if ($debug) { error_log('5.4. Setting the $expiredTime: '.$expiredTime[$current_expired_time_key] ); };
 	}
 	$exe_id = $objExercise->save_stat_track_exercise_info($clock_expired_time, $learnpath_id, $learnpath_item_id, $learnpath_item_view_id, $questionListUncompressed, $total_weight);
 	$exercise_stat_info = $objExercise->getStatTrackExerciseInfo($learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
@@ -545,26 +587,28 @@ if ($reminder == 2 && empty($my_remind_list)) {
  */
 
 if ($time_control) {
-	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]);
+    $expiredTimeInSession = Session::read('expired_time');
 
-    if (!isset($_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 (!isset($expiredTimeInSession[$current_expired_time_key])) {
+        if ($debug) error_log('7.3. $expiredTimeInSession[$current_expired_time_key] not set.');
         //Timer - Get expired_time for a student
         if (!empty($exercise_stat_info)) {
         	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('7.5 $expired_time_of_this_attempt: '.$expired_time_of_this_attempt); }
-	        //Get the last attempt of an exercice
+	        // Get the last attempt of an exercise.
 	    	$last_attempt_date = getLastAttemptDateOfExercise($exercise_stat_info['exe_id']);
 
-	    	//This means that the user enters the exam but do not answer the first question we get the date from the track_e_exercises not from the track_et_attempt see #2069
+	    	/* This means that the user enters the exam but do not answer the first question we get the date
+            from the track_e_exercises not from the track_et_attempt see #2069 */
 	    	if (empty($last_attempt_date)) {
 	    		$diff = $current_timestamp - api_strtotime($exercise_stat_info['start_date'], 'UTC');
 	    		$last_attempt_date = api_get_utc_datetime(api_strtotime($exercise_stat_info['start_date'],'UTC') + $diff);
 	    	} else {
 	    		//Recalculate the time control due #2069
-	    		$diff = $current_timestamp - api_strtotime($last_attempt_date,'UTC');
+	    		$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('7.6. $last_attempt_date: '.$last_attempt_date); }
@@ -574,7 +618,7 @@ if ($time_control) {
 	        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('7.8. $expected_time1: '.$expected_time); }
+	        if ($debug) {error_log('7.8. $expected_time: '.$expected_time); }
 
 	        $clock_expired_time  = api_get_utc_datetime($expected_time);
 	        if ($debug) {error_log('7.9. $clock_expired_time: '.$clock_expired_time); }
@@ -583,12 +627,16 @@ if ($time_control) {
 			// How the expired time is changed into "track_e_exercices" table,then the last attempt for this student should be changed too,so
             ExerciseLib::update_attempt_date($exercise_stat_info['exe_id'], $last_attempt_date);
 
-	        //Sessions  that contain the expired time
-	        $_SESSION['expired_time'][$current_expired_time_key] 		= $clock_expired_time;
-	        if ($debug) {error_log('7.11. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key] ); };
+	        // Sessions that contain the expired time
+	        $expiredTimeInSession[$current_expired_time_key] 		= $clock_expired_time;
+            Session::write('expired_time', $expiredTimeInSession);
+	        if ($debug) {
+                error_log('7.11. Setting the $expiredTimeInSession: '.$expiredTimeInSession[$current_expired_time_key] );
+            };
         }
     } else {
-        $clock_expired_time =  $_SESSION['expired_time'][$current_expired_time_key];
+        if ($debug) error_log('7.3. $expiredTimeInSession[$current_expired_time_key]: '.$expiredTimeInSession[$current_expired_time_key]);
+        $clock_expired_time =  $expiredTimeInSession[$current_expired_time_key];
     }
 } else {
     if ($debug) { error_log("7. No time control"); };
@@ -622,8 +670,8 @@ if ($formSent && isset($_POST)) {
     }
 
     // Only for hot spot
-    if (!isset($choice) && isset($_REQUEST['hidden_hotspot_id'])) {
-        $hotspot_id = (int)($_REQUEST['hidden_hotspot_id']);
+    if (!isset($choice) && isset($_GET['hidden_hotspot_id'])) {
+        $hotspot_id = (int)($_GET['hidden_hotspot_id']);
         $choice     = array($hotspot_id => '');
     }
 
@@ -664,18 +712,18 @@ if ($formSent && isset($_POST)) {
                 }
             }
         }
-        if ($debug) { error_log('9.3.  $choice is an array - end'); }
-        if ($debug) { error_log('9.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)); }
     }
 
     // the script "exercise_result.php" will take the variable $exerciseResult from the session
-    Session::write('exerciseResult',$exerciseResult);
+    Session::write('exerciseResult', $exerciseResult);
     //Session::write('remind_list', $remind_list);
-    Session::write('exerciseResultCoordinates',$exerciseResultCoordinates);
+    Session::write('exerciseResultCoordinates', $exerciseResultCoordinates);
 
     // 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 (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) {
@@ -698,7 +746,8 @@ if ($formSent && isset($_POST)) {
                 header("Location: ".$urlMainExercise."exercise_result.php?".api_get_cidreq()."&exe_id=$exe_id&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id");
                 exit;
             } else {
-                //Time control is only enabled for ONE PER PAGE
+                // Time control is only enabled for ONE PER PAGE
+
                 if (!empty($exe_id) && is_numeric($exe_id)) {
                     //Verify if the current test is fraudulent
                     if (ExerciseLib::exercise_time_control_is_valid($exerciseId, $learnpath_id, $learnpath_item_id)) {
@@ -1006,7 +1055,7 @@ if (!empty($error)) {
     }
 
     if ($number_of_hotspot_questions > 0) {
-        $onsubmit = "onsubmit=\"return validateFlashVar('" . $number_of_hotspot_questions . "', '" . get_lang('HotspotValidateError1') . "', '" . get_lang('HotspotValidateError2') . "');\"";
+        $onsubmit = " onsubmit=\"return validateFlashVar('".$number_of_hotspot_questions."', '" .get_lang('HotspotValidateError1')."', '".get_lang('HotspotValidateError2')."');\"";
     }
 
     echo '<script>
@@ -1192,8 +1241,7 @@ if (!empty($error)) {
     if (isset($exercise_stat_info['questions_to_check']) && !empty($exercise_stat_info['questions_to_check'])) {
         $remind_list = explode(',', $exercise_stat_info['questions_to_check']);
     }
-
-    echo '<form id="exercise_form" method="post" action="'.api_get_path(WEB_CODE_PATH).'exercice/exercise_submit.php?'.api_get_cidreq().'&autocomplete=off&gradebook='.$gradebook."&exerciseId=".$exerciseId .'" name="frm_exercise" '.$onsubmit.'>
+    echo '<form id="exercise_form" method="post" action="'.api_get_path(WEB_PUBLIC_PATH).'main/exercice/exercise_submit.php?'.api_get_cidreq().'&autocomplete=off&gradebook='.$gradebook."&exerciseId=".$exerciseId .'" name="frm_exercise" '.$onsubmit.'>
      <input type="hidden" name="formSent"				value="1" />
      <input type="hidden" name="exerciseId" 			value="'.$exerciseId . '" />
      <input type="hidden" name="num" 					value="'.$current_question.'" id="num_current_id" />

+ 16 - 6
main/inc/ajax/exercise.ajax.php

@@ -4,9 +4,6 @@
  * Responses to AJAX calls
  */
 
-
-require_once '../global.inc.php';
-
 require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
 require_once api_get_path(SYS_CODE_PATH).'exercice/question.class.php';
 require_once api_get_path(SYS_CODE_PATH).'exercice/answer.class.php';
@@ -517,8 +514,16 @@ switch ($action) {
 
                 $key = ExerciseLib::get_time_control_key($exercise_id, $exercise_stat_info['orig_lp_id'], $exercise_stat_info['orig_lp_item_id']);
 
-                if (isset($_SESSION['duration_time'][$key]) && !empty($_SESSION['duration_time'][$key])) {
-                    $duration = $now - $_SESSION['duration_time'][$key];
+                /*$durationTime = array(
+                    'duration_time' => array(
+                        $key => time()
+                    )
+                );*/
+
+                $durationTime = Session::read('duration_time');
+
+                if (isset($durationTime[$key]) && !empty($durationTime[$key])) {
+                    $duration = $now - $durationTime[$key];
 
                     if (!empty($exercise_stat_info['exe_duration'])) {
                         $duration += $exercise_stat_info['exe_duration'];
@@ -530,7 +535,12 @@ switch ($action) {
                     }
                 }
 
-                $_SESSION['duration_time'][$key] = time();
+                $durationTime = array(
+                    $key => time()
+                );
+
+                Session::write('duration_time', $durationTime);
+                // $_SESSION['duration_time'][$key] = time();
 
                 update_event_exercise(
                     $exe_id,