Search This Blog

Tuesday, January 17, 2012

Customize the navigation block and user permissions

Here I've made two changes to Moodle's navigation block. Both of these changes concern the way user courses are displayed.

  • I like the way that "my courses" displays only the courses you are enrolled in
  • I like how "Courses" displays categories......but it shows courses you aren't even enrolled in.
So the purpose of this blog post is mostly to hide "My Courses" and only show "Courses", while modifying the latter to only show user's courses.

First, you need to enable "Courses" to actually show up on the navigation block.
Inside your Moodle site, you need administrator access.
1)  Admin ->Appearance->Navigation
2)  Show course categories = yes
3)  Show all courses = yes
4)  save preferences
Now we also need access to our core moodle server, because we need to make changes to the code. 
Inside this file:   <yourmoodleroot>/lib/navigationlib.php

1) Go to line 1290 where you see this current code:
foreach ($this->rootnodes as $node) 
{
            // Dont remove the home node
            if ($node->key !== 'home' && !$node->has_children()) {
                $node->remove();
            }
}
2) and change it to:
foreach ($this->rootnodes as $node) 
{
            // Dont remove the home node
            if (($node->key !== 'home' && !$node->has_children()) || $node->key === 'mycourses') {
                $node->remove();
            }
}
3) This will hide the "my courses" link in the navigation block.

Now we need to modify "Courses" to only show the category/course list that the user is enrolled in.
Inside this file:

1) Go to around line 1523, inside the function called  "add_category(stdClass $category, navigation_node $parent)"   where you see this code:

if (has_capability('moodle/category:viewhiddencategories', get_system_content()))

{
                $categorynode->hidden= true;
            } else {
                $categorynode->display = false;
}

2) and change it to:

if (has_capability('moodle/category:viewhiddencategories', get_context_instance(CONTEXT_COURSECAT, $category->id)))
{
                $categorynode->display = true;
            } else {
                $categorynode->display = false;
}

NOTE: changing the "hidden" variable will make your hidden categories display with normal colored text, instead of that gray color

If you would like your hidden courses to also display with black text (this is for later in the tutorial), 
1) Go to around line 2177 and change this code:
$coursenode->hidden = (!$course->visible);

2) to this code:
$coursenode->hidden = false; 


3) Add this line next:
if(!$course->visible) { $coursenode->display = false; }

====================================================================
Okay so our core changes to the code are done. 
Now we need to have fun with roles and permissions!

An average user on my site is using the role "student", so the following example will be changing the role of student so they can only see the courses/categories they are currently enrolled in.
  1. Go to Admin -> Add/Edit courses
  2. Find the category you want
  3. Click the "eye icon" to make this category hidden
  4. Now click the category. You should see a list of courses or sub-categories......so while you are on this page, go to your settings block. You should see a link called "Assign Roles"
  5. Assign your user to the role "Student"

Now let's modify the role of  "Student" a little bit
  1. ON THE SAME PAGE (category administration), go to the settings block -> permissions
  2. Choose the role of "student"
  3. "View hidden categories"  should be set to true
So deep breath:  now on the Navigation block, the "Courses" link should only display the categories in which that user has been assigned to. 
==================================================================

Currently a normal "student" user in our case, can go tot the Navigation Block, and click on "Courses" to see a drop-down list of categories they are assigned to. 
That user can also see all the courses within that category, whether they are enrolled or not.

To make a user only see their courses:
  1. First enroll that student in a course via the enrollment method your prefer
  2. Then go the course and click the "eye icon" to make this couses hidden.
  3. On your chosen category administration page, go to settings block -> Permissions
  4. Choose the role of "student"
  5. "View hidden courses"  should be set to true
Second deep breath:  now on the Navigation block, the "Courses" link will show only the CATEGORIES AND COURSES the student is currently enrolled in. 

It was a lot of work, and I feel like I hacked the moodle core a little.......but now I finally have a navigation block I can use properly :)

4 comments:

  1. Hi,

    Thank you for the lovely post.
    I was wondering, if you can help me to slove my problem!

    My problem is:
    I have created a new navigation block. So I want my navigation block will be displayed instead of core navigation block. How can I do that?

    ReplyDelete
  2. Hello bdtraveler - I think there are two parts to your question.

    First part: how to hide the core navigation block.
    -- you'll need administrator priviledges
    -- Go to: Site administration>Plugins>Blocks>Manage Blocks
    -- Scroll down to the Navigation block and click on the eyeball icon to hide it.

    Second part: how to show your own custom block
    -- Go to the front page
    -- Turn editing on
    -- With the "Add a block" widget, select your custom block
    -- Position wherever you like!
    -- Within the block's settings, you can choose if it should display throughout the site

    I hope that answered your question :)

    ReplyDelete
  3. Extreme, excessive and extravagant effort put by your side on blog posting on net. I believe your blog is the wonderful blog put by your side on net. Thanks you very much for a great posting.

    palinfocom.net/moodle-customization-development.php

    ReplyDelete
  4. Excellent help! Thank you so much!

    ReplyDelete