Which tables involved in course enrolment

Which tables involved in course enrolment

by Deleted user -
Number of replies: 6

Can anybody tell me which tables are involved in enroling a student in a course (assigning the role of legacy:student within a particular course) in Moodle 1.9?

I've attempted to figure it out, but have not been succesful. I have also searched the forums, and not found any answers there. I also left a post in another forum over a week ago, but got no response, so hopefully a developer can point me to the correct tables.

Since the API is still being worked on, I need to be able to do a direct write to the db to enrol students. And yes, I have looked at other enrolment methods like external db, but they do not meet our requirement that the student can be manually unenrolled from within Moodle as well, so I need to automatically simulate a manual enrolment, and not rely on any external source once this enrolment is complete.

Thanks

Average of ratings: -
In reply to Deleted user

Re: Which tables involved in course enrolment

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
No worries, this is easy. When you assign a role, these are stored in mdl_role_assignments, that's all you need to add to. It basically takes these three fields (I am assuming the course and user both exist):

role id - the id in mdl_role of the row with shortname 'student'

context id - the id in mdl_context with contextlevel 50 (=CONTEXT_COURSE) and instanceid = mdl_course id of the course you want to enrol them in

user id - the user's id in mdl_user

Incidentally, there are also fields timestart and timeend in mdl_role_assignments. Neither of them work as expected - timestart doesn't do anything at all [I provided a patch to make it work, in MDL-8789, but this hasn't been accepted] and timeend does work (Moodle will delete enrolments when they 'expire') but only if you turn on the 'limited length enrolments' option for the course, which you probably don't want to.

So basically you might be tempted to look at the table and think 'hm, cool, we can make our database fill those in and it will automatically allow students into the site at the right date, then remove their access when the course is over'. That's what we thought too! But don't do that unless you fancy fixing the code yourself (as we had to - and no we didn't find out about this until somebody complained that students could access a website before it was ready).

As for the practice of inserting stuff in the database directly, not doing a proper role_assign call doesn't appear to cause us any problems. It is probably a good idea to leave role_unassign to Moodle though (i.e. don't delete rows from the table yourself) as this handles other functions such as deleting forum subscriptions, iirc.

--sam
Average of ratings: Useful (1)
In reply to sam marshall

Re: Which tables involved in course enrolment

by Deleted user -

Thanks sam... I knew it wouldn't be that difficult, I just couldn't quite dechiper what was happening!!

We don't need the timestart or timeend fields, but that's good info to know about! And we will leave the role_unassign to Moodle.

Thanks again

In reply to sam marshall

Re: Which tables involved in course enrolment

by Ray H -
Hi Sam,

I understand that this is a somewhat old thread, but there is a bug in Moodle where if you unenroll a student who is still subscribed to receive emails in a forum, that student will continue to receive emails even though s/he is unenrolled!

Do you know how to properly unenroll a student, while also removing any forum subscriptions from a certain course?

What table or entry would we have to look at in order to make this happen?

Thanks for reading,
Ray
In reply to Ray H

Re: Which tables involved in course enrolment

by Matt Campbell -
MDL-10624 & MDL-11646, fixed in Moodle 1.9.1. MDL-11646 has some really good conversation talking about what all it took to fix it.

Thanks,
Matt
In reply to Matt Campbell

Re: Which tables involved in course enrolment

by Robert Russo -
Picture of Plugin developers
Some sql to get you who is enrolled in what course... The last line is optional

SELECT DISTINCT c.shortname, usr.username
FROM mdl_course AS c
INNER JOIN mdl_context AS cx ON c.id = cx.instanceid
AND cx.contextlevel = '50'
INNER JOIN mdl_role_assignments AS ra ON cx.id = ra.contextid
INNER JOIN mdl_role AS r ON ra.roleid = r.id
INNER JOIN mdl_user AS usr ON ra.userid = usr.id
WHERE r.name = "Student"
AND c.shortname LIKE "%SOMETHING%"