From 9df03b1940de056a9d09d915068c890cab546cd8 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Fri, 7 Sep 2018 11:55:06 +0800 Subject: [PATCH] MDL-62142 navigation: accessibility Set unique meaningful labels on all nav components rendered in the "boost" theme. --- lib/navigationlib.php | 95 +++++++++++++++++-- lib/upgrade.txt | 1 + theme/boost/layout/columns2.php | 4 +- theme/boost/templates/columns1.mustache | 2 +- theme/boost/templates/columns2.mustache | 4 +- theme/boost/templates/core/navbar.mustache | 2 +- .../boost/templates/flat_navigation.mustache | 4 +- theme/boost/templates/footer.mustache | 4 +- theme/boost/templates/login.mustache | 2 +- theme/boost/templates/maintenance.mustache | 2 +- theme/boost/templates/navbar-secure.mustache | 4 +- theme/boost/templates/secure.mustache | 4 +- 12 files changed, 105 insertions(+), 23 deletions(-) diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 93d32b5d2bd..1090652fc70 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -430,15 +430,16 @@ class navigation_node implements renderable { * * @param flat_navigation $nodes List of the found flat navigation nodes. * @param boolean $showdivider Show a divider before the first node. + * @param string $label A label for the collection of navigation links. */ - public function build_flat_navigation_list(flat_navigation $nodes, $showdivider = false) { + public function build_flat_navigation_list(flat_navigation $nodes, $showdivider = false, $label = '') { if ($this->showinflatnavigation) { $indent = 0; if ($this->type == self::TYPE_COURSE || $this->key === self::COURSE_INDEX_PAGE) { $indent = 1; } $flat = new flat_navigation_node($this, $indent); - $flat->set_showdivider($showdivider); + $flat->set_showdivider($showdivider, $label); $nodes->add($flat); } foreach ($this->children as $child) { @@ -913,6 +914,12 @@ class navigation_node_collection implements IteratorAggregate, Countable { */ protected $count = 0; + /** + * Label for collection of nodes. + * @var string + */ + protected $collectionlabel = ''; + /** * Adds a navigation node to the collection * @@ -988,6 +995,24 @@ class navigation_node_collection implements IteratorAggregate, Countable { return $keys; } + /** + * Set a label for this collection. + * + * @param string $label + */ + public function set_collectionlabel($label) { + $this->collectionlabel = $label; + } + + /** + * Return a label for this collection. + * + * @return string + */ + public function get_collectionlabel() { + return $this->collectionlabel; + } + /** * Fetches a node from this collection. * @@ -3770,6 +3795,9 @@ class flat_navigation_node extends navigation_node { /** @var $showdivider bool Show a divider before this element */ private $showdivider = false; + /** @var $collectionlabel string Label for a group of nodes */ + private $collectionlabel = ''; + /** * A proxy constructor * @@ -3791,6 +3819,31 @@ class flat_navigation_node extends navigation_node { $this->indent = $indent; } + /** + * Setter, a label is required for a flat navigation node that shows a divider. + * + * @param string $label + */ + public function set_collectionlabel($label) { + $this->collectionlabel = $label; + } + + /** + * Getter, get the label for this flat_navigation node, or it's parent if it doesn't have one. + * + * @return string + */ + public function get_collectionlabel() { + if (!empty($this->collectionlabel)) { + return $this->collectionlabel; + } + if ($this->parent && ($this->parent instanceof flat_navigation_node || $this->parent instanceof flat_navigation)) { + return $this->parent->get_collectionlabel(); + } + debugging('Navigation region requires a label', DEBUG_DEVELOPER); + return ''; + } + /** * Does this node represent a course section link. * @return boolean @@ -3828,9 +3881,15 @@ class flat_navigation_node extends navigation_node { /** * Setter for "showdivider" * @param $val boolean + * @param $label string Label for the group of nodes */ - public function set_showdivider($val) { + public function set_showdivider($val, $label = '') { $this->showdivider = $val; + if ($this->showdivider && empty($label)) { + debugging('Navigation region requires a label', DEBUG_DEVELOPER); + } else { + $this->set_collectionlabel($label); + } } /** @@ -3848,7 +3907,6 @@ class flat_navigation_node extends navigation_node { public function set_indent($val) { $this->indent = $val; } - } /** @@ -3903,6 +3961,7 @@ class flat_navigation extends navigation_node_collection { format_string($course->fullname, true, array('context' => $coursecontext)); $flat = new flat_navigation_node(navigation_node::create($coursename, $url), 0); + $flat->set_collectionlabel($coursename); $flat->key = 'coursehome'; $flat->icon = new pix_icon('i/course', ''); @@ -3930,9 +3989,9 @@ class flat_navigation extends navigation_node_collection { } } - $this->page->navigation->build_flat_navigation_list($this, true); + $this->page->navigation->build_flat_navigation_list($this, true, get_string('site')); } else { - $this->page->navigation->build_flat_navigation_list($this, false); + $this->page->navigation->build_flat_navigation_list($this, false, get_string('site')); } $admin = $PAGE->settingsnav->find('siteadministration', navigation_node::TYPE_SITE_ADMIN); @@ -3942,7 +4001,7 @@ class flat_navigation extends navigation_node_collection { } if ($admin) { $flat = new flat_navigation_node($admin, 0); - $flat->set_showdivider(true); + $flat->set_showdivider(true, get_string('sitesettings')); $flat->key = 'sitesettings'; $flat->icon = new pix_icon('t/preferences', ''); $this->add($flat); @@ -3956,7 +4015,7 @@ class flat_navigation extends navigation_node_collection { $url = new moodle_url($PAGE->url, ['bui_addblock' => '', 'sesskey' => sesskey()]); $addablock = navigation_node::create(get_string('addblock'), $url); $flat = new flat_navigation_node($addablock, 0); - $flat->set_showdivider(true); + $flat->set_showdivider(true, get_string('blocksaddedit')); $flat->key = 'addblock'; $flat->icon = new pix_icon('i/addblock', ''); $this->add($flat); @@ -3969,6 +4028,26 @@ class flat_navigation extends navigation_node_collection { } } + + /** + * Override the parent so we can set a label for this collection if it has not been set yet. + * + * @param navigation_node $node Node to add + * @param string $beforekey If specified, adds before a node with this key, + * otherwise adds at end + * @return navigation_node Added node + */ + public function add(navigation_node $node, $beforekey=null) { + $result = parent::add($node, $beforekey); + // Extend the parent to get a name for the collection of nodes if required. + if (empty($this->collectionlabel)) { + if ($node instanceof flat_navigation_node) { + $this->set_collectionlabel($node->get_collectionlabel()); + } + } + + return $result; + } } /** diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 1c2f473b55a..5478644d350 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -3,6 +3,7 @@ information provided here is intended especially for developers. === 3.7 === +* Nodes in the navigation api can have labels for each group. See set/get_collectionlabel(). * The method core_user::is_real_user() now returns false for userid = 0 parameter === 3.6 === diff --git a/theme/boost/layout/columns2.php b/theme/boost/layout/columns2.php index d942fcbabba..b44726f91bf 100644 --- a/theme/boost/layout/columns2.php +++ b/theme/boost/layout/columns2.php @@ -51,6 +51,8 @@ $templatecontext = [ 'hasregionmainsettingsmenu' => !empty($regionmainsettingsmenu) ]; -$templatecontext['flatnavigation'] = $PAGE->flatnav; +$nav = $PAGE->flatnav; +$templatecontext['flatnavigation'] = $nav; +$templatecontext['firstcollectionlabel'] = $nav->get_collectionlabel(); echo $OUTPUT->render_from_template('theme_boost/columns2', $templatecontext); diff --git a/theme/boost/templates/columns1.mustache b/theme/boost/templates/columns1.mustache index 022df603aa6..49ed3ba1e9c 100644 --- a/theme/boost/templates/columns1.mustache +++ b/theme/boost/templates/columns1.mustache @@ -47,7 +47,7 @@
-
+
{{{ output.course_content_header }}} {{{ output.main_content }}} {{{ output.activity_navigation }}} diff --git a/theme/boost/templates/columns2.mustache b/theme/boost/templates/columns2.mustache index 7b329cbecfc..d0425d39965 100644 --- a/theme/boost/templates/columns2.mustache +++ b/theme/boost/templates/columns2.mustache @@ -68,7 +68,7 @@
{{{ output.region_main_settings_menu }}}
{{/hasregionmainsettingsmenu}} -
+
{{#hasregionmainsettingsmenu}}
@@ -80,7 +80,7 @@
{{#hasblocks}} -
+
{{{ sidepreblocks }}}
{{/hasblocks}} diff --git a/theme/boost/templates/core/navbar.mustache b/theme/boost/templates/core/navbar.mustache index 07d0c0404aa..207081f4e64 100644 --- a/theme/boost/templates/core/navbar.mustache +++ b/theme/boost/templates/core/navbar.mustache @@ -63,7 +63,7 @@ ] } }} -
- \ No newline at end of file + diff --git a/theme/boost/templates/login.mustache b/theme/boost/templates/login.mustache index ebca1095608..f44017ddb6c 100644 --- a/theme/boost/templates/login.mustache +++ b/theme/boost/templates/login.mustache @@ -40,7 +40,7 @@