MDL-72930 navigation: Update to badges navigation.

This allows navigation nodes injected into the badges section
to be displayed as either a button or a dropdown url_select.
This commit is contained in:
abgreeve 2021-12-02 15:27:36 +08:00 committed by Adrian Greeve
parent f0ecd475d0
commit f1d22c9830
9 changed files with 101 additions and 2 deletions

View File

@ -17,8 +17,10 @@
namespace core_badges\output;
use renderable;
use templatable;
use renderer_base;
use moodle_page;
use navigation_node;
use templatable;
/**
* Abstract class for the badges tertiary navigation. The class initialises the page and type class variables.
@ -50,4 +52,52 @@ abstract class base_action_bar implements renderable, templatable {
* @return string
*/
abstract public function get_template(): string;
/**
* Gets additional third party navigation nodes for display.
*
* @param renderer_base $output The output
* @return array All that sweet third party navigation action.
*/
public function get_third_party_nav_action(renderer_base $output): array {
$badgenode = $this->page->settingsnav->find('coursebadges', navigation_node::TYPE_CONTAINER);
if (!$badgenode) {
return [];
}
$leftovernodes = [];
foreach ($badgenode->children as $key => $value) {
if (array_search($value->key, $this->expected_items()) === false) {
$leftovernodes[] = $value;
}
}
$result = \core\navigation\views\secondary::create_menu_element($leftovernodes);
if ($result == false) {
return [];
} else {
$data ['thirdpartybutton'] = true;
if (count($result) == 1) {
// Return a button.
$link = key($result);
$text = current($result);
$data['thirdpartynodes'] = ['link' => $link, 'text' => $text];
} else {
// Return a url_select.
$selectobject = new \url_select($result, $this->page->url, get_string('othernavigation', 'badges'));
$data['thirdpartynodes'] = $selectobject->export_for_template($output);
$data['thirdpartybutton'] = false;
}
}
return $data;
}
/**
* Expected navigation node keys for badges.
*
* @return array default badge navigation node keys.
*/
protected function expected_items(): array {
return ['coursebadges', 'newbadge'];
}
}

View File

@ -71,6 +71,8 @@ class manage_badge_action_bar extends base_action_bar {
foreach ($elements as $key => $element) {
$elements[$key] = $element->export_for_template($output);
}
$additional = $this->get_third_party_nav_action($output);
$elements += $additional ?: [];
return $elements;
}

View File

@ -53,6 +53,8 @@ class recipients_action_bar extends manage_badge_action_bar {
$button = new single_button($url, get_string('award', 'badges'), 'post', true);
$elements['awardbutton'] = $button->export_for_template($output);
}
$thirdpartynav = $this->get_third_party_nav_action($output);
$elements += $thirdpartynav ?: [];
return $elements;
}

View File

@ -96,6 +96,10 @@ class standard_action_bar extends base_action_bar {
$buttons[$key] = $button->export_for_template($output);
}
return ['buttons' => $buttons];
$data = ['buttons' => $buttons];
$additional = $this->get_third_party_nav_action($output);
$data += $additional ?: [];
return $data;
}
}

View File

@ -103,5 +103,6 @@
{{> core/single_button }}
</div>
{{/awardbutton}}
{{> core_badges/badge_more_nav }}
</div>
</div>

View File

@ -0,0 +1,37 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template core_badges/badge_more_nav
Example context (json):
{
"thirdpartybutton": true,
"thirdpartynodes": {
"link": "http://example.org/help",
"text": "Example text"
}
}
}}
{{#thirdpartybutton}}
{{#thirdpartynodes}}
<div>
<a href="{{link}}" class="btn btn-secondary">{{text}}</a>
</div>
{{/thirdpartynodes}}
{{/thirdpartybutton}}
{{^thirdpartybutton}}
{{#thirdpartynodes}}
{{> core/url_select}}
{{/thirdpartynodes}}
{{/thirdpartybutton}}

View File

@ -83,5 +83,6 @@
{{> core/url_select }}
</div>
{{/urlselect}}
{{> core_badges/badge_more_nav }}
</div>
</div>

View File

@ -59,5 +59,6 @@
{{> core/single_button }}
</div>
{{/buttons}}
{{> core_badges/badge_more_nav }}
</div>
</div>

View File

@ -456,6 +456,7 @@ $string['oauth2issuer'] = 'OAuth 2 services';
$string['openbadgesv1'] = 'Open Badges v1.0';
$string['openbadgesv2'] = 'Open Badges v2.0';
$string['openbadgesv2p1'] = 'Open Badges v2.1';
$string['othernavigation'] = 'Other navigation ...';
$string['potentialrecipients'] = 'Potential badge recipients';
$string['preferences'] = 'Badge preferences';
$string['privacy:metadata:backpack'] = 'A record of user\'s backpacks';