Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Creating different custom menu bars for different courses: Creating different menu bars: Difference between revisions

From MoodleDocs
(Created page with "{{Template:Themes}}This tutorials explains how to create different custom menu bars for different courses in a Moodle site This tutorial involves some previous knowledge of p...")
 
Line 3: Line 3:
This tutorial involves some previous knowledge of php and css. The level of difficulty of the tutorial is moderate.
This tutorial involves some previous knowledge of php and css. The level of difficulty of the tutorial is moderate.


==Before we begin==
==What is the 'custom menu' bar==
First things first you need to know a little something about the custom menu. The custom menu is populated from the manually entered input in ''"Theme Settings"''. Once the administrator has entered the custom menu items and saved them the following steps are what they go through in order to be displayed:
# The theme calls '''''$OUTPUT->custom_menu()''''' which is the core_renderers method responsible for producing the custom menu.
# core_renderer::custom_menu() does the following:
## Checks to make sure the admin has added custom menu items.
## Creates a new '''''custom_menu''''' object. When the custom menu object is created it turns what ever the admin entered into a structured array of information.
## Calls core_renderer::render_custom_menu and gives it the custom_menu object.
# core_renderer::render_custom_menu is where the magic happens, it does the following.
## First it checks to make sure custom menu contains items.
## Initialises the JavaScript for the custom menu, this is the YUI3 menunode component.
## Creates the HTML base of the custom menu
## Then iterates through all of the items and calls the custom menu and passes them to another function that will turn them into HTML correctly.


I'm going to stop there, as there is no need at this point to go into any more detail. Don't worry if you are a little lost, it will get clearer as we start working through code.
Number list
#
#
##
##


In this tutorial will we look at how to extend the theme's renderer in two different way.
I
# Add a dynamic My Courses branch to the custom menu that will display all of the courses a user is enrolled in.
# Get the custom menu to fetch strings from the language files rather than just static strings you type, allowing for a translatable custom menu.


Before you start into this tutorial you should have read the following tutorials I have written, or at least have a good knowledge of the the Moodle 2.0 theme engine and Moodle development.
Link to other pages
* [[Themes 2.0 creating your first theme]]
* [[Themes 2.0 creating your first theme]]
* [[Themes 2.0 overriding a renderer]]
* [[Themes 2.0 overriding a renderer]]
As this is a quick tutorial I am going to assume you have created a theme, tested it and got it all working, and are already well on your way to becoming a theme guru.
Because the pressure is on to get Moodle 2.0 out the door this will be a quick tutorial, please if you have any questions or need a hand ask in the theme's forum.


==Getting started==
==Getting started==

Revision as of 13:40, 2 August 2013

This tutorials explains how to create different custom menu bars for different courses in a Moodle site

This tutorial involves some previous knowledge of php and css. The level of difficulty of the tutorial is moderate.

What is the 'custom menu' bar

Number list

I

Link to other pages

Getting started

Alright, as you have already have your theme ready to go preparation is pretty simple, we are going to go through and create (if you haven't already) the following files:

  • theme/themename/lib.php
  • theme/themename/renderers.php
  • theme/themename/lang/en/themename.php

Next step open up your themes config.php file and add the following configuration option to it (at the bottom): $THEME->rendererfactory = 'theme_overridden_renderer_factory';

If you've already got a custom renderer you will already have this line.

And thats it! now we move on to extending the custom menu.

Adding the My Courses branch

So the point of this extension is to add a My Courses branch to the end of the custom menu.

The plan is to have the my courses branch and then within that branch have an entry for every course the user is enrolled in, much the same as the my courses branch of the navigation.

In order to achieve this we need to add some items to the custom menu, the my courses branch and all of the courses within it. We can do this overriding the core_renderers render_custom_menu method, of more accuratly create our own render_custom_menu method that adds our items and then calls the original. Remember we only need to add items, we don't need to change what is being produced.

So within our renderers.php file add the following code: class theme_themename_core_renderer extends core_renderer {

   protected function render_custom_menu(custom_menu $menu) {
       // Our code will go here shortly
   }

}