General developer forum

Table for role relationship

 
 
Picture of Kimber Warden
Table for role relationship
 

Is there a table that stores the relationship of one role to another, for example, student and parent? I'm looking for where the information is stored when I "Assign roles relative to this user" in a user's profile.

 
Average of ratings: -
Picture of Chirag Patel
Re: Table for role relationship
 

sorry can you elaborate this question,

And in the mean time you can check the mdl_role and mdl_role _assignments table!!

and also mdl_context table

 
Average of ratings: -
Davo
Re: Table for role relationship
Group DevelopersGroup Particularly helpful Moodlers

All roles are assigned by joining together the following tables (I'm assuming the default 'mdl_' prefix in all of these cases):

  • mdl_user - the users on the system
  • mdl_role - the available roles
  • mdl_context - the different contexts (system, course, category, activity, block, user) in which roles can be assigned
  • mdl_role_assignments - the table that links together the above 3 tables

To get from user => roles assigned to that user, you need to do the following:

  1. Get the userid (the 'id' field from mdl_user)
  2. Look up the 'context' for this user in mdl_context: context.instanceid = user.id AND context.contextlevel = 30 (CONTEXT_USER)
  3. Look up the role assignments in this context in mdl_role_assignments: role_assignments.contextid = context.id
  4. Find out which users are assigned to the user: user.id = role_assignments.userid
  5. Look in mdl_role to find out what roles these users have been given: role.id = role_assignments.roleid

I hope that makes some sense?

 

 

 
Average of ratings:Useful (4)
Picture of David Mudrák
Re: Table for role relationship
Group DevelopersGroup Documentation writersGroup Moodle HQGroup Particularly helpful MoodlersGroup Translators

Davo is right. However, as a developer, you never want to look for particular role assigned. It's a common misunderstanding. As a developer, what you really want, is to check for capabilities in the given context. For example, IIRC the parent role is assigned in the student's user context. So what you check in the code is if the current USER grants a particular capability in another user's context.

 
Average of ratings:Useful (2)
Picture of Kimber Warden
Re: Table for role relationship
 

Thanks Davo, that helps!

David, can you please elaborate a little? My goal is to write a report that's viewable by mentors that includes only their mentees.

Kimber

 
Average of ratings: -
Picture of David Mudrák
Re: Table for role relationship
Group DevelopersGroup Documentation writersGroup Moodle HQGroup Particularly helpful MoodlersGroup Translators

Your report should define a capability like "report/foobar:viewuserdata" (where foobar is the name of your report plugin). The context level where this capability is checked should be CONTEXT_USER. The capability definition could be something like

'report/foobar:viewuserdata' => array(
    'riskbitmask' => RISK_PERSONAL,
    'captype' => 'read',
    'contextlevel' => CONTEXT_USER,
    'archetypes' => array(),
),

Then, in order to check if the current $USER (the mentor) is allowed to see the report on the given student (with user id $userid), use

if (has_capability('report/foobar:viewuserdata', context_user::instance($userid))) { ... }

Then create the Mentor role if you do not have it already. The procedure is similar to the Parent role described in the docs. Most notably, the context where the role can be assigned, must be set to User. The Mentor role should (among other things of your choice) grant your capability.

So the only thing you check in the code is "does the currently logged-in user have the permission "report/foobar:viewuserdata" in the context of this other user?"

To display the list of mentees you may wish to re-use the code used by the block/mentees/.

Good luck.

 
Average of ratings:Useful (2)
Picture of Kimber Warden
Re: Table for role relationship
 

I get it. You're saying that, for example, the definition of roles can change, and I'd want this report to be available to whichever roles I have specified in role settings that can view it. Today that might be mentors, but a year from now, it might be teachers, and I don't want to have to remember to change my report code to allow that. I only want to have to change the capability in role settings. That makes perfect sense. Thanks!

 
Average of ratings: -