From e3c637ddce33ee53d9c4e3fa4ec1d376355fbb5a Mon Sep 17 00:00:00 2001
From: guillaumebarat <guillaumebarat@catalyst-au.net>
Date: Fri, 6 Dec 2024 12:30:41 +1000
Subject: [PATCH] Remove previous unused settings, add enable menu control

---
 classes/hook_callbacks.php |  7 +++-
 lang/en/local_envbar.php   |  4 --
 renderer.php               | 84 --------------------------------------
 settings.php               | 12 ------
 4 files changed, 6 insertions(+), 101 deletions(-)

diff --git a/classes/hook_callbacks.php b/classes/hook_callbacks.php
index c332f82..483f6a3 100644
--- a/classes/hook_callbacks.php
+++ b/classes/hook_callbacks.php
@@ -52,8 +52,13 @@ public static function before_standard_top_of_body_html_generation(before_standa
      */
     public static function extend_user_menu(extend_user_menu $hook): void {
         global $CFG;
-        $prodwwwroot = envbarlib::getprodwwwroot();
 
+        $config = get_config('local_envbar');
+        if (empty($config->enablemenu)) {
+            return;
+        }
+
+        $prodwwwroot = envbarlib::getprodwwwroot();
         // Do not display on the production environment!
         if ($prodwwwroot === $CFG->wwwroot) {
             return;
diff --git a/lang/en/local_envbar.php b/lang/en/local_envbar.php
index 57c7217..424c6df 100644
--- a/lang/en/local_envbar.php
+++ b/lang/en/local_envbar.php
@@ -36,8 +36,6 @@
 $string['debuggingon'] = 'Debugging On';
 $string['debugtogglelinkoff'] = 'Turn Off';
 $string['debugtogglelinkon'] = 'Turn On';
-$string['dividerselector'] = 'Divider selector';
-$string['dividerselector_desc'] = 'This is a css class that is used for the menu divider element. If your theme uses different html then you may need to adjust this selector.<br />This setting is only used if the environment swapper menu is enabled.';
 $string['emailheading'] = 'Email prefix';
 $string['enableemailprefix'] = 'Enable email subject prefixing';
 $string['enableemailprefix_desc'] = 'This setting controls if email subject fields will be prefixed with the first 4 letters of the environment\'s name or not.';
@@ -64,8 +62,6 @@
 $string['menuheading'] = 'Environment swapper menu';
 $string['menulastrefresh'] = 'Last refresh';
 $string['menupresentation'] = 'Presentation';
-$string['menuselector'] = 'Menu selector';
-$string['menuselector_desc'] = 'This is a css or xpath selector to find the menu ul element for injecting the env swapper menu. If your theme uses different html then you may need to adjust this selector.<br />This setting is only used if the environment swapper menu is enabled.';
 $string['missing_required_parameter'] = 'A required parameter was missing. Required params are wwwroot and lastrefresh.';
 $string['nextrefreshin'] = 'Reset in {$a}';
 $string['notconfigured'] = 'UNKNOWN';
diff --git a/renderer.php b/renderer.php
index 517e7d9..0a60db9 100644
--- a/renderer.php
+++ b/renderer.php
@@ -197,7 +197,6 @@ public function render_envbar($match, $fixed = true, $envs = array()) {
 
         if ($fixed) {
             $js .= local_envbar_favicon_js($match);
-            $js .= local_envbar_user_menu($envs, $match);
             $js .= local_envbar_title($match);
         }
 
@@ -384,86 +383,3 @@ function local_envbar_favicon_js($match) {
 
     return $js;
 }
-
-/**
- * Gets some JS which inserts env jump links into the user menu
- *
- * @param array $envs
- * @return string A chunk of JS
- */
-function local_envbar_user_menu($envs) {
-
-    global $CFG, $PAGE;
-
-    $config = get_config('local_envbar');
-
-    if (empty($config->enablemenu)) {
-        return '';
-    }
-
-    if (isset($config->menuselector)) {
-        $menuselector = $config->menuselector;
-    } else {
-        $menuselector = '.usermenu .menu';
-    }
-
-    if (empty($menuselector)) {
-        return ''; // Not using user menu, nothing to do.
-    }
-
-    $html = '';
-
-    if ($PAGE->has_set_url()) {
-        $url = $PAGE->url->out();
-
-        foreach ($envs as $env) {
-            $jump = $url;
-            $jump = str_replace($CFG->wwwroot, $env->matchpattern, $jump);
-            if ($jump == $url) {
-                continue;
-            }
-            $jump = s($jump);
-            $show = s($env->showtext);
-            $link = <<<EOD
-<li role="presentation">
-  <a class="icon menu-action no-envbar-highlight" role="menuitem" href="{$jump}">
-    <span class="menu-action-text">$show</span>
-  </a>
-</li>
-EOD;
-            $html .= $link;
-        }
-    }
-
-    if (!$html) {
-        return '';
-    }
-
-    if (isset($config->dividerselector)) {
-        $divider = $config->dividerselector;
-    } else {
-        $divider = 'filler';
-    }
-    $html = '<li role="presentation"><span class="'. $divider .'">&nbsp;</span></li>' . $html;
-
-    $html = str_replace("\n", '', $html);
-    $html = str_replace("\"", "\\\"", $html);
-
-    $url = (new moodle_url('/admin/settings.php?section=local_envbar_presentation'))->out();
-    $isadmin = (is_siteadmin() ) ? '1' : '0';
-
-    $js = <<<EOD
-
-    var menu = document.querySelector('$menuselector');
-    var html = "$html";
-    if (menu) {
-        menu.insertAdjacentHTML('beforeend', html);
-    } else {
-        $isadmin && console.error(
-            "local_envbar: Menu selector is misconfigured '$menuselector' \\n Please configure it here: $url");
-    }
-EOD;
-    return $js;
-
-}
-
diff --git a/settings.php b/settings.php
index aa29f4d..c029ac7 100644
--- a/settings.php
+++ b/settings.php
@@ -85,18 +85,6 @@
             get_string('enablemenu_desc', 'local_envbar', null, true),
             true));
 
-    $presentation->add(new admin_setting_configtext('local_envbar/menuselector',
-            get_string('menuselector', 'local_envbar', null, true),
-            get_string('menuselector_desc', 'local_envbar', null, true),
-            '.usermenu .menu',
-            PARAM_RAW));
-
-    $presentation->add(new admin_setting_configtext('local_envbar/dividerselector',
-            get_string('dividerselector', 'local_envbar', null, true),
-            get_string('dividerselector_desc', 'local_envbar', null, true),
-            'filler',
-            PARAM_RAW));
-
     $presentation->add(new admin_setting_heading('local_envbar/linksheading',
             get_string('linksheading', 'local_envbar', null, true),
             ''));