Browse Source

Fix question remind list order see BT#13664

- Fix question remind list update
jmontoyaa 7 years ago
parent
commit
6dd2a9b360

+ 71 - 31
main/exercise/exercise.class.php

@@ -3126,23 +3126,24 @@ class Exercise
      * @param int $questionNum
      * @param array $questions_in_media
      * @param string $currentAnswer
+     * @param array $myRemindList
      * @return string
      */
     public function show_button(
         $question_id,
         $questionNum,
-        $questions_in_media = array(),
-        $currentAnswer = ''
+        $questions_in_media = [],
+        $currentAnswer = '',
+        $myRemindList = []
     ) {
         global $origin, $safe_lp_id, $safe_lp_item_id, $safe_lp_item_view_id;
         $nbrQuestions = $this->get_count_question_list();
-        $all_button = [];
+        $buttonList = [];
         $html = $label = '';
         $hotspot_get = isset($_POST['hotspot']) ? Security::remove_XSS($_POST['hotspot']) : null;
 
         if ($this->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT && $this->type == ONE_PER_PAGE) {
             $urlTitle = get_lang('ContinueTest');
-
             if ($questionNum == count($this->questionList)) {
                 $urlTitle = get_lang('EndTest');
             }
@@ -3158,7 +3159,8 @@ class Exercise
                     'nbrQuestions' => $nbrQuestions,
                     'num' => $questionNum,
                     'exerciseType' => $this->type,
-                    'exerciseId' => $this->id
+                    'exerciseId' => $this->id,
+                    'reminder' => empty($myRemindList) ? null : 2
                 ]),
                 [
                     'class' => 'ajax btn btn-default',
@@ -3170,7 +3172,14 @@ class Exercise
         } else {
             // User
             if (api_is_allowed_to_session_edit()) {
-                if ($this->type == ALL_ON_ONE_PAGE || $nbrQuestions == $questionNum) {
+                $endReminderValue = false;
+                if (!empty($myRemindList)) {
+                    $endValue = end($myRemindList);
+                    if ($endValue == $question_id) {
+                        $endReminderValue = true;
+                    }
+                }
+                if ($this->type == ALL_ON_ONE_PAGE || $nbrQuestions == $questionNum || $endReminderValue) {
                     if ($this->review_answers) {
                         $label = get_lang('ReviewQuestions');
                         $class = 'btn btn-success';
@@ -3188,22 +3197,49 @@ class Exercise
                     if ($questionNum != 1) {
                         if ($this->showPreviousButton()) {
                             $prev_question = $questionNum - 2;
-                            $all_button[] = Display::button(
-                                'previous_question_and_save',
-                                get_lang('PreviousQuestion'),
-                                [
-                                    'type' => 'button',
-                                    'class' => 'btn btn-default',
-                                    'data-prev' => $prev_question,
-                                    'data-question' => $question_id
-                                ]
-                            );
+                            $showPreview = true;
+                            if (!empty($myRemindList)) {
+                                $beforeId = null;
+                                for ($i = 0; $i < count($myRemindList); $i++) {
+                                    if (isset($myRemindList[$i]) && $myRemindList[$i] == $question_id) {
+                                        $beforeId = isset($myRemindList[$i - 1]) ? $myRemindList[$i - 1] : null;
+                                        break;
+                                    }
+                                }
+
+                                if (empty($beforeId)) {
+                                    $showPreview = false;
+                                } else {
+                                    $num = 0;
+                                    foreach ($this->questionList as $originalQuestionId) {
+                                        if ($originalQuestionId == $beforeId) {
+                                            break;
+                                        }
+                                        $num++;
+                                    }
+                                    $prev_question = $num;
+                                    //$question_id = $beforeId;
+                                }
+                            }
+
+                            if ($showPreview) {
+                                $buttonList[] = Display::button(
+                                    'previous_question_and_save',
+                                    get_lang('PreviousQuestion'),
+                                    [
+                                        'type' => 'button',
+                                        'class' => 'btn btn-default',
+                                        'data-prev' => $prev_question,
+                                        'data-question' => $question_id
+                                    ]
+                                );
+                            }
                         }
                     }
 
                     // Next question
                     if (!empty($questions_in_media)) {
-                        $all_button[] = Display::button(
+                        $buttonList[] = Display::button(
                             'save_question_list',
                             $label,
                             [
@@ -3213,15 +3249,15 @@ class Exercise
                             ]
                         );
                     } else {
-                        $all_button[] = Display::button(
+                        $buttonList[] = Display::button(
                             'save_now',
                             $label,
                             ['type' => 'button', 'class' => $class, 'data-question' => $question_id]
                         );
                     }
-                    $all_button[] = '<span id="save_for_now_'.$question_id.'" class="exercise_save_mini_message"></span>&nbsp;';
+                    $buttonList[] = '<span id="save_for_now_'.$question_id.'" class="exercise_save_mini_message"></span>&nbsp;';
 
-                    $html .= implode(PHP_EOL, $all_button);
+                    $html .= implode(PHP_EOL, $buttonList);
                 } else {
                     if ($this->review_answers) {
                         $all_label = get_lang('ReviewQuestions');
@@ -3232,13 +3268,14 @@ class Exercise
                     }
                     // used to select it with jquery
                     $class .= ' question-validate-btn';
-                    $all_button[] = Display::button(
+                    $buttonList[] = Display::button(
                         'validate_all',
                         $all_label,
                         ['type' => 'button', 'class' => $class]
                     );
-                    $all_button[] = '&nbsp;'.Display::span(null, ['id' => 'save_all_reponse']);
-                    $html .= implode(PHP_EOL, $all_button);
+                    $buttonList[] = '&nbsp;'.Display::span(null, ['id' => 'save_all_response']);
+                    $html .= implode(PHP_EOL, $buttonList);
+
                 }
             }
         }
@@ -6733,7 +6770,6 @@ class Exercise
         $exe_id = intval($exe_id);
         $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
         if ($exercise_info) {
-
             if (empty($exercise_info['questions_to_check'])) {
                 if ($action == 'add') {
                     $sql = "UPDATE $track_exercises 
@@ -6743,16 +6779,20 @@ class Exercise
                 }
             } else {
                 $remind_list = explode(',', $exercise_info['questions_to_check']);
-
                 $remind_list_string = '';
                 if ($action == 'add') {
                     if (!in_array($question_id, $remind_list)) {
+                        $newRemindList = [];
                         $remind_list[] = $question_id;
-                        if (!empty($remind_list)) {
-                            sort($remind_list);
-                            array_filter($remind_list);
+                        $questionListInSession = Session::read('questionList');
+                        if (!empty($questionListInSession)) {
+                            foreach ($questionListInSession as $originalQuestionId) {
+                                if (in_array($originalQuestionId, $remind_list)) {
+                                    $newRemindList[] = $originalQuestionId;
+                                }
+                            }
                         }
-                        $remind_list_string = implode(',', $remind_list);
+                        $remind_list_string = implode(',', $newRemindList);
                     }
                 } elseif ($action == 'delete') {
                     if (!empty($remind_list)) {
@@ -6769,9 +6809,9 @@ class Exercise
                         }
                     }
                 }
-                $remind_list_string = Database::escape_string($remind_list_string);
+                $value = Database::escape_string($remind_list_string);
                 $sql = "UPDATE $track_exercises 
-                        SET questions_to_check = '$remind_list_string' 
+                        SET questions_to_check = '$value' 
                         WHERE exe_id = $exe_id ";
                 Database::query($sql);
             }

+ 1 - 2
main/exercise/exercise_reminder.php

@@ -132,7 +132,7 @@ if ($time_control) {
 
 echo Display::div('', array('id'=>'message'));
 echo '<script>
-    lp_data = $.param({"learnpath_id": '.$learnpath_id.', "learnpath_item_id" : '.$learnpath_item_id.', "learnpath_item_view_id": '.$learnpath_item_view_id.'});
+    var 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
@@ -203,7 +203,6 @@ $counter = 0;
 foreach ($question_list as $questionId) {
     // destruction of the Question object
     unset($objQuestionTmp);
-
     // creates a temporary Question object
     $objQuestionTmp = Question:: read($questionId);
     $quesId = $objQuestionTmp->selectId();

+ 42 - 32
main/exercise/exercise_submit.php

@@ -170,7 +170,7 @@ $templateName = $template->get_template('exercise/submit.js.tpl');
 $htmlHeadXtra[] = $template->fetch($templateName);
 
 $current_timestamp = time();
-$my_remind_list = array();
+$myRemindList = array();
 
 $time_control = false;
 if ($objExercise->expired_time != 0) {
@@ -454,17 +454,17 @@ if ($debug) {
 };
 
 if (!empty($exercise_stat_info['questions_to_check'])) {
-    $my_remind_list = $exercise_stat_info['questions_to_check'];
-    $my_remind_list = explode(',', $my_remind_list);
-    $my_remind_list = array_filter($my_remind_list);
+    $myRemindList = $exercise_stat_info['questions_to_check'];
+    $myRemindList = explode(',', $myRemindList);
+    $myRemindList = array_filter($myRemindList);
 }
 
-$params = "exe_id=$exe_id&exerciseId=$exerciseId&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id&".api_get_cidreq();
+$params = "exe_id=$exe_id&exerciseId=$exerciseId&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id&".api_get_cidreq().'&reminder='.$reminder;
 if ($debug) {
     error_log("6.1 params: ->  $params");
 }
 
-if ($reminder == 2 && empty($my_remind_list)) {
+if ($reminder == 2 && empty($myRemindList)) {
     if ($debug) {
         error_log("6.2 calling the exercise_reminder.php ");
     };
@@ -658,7 +658,6 @@ if ($formSent && isset($_POST)) {
 
     // the script "exercise_result.php" will take the variable $exerciseResult from the session
     Session::write('exerciseResult', $exerciseResult);
-    // Session::write('remind_list', $remind_list);
     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)
@@ -666,7 +665,9 @@ if ($formSent && isset($_POST)) {
         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('10. 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) {
                     $attempt_count = Event::get_attempt_count(
@@ -913,7 +914,7 @@ if (isset($_custom['exercises_hidden_when_no_start_date']) &&
     }
 }
 
-//Timer control
+// Timer control
 if ($time_control) {
     echo $objExercise->return_time_left_div();
     echo '<div style="display:none" class="warning-message" id="expired-message-id">'.get_lang('ExerciseExpiredTimeMessage').'</div>';
@@ -931,15 +932,15 @@ if ($reminder == 2) {
     $data_tracking = explode(',', $data_tracking);
     $current_question = 1; //set by default the 1st question
 
-    if (!empty($my_remind_list)) {
-        //Checking which questions we are going to call from the remind list
+    if (!empty($myRemindList)) {
+        // 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++) {
+            for ($j = 0; $j < count($myRemindList); $j++) {
                 if (!empty($remind_question_id)) {
-                    if ($remind_question_id == $my_remind_list[$j]) {
+                    if ($remind_question_id == $myRemindList[$j]) {
                         if ($remind_question_id == $data_tracking[$i]) {
-                            if (isset($my_remind_list[$j + 1])) {
-                                $remind_question_id = $my_remind_list[$j + 1];
+                            if (isset($myRemindList[$j + 1])) {
+                                $remind_question_id = $myRemindList[$j + 1];
                                 $current_question = $i + 1;
                             } else {
                                 // We end the remind list we go to the exercise_reminder.php please
@@ -950,9 +951,9 @@ if ($reminder == 2) {
                         }
                     }
                 } else {
-                    if ($my_remind_list[$j] == $data_tracking[$i]) {
-                        if (isset($my_remind_list[$j + 1])) {
-                            $remind_question_id = $my_remind_list[$j + 1];
+                    if ($myRemindList[$j] == $data_tracking[$i]) {
+                        if (isset($myRemindList[$j + 1])) {
+                            $remind_question_id = $myRemindList[$j + 1];
                             $current_question = $i + 1; // last question
                         } else {
                             // We end the remind list we go to the exercise_reminder.php please
@@ -966,7 +967,9 @@ if ($reminder == 2) {
         }
     } else {
         if ($objExercise->review_answers) {
-            if ($debug) { error_log('. redirecting to exercise_reminder.php '); }
+            if ($debug) {
+                error_log('. redirecting to exercise_reminder.php ');
+            }
             header("Location: exercise_reminder.php?$params");
             exit;
         }
@@ -1000,13 +1003,17 @@ if (!empty($error)) {
                 if ($current_question != $i) {
                     continue;
                 } else {
-                    if ($objQuestionTmp->selectType() == HOT_SPOT || $objQuestionTmp->selectType() == HOT_SPOT_DELINEATION) {
+                    if ($objQuestionTmp->selectType() == HOT_SPOT ||
+                        $objQuestionTmp->selectType() == HOT_SPOT_DELINEATION
+                    ) {
                         $number_of_hotspot_questions++;
                     }
                     break;
                 }
             } else {
-                if ($objQuestionTmp->selectType() == HOT_SPOT || $objQuestionTmp->selectType() == HOT_SPOT_DELINEATION) {
+                if ($objQuestionTmp->selectType() == HOT_SPOT ||
+                    $objQuestionTmp->selectType() == HOT_SPOT_DELINEATION
+                ) {
                     $number_of_hotspot_questions++;
                 }
             }
@@ -1218,8 +1225,7 @@ if (!empty($error)) {
             });
 
             free_answers = $.param(free_answers);
-
-            $("#save_all_reponse").html(\'' . Display::returnFontAwesomeIcon('spinner', null, true, 'fa-spin').'\');
+            $("#save_all_response").html(\'' . Display::returnFontAwesomeIcon('spinner', null, true, 'fa-spin').'\');
 
             $.ajax({
                 type:"post",
@@ -1228,14 +1234,14 @@ if (!empty($error)) {
                 data: "'.$params.'&type=all&"+my_choice+"&"+hotspot+"&"+free_answers+"&"+remind_list,
                 success: function(return_value) {
                     if (return_value == "ok") {
-                        //$("#save_all_reponse").html(\'' . Display::return_icon('accept.png').'\');
+                        //$("#save_all_response").html(\'' . Display::return_icon('accept.png').'\');
                         if (validate == "validate") {
                             window.location = "'.$script_php.'?'.$params.'";
                         } else {
-                            $("#save_all_reponse").html(\'' . Display::return_icon('accept.png').'\');
+                            $("#save_all_response").html(\'' . Display::return_icon('accept.png').'\');
                         }
                     } else {
-                        $("#save_all_reponse").html(\'' . Display::return_icon('wrong.gif').'\');
+                        $("#save_all_response").html(\'' . Display::return_icon('wrong.gif').'\');
                     }
                 }
             });
@@ -1247,15 +1253,16 @@ if (!empty($error)) {
         }
     </script>';
 
-    echo '<form id="exercise_form" method="post" action="'.api_get_self().'?'.api_get_cidreq().'&autocomplete=off&&exerciseId='.$exerciseId.'" name="frm_exercise" '.$onsubmit.'>
+    echo '<form id="exercise_form" method="post" action="'.api_get_self().'?'.api_get_cidreq().'&reminder='.$reminder.'&autocomplete=off&&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" />
          <input type="hidden" name="num_answer" value="'.$currentAnswer.'" id="num_current_answer_id" />
          <input type="hidden" name="exe_id" value="'.$exe_id.'" />
          <input type="hidden" name="origin" value="'.$origin.'" />
+         <input type="hidden" name="reminder" value="'.$reminder.'" />
          <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_id" value="'.$learnpath_item_id.'" />         
          <input type="hidden" name="learnpath_item_view_id" value="'.$learnpath_item_view_id.'" />';
 
     // Show list of questions
@@ -1334,8 +1341,8 @@ if (!empty($error)) {
         // Showing the exercise description
         if (!empty($objExercise->description)) {
             if ($objExercise->type == ONE_PER_PAGE || ($objExercise->type != ONE_PER_PAGE && $i == 1)) {
-                echo Display::panelCollapse('<span>'.
-                    get_lang('ExerciseDescriptionLabel').'</span>',
+                echo Display::panelCollapse(
+                    '<span>'.get_lang('ExerciseDescriptionLabel').'</span>',
                     $objExercise->description,
                     'exercise-description',
                     [],
@@ -1367,10 +1374,13 @@ if (!empty($error)) {
             case ONE_PER_PAGE:
                 $exerciseActions .= $objExercise->show_button(
                     $questionId,
-                    $current_question
+                    $current_question,
+                    [],
+                    [],
+                    $myRemindList
                 );
                 break;
-            case ALL_ON_ONE_PAGE :
+            case ALL_ON_ONE_PAGE:
                 $button = [
                     Display::button(
                         'save_now',