Display parent / top level category name in header

Display parent / top level category name in header

by Miriam Laidlaw -
Number of replies: 15
Picture of Plugin developers

Hey everyone, playing catch up on posts here, but have a question of my own.

I have my Moodle Themes I'm developing in 2.2 and I'm trying to work out how to automatically echo the top level category name in the header of my theme.

I have found the following threads:

- http://moodle.org/mod/forum/discuss.php?d=42882 - Moodle 1.9 specific and I've tried the method outlined here, no luck.

- http://moodle.org/mod/forum/discuss.php?d=181702 - this question was also about 1.9, though it was ambiguous in the discussion and Mary replied very helpfully with some Moodle 2 stuff, but I just can't figure out how to override the renderer to show the category name. I've done plenty with overriding the renderer in the custom menu...

- http://moodle.org/mod/forum/discuss.php?d=181998 - is an copy of the previous post in a different forum.

So at the moment I have a mock-up:

Screenshot 1

Made using this code:

Screenshot 2

As you can see I have just written the "Department of Something Or Other" in manually as I play around. I want this to show the top level category that the course is in (not whichever sub-category it happens to be in).

If I come up with a solution before someone else, I will post it here.

Average of ratings: -
In reply to Miriam Laidlaw

Re: Display parent / top level category name in header

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

You need something like...

<?php echo $COURSE->course_category() ?>

However I need to check this out and don't have the time right now.

Best is to use phpMyAdmin and look inside the database and see what's there under mdl_course_category  or mdl_course.

EDIT:

I have just checked and it looks like you might be able to use...

<?php echo $COURSE->course_categories->id=1 ?>

but I may be wrong...lol

Mary

In reply to Mary Evans

Re: Display parent / top level category name in header

by Miriam Laidlaw -
Picture of Plugin developers

Hi Mary,

Thanks for your response. I'd also tried various combinations of <?php echo $COURSE->course_category ?> and <?php echo $COURSE->course_category() ?> etc and just ended up with everything blank below the header, or PHP error messages.

The <?php echo $COURSE->course_categories->id=1 ?> just seems to echo out the number 1 where I put it.

Will keep looking.

In reply to Miriam Laidlaw

Re: Display parent / top level category name in header

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

Hi Miriam,

I'm not sure, but perhaps Bas Brand's presentation from the iMoot might help point you in the right direction? The actual presentation was about using Google Analytics but part of that was creating a readable url from the breadcrumb, including the categories.

Bas' blogpost can be found at http://basbrands.nl/2012/04/18/google-analytics-with-moodle/

HTH

Richard

In reply to Richard Oelmann

Re: Display parent / top level category name in header

by Miriam Laidlaw -
Picture of Plugin developers

Hi Richard,

Ok, I've been messing around with that and by using:

            <?php
            global $DB;
            $category = $DB->get_record('course_categories',array('id'=>$COURSE->category));
            echo $category->name ?>

I was able to get the category name to show. I'm a bit wary about having a global statement like that in a theme, but it's the only way to get it to work how it is right now. I will keep tweaking. However, this only shows the category that the course is in. I want it to show the top level root category only.

This is what I have:

Theme category

Theme 1 is the course name

HRDNZ is the category it is in, but HRDNZ is inside a root category called Work, which is what I want it to display.

Again, I will keep tweaking. smile Thanks for showing me the blog, it has at least helped me get this far.

In reply to Miriam Laidlaw

Re: Display parent / top level category name in header

by Miriam Laidlaw -
Picture of Plugin developers

Still can't get this to display only the top level category, rather than the category that the course is directly in.

So I'm putting it away. Perhaps someone will come up with the solution sometime. Thanks for your suggestions, guys, they were helpful. smile

In reply to Miriam Laidlaw

Re: Display parent / top level category name in header

by Szilard Szabo -

In the course_categories table there is a column named path. This field shows the way from the current category up to its root category including all the parent's ids. For example if the category id is 123 and you can see the following in the path field: /14/72/123 then it means that the parent category id of the current category is 72, and its root category id is 14. The lenght of the path can be any. For example for a category which lives in a deeper level the path field can be like this: /14/72/123/456/1236/12545.

But in all case, the root category is the first number in this path string.

So you have to make the following to get the name of it:

<?php

global $DB;

$category = $DB->get_record('course_categories',array('id'=>$COURSE->category));

$path = explode('/',$category->path);

$root_category_id = $path[1];

$root_category = $DB->get_record('course_categories',array('id'=>$root_category_id));

echo $root_category->name

?> 

Hope it helps.

In reply to Szilard Szabo

Re: Display parent / top level category name in header

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Note that, once everthing has been initialised, you may be able to get this information from the $PAGE global. This has the advantage that it will not use extra DB queries.

In reply to Tim Hunt

Re: Display parent / top level category name in header

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Tim,

Can you give me an example to illustrate what you mean by ...

[quote]...once everthing has been initialised, you may be able to get this information from the $PAGE global...[/quote]

In reply to Mary Evans

Re: Display parent / top level category name in header

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Try

global $PAGE;

print_object($PAGE->category);

print_object($PAGE->categories);

If you get an error, then "everything" must not have been initialised yet in your script, so try moving that code later. (I think the necessary initialisation happens in require_login).

In reply to Tim Hunt

Re: Display parent / top level category name in header

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Yes!

<?php
global $PAGE;
echo $PAGE->category->name;
?>

Works for me!!!

In reply to Szilard Szabo

Re: Display parent / top level category name in header

by Miriam Laidlaw -
Picture of Plugin developers

Ah... I must have forgotten to post what we ended up with here... We had something else first, but it threw up exceptions when debugging was turned on or some such thing, so this seems to be ok even with debugging turned on.

We did:

<?php
global $DB; //makes sure the database is available
$category = $DB->get_record('course_categories',array('id'=>$COURSE->category));
if(empty($category)) {echo "Unitec eLearning Platform";
}else{ //gets the database record from the course_categories table for the active course
$cats=explode("/",$category->path); // splits the category path into an array so that each level in the categories is a different level in the array
$depth=1; // what depth of sub-category you want to display: Note this may need some error trapping to ensure there are that many levels of subcategories for the course - or setting a default value if not
$categorydepth = $DB->get_record("course_categories", array("id" => $cats[$depth]) ); //gets the database record for the course_category with the id number set by $depth position in the array of the category path for the active path
$categoryname = $categorydepth->name; //sets a variable name for the set depth of subcategory ready to be displayed as required
echo $categoryname;
}
?>
In reply to Szilard Szabo

Re: Display parent / top level category name in header

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

Not sure how/why/if this discussion got split from the original or why we ended up with several very similar discussions here, but this was also the subject of (and solved in) https://moodle.org/mod/forum/discuss.php?d=205451

Richard

In reply to Richard Oelmann

Re: Display parent / top level category name in header

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Rich,

No, this was one started by Miriam for a specific thing...but we also had the other one you linked to at the same time. I think we moved from this to that other one from memory.

I know...it's confusing...such is life in the fast lane! LOL

BTW I've deleted the older post. smile

Cheers

Mary

In reply to Mary Evans

Re: Display parent / top level category name in header

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

I don't know how you and Miriam keep your head straight with all of this Mary :D I take my hat off to you both every time!

Thanks for all your hard work - in the moderating of the forum as well as everything you do with the themes themselves!!!

In reply to Richard Oelmann

Re: Display parent / top level category name in header

by Miriam Laidlaw -
Picture of Plugin developers

Ah! I thought the solution had ended up somewhere, which is why I couldn't understand why I couldn't see it in this thread.

I don't think I'm designed for life in the fast lane. I just get run over. wink

roadkill