Category names in header

Category names in header

by Mark Berthelemy -
Number of replies: 6
Hi everyone,

I'm putting together a Moodle site for a primary (ages 4-11) school, and want to be able to extract the top level category names and their URLs in order to display them as permanent tabs in the header.

I can do the styling (I'll be using css-styled lists), but I'm not yet sure how to get the raw data, the name and URL, out of the database.

I was going to start with the code that already displays the category names on other pages, but, before I do, I thought I'd better check to see if anyone else has done this first.

Thanks in advance,

Mark

Average of ratings: -
In reply to Mark Berthelemy

Re: Category names in header

by P H -

I'm looking for the same functions. In the header, I require links to things that normally are located in the blocks. How do I fix this?

Mark, did you find a solution?

Pieter

In reply to P H

Re: Category names in header

by Mark Berthelemy -
Hi Pieter,

No solution yet. But I'm working on it. I think it's something to do with the get_records_menu function...

Mark
In reply to P H

Re: Category names in header

by Mark Berthelemy -
Hi Pieter,

I've worked it out.

If you add the following into the header.html file of your theme, it will display a list of the top level categories as links.

[div id='navigation']
<?php
$menu=get_records_menu('course_categories', 'parent', '*', 'name ASC', 'id,name');
if ($menu) {
print "<ul>";
foreach ($menu as $id => $name) {
print'<li><a href="'.$CFG->wwwroot.'/course/category.php?id='.$id.'">';
p($name);
print'</a></li>';
}
print '</ul>';
} else {
print "No menu item"; }
?>
[/div]

(replace the [] with greater than / less than signs - even with plain text format these were not displaying in the forum post.)

I've put it in a div to aid CSS styling.

I think the if / then / else statement could be removed  - that was just there for some debugging.

Mark
In reply to Mark Berthelemy

Re: Category names in header

by Jeremy FitzPatrick -
I'm not sure if you've solved this already but I show the course category in the header in a couple of our sites. This code goes in header.html at the point you wish to display it.

<?php
    if($course->category != NULL) {
        $category = get_record("course_categories", "id", $course->category);
        echo($category->name);
    }
?>

Take a look at http://rototuna.elearning.ac.nz/course/view.php?id=27 this is also a primary school by the way.

Jeremy
In reply to Jeremy FitzPatrick

Re: Category names in header

by Mark Berthelemy -
Thanks Jeremy,

That looks useful. As you'll see from an earlier, I've solved the initial problem, which was to display all the top-level categories in a permanent menu.

All the best,

Mark
In reply to Mark Berthelemy

Re: Category names in header

by Deon Metelski -
Hi Mark,

I see that you came up with a solution. I am looking to do something similar but want to display the courses and links to the class grades in the header after a user logs in only for them. Any hints on how to do this? Looking at the get_records_menu but can't figure it out. Not really a programmer but appreciate everyone on here who is wide eyes.

Thanks,
Deon