Customizing content depending on role

Customizing content depending on role

дэргэд Arthur Lui -
Хариу нийтлэлийн тоо: 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.

Үнэлгээний дундаж: -
Arthur Lui-н хариуд

Re: Customizing content depending on role

дэргэд Iñaki Arenaza -
Core developers зураг Documentation writers зураг Peer reviewers зураг 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.

Iñaki Arenaza-н хариуд

Re: Customizing content depending on role

дэргэд 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.

Arthur Lui-н хариуд

Re: Customizing content depending on role

дэргэд 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
David Willington-н хариуд

Re: Customizing content depending on role

дэргэд 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.


Arthur Lui-н хариуд

Re: Customizing content depending on role

дэргэд Martin Dougiamas -
Core developers зураг Documentation writers зураг Moodle HQ зураг Particularly helpful Moodlers зураг Plugin developers зураг 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);

Arthur Lui-н хариуд

Re: Customizing content depending on role

дэргэд Martin Dougiamas -
Core developers зураг Documentation writers зураг Moodle HQ зураг Particularly helpful Moodlers зураг Plugin developers зураг Testers зураг
Sounds like a nice idea for a filter

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

<span role="student"> blah blah blab</span>
Martin Dougiamas-н хариуд

Re: Customizing content depending on role

дэргэд 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