Table for role relationship

Table for role relationship

by Kimber Warden -
Number of replies: 6

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: -
In reply to Kimber Warden

Re: Table for role relationship

by Chirag Patel -

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

In reply to Kimber Warden

Re: Table for role relationship

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

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 (5)
In reply to Kimber Warden

Re: Table for role relationship

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

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)
In reply to David Mudrák

Re: Table for role relationship

by Kimber Warden -

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

In reply to Kimber Warden

Re: Table for role relationship

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

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)
In reply to David Mudrák

Re: Table for role relationship

by Kimber Warden -

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: Useful (1)