cannod / moodle-drupalservices

Moodle plugin to connect to Drupal services
38 stars 25 forks source link

moodle not creating user before first sign in, when new user is created in drupal #75

Open deepsimran opened 7 years ago

deepsimran commented 7 years ago

This plugin is working well and serves everything i need, instead of one thing.

I am using commerce moodle integration and moodle connector modules with this plugin for the auto enrollment in the courses in moodle.

Its working as a charm for the existing users, but when a new user creates a user account on the drupal side, buy a course and then goes to the moodle and found that it is not enrolled in the course which is quite obvious as user is not present in the moodle database before his first sign-in.

But when the same user goes to the drupal side and buy a course again, this time he will be automatically enrolled in the course as same user is in the database.

Does somebody have some sort of solution to my problem?
I like the functionality of this plugin as it offers SSO. But i really want to solve this problem. Anyone knows how to solve this issue please share your suggestions.

Thanks in Advance.

goose2000 commented 7 years ago

Hi deepsimran,

am selling moodle courses through Drupal Commerce, and because I need users to have immediate access the course they just bought, the sync script included in "moodle-drupalservices" won't cut it.

So if you are using MySQL (and both Drupal & Moodle are in same DB) you can easily get the solution you are after using a few MySQL TRIGGERs. Mostly you'll want a trigger to create a new user in the mdl_user table every time a user is created in the Drupal user table.

You might want to make an UPDATE and DELETE triggers too, keep everything tidy between the two tables.

netw3rker commented 7 years ago

@goose2000, New users should propagate instantly from drupal to moodle when the user arrives at the moodle site. the sync script is really just a journaling process to pick up edits to accounts that happen on the drupal side when the users don't return to moodle and force the update.

@deepsimran, I'm not sure what to tell you here. My guess is that this is more an issue with the auto enrollment tools potentially not triggering when a new user is generated. I don't want to punt this issue though, so lets start with looking for examples of other SSO systems having the same symptoms, and if there are any resolutions. If no other systems have this problem, lets look within & see if we can track it down.

goose2000 commented 7 years ago

@netw3rker , Sure, but in this case using 'commerce moodle integration' the user must exist prior to visiting the moodle side. See, you can't enroll a user Y into course X if moodle can't find user Y (because he/she has not visited the moodle side yet) You have to have them in place, to get the e-commerce enrollment done.

deepsimran commented 7 years ago

Thanx @netw3rker @goose2000 for replying so early,

I totally agree with goose2000 that the user creation is must on moodle side while its created on drupal. So that e-commerce enrollment should be done without any issues.

I think this feature must be the part of this plugin.

netw3rker commented 7 years ago

@goose2000 Yeah, that's really a problem with using a pull on demand system on one end, and a push on demand with the other. It's too bad the commerce guys decided to go the drupal knows about moodle route, and didn't create an enrolment system in moodle that knows about drupal (the right way go).

If I had to guess, there is probably a way in moodle to create a service that when hit will run the import. That could get attached to the hook_user_save in drupal. That probably won't create a race condition & might resolve this issue. Lemme take a look & get back to you guys.

Also @goose2000 , your setup is slick! The one downside is that not everyone runs moodle and drupal within the same database or database server, so update triggers are a somewhat limited solution.

goose2000 commented 7 years ago

@netw3rker , I started a module with hook_user_save ! but I got lazy and my quick fix work well enough. But you are right, some work with that hook, we could INSERT the same user into the mdl_user table. Something WHERE uid will be saved as moodle 'idnumber' field. Then we could have an out of the box e-commerce ready version.

goose2000 commented 7 years ago

Oh, we could leverage the moodle connector module too, everything we need is there for moodle connection.

@deepsimran

You will have to edit these for your DB names and tables. TEST somewhere not production. If you are not comfortable with MySQL, this may not be an advisable solution.

Here is AFTER INSERT trigger // only the bare necessities to get the account going

CREATE DEFINER=root@%TRIGGERmoodle_user_insertAFTER INSERT ONdp7_usersFOR EACH ROW BEGIN INSERT INTO zmdl_user(auth, confirmed, mnethostid, username, idnumber, email, country, lang) VALUES("drupalservices", "1", "1", new.name, new.uid, new.mail, "US", "en_us"); END

AFTER UPDATE: // updated only the email and username

CREATE DEFINER=root@%TRIGGERmoodle_user_update_emailAFTER UPDATE ONdp7_usersFOR EACH ROW BEGIN UPDATE zmdl_user SET username = new.name, email = new.mail WHERE old.uid = idnumber; END

BEFORE DELETE // because after they're gone, we can't find them...

CREATE DEFINER=root@%TRIGGERmoodle_user_deleteBEFORE DELETE ONdp7_usersFOR EACH ROW BEGIN DELETE FROM zmdl_user WHERE OLD.uid = idnumber; END

netw3rker commented 7 years ago

I think it makes more sense for this moodle plugin (not the drupal module) to provide a service that at a minimum fires off the import users code. Then the drupal moodle_services module can make a call to that service. That gets around the need for direct access to the moodle database from drupal.

goose2000 commented 7 years ago

@netw3rker - slowing down and re-reading your post, your idea sounds best, and fits with the spirit of drupal moodle_services module - just make a service call to M. import code -

netw3rker commented 7 years ago

@goose2000 you have no idea how much I appreciate your constant monitoring of the queue!

Now that we have dropped using moodle w/in my org, I don't have a lot of bandwidth to maintain this integration, so having you around to work with everyone is really great.

Any chance you'd like to cut your teeth on making a fix for this issue happen? I'm happy to work with / mentor you where needed :)

deepsimran commented 7 years ago

Hello everyone I found a solution to this issue. I wrote a new moodle php script that will be run when user is created on drupal side which will visit the moodle site (in the back, creates a user) and redirect back to the same page on which user was creating his profile. What you have to do is to write a hook on drupal side to redirect the save profile button to this script.

This is a solution to my problem, but i will love to see it as a feature in the next release. Thanx @netw3rker and @goose2000 for marking this as enhancement.

Fo more details i am attaching a sample script. sample_script.txt