My Courses custom dropdown menu for 2.6

My Courses custom dropdown menu for 2.6

by Jack Challenger -
Number of replies: 7

Hi,

I cannot seem to get a My Courses dropdown menu working in 2.6.

I have based my work till now on Extending the theme custom menu - the Courses dropdown works fine, but no luck displaying only My Courses. 

Has anyone perhaps implemented this successfully in Moodle 2.6?

Here is what I have so far:

(apologies, I cannot seem to get the code formatted properly in the forum)

 

<?php


class theme_testtheme_core_renderer extends core_renderer {

protected function render_custom_menu(custom_menu $menu) {
global $CFG;

require_once($CFG->dirroot.'/course/lib.php');

$branch = $menu->add(get_string('courses', 'theme_testtheme'), null, null, 2);

$categorytree = get_course_category_tree();
foreach ($categorytree as $category) {
$this->add_category_to_custommenu($branch, $category);
}

return parent::render_custom_menu($menu);

$this->render_mycourses_custom_menu($menu, 12000) ;
}

protected function add_category_to_custommenu(custom_menu_item $parent, stdClass $category) {
$branch = $parent->add($category->name, new moodle_url('/course/category.php', array('id' => $category->id)));
if (!empty($category->categories)) {
foreach ($category->categories as $subcategory) {
$this->add_category_to_custommenu($branch, $subcategory);
}
}
if (!empty($category->courses)) {
foreach ($category->courses as $course) {
$branch->add($course->shortname, new moodle_url('/course/view.php', array('id' => $course->id)), $course->fullname);
}
}
}


protected function render_mycourses_custom_menu(custom_menu_item $menu, $position) {

global $CFG;
require_once($CFG->dirroot.'/course/lib.php');

// Moodle 2.4 doesn't appear to support $mycourses = $this->page->navigation->get('mycourses'); so

if (isloggedin() && !isguestuser() && $mycourses = enrol_get_my_courses(NULL, 'visible DESC, fullname ASC')) { //which does work

$branchlabel = get_string('mycourses') ;
$branchurl = new moodle_url('/course/index.php');
$branchtitle = $branchlabel;
$branchsort = 8000 ; // lower numbers = higher priority e.g. move this item to the left on the Custom Menu
$branch = $menu->add($branchlabel, $branchurl, $branchtitle, $branchsort);

foreach ($mycourses as $mycourse) {
$branch->add($mycourse->shortname, new moodle_url('/course/view.php', array('id' => $mycourse->id)), $mycourse->fullname);
}
}

}

}
Thanks!
Average of ratings: -
In reply to Jack Challenger

Re: My Courses custom dropdown menu for 2.6

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Is 'testtheme' based on 'base' or 'bootstrapbase'? Could you be a bit more specific than 'cannot get working' please.

In reply to Gareth J Barnard

Re: My Courses custom dropdown menu for 2.6

by Jack Challenger -

Hi Gareth,

My theme (testtheme) is based on standard (    $THEME->parents = array('standard', 'base');    )

When I follow the article Extending the theme custom menu , the menu works perfectly in my theme, i.e. displays the Courses dropdown with Categories and all courses in it.

But then I try to add the extra bit in that tutorial, 'Adding the My Courses branch', and it does not change anything. I still only have the 'Courses' menu item in the menu, and not the 'My Courses' item with only a list of enrolled courses.

Thank you very much, please ask if you need more information!

 

In reply to Jack Challenger

Re: My Courses custom dropdown menu for 2.6

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Thanks Jack for the information, I'll try when I get a moment!

In reply to Gareth J Barnard

Re: My Courses custom dropdown menu for 2.6

by Jack Challenger -

Thanks it is hugely appreciated!

In reply to Jack Challenger

Re: My Courses custom dropdown menu for 2.6

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Silly question.  Is the user you are logged in as enrolled on any courses?

In reply to Gareth J Barnard

Re: My Courses custom dropdown menu for 2.6

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Just spotted this coding fault:

 
return parent::render_custom_menu($menu);

$this->render_mycourses_custom_menu($menu, 12000);

Where the return is before the call to 'render_mycourses_custom_menu' and therefore it will not be called.  So needs to be:

$this->render_mycourses_custom_menu($menu, 12000);

return parent::render_custom_menu($menu);

Cheers, 

Gareth