How to build a main menu using the navigation block

How to build a main menu using the navigation block

by Martín Maglianesi -
Number of replies: 56

Hello.

I'm wanting to create a block similar to the navigation block that functions as a "Main Menu" from the educational institution. I have read the document "Development: Navigation 2.0 navbar proposal " and "Development: Moodle 2.0 navigation how to" and several discussions in the forums but do not quite understand how it should proceed.

Someone can guide me to read the documentation. Help me a lot to have a practical example.

Thank you very much and please excuse my English.

Martin

Average of ratings: -
In reply to Martín Maglianesi

Re: How to build a main menu using the navigation block

by Danny Wahl -
it sounds like you're asking to have your own custom nav - one that's not based on anything built into Moodle. Basically all you need to do is add a new text block, set the type as HTML and code your own.
In reply to Danny Wahl

Re: How to build a main menu using the navigation block

by Martín Maglianesi -

Effectively, I am wanting to have a custom navigation block consisting of a hierarchically organized set of links. The issue is that I want to use the core moodle navigation block to make it integrated.

In reply to Martín Maglianesi

Re: How to build a main menu using the navigation block

by Martín Maglianesi -

Well, I searched for documentation but the most promising is incomplete sad the document is the one I quoted above: "Development: Moodle 2.0 how-to navigation".

I searched the forum and I see a thread that suggests I might have a solution for what I want to directly modifying the function initialise() from /lib/navigationlib.php. There may wish to add the links to the site. However, I do not think that is the correct way of doing things.

How do I add a series of links to rootnode ['home '] without changing the class code global_navigation?

Forgive my insistence and English wink

Thank you very much.

Martin

In reply to Martín Maglianesi

Re: How to build a main menu using the navigation block

by Eric Millin -

I have just discovered how to do this and I figured I'd post it incase someone else is looking for help.

In initialise(), lines 1114-1123 of /lib/navigationlib.php, Moodle searches /local for plugins that extend the navigation menu.  If it finds one, it passes a $global_navigation object.  So here's what you do:

1) Create the directory /wwwroot/local/myplugin

2) Create a new file called "lib.php" in the directory.

3) Moodle looks for a function name that matches "myplugin" + "_extends_navigation".  In our case, add the following function to your file:

function myplugin_extends_navigation(global_navigation $navigation) {
$nodeFoo = $navigation->add('Foo');
$nodeBar = $nodeFoo->add('Bar');
}

Reload the main page and you should now see this:

Average of ratings:Useful (11)
In reply to Eric Millin

Re: How to build a main menu using the navigation block

by Javier Fernandez -

Hi Eric,

Thanks for sharing it with us. Unfortunately, it doesn´t work work us. This is what I get. Any advice please?

In reply to Javier Fernandez

Re: How to build a main menu using the navigation block

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi Javier,

This may seem like an odd question, but is that code in your lib.php file enclosed in <?php and ?> tags? It looks to me like they've been omitted, so it's just printing the contents of the file.

Average of ratings:Useful (2)
In reply to Mark Johnson

Re: How to build a main menu using the navigation block

by Javier Fernandez -

Thanks Mark,

It was that. As you can see I dont know anything about php. Is there any manual for moodle developpers I could have a look?

I just installed it and need to tweak it a bit.

Thanks,

Javier

In reply to Eric Millin

Re: How to build a main menu using the navigation block

by Jamie Brett -

Eric,

Your 'navigationlib.php' solution is a good one, and looks close to a solution for something we'd like to do. We'd like to hide from students the Navigation block 'My profile' node and sub nodes. I don't know php enough to intelligently edit navigationlib.php to hide that node. Would you make a suggestion? Thank you in advance.

In reply to Jamie Brett

Re: How to build a main menu using the navigation block

by Javier Fernandez -

Hi Eric,

What about if you want to link "Bar" to an external website?

<?php

function myplugin_extends_navigation(global_navigation $navigation) {

$nodeHelp = $navigation->add('Help');

$nodeHelp->add('Bar');}

?>

Thanks in advance,

Javier

Average of ratings:Useful (1)
In reply to Javier Fernandez

Re: How to build a main menu using the navigation block

by Eric Millin -

Here are some snippets of what I've done so far.  This is experimentation and I'm new to PHP, so take it for what it's worth.

function custom_nav_extends_navigation(global_navigation $navigation) {

//get the "Home" node

$nodeHome = $navigation->children->get('1')->parent;

//Rename it

$nodeHome->title= 'Foo';
$nodeHome->key='Foo';
$nodeHome->text = 'Foo';

//Create a child node
$nodeAwesome = $navigation->add('10 Reasons We're Awesome');

//Add children to nodeAwesome. Pretend we have a list "$myList" of links to add.
for ($i = 0; $i <= count($myList); $i += 1)
$nodeAwesome->add($myList[$i], new moodle_url('/path/to/file/'.$myList[$i]), null, null, $myList[$i]);

//force the node open
$nodeAwesome->forceopen = true;
}

You can put whatever url you like when you add a node.
Average of ratings:Useful (2)
In reply to Eric Millin

Re: How to build a main menu using the navigation block

by Tuan Tran -

Thank you so much. It works smile

In reply to Jamie Brett

Re: How to build a main menu using the navigation block

by Jamie Brett -

Just noting that my previous post in this thread is a little off topic, and addressed more specifically in this thread:

http://moodle.org/mod/forum/discuss.php?d=170934#p755095

In reply to Jamie Brett

Re: How to build a main menu using the navigation block

by Eric Millin -

You should be able to extend the nav block, then recursively loop through all of the nodes starting from "Home" and pluck out the ones you want to remove.

In reply to Eric Millin

Re: How to build a main menu using the navigation block

by Lavanya Manne -
Picture of Plugin developers

Hi Eric Millin,

Could I know how could we call only the courses section from navigation block and display in my custom block. I need a particular function for courses.

 

Thanks

Lavanya

In reply to Eric Millin

Re: How to build a main menu using the navigation block

by Rosario Carcò -

Thanks a lot for this. I developed a sitenavigation for Moodle 1.9 you will find in the new plugins section and in these forums. I implemented a lot of admin options to tailor what should be displayed and you can use the latest version with popUp-windows or inline display. I might think about porting some features to 2.x one day.

Rosario

In reply to Eric Millin

Re: How to build a main menu using the navigation block

by Stephen D -

Thanks, I have followed this solution and managed to generate custom navigation items. Do you know how I would be able to only show these for teachers? For example to add shortcuts to commonly used pages that I only want teachers to have access to?

In reply to Stephen D

Re: How to build a main menu using the navigation block

by Rosario Carcò -

YES, this is sort of things I do in my own myCourses Block. You have to check the roles/capabilities and show only if the logged in user meets those conditions. You will find examples here in the forums or when diving into the code of myCourses Block.

Rosario

In reply to Eric Millin

Re: How to build a main menu using the navigation block

by Sabin Pradhan -

Hi Eric,

I just look at your code of adding the node to navigation.But I like to rename the Courses node to something else.I am also new babies to php.

Can you suggest me how to do that.I had attached the screen shot of actually what I want to achieve.

 

Thanks for your help.

Sabin

Attachment Capture3.PNG
In reply to Sabin Pradhan

Re: How to build a main menu using the navigation block

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

You dont need any php or code changes to rename this - just edit the language pack so that it says what you want smile

Site Administration > Language > Language customisation

Richard

In reply to Richard Oelmann

Re: How to build a main menu using the navigation block

by Sabin Pradhan -

Hi Richard,

You are right.I already change the name of node and others by using Language customisation.

Well it will be great if you can also help me in another issue.

I would like to redirect eh user to particular page after they successfully log in for e.g to this url :http://localhost/moodle/mod/lesson/view.php?id=1

Do you know how to do it.

Thanks

Sabin

In reply to Sabin Pradhan

Re: How to build a main menu using the navigation block

by nitin sharma -

go to index page of your moodle root i.e index.php

add code over there

if(is_loggedin())

{

$url = $CFG->wwwroot."/mod/lesson/view.php?id=1";

redirect($url);

}

In reply to Sabin Pradhan

Trả lời: Re: How to build a main menu using the navigation block

by duy vu -

you do it follow image

place folder lib

In reply to Martín Maglianesi

Re: How to build a main menu using the navigation block

by David Bogner -
Picture of Core developers Picture of Plugin developers

Hi,

we just finished a block "simple navigation" perhaps this block does the job for you, or you can extend it to fit your needs:

https://github.com/dasistwas/moodle-block_simple_nav

Best regards,

David

In reply to David Bogner

Re: How to build a main menu using the navigation block

by Rosario Carcò -

David, could you specify a little bit?? My siteNavigation offers a lot of features like this:


- display only categories, display categories AND courses of the whole site

- display to logged in users only or also to not logged in users

- nested hierarchical display with collapsible/expandable folders or flat list only


As described above and as you can read in the readme you can find in yourMoodleDir/local, any sort of addition can be made to the existing navigation block. So I will have to decide whether to rewrite my entire block or try to add my features to the existing one, including the myCourses part which has also its nice feature list.

Rosario

In reply to Rosario Carcò

Re: How to build a main menu using the navigation block

by David Bogner -
Picture of Core developers Picture of Plugin developers

Hi Rosario,

I saw your block in the plugins database, but as it is only for 1.9, we decided to write a new one.

The Moodle 2.X navigation block provides great navigation features, but our goal was to make it simpler, to have a block, that is used principally for the parts of our site, where no login is needed. That means we use moodle courses with guest and autologin as normal webpages. Therefore we do not have to use another CMS like TYPO3, Drupal, etc. and avoiding to have to learn and train all our colleagues to administrate 2 different CMS.

But the Moodle navigation block was not suitable for standard website navigation, so we programmed the simple navigation block. (Users should always have the same links, logged in, or not logged in, but Moodle navigation block creates completely different views whether you are logged in or not.)

We tried to override the navigation renderer, but it always affected the whole navigation object, so we decided not to use the navigation object as base for the block, but wrote some simple functions, that just render modules/courses/categories.

If you have any further questions, just contact me again.

Best regards,

David

In reply to David Bogner

Re: How to build a main menu using the navigation block

by Rosario Carcò -

David, thanks a lot, you are confirming what I discovered and wrote in my own siteNavigation thread. The built in navigation is deeply embedded with core code, so that I assumed it would be difficult to tune it for our needs without compromising future upgrades of core and navigation code.

But as I wrote above, I discovered that you can add custom code to the existing block by using the yourMoodleDir/local directory. So before rewriting my sitenavigation I will have to explore those possibilites. It might be, you could overwrite the whole output with a simple trick or the opposite might be true, that you could only add to the existing output, making display a lot more complicated. And hence your idea, like mine, is correct to provide an own block with a very simple navigation to find categories and courses.

Rosario

In reply to Rosario Carcò

Re: How to build a main menu using the navigation block

by Mari Cruz García -

Hello,

Thank you very much for this development.

I am trying to implement a navigation menu that displays all the visible and hidden categories are nodes (therefore, we need different views depending on users' roles), so that, when a user clicks on a node, the list of available courses within that category is displayed.

I have tried to adapt original Rosario's block for Moodle 1.9, but it is too complicated,

so I think that I will use the /local/myplugin solution instead.


I am not a php programmer, but an educational technologist, so I am not familiar with all programming options in Moodle.

I guess that the best way of achieving what I want is try something like this:

Pass the list of categories and subcategories to $nodeAwasome as an array and $mylist would be the list of courses for each one of the categories or subcategories.


Could you please give some advice about how to programme this in php?


Thank you very much.

 




 
In reply to Mari Cruz García

Re: How to build a main menu using the navigation block

by Mari Cruz García -

Hello David,

I have just tried your block. It is what I was looking for, but, when I tested it in Moodle 2.0: it displayed all the categories and subcategories as a displayable menu, but not courses.

I checked that 'Show courses' and 'show modules' are ticked.


Do you know why is it doing that?

In reply to Mari Cruz García

Re: How to build a main menu using the navigation block

by David Bogner -
Picture of Core developers Picture of Plugin developers

Hi Aaricia,


thank you for the feedback. We developed the block using Moodle 2.2, so there might be issues with moodle 2.0. Could you send me a screenshot of the problem? Just to be sure, click on save again in the settings menu.

Also if you do not have the right to access a course, the content is not shown in the menu. So you have to enable guest access in the course, if you want to have the content displayed in the block, when you are not logged in.

Best regards,

David

In reply to Rosario Carcò

Re: How to build a main menu using the navigation block

by David Bogner -
Picture of Core developers Picture of Plugin developers

Hi Rosario,

we actually also started with writing code in local directory, but that does not make much difference to writing it in the block itself.

The problem is the navigation object. Everytime we tried to get rid of some elements, also the breadcrumb, etc. was affected. Because everytime we interfered with the navigation object, the whole site was affected and not only the navigation block.

I think it is not possible to alter the navigation block substantially by using the navigation object.

Best regards,

David

In reply to David Bogner

Re: How to build a main menu using the navigation block

by Rosario Carcò -

You are right, and that's what I feared. So I will program my own sitnavigation combined with myCourses again. But it is a matter of some weeks because I am busy with porting my uploadusersandcourses.php script to Moodle 2.3

Rosario

In reply to Rosario Carcò

Re: How to build a main menu using the navigation block

by Mari Cruz García -

Hello David,

I tested exhaustively the 'Simple Navigation block' in Moodle 2.2 and Moodle 2.0 and these are my findings:

The block shows the same kind of behaviour for Moodle 2.0 and Moodle 2.0: in general, it works well and displays the courses under the different categories and subcategories, but it is very flaky for some features:

1) It doesn't work very well when you select show modules and, sometimes, it stops displaying courses if you select that option.

As I have customised our theme for Moodle, I tried with different Moodle standard sites and I always have the same problem.

 

2) The block interferes with the standard Navigation bar, and I think that it is because it is using the same navigation objects. When I say 'interfere', I mean, for instance: before logging in the Moodle site, if you select a category in order to display all the courses under that category, what the blocks does actually is to merge itself with the Navigation bar menu, that is, the Simple Navigation block disappears and the Navigation bar only shows the links to course categories, but not 'Courses for instance, or other elements in the Navigation menu.


Another problem that I have noticed is: if you click on one of the categories, to display the list of courses under that categories, it goes to the page 'moodlesite/course/category.php?id=1', being id the id number of the selected category.

I am totally sure that I haven't clicked on the category hyperlink to go to /category.php?id, just the category icon on the left hand side of the menu.


I am starting to look at the code now to understand how it works.

 

Regards

 

In reply to Mari Cruz García

Re: How to build a main menu using the navigation block

by Mari Cruz García -

Hello David,

I just let you know that the block is more stable if I comment the line:

// is it a category
        if ($mytype == 'category') {
            //$myurl =$CFG->wwwroot.'/course/category.php?id='.$myid;

in renderer.php.

We don't have need to create a hyperlink to the categories, so for us it is better in this way.

Likewise, the block is only visible to administrators (students can see their courses through 'My Courses' in the Nav bar) so I don't have the problem of the two navigation blocks interfering with each other.

Thank you very much for this.

Have you thought of releasing it as a plug-in?

In reply to Mari Cruz García

Re: How to build a main menu using the navigation block

by David Bogner -
Picture of Core developers Picture of Plugin developers

Hello Aaricia,

thank you for your feedback. I did not quite understand your previous comments, but I hope the issues you raised have been solved. 

Adapting the code to not show the links to the categories is great. We could have added an option for that. But our intention was more to use categories as overview pages for our website. Because we use Moodle as CMS and LMS, so we do not have to install TYPO3, Drupal or any other software. 

That makes it easier for us to keep administration effort of the website very minimal with maximum usability of the site. 

The plugin is already in the plugins database and has been approved by Anthony. I hope we can do some documentation for the block soon.

Link to the block: http://moodle.org/plugins/view.php?plugin=block_simple_nav

Best regards,

David

In reply to David Bogner

Re: How to build a main menu using the navigation block

by Mari Cruz García -

Sorry if you didn't understand my previous comments. I am looking forward to testing the final version in the plug-in database.

I have noticed that, in the database, it is not stated if the plug-in is stable or not, because there was a discussion a while ago in Moodle.org regarding if the plug-ins database should only include stable versions or not.

I am saying that because in Moodle 2.2 and Moodle 2.2+ (latest version), if you select to show all the modules, then when you click on each category item, it does not display any modules under that category (that was in the version I downloaded originally, maybe this has been solved in new version in the plug-ins database.

If you are using the block as a short of 'site map' (as I can see it), it makes sense that all the displayed items are hyperlinks.
 

If you just want to use the block as a sort of 'accordion menu', you also need to alter this part:

$myitem = $myclass_ul_open.'<li class=".$myclass_li."><p class=".$myclass_p."><a '.$myclass_a.' href="'.$myurl.'">'.$icon.''.$myname.'</a></p>'.$myopentag;
        return $myitem;

so that $myurl is only considered for those items that are not categories or subcategories. Otherwise, the browser will be all the time trying to navigate to that $myurl and trying to re-upload the frontpage which can be very annoying for our users.


I hope that my comments are clearer now.

 

Again, thank you very much for sharing this smart block with the community.

Regards

In reply to David Bogner

Re: How to build a main menu using the navigation block

by Rosario Carcò -

I am late... but in November 2012 I released my 2.3 Versions of myCourses and siteNavigation. You can find them in their respective Forum Threads and siteNavigation as it is a merged version whick offers two in one, mycourses AND sitenavigation in one block with floating-pop-up-windows for navigation or block inline display, can be found in the Modules&Plugins Database.

And of course, as it is opensource, you may use both modules to tailor them to fit you exact needs or ask me for a new feature.

Just drop me a comment in the M&P Database or in one of the threads where you can find myCourses and siteNavigation.

Rosario

In reply to David Bogner

Re: How to build a main menu using the navigation block

by H B -

Hi

I added the block in my moodle 2.3.1.

But I never se the block appear? I don't have the possibility for guests to login. What should I change in the php to make this block show>

In reply to H B

Re: How to build a main menu using the navigation block

by H B -

OKay, I managed to show the block Simple-navigation. I try to use the options in the settings of this block,, but if i tick to show all modules and course or if I untick them, its all the same. This block always shows all the courses and modules. There are some places in my moodle LMS which I don not want to show the courses and modules. Is there something I forget?

In reply to H B

Re: How to build a main menu using the navigation block

by David Bogner -
Picture of Core developers Picture of Plugin developers

Hi Hellen,


thank you for this comment. The latest version has a bug. I will fix this as soon a possible. If you use the first version it should work: http://moodle.org/plugins/download.php/962/block_simple_nav_moodle23_2012062800.zip

Best regards,

David

In reply to David Bogner

Re: How to build a main menu using the navigation block

by H B -

Hi David,

sad there seems to be some errors here to in this version

1) if I leave 'Show subcategories ' and 'show course' empty then the layout of this block is messed up (in IE 9) , see attachment.

2) I can then click on the category which then still shows shows me all the courses.

Is there a way not to show the categories also?

Or maybe better, can I just show the categories and courses a user has acces to?

I really need a simple navigation block like this, because the normal navigation shows too much 'possibilites for our users. They get confused. So I would be very grateful if you can help me with this, or just give me some tips for how to change it. I have a little bit knowledge of PHP.

Attachment messed up.jpg
In reply to H B

Re: How to build a main menu using the navigation block

by David Bogner -
Picture of Core developers Picture of Plugin developers

Hi Hellen,

we are working on the bug fixes. I think this will be ready on monday. If I do understand your screenshot correctly, it shows the following:

-> Three first level course categories

-> Under these three levels there are the pages of the frontpage (Modules created in the main menu on the frontpage)


There are two sections in the settings: Modules on the frontpage and other modules. Also the "show subcategories" option should not do anything, we considered that as useless.

We are working, that the settings work correctly. I will post here, when the new version is ready.

 

Best regards,

David

In reply to David Bogner

Re: How to build a main menu using the navigation block

by H B -

Hi David,

About my layout: the structure should look like:

Home
>Overig (1)
  > s1
   > s2
>De Laatste toets (2)
   > Engels
   > Duits
   > Nederlands
   > Frans
>Intensief LEzen (3)
  >Frans
  >Engels
  >Duits
> Demo programma's (4)
> Informatie (5)
FAQ
Hulp

So 5 Categories with sub-categories and 2 modules. The sub- categories coptains courses which are scormpackages. In the messed-up layout some sub-categories are pushed on top of the menu structure.

 

In reply to H B

Re: How to build a main menu using the navigation block

by Derek Chirnside -

David and Hellen,

Just in case you are interested, there is another menu in the plugins: http://moodle.org/plugins/view.php?plugin=block_course_menu

-Derek

In reply to Derek Chirnside

Re: How to build a main menu using the navigation block

by H B -

Hi Derek,

This course-menu is block is nice, but it stil show superfluous information in the navigation, just like moodle's own navaigation.

E.g. It shows the section, participants. In the attachment you see:

sub-category De Laatstet Toets Engels HAVO. This contains also the ' Deelnemers' (translated : participants), and 'Sectie 0 ' (tranlated Section 0).  I woul like to have a simple navigation. The user should only see the category and the coursenames.

 

Attachment menu.jpg
In reply to H B

Re: How to build a main menu using the navigation block

by Derek Chirnside -

Helen, this is all configurable.

I also like the clean look.  Here is my latest course with everything switched off in configuration.

-Derek

In reply to Derek Chirnside

Re: How to build a main menu using the navigation block

by H B -

Hi Derek,

Could you tell me how you managed that. I don't see where this option is to NOT show the section e.g.

In reply to H B

Re: How to build a main menu using the navigation block

by David Bogner -
Picture of Core developers Picture of Plugin developers

Hi Hellen,


I just uploaded a bug-fixed version of the simple navigation block. The settings should now work correctly. Default is not to show url and label modules. But that can be modified for a) the frontpage b) for all other site pages.


Best regards,

David

In reply to David Bogner

Re: How to build a main menu using the navigation block

by H B -

Hi David,

Thank you, I downloaded the latest version. Could you tell me which options I need to tick/untick when I only want to show the modules directly beneath the startpage? So no courses should be visible

In reply to H B

Re: How to build a main menu using the navigation block

by David Bogner -
Picture of Core developers Picture of Plugin developers

Hi Hellen,


I think you can make disappear the courses with some CSS rule in your theme. Or change the pages to courses. If you want the modules directly beneath the startpage you have to create them on the startpage.

Best regards.

David

In reply to David Bogner

Re: How to build a main menu using the navigation block

by H B -

Hi David

Thanks for all your answers and work.

In reply to H B

Re: How to build a main menu using the navigation block

by Derek Chirnside -

Hellen, sorry, I missed this question yesterday.  I've PM'ed you with a site URL with the menu working, If you really want to see this from the inside, create an account and I'll put you in as teacher to see the config screen.

It looks like this (below).  That's how I like the menu.  No clutter. Helps with ADHD enhanced teenagers.  And prpobably a few others as well smile The custom links can be anything you like.

In reply to Derek Chirnside

Re: How to build a main menu using the navigation block

by Sabin Pradhan -

Hi ,

I just look at code mentioned by Eric in this thread of adding the node to navigation.But I like to rename the Courses node to something else.I am also new babies to php.

Can you suggest me how to do that.I had attached the screen shot of actually what I want to achieve.

 

Thanks for your help.

Sabin

 
Attachment Capture3.PNG
In reply to Sabin Pradhan

Re: How to build a main menu using the navigation block

by Deepak garg -

server->moodle->lang->en->moodle.php->$string['courses'] = 'set your own string'

Good Luck.........

 

 

In reply to Deepak garg

Re: How to build a main menu using the navigation block

by Dolly Miglani -

can we change the sorting order of main menu block

In reply to Dolly Miglani

Re: How to build a main menu using the navigation block

by Dolly Miglani -

shall we hide main menu block on some pages

In reply to Dolly Miglani

Re: How to add a menu link in navigation after installing hello word plugin

by Mohammad Tanzeel -

Hi 


I have installed local_greet plugin, But I want to show the content of plugin.

For displaying the content we need to add a menu link.

How can we do, also find my installed plugin with attached.


Thanks

Tanzeel