Users with no courses

Users with no courses

by Vicke Denniston -
Number of replies: 6
Picture of Testers

I am trying to write/find a report that will allow me to find users who are not enrolled in any courses and have never logged into the system

What I need is username, first name, last name, time created, last access for users who are not enrolled in any course.

My goal is a list of users who can be deleted. Almost 1/4 of our users have never been in Moodle and were created more than 2.5 years ago, and we are hoping to cut our user numbers down.

I have a report that gives me the list of users who have never logged in (see below). Would I need to tie in the user_enrolments status field? I have another report created by Gemma Lesterhuis that gives me all of the users with their courses



SELECT id AS 'User ID',
idnumber AS 'ID number',
username AS 'User Name',
deleted AS 'Flagged deleted',
firstname AS 'First Name',
lastname AS 'Last Name',
email AS 'Email Address',
timecreated AS 'UNIX time created',
FROM_UNIXTIME (timecreated, '%d/%m/%Y') AS 'Time Created',
FROM_UNIXTIME (lastaccess, '%d/%m/%Y') AS 'Last Access'
FROM prefix_user
WHERE lastaccess='0'
AND timecreated < 1356912000 /*before 01/01/13*/
AND deleted =0
ORDER BY timecreated ASC, CAST(id AS UNSIGNED) ASC

Average of ratings: -
In reply to Vicke Denniston

Re: Users with no courses

by Vicke Denniston -
Picture of Testers

I'm still working on this.

I was able to get a query of users with no courses, but it came up blank. I then realised that Moodle considers the Dashboard/MyMoodle page to be a course, so everyone is enrolled in a class with the course id =1. But when I try to find the users listed for courseid=1, it comes up empty.

Below is the slightly modified of Gemma Lesterhuis' report. When I put in the id number of any course, it returns the users enrolled in that class, but if I put c.id=1 there, I get no users.

Here is my confusion. The course with id=1 is listed as Moodle when you look at a list of all courses, but when I try to find out who is enrolled in that course, it returns no users.

Help?


SELECT
u.id,
u.username AS 'Username',
u.firstname AS'Firstname',
u.lastname AS 'Lastname',
u.email AS 'email',
cc.name AS 'category',
c.fullname AS 'course',
FROM_UNIXTIME(firstaccess, '%d-%m-%Y') AS 'First login',
FROM_UNIXTIME(lastaccess, '%d-%m-%Y') AS 'Last login'
FROM prefix_user as u
INNER JOIN prefix_user_enrolments AS e ON e.userid = u.id
INNER JOIN prefix_enrol AS en ON en.id = e.enrolid
INNER JOIN prefix_course AS c ON c.id = en.courseid
INNER JOIN prefix_course_categories AS cc ON cc.id = c.category
WHERE u.deleted = 0
AND u.suspended = 0
AND c.id = 18184 /* this returns users, but c.id=1 does not*/
ORDER BY cc.name, c.fullname


In reply to Vicke Denniston

Re: Users with no courses

by Randy Thornton -
Picture of Documentation writers

Vicke,

id = 1 is just the Frontpage, which is technically a course, because of weird historical things better left buried in the Moodle crypt.  So your report data is correct.

id = 1 it is not the Moodle site. It will not do what you hope it will. You have to actually do the opposite: exclude 1 and include all other courses. 

The fundamental challenge of the query is that it is easy to get all users who have enrollments, but what you need it the inverse set: all users without any enrollments.

In reply to Randy Thornton

Re: Users with no courses

by Vicke Denniston -
Picture of Testers

So if I searched for courses with an id>1 and they have no other classes, that should, maybe, get me what I want?

In reply to Vicke Denniston

Re: Users with no courses

by Randy Thornton -
Picture of Documentation writers

Actually, no one will have enrollments in course 1 probably, unless you limiting roles to activities on the Frontpage itself. What you are looking for is people who have no enrollments anywhere.

In reply to Vicke Denniston

Re: Users with no courses

by Vicke Denniston -
Picture of Testers

I think I have something that if not perfect, at least covers what I need.

I realised 3 things:

     1. Everyone has the role 'authenticated user' until you eliminate all other roles, then no one does.

      2. Everyone is enrolled in the course with id=1 (the Front page), until you eliminate all other courses, then no one is enrolled.

       3. Moodle does not seem to want to admit that someone who has never logged in could be enrolled in a class.
I'd love to be wrong on those.


This query returns everyone who has never logged in and was created before the oldest of the regular courses was deleted, so they would be in no currently available academic courses. My guess is that if they weren't modified after they were created, they won't have any active classes on the server. The date shown is 01/01/15 at 00:00 US Central Time
SELECT
id,
username,
firstname,
lastname,
email,
FROM_UNIXTIME (firstaccess, '%d/%m/%Y') AS 'First Access',
FROM_UNIXTIME (lastaccess, '%d/%m/%Y') AS 'Last Access',
FROM_UNIXTIME (lastlogin, '%d/%m/%Y') AS 'Last Login',
FROM_UNIXTIME (timecreated, '%d/%m/%Y') AS 'Time Created',
FROM_UNIXTIME (timemodified, '%d/%m/%Y') AS 'Time Modified'  
FROM prefix_user
WHERE lastaccess=0
AND timecreated < 1420092000
AND timecreated=timemodified
ORDER BY lastname, firstname

This one shows the classes that people who were created and were last modified before my cut-off date. This brought me a list of inactive clubs and groups. Given that this list is very short, we can check for content before deleting.

SELECT DISTINCT
u.username AS 'User',
u.firstname AS 'First Name',
u.lastname AS 'Last Name',
FROM_UNIXTIME (u.lastaccess, '%m/%d/%Y') AS 'Last User Access',
c.id AS 'COURSE ID',
c.fullname AS 'Course Name',
FROM_UNIXTIME (c.timecreated, '%m/%d/%Y') AS 'Date Course Created'

FROM prefix_course AS c
JOIN prefix_course_categories AS ca
ON c.category = ca.id
JOIN prefix_user_lastaccess AS la
ON c.id = la.courseid
JOIN prefix_user AS u
ON la.userid = u.id
JOIN prefix_context AS ctx
ON c.id = ctx.instanceid
INNER JOIN prefix_role_assignments AS ra
ON ra.contextid = ctx.id

WHERE ctx.instanceid = c.id
AND u.timecreated < 1420092000
AND u.timecreated=u.timemodified
AND u.lastaccess < 1420092000

ORDER BY  u.lastname, u.firstname