SQL to find unenrolled user for checking CSV enrol file creation

SQL to find unenrolled user for checking CSV enrol file creation

by Stuart Disney -
Number of replies: 2

Hi,

We enrol all our students on courses using a CSV file created from our Student Management system. The problem currently with this is that it creates a file that has 129000 rows (as there are several courses where all staff and students are enrolled ) which the bulk of the data has been already applied to moodle so its just making our cron run for 10 mins. To cut down the file size I have changed the sql query to join with the moodle tables to check the student exists in moodle and that they are not already on the course, the moodle bit of the query to identify students on a course looks like this for course with idnumber 18

SELECT user2.username

FROM moodle_db.dbo.mdl_course AS course

JOIN moodle_db.dbo.mdl_enrol AS en ON en.courseid = course.id

JOIN moodle_db.dbo.mdl_user_enrolments AS ue ON ue.enrolid = en.id

JOIN moodle_db.dbo.mdl_user AS user2 ON ue.userid = user2.id

where course.idnumber=18)

and rtrim(CAST(SD.refno AS nvarchar(6))) in (select username from moodle_db.dbo.mdl_user)

The problem is that the cron issues error ' Unknown user idnumber or deleted user in field 3' for students that have been unenrolled which from the crons point of view is valid (i.e don't enrol if they have been deleted).

However the query above does not include students that have been unenrolled, I can't find in the moodle tables where this is kept? I know that the cron is finding this info and if you re-enrol the user it asks if you want to restore their grades.

Any ideas on how to amend the query to pick up unenrolled/deleted  users from a course?

thanks,

Stu 

 

Average of ratings: -
In reply to Stuart Disney

Re: SQL to find unenrolled user for checking CSV enrol file creation

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

I haven't had time to verify but in the user_enrolments tables there is a status field.  I wonder if that is where the reference is?

In reply to Emma Richardson

Re: SQL to find unenrolled user for checking CSV enrol file creation

by Stuart Disney -

Emma,

Thanks I did a bit more testing. When I deleted the student enrolment the relevant row in user_enrolments table was deleted. If I suspend the student enrolment the status field goes from 0 to 1. So still can't find where cron is looking to find that the enrolment has been deleted.

thanks,

Stu