diff --git a/CHANGES.md b/CHANGES.md index 7236b279cf1..328e99a9971 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,10 @@ moodle-theme_boost_union Changes ------- +### Unreleased + +* 2024-03-01 - Improvement: implement new setting to show starred courses in a popover menu in navbar, solves #289. + ### v4.3-r8 * 2024-02-22 - Feature: Allow the admin to change the link behind the logo in the navbar, resolves #565. diff --git a/README.md b/README.md index 3fe4763be4c..4d4c6169d3f 100644 --- a/README.md +++ b/README.md @@ -321,6 +321,10 @@ With this setting, you can set an alternative link URL which will be used as lin With this setting, you can add a 'Set preferred language' setting to the language menu within the user menu. Understandably, this setting is only processed if the language menu is enabled at all. +###### Show starred courses in primary navigation bar + +With this setting, you can show a popover menu with links to starred courses next to the messages and notifications menus. + ##### Breadcrumbs ###### Display the category breadcrumbs in the course header @@ -774,5 +778,6 @@ Moodle an Hochschulen e.V. would like to thank these main contributors (in alpha * University of Graz, André Menrath: Code * University of Lübeck, Christian Wolters: Code, Peer Review, Ideating * Zurich University of Applied Sciences (ZHAW): Funding, Ideating +* Academic Moodle Cooperation (AMC): Code, Ideating Additionally, we thank all other contributors who contributed ideas, feedback and code snippets within the Github issues and pull requests as well as all contributors who contributed additional translations in AMOS, the Moodle translation tool. diff --git a/lang/en/theme_boost_union.php b/lang/en/theme_boost_union.php index 200640f7bd7..52d8759751a 100644 --- a/lang/en/theme_boost_union.php +++ b/lang/en/theme_boost_union.php @@ -398,6 +398,9 @@ $string['addpreferredlangsetting'] = 'Add preferred language link to language menu'; $string['addpreferredlangsetting_desc'] = 'With this setting, you can add a \'Set preferred language\' setting to the language menu within the user menu. Understandably, this setting is only processed if the setting Display language menu is enabled, and if at least a second language pack is installed and offered for selection.'; $string['setpreferredlanglink'] = 'Set preferred language'; +// ... ... Settings: show a popover menu with starred courses next to the messages and notifications menus. +$string['shownavbarstarredcoursessetting'] = 'Show starred courses in primary navigation bar'; +$string['shownavbarstarredcoursessetting_desc'] = 'Show a popover menu with links to starred courses next to the messages and notifications menus.'; // ... Section: Breadcrumbs. $string['breadcrumbsheading'] = 'Breadcrumbs'; // ... ... Setting: Course category breadcrumb. diff --git a/lib.php b/lib.php index 33c2f02a559..b05b05af99b 100644 --- a/lib.php +++ b/lib.php @@ -602,3 +602,13 @@ function theme_boost_union_user_preferences(): array { } return $preferences; } + +/** + * Returns the html for the starred courses popover menu. + * + * @return string + */ +function theme_boost_union_render_navbar_output() { + require_once(__DIR__ . '/locallib.php'); + return theme_boost_union_get_favourites_popover_menu(); +} diff --git a/locallib.php b/locallib.php index 66516088d3e..e03539bbf28 100644 --- a/locallib.php +++ b/locallib.php @@ -1867,3 +1867,50 @@ function theme_boost_union_yesno_to_boolstring($var) { return 'false'; } } + +/** + * Function that fetches all favorite courses, and renders them as a popover menu. + * + * @return string HTML to display the main header. + */ +function theme_boost_union_get_favourites_popover_menu() { + global $USER, $DB, $OUTPUT; + // Menu is relevant only for logged in users. + if (isloggedin()) { + $settings = get_config('theme_boost_union'); + if (!isset($settings->shownavbarstarredcourses) || $settings->shownavbarstarredcourses == 'no') { + return ''; + } + // Get all favourite courses. + $ufservice = \core_favourites\service_factory::get_service_for_user_context(\context_user::instance($USER->id)); + $favourites = $ufservice->find_favourites_by_type('core_course', 'courses'); + if (!$favourites) { + return ''; + } + $favouritecourseids = array_map( + function($favourite) { + return $favourite->itemid; + }, $favourites); + $coursefields = 'id, shortname, fullname, visible'; + $courses = $DB->get_records_list('course', 'id', $favouritecourseids, 'visible DESC,sortorder ASC', $coursefields); + + // Sort courses by visibility and name. + usort($courses, function($a, $b) { + if ($a->visible != $b->visible) { + return $a->visible == 0 ? 1 : -1; + } + return strcasecmp(trim($a->fullname), trim($b->fullname)); + }); + $menu = []; + foreach ($courses as $course) { + $menu[] = [ + 'url' => new \moodle_url('/course/view.php', ['id' => $course->id]), + 'fullname' => $course->fullname, + 'visible' => $course->visible == 1, + ]; + } + $html = $OUTPUT->render_from_template('theme_boost_union/favourites-popover', ['favourites' => $menu]); + return $html; + } + return ''; +} diff --git a/scss/boost_union/post.scss b/scss/boost_union/post.scss index f1f37eb5bdc..c19b3ec1ca9 100644 --- a/scss/boost_union/post.scss +++ b/scss/boost_union/post.scss @@ -472,6 +472,43 @@ body.hascourseindexcplsol.editing { padding-top: 0.5rem; } +/*--------------------------------------- + * Setting: show a popover menu with starred courses next to the messages and notifications menus. + --------------------------------------*/ +.navbar #nav-favourites-popover-container { + .popover-region-container { + height: auto; + /* Limit the width to some reasonable size to avoid that the popover menu is too wide. */ + max-width: 78vw; + border-radius: 0.5rem; + right: -5vw; + width: auto; + @include media-breakpoint-down(lg) { + max-width: 73vw; + } + @include media-breakpoint-down(md) { + max-width: 68vw; + } + @include media-breakpoint-down(xs) { + right: -35vw; + top: 60px; + } + /* Hide overflowing course names. */ + .dropdown-item { + overflow-x: hidden; + text-overflow: ellipsis; + } + } + .popover-region-header-container { + display: none; + } + .popover-region-content-container { + height: auto; + min-width: 10rem; + } +} + + /*--------------------------------------- * Setting: Back to top button. --------------------------------------*/ diff --git a/settings.php b/settings.php index 787192d813d..cecfee49e1d 100644 --- a/settings.php +++ b/settings.php @@ -1259,6 +1259,14 @@ $setting = new admin_setting_configselect($name, $title, $description, THEME_BOOST_UNION_SETTING_SELECT_NO, $yesnooption); $tab->add($setting); + // Setting: show a popover menu with starred courses next to the messages and notifications menus. + $name = 'theme_boost_union/shownavbarstarredcourses'; + $title = get_string('shownavbarstarredcoursessetting', 'theme_boost_union', null, true); + $description = get_string('shownavbarstarredcoursessetting_desc', 'theme_boost_union', null, true); + $setting = new admin_setting_configselect($name, $title, $description, THEME_BOOST_UNION_SETTING_SELECT_YES, $yesnooption); + $setting->set_updatedcallback('theme_reset_all_caches'); + $tab->add($setting); + // Create breadcrumbs heading. $name = 'theme_boost_union/breadcrumbsheading'; $title = get_string('breadcrumbsheading', 'theme_boost_union', null, true); diff --git a/templates/favourites-popover.mustache b/templates/favourites-popover.mustache new file mode 100644 index 00000000000..929f6a39e61 --- /dev/null +++ b/templates/favourites-popover.mustache @@ -0,0 +1,27 @@ +{{< core/popover_region }} + {{$classes}}popover-region-favourites{{/classes}} + {{$attributes}}id="nav-favourites-popover-container" {{/attributes}} + + {{$togglelabel}}{{#str}} favourites {{/str}}{{/togglelabel}} + {{$togglecontent}} + + {{/togglecontent}} + + {{$containerlabel}}{{#str}} favourites {{/str}}{{/containerlabel}} + + + {{$content}} +