Browse Source

Fix firefox issue when loading events during "unload" see BT#14149

jmontoyaa 7 years ago
parent
commit
7cdf154a34

+ 25 - 15
main/exercise/exercise_submit.php

@@ -1041,6 +1041,28 @@ if (!empty($error)) {
     }
 
     echo '<script>
+        function addExerciseEvent(elm, evType, fn, useCapture) {
+            if (elm.addEventListener) {
+                elm.addEventListener(evType, fn, useCapture);
+                return true;
+            } else if (elm.attachEvent) {
+                elm.attachEvent(\'on\' + evType, fn);
+            } else{
+                elm[\'on\'+evType] = fn;
+            }
+        }
+        
+        function updateDuration() {
+            var saveDurationUrl = "'.$saveDurationUrl.'";
+            // Logout of course just in case
+            $.ajax({
+                url: saveDurationUrl,
+                success: function (data) {
+                    return 1;
+                }
+            });
+        }
+        
         $(function() {
             //This pre-load the save.png icon
             var saveImage = new Image();
@@ -1103,8 +1125,7 @@ if (!empty($error)) {
 
             $(\'button[name="save_now"]\').on(\'click\', function (e) {
                 e.preventDefault();
-                e.stopPropagation();
-                
+                e.stopPropagation();                
                 var
                     $this = $(this),
                     questionId = parseInt($this.data(\'question\')) || 0,
@@ -1115,23 +1136,12 @@ if (!empty($error)) {
 
             $(\'button[name="validate_all"]\').on(\'click\', function (e) {
                 e.preventDefault();
-                e.stopPropagation();
-                
+                e.stopPropagation();                
                 validate_all();
             });
             
-            var saveDurationUrl = "'.$saveDurationUrl.'";
-            
             // Save attempt duration
-            $(window).on(\'beforeunload\', function () {
-                // Logout of course just in case
-                $.ajax({
-                    url: saveDurationUrl,
-                    success: function (data) {
-                        return 1;
-                    }
-                });
-            });
+            addExerciseEvent(window, \'unload\', updateDuration ,false);
         });
 
         function previous_question(question_num) {

+ 1 - 1
main/lp/lp_ajax_start_timer.php

@@ -4,7 +4,7 @@
 /**
  * @package chamilo.learnpath
  *
- * @author Yannick Warnier <ywarnier@beeznest.org>
+ * @author  Yannick Warnier <ywarnier@beeznest.org>
  */
 
 echo time();

+ 21 - 10
main/template/rainbow/layout/head.tpl

@@ -28,17 +28,28 @@ var connect_lang = '{{ "ChatConnected"|get_lang }}';
 var disconnect_lang = '{{ "ChatDisconnected"|get_lang }}';
 var logOutUrl = '{{ _p.web_ajax }}course.ajax.php?a=course_logout&{{ _p.web_cid_query }}';
 
-$(function() {
-    // Executes course logout when user close the browser tab/window
-    $(window).on('beforeunload', function () {
-        // Logout of course just in case
-        $.ajax({
-            url: logOutUrl,
-            success: function (data) {
-                return 1;
-            }
-        });
+function addMainEvent(elm, evType, fn, useCapture) {
+    if (elm.addEventListener) {
+        elm.addEventListener(evType, fn, useCapture);
+        return true;
+    } else if (elm.attachEvent) {
+        elm.attachEvent('on' + evType, fn);
+    } else{
+        elm['on'+evType] = fn;
+    }
+}
+
+function courseLogout() {
+    $.ajax({
+        url: logOutUrl,
+        success: function (data) {
+            return 1;
+        }
     });
+}
+
+$(function() {
+    addMainEvent(window, 'unload', courseLogout ,false);
 });
 
 </script>