From eff7ef2cd22ec38fcbf808bc8ece046740cc80ad Mon Sep 17 00:00:00 2001 From: Mathew May Date: Thu, 21 May 2020 16:24:46 +0800 Subject: [PATCH] MDL-68788 core_notification: Check if the user is logged in --- lib/amd/build/notification.min.js | 2 +- lib/amd/build/notification.min.js.map | 2 +- lib/amd/src/notification.js | 10 +++++++--- lib/outputrenderers.php | 3 ++- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/amd/build/notification.min.js b/lib/amd/build/notification.min.js index ae7246225c06d..42cdec9154c2c 100644 --- a/lib/amd/build/notification.min.js +++ b/lib/amd/build/notification.min.js @@ -1,2 +1,2 @@ -define ("core/notification",["exports","core/pending","core/log"],function(a,b,c){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.default=a.init=a.exception=a.saveCancel=a.confirm=a.alert=a.addNotification=a.fetchNotifications=void 0;b=d(b);c=d(c);var q="undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:{};function d(a){return a&&a.__esModule?a:{default:a}}function e(a,b){return k(a)||j(a,b)||g(a,b)||f()}function f(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function g(a,b){if(!a)return;if("string"==typeof a)return h(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);if("Object"===c&&a.constructor)c=a.constructor.name;if("Map"===c||"Set"===c)return Array.from(c);if("Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c))return h(a,b)}function h(a,b){if(null==b||b>a.length)b=a.length;for(var c=0,d=Array(b);ca.length)b=a.length;for(var c=0,d=Array(b);c.\n\nimport Pending from 'core/pending';\nimport Log from 'core/log';\n\nlet currentContextId = M.cfg.contextid;\n\nconst notificationTypes = {\n success: 'core/notification_success',\n info: 'core/notification_info',\n warning: 'core/notification_warning',\n error: 'core/notification_error',\n};\n\nconst notificationRegionId = 'user-notifications';\n\nconst Selectors = {\n notificationRegion: `#${notificationRegionId}`,\n fallbackRegionParents: [\n '#region-main',\n '[role=\"main\"]',\n 'body',\n ],\n};\n\nconst setupTargetRegion = () => {\n let targetRegion = getNotificationRegion();\n if (targetRegion) {\n return false;\n }\n\n const newRegion = document.createElement('span');\n newRegion.id = notificationRegionId;\n\n return Selectors.fallbackRegionParents.some(selector => {\n const targetRegion = document.querySelector(selector);\n\n if (targetRegion) {\n targetRegion.prepend(newRegion);\n return true;\n }\n\n return false;\n });\n};\n\n\n/**\n * Poll the server for any new notifications.\n *\n * @returns {Promise}\n */\nexport const fetchNotifications = async() => {\n const Ajax = await import('core/ajax');\n\n return Ajax.call([{\n methodname: 'core_fetch_notifications',\n args: {\n contextid: currentContextId\n }\n }])[0]\n .then(addNotifications);\n};\n\n/**\n * Add all of the supplied notifications.\n *\n * @param {Array} notifications The list of notificaitons\n * @returns {Promise}\n */\nconst addNotifications = notifications => {\n if (!notifications.length) {\n return Promise.resolve();\n }\n\n const pendingPromise = new Pending('core/notification:addNotifications');\n notifications.forEach(notification => renderNotification(notification.template, notification.variables));\n\n return pendingPromise.resolve();\n};\n\n/**\n * Add a notification to the page.\n *\n * Note: This does not cause the notification to be added to the session.\n *\n * @param {Object} notification The notification to add.\n * @param {string} notification.message The body of the notification\n * @param {string} notification.type The type of notification to add (error, warning, info, success).\n * @param {Boolean} notification.closebutton Whether to show the close button.\n * @param {Boolean} notification.announce Whether to announce to screen readers.\n * @returns {Promise}\n */\nexport const addNotification = notification => {\n const pendingPromise = new Pending('core/notification:addNotifications');\n\n let template = notificationTypes.error;\n\n notification = {\n closebutton: true,\n announce: true,\n type: 'error',\n ...notification,\n };\n\n if (notification.template) {\n template = notification.template;\n delete notification.template;\n } else if (notification.type) {\n if (typeof notificationTypes[notification.type] !== 'undefined') {\n template = notificationTypes[notification.type];\n }\n delete notification.type;\n }\n\n return renderNotification(template, notification)\n .then(pendingPromise.resolve);\n};\n\nconst renderNotification = async(template, variables) => {\n if (typeof variables.message === 'undefined' || !variables.message) {\n Log.debug('Notification received without content. Skipping.');\n return;\n }\n\n const pendingPromise = new Pending('core/notification:renderNotification');\n const Templates = await import('core/templates');\n\n Templates.renderForPromise(template, variables)\n .then(({html, js = ''}) => {\n Templates.prependNodeContents(getNotificationRegion(), html, js);\n\n return;\n })\n .then(pendingPromise.resolve)\n .catch(exception);\n};\n\nconst getNotificationRegion = () => document.querySelector(Selectors.notificationRegion);\n\n/**\n * Alert dialogue.\n *\n * @param {String|Promise} title\n * @param {String|Promise} message\n * @param {String|Promise} cancelText\n * @returns {Promise}\n */\nexport const alert = async(title, message, cancelText) => {\n var pendingPromise = new Pending('core/notification:alert');\n\n const ModalFactory = await import('core/modal_factory');\n\n return ModalFactory.create({\n type: ModalFactory.types.ALERT,\n body: message,\n title: title,\n buttons: {\n cancel: cancelText,\n },\n removeOnClose: true,\n })\n .then(function(modal) {\n modal.show();\n\n pendingPromise.resolve();\n return modal;\n });\n};\n\n/**\n * The confirm has now been replaced with a save and cancel dialogue.\n *\n * @param {String|Promise} title\n * @param {String|Promise} question\n * @param {String|Promise} saveLabel\n * @param {String|Promise} noLabel\n * @param {String|Promise} saveCallback\n * @param {String|Promise} cancelCallback\n * @returns {Promise}\n */\nexport const confirm = (title, question, saveLabel, noLabel, saveCallback, cancelCallback) =>\n saveCancel(title, question, saveLabel, saveCallback, cancelCallback);\n\n/**\n * The Save and Cancel dialogue helper.\n *\n * @param {String|Promise} title\n * @param {String|Promise} question\n * @param {String|Promise} saveLabel\n * @param {String|Promise} saveCallback\n * @param {String|Promise} cancelCallback\n * @returns {Promise}\n */\nexport const saveCancel = async(title, question, saveLabel, saveCallback, cancelCallback) => {\n const pendingPromise = new Pending('core/notification:confirm');\n\n const [\n ModalFactory,\n ModalEvents,\n ] = await Promise.all([\n import('core/modal_factory'),\n import('core/modal_events'),\n ]);\n\n return ModalFactory.create({\n type: ModalFactory.types.SAVE_CANCEL,\n title: title,\n body: question,\n buttons: {\n // Note: The noLabel is no longer supported.\n save: saveLabel,\n },\n removeOnClose: true,\n })\n .then(function(modal) {\n modal.show();\n\n modal.getRoot().on(ModalEvents.save, saveCallback);\n modal.getRoot().on(ModalEvents.cancel, cancelCallback);\n pendingPromise.resolve();\n\n return modal;\n });\n};\n\n/**\n * Wrap M.core.exception.\n *\n * @param {Error} ex\n */\nexport const exception = async ex => {\n const pendingPromise = new Pending('core/notification:displayException');\n\n // Fudge some parameters.\n if (!ex.stack) {\n ex.stack = '';\n }\n\n if (ex.debuginfo) {\n ex.stack += ex.debuginfo + '\\n';\n }\n\n if (!ex.backtrace && ex.stacktrace) {\n ex.backtrace = ex.stacktrace;\n }\n\n if (ex.backtrace) {\n ex.stack += ex.backtrace;\n const ln = ex.backtrace.match(/line ([^ ]*) of/);\n const fn = ex.backtrace.match(/ of ([^:]*): /);\n if (ln && ln[1]) {\n ex.lineNumber = ln[1];\n }\n if (fn && fn[1]) {\n ex.fileName = fn[1];\n if (ex.fileName.length > 30) {\n ex.fileName = '...' + ex.fileName.substr(ex.fileName.length - 27);\n }\n }\n }\n\n if (typeof ex.name === 'undefined' && ex.errorcode) {\n ex.name = ex.errorcode;\n }\n\n const Y = await import('core/yui');\n Y.use('moodle-core-notification-exception', function() {\n var modal = new M.core.exception(ex);\n\n modal.show();\n\n pendingPromise.resolve();\n });\n};\n\n/**\n * Initialise the page for the suppled context, and displaying the supplied notifications.\n *\n * @param {Number} contextId\n * @param {Array} notificationList\n */\nexport const init = (contextId, notificationList) => {\n currentContextId = contextId;\n\n // Setup the message target region if it isn't setup already\n setupTargetRegion();\n\n // Add provided notifications.\n addNotifications(notificationList);\n\n // Perform an initial poll for any new notifications.\n fetchNotifications();\n};\n\n// To maintain backwards compatability we export default here.\nexport default {\n init,\n fetchNotifications,\n addNotification,\n alert,\n confirm,\n saveCancel,\n exception,\n};\n"],"file":"notification.min.js"} \ No newline at end of file +{"version":3,"sources":["../src/notification.js"],"names":["currentContextId","M","cfg","contextid","notificationTypes","success","info","warning","error","notificationRegionId","Selectors","notificationRegion","fallbackRegionParents","setupTargetRegion","targetRegion","getNotificationRegion","newRegion","document","createElement","id","some","selector","querySelector","prepend","fetchNotifications","Ajax","call","methodname","args","then","addNotifications","notifications","length","Promise","resolve","pendingPromise","Pending","forEach","notification","renderNotification","template","variables","addNotification","closebutton","announce","type","message","Log","debug","Templates","renderForPromise","html","js","prependNodeContents","catch","exception","alert","title","cancelText","ModalFactory","create","types","ALERT","body","buttons","cancel","removeOnClose","modal","show","confirm","question","saveLabel","noLabel","saveCallback","cancelCallback","saveCancel","all","ModalEvents","SAVE_CANCEL","save","getRoot","on","ex","stack","debuginfo","backtrace","stacktrace","ln","match","fn","lineNumber","fileName","substr","name","errorcode","Y","use","core","init","contextId","notificationList","userLoggedIn"],"mappings":"0PAeA,OACA,O,uoEAEIA,CAAAA,CAAgB,CAAGC,CAAC,CAACC,GAAF,CAAMC,S,CAEvBC,CAAiB,CAAG,CACtBC,OAAO,CAAG,2BADY,CAEtBC,IAAI,CAAM,wBAFY,CAGtBC,OAAO,CAAG,2BAHY,CAItBC,KAAK,CAAK,yBAJY,C,CAOpBC,CAAoB,CAAG,oB,CAEvBC,CAAS,CAAG,CACdC,kBAAkB,YAAMF,CAAN,CADJ,CAEdG,qBAAqB,CAAE,CACnB,cADmB,CAEnB,iBAFmB,CAGnB,MAHmB,CAFT,C,CASZC,CAAiB,CAAG,UAAM,CAC5B,GAAIC,CAAAA,CAAY,CAAGC,CAAqB,EAAxC,CACA,GAAID,CAAJ,CAAkB,CACd,QACH,CAED,GAAME,CAAAA,CAAS,CAAGC,QAAQ,CAACC,aAAT,CAAuB,MAAvB,CAAlB,CACAF,CAAS,CAACG,EAAV,CAAeV,CAAf,CAEA,MAAOC,CAAAA,CAAS,CAACE,qBAAV,CAAgCQ,IAAhC,CAAqC,SAAAC,CAAQ,CAAI,CACpD,GAAMP,CAAAA,CAAY,CAAGG,QAAQ,CAACK,aAAT,CAAuBD,CAAvB,CAArB,CAEA,GAAIP,CAAJ,CAAkB,CACdA,CAAY,CAACS,OA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,CACnD,GAAI7B,CAAAA,CAAK,CAAG,GAAIlE,CAAAA,CAAC,CAACgG,IAAF,CAAO1C,SAAX,CAAqB2B,CAArB,CAAZ,CAEAf,CAAK,CAACC,IAAN,GAEAjC,CAAc,CAACD,OAAf,EACH,CAND,EApCqB,yCAAH,uDAAf,C,cAoDA,GAAMgE,CAAAA,CAAI,CAAG,SAACC,CAAD,CAAYC,CAAZ,CAA8BC,CAA9B,CAA+C,CAC/DrG,CAAgB,CAAGmG,CAAnB,CAGAtF,CAAiB,GAGjBiB,CAAgB,CAACsE,CAAD,CAAhB,CAGA,GAAIC,CAAJ,CAAkB,CAEd7E,CAAkB,EACrB,CACJ,CAdM,C,mBAiBQ,CACX0E,IAAI,CAAJA,CADW,CAEX1E,kBAAkB,CAAlBA,CAFW,CAGXkB,eAAe,CAAfA,CAHW,CAIXc,KAAK,CAALA,CAJW,CAKXa,OAAO,CAAPA,CALW,CAMXM,UAAU,CAAVA,CANW,CAOXpB,SAAS,CAATA,CAPW,C","sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\nimport Pending from 'core/pending';\nimport Log from 'core/log';\n\nlet currentContextId = M.cfg.contextid;\n\nconst notificationTypes = {\n success: 'core/notification_success',\n info: 'core/notification_info',\n warning: 'core/notification_warning',\n error: 'core/notification_error',\n};\n\nconst notificationRegionId = 'user-notifications';\n\nconst Selectors = {\n notificationRegion: `#${notificationRegionId}`,\n fallbackRegionParents: [\n '#region-main',\n '[role=\"main\"]',\n 'body',\n ],\n};\n\nconst setupTargetRegion = () => {\n let targetRegion = getNotificationRegion();\n if (targetRegion) {\n return false;\n }\n\n const newRegion = document.createElement('span');\n newRegion.id = notificationRegionId;\n\n return Selectors.fallbackRegionParents.some(selector => {\n const targetRegion = document.querySelector(selector);\n\n if (targetRegion) {\n targetRegion.prepend(newRegion);\n return true;\n }\n\n return false;\n });\n};\n\n\n/**\n * Poll the server for any new notifications.\n *\n * @returns {Promise}\n */\nexport const fetchNotifications = async() => {\n const Ajax = await import('core/ajax');\n\n return Ajax.call([{\n methodname: 'core_fetch_notifications',\n args: {\n contextid: currentContextId\n }\n }])[0]\n .then(addNotifications);\n};\n\n/**\n * Add all of the supplied notifications.\n *\n * @param {Array} notifications The list of notificaitons\n * @returns {Promise}\n */\nconst addNotifications = notifications => {\n if (!notifications.length) {\n return Promise.resolve();\n }\n\n const pendingPromise = new Pending('core/notification:addNotifications');\n notifications.forEach(notification => renderNotification(notification.template, notification.variables));\n\n return pendingPromise.resolve();\n};\n\n/**\n * Add a notification to the page.\n *\n * Note: This does not cause the notification to be added to the session.\n *\n * @param {Object} notification The notification to add.\n * @param {string} notification.message The body of the notification\n * @param {string} notification.type The type of notification to add (error, warning, info, success).\n * @param {Boolean} notification.closebutton Whether to show the close button.\n * @param {Boolean} notification.announce Whether to announce to screen readers.\n * @returns {Promise}\n */\nexport const addNotification = notification => {\n const pendingPromise = new Pending('core/notification:addNotifications');\n\n let template = notificationTypes.error;\n\n notification = {\n closebutton: true,\n announce: true,\n type: 'error',\n ...notification,\n };\n\n if (notification.template) {\n template = notification.template;\n delete notification.template;\n } else if (notification.type) {\n if (typeof notificationTypes[notification.type] !== 'undefined') {\n template = notificationTypes[notification.type];\n }\n delete notification.type;\n }\n\n return renderNotification(template, notification)\n .then(pendingPromise.resolve);\n};\n\nconst renderNotification = async(template, variables) => {\n if (typeof variables.message === 'undefined' || !variables.message) {\n Log.debug('Notification received without content. Skipping.');\n return;\n }\n\n const pendingPromise = new Pending('core/notification:renderNotification');\n const Templates = await import('core/templates');\n\n Templates.renderForPromise(template, variables)\n .then(({html, js = ''}) => {\n Templates.prependNodeContents(getNotificationRegion(), html, js);\n\n return;\n })\n .then(pendingPromise.resolve)\n .catch(exception);\n};\n\nconst getNotificationRegion = () => document.querySelector(Selectors.notificationRegion);\n\n/**\n * Alert dialogue.\n *\n * @param {String|Promise} title\n * @param {String|Promise} message\n * @param {String|Promise} cancelText\n * @returns {Promise}\n */\nexport const alert = async(title, message, cancelText) => {\n var pendingPromise = new Pending('core/notification:alert');\n\n const ModalFactory = await import('core/modal_factory');\n\n return ModalFactory.create({\n type: ModalFactory.types.ALERT,\n body: message,\n title: title,\n buttons: {\n cancel: cancelText,\n },\n removeOnClose: true,\n })\n .then(function(modal) {\n modal.show();\n\n pendingPromise.resolve();\n return modal;\n });\n};\n\n/**\n * The confirm has now been replaced with a save and cancel dialogue.\n *\n * @param {String|Promise} title\n * @param {String|Promise} question\n * @param {String|Promise} saveLabel\n * @param {String|Promise} noLabel\n * @param {String|Promise} saveCallback\n * @param {String|Promise} cancelCallback\n * @returns {Promise}\n */\nexport const confirm = (title, question, saveLabel, noLabel, saveCallback, cancelCallback) =>\n saveCancel(title, question, saveLabel, saveCallback, cancelCallback);\n\n/**\n * The Save and Cancel dialogue helper.\n *\n * @param {String|Promise} title\n * @param {String|Promise} question\n * @param {String|Promise} saveLabel\n * @param {String|Promise} saveCallback\n * @param {String|Promise} cancelCallback\n * @returns {Promise}\n */\nexport const saveCancel = async(title, question, saveLabel, saveCallback, cancelCallback) => {\n const pendingPromise = new Pending('core/notification:confirm');\n\n const [\n ModalFactory,\n ModalEvents,\n ] = await Promise.all([\n import('core/modal_factory'),\n import('core/modal_events'),\n ]);\n\n return ModalFactory.create({\n type: ModalFactory.types.SAVE_CANCEL,\n title: title,\n body: question,\n buttons: {\n // Note: The noLabel is no longer supported.\n save: saveLabel,\n },\n removeOnClose: true,\n })\n .then(function(modal) {\n modal.show();\n\n modal.getRoot().on(ModalEvents.save, saveCallback);\n modal.getRoot().on(ModalEvents.cancel, cancelCallback);\n pendingPromise.resolve();\n\n return modal;\n });\n};\n\n/**\n * Wrap M.core.exception.\n *\n * @param {Error} ex\n */\nexport const exception = async ex => {\n const pendingPromise = new Pending('core/notification:displayException');\n\n // Fudge some parameters.\n if (!ex.stack) {\n ex.stack = '';\n }\n\n if (ex.debuginfo) {\n ex.stack += ex.debuginfo + '\\n';\n }\n\n if (!ex.backtrace && ex.stacktrace) {\n ex.backtrace = ex.stacktrace;\n }\n\n if (ex.backtrace) {\n ex.stack += ex.backtrace;\n const ln = ex.backtrace.match(/line ([^ ]*) of/);\n const fn = ex.backtrace.match(/ of ([^:]*): /);\n if (ln && ln[1]) {\n ex.lineNumber = ln[1];\n }\n if (fn && fn[1]) {\n ex.fileName = fn[1];\n if (ex.fileName.length > 30) {\n ex.fileName = '...' + ex.fileName.substr(ex.fileName.length - 27);\n }\n }\n }\n\n if (typeof ex.name === 'undefined' && ex.errorcode) {\n ex.name = ex.errorcode;\n }\n\n const Y = await import('core/yui');\n Y.use('moodle-core-notification-exception', function() {\n var modal = new M.core.exception(ex);\n\n modal.show();\n\n pendingPromise.resolve();\n });\n};\n\n/**\n * Initialise the page for the suppled context, and displaying the supplied notifications.\n *\n * @param {Number} contextId\n * @param {Array} notificationList\n * @param {Boolean} userLoggedIn\n */\nexport const init = (contextId, notificationList, userLoggedIn) => {\n currentContextId = contextId;\n\n // Setup the message target region if it isn't setup already\n setupTargetRegion();\n\n // Add provided notifications.\n addNotifications(notificationList);\n\n // If the user is not logged in then we can not fetch anything for them.\n if (userLoggedIn) {\n // Perform an initial poll for any new notifications.\n fetchNotifications();\n }\n};\n\n// To maintain backwards compatability we export default here.\nexport default {\n init,\n fetchNotifications,\n addNotification,\n alert,\n confirm,\n saveCancel,\n exception,\n};\n"],"file":"notification.min.js"} \ No newline at end of file diff --git a/lib/amd/src/notification.js b/lib/amd/src/notification.js index 082289991711e..62cec7e903729 100644 --- a/lib/amd/src/notification.js +++ b/lib/amd/src/notification.js @@ -292,8 +292,9 @@ export const exception = async ex => { * * @param {Number} contextId * @param {Array} notificationList + * @param {Boolean} userLoggedIn */ -export const init = (contextId, notificationList) => { +export const init = (contextId, notificationList, userLoggedIn) => { currentContextId = contextId; // Setup the message target region if it isn't setup already @@ -302,8 +303,11 @@ export const init = (contextId, notificationList) => { // Add provided notifications. addNotifications(notificationList); - // Perform an initial poll for any new notifications. - fetchNotifications(); + // If the user is not logged in then we can not fetch anything for them. + if (userLoggedIn) { + // Perform an initial poll for any new notifications. + fetchNotifications(); + } }; // To maintain backwards compatability we export default here. diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 2e589f5d8987f..9d5cc168def46 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -1414,7 +1414,8 @@ public function footer() { if (!empty($this->page->context->id)) { $this->page->requires->js_call_amd('core/notification', 'init', array( $this->page->context->id, - \core\notification::fetch_as_array($this) + \core\notification::fetch_as_array($this), + isloggedin() )); } $footer = str_replace($this->unique_end_html_token, $this->page->requires->get_end_code(), $footer);