Browse Source

Minor - cleaning code, adding error_logs.

Julio Montoya 11 years ago
parent
commit
ef78614dc9
1 changed files with 105 additions and 83 deletions
  1. 105 83
      main/inc/ajax/exercise.ajax.php

+ 105 - 83
main/inc/ajax/exercise.ajax.php

@@ -15,7 +15,9 @@ api_protect_course_script(true);
 $action = $_REQUEST['a'];
 $course_id = api_get_course_int_id();
 
-if ($debug) error_log("$action ajax call");
+if ($debug) {
+    error_log("$action ajax call");
+}
 
 $session_id = isset($_REQUEST['session_id']) ? intval($_REQUEST['session_id']) : api_get_session_id();
 $course_code = isset($_REQUEST['cidReq']) ? $_REQUEST['cidReq'] : api_get_course_id();
@@ -196,58 +198,64 @@ switch ($action) {
         // Use have permissions?
         if (api_is_allowed_to_session_edit()) {
 
-            //"all" or "simple" strings means that there's one or all questions exercise type
-            $type                   = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
+            // "all" or "simple" strings means that there's one or all questions exercise type
+            $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
 
-            //Questions choices
-            $choice                 = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : null;
+            // Questions choices.
+            $choice = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : null;
 
-            //Hotspot coordinates from all questions
-            $hot_spot_coordinates   = isset($_REQUEST['hotspot']) ? $_REQUEST['hotspot'] : null;
+            // Hot spot coordinates from all questions.
+            $hot_spot_coordinates = isset($_REQUEST['hotspot']) ? $_REQUEST['hotspot'] : null;
 
-            //There is a reminder?
-            $remind_list            = isset($_REQUEST['remind_list']) && !empty($_REQUEST['remind_list'])? array_keys($_REQUEST['remind_list']) : null;
+            // There is a reminder?
+            $remind_list = isset($_REQUEST['remind_list']) && !empty($_REQUEST['remind_list']) ? array_keys($_REQUEST['remind_list']) : null;
 
-            //Needed in manage_answer
-            $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;
+            // Needed in manage_answer.
+            $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;
 
-            //Attempt id
+            // Attempt id.
             $exe_id = $_REQUEST['exe_id'];
 
             if ($debug) {
-                error_log("exe_id = $exe_id ");
-                error_log("type = $type ");
+                error_log("exe_id = $exe_id");
+                error_log("type = $type");
                 error_log("choice = ".print_r($choice, 1)." ");
                 error_log("hot_spot_coordinates = ".print_r($hot_spot_coordinates, 1));
                 error_log("remind_list = ".print_r($remind_list, 1));
             }
 
             // Exercise information.
-            $objExercise             = isset($_SESSION['objExercise']) ? $_SESSION['objExercise'] : null;
+            $objExercise = isset($_SESSION['objExercise']) ? $_SESSION['objExercise'] : null;
 
             // Question info.
-            $question_id             = intval($_REQUEST['question_id']);
-            $question_list           = $_SESSION['questionList'];
+            $question_id = intval($_REQUEST['question_id']);
+            $question_list = $_SESSION['questionList'];
 
             // If exercise or question is not set then exit.
             if (empty($question_list) || empty($objExercise)) {
                 echo 'error';
+                if ($debug) {
+                    if (empty($question_list)) {
+                        error_log("question_list is empty");
+                    }
+                    if (empty($objExercise)) {
+                        error_log("objExercise is empty");
+                    }
+                }
                 exit;
             }
 
-            //Getting information of the current exercise
+            // Getting information of the current exercise.
             $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);
-
             $exercise_id = $exercise_stat_info['exe_exo_id'];
-
             $attempt_list = array();
 
-            //First time here we create an attempt (getting the exe_id)
+            // First time here we create an attempt (getting the exe_id).
             if (empty($exercise_stat_info)) {
 
             } else {
-                //We know the user we get the exe_id
+                // We know the user we get the exe_id.
                 $exe_id        = $exercise_stat_info['exe_id'];
                 $total_score   = $exercise_stat_info['exe_result'];
 
@@ -255,34 +263,36 @@ switch ($action) {
                 $attempt_list  = get_all_exercise_event_by_exe_id($exe_id);
             }
 
-            //Updating Reminder algorythm
+            // Updating Reminder algorythm.
             if ($objExercise->type == ONE_PER_PAGE) {
-            	$bd_reminder_list = explode(',', $exercise_stat_info['questions_to_check']);
-
-            	if (empty($remind_list)) {
-            		$remind_list = $bd_reminder_list;
-
-            		$new_list = array();
-            		foreach($bd_reminder_list as $item) {
-            			if ($item != $question_id) {
-            				$new_list[] = $item;
-            			}
-            		}
-            		$remind_list = $new_list;
-            	} else {
-            		if (isset($remind_list[0])) {
-            			if (!in_array($remind_list[0], $bd_reminder_list)) {
-            				array_push($bd_reminder_list, $remind_list[0]);
-            			}
-            			$remind_list = $bd_reminder_list;
-            		}
-            	}
+                $bd_reminder_list = explode(',', $exercise_stat_info['questions_to_check']);
+
+                if (empty($remind_list)) {
+                    $remind_list = $bd_reminder_list;
+                    $new_list = array();
+                    foreach ($bd_reminder_list as $item) {
+                        if ($item != $question_id) {
+                            $new_list[] = $item;
+                        }
+                    }
+                    $remind_list = $new_list;
+                } else {
+                    if (isset($remind_list[0])) {
+                        if (!in_array($remind_list[0], $bd_reminder_list)) {
+                            array_push($bd_reminder_list, $remind_list[0]);
+                        }
+                        $remind_list = $bd_reminder_list;
+                    }
+                }
             }
 
-            //No exe id? Can't save answer.
+            // No exe id? Can't save answer.
             if (empty($exe_id)) {
-                //Fires an error
+                // Fires an error.
                 echo 'error';
+                if ($debug) {
+                    error_log("exe_id is empty");
+                }
                 exit;
             } else {
                 $_SESSION['exe_id'] = $exe_id;
@@ -297,13 +307,14 @@ switch ($action) {
                     $total_weight   += $objQuestionTmp->selectWeighting();
                 }
             }
-
             unset($objQuestionTmp);
 
             // Looping the question list
 
             foreach ($question_list as $my_question_id) {
-                if ($debug) error_log("Saving question_id = $my_question_id ");
+                if ($debug) {
+                    error_log("Saving question_id = $my_question_id ");
+                }
 
                 if ($type == 'simple' && $question_id != $my_question_id) {
                     continue;
@@ -311,56 +322,61 @@ switch ($action) {
 
                 $my_choice = $choice[$my_question_id];
 
-                if ($debug) error_log("my_choice = ".print_r($my_choice, 1)."");
+                if ($debug) {
+                    error_log("my_choice = ".print_r($my_choice, 1)."");
+                }
 
-               // creates a temporary Question object
-            	$objQuestionTmp = Question::read($my_question_id, $course_id);
+                // Creates a temporary Question object
+                $objQuestionTmp = Question::read($my_question_id, $course_id);
 
-            	//Getting free choice data
-            	if ($objQuestionTmp->type  == FREE_ANSWER && $type == 'all') {
-            	    $my_choice = isset($_REQUEST['free_choice'][$my_question_id]) && !empty($_REQUEST['free_choice'][$my_question_id])? $_REQUEST['free_choice'][$my_question_id]: null;
-            	}
+                // Getting free choice data.
+                if ($objQuestionTmp->type  == FREE_ANSWER && $type == 'all') {
+                    $my_choice = isset($_REQUEST['free_choice'][$my_question_id]) && !empty($_REQUEST['free_choice'][$my_question_id]) ? $_REQUEST['free_choice'][$my_question_id]: null;
+                }
 
                 if ($type == 'all') {
                     $total_weight += $objQuestionTmp->selectWeighting();
                 }
 
-            	//this variable commes from exercise_submit_modal.php
+                // This variable came from exercise_submit_modal.php.
                 $hotspot_delineation_result = null;
                 if (isset($_SESSION['hotspot_delineation_result']) && isset($_SESSION['hotspot_delineation_result'][$objExercise->selectId()])) {
                     $hotspot_delineation_result = $_SESSION['hotspot_delineation_result'][$objExercise->selectId()][$my_question_id];
                 }
 
                 if ($type == 'simple') {
-                    //Getting old attempt in order to decrees the total score
+                    // Getting old attempt in order to decrees the total score.
                     $old_result = $objExercise->manage_answer($exe_id, $my_question_id, null, 'exercise_show', array(), false, true, false, $objExercise->selectPropagateNeg());
 
-                    //Removing old score
+                    // Removing old score.
                     $total_score = $total_score - $old_result['score'];
                 }
 
                 // Deleting old attempt
                 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() , $course_code, $session_id, $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(), $course_code, $session_id, $my_question_id);
                     if ($objQuestionTmp->type  == HOT_SPOT) {
-            	        delete_attempt_hotspot($exe_id, api_get_user_id() , $course_code, $my_question_id);
+                        delete_attempt_hotspot($exe_id, api_get_user_id(), $course_code, $my_question_id);
                     }
 
                     if (isset($attempt_list[$my_question_id]) && isset($attempt_list[$my_question_id]['marks'])) {
                         $total_score  -= $attempt_list[$my_question_id]['marks'];
                     }
-            	}
-
+                }
 
-            	// 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, false, $objExercise->selectPropagateNeg(), $hotspot_delineation_result, true);
+                // 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, false, $objExercise->selectPropagateNeg(), $hotspot_delineation_result, true);
 
-                //Adding the new score
+                //  Adding the new score.
                 $total_score += $result['score'];
 
-                if ($debug) error_log("total_score: $total_score ");
-                if ($debug) error_log("total_weight: $total_weight ");
+                if ($debug) {
+                    error_log("total_score: $total_score ");
+                    error_log("total_weight: $total_weight ");
+                }
 
                 $duration = 0;
                 $now = time();
@@ -385,24 +401,30 @@ switch ($action) {
 
                 $_SESSION['duration_time'][$key] = time();
 
-                update_event_exercice(  $exe_id,
-                                        $objExercise->selectId(),
-                                        $total_score,
-                                        $total_weight,
-                                        $session_id,
-                                        $exercise_stat_info['orig_lp_id'],
-                                        $exercise_stat_info['orig_lp_item_id'],
-                                        $exercise_stat_info['orig_lp_item_view_id'],
-                                        $duration,
-                                        $question_list,
-                                        'incomplete',
-                                        $remind_list);
+                update_event_exercice(
+                    $exe_id,
+                    $objExercise->selectId(),
+                    $total_score,
+                    $total_weight,
+                    $session_id,
+                    $exercise_stat_info['orig_lp_id'],
+                    $exercise_stat_info['orig_lp_item_id'],
+                    $exercise_stat_info['orig_lp_item_view_id'],
+                    $duration,
+                    $question_list,
+                    'incomplete',
+                    $remind_list
+                );
 
                  // Destruction of the Question object
-            	unset($objQuestionTmp);
-                if ($debug) error_log(" -- end question -- ");
+                unset($objQuestionTmp);
+                if ($debug) {
+                    error_log(" -- end question -- ");
+                }
+            }
+            if ($debug) {
+                error_log(" ------ end ajax call ------- ");
             }
-            if ($debug) error_log(" ------ end ajax call ------- ");
         }
 
         if ($objExercise->type == ONE_PER_PAGE) {