From 10d7ae7a2473543b8b57da9c729e5669b320717f Mon Sep 17 00:00:00 2001 From: Nikolai Jahreis Date: Mon, 13 May 2024 12:32:34 +0000 Subject: [PATCH] Add docs type smartmenu item, resolves: #657 --- classes/smartmenu_item.php | 47 +++++++++++++++++-- lang/en/theme_boost_union.php | 3 +- ...artmenusettings_menuitems_docslink.feature | 23 +++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 tests/behat/theme_boost_union_smartmenusettings_menuitems_docslink.feature diff --git a/classes/smartmenu_item.php b/classes/smartmenu_item.php index 394e13e4c49..2d46010f3ef 100644 --- a/classes/smartmenu_item.php +++ b/classes/smartmenu_item.php @@ -64,6 +64,12 @@ class smartmenu_item { */ const TYPEDYNAMIC = 2; + /** + * Represents the type of a dynamic element. + * @var int + */ + const TYPEDOCS = 3; + /** * Represents the completion status of an item where the status is 'enrolled'. * @var int @@ -771,6 +777,23 @@ protected function generate_dynamic_item() { return $items; } + /** + * Generate a node data for doc link item. Use get_docs_url to get the link and generate the data. + * + * @return array + */ + protected function generate_docs_item() { + global $PAGE; + $path = page_get_doc_link_path($PAGE); + $docurl = get_docs_url($path); + return $this->generate_node_data( + $this->item->title, + $docurl, + null, + $this->item->tooltip, + ); + } + /** * Given some text and an ideal length, this function truncates the text based on words count. * @@ -1052,7 +1075,12 @@ public function build() { // Add classes for item title placement on card. $class[] = $this->get_textposition_class(); // Menu item class. - $types = [self::TYPESTATIC => 'static', self::TYPEDYNAMIC => 'dynamic', self::TYPEHEADING => 'heading']; + $types = [ + self::TYPESTATIC => 'static', + self::TYPEDYNAMIC => 'dynamic', + self::TYPEHEADING => 'heading', + self::TYPEDOCS => 'docs', + ]; $class[] = 'menu-item-'.($types[$this->item->type] ?? ''); // Add classes to item data. $this->item->classes = $class; @@ -1068,11 +1096,20 @@ public function build() { $static = $this->generate_static_item(); $result = [$static]; // Return the result as recursive array for merge with dynamic items. $type = 'static'; + $cacheable = true; break; case self::TYPEDYNAMIC: $result = $this->generate_dynamic_item(); $type = 'dynamic'; + $cacheable = true; + break; + + case self::TYPEDOCS: + $docs = $this->generate_docs_item(); + $result = [$docs]; // Return the result as recursive array useful to merge with dynamic items. + $type = 'docs'; + $cacheable = false; break; case self::TYPEHEADING: @@ -1080,11 +1117,14 @@ public function build() { $heading = $this->generate_heading(); $result = [$heading]; // Return the result as recursive array useful to merge with dynamic items. $type = 'heading'; + $cacheable = true; endswitch; - // Save the items cache. - $this->cache->set($cachekey, $result); + // If cachable save the items cache. + if ($cacheable) { + $this->cache->set($cachekey, $result); + } return $result; } @@ -1321,6 +1361,7 @@ public static function load_custom_field_config(&$mform) { public static function get_types(?int $type = null) { $types = [ self::TYPESTATIC => get_string('smartmenusmenuitemtypestatic', 'theme_boost_union'), + self::TYPEDOCS => get_string('smartmenusmenuitemtypedocs', 'theme_boost_union'), self::TYPEHEADING => get_string('smartmenusmenuitemtypeheading', 'theme_boost_union'), self::TYPEDYNAMIC => get_string('smartmenusmenuitemtypedynamiccourses', 'theme_boost_union'), ]; diff --git a/lang/en/theme_boost_union.php b/lang/en/theme_boost_union.php index 36c8caeb48b..b3b525bc2d9 100644 --- a/lang/en/theme_boost_union.php +++ b/lang/en/theme_boost_union.php @@ -1197,10 +1197,11 @@ $string['smartmenusmenuitemtooltip'] = 'Tooltip'; $string['smartmenusmenuitemtooltip_help'] = 'The tooltip which will be displayed when the user hovers over the menu item.'; $string['smartmenusmenuitemtype'] = 'Menu item type'; -$string['smartmenusmenuitemtype_help'] = '

Select the type of menu item you want to create, choosing between static, heading and dynamic courses.

'; +$string['smartmenusmenuitemtype_help'] = '

Select the type of menu item you want to create, choosing between static, heading, moodle documentation and dynamic courses.

'; $string['smartmenusmenuitemtypedynamiccourses'] = 'Dynamic courses'; $string['smartmenusmenuitemtypeheading'] = 'Heading'; $string['smartmenusmenuitemtypestatic'] = 'Static'; +$string['smartmenusmenuitemtypedocs'] = 'Moodle Documentation'; $string['smartmenusmenuitemurl'] = 'Menu item URL'; $string['smartmenusmenuitemurl_help'] = 'The static URL for the menu item. This is the link that will be followed when the menu item is clicked.'; $string['smartmenusmenulocation'] = 'Menu location(s)'; diff --git a/tests/behat/theme_boost_union_smartmenusettings_menuitems_docslink.feature b/tests/behat/theme_boost_union_smartmenusettings_menuitems_docslink.feature new file mode 100644 index 00000000000..a32d65ae713 --- /dev/null +++ b/tests/behat/theme_boost_union_smartmenusettings_menuitems_docslink.feature @@ -0,0 +1,23 @@ +@theme @theme_boost_union @theme_boost_union_smartmenusettings @theme_boost_union_smartmenusettings_menuitems @theme_boost_union_smartmenusettings_menuitems_docslink +Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, using a dynamic documentationn link item + In order to use the features + As admin + I need to be able to configure the theme Boost Union plugin + + Background: + Given I log in as "admin" + And I create smart menu with the following fields to these values: + | Title | Quick links | + | Menu location(s) | Main, Menu, User, Bottom | + + @javascript + Scenario: Smartmenus: Menu items: Documentation Link - Add a smart menu documentation link item in smart menu + When I log in as "admin" + And I set "Quick links" smart menu items with the following fields to these values: + | Title | Documentation | + | Menu item type | Moodle Documentation | + And I should see smart menu "Quick links" item "Documentation" in location "Main, Menu, User, Bottom" + And the "href" attribute of "//div[@class='primary-navigation']//a[contains(normalize-space(.), 'Documentation')]" "xpath_element" should contain "/en/theme/boost_union/smartmenus/items" + And I go to the courses management page + And I should see smart menu "Quick links" item "Documentation" in location "Main, Menu, User, Bottom" + And the "href" attribute of "//div[@class='primary-navigation']//a[contains(normalize-space(.), 'Documentation')]" "xpath_element" should contain "en/course/management"