Check if user exists before creating

Check if user exists before creating

Krzysztof K. -
回帖数:9

Hello.

I'am begining with webservices in Moodle. I am using Zend XMLRPC Client to interact with Moodle. I am able to create users and enrol them to courses. I would also like to check if user exists (knowing his username or email) not knowing his id. I wouldn't like to fetch all users to do that.

Kind regards,

Krzysztof

回复Krzysztof K.

Re: Check if user exists before creating

Krzysztof K. -

Any idea? Anyone?

回复Krzysztof K.

Re: Check if user exists before creating

Gary Hardy -

Call moodle_user_get_users_by_id See example

回复Gary Hardy

Re: Check if user exists before creating

Krzysztof K. -

I don't know the id of user. I would like to check if user exists but I know email or username only. I could fetch all users using moodle_user_get_users_by_id (providing wide array of id's, eg 0..99999) and then check the response for searched user but this is very non optimal. How do I check if user exists knowing only email or username?

回复Krzysztof K.

Re: Check if user exists before creating

David boudreault -

I have the same problem.

I want to update user when user already exists, but fetch all user using moodle_user_get_users_by_id and then check the response for searched user is non optimal.

How can we check if user exists knowing only email and username ?

David

回复Krzysztof K.

Re: Check if user exists before creating

Dan Kennedy -

Hi Krzysztof,

Sorry for bein annoying and asking, but are you able to copy the code you're using to create the users?  I've been struggling with this for quite some time and I can't seem to figure out why it doesn't work for me - it's the structure of the array parameter that's causing the issue.  I can request courses, no problem, but when adding a user I run into problems.

Cheers,

Dan

回复Dan Kennedy

Re: Check if user exists before creating

Krzysztof K. -
function wykonaj($function, $params) {

    $token = 'xxxx';
    $serverurl = "xxxx";

    $client = new Zend_XmlRpc_Client($serverurl);

    try {
        $data = $client->call($function,$params);
        $ret['data'] = $data;
        }
    catch (exception $exception) {
        $ret['exception'] = $exception;
        }
    return $ret;
}

function utworz_uzytkownika($user) {
    $function = 'moodle_user_create_users';
    $params['users'][0] = array('username' => $user['username'],
                        'password' => $user['password'],
                        'firstname' => $user['firstname'],
                        'lastname' => $user['lastname'],
                        'email' => $user['email'],
                        'lang' => $user['lang']);
    $ret = wykonaj($function, $params);
    return $ret;
}

It's in polish. wykonaj means run and utworz_uzytkownika means create_user. Hope this helps.

回复Krzysztof K.

Re: Check if user exists before creating

Dan Kennedy -

Hi Krzysztof,

Thanks a lot for your time and effort (and Polish translation!).  I've copied the code above directly into my test page, but I still get this error:

Invalid external api parameter: users (Only arrays accepted.)

If it works for you and not for me, then it must be something with my installation.  If you have any ideas, I would greatly appreciate it, but no problem if you don't.

D

回复Dan Kennedy

Re: Check if user exists before creating

Krzysztof K. -

Hi!

Paste your whole code please.

回复Krzysztof K.

Re: Check if user exists before creating

Dan Kennedy -

Hi Krzysztof,

Thanks for all your help on this.  Because Moodle's documentation is incomplete, the API is not working consistantly and is unstable, and many people are complaining the code is out of date and not commented (and I agree), we've decided to move onto eFront.  Already I've had much more success with their support, API and code quality, and it's not nearly as bloated or hard to use.

Once again, thanks for all your help, it was greatly appreciated 微笑

Regards,
Dan