General plugins

Google Apps integration: Automatically create new Gmail accounts for new users

 
Picture of Mark Chaney
Google Apps integration: Automatically create new Gmail accounts for new users
 

Hi, I have recently started to set up a new Moodle 2.2 and i have installed the Gmail block and google user sync.


I can send emails from Moodle through gmail ok. I can add new users to sync using the manual select and add them. Those users can see their emails and SSO works find.


What I cant seem to be able to do is to get the users to be added to google automatically on creation of their accounts in Moodle. Please tell me all I have done is miss a setting somewhere. One of our other Moodles (1.9) does it fine.


Any help would be greately appreciated. I dont particularly want to have to write the feature myself so I am hoping it exists.

 
Average of ratings: -
Picture of Mark Chaney
Re: Google Apps integration: Automatically create new Gmail accounts for new users
 

Ok, transpires that the new block for Moodle 2.+ for gmail hasnt got a method or event handler for 'user_created'. So, I created one.

I added the following to blocks/gdata/db/events.php:

'user_created' => array(
        'handlerfile'     => '/blocks/gdata/gapps.php',
        'handlerfunction' => array('blocks_gdata_gapps', 'user_created_event'),
        'schedule'        => 'instant'
    )


And then added the following to blocks/gdata/gapps.php :

public static function user_created_event($user) {
        try{
            $gapps      = new blocks_gdata_gapps();
            $gapps ->create_user($user, $checkexists = true);
        }
        catch (blocks_gdata_exception $e) {
            
        }
    }

 

Ideally I wanted to put something inside the catch block, but, the rest of the gmail code, and where it handles the other functions, they dont. This seems to work like a charm for me.

 
Average of ratings: -
Picture of Rob Johnson
Re: Google Apps integration: Automatically create new Gmail accounts for new users
 

Auto user creation has been a headache for me since upgrading to Moodle 2 last year.  Thank you for posting your code tweaks.  I plan to test them later in the month, and will post feedback to this thread. smile

 
Average of ratings: -
Picture of Rob Johnson
Re: Google Apps integration: Automatically create new Gmail accounts for new users
 

Hi Mark,

I am going to test your code changes, and want to know if it matters where (line numbers) I paste the additions into the respective files.

Thanks in advance.

Rob

 
Average of ratings: -
Picture of Mark Chaney
Re: Google Apps integration: Automatically create new Gmail accounts for new users
 

Line numbers dont matter for either of them. The first is a new array for a new entry into the event handler table. Think you might have to increase the version number to todays date to get it to fire them into the database.

The second is a function call so can go anywhere as long as its not in any other function, bottom of the file is the safest, but I  put it between the other functions that are like this one for consistency.


Thanks

Mark

 
Average of ratings:Useful (1)