Get current User's role per course

Get current User's role per course

by Eric Brown -
Number of replies: 22
I'm trying to do what would seem fairly easy.
On the course/view.php page, I need to figure out (within a stickyblock I've created), if the currently logged in user (the one looking at the course) is a Teacher or a Student.

Does anyone know either a function, or a snippet that I could use to tell if the user viewing a course is a 'Student' or 'Instructor'. I've looked at the $USER object, and it doesn't appear to be a property of that class.


Any help is *greatly* appreciated.

Thanks,
Eric

Average of ratings: Useful (1)
In reply to Eric Brown

Re: Get current User's role per course

by Nadav Kavalerchik -
Picture of Core developers Picture of Plugin developers Picture of Testers Picture of Translators
here is what i am using from time to time (maybe there are better ways?)

global $COURSE, $USER;

$context = get_context_instance(CONTEXT_COURSE,$COURSE->id);

if (has_capability('moodle/legacy:student', $context, $USER->id, false) ) {
echo "is Student<br/>";
}
if (has_capability('moodle/legacy:teacher', $context, $USER->id, false) ) {
echo "is Assitent Teacher<br/>";
}
if (has_capability('moodle/legacy:editingteacher', $context, $USER->id, false)) {
echo "is Teacher<br/>";
}
if (has_capability('moodle/legacy:admin', $context, $USER->id, false)) {
echo "is ADMIN<br/>";
}

Average of ratings: Useful (1)
In reply to Nadav Kavalerchik

Re: Get current User's role per course

by Phil Australia -
To get the course Id from the session simply use:
$SESSION->cal_course_referer

In reply to Phil Australia

Re: Get current User's role per course

by Phil Australia -
List of role ids and names for the current user and the current course:

$context = get_context_instance(CONTEXT_COURSE,$SESSION->cal_course_referer);

if ($roles = get_user_roles($context, $USER->id)) {
foreach ($roles as $role) {
echo $role->roleid.'<br />';
echo $role->name.'<br />';
}
}
Average of ratings: Useful (2)
In reply to Eric Brown

Re: Get current User's role per course

by Eric Brown -
Thank you Nadav.
I was very close to a similar solution using the get_role_users() function and checking the roleid.

Your idea is better because if for some crazy reason the roleid's change, mine would break and yours would still work.

Thanks for the response!!
In reply to Eric Brown

Re: Get current User's role per course

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
What are you actually trying to do? Bear in mind that it is perfectly possible (but unlikely) to have a Moodle site without the default Student and Teacher roles.

So, what really is the distinguishing feature of the two types of user you want to display different content to? Wouldn't it be better to check a real capability.
Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Get current User's role per course

by Red Morris -
I'm also wondering about how long the legacy will remain. The legacy roles have been around since 1.7 now. Will they continue in Moodle 2, or is that beign seen as a chance to clean shop? Something to concider when programming at this stage
In reply to Red Morris

Re: Get current User's role per course

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
We definitely want to get rid of them but we may run out of time before the 2.0 release.
In reply to Tim Hunt

Re: Get current User's role per course

by Amy Groshek -
So, Tim, what would be an example of determining this same thing by checking a real capability? Something like: gradereport/grader:view ?

Additionally, is there any way for me to call a JavaScript function in order to do this, or do I have to write a PHP intermediary (for my flash app)? I was just thinking that if I could determine whether the user had student or greater than student permissions, that would be all I needed...

Thanks for any pointers,
A
In reply to Amy Groshek

Re: Get current User's role per course

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Well what does your flash app do? Without knowing that it is hard to say which capability is most appropriate.

Also, there is not currently a way to do this in JavaScript - trying to enforce security on the client-side is essentially impossible. You need to check all permissions server-side. Again, it is hard to say mroe without knowing exactly what you are trying to do.
In reply to Tim Hunt

Re: Get current User's role per course

by tony chesney -

I also have problems with this concept. 

I want to check if a user is a student in a given context (a course).  I'd like to follow your advice Tim and use a capability test instead of

if (has_capability('moodle/legacy:student', $context, $USER->id, false) )

Let's say the set of student capabilities are a subset of those of a teacher, which seems quite likely;  that means I can't distinguish by testing for the presence of a capability.   

You would presumably advise me to test for the absence of a capability that is available to other roles.  Doesn't that mean I'd have to check all those roles, and even then someone could create a new role that also lacked that capability?

In reply to tony chesney

Re: Get current User's role per course

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Well, you may be straying into the area where the current roles system is not really the right answer. You are not asking about permissions, but about enrolment. There are some plans to make that more explicit in Moodle 2.0. Development:Enrolment_rewrite_and_role_tweaks_proposal

In the mean-time you have to find some way of getting the result you want without doing anything too evil.

I am afraid that doing has_capability('moodle/legacy:student', ...) is very evil.

You have not explained what you are really trying to do, so I can't really suggest something better for your situation. What is the feature you are trying to implement?
In reply to Tim Hunt

Re: Get current User's role per course

by Eugene Matusov -

The moodle/legacy solution does not work in Moodle 2.2 anymore. Can somebody provide an alternative AS SOON AS POSSIBLE, please?!!!!

Thanks!

In reply to Eugene Matusov

Re: Get current User's role per course

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Yes. Look at the new *enrol* functions in Moodle 2.x. The propsal mentioned above was implemented.

In reply to Tim Hunt

Re: Get current User's role per course

by Ana Marjanovic-Shane -

Hi,I am also looking for a "simple" solution to make a decision on where to send a current user based on her/his role -- when s/he presses the  "submit" button in the "feedback activity".

I need to get the current user's role in the current context but the legacy solution does not seem to work.

What would you suggest?

Ana

In reply to Tim Hunt

Re: Get current User's role per course

by Eugene Matusov -

Thansk, Tim, for a lead. I found two possible 'enrol' functions:

1) 

course_enrolments($courseid, $roles = '') X-Ref
Get a list of users from the client server who are enrolled in a course

param: int $courseid The Course ID
param: string $roles Comma-separated list of role shortnames
return: array Array of usernames who are homed on the


2) user_enrolments($userid)

Which one should I use and how? Or should I use something else?

I do not know what I wrote below makes sense (I'm pretty much a newbee):

--------------

global $COURSE, $USER;

if (course_enrolments($courseid->id, $roles = 'student')==$userid->) {
echo "is Student<br/>";
}

-------------

Sorry, it is a nonsense. Can you correct me, please? Thanks, Eugene

PS Ana, as soon as Tim or somebody helps with checking current user's role, I think I can help you with your task.

In reply to Eugene Matusov

Re: Get current User's role per course

by Eugene Matusov -

Hello everybody--

I have found how to get User's role per course (tested):

--------------

$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);

if (!has_capability('moodle/course:viewhiddensections', $coursecontext)) {

echo "is Student<br/>";
}

-----------

Note: $course->id may not work for a specific moodle php page -- you need to figure out how to get the course id from the page (e.g., using feedback plugin, I had to get it in the following way: get_coursemodule_from_id('feedback', $id)->course).

 

In reply to Eugene Matusov

Re: Get current User's role per course

by Rosario Carcò -

Sorry but you are not testing if it is a student, you are testing the given capability. So you really have to deal with roles here. See in the thread called useful sql queries or similar to get an idea of how to deal with roles.

Rosario

In reply to Rosario Carcò

Re: Get current User's role per course

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

But testing if 'somebody is a student' is rubbish anyway. Fact is that in Moodle since 1.7 or so, there is no such thing as a student, there are just a collection of roles with different permissions. You could quite easily just delete the student role for a particular system, or change it dramatically so that whatever distinction is being drawn here doesn't matter.

In other words, compared to testing if somebody has a student role as one of their roles in a particular context, I think checking if they aren't allowed to view hidden activities is only equally hacky (and almost equally likely to work on most sites and behave appropriately on a few others or in theoretical situations).

Basically the right thing to do normally is to test for an appropriate capability, or if there isn't already one, then add a new one. If you're not doing the right thing, I think this wrong thing would be ok smile

--sam

Average of ratings: Useful (1)
In reply to sam marshall

Re: Get current User's role per course

by Jamal Aruna -

Hi Sam,

what if you test for a roleid  of 5 with regards to the course id in the role assignments table would that work ?

In reply to Eric Brown

Re: Get current User's role per course

by Alfie Punnoose -

You can either know the user role by checking all the roles a user is assigned in particular course, as follows.

$cContext = context_course::instance($COURSE->id); // global $COURSE

$isStudent = current(get_user_roles($cContext, $USER->id))->shortname=='student'? true : false; // instead of shortname you can also use roleid

Shortnames for the roles you want to check can be found at Site Administration->users->Permissions->Define roles (moodle/admin/roles/manage.php)

I am assuming that students are only assigned one role per course and hence I get the first role assignment record with 'current()'

 

or you could check the user capability to update a course (assuming that the student are not granted the capacity to update a course)

$isStudent = !has_capability ('moodle/course:update', $context) ? true : false;

In reply to Alfie Punnoose

Re: Get current User's role per course

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators
Please, don't do such things. As Sam pointed out above, asking for a role is conceptually wrong.
Average of ratings: Useful (2)
In reply to Alfie Punnoose

Re: Get current User's role per course

by Alfie Punnoose -

The previous steps that I mentioned only works at a Course level, but if you are outside a course and want to identify a user type, use the following: 

NB: There is nothing such as a user type in Moodle, so its you making the call according to the roles you have assigned the users. Here I assume that a user can be called a student if he/she is assigned a 'student' role anywhere in the system.

// user_has_role_assignment($userid, $roleid, $contextid = 0) // $contextid empty means any context

This returns true if the user have a student role in any context. 

user_has_role_assignment($USER->id,5) // $roleid == 5 for student role //inside functions declare "global $USER;"

 

 

Average of ratings: Useful (1)