Get_user_by_email with webservice

Get_user_by_email with webservice

by René Sodemann -
Number of replies: 6

Hi,

as you can see, my question is very short: Is there a way to get a user by the email-address with a webservice. I mean something like the get_user_by_id function in the api documentation.

 

Greetings

René

Average of ratings: -
In reply to René Sodemann

Re: Get_user_by_email with webservice

by Patrick Pollet -

I guess your are under Moodle 2.x ?

this operation is not yet available via the official Web Service, but you can install OK Tech WS (see https://github.com/patrickpollet/moodlews/wiki/Using-oktech-ws-with-moodle-2.0-official-web-service-infrastructure ) and use the call

oktech_get_user, with these two parameters

userinfo= the email you are looking for

idfield='email'

 

so in REST mode in a browser something like this in singleshot mode

http://yourmoodle20/webservice/rest/simpleserver.php?wsusername=xxxxx&wspassword=zzzzz&wsfunction=oktech_get_user&userinfo=me@somewhere.com&idfield=email

or in token mode :

 

http://yourmoodle20/webservice/rest/server.php?wstoken=xxxxxxxxxxxxxxxxxxxxxxxxxxxx&wsfunction=oktech_get_user&userinfo=me@somewhere.com&idfield=email

Cheers

edit : see http://prope.insa-lyon.fr/wshelper/service.php?project=moodlews2&class=mdl_soapserver for a list of available operations and required parameters. don't forget to add oktech_ as the function prefix if you use OK Tech under Moodle 2.x infrastructure

 

edit2 : the function oktech_get_user accepts as idfield almost any column name defined in table mdl_user

for example idfield='lastname' and userinfo='pollet'  all users with lastname=pollet

or

idfield='deleted' and userinfo=1  all users flagged as deleted ...

In reply to Patrick Pollet

Re: Get_user_by_email with webservice

by René Sodemann -

Hi,

you are right. I use Moodle 2.

I tried to use the oktech webservices and it seems to be the right way. But I have still a problem: When I'm searching a user by the email, the webservice give me all users. It won't give me only the user with the email I'm looking for. Is this a problem of the oktech webservice or is it me?

Greetings

René

In reply to René Sodemann

Re: Get_user_by_email with webservice

by Patrick Pollet -

Real strange, are you sure you are not calling oktech_get_users with an s)  that expects an array of user ids  and if absent returns all users of a moodle site ? 

In reply to Patrick Pollet

Re: Get_user_by_email with webservice

by René Sodemann -

No, I'm sure, I'm not calling the oktech_get_userS function. I use the "oktech_get_user" function.

Maybe it's my code. I'm using this SOAP-CLient (Link). I filled the "setup"-section with my information. Then I deleted the userinformation and add the following lines:

$user = new stdClass();
$user->idfield = "email";
$user->userinfo = "mail@mail.de";
$params = array($user);

The rest of the code is the same as in the link. It's a little bit strange. When I delete the lines with "idfield" and "userinfo" it is the same result. I get all users. These two lines seems to be without effect.

Greetings

René

In reply to René Sodemann

Re: Get_user_by_email with webservice

by Patrick Pollet -

@René,

 

     You are not calling properly the function oktech_get_user .

It does not find the values you sent, so it returns all users  (see code at lines 419-445 of wspp/server.class.php) 

In the example of Jerome on github, the function moodle_user_create_users' expects an array of users, each one being an array of userdata 

This function expects TWO strings thus it must be called like this 

$user = array();

//WATCH the ORDER OF THE PARAMETERS !!!!
// if one reverse these two lines, we get a ws_nomatch error !
$user['userinfo'] = "pp@patrickpollet.net";
$user['idfield'] = "email";

$resp = $client->__soapCall($functionname, $user);

actually only the order of the arguments in the passed array is important , not the fields names so this also work ! 

$user = array();

// if one reverse these two lines, we get a ws_nomatch error !
$user[] = "pp@patrickpollet.net";
$user[] = "email";

$resp = $client->__soapCall($functionname, $user);

Attached the complete php code in this case 

Cheers

In reply to Patrick Pollet

Re: Get_user_by_email with webservice

by René Sodemann -

Thank you very much Patrick. Everything works now. Thank you for your help.

Greetings

René