How to check if a User has Role User,Guest or Teacher, Course Creator, Administrator

How to check if a User has Role User,Guest or Teacher, Course Creator, Administrator

by Sebastian Wagner -
Number of replies: 8
I am developing a module for conferencing (http://openmeetings.googlecode.com/).

I need to know if a user has a certain Role. How can I get what Role a user is?

by doing a simple:
pint_r($USER->access)
=> I do get a lot of different information, but basically it seems like there is no definitive attribute that can be compared between users to check what kind of Role this User actually is.

For a user it looks like:
[access] => Array
(
[ra] => Array
(
[/1/8/9] => Array
(
[0] => 5
[1] => 5
)
[/1] => Array
(
[0] => 7
)
)
[rdef] => Array
(
[/1:5] => Array
(
[moodle/block:view] => 1
.....

For a Admin it looks like:
[access] => Array
 (
 [ra] => Array
 (
 [/1] => Array
 (
 [0] => 1
 [1] => 7
 )
 )
 [rdef] => Array
 (
 [/1:1] => Array
 (
 [block/online_users:viewlist] => 1


So how do you see now what kind of Role this User is?


thanks,
sebastian
Average of ratings: -
In reply to Sebastian Wagner

Re: How to check if a User has Role User,Guest or Teacher, Course Creator, Administrator

by Sebastian Wagner -
For a teacher it looks like:
[access] => Array
(
[ra] => Array
(
[/1] => Array
(
[0] => 3
[1] => 7
)

)


=> so this
[0] => 3
[1] => 7

is the magic bit?
[0] => 3 means that it has the Role with the Id 3 ... that would mean its a teacher.

thats correct? Is there no API function that can give a Role-Object by passing the USER-Object?
In reply to Sebastian Wagner

Re: How to check if a User has Role User,Guest or Teacher, Course Creator, Administrator

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
You probably don't really want to know whether a user has a certain role.

It is much more likely that you should be defining a capability like mod/mymodule:dosomething, and then asking whether a user has that capability in the context of your module:

$context = get_context_instance(CONTEXT_MODULE, $cm->id);
if (has_capability('mod/mymodule:dosomething', $context)) {
Average of ratings: Useful (1)
In reply to Tim Hunt

Re: How to check if a User has Role User,Guest or Teacher, Course Creator, Administrator

by Sebastian Wagner -
that was the kind of answer I was afraid of smile

The Flow in my Activity Module is:
A user xy enters a room by clicking on my activity module.
All I want to know is:
Is this user a simple Student or has he the Role of a Teacher or higher.


Long description:
I extend the Plugin for the Conferencing Software OpenMeetings.
The Plugin is implemented as Activity Module. You can add a new Activity to each Course, that Activity will become a Conference Room.
What I want is that if a Teacher enters a Conference Room he should become automatically the Moderator of that Conference Room. Or the other way round, if a simple Student enters a Room he should be forced to wait until the teacher comes.
So I need to know who is Student and who is teacher.

Applying capabilities is somehow strange to me, as that would mean I would have to give EVERY user this special capability. How should I realize that? If there are 1000 Users in a Moodle Course and somebody installs the OpenMeetings Mod. How should I then give this capability into the existing 1000 User Profiles?

And I still would have the same problem:
Lets say I somehow am able to apply capabilities to user x,y how should I know if this User should become the capability or not? Well short Answer: If he is a teacher he gets the capability, if not he doesn't ? Well then the story starts again ... who is a teacher ?! ...

thanks for any enlightenment
sebastian
In reply to Sebastian Wagner

Re: How to check if a User has Role User,Guest or Teacher, Course Creator, Administrator

by Matt Campbell -
You'll want to create a /db/access.php file and define your capabilities there. Take a look at http://docs.moodle.org/en/Development:Roles#Programming_Interface - basically you're going to define some capabilities that are new to Moodle, and then you'll say 'Give this capability to everyone with the default student role', or teacher role, or whatever you think is appropriate.

This gives admins the ability to add or remove the capabilities you define from roles - so if you say non-editing teachers should have it and we don't want it to be that way on our site, we can simply remove that capability from their role and we'll know that it won't affect other blocks, modules, etc.

Thanks,
Matt
In reply to Matt Campbell

Re: How to check if a User has Role User,Guest or Teacher, Course Creator, Administrator

by Sebastian Wagner -
okay I got it working now. Thank you!

sebastian
In reply to Sebastian Wagner

Re: How to check if a User has Role User,Guest or Teacher, Course Creator, Administrator

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I think Matt already explained it, but I want to explain it in my own words too.

The way it is supposed to work is: your roles gives you capabilities, the capabilities let you do stuff.

So I think you need two capabilities:
* mod/openmeeting:enterconference - if you don't have this, the link does not work for you (useful for courses that allow guests in, and you don't want them to come into your openmeeting)

* mod/openmeeting:moderateconference - if you have this, you are made a moderator when you enter the conference.

In access.php, you can set it up so that teachers get both by default, and students only get mod/openmeeting:enterconference

But, the administrator has the flexibility to change the default rules, or make rules for any non-standard roles. And teachers can change the rules in a specific conference using role overrides.
In reply to Tim Hunt

Re: How to check if a User has Role User,Guest or Teacher, Course Creator, Administrator

by Sebastian Wagner -
hi,

I only realized the second one *mod/openmeeting:moderateconference* now.

I do not completely get what you make with *mod/openmeeting:enterconference* and role overrides.

*mod/openmeeting:enterconference* => could prevent / or make an additional option *Do not allow Guests to go into a Meeting room*, so if the room has the *no_guest_allow* flag AND the capability *mod/openmeeting:enterconference* is not available (so you are a guest) => then you are not allowed to enter the room.
That is how far I do get the story.

But for the Role-Overrides and *flexibility to change the default rules* ... hmm basically the Rules are defined in the access.php by ME so without touching this file => who should be able to change it?

thanks,
sebastian
In reply to Sebastian Wagner

Re: How to check if a User has Role User,Guest or Teacher, Course Creator, Administrator

by Sebastian Wagner -
thx to your help I was able to complete and release the new Mod now:

The Moodle Plugin enables you now to use more feature of OpenMeetings:

  • Create both Room Types Conference Room (Multiple Videos) and Event Room (Single Bigger Video)
  • Set an individual Room Language and Max Number of Participant per Room
  • Set a Flag for a Room so that Students and Guests have to wait and will NOT become the Moderation Role in a Conference Room by default, they will have to wait untill a Moodle-Teacher, -Admin or -Creating-Teacher comes and enters a Room.
see also at http://code.google.com/p/openmeetings/wiki/OpenMeetings084

thanks,
sebastian