Get current User's role per course

Get current User's role per course

- Eric Brown の投稿
返信数: 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

評点平均:Useful (1)
Eric Brown への返信

Re: Get current User's role per course

- Nadav Kavalerchik の投稿
画像 Core developers 画像 Plugin developers 画像 Testers 画像 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/>";
}

Nadav Kavalerchik への返信

Re: Get current User's role per course

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

Phil Australia への返信

Re: Get current User's role per course

- 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 />';
}
}
Eric Brown への返信

Re: Get current User's role per course

- 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!!
Eric Brown への返信

Re: Get current User's role per course

- Tim Hunt の投稿
画像 Core developers 画像 Documentation writers 画像 Particularly helpful Moodlers 画像 Peer reviewers 画像 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.
Tim Hunt への返信

Re: Get current User's role per course

- 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
Tim Hunt への返信

Re: Get current User's role per course

- 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
Amy Groshek への返信

Re: Get current User's role per course

- Tim Hunt の投稿
画像 Core developers 画像 Documentation writers 画像 Particularly helpful Moodlers 画像 Peer reviewers 画像 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.
Tim Hunt への返信

Re: Get current User's role per course

- 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?

tony chesney への返信

Re: Get current User's role per course

- Tim Hunt の投稿
画像 Core developers 画像 Documentation writers 画像 Particularly helpful Moodlers 画像 Peer reviewers 画像 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?
Tim Hunt への返信

Re: Get current User's role per course

- 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!

Eugene Matusov への返信

Re: Get current User's role per course

- Tim Hunt の投稿
画像 Core developers 画像 Documentation writers 画像 Particularly helpful Moodlers 画像 Peer reviewers 画像 Plugin developers

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

Tim Hunt への返信

Re: Get current User's role per course

- 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

Tim Hunt への返信

Re: Get current User's role per course

- 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.

Eugene Matusov への返信

Re: Get current User's role per course

- 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).

 

Eugene Matusov への返信

Re: Get current User's role per course

- 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

Rosario Carcò への返信

Re: Get current User's role per course

- sam marshall の投稿
画像 Core developers 画像 Peer reviewers 画像 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 笑顔

--sam

Eric Brown への返信

Re: Get current User's role per course

- 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;

Alfie Punnoose への返信

Re: Get current User's role per course

- 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;"