diff --git a/classes/hook_callbacks.php b/classes/hook_callbacks.php index c63eb47..483f6a3 100644 --- a/classes/hook_callbacks.php +++ b/classes/hook_callbacks.php @@ -20,7 +20,9 @@ require_once($CFG->dirroot . '/local/envbar/lib.php'); +use context_system; use core\hook\output\before_standard_top_of_body_html_generation; +use core_user\hook\extend_user_menu; use local_envbar\local\envbarlib; /** @@ -43,6 +45,39 @@ public static function before_standard_top_of_body_html_generation(before_standa $hook->add_html(envbarlib::get_inject_code()); } + /** + * This is the hook enables the plugin to add one or more menu item. + * + * @param extend_user_menu $hook + */ + public static function extend_user_menu(extend_user_menu $hook): void { + global $CFG; + + $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; + } + + // If the prodwwwroot is not set, only show the bar to admin users. + if (empty($prodwwwroot)) { + if (!has_capability('moodle/site:config', context_system::instance())) { + return; + } + } + + // Get items to add. + $navitems = envbarlib::add_menuuser(); + foreach ($navitems as $item) { + $hook->add_navitem($item); + } + } + /** * Listener for the after_config hook. * diff --git a/classes/local/envbarlib.php b/classes/local/envbarlib.php index ea11922..aee6215 100644 --- a/classes/local/envbarlib.php +++ b/classes/local/envbarlib.php @@ -684,4 +684,39 @@ public static function config() { } } + /** + * Add items to the menu navigation. + * + * @return array New menu items. + */ + public static function add_menuuser(): array { + global $PAGE; + $userfirstmenu = new stdClass(); + $userfirstmenu->itemtype = 'divider'; + $here = (new moodle_url('/'))->out(); + $prodwwwroot = self::getprodwwwroot(); + // Get prod Environment. + $prodenv = new stdClass(); + $prodenv->matchpattern = $prodwwwroot; + $prodenv->showtext = get_string('prod', 'local_envbar'); + $envsprod[] = $prodenv; + // Attached the list of Environments to the prod one. + $envslistfinal = array_merge($envsprod, self::get_records()); + $navitem[] = $userfirstmenu; + foreach ($envslistfinal as $env) { + $usermenu = new stdClass(); + $usermenu->itemtype = 'link'; + $usermenu->title = $env->showtext; + $pathurl = (new moodle_url( $PAGE->__get('url')))->get_path(); + $currenturl = $env->matchpattern.$pathurl; + $usermenu->url = new moodle_url($currenturl); + // Which env matches? + if (self::is_match($here, $env->matchpattern)) { + $usermenu->pix = 'e/tick'; + } + $navitem[] = $usermenu; + } + return $navitem; + } + } diff --git a/db/hooks.php b/db/hooks.php index 4c8cac0..41d8edd 100644 --- a/db/hooks.php +++ b/db/hooks.php @@ -35,4 +35,9 @@ 'hook' => \core\hook\after_config::class, 'callback' => [\local_envbar\hook_callbacks::class, 'after_config'], ], + [ + 'hook' => core_user\hook\extend_user_menu::class, + 'callback' => '\local_envbar\hook_callbacks::extend_user_menu', + 'priority' => 0, + ], ]; 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.
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.
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 = << - - $show - - -EOD; - $html .= $link; - } - } - - if (!$html) { - return ''; - } - - if (isset($config->dividerselector)) { - $divider = $config->dividerselector; - } else { - $divider = 'filler'; - } - $html = '
  •  
  • ' . $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 = <<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), ''));