Parent Role Report: Who belongs to who?

Parent Role Report: Who belongs to who?

Marcus Wynwood གིས-
Number of replies: 8

Hi everyone,

Just wondering if there is an admin report that shows Parent/Child relationships site-wide.

For example:

  • Who are the users "Parents"
  • Who are the users "Children"

Something like this:

Homer's Children:

  • Bart
  • Lisa
  • Maggie

Bart's Parents:

  • Homer
  • Marge

We are planning on having quite a few "Parent" users soon, and it would be nice to easily keep track of it all.

Thanks! དགའ་འཛུམ་

དཔྱ་སྙོམས་ཀྱི་སྐུགས་ཚུ།: -
In reply to Marcus Wynwood

Re: Parent Role Report: Who belongs to who?

Marcus Wynwood གིས-

Oh, I forgot - we are on Moodle 2.2.1+

Even a bit of SQL would be useful... SELECT users who have parents... or something ;)

I'll continue exploring...

Thanks again!

In reply to Marcus Wynwood

Re: Parent Role Report: Who belongs to who?

Marcus Wynwood གིས-

Getting somewhere...

# Show users who have the 'Parent' role
select username from mdl_user where id = (select userid from mdl_role_assignments where roleid = (select id from mdl_role where shortname = 'parent'))

 The next step is to find out who their 'children' are...

In reply to Marcus Wynwood

Re: Parent Role Report: Who belongs to who?

Marcus Wynwood གིས-

A very polite *BUMPངོས་ལན་

In reply to Marcus Wynwood

Re: Parent Role Report: Who belongs to who?

Tim Hunt གིས-
Core developers གི་པར Documentation writers གི་པར Particularly helpful Moodlers གི་པར Peer reviewers གི་པར Plugin developers གི་པར

First I am going to rewrite your query

select username from mdl_user where id = (select userid from mdl_role_assignments where roleid = (select id from mdl_role where shortname = 'parent'))

to use joins instead of nested subqueries

SELECT username
FROM mdl_user user
JOIN mdl_role_assignments ra ON ra.userid = user.id
JOIN mdl_role role ON role.id = ra.roleid
WHERE role.shortname = 'parent'

Now to add the link to the child

SELECT user.username AS parent, child.username AS child
FROM mdl_user user
JOIN mdl_role_assignments ra ON ra.userid = user.id
JOIN mdl_role role ON role.id = ra.roleid
JOIN mdl_context ctx ON ctx.id = ra.contextid AND ctx.contextlevel = 30
JOIN mdl_user child ON child.id = ctx.instanceid
WHERE role.shortname = 'parent'

I have not tested that, so there may be typos, but it is about right.

In reply to Tim Hunt

Re: Parent Role Report: Who belongs to who?

Marcus Wynwood གིས-

Thanks Tim!

That's exactly what I was looking for.

འཛུམ་དྲག་

In reply to Marcus Wynwood

Re: Parent Role Report: Who belongs to who?

Monty Winters གིས-

Is there a way for those of us who are query-language-challenged to produce this kind of report? Or is that something we need to ask for in Version 2.5?

In reply to Tim Hunt

Re: Parent Role Report: Who belongs to who?

james mergenthaler གིས-

Thanks from here too.  This was what I was looking for.  I had something, but your query is more clear, especially with the use of the table alias child on the second use of the user table.