Is there way to see/run a query to find non-enrolled users in mysql?

Is there way to see/run a query to find non-enrolled users in mysql?

by Katrina Belcher -
Number of replies: 7

I have about 650 "registered" users, but only about 200 students. I'd like to delete the 450 non-students, all in one fell swoop instead of one-by-one.

Is there way to see/run a query to find non-enrolled users in mysql so I can delete them that way, or is there another way to identify and delete non-students?

Thanks in advance.

Average of ratings: -
In reply to Katrina Belcher

Re: Is there way to see/run a query to find non-enrolled users in mysql?

by Rick Jerz -
Picture of Particularly helpful Moodlers Picture of Testers

I am not the best at this, but I did write the SQL query for myself for this same purpose.  If you are good with SQL, you might be able to improve upon this.

I clean up these "bogus" users somewhat regularly.  For example, when I ran my query right now I came up with 2 bogus users.  In your case, you seem to want to get rid of 450, which could take some time doing it one by one.  If you are good with SQL and databases, you could probably find a faster way of doing this.

You might want to consider using Moodle's "Bulk user actions" to start finding groups of bogus users.  For example, you might want to find all users that are "not confirmed."  Or all users "not in your country."  Of the 450, this process might get you somewhat quickly to where you want to be.

Okay, here's the code.

# User who are not valid

SELECT id,

       firstname,

       lastname,

       email,

       confirmed,

       city,

       country

FROM prefix_user

WHERE id NOT IN

    (SELECT DISTINCT u.id

     FROM prefix_role_assignments ra

     JOIN prefix_user u ON u.id = ra.userid

     JOIN prefix_role r ON r.id = ra.roleid

     JOIN prefix_context cxt ON cxt.id = ra.contextid

     JOIN prefix_course c ON c.id = cxt.instanceid

     WHERE ra.userid = u.id

       AND ra.contextid = cxt.id

       AND cxt.contextlevel =50

       AND cxt.instanceid = c.id)

  AND deleted = 0

  AND suspended = 0

  AND firstname NOT LIKE "guest user"

In reply to Rick Jerz

Re: Is there way to see/run a query to find non-enrolled users in mysql?

by Katrina Belcher -

Thanks so much for your help!

I'm not good with SQL, so I'm not sure I could've done as you suggested!  What I was trying to do is export my course in order to move it to a new server. I found out that when you export the course, it automatically only exports "real" students!

I would be nice if Moodle made massive grab and deletes an option.

Regardless - thank you again!

In reply to Katrina Belcher

Re: Is there way to see/run a query to find non-enrolled users in mysql?

by Rick Jerz -
Picture of Particularly helpful Moodlers Picture of Testers

I think that you are correct, Katrina.  When you "backup" a course, with "Include enrolled users" and then "restore" this course in another Moodle, the users in this course are added to the second Moodle.

In reply to Rick Jerz

Re: Is there way to see/run a query to find non-enrolled users in mysql?

by Katrina Belcher -
Yes - nice feature! I still think Moodle needs to figure out a way to mass delete non-enrolled users. This worked for me at this time, because I decided to move my course to a new server, but in the future, I'm sure I'll get a lot more registered users who are not enrolled in the course, and I'll want to delete them.  At this time the only way to do that from within the Admin panel is one-by-one, or via MySQL. Not very efficient, IMHO.


Thanks again for your help, Rick!

In reply to Katrina Belcher

Re: Is there way to see/run a query to find non-enrolled users in mysql?

by Rick Jerz -
Picture of Particularly helpful Moodlers Picture of Testers

Okay, Katrina.  I forgot that you kind of solved your problem by moving courses to a new Moodle.  I will put this on the lower priority list, for now.

In reply to Katrina Belcher

Re: Is there way to see/run a query to find non-enrolled users in mysql?

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

So, now am I confused...doesn't the export automatically do exactly what you want?

In reply to Emma Richardson

Re: Is there way to see/run a query to find non-enrolled users in mysql?

by Katrina Belcher -

Yes, however, #1 - that's not what I initially asked, and #2 - After I decided to export the course, I then found out that exporting with just enrolled users was an option. Make sense?