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
בתגובה ל: Daniel Auger

Re: Customizing content depending on role

על ידי 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.