手动创建用户时需要设置一些值。我面临同样的情况并最终得到了这个解决方案:
global $DB;
$user = new StdClass();
$user->email = strtolower('someemail'); //MOODLE requires lowercase
$user->username = strtolower('someusername');//MOODLE requires lowercase
$user->password = hash_internal_user_password('somepassword');
$user->lastname = 'somelastname';
$user->firstname = 'somename';
// These values are required.
// Default values are stored in moodle config files but... this is easier.
$user->auth = 'manual';
$user->confirmed = 1;
$user->mnethostid = 1;
$user->country = 'ES'; //Or another country
$user->lang = 'es'; //Or another country
$user->timecreated = time();
$user->maildisplay= 0;
$user->id = $DB->insert_record('user', $user); // returns new userid
如果您愿意,您可以检索整个用户的数据:
$lastid = $DB->insert_record('user', $user);
$user2 = get_complete_user_data('id', $lastid);