dxiai / moodle-autopilot

Intentional Educational Design Flows
MIT License
2 stars 1 forks source link

Find out User ID for a User based on the Moodle API #1

Open phish108 opened 3 years ago

phish108 commented 3 years ago

Many API Calls depend on the user id of the active user. The user id is not easy to find in Moodle. Is there an API call to get the user ID for the active user?

phish108 commented 3 years ago

If one possesses the USERTOKEN, a known vector for the token owner should be also known. The best thing to use is the email address. The email address is normally a standard attribute with moodle systems. In rare cases it is possible no email is stored for the user. In these cases the user should use the users full name, which is shown in Moodle's User Menu. This may yield multiple matches for common names.

The user information is obtained by searching the user using the function core_user_get_users_by_field.

The API Call for getting the user id for a known vector such as the user's email is:

/webservice/rest/server.php?wstoken=<USERTOKEN>&wsfunction=core_user_get_users_by_field&moodlewsrestformat=json&field=email&values[0]=user@example.com

Call structure

Base URL: /webservice/rest/server.php?moodlewsrestformat=json&wstoken=${ USERTOKEN }

Attributes:

Result structure

The result is an array of user records or an empty array if no matches are found.

The search is successful if the any user is found. It MAY yield records for other users that match the lookup value.

The following attributes are only present if the USERTOKEN matches the selected user:

phish108 commented 3 years ago

Achtung: bei der Verwendung mit curl muss die URL in Einfache Anführungszeichen gesetzt werden!

phish108 commented 3 years ago

Ab moodle 4 ist es nicht mehr möglich alle Nutzer zu finden. So kann über die API für einen regulären Nutzer nicht mehr herausgefunden werden, ob ein anderer Nutzer existiert, solange es keine Peerverbindung (z.B. über einen Kurs) gibt.

phish108 commented 3 years ago

A much smarter variant for bootstrapping is core_webservice_get_site_info. It is parameterless and also reports the available functions for the user. This function only leaves out the user's email address, but this is OK, because Moodle uses the email only internally.