Parent Role Report: Who belongs to who?

Parent Role Report: Who belongs to who?

by 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! smile

Average of ratings: -
In reply to Marcus Wynwood

Re: Parent Role Report: Who belongs to who?

by 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?

by 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?

by Marcus Wynwood -

A very polite *BUMPapprove

In reply to Marcus Wynwood

Re: Parent Role Report: Who belongs to who?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of 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.

Average of ratings: Useful (3)
In reply to Tim Hunt

Re: Parent Role Report: Who belongs to who?

by Marcus Wynwood -

Thanks Tim!

That's exactly what I was looking for.

big grin

In reply to Marcus Wynwood

Re: Parent Role Report: Who belongs to who?

by 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?

by 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.

In reply to Tim Hunt

Re: Parent Role Report: Who belongs to who?

by Paul Bovis -

Tim

I just want to say that this was exactly what I was looking for as well. Many thanks for all of your help smile

Paul