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.
Maybe get_user_roles() is a better option, as has_capability() checks for specific permissions, rather than having a particular role.
Saludos. Iñaki.
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.
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.
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
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
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.
$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.
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);
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);
Sounds like a nice idea for a filter
<span role="teacher"> blah blah blah</span>
<span role="student"> blah blah blab</span>
<span role="teacher"> blah blah blah</span>
<span role="student"> blah blah blab</span>
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
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.