MDL-68991 core: Prevent popup blockers blocking feedback window
authorShamim Rezaie <shamim@moodle.com>
Tue, 9 Jun 2020 11:34:44 +0000 (21:34 +1000)
committerShamim Rezaie <shamim@moodle.com>
Wed, 10 Jun 2020 06:10:53 +0000 (16:10 +1000)
Some browsers like Firefox are very inflexible with window.open()
and block it if it is not instantly invoked after the user click.

Also according to https://stackoverflow.com/a/6807615 it is best
practice to replace self:: with static::

lib/amd/build/userfeedback.min.js
lib/amd/build/userfeedback.min.js.map
lib/amd/src/userfeedback.js
lib/classes/userfeedback.php
lib/tests/behat/userfeedback.feature

index 3eee54b..03f8faf 100644 (file)
Binary files a/lib/amd/build/userfeedback.min.js and b/lib/amd/build/userfeedback.min.js differ
index 2a4225f..71fb5b6 100644 (file)
Binary files a/lib/amd/build/userfeedback.min.js.map and b/lib/amd/build/userfeedback.min.js.map differ
index c1b1a7f..28a77ed 100644 (file)
@@ -42,8 +42,12 @@ export const registerEventListeners = () => {
         if (giveAction) {
             e.preventDefault();
 
-            giveFeedback()
-                .then(() => hideRoot(giveAction))
+            if (!window.open(giveAction.href)) {
+                throw new Error('Unable to open popup');
+            }
+
+            Promise.resolve(giveAction)
+                .then(hideRoot)
                 .then(recordAction)
                 .catch(Notification.exception);
         }
@@ -60,26 +64,6 @@ export const registerEventListeners = () => {
     });
 };
 
-/**
- * The action function that is called when users choose to give feedback.
- *
- * @returns {Promise<void>}
- */
-const giveFeedback = () => {
-    return Ajax.call([{
-        methodname: 'core_get_userfeedback_url',
-        args: {
-            contextid: M.cfg.contextid,
-        }
-    }])[0]
-        .then(url => {
-            if (!window.open(url)) {
-                throw new Error('Unable to open popup');
-            }
-            return;
-        });
-};
-
 /**
  * Record the action that the user took.
  *
index 8128336..7d315ee 100644 (file)
@@ -57,9 +57,9 @@ class core_userfeedback {
         $actions = [
             [
                 'title' => get_string('calltofeedback_give'),
-                'url' => '#',
+                'url' => static::make_link()->out(false),
                 'data' => [
-                        'action' => 'give',
+                    'action' => 'give',
                     'record' => 1,
                     'hide' => 1,
                 ],
@@ -103,13 +103,13 @@ class core_userfeedback {
             $lastactiontime = max($give ?: 0, $remind ?: 0);
 
             switch ($CFG->userfeedback_nextreminder) {
-                case self::REMIND_AFTER_UPGRADE:
-                    $lastupgrade = self::last_major_upgrade_time();
+                case static::REMIND_AFTER_UPGRADE:
+                    $lastupgrade = static::last_major_upgrade_time();
                     if ($lastupgrade >= $lastactiontime) {
                         return $lastupgrade + ($CFG->userfeedback_remindafter * DAYSECS) < time();
                     }
                     break;
-                case self::REMIND_PERIODICALLY:
+                case static::REMIND_PERIODICALLY:
                     return $lastactiontime + ($CFG->userfeedback_remindafter * DAYSECS) < time();
                     break;
             }
index b090f80..6cf33d6 100644 (file)
@@ -34,3 +34,17 @@ Feature: Gathering user feedback
     And I reload the page
     Then I should not see "Give feedback" in the "region-main" "region"
     And I should not see "Remind me later" in the "region-main" "region"
+
+  @javascript
+  Scenario: Users should not see the notification after they click on the give feedback link
+    Given the following config values are set as admin:
+      | enableuserfeedback        | 1   |
+      | userfeedback_nextreminder | 2   |
+      | userfeedback_remindafter  | 90  |
+    When I log in as "admin"
+    And I follow "Dashboard" in the user menu
+    And I click on "Give feedback" "link"
+    And I close all opened windows
+    And I reload the page
+    Then I should not see "Give feedback" in the "region-main" "region"
+    And I should not see "Remind me later" in the "region-main" "region"