Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually create a user in Moodle?

I am trying to create an authentication plugin, but I am having problems when the user does not exist in the moodle database. Therefore I am trying to find a way to manually create a user.

I tried:

$user            = new StdClass();
$user->username  = $ucUser;
$user->auth      = 'ucauth';
$user->firstname = "First";
$user->lastname  = "Last";
$user->id        = $DB->insert_record('user', $user);

But it didn't work... I got an insert error. What else do I need in the $user object?

like image 881
Naner Avatar asked Jun 06 '12 17:06

Naner


2 Answers

User object should have the following values:

$user             = new StdClass();
$user->auth       = 'manual';
$user->confirmed  = 1;
$user->mnethostid = 1;
$user->email      = "email";
$user->username   = "username";
$user->password   = md5('password');
$user->lastname   = "lastname";
$user->firstname  = "firstname";
$user->id         = $DB->insert_record('user', $user);

Please try this.

like image 200
gnuwings Avatar answered Sep 29 '22 11:09

gnuwings


Well, I recommend you to use authenticate_user_login($username, null) this is going to give you an empty user, wich you can complete later on in the proccess. Then you can use complete_user_login($user); and if you want to send the user to the edit page something like

if (user_not_fully_set_up($USER)) {
            $urltogo = $CFG->wwwroot.'/user/edit.php';
        }else{
            $urltogo = $CFG->wwwroot.'/';
        }

    redirect($urltogo);

I don't know exactly what are you trying to achieve. But I've made a plugin to connect with an external web service and it took me a while to figure out how to do it properly. I'm able to help you with anything you need.

like image 25
limoragni Avatar answered Sep 29 '22 12:09

limoragni