|
| 1 | +// This file is part of Moodle - http://moodle.org/ |
| 2 | +// |
| 3 | +// Moodle is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// Moodle is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +/** |
| 17 | + * Test tool for checking the connection and credentials of a given Etherpad Lite API-URL and API-key. |
| 18 | + * |
| 19 | + * @module mod_etherpadlite/test_tool |
| 20 | + * @copyright 2025 André Menrath <andre.menrath@uni-graz.at>, University of Graz |
| 21 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 22 | + */ |
| 23 | + |
| 24 | +import Modal from 'core/modal'; |
| 25 | +import Notification from 'core/notification'; |
| 26 | +import Templates from 'core/templates'; |
| 27 | +import {call as fetchMany} from 'core/ajax'; |
| 28 | +import {getString} from 'core/str'; |
| 29 | + |
| 30 | +const SELECTORS = { |
| 31 | + SETTING_ETHERPAD_URL: 'url', |
| 32 | + SETTING_ETHERPAD_APIKEY: 'apikey', |
| 33 | + TEST_TOOL_BUTTON: '[data-action="mod-etherpadlite-test-tool"]', |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Entrypoint of the JS. |
| 38 | + * |
| 39 | + * @method init |
| 40 | + */ |
| 41 | +export const init = () => { |
| 42 | + if (window.location.pathname === '/admin/settings.php') { |
| 43 | + registerListenerEvents(); |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * Register snippet related event listeners. |
| 49 | + * |
| 50 | + * @method registerListenerEvents |
| 51 | + */ |
| 52 | +const registerListenerEvents = () => { |
| 53 | + const testButton = document.querySelector(SELECTORS.TEST_TOOL_BUTTON); |
| 54 | + |
| 55 | + // If the button for the Connection Test Tool is not found, no listeners are registered. |
| 56 | + if (!testButton) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // Add event listener which will trigger the connection test with the given Etherpad Lite API credentials. |
| 61 | + testButton.addEventListener('click', (event) => { |
| 62 | + event.preventDefault(); |
| 63 | + testConnection().catch(Notification.exception); |
| 64 | + }); |
| 65 | + |
| 66 | + // Make the button clickable after the event listener is added. |
| 67 | + if (testButton.hasAttribute('disabled')) { |
| 68 | + testButton.removeAttribute('disabled'); |
| 69 | + } |
| 70 | + if (testButton.classList.contains('disabled')) { |
| 71 | + testButton.classList.remove('disabled'); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +/** |
| 76 | + * Execute the tests via webservice. |
| 77 | + * |
| 78 | + * @param {string} url |
| 79 | + * @param {string} apikey |
| 80 | + * |
| 81 | + * @returns string The HTML of the test. |
| 82 | + */ |
| 83 | +export const getTestResults = (url, apikey) => fetchMany([{ |
| 84 | + methodname: 'mod_etherpadlite_test_tool', |
| 85 | + args: { |
| 86 | + url: url, |
| 87 | + apikey: apikey, |
| 88 | + } |
| 89 | +}])[0]; |
| 90 | + |
| 91 | +/** |
| 92 | + * Get the value of a Moodle Admin setting on the current page. |
| 93 | + * |
| 94 | + * @param {string} setting |
| 95 | + * |
| 96 | + * @returns {string|integer} The settings Value |
| 97 | + */ |
| 98 | +const getAdminSettingValue = (setting) => { |
| 99 | + const settingElementId = 'admin-' + setting; |
| 100 | + const settingElement = document.getElementById(settingElementId); |
| 101 | + const input = settingElement.querySelector('input'); |
| 102 | + return input ? input.value : null; |
| 103 | +}; |
| 104 | + |
| 105 | +/** |
| 106 | + * Build the modal with the provided data. |
| 107 | + * |
| 108 | + * @method buildModal |
| 109 | + */ |
| 110 | +const testConnection = async() => { |
| 111 | + const url = getAdminSettingValue(SELECTORS.SETTING_ETHERPAD_URL); |
| 112 | + const apikey = getAdminSettingValue(SELECTORS.SETTING_ETHERPAD_APIKEY); |
| 113 | + |
| 114 | + const testResult = await getTestResults(url, apikey); |
| 115 | + |
| 116 | + const modal = await Modal.create({ |
| 117 | + title: getString('testmodaltitle', 'mod_etherpadlite'), |
| 118 | + body: Templates.render('mod_etherpadlite/test_tool_result', testResult), |
| 119 | + large: true, |
| 120 | + isVerticallyCentered: true, |
| 121 | + buttons: { |
| 122 | + 'cancel': getString('closebuttontitle', 'moodle'), |
| 123 | + }, |
| 124 | + }); |
| 125 | + modal.show(); |
| 126 | +}; |
0 commit comments