Check if user exists before creating

Check if user exists before creating

by Krzysztof K. -
Number of replies: 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

Average of ratings: -
In reply to Krzysztof K.

Re: Check if user exists before creating

by Krzysztof K. -

Any idea? Anyone?

In reply to Krzysztof K.

Re: Check if user exists before creating

by Gary Hardy -

Call moodle_user_get_users_by_id See example

In reply to Gary Hardy

Re: Check if user exists before creating

by 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?

In reply to Krzysztof K.

Re: Check if user exists before creating

by 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

In reply to Krzysztof K.

Re: Check if user exists before creating

by 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

In reply to Dan Kennedy

Re: Check if user exists before creating

by 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.

In reply to Krzysztof K.

Re: Check if user exists before creating

by 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

In reply to Dan Kennedy

Re: Check if user exists before creating

by Krzysztof K. -

Hi!

Paste your whole code please.

In reply to Krzysztof K.

Re: Check if user exists before creating

by 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 smile

Regards,
Dan