2.3.2: Getting a user's role

2.3.2: Getting a user's role

by Richard Crawford -
Number of replies: 7

What's the best way to determine whether a given user has the student role (and only the student role) in a course? I could use the get_user_roles_in_course() function like this:

$role = get_user_roles_in_course($id,$course->id);

if ($role == 'student') echo "Is a student";

but that seems rather clunky to me.

The goal is to display text to an admin user on a user:view page if the user is a student, and different text if they are not.

Average of ratings: -
In reply to Richard Crawford

Re: 2.3.2: Getting a user's role

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Best, most 'Moodley' solution would be the following:

create a new block plugin, which should be added to the front page and set to appear on all 'user' pages

within the blocks/blockname/db/access.php file, define a new capability 'block/blockname:nonstudenttext' (or find a better name for it than that!) and make sure that it defaults to being set for teacher, editingteacher, coursecreator, manager (but not student, guest, etc.).

in your block, do:

if (has_capability('block/blockname:nonstudent', $this->context)) {
$this->content->text = 'You are not a student';
} else {
$this->content->text = 'You are only a student, you have not been given any higher roles';
}

In reply to Davo Smith

Re: 2.3.2: Getting a user's role

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

That is not a very good solution. The capability block/blockname:nonstudent is horrible. In this case, why not just make the block display the list of roles that the student has.

There will be a function in lib/accesslib.php that returns that information.

 

In reply to Tim Hunt

Re: 2.3.2: Getting a user's role

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

My reason for suggesting this was that the requirements were to show certain information to users who are not a student and different information if they are a student, but don't have any of the non-student roles assigned. Whilst the capability name is horrible (as the original post didn't give enough information to determine a better name for the capability), I still think that the capability-based approach is the right one. It would cope perfectly with the situation in which an additional 'student' role was created (with a different name), as well as showing the correct 'non-student' information to any user who had an additional role ...

On second thoughts, I've just re-read the original specification and realised that the information is not being shown to the student / non-student user, but to the admin user who is viewing their profile. So, forget what I suggested - I had the wrong end of the stick.

In reply to Richard Crawford

Re: 2.3.2: Getting a user's role

by Michael Aherne -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you get the context for the course you could use get_user_roles to get an actual array of the role assignments, then check there's only one item in it, and that the role has the shortname "student".

You could also look at using get_archetype_roles('student') to avoid hard-coding the role shortname.

In reply to Richard Crawford

Re: 2.3.2: Getting a user's role

by Richard Crawford -

Good morning,

I'm sorry I was not clear earlier. What I really need is an equivalent to the old isstudent() function or has_capability('moodle/legacy:student'). Neither option seems to be available in 2.3.2.

I ended up creating my own function, dlc_isstudent() which checks the value of the roleid field in the user's assignment record in mdl_role_assignments and checks to see if it's equal to STUDENTROLEID, which is set to 5. It's an inelegant solution, I know, but it serves its purpose.

 

In reply to Richard Crawford

Re: 2.3.2: Getting a user's role

by George Psirids -

Richard Crawford , I am having a similar issue and I try to get the role of the current logged in user , so I was wondering if you could post here your solution I think it would be a great help for me!