From ef1ebea848bbc5ba93a47865d169127bd22ed403 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Fri, 2 Aug 2024 16:41:55 +0100 Subject: [PATCH] MDL-82693 contentbank: improve display unlisted module, add tests. --- contentbank/amd/build/displayunlisted.min.js | 9 ++++- .../amd/build/displayunlisted.min.js.map | 2 +- contentbank/amd/src/displayunlisted.js | 40 +++++++++---------- contentbank/index.php | 12 ++---- contentbank/tests/behat/visibility.feature | 17 ++++++++ 5 files changed, 48 insertions(+), 32 deletions(-) diff --git a/contentbank/amd/build/displayunlisted.min.js b/contentbank/amd/build/displayunlisted.min.js index f62ae5c947c..a6c29f9c0d7 100644 --- a/contentbank/amd/build/displayunlisted.min.js +++ b/contentbank/amd/build/displayunlisted.min.js @@ -1,3 +1,10 @@ -define("core_contentbank/displayunlisted",["exports","core/ajax"],(function(_exports,_ajax){Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.update=void 0;_exports.update=elementSelector=>{const element=document.querySelector(elementSelector);element.addEventListener("click",(function(){const args={userid:this.userId,preferences:[{type:"core_contentbank_displayunlisted",value:!!element.checked}]};(0,_ajax.call)([{methodname:"core_user_update_user_preferences",args:args}])[0].done((function(){document.location.reload()}))}))}})); +define("core_contentbank/displayunlisted",["exports","core/notification","core/pending","core_user/repository"],(function(_exports,_notification,_pending,_repository){function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}} +/** + * Module to handle toggling "Display unlisted" preference + * + * @module core_contentbank/displayunlisted + * @copyright 2023 Daniel Neis Araujo + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=void 0,_notification=_interopRequireDefault(_notification),_pending=_interopRequireDefault(_pending);_exports.init=elementSelector=>{document.addEventListener("click",(event=>{const element=event.target.closest(elementSelector);if(element){const pendingPromise=new _pending.default("core_contentbank/displayunlisted");(0,_repository.setUserPreference)("core_contentbank_displayunlisted",!!element.checked).then((()=>(pendingPromise.resolve(),document.location.reload()))).catch(_notification.default.exception)}}))}})); //# sourceMappingURL=displayunlisted.min.js.map \ No newline at end of file diff --git a/contentbank/amd/build/displayunlisted.min.js.map b/contentbank/amd/build/displayunlisted.min.js.map index 6f867e581b7..c9638879776 100644 --- a/contentbank/amd/build/displayunlisted.min.js.map +++ b/contentbank/amd/build/displayunlisted.min.js.map @@ -1 +1 @@ -{"version":3,"file":"displayunlisted.min.js","sources":["../src/displayunlisted.js"],"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\n/**\n * Module to handle AJAX interactions with content bank upload files.\n *\n * @module core_contentbank/displayunlisted\n * @copyright 2023 Daniel Neis Araujo\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nimport {call as fetchMany} from 'core/ajax';\n\n/**\n * Initialize upload files to the content bank form as Modal form.\n *\n * @param {String} elementSelector\n */\nexport const update = (elementSelector) => {\n const element = document.querySelector(elementSelector);\n element.addEventListener('click', function() {\n const args = {\n userid: this.userId,\n preferences: [\n {\n type: 'core_contentbank_displayunlisted',\n value: !!element.checked,\n }\n ],\n };\n fetchMany([{\n methodname: 'core_user_update_user_preferences',\n args: args,\n }])[0].done(function() {\n document.location.reload();\n });\n });\n};\n"],"names":["elementSelector","element","document","querySelector","addEventListener","args","userid","this","userId","preferences","type","value","checked","methodname","done","location","reload"],"mappings":"2LA6BuBA,wBACbC,QAAUC,SAASC,cAAcH,iBACvCC,QAAQG,iBAAiB,SAAS,iBACxBC,KAAO,CACTC,OAAQC,KAAKC,OACbC,YAAa,CACT,CACIC,KAAM,mCACNC,QAASV,QAAQW,0BAInB,CAAC,CACPC,WAAY,oCACZR,KAAMA,QACN,GAAGS,MAAK,WACRZ,SAASa,SAASC"} \ No newline at end of file +{"version":3,"file":"displayunlisted.min.js","sources":["../src/displayunlisted.js"],"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\n/**\n * Module to handle toggling \"Display unlisted\" preference\n *\n * @module core_contentbank/displayunlisted\n * @copyright 2023 Daniel Neis Araujo\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Notification from 'core/notification';\nimport Pending from 'core/pending';\nimport {setUserPreference} from 'core_user/repository';\n\n/**\n * Initialize module, add event listeners\n *\n * @param {String} elementSelector\n */\nexport const init = elementSelector => {\n document.addEventListener('click', event => {\n const element = event.target.closest(elementSelector);\n if (element) {\n const pendingPromise = new Pending('core_contentbank/displayunlisted');\n\n setUserPreference('core_contentbank_displayunlisted', !!element.checked)\n .then(() => {\n pendingPromise.resolve();\n return document.location.reload();\n })\n .catch(Notification.exception);\n }\n });\n};\n"],"names":["elementSelector","document","addEventListener","event","element","target","closest","pendingPromise","Pending","checked","then","resolve","location","reload","catch","Notification","exception"],"mappings":";;;;;;;8LAgCoBA,kBAChBC,SAASC,iBAAiB,SAASC,cACzBC,QAAUD,MAAME,OAAOC,QAAQN,oBACjCI,QAAS,OACHG,eAAiB,IAAIC,iBAAQ,sEAEjB,qCAAsCJ,QAAQK,SAC3DC,MAAK,KACFH,eAAeI,UACRV,SAASW,SAASC,YAE5BC,MAAMC,sBAAaC"} \ No newline at end of file diff --git a/contentbank/amd/src/displayunlisted.js b/contentbank/amd/src/displayunlisted.js index 3f1eab1f69d..11876622ad2 100644 --- a/contentbank/amd/src/displayunlisted.js +++ b/contentbank/amd/src/displayunlisted.js @@ -14,36 +14,34 @@ // along with Moodle. If not, see . /** - * Module to handle AJAX interactions with content bank upload files. + * Module to handle toggling "Display unlisted" preference * * @module core_contentbank/displayunlisted * @copyright 2023 Daniel Neis Araujo * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -import {call as fetchMany} from 'core/ajax'; + +import Notification from 'core/notification'; +import Pending from 'core/pending'; +import {setUserPreference} from 'core_user/repository'; /** - * Initialize upload files to the content bank form as Modal form. + * Initialize module, add event listeners * * @param {String} elementSelector */ -export const update = (elementSelector) => { - const element = document.querySelector(elementSelector); - element.addEventListener('click', function() { - const args = { - userid: this.userId, - preferences: [ - { - type: 'core_contentbank_displayunlisted', - value: !!element.checked, - } - ], - }; - fetchMany([{ - methodname: 'core_user_update_user_preferences', - args: args, - }])[0].done(function() { - document.location.reload(); - }); +export const init = elementSelector => { + document.addEventListener('click', event => { + const element = event.target.closest(elementSelector); + if (element) { + const pendingPromise = new Pending('core_contentbank/displayunlisted'); + + setUserPreference('core_contentbank_displayunlisted', !!element.checked) + .then(() => { + pendingPromise.resolve(); + return document.location.reload(); + }) + .catch(Notification.exception); + } }); }; diff --git a/contentbank/index.php b/contentbank/index.php index 4a34c1d9950..0568e8fb956 100644 --- a/contentbank/index.php +++ b/contentbank/index.php @@ -79,25 +79,19 @@ foreach ($enabledcontenttypes as $contenttypename) { $toolbar = array (); if (has_capability('moodle/contentbank:viewunlistedcontent', $context)) { - $setdisplay = optional_param('displayunlisted', null, PARAM_INT); - if (is_null($setdisplay)) { - $display = get_user_preferences('core_contentbank_displayunlisted', 1); - } else { - set_user_preference('core_contentbank_displayunlisted', $setdisplay); - $display = $setdisplay; - } + $display = get_user_preferences('core_contentbank_displayunlisted', 1); $toolbar[] = [ 'name' => 'displayunlisted', 'id' => 'displayunlisted', 'checkbox' => true, - 'checked' => $display, + 'checked' => !empty($display), 'label' => get_string('displayunlisted', 'contentbank'), 'class' => 'displayunlisted m-2', 'action' => 'displayunlisted', ]; $PAGE->requires->js_call_amd( 'core_contentbank/displayunlisted', - 'update', + 'init', ['[data-action=displayunlisted]'] ); } diff --git a/contentbank/tests/behat/visibility.feature b/contentbank/tests/behat/visibility.feature index 6f1847d0be4..3fabd66a0e3 100644 --- a/contentbank/tests/behat/visibility.feature +++ b/contentbank/tests/behat/visibility.feature @@ -36,6 +36,23 @@ Feature: Make content public or unlisted And I click on "More" "button" And I should see "Make public" + Scenario: Unlisted content can be hidden by user preference + Given the following "contentbank content" exist: + | contextlevel | reference | contenttype | user | contentname | filepath | visibility | + | System | | contenttype_h5p | admin | filltheblanks.h5p | /h5p/tests/fixtures/filltheblanks.h5p | 1 | + | System | | contenttype_h5p | admin | find-the-words.h5p | /h5p/tests/fixtures/find-the-words.h5p | 2 | + And I am on the "Content bank" page logged in as "admin" + And I should see "filltheblanks.h5p" + And I should see "find-the-words.h5p (Unlisted)" + When I set the field "Show unlisted content" to "0" + And I wait until the page is ready + Then I should see "filltheblanks.h5p" + And I should not see "find-the-words.h5p (Unlisted)" + And I set the field "Show unlisted content" to "1" + And I wait until the page is ready + And I should see "filltheblanks.h5p" + And I should see "find-the-words.h5p (Unlisted)" + Scenario: Unlisted content cannot be seen by other users Given the following "users" exist: | username | firstname | lastname | email |