Note: You are currently viewing documentation for Moodle 2.9. Up-to-date documentation for the latest stable version of Moodle may be available here: Navigation 2.0 navbar proposal.

Development:Navigation 2.0 navbar proposal

From MoodleDocs
Revision as of 04:08, 13 July 2009 by Sam Hemelryk (talk | contribs) (New page: ==Navigation Bar in Moodle 2.0== Currently I am aiding Tim and his mega-development effort by looking into what can be done with the navigation bar in Moodle 2.0. The end desire is that '...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Navigation Bar in Moodle 2.0

Currently I am aiding Tim and his mega-development effort by looking into what can be done with the navigation bar in Moodle 2.0. The end desire is that build_navigation() is deprecated and replaced by a new OO navbar.

We would obviously like this new navbar to be self responsible whilst still being flexible enough that you can interact and modify it as you wish.

The following is my current thinking on this topic and I would love to know your thoughts on the matter, as well as any changes or work-overs you can spot.

The Plan

As suggested in Development:Navigation 2.0 the navbar object will be made available through $OUTPUT->navbar, both for output and interaction. It will be set up to generate as much of the navbar as it possibly can without any input, and with sufficient methods to allow you to add, edit, and remove items registered in the navbar.

By default the navigation bar will generate the following items:

  • Site's short name
  • Course categories (1..n)
  • Course short name

If you pass the navbar a module as well by calling navbar->add_module($cm); then it will also generate:

  • Module plural (e.g. forums, SCORMs/AICCs)
  • Module's name

When interacting with the navbar items you will be able to access them by calling the get method with the navbar items title. However this relies on you knowing the title of the navbar item you are after, something which you may not always know. Because of this when each navbar item is generated it will be given a type that matches what it is (e.g. course, coursecategory, module) and the coder can call get_by_type(my_type) to fetch an array of navbar items that match type specified.

Also worth noting is that to me when looking at the structure of a Moodle course it made more sense to me that rather than adding an item for the modules plural name, an item should be added for the course section (e.g. week 1, week 2, week 3) however as there is no page specifically for the course section this is not possible. I will however comment an appropriate point for which this could be modified should this ever be an option.

Interaction

When developing code for Moodle the following methods can be used to interact with the navbar. $OUTPUT->navbar->add_module(course_module); // Pass a module to the navbar to generate items for $OUTPUT->navbar->add(title, url, type[optional:default=custom]); // Add a new navbar item $OUTPUT->navbar->exists(title); // Checks if a navbar item with a given title exists $OUTPUT->navbar->get(title) // Returns a single navbar item $OUTPUT->navbar->get_by_type(type); // Returns an array of navbar items that match the type specified $OUTPUT->navbar->remove(title); // Removes a navbar item Editing navbar items $OUTPUT->navbar->get(title)->title = 'newtitle'; $OUTPUT->navbar->get(title)->url = 'newtitle'; $OUTPUT->navbar->get(title)->icon = 'newtitle'; $OUTPUT->navbar->get(title)->elementid = 'newid'; $OUTPUT->navbar->get(title)->add_css_class('newclass');

Implementation

The plan is to deprecate build_navigation and would be done using the above planned code in the following manner.

Replace: $navlinks = array(); $navlinks[] = array('name' => $strcategories, 'link' => 'index.php', 'type' => 'misc'); $navlinks[] = array('name' => format_string($category->name), 'link' => null, 'type' => 'misc'); $navigation = build_navigation($navlinks); print_header("$site->shortname: $category->name", "$site->fullname: $strcourses", $navigation, , , true, $navbaritem);

With: $OUTPUT->navbar->add($strcategories, $CFG->wwwroot.'/course/index.php'); $OUTPUT->navbar->add(format_string($category->name));

In the template: if ($navigation) { // This is the navigation bar with breadcrumbs  ?>