Authentication and Enrolment by the same external database

Authentication and Enrolment by the same external database

by Tim Allen -
Number of replies: 9

Using Moodle 1.4.1 external enrolment is not working for me.  sad  I am trying to do both authentication, which works well, and enrolment, which doesn't, from the same mysql database table.

When the new user logs in he is authenticated successfully.  However, when he tries to enter his course he can't enter - he hasn't been enrolled.  I am sure I have set up all the correct parameters. 

If the user then logs out and logs in again, he gets a "Weird error: User not found" error message, after which however he is still logged in at the front page of the site.  But he still cannot enter any courses.  If internal enrolment is enabled as an alternative method and the user logs in using the enrolment key, he can enter the course, however, upon subsequent logins he is no longer enrolled in the course, and the same error is shown!  mixed

I have added this to the bugtracker at:

http://moodle.org/bugs/bug.php?op=show&bugid=1960&pos=2

Anyone else experienced this, or any comments about it?

Tim.

Average of ratings: -
In reply to Tim Allen

Re: Authentication and Enrolment by the same external database

by James M -

Hi Tim. This looks like the same thing Crafton and I are discussing in: http://moodle.org/mod/forum/discuss.php?d=12670

Basically, the code in database/enrol.php needs to be modified.

Don't seem to be able to attach documents to this forum, so I'll email you a modified enrol.php to try out. I'm interested in whether it works for you.

Let me know if it works (or not)

In reply to James M

Re: Authentication and Enrolment by the same external database

by Tim Allen -

Hi James,

Well thank you so much - the modified file has fixed the problem! smile  big grin  I saw the other discussion you were having but it was a little too technical for me and I wasn't sure if it related to my issue - I'm not a coder.  shy

Did I call this correctly as a bug, and what are the chances of this becoming part of moodle code?  Also, do you think I could safely use this modified file on my production site? 

I am very happy as I have spent hours on this in the last couple of days and it will really help simplify my learners' registration process.  approve

Thanks again,
Tim.

In reply to Tim Allen

Re: Authentication and Enrolment by the same external database

by James M -

Hey Tim. Glad it fixed things. It's a nice feeling when that automatic enrolment happens.

You were right in calling it a bug. I'm kind of new to Moodle so not exactly sure how things move forward, but Martin's now got the code (see the other forum thread) so I'm hoping the fixes will get into the main code. I'd guess so.

As for using what you have now on a production system .... I'm just using it on a development system so haven't really tried it in too many scenarios or with a large number of users. I'm a bit cautious, so would probably wait a day or two and see what happens with comments on the forum and until I get a bit more testing done. My confidence would be somewhere around 70-80% at the moment.  I'd at least watch it like a hawk for the first day or two and have a fallback plan. Don't know if that helps.

By the way, what field are you using for the enrol_localuserfield ? If you're using mdl_users.idnumber, be a bit careful, because the student can modify that. Have a look at this thread http://moodle.org/mod/forum/discuss.php?d=12366 I'm not using idnumber, but Martin L mentions a patch.


In reply to James M

Re: Authentication and Enrolment by the same external database

by Marcone Hilton -

Hi, James.

Having the same problem here, too. If it's not too much asking for, could you also send the modified enrol.php to me (marconehilton@terra.com.br)?

Thanks in advance!

In reply to Marcone Hilton

Re: Authentication and Enrolment by the same external database

by James M -
Hi Marcone. Have a look at this other thread http://moodle.org/mod/forum/discuss.php?d=12670 . Both Crafton and I attached our changed enrol.php (you'll have to change mine to that name). Please let us know whether this fixes your problem. I've done more testing now, and no problems yet.
In reply to James M

Re: Authentication and Enrolment by the same external database

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
I've also put my own new version of this file (it scales better for big sites) in CVS (STABLE and HEAD). cvs:/moodle/enrol/database/enrol.php

Might take a few hours to be visible there.  Please test it before 1.4.2 comes out with it!
In reply to Martin Dougiamas

Re: Authentication and Enrolment by the same external database

by James M -

Hi Martin, just had a chance to test it. The enrolment works sweetly, though I'm seeing two things:
1. function doesn't exit with student enrolled in course 1 (site), so they can't change their user profile if they go to it from the homepage (this routine checks $user->student array for that. This isn't set because variable $newstudent doesn't get course #1 set.

2. $CFG->prefix for the database doesn't get restored to its original (it gets tweaked at the beginning of the function). I think some later routines may use this and give errors. I only see these when I'm debugging and doing some modifications though. For example we have to change the query to the external database, and before I got it right ... I got .... (name of our database is moodle1_4). I'm not sure where these calls are actually being made, but two that I see are ....

Weird error: User not found

Continue

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '.mdl_course WHERE category = '0'' at line 1

SELECT * FROM moodle1_4.moodle1_4.mdl_course WHERE category = '0'

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '.mdl_user WHERE id = '116' AND username = 'guest' LIMIT 1' at l

SELECT * FROM moodle1_4.moodle1_4.mdl_user WHERE id = '116' AND username = 'guest' LIMIT 1

In reply to James M

Re: Authentication and Enrolment by the same external database

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Thanks for the feedback!

1. That's a general bug in require_login, thanks.  Now fixed to always return true for a user in the site course.

2.  Hmm ... I can't see why that happens but I'll restore the $CFG->prefix variable at the end of that function.  BTW, why did you need to change the query - weren't the enrolment plugin parameters enough?
In reply to Martin Dougiamas

Re: Authentication and Enrolment by the same external database

by James M -

Hey again.
1. I only noticed that because $user->student(1) (the site "course") is already set when get_student_courses is called, and when I first fiddled with the function, I was deleting that and just setting my new courses. If, instead I appended the new courses, then the problem didn't occur. I'll take your word for it though smile but if the function returns with $user->student(1)  set then the problem doesn't occur. I wasn't sure where else that is referenced, but thought it best that get_student_courses didn't stomp on it especially as it was just "freshly" set (in datalib.php in function get_user_info_from_db just before get_student_courses is called)

2. About my query ... sorry, I should have explained that better. The query is fine, just we support groups within a course in our external database, so have to query 2 tables (courses to groups, groups to users) to get the course to user mapping. So it's just our special case.