Display course category name in a course and under course category

Display course category name in a course and under course category

by Matthias Holzer -
Number of replies: 18

Hello,  

 i've a problem with the issue in the topic.

Here is one example for the structor:

Hochschule (category) -> Informatik&Mathematik (undercategory) -> Dr. Max Mustermann (underundercategory) -> Course 01 (course)

 Here is what i want to do.

I want to show the name "Informatik&Mathematik" in the undercategory Dr. Max Mustermann and in the Course 01.

 I've got something but it didn't really work...

Here the code:

global $DB; if ($COURSE->id != SITEID) {

// get Categoryinformation 

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

// categoryparent

$parentcatids = explode("/",trim( $category->path, "/"));

//get the name of the parent category

$sql = "SELECT id, name FROM {course_categories} WHERE id in (".implode(",", $parentcatids).")"; $parentcatsnames = $DB->get_records_sql($sql);

 //0 = parent or 1 = undercategory

$headline = $parentcatsnames[$parentcatids[0]];

}

  

But i can't get it to work =/

 Some ideas?

Average of ratings: -
In reply to Matthias Holzer

Re: Display course category name in a course and under course category

by Miriam Laidlaw -
Picture of Plugin developers

I've been trying to do something similar.

By using this:

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

I can get the category that the course is in to show, but I can't figure out how to access further up the tree.

So if I have a course:

Faculty of Social & Health Sciences (top level category) > Education (sub-category) > Education Studies (sub-sub-category) > EDUC8304: Education 101 (course)

On the course page using the above code I can get the category "Education Studies" to show.

In reply to Miriam Laidlaw

Re: Display course category name in a course and under course category

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

Hi Miriam,

I'll try to look at this again in the next day or so and come up with something if I can, but in the first instance I think adding the line

echo $category->parent

would display a list of the id numbers of the parent categories. I'm thinking that if you explode this into an array (instead of echoing them), you should be able to make use of the array to do whatever you need to do with the parent categories.

I haven't actually tried this out yet, but as I said, i will try to get chance to take a look later today or in the next day or so - but that might be a starter idea for you in the meantime?

Richard

In reply to Richard Oelmann

Re: Display course category name in a course and under course category

by Matthias Holzer -

Hey,

thanks für the replay.

I got the result over the path ($PAGE->category->path) which every page has.

In my case i had to design the theme for a undercategory and everything within it. Whats why i search for categoryid[2] because thats there i'm. But i'm sure if someone needs it universal, it would be easy to extent.

 

$DB;

$categoryid = explode("/", $PAGE->category->path);

if(!$categoryid[2])

$fakname->name = "University";

else

$fakname = $DB->get_record('course_categories', array("id" => $categoryid[2]));

 

Hope that helps people that want to do it too.

In reply to Matthias Holzer

Re: Display course category name in a course and under course category

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

I don't think it needs all that code, I suspect it is something simple.

For example. If you add a new field in USER profile and call the shortname 'mailbox' you can add this into your theme's layout file as...

<?php echo $USER->profile['mailbox'] ;?>

and the mailbox URL which is the purpose of that field shortname shows up.

So adding a course category should be something similar one would think wouldn't it?

In reply to Mary Evans

Re: Display course category name in a course and under course category

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

Hi Mary,

I think the issue is that it is straightforward to put the course's direct category in, but Miriam and others seem to want/need to be altering the way parent categories display and these are not (I think) a simple call from the database as they are stored as one field - but my post above was wrong - its in 'path' not 'parent' - as the /46/15/16/33/8 below, where 8 is the direct category the course sits in

8 Sandbox Pages     1 33 1730000 18 1 1 0 5 /46/15/16/33/8

More than happy to be shown that I've missed something and its as simple as you hope smile

 

Richard

In reply to Richard Oelmann

Re: Display course category name in a course and under course category

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

Hi Richard,

I know that if you add <?php echo $COURSE->category ;?> in a page the id is displayed. So why is it not possible to associate the course_categories ID to course_categories NAME and get it to PRINT? That's what annoys me! LOL

I will go an consult the ORACLE - and to think I have a degree in this area! Goes to show how easy it is to lose touch with technology.

Thanks

Mary

In reply to Mary Evans

Re: Display course category name in a course and under course category

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

Hi Mary,

You can - with a bit of fiddling - that's what Miriam's original post does, but it seems you have to do it by calling the database - although looking at Matthias' post you could probably avoid that using $PAGE->category->name?

But I took the challenge to be wanting to display one of the levels in a situation where your course is set up nested in several levels of sub categories, but only wanting to display say the 2nd sublevel category e.g.

Category1 (Faculty) > SubCategory (School) > Sub-SubCategory (Department) > Sub-Sub-SubCategory (Programme) > Course

But you want to display the Department as part of the page header - How do you extract that level of the category path to display it?

It maybe that Matthias' post has solved what Miriam needed as well, but I went ahead as a bit of a learning process for myself with mysql and calling the database etc. and came up with the following if its useful to anyone. I was not actually applying this code into a working theme (I just trialled it in base general.php layout file) so it is still full of test 'echo' instructions, but I have left those in so that anyone wanting to make use of it can adapt it for themselves.

<?php
    global $DB;   //makes sure the database is available
    $category = $DB->get_record('course_categories',array('id'=>$COURSE->category));    //gets the database record from the course_categories table for the active course
    echo $category->name.":".$category->path."<br />";    //for testing only - prints the course category and the category path ids, that is the database id for each of the parent categories where there are several levels of sub-categories
    $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
    $countcats=sizeof($cats);    // if needed - finds the number of sub-level categories
    echo ("Depth of categories:".$countcats);    // for testing only - prints the number of sub-levels
    for($counter=0;$counter<$countcats;$counter++){    // this loops through all the sublevels - if you only want a specific sublevel then just set $counter at a fixed number, e.g. if your moodle has a faculty category, school subcategory, department sub-subcategory (and possibly others) but you only want to display the department then you would set $counter to 3
        $catname = $DB->get_record("course_categories", array("id" => $cats[$counter]) );    //gets the database record for the course_category with the id number given by that position in the array of the category path
        echo "<br />category ".$cats[$counter]." = ".$catname->name;    //prints the subcategory name for the appropriate level in the category path
    }    //ends the loop
?>

 

and the slightly cut down finished version

 

<?php
    global $DB;   //makes sure the database is available
    $category = $DB->get_record('course_categories',array('id'=>$COURSE->category));    //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=3;    // 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
?>

Hope that might be useful to someone - but if not, well, I've learnt/practiced a bit about accessing the database from inside Moodle smile

Richard

Average of ratings: Useful (4)
In reply to Richard Oelmann

Re: Display course category name in a course and under course category

by Miriam Laidlaw -
Picture of Plugin developers

Thank you, Richard! Thank you thank you!

This code worked to get the first level category showing, even when the course is nestled within sub-categories.

<?php
global $DB;   //makes sure the database is available
$category = $DB->get_record('course_categories',array('id'=>$COURSE->category));    //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;
?>

Virtual chocolate fish for you!

chocolate fish

Average of ratings: Useful (1)
In reply to Richard Oelmann

Re: Display course category name in a course and under course category

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

Just had time to read all that's been happening in this thread. Was at Schools Sports Day yesterday (Wednesday)!  Then watching episodes of SILK on catchup TV smile

WOW what a mammoth task this turned out to be. And yet I still find it quite bizarre that one has to go all around the houses just to get at a specific category.

Thanks for all the hard work.

In reply to Richard Oelmann

Re: Display course category name in a course and under course category

by Matthias Holzer -

Hello,

correct me if i'm wrong.. but you could just change the number in the array $categoryid[] and get the same result, wouldn't you?

$fakname = $DB->get_record('course_categories'array("id" => $categoryid[2]));

Because the result is exactly the same...

The echo would be like that.

<?php
echo $fakname->name;
?>

 

I figured, because i had a solution very simular to yours but i tryed to minimize the db connections. Because i make the Design for a University, and it could be that a few thousend people are connecting at the same time. 

If i'm wrong could you explain why? ;)

Regards, r00t

In reply to Matthias Holzer

Re: Display course category name in a course and under course category

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

Hi Matthias,

No I don't think its the same (although I could be wrong).

The way your solution works I think you would have to know the category id for the category you want to display ie $categoryid[n] - which you would in some cases and as you say would then have significantly fewer database calls.

The way I set up for my solution was to find out the category which is n steps deep in the category path - and so may have a different $categoryid each time

e.g. a simplified extract of my course_catetories table might look something like this

categoryid category name category path
 1  Faculty of Education  
 2  Faculty of Arts  
 3  Secondary ITT  /1
 4  Secondary Science  /1/3
 5  Primary ITT  /1
 6  Curriculum 7-11  /1/5

 My understanding is that with your solution, I could set $categoryid[1] to always display 'Faculty of Education' or $category[2] to always display 'Faculty of Arts' and so on - whatever the actual pattern of categories/sub categories. And this may be exactly what is wanted in some circumstances - in which case, as you say, your code would be the better one to use as it minimises database calls.

My solution - which was done purely for my learning and not for my practical application, although I believe Miriam has found it worked for her - is to be able to display the nth step in the category path. For instance if I have 2 courses - 'Practical lab work' in the Secondary Science category and Teaching IT in the Curriculum 7-11 category, the breadcrumbs for these would look like:

My Home>Faculty of Education>Secondary ITT>Secondary Science>Practical Lab Work

My Home>Faculty of Education>Primary IT>Curriculum 7-11>Teaching IT

If, in my code, the $depth is set to 2 then the $category name (possibly as part of the header being displayed) would, in the first example, display 'Secondary ITT'  and would do so for any other courses, however many sub-categories deep within the Secondary ITT department (My Home would be $depth=0 etc), while in the second example it would display 'Primary IT' for this course and any others in the Primary ITT category and further sub-categories.

So the reason for the extra database calls is to determine and then breakdown the path of categories for the course - stored as a single string in the table as above - and then to find the name of the nth category in that path, which could have a different $categoryid for different courses.

That at least is my understanding of the differences between the two solutions smile

HTH

Richard

In reply to Richard Oelmann

Re: Display course category name in a course and under course category

by Matthias Holzer -

Hello,

ah ok.. know i see what you mean.

Actually our solutions are the same.

Maybe i could have named my variable better.

$categoryid = explode("/"$PAGE->category->path);

In categoryid i also defide the path, so i've and 1/2/3/4 as well, and in my think i've to display the secound. 

But i think both solutions are ok =)

 

Regards, r00t

In reply to Matthias Holzer

Re: Display course category name in a course and under course category

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

In that case Matthias, yes I think both solutions are effectively the same (although yours is probably neater smile ) - I missed something when looking at yours because categoryid is one of the fields in the table I was looking at so I thought you were refering to that smile

Richard

In reply to Richard Oelmann

This forum post has been removed

The content of this forum post has been removed and can no longer be accessed.
In reply to Deleted user

Re: Display course category name in a course and under course category

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

Hi Monia,

The code goes in the position you want it to appear in the layout files (I think formal white has both frontpage.php and general.php but I'm on my android tablet at the moment and dont have the code available)

In your case, based on what you were asking in the other thread, it would probably replace the navbar code.

But bear in mind that the code above was a 'proof of concept' rather than a specific navbar/breadcrumb replacement, so you may need to tinker with it to get exactly what you want! Anyway, I hope it gets you towards where you want it to be smile

Richard

In reply to Richard Oelmann

This forum post has been removed

The content of this forum post has been removed and can no longer be accessed.
In reply to Richard Oelmann

Re: Display course category name in a course and under course category

by Miguel Rubio -

Thank you very much! I was trying and trying to do a query to a custom table and couldn't do that after almost a week.

You open the heaven's door for me...

Thank you again.

Miguel

In reply to Miguel Rubio

Re: Display course category name in a course and under course category

by Judy Hsu -

It really sounds crazy that just to hide one or two items in the breadcrumb we have to dig into the code levels to do this. Is there a "feature request' ticket somewhere in the Moodle bug tracker that talks about implementing this so that admin users can easily customize this at the UI level? Please let me know and I'm going to vote for that ticket. Thanks!

For my case, I JUST want to hide the "course category" in the breadcrumb and that's it. For example:

In Moodle 1.9 we used to see this type of breadcrumb:

CCC > PM (course name) > SCORMs/AICCs > Course Content

Now on Moodle 2.4 we are seeing this breadcrumb:

Home > My courses > [Course Category] > PM (course name) > / > Course Content

1. Is there a easy way for me to just remove that [Course Category] from the breadcrumb in 2.4?

2. Why is there an empty breadcrumb for the SCORMs/AICCs?? Is there a easy way to fix this (hopefully just remove it all together instead of showing '/', or at least let's put back the SCORMs/AICCs in the breadcrumb.

And yes this is a course using SCORM package. Thanks again!