Bladeren bron

Exercise popup feedback: Only allow one attempt see BT#15827

Julio Montoya 5 jaren geleden
bovenliggende
commit
37395c0d30
3 gewijzigde bestanden met toevoegingen van 104 en 41 verwijderingen
  1. 15 6
      main/exercise/exercise.class.php
  2. 27 14
      main/exercise/exercise_submit.php
  3. 62 21
      main/exercise/exercise_submit_modal.php

+ 15 - 6
main/exercise/exercise.class.php

@@ -3139,6 +3139,7 @@ class Exercise
             if ($questionNum == count($this->questionList)) {
                 $urlTitle = get_lang('EndTest');
             }
+
             $url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit_modal.php?'.api_get_cidreq();
             $url .= '&'.http_build_query([
                 'learnpath_id' => $safe_lp_id,
@@ -3151,15 +3152,23 @@ class Exercise
                 'exerciseId' => $this->id,
                 'reminder' => empty($myRemindList) ? null : 2,
             ]);
+
+            $params = [
+                'class' => 'ajax btn btn-default no-close-button',
+                'data-title' => Security::remove_XSS(get_lang('Comment')),
+                'data-size' => 'md',
+                'id' => "button_$question_id",
+            ];
+
+            if ($this->getFeedbackType() === EXERCISE_FEEDBACK_TYPE_POPUP) {
+                //$params['data-block-div-after-closing'] = "question_div_$question_id";
+                $params['data-block-closing'] = 'true';
+            }
+
             $html .= Display::url(
                 $urlTitle,
                 $url,
-                [
-                    'class' => 'ajax btn btn-default',
-                    'data-title' => Security::remove_XSS(get_lang('Comment')),
-                    'data-size' => 'md',
-                    'id' => "button_$question_id",
-                ]
+                $params
             );
             $html .= '<br />';
         } else {

+ 27 - 14
main/exercise/exercise_submit.php

@@ -151,6 +151,7 @@ if (!isset($exerciseInSession) || isset($exerciseInSession) && ($exerciseInSessi
     Session::write('firstTime', false);
 }
 //2. Checking if $objExercise is set
+/** @var |Exercise $objExercise  */
 if (!isset($objExercise) && isset($exerciseInSession)) {
     if ($debug) {
         error_log('2. Loading $objExercise from session');
@@ -1472,21 +1473,33 @@ if (!empty($error)) {
 
         echo '<div id="question_div_'.$questionId.'" class="main-question '.$remind_highlight.'" >';
 
+        $showQuestion = true;
+        $exerciseResultFromSession = Session::read('exerciseResult');
+        if ($objExercise->getFeedbackType() === EXERCISE_FEEDBACK_TYPE_POPUP &&
+            isset($exerciseResultFromSession[$questionId])
+        ) {
+            $showQuestion = false;
+        }
+
         // Shows the question and its answers
-        ExerciseLib::showQuestion(
-            $objExercise,
-            $questionId,
-            false,
-            $origin,
-            $i,
-            $objExercise->getHideQuestionTitle() ? false : true,
-            false,
-            $user_choice,
-            false,
-            null,
-            false,
-            true
-        );
+        if ($showQuestion) {
+            ExerciseLib::showQuestion(
+                $objExercise,
+                $questionId,
+                false,
+                $origin,
+                $i,
+                $objExercise->getHideQuestionTitle() ? false : true,
+                false,
+                $user_choice,
+                false,
+                null,
+                false,
+                true
+            );
+        } else {
+            echo Display::return_message(get_lang('AlreadyAnswered'));
+        }
 
         // Button save and continue
         switch ($objExercise->type) {

+ 62 - 21
main/exercise/exercise_submit_modal.php

@@ -16,6 +16,7 @@ require_once api_get_path(LIBRARY_PATH).'geometry.lib.php';
 
 /** @var Exercise $objExercise */
 $objExercise = Session::read('objExercise');
+$exerciseResult = Session::read('exerciseResult');
 
 if (empty($objExercise)) {
     api_not_allowed();
@@ -27,19 +28,20 @@ if (!in_array($feedbackType, [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_T
     api_not_allowed();
 }
 
-$exerciseResult = Session::read('exerciseResult');
 $learnpath_id = isset($_REQUEST['learnpath_id']) ? (int) $_REQUEST['learnpath_id'] : 0;
 $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? (int) $_REQUEST['learnpath_item_id'] : 0;
 $questionList = Session::read('questionList');
 
 $exerciseId = (int) $_GET['exerciseId'];
-//$exerciseType = (int) $_GET['exerciseType'];
 $questionNum = (int) $_GET['num'];
-//$nbrQuestions = isset($_GET['nbrQuestions']) ? (int) $_GET['nbrQuestions'] : null;
 $questionId = $questionList[$questionNum];
-
 $choiceValue = isset($_GET['choice']) ? $_GET['choice'] : '';
 $hotSpot = isset($_GET['hotspot']) ? $_GET['hotspot'] : '';
+$loaded = isset($_GET['loaded']);
+
+if (empty($choiceValue) && isset($exerciseResult[$questionId])) {
+    $choiceValue = $exerciseResult[$questionId];
+}
 
 if (!empty($hotSpot)) {
     if (isset($hotSpot[$questionId])) {
@@ -53,8 +55,61 @@ if (!empty($choiceValue)) {
     }
 }
 
+echo '<script>
+function tryAgain() {
+    $(function () {
+        $("#global-modal").modal("hide");
+    });
+}
+
+function SendEx(num) {
+    if (num == -1) {
+        window.location.href = "exercise_result.php?'.api_get_cidreq().'&take_session=1&exerciseId='.$exerciseId.'&num="+num+"&learnpath_item_id='.$learnpath_item_id.'&learnpath_id='.$learnpath_id.'";
+    } else {
+        num -= 1;
+        window.location.href = "exercise_submit.php?'.api_get_cidreq().'&tryagain=1&exerciseId='.$exerciseId.'&num="+num+"&learnpath_item_id='.$learnpath_item_id.'&learnpath_id='.$learnpath_id.'";
+    }    
+    return false;
+}
+</script>';
+
 echo '<div id="delineation-container">';
 // Getting the options by js
+if (empty($choiceValue) && empty($hotSpot) && $loaded) {
+    $nextQuestion = $questionNum + 1;
+    $destinationId = isset($questionList[$nextQuestion]) ? $questionList[$nextQuestion] : -1;
+    $icon = Display::return_icon(
+        'reload.png',
+        '',
+        ['style' => 'width:22px; height:22px; padding-left:0px;padding-right:5px;']
+    );
+    $links = '<a onclick="tryAgain();" href="#">'.get_lang('TryAgain').'</a>&nbsp;'.$icon.'&nbsp;';
+
+    // the link to finish the test
+    if ($destinationId == -1) {
+        $links .= Display::return_icon(
+                'finish.gif',
+                '',
+                ['style' => 'width:22px; height:22px; padding-left:0px;padding-right:5px;']
+            ).'<a onclick="SendEx(-1);" href="#">'.get_lang('EndActivity').'</a><br /><br />';
+    } else {
+        // the link to other question
+        if (in_array($destinationId, $questionList)) {
+            $num_value_array = array_keys($questionList, $destinationId);
+            $icon = Display::return_icon(
+                'quiz.png',
+                '',
+                ['style' => 'padding-left:0px;padding-right:5px;']
+            );
+            $links .= '<a onclick="SendEx('.$num_value_array[0].');" href="#">'.
+                get_lang('Question').' '.$num_value_array[0].'</a>&nbsp;';
+            $links .= $icon;
+        }
+    }
+    echo '<div class="row"><div class="col-md-5 col-md-offset-7"><h5 class="pull-right">'.$links.'</h5></div></div>';
+    exit;
+}
+
 if (empty($choiceValue) && empty($hotSpot)) {
     echo "<script>
         // this works for only radio buttons
@@ -89,16 +144,16 @@ if (empty($choiceValue) && empty($hotSpot)) {
         }
         
         var my_choice = $('*[name*=\"choice[".$questionId."]\"]').serialize();
-        var hotspot = $('*[name*=\"hotspot[".$questionId."]\"]').serialize();
-        //var my_choiceDc = $('*[name*=\"choiceDegreeCertainty['+question_id+']\"]').serialize();        
+        var hotspot = $('*[name*=\"hotspot[".$questionId."]\"]').serialize();     
     ";
 
     // IMPORTANT
     // This is the real redirect function
-    $extraUrl = '&exerciseId='.$exerciseId.'&num='.$questionNum.'&learnpath_id='.$learnpath_id.'&learnpath_item_id='.$learnpath_item_id;
+    $extraUrl = '&loaded=1&exerciseId='.$exerciseId.'&num='.$questionNum.'&learnpath_id='.$learnpath_id.'&learnpath_item_id='.$learnpath_item_id;
     $url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit_modal.php?'.api_get_cidreq().$extraUrl;
     echo ' url = "'.addslashes($url).'&hotspotcoord="+ hotspotcoord + "&"+ hotspot + "&"+ my_choice;';
     echo "$('#global-modal .modal-body').load(url);";
+
     echo '</script>';
     exit;
 }
@@ -131,8 +186,6 @@ Session::write('exerciseResult', $exerciseResult);
 
 $objQuestionTmp = Question::read($questionId);
 $answerType = $objQuestionTmp->selectType();
-$objAnswerTmp = new Answer($questionId);
-$nbrAnswers = $objAnswerTmp->selectNbrAnswers();
 $showResult = false;
 
 $objAnswerTmp = new Answer($questionId, api_get_course_int_id());
@@ -296,18 +349,6 @@ if ($destinationId == -1) {
     }
 }
 
-echo '<script>
-function SendEx(num) {
-    if (num == -1) {
-        window.location.href = "exercise_result.php?'.api_get_cidreq().'&take_session=1&exerciseId='.$exerciseId.'&num="+num+"&learnpath_item_id='.$learnpath_item_id.'&learnpath_id='.$learnpath_id.'";
-    } else {
-        num -= 1;
-        window.location.href = "exercise_submit.php?'.api_get_cidreq().'&tryagain=1&exerciseId='.$exerciseId.'&num="+num+"&learnpath_item_id='.$learnpath_item_id.'&learnpath_id='.$learnpath_id.'";
-    }    
-    return false;
-}
-</script>';
-
 if (!empty($links)) {
     echo '<div>'.$contents.'</div>';
     echo '<div style="padding-left: 450px"><h5>'.$links.'</h5></div>';