1

I am trying to create a new user in Moodle via the Web service API (version 2.7.1+ (Build: 20140829). I want to auto-generate the password and notify the user via email about his new account.

When I create a user via GUI there is a checkbox for doing exactly that: generating password and notifying user via email.

enter image description here

However, when I create a user via the API I do not know how to force password generation and the email notification. Unfortunately I cannot find anything in the Moodle API about how to automatically send email after user creation.

private function createUser($firstName, $lastName, $email){
    $newUser = new stdClass();
    $newUser->username = strtolower($email);
    $newUser->password = getInitialPassword();
    $newUser->firstname = $firstName ?: getRandomUsername();
    $newUser->lastname = $lastName ?: '.';
    $newUser->email = $email;
    $newUser->preferences = array(array('type' =>'auth_forcepasswordchange', 'value' => true));
    $users = array($newUser);
    $params = array('users' => $users);

    return post(buildServerUrl($create_user_command), $params);
}

Does anyone know how to do that?

GEMI
  • 2,239
  • 3
  • 20
  • 28

1 Answers1

2

it seems to be the same question asked at https://moodle.org/mod/forum/discuss.php?d=323422 , so here is the same answer:

"this improvement was introduced in 3.0, you may be interested in MDL-51182, it seems simple to backport."

Kind regards, Daniel

  • Yes, I had to update Moodle in order to get this working, no other way around it :( – GEMI Jan 05 '16 at 10:10
  • @GEMI Do you recall how it worked for you? Do you mean it worked with the above code in place? or was this a default behavior in moodle? Or it needed an extra param to be set in order to send notification? – jitendrapurohit Oct 13 '20 at 06:02