Skip to content

Commit 4c0832b

Browse files
abiaslucaboesch
authored andcommitted
Issue #289 - implement popover menu in navbar with starred courses
1 parent ab6eeea commit 4c0832b

File tree

10 files changed

+193
-1
lines changed

10 files changed

+193
-1
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ moodle-theme_boost_union
44
Changes
55
-------
66

7+
### Unreleased
8+
9+
* 2024-03-01 - Improvement: implement new setting to show starred courses in a popover menu in navbar, solves #289.
10+
711
### v4.3-r8
812

913
* 2024-02-22 - Feature: Allow the admin to change the link behind the logo in the navbar, resolves #565.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ With this setting, you can set an alternative link URL which will be used as lin
321321

322322
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.
323323

324+
###### Show starred courses in primary navigation bar
325+
326+
With this setting, you can show a popover menu with links to starred courses next to the messages and notifications menus.
327+
324328
##### Breadcrumbs
325329

326330
###### 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
774778
* University of Graz, André Menrath: Code
775779
* University of Lübeck, Christian Wolters: Code, Peer Review, Ideating
776780
* Zurich University of Applied Sciences (ZHAW): Funding, Ideating
781+
* Academic Moodle Cooperation (AMC): Code, Ideating
777782

778783
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.

lang/en/theme_boost_union.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@
398398
$string['addpreferredlangsetting'] = 'Add preferred language link to language menu';
399399
$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 <a href="{$a->url1}">Display language menu</a> is enabled, and if at least <a href="{$a->url2}">a second language pack is installed</a> and <a href="{$a->url3}">offered for selection</a>.';
400400
$string['setpreferredlanglink'] = 'Set preferred language';
401+
// ... ... Settings: show a popover menu with starred courses next to the messages and notifications menus.
402+
$string['shownavbarstarredcoursessetting'] = 'Show starred courses in primary navigation bar';
403+
$string['shownavbarstarredcoursessetting_desc'] = 'Show a popover menu with links to starred courses next to the messages and notifications menus.';
401404
// ... Section: Breadcrumbs.
402405
$string['breadcrumbsheading'] = 'Breadcrumbs';
403406
// ... ... Setting: Course category breadcrumb.

lib.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,3 +602,13 @@ function theme_boost_union_user_preferences(): array {
602602
}
603603
return $preferences;
604604
}
605+
606+
/**
607+
* Returns the html for the starred courses popover menu.
608+
*
609+
* @return string
610+
*/
611+
function theme_boost_union_render_navbar_output() {
612+
require_once(__DIR__ . '/locallib.php');
613+
return theme_boost_union_get_favourites_popover_menu();
614+
}

locallib.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,3 +1867,50 @@ function theme_boost_union_yesno_to_boolstring($var) {
18671867
return 'false';
18681868
}
18691869
}
1870+
1871+
/**
1872+
* Function that fetches all favorite courses, and renders them as a popover menu.
1873+
*
1874+
* @return string HTML to display the main header.
1875+
*/
1876+
function theme_boost_union_get_favourites_popover_menu() {
1877+
global $USER, $DB, $OUTPUT;
1878+
// Menu is relevant only for logged in users.
1879+
if (isloggedin()) {
1880+
$settings = get_config('theme_boost_union');
1881+
if (!isset($settings->shownavbarstarredcourses) || $settings->shownavbarstarredcourses == 'no') {
1882+
return '';
1883+
}
1884+
// Get all favourite courses.
1885+
$ufservice = \core_favourites\service_factory::get_service_for_user_context(\context_user::instance($USER->id));
1886+
$favourites = $ufservice->find_favourites_by_type('core_course', 'courses');
1887+
if (!$favourites) {
1888+
return '';
1889+
}
1890+
$favouritecourseids = array_map(
1891+
function($favourite) {
1892+
return $favourite->itemid;
1893+
}, $favourites);
1894+
$coursefields = 'id, shortname, fullname, visible';
1895+
$courses = $DB->get_records_list('course', 'id', $favouritecourseids, 'visible DESC,sortorder ASC', $coursefields);
1896+
1897+
// Sort courses by visibility and name.
1898+
usort($courses, function($a, $b) {
1899+
if ($a->visible != $b->visible) {
1900+
return $a->visible == 0 ? 1 : -1;
1901+
}
1902+
return strcasecmp(trim($a->fullname), trim($b->fullname));
1903+
});
1904+
$menu = [];
1905+
foreach ($courses as $course) {
1906+
$menu[] = [
1907+
'url' => new \moodle_url('/course/view.php', ['id' => $course->id]),
1908+
'fullname' => $course->fullname,
1909+
'visible' => $course->visible == 1,
1910+
];
1911+
}
1912+
$html = $OUTPUT->render_from_template('theme_boost_union/favourites-popover', ['favourites' => $menu]);
1913+
return $html;
1914+
}
1915+
return '';
1916+
}

scss/boost_union/post.scss

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,43 @@ body.hascourseindexcplsol.editing {
472472
padding-top: 0.5rem;
473473
}
474474

475+
/*---------------------------------------
476+
* Setting: show a popover menu with starred courses next to the messages and notifications menus.
477+
--------------------------------------*/
478+
.navbar #nav-favourites-popover-container {
479+
.popover-region-container {
480+
height: auto;
481+
/* Limit the width to some reasonable size to avoid that the popover menu is too wide. */
482+
max-width: 78vw;
483+
border-radius: 0.5rem;
484+
right: -5vw;
485+
width: auto;
486+
@include media-breakpoint-down(lg) {
487+
max-width: 73vw;
488+
}
489+
@include media-breakpoint-down(md) {
490+
max-width: 68vw;
491+
}
492+
@include media-breakpoint-down(xs) {
493+
right: -35vw;
494+
top: 60px;
495+
}
496+
/* Hide overflowing course names. */
497+
.dropdown-item {
498+
overflow-x: hidden;
499+
text-overflow: ellipsis;
500+
}
501+
}
502+
.popover-region-header-container {
503+
display: none;
504+
}
505+
.popover-region-content-container {
506+
height: auto;
507+
min-width: 10rem;
508+
}
509+
}
510+
511+
475512
/*---------------------------------------
476513
* Setting: Back to top button.
477514
--------------------------------------*/

settings.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,14 @@
12591259
$setting = new admin_setting_configselect($name, $title, $description, THEME_BOOST_UNION_SETTING_SELECT_NO, $yesnooption);
12601260
$tab->add($setting);
12611261

1262+
// Setting: show a popover menu with starred courses next to the messages and notifications menus.
1263+
$name = 'theme_boost_union/shownavbarstarredcourses';
1264+
$title = get_string('shownavbarstarredcoursessetting', 'theme_boost_union', null, true);
1265+
$description = get_string('shownavbarstarredcoursessetting_desc', 'theme_boost_union', null, true);
1266+
$setting = new admin_setting_configselect($name, $title, $description, THEME_BOOST_UNION_SETTING_SELECT_YES, $yesnooption);
1267+
$setting->set_updatedcallback('theme_reset_all_caches');
1268+
$tab->add($setting);
1269+
12621270
// Create breadcrumbs heading.
12631271
$name = 'theme_boost_union/breadcrumbsheading';
12641272
$title = get_string('breadcrumbsheading', 'theme_boost_union', null, true);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{{< core/popover_region }}
2+
{{$classes}}popover-region-favourites{{/classes}}
3+
{{$attributes}}id="nav-favourites-popover-container" {{/attributes}}
4+
5+
{{$togglelabel}}{{#str}} favourites {{/str}}{{/togglelabel}}
6+
{{$togglecontent}}
7+
<i class="fa fa-star icon" title="{{#str}}favourites{{/str}}"></i>
8+
{{/togglecontent}}
9+
10+
{{$containerlabel}}{{#str}} favourites {{/str}}{{/containerlabel}}
11+
12+
13+
{{$content}}
14+
<div class="">
15+
{{#favourites}}
16+
<a class="dropdown-item {{^visible}}dimmed{{/visible}}" href="{{{url}}}" title="{{{fullname}}}">{{{fullname}}}</a>
17+
{{/favourites}}
18+
</div>
19+
{{/content}}
20+
{{/ core/popover_region }}
21+
22+
{{#js}}
23+
require(['jquery', 'core/popover_region_controller'], function($, Controller) {
24+
var container = $('#nav-favourites-popover-container');
25+
var controller = new Controller(container);
26+
});
27+
{{/js}}

tests/behat/theme_boost_union_feelsettings_navigation.feature

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,63 @@ Feature: Configuring the theme_boost_union plugin for the "Navigation" tab on th
7777
And I click on "User menu" "button" in the ".usermenu" "css_element"
7878
And I click on "Language" "link" in the ".usermenu" "css_element"
7979
Then I <shouldornot> see "Set preferred language" in the ".usermenu .carousel-item.submenu" "css_element"
80+
Examples:
81+
| setting | shouldornot |
82+
| yes | should |
83+
| no | should not |
8084

85+
@javascript
86+
Scenario Outline: Check whether a popover menu with starred courses is displayed in the navbar
87+
Given the following config values are set as admin:
88+
| config | value | plugin |
89+
| shownavbarstarredcourses | <setting> | theme_boost_union |
90+
When I log in as "admin"
91+
And I navigate to "Development > Purge caches" in site administration
92+
And I press "Purge all caches"
93+
Then I should see "All caches were purged"
94+
Then I log in as "student1"
95+
And I follow "My courses"
96+
And I click on ".coursemenubtn" "css_element" in the "section.block_myoverview.block div[data-region=course-content] .menu" "css_element"
97+
And I click on "Star this course" "link" in the "section.block_myoverview.block div[data-region=course-content] .menu" "css_element"
98+
When I reload the page
99+
Then "nav.navbar #usernavigation .popover-region-favourites" "css_element" <shouldornot> be visible
81100
Examples:
82101
| setting | shouldornot |
83102
| yes | should |
84103
| no | should not |
85104

105+
@javascript
106+
Scenario: Check whether the correct courses are displayed in the "Starred courses" popover menu
107+
Given the following config values are set as admin:
108+
| config | value | plugin |
109+
| shownavbarstarredcourses | yes | theme_boost_union |
110+
And the following "courses" exist:
111+
| fullname | shortname |
112+
| Course 2 | C2 |
113+
| Course 3 | C3 |
114+
| Course 4 | C4 |
115+
And the following "course enrolments" exist:
116+
| user | course | role |
117+
| student1 | C2 | student |
118+
| student1 | C3 | student |
119+
| student1 | C4 | student |
120+
When I log in as "admin"
121+
And I navigate to "Development > Purge caches" in site administration
122+
And I press "Purge all caches"
123+
Then I should see "All caches were purged"
124+
Then I log in as "student1"
125+
And I follow "My courses"
126+
And I click on ".coursemenubtn" "css_element" in the "section.block_myoverview.block div[data-region=course-content]:nth-child(1) .menu" "css_element"
127+
And I click on "Star this course" "link" in the "section.block_myoverview.block div[data-region=course-content]:nth-child(1) .menu" "css_element"
128+
And I click on ".coursemenubtn" "css_element" in the "section.block_myoverview.block div[data-region=course-content]:nth-child(2) .menu" "css_element"
129+
And I click on "Star this course" "link" in the "section.block_myoverview.block div[data-region=course-content]:nth-child(2) .menu" "css_element"
130+
When I reload the page
131+
Then I click on "nav.navbar #usernavigation .popover-region-favourites .nav-link" "css_element"
132+
And I should see "Course 1" in the ".popover-region-favourites .popover-region-content-container" "css_element"
133+
And I should see "Course 2" in the ".popover-region-favourites .popover-region-content-container" "css_element"
134+
And I should not see "Course 3" in the ".popover-region-favourites .popover-region-content-container" "css_element"
135+
And I should not see "Course 4" in the ".popover-region-favourites .popover-region-content-container" "css_element"
136+
86137
Scenario Outline: Setting: Course category breadcrumbs
87138
Given the following "categories" exist:
88139
| name | category | idnumber | category |

version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
defined('MOODLE_INTERNAL') || die();
2626

2727
$plugin->component = 'theme_boost_union';
28-
$plugin->version = 2023102025;
28+
$plugin->version = 2023102026;
2929
$plugin->release = 'v4.3-r8';
3030
$plugin->requires = 2023100900;
3131
$plugin->supported = [403, 403];

0 commit comments

Comments
 (0)