Customizing content depending on role

Customizing content depending on role

by Arthur Lui -
Number of replies: 8
I'm quite new to Moodle, and have been digging through the documentation for hours without luck.

I want each user's front page (when they log in) to show different content, based on their Role. How do I achieve this? Using has_capability()?

Thanks for any help you can offer.

Average of ratings: -
In reply to Arthur Lui

Re: Customizing content depending on role

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers

Maybe get_user_roles() is a better option, as has_capability() checks for specific permissions, rather than having a particular role.

Saludos. Iñaki.

In reply to Iñaki Arenaza

Re: Customizing content depending on role

by Arthur Lui -
Hmm, I'm having trouble using get_user_roles() working.

From the API, we see:
array get_user_roles (object $context, [int $userid = 0], [view $checkparentcontexts = true], [ $order = 'c.contextlevel DESC, r.sortorder ASC'], [ $view = false])

I'm unsure of how to define the context, as an object. I figured it would be defined as an integer (id from context table or something).

I've been trying:
$roles = get_user_roles( _______ , $USER->id);

I need this for the main index.php page after the user logs in.

Thanks for any help you can offer.

In reply to Arthur Lui

Re: Customizing content depending on role

by David Willington -
Hi Arthur

I think get_user_roles() takes a context object, not a string or integer, so you need something like

$context_instance=get_context_instance(CONTEXT_COURSE, $course->id);
$teachers = get_role_users(3,$context_instance);

Actually I've just spotted that I've used get_role_users() instead, but I assume that they both want a context object rather than an integer or string

Hope this helps.

David
In reply to David Willington

Re: Customizing content depending on role

by Arthur Lui -
I tried:

$context_instance = get_context_instance(CONTEXT_USER, $USER->id);
$rolesvar = get_user_roles($context_instance,$USER->id);

and,

$user = get_record("user", "id", $user);
$context_instance = get_context_instance(CONTEXT_USER, $user->id);
$rolesvar = get_user_roles($context_instance,$USER->id);

but the error message I get on /index.php is:

This is an unknown context () in get_parent_contexts!

I like the exclamation point on error messages by the way, it makes debugging that much more fun.

Thanks for any help you can provide.


In reply to Arthur Lui

Re: Customizing content depending on role

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
You don't want the user context (that's things like user activity reports etc).

Where will you be assigning users these roles? At the system level (thereby giving them that role for all courses etc)? If so then:

$context = get_context_instance(CONTEXT_SYSTEM);

If you are assigning the roles at the Frontpage course only, then use:

$context = get_context_instance(CONTEXT_COURSE, SITEID);

In reply to Arthur Lui

Re: Customizing content depending on role

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Sounds like a nice idea for a filter

<span role="teacher"> blah blah blah</span>

<span role="student"> blah blah blab</span>
In reply to Martin Dougiamas

Re: Customizing content depending on role

by Daniel Auger -
Wish I knew how to program this, but it would be great if Moodle could show content from a different "course" (calendar, announcements, blocks) depending on the user's role. We have 3 campuses and we would like to use Moodle as our Intranet home page and push information based on what campus a user is from.

Daniel
In reply to Daniel Auger

Re: Customizing content depending on role

by Samuli Karevaara -
That sounds like it would be easier to implement as three separate courses. Then maybe throw the user to by default to the "info course" for his/her campus after login.